sendnotification.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // Gasteizko Margolariak API v3 //
  3. // $_GET valid parameters
  4. define('GET_USER', 'user');
  5. define('GET_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', 'mensaje');
  19. define('ACTION_BLOG', 'blog');
  20. define('ACTION_ACTIVITIES', 'actividades');
  21. define('ACTION_GALLERY', 'galeria');
  22. define('ACTION_LOCALIZATION', 'localizacion');
  23. define('ACTION_LABLANCA', 'lablanca');
  24. define('ACTION_SCHEDULE', 'programa');
  25. define('ACTION_GM_SCHEDULE', 'gprograma');
  26. define('ACTION_US', 'nosotros');
  27. $actions = [ACTION_TEXT, ACTION_BLOG, ACTION_ACTIVITIES, ACTION_GALLERY, ACTION_LOCALIZATION, ACTION_LABLANCA, ACTION_SCHEDULE, ACTION_GM_SCHEDULE, 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();
  42. // Get fields
  43. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  44. $title_es = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_ES]));
  45. $title_en = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_EN]));
  46. $title_eu = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_EU]));
  47. $text_es = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_ES]));
  48. $text_en = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_EN]));
  49. $text_eu = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_EU]));
  50. $duration = mysqli_real_escape_string($con, $_GET[GET_DURATION]);
  51. $action = mysqli_real_escape_string($con, $_GET[GET_ACTION]);
  52. $id = mysqli_real_escape_string($con, $_GET[GET_ID]);
  53. $perm = mysqli_real_escape_string($con, $_GET[GET_PERM]);
  54. $gm = mysqli_real_escape_string($con, $_GET[GET_GM]);
  55. // Error control
  56. $error = "";
  57. // Validate user/pass
  58. if (!fastLogin($con)){
  59. error_log(":SECURITY: Reporting location with wrong credentials (IP $_SERVER[REMOTE_ADDR])");
  60. $error = $error . ERR_USER . mysqli_real_escape_string($con, $_POST[GET_USER]);
  61. error_log($error);
  62. http_response_code(403); // Forbidden
  63. exit(-1);
  64. }
  65. //Validate fields
  66. if (strlen($title_es) == 0){
  67. http_response_code(400); // Bad request
  68. $error = $error . ERR_TITLE . $title_es;
  69. error_log($error);
  70. exit(-2);
  71. }
  72. if (strlen($text_es) == 0){
  73. http_response_code(400); // Bad request
  74. $error = $error . ERR_TEXT . $text_es;
  75. error_log($error);
  76. exit(-3);
  77. }
  78. if (is_numeric($duration) == false || $duration < 1 && $duration > 48 * 60){
  79. http_response_code(400); // Bad request
  80. $error = $error . ERR_DURATION . $duration;
  81. error_log($error);
  82. exit(-4);
  83. }
  84. if (!in_array($action, $actions)){
  85. http_response_code(400); // Bad request
  86. $error = $error . ERR_ACTION . $action;
  87. error_log($error);
  88. exit(-5);
  89. }
  90. //Handle translations
  91. if (strlen($title_en) == 0){
  92. $title_en = $title_es;
  93. }
  94. if (strlen($title_eu) == 0){
  95. $title_eu = $title_es;
  96. }
  97. if (strlen($text_en) == 0){
  98. $text_en = $text_es;
  99. }
  100. if (strlen($text_eu) == 0){
  101. $text_eu = $text_es;
  102. }
  103. //Insert
  104. if (strlen($error) == 0){
  105. mysqli_query($con, "INSERT INTO notification (user, title_es, title_en, title_eu, text_es, text_en, text_eu, action, duration) VALUES ($uid, '$title_es', '$title_en', '$title_eu', '$text_es', '$text_en', '$text_eu', '$action', $duration);");
  106. http_response_code(204); // No content;
  107. exit(0);
  108. }
  109. else{
  110. error_log($error);
  111. exit(-6);
  112. }
  113. ?>