sendnotification.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. include("../functions.php");
  3. $con = startdb('rw');
  4. // $_GET valid parameters
  5. define('GET_USER', 'user');
  6. define('GET_PASS', 'pass');
  7. define('GET_TITLE_ES', 'title_es');
  8. define('GET_TITLE_EN', 'title_en');
  9. define('GET_TITLE_EU', 'title_eu');
  10. define('GET_TEXT_ES', 'text_es');
  11. define('GET_TEXT_EN', 'text_en');
  12. define('GET_TEXT_EU', 'text_eu');
  13. define('GET_DURATION', 'duration');
  14. define('GET_ACTION', 'action');
  15. define('GET_PERM', 'permalink');
  16. define('GET_ID', 'id');
  17. define('GET_GM', 'gm');
  18. // Valid values
  19. define('ACTION_TEXT', 'mensaje');
  20. define('ACTION_BLOG', 'blog');
  21. define('ACTION_ACTIVITIES', 'actividades');
  22. define('ACTION_GALLERY', 'galeria');
  23. define('ACTION_LOCALIZACION', 'localizacion');
  24. define('ACTION_LABLANCA', 'lablanca');
  25. define('ACTION_SCHEDULE', 'programa');
  26. define('ACTION_GM_SCHEDULE', 'gprograma');
  27. define('ACTION_US', 'nosotros');
  28. $actions = [ACTION_TEXT, ACTION_BLOG, ACTION_ACTIVITIES, ACTION_GALLERY, ACTION_LOCALIZATION, ACTION_LABLANCA, ACTION_SCHEDULE, ACTION_GM_SCHEDULE, ACTION_US];
  29. // Default values
  30. define('DEF_GM', 0);
  31. define('DEF_ACTION', ACTION_TEXT);
  32. // Error messages
  33. define('ERR_USER', '-USER:');
  34. define('ERR_ACTION', '-ACTION:');
  35. define('ERR_TITLE', '-TITLE:');
  36. define('ERR_TEXT', '-TEXT:');
  37. define('ERR_DURATION', '-DURATION:');
  38. define('ERR_GM', '-GM:');
  39. define('ERR_PERM', '-PERM:');
  40. define('ERR_ID', '-ID:');
  41. /****************************************************
  42. * This function is called from almost everywhere at *
  43. * the beggining of the page. It initializes the *
  44. * session variables, connect to the db, enabling *
  45. * the variable $con for futher use everywhere in *
  46. * the php code, and populates the arrays $user *
  47. * and $permission, with info about the user. *
  48. * *
  49. * @return: (db connection): The connection handler. *
  50. ****************************************************/
  51. function startdb(){
  52. //Include the db configuration file. It's somehow like this
  53. /*
  54. <?php
  55. $host = 'XXXX';
  56. $db_name = 'XXXX';
  57. $username_ro = 'XXXX';
  58. $username_rw = 'XXXX';
  59. $pass_ro = 'XXXX';
  60. $pass_rw = 'XXXX';
  61. ?>
  62. */
  63. include('../../.htpasswd');
  64. //Connect to to database
  65. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  66. //Set encoding options
  67. mysqli_set_charset($con, 'utf-8');
  68. header('Content-Type: text/html; charset=utf8');
  69. mysqli_query($con, 'SET NAMES utf8;');
  70. //Return the db connection
  71. return $con;
  72. }
  73. // Get fields
  74. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  75. $pass = mysqli_real_escape_string($con, $_GET[GET_PASS]);
  76. $title_es = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_ES]));
  77. $title_en = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_EN]));
  78. $title_eu = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_EU]));
  79. $text_es = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_ES]));
  80. $text_en = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_EN]));
  81. $text_eu = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_EU]));
  82. $duration = mysqli_real_escape_string($con, $_GET[GET_DURATION]);
  83. $action = mysqli_real_escape_string($con, $_GET[GET_ACTION]);
  84. $id = mysqli_real_escape_string($con, $_GET[GET_ID]);
  85. $perm = mysqli_real_escape_string($con, $_GET[GET_PERM]);
  86. $gm = mysqli_real_escape_string($con, $_GET[GET_GM]);
  87. // Error control
  88. $error = '';
  89. // TODO: Validate user/pass
  90. $q = mysqli_query($con, "SELECT id FROM user WHERE id = $user AND md5(concat(password, md5(salt))) = '$pass'");
  91. if (mysqli_num_rows($q) == 0){
  92. error_log(":SECURITY: Reporting location with wrong credentials (IP $_SERVER[REMOTE_ADDR])");
  93. http_response_code(403); // Forbidden
  94. $error = $error . ERR_TARGET . mysqli_real_escape_string($con, $_GET[GET_TARGET]);
  95. }
  96. //Validate fields
  97. if (strlen($title_es) == 0){
  98. http_response_code(400); // Bad request
  99. $error = $error . ERR_TITLE . $title_es;
  100. }
  101. if (strlen($text_es) == 0){
  102. http_response_code(400); // Bad request
  103. $error = $error . ERR_TEXT . $text_es;
  104. }
  105. if (is_numeric($duration) == false || $duration < 1 && $duration > 48 * 60){
  106. http_response_code(400); // Bad request
  107. $error = $error . ERR_DURATION . $duration;
  108. }
  109. if (!in_array($action, $actions){
  110. http_response_code(400); // Bad request
  111. $error = $error . ERR_ACTION . $action;
  112. }
  113. //Handle translations
  114. if (strlen($title_en) == 0){
  115. $title_en = $title_es;
  116. }
  117. if (strlen($title_eu) == 0){
  118. $title_eu = $title_es;
  119. }
  120. if (strlen($text_en) == 0){
  121. $text_en = $text_es;
  122. }
  123. if (strlen($text_eu) == 0){
  124. $text_eu = $text_es;
  125. }
  126. //Insert
  127. if (strlen($error) == 0){
  128. mysqli_query($con, "INSERT INTO notification (user, title_es, title_en, title_eu, text_es, text_en, text_eu, action, duration) VALUES ($user, '$title_es', '$title_en', '$title_eu', '$text_es', '$text_en', '$text_eu', '$action', $duration);");
  129. http_response_code(204) // No content;
  130. else{
  131. error_log($error);
  132. }
  133. ?>