notifications.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Optional. Array with the request parameters. Default
  61. * is $_GET.
  62. * @return string 'all' or 'gm' if it was passed as the 'target' GET
  63. * parameter. 'gm' if there was no 'target' parameter or
  64. * null if some invalid value was passed as the 'target'
  65. * parameter.
  66. */
  67. function get_target($get = $_GET){
  68. $target = $get[GET_TARGET];
  69. if (strlen($target) < 1){
  70. return DEF_TARGET;
  71. }
  72. else{
  73. if ($target != TARGET_ALL && $target != TARGET_GM){
  74. return null;
  75. }
  76. else{
  77. return $target;
  78. }
  79. }
  80. }
  81. /**
  82. * Retrieves the notifications
  83. *
  84. * Retrieves the notificatios for the selected target that are still on
  85. * time to be delivered and formats them as a JSON object.
  86. *
  87. * @since 3.0.0
  88. * @param object $con Open database connection.
  89. * @param string $target Optional. 'gm' or 'all'. Default is 'all'.
  90. * @return string JSON object with the fetched notifications.
  91. */
  92. function select_notifications($con, $target = TARGET_ALL){
  93. if ($target == TARGET_GM){
  94. $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";
  95. }
  96. else{
  97. $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";
  98. }
  99. $q = mysqli_query($con, $query);
  100. if (mysqli_num_rows($q) == 0){
  101. return null;
  102. }
  103. else{
  104. $rows = array();
  105. while($r = mysqli_fetch_assoc($q)) {
  106. $rows[] = $r;
  107. }
  108. return(json_encode($rows));
  109. }
  110. }
  111. /**
  112. * Prints the notifications.
  113. *
  114. * @since 3.0.0
  115. * @param string $notifications JSON object with the notifications,
  116. * as generated by
  117. * {@see select_notifications($con, $target)}
  118. */
  119. function print_notifications($notifications){
  120. echo($notifications);
  121. }
  122. // SCRIPT START
  123. // Connect to the database
  124. $con = startdb('rw');
  125. // Select target
  126. $target = get_target($_GET);
  127. if ($target == null){
  128. // Bad request
  129. http_response_code(400);
  130. exit(-1);
  131. }
  132. // Get notifications
  133. $notifications = select_notifications($con, $_GET[GET_TARGET]);
  134. if ($notifications == null){
  135. // No content
  136. http_response_code(204);
  137. }
  138. else{
  139. print_notifications($notifications);
  140. }
  141. ?>