notifications.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Gasteizko Margolariak API v3 - Location
  4. *
  5. * Used to get notifications.
  6. * This file is to be called directly from a URL request.
  7. *
  8. * https://margolariak.com/API/v3/help/
  9. *
  10. * @since 1.0.0
  11. */
  12. //Posible notification target
  13. define('TARGET_ALL', 'all');
  14. define('TARGET_GM', 'gm');
  15. //Default target
  16. define('DEF_TARGET', TARGET_ALL);
  17. //$_GET valid parameters
  18. define('GET_TARGET', 'target');
  19. //Error messages
  20. define('ERR_TARGET', '-TARGET:');
  21. /**
  22. * Initializes the MySQL database connection.
  23. *
  24. * Called at the beggining of the script. It connects to the database using the
  25. * parameters in the .htpasswd file. It also sets database and page encodings.
  26. *
  27. * @since 1.0.0
  28. * @return object Database connection.
  29. */
  30. function startdb(){
  31. //Include the db configuration file. It's somehow like this
  32. /*
  33. <?php
  34. $host = 'XXXX';
  35. $db_name = 'XXXX';
  36. $username_ro = 'XXXX';
  37. $username_rw = 'XXXX';
  38. $pass_ro = 'XXXX';
  39. $pass_rw = 'XXXX';
  40. ?>
  41. */
  42. include('../../.htpasswd');
  43. //Connect to to database
  44. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  45. //Set encoding options
  46. mysqli_set_charset($con, 'utf-8');
  47. header('Content-Type: text/html; charset=utf8');
  48. mysqli_query($con, 'SET NAMES utf8;');
  49. //Return the db connection
  50. return $con;
  51. }
  52. /**
  53. * Discerns the type of notifications requested.
  54. *
  55. * Determines which notificatios the request is looking for: public
  56. * notification or members-only notifications. To do so, it looks for a
  57. * request parameter keyed 'target'
  58. *
  59. * @since 3.0.0
  60. * @param array $get Array with the request parameters.
  61. * @return string 'all' or 'gm' if it was passed as the 'target' GET
  62. * parameter. 'gm' if there was no 'target' parameter or
  63. * null if some invalid value was passed as the 'target'
  64. * parameter.
  65. */
  66. function get_target($get){
  67. $target = $get[GET_TARGET];
  68. if (strlen($target) < 1){
  69. return DEF_TARGET;
  70. }
  71. else{
  72. if ($target != TARGET_ALL && $target != TARGET_GM){
  73. return null;
  74. }
  75. else{
  76. return $target;
  77. }
  78. }
  79. }
  80. /**
  81. * Retrieves the notifications
  82. *
  83. * Retrieves the notificatios for the selected target that are still on
  84. * time to be delivered and formats them as a JSON object.
  85. *
  86. * @since 3.0.0
  87. * @param object $con Open database connection.
  88. * @param string $target Optional. 'gm' or 'all'. Default is 'all'.
  89. * @return string JSON object with the fetched notifications.
  90. */
  91. function select_notifications($con, $target = TARGET_ALL){
  92. if ($target == TARGET_GM){
  93. $query = "SELECT id, title_es, title_en, title_eu, text_es, text_en, text_eu, dtime, internal AS gm, duration, action, 0 AS seen FROM notification WHERE internal = 1 AND dtime > NOW() - INTERVAL duration MINUTE ORDER BY dtime DESC";
  94. }
  95. else{
  96. $query = "SELECT id, title_es, title_en, title_eu, text_es, text_en, text_eu, dtime, internal AS gm, duration, action, 0 AS seen FROM notification WHERE dtime > NOW() - INTERVAL duration MINUTE ORDER BY dtime DESC";
  97. }
  98. $q = mysqli_query($con, $query);
  99. if (mysqli_num_rows($q) == 0){
  100. return null;
  101. }
  102. else{
  103. $rows = array();
  104. while($r = mysqli_fetch_assoc($q)) {
  105. $rows[] = $r;
  106. }
  107. return(json_encode($rows));
  108. }
  109. }
  110. /**
  111. * Prints the notifications.
  112. *
  113. * @since 3.0.0
  114. * @param string $notifications JSON object with the notifications,
  115. * as generated by
  116. * {@see select_notifications($con, $target)}
  117. */
  118. function print_notifications($notifications){
  119. echo($notifications);
  120. }
  121. // SCRIPT START
  122. // Connect to the database
  123. $con = startdb('rw');
  124. // Select target
  125. $target = get_target($_GET);
  126. if ($target == null){
  127. // Bad request
  128. http_response_code(400);
  129. exit(-1);
  130. }
  131. // Get notifications
  132. $notifications = select_notifications($con, $_GET[GET_TARGET]);
  133. if ($notifications == null){
  134. // No content
  135. http_response_code(204);
  136. }
  137. else{
  138. print_notifications($notifications);
  139. }
  140. ?>