sendnotification.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // Gasteizko Margolariak API v3 //
  3. // $_GET and $_POST valid parameters
  4. define('POST_USER', 'user');
  5. define('POST_PASS', 'pass');
  6. define('GET_TITLE_ES', 'title_es');
  7. define('GET_TITLE_EN', 'title_en');
  8. define('GET_TITLE_EU', 'title_eu');
  9. define('GET_TEXT_ES', 'text_es');
  10. define('GET_TEXT_EN', 'text_en');
  11. define('GET_TEXT_EU', 'text_eu');
  12. define('GET_DURATION', 'duration');
  13. define('GET_ACTION', 'action');
  14. define('GET_PERM', 'permalink');
  15. define('GET_ID', 'id');
  16. define('GET_GM', 'gm');
  17. // Valid values
  18. define('ACTION_TEXT', 'text');
  19. define('ACTION_BLOG', 'blog');
  20. define('ACTION_ACTIVITIES', 'activities');
  21. define('ACTION_GALLERY', 'gallery');
  22. define('ACTION_LOCATION', 'location');
  23. define('ACTION_LABLANCA', 'lablanca');
  24. define('ACTION_SCHEDULE_CITY', 'schedule_city');
  25. define('ACTION_SCHEDULE_GM', 'schedule_gm');
  26. define('ACTION_US', 'us');
  27. $actions = [ACTION_TEXT, ACTION_BLOG, ACTION_ACTIVITIES, ACTION_GALLERY, ACTION_LOCATION, ACTION_LABLANCA, ACTION_SCHEDULE_CITY, ACTION_SCHEDULE_GM, ACTION_US];
  28. // Default values
  29. define('DEF_GM', 0);
  30. define('DEF_ACTION', ACTION_TEXT);
  31. // Error messages
  32. define('ERR_USER', '-USER:');
  33. define('ERR_ACTION', '-ACTION:');
  34. define('ERR_TITLE', '-TITLE:');
  35. define('ERR_TEXT', '-TEXT:');
  36. define('ERR_DURATION', '-DURATION:');
  37. define('ERR_GM', '-GM:');
  38. define('ERR_PERM', '-PERM:');
  39. define('ERR_ID', '-ID:');
  40. include('functions.php');
  41. $con = startdb('rw');
  42. // Get fields
  43. $user = mysqli_real_escape_string($con, $_POST[POST_USER]);
  44. $pass = mysqli_real_escape_string($con, $_POST[POST_PASS]);
  45. $title_es = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_ES]));
  46. $title_en = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_EN]));
  47. $title_eu = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_EU]));
  48. $text_es = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_ES]));
  49. $text_en = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_EN]));
  50. $text_eu = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_EU]));
  51. $duration = mysqli_real_escape_string($con, $_GET[GET_DURATION]);
  52. $action = mysqli_real_escape_string($con, $_GET[GET_ACTION]);
  53. $id = mysqli_real_escape_string($con, $_GET[GET_ID]);
  54. $perm = mysqli_real_escape_string($con, $_GET[GET_PERM]);
  55. $gm = mysqli_real_escape_string($con, $_GET[GET_GM]);
  56. // Error control
  57. $error = "";
  58. // Validate user/pass
  59. $uid = login($con, $user, $pass);
  60. if ($uid == -1){
  61. error_log(":SECURITY: Reporting location with wrong credentials (IP $_SERVER[REMOTE_ADDR])");
  62. $error = $error . ERR_USER . mysqli_real_escape_string($con, $_POST[POST_USER]);
  63. error_log($error);
  64. http_response_code(403); // Forbidden
  65. exit(-1);
  66. }
  67. //Validate fields
  68. if (strlen($title_es) == 0){
  69. http_response_code(400); // Bad request
  70. $error = $error . ERR_TITLE . $title_es;
  71. error_log($error);
  72. exit(-2);
  73. }
  74. if (strlen($text_es) == 0){
  75. http_response_code(400); // Bad request
  76. $error = $error . ERR_TEXT . $text_es;
  77. error_log($error);
  78. exit(-3);
  79. }
  80. if (is_numeric($duration) == false || $duration < 1 && $duration > 48 * 60){
  81. http_response_code(400); // Bad request
  82. $error = $error . ERR_DURATION . $duration;
  83. error_log($error);
  84. exit(-4);
  85. }
  86. if (!in_array($action, $actions)){
  87. http_response_code(400); // Bad request
  88. $error = $error . ERR_ACTION . $action;
  89. error_log($error);
  90. exit(-5);
  91. }
  92. //Handle translations
  93. if (strlen($title_en) == 0){
  94. $title_en = $title_es;
  95. }
  96. if (strlen($title_eu) == 0){
  97. $title_eu = $title_es;
  98. }
  99. if (strlen($text_en) == 0){
  100. $text_en = $text_es;
  101. }
  102. if (strlen($text_eu) == 0){
  103. $text_eu = $text_es;
  104. }
  105. //Insert
  106. if (strlen($error) == 0){
  107. mysqli_query($con, "INSERT INTO notification (user, title_es, title_en, title_eu, text_es, text_en, text_eu, action, duration) VALUES ($_SESSION[id], '$title_es', '$title_en', '$title_eu', '$text_es', '$text_en', '$text_eu', '$action', $duration);");
  108. error_log("INSERT INTO notification (user, title_es, title_en, title_eu, text_es, text_en, text_eu, action, duration) VALUES ($_SESSION[id], '$title_es', '$title_en', '$title_eu', '$text_es', '$text_en', '$text_eu', '$action', $duration);");
  109. http_response_code(204); // No content;
  110. exit(0);
  111. }
  112. else{
  113. http_response_code(400); // Bad request
  114. error_log($error);
  115. exit(-6);
  116. }
  117. ?>