notifications.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // Gasteizko Margolariak API v1 //
  3. //List of available data formatting
  4. define('FOR_JSON', 'json');
  5. //Default info format
  6. define('DEF_FORMAT', FOR_JSON);
  7. //Posible notification target
  8. define('TARGET_ALL', 'all');
  9. define('TARGET_GM', 'gm');
  10. //Default target
  11. define('DEF_TARGET', TARGET_ALL);
  12. //$_GET valid parameters
  13. define('GET_CLIENT', 'client');
  14. define('GET_USER', 'user');
  15. define('GET_TARGET', 'target');
  16. define('GET_FORMAT', 'json');
  17. //Error messages
  18. define('ERR_TARGET', '-TARGET:');
  19. define('ERR_FORMAT', '-FORMAT:');
  20. /****************************************************
  21. * This function is called from almost everywhere at *
  22. * the beggining of the page. It initializes the *
  23. * session variables, connect to the db, enabling *
  24. * the variable $con for futher use everywhere in *
  25. * the php code, and populates the arrays $user *
  26. * and $permission, with info about the user. *
  27. * *
  28. * @return: (db connection): The connection handler. *
  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. function show_notifications($con, $target = DEF_TARGET, $format = DEF_FORMAT){
  53. if ($target == TARGET_GM){
  54. $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";
  55. }
  56. else{
  57. $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";
  58. }
  59. $q = mysqli_query($con, $query);
  60. if (mysqli_num_rows($q) == 0){
  61. http_response_code(204);
  62. }
  63. else{
  64. switch ($format){
  65. case FOR_JSON:
  66. //Create result array
  67. $rows = array();
  68. while($r = mysqli_fetch_assoc($q)) {
  69. $rows[] = $r;
  70. }
  71. return(json_encode($rows));
  72. break;
  73. }
  74. }
  75. }
  76. //Connect to the database
  77. $con = startdb('rw');
  78. //Get data from URL
  79. $client = mysqli_real_escape_string($con, $_GET[GET_CLIENT]);
  80. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  81. $target = strtolower(mysqli_real_escape_string($con, $_GET[GET_TARGET]));
  82. $format = strtolower(mysqli_real_escape_string($con, $_GET[GET_FORMAT]));
  83. //Initialize some variables
  84. $error = '';
  85. //Validate data
  86. if (strlen($client) < 1){
  87. $client = '';
  88. }
  89. if (strlen($user) < 1){
  90. $user = '';
  91. }
  92. if (strlen($target) < 1){
  93. $target = DEF_TARGET;
  94. }
  95. if ($target != TARGET_ALL && $action != TARGET_GM){
  96. //Bad request
  97. http_response_code(400);
  98. $error = $error . ERR_TARGET . mysqli_real_escape_string($con, $_GET[GET_TARGET]);
  99. }
  100. if (strlen($format) < 1){
  101. $format = DEF_FORMAT;
  102. }
  103. if ($format != FOR_JSON){
  104. //Bad request
  105. http_response_code(400);
  106. $error = $error . ERR_FORMAT . mysqli_real_escape_string($con, $_GET[GET_FORMAT]);
  107. }
  108. //If there has not been an error, procede
  109. if (strlen($error) == 0){
  110. echo(show_notifications($con, $target, $format));
  111. }
  112. ?>