notifications.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. // Gasteizko Margolariak API v3 //
  3. //Posible notification target
  4. define('TARGET_ALL', 'all');
  5. define('TARGET_GM', 'gm');
  6. //Default target
  7. define('DEF_TARGET', TARGET_ALL);
  8. //$_GET valid parameters
  9. define('GET_TARGET', 'target');
  10. //Error messages
  11. define('ERR_TARGET', '-TARGET:');
  12. /*****************************************************
  13. * Initializes the session variables and connects to *
  14. * the db. *
  15. * *
  16. * @return: (MySQL server connection): The *
  17. * connection handler. *
  18. *****************************************************/
  19. function startdb(){
  20. //Include the db configuration file. It's somehow like this
  21. /*
  22. <?php
  23. $host = 'XXXX';
  24. $db_name = 'XXXX';
  25. $username_ro = 'XXXX';
  26. $username_rw = 'XXXX';
  27. $pass_ro = 'XXXX';
  28. $pass_rw = 'XXXX';
  29. ?>
  30. */
  31. include('../../.htpasswd');
  32. //Connect to to database
  33. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  34. //Set encoding options
  35. mysqli_set_charset($con, 'utf-8');
  36. header('Content-Type: text/html; charset=utf8');
  37. mysqli_query($con, 'SET NAMES utf8;');
  38. //Return the db connection
  39. return $con;
  40. }
  41. /*****************************************************
  42. * Discerns the type of notificatios to look for *
  43. * using the parameter 'target' from the list of get *
  44. * arguments. Valid values are 'all'or 'gm'. The *
  45. * default is 'all'. *
  46. * *
  47. * @params: *
  48. * get: (String array) List of GET parameters. *
  49. * @return: (String): 'all' if it was passed as GET *
  50. * parameter, 'gm' if it was passed or if *
  51. * the parameter was not passed, or null if *
  52. * some other value was passed. *
  53. *****************************************************/
  54. function get_target($get){
  55. $target = $get[GET_TARGET];
  56. if (strlen($target) < 1){
  57. return DEF_TARGET;
  58. }
  59. else{
  60. if ($target != TARGET_ALL && $target != TARGET_GM){
  61. return null;
  62. }
  63. else{
  64. return $target;
  65. }
  66. }
  67. }
  68. /*****************************************************
  69. * Retrieves the notificatios that are still on time *
  70. * to be delivered. *
  71. * *
  72. * @params: *
  73. * con: (MySQL server connection) Db connector. *
  74. * target: (String) 'gm' or 'all'. *
  75. * @return: (String): The list of notifications to *
  76. * be sent to the app, in JSON format, or *
  77. * null if there are none. *
  78. *****************************************************/
  79. function select_notifications($con, $target){
  80. if ($target == TARGET_GM){
  81. $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";
  82. }
  83. else{
  84. $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";
  85. }
  86. $q = mysqli_query($con, $query);
  87. if (mysqli_num_rows($q) == 0){
  88. return null;
  89. }
  90. else{
  91. $rows = array();
  92. while($r = mysqli_fetch_assoc($q)) {
  93. $rows[] = $r;
  94. }
  95. return(json_encode($rows));
  96. }
  97. }
  98. /*****************************************************
  99. * Prints the notifications. *
  100. * *
  101. * @params: *
  102. * notifications: (String) Notification list, in *
  103. * JSON format. *
  104. *****************************************************/
  105. function print_notifications($notifications){
  106. print($notifications);
  107. }
  108. // Connect to the database
  109. $con = startdb('rw');
  110. // Select target
  111. $target = get_target($_GET);
  112. if ($target == null){
  113. // Bad request
  114. http_response_code(400);
  115. exit(-1);
  116. }
  117. // Get notifications
  118. $notifications = select_notifications($con, $_GET[GET_TARGET]);
  119. if ($notifications == null){
  120. // No content
  121. http_response_code(204);
  122. }
  123. else{
  124. print_notifications($notifications);
  125. }
  126. ?>