sendnotification.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_LOCALIZATION', '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. $con = startdb();
  74. // Get fields
  75. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  76. $pass = mysqli_real_escape_string($con, $_GET[GET_PASS]);
  77. $title_es = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_ES]));
  78. $title_en = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_EN]));
  79. $title_eu = urldecode(mysqli_real_escape_string($con, $_GET[GET_TITLE_EU]));
  80. $text_es = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_ES]));
  81. $text_en = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_EN]));
  82. $text_eu = urldecode(mysqli_real_escape_string($con, $_GET[GET_TEXT_EU]));
  83. $duration = mysqli_real_escape_string($con, $_GET[GET_DURATION]);
  84. $action = mysqli_real_escape_string($con, $_GET[GET_ACTION]);
  85. $id = mysqli_real_escape_string($con, $_GET[GET_ID]);
  86. $perm = mysqli_real_escape_string($con, $_GET[GET_PERM]);
  87. $gm = mysqli_real_escape_string($con, $_GET[GET_GM]);
  88. // Error control
  89. $error = "";
  90. // Validate user/pass
  91. $q = mysqli_query($con, "SELECT id FROM user WHERE (lower(username) = lower('$user') OR lower(email) = lower('$user')) AND password = sha1(concat('$pass', sha1(salt)))");
  92. if (mysqli_num_rows($q) == 0){
  93. error_log(":SECURITY: Reporting location with wrong credentials (IP $_SERVER[REMOTE_ADDR])");
  94. http_response_code(403); // Forbidden
  95. $error = $error . ERR_USER . mysqli_real_escape_string($con, $_GET[GET_USER]);
  96. error_log($error);
  97. exit(-1);
  98. }
  99. $r = mysqli_fetch_array($q);
  100. $uid = $r['id'];
  101. //Validate fields
  102. if (strlen($title_es) == 0){
  103. http_response_code(400); // Bad request
  104. $error = $error . ERR_TITLE . $title_es;
  105. error_log($error);
  106. exit(-2);
  107. }
  108. if (strlen($text_es) == 0){
  109. http_response_code(400); // Bad request
  110. $error = $error . ERR_TEXT . $text_es;
  111. error_log($error);
  112. exit(-3);
  113. }
  114. if (is_numeric($duration) == false || $duration < 1 && $duration > 48 * 60){
  115. http_response_code(400); // Bad request
  116. $error = $error . ERR_DURATION . $duration;
  117. error_log($error);
  118. exit(-4);
  119. }
  120. if (!in_array($action, $actions)){
  121. http_response_code(400); // Bad request
  122. $error = $error . ERR_ACTION . $action;
  123. error_log($error);
  124. exit(-5);
  125. }
  126. //Handle translations
  127. if (strlen($title_en) == 0){
  128. $title_en = $title_es;
  129. }
  130. if (strlen($title_eu) == 0){
  131. $title_eu = $title_es;
  132. }
  133. if (strlen($text_en) == 0){
  134. $text_en = $text_es;
  135. }
  136. if (strlen($text_eu) == 0){
  137. $text_eu = $text_es;
  138. }
  139. //Insert
  140. if (strlen($error) == 0){
  141. error_log("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);");
  142. 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);");
  143. http_response_code(204); // No content;
  144. exit(0);
  145. }
  146. else{
  147. error_log($error);
  148. exit(-6);
  149. }
  150. ?>