notifications.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, 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, 0 AS seen FROM notification WHERE dtime > NOW() - INTERVAL duration MINUTE ORDER BY dtime DESC";
  58. }
  59. $q = mysqli_query($con, $query);
  60. switch ($format){
  61. case FOR_JSON:
  62. //Create result array
  63. $rows = array();
  64. while($r = mysqli_fetch_assoc($q)) {
  65. $rows[] = $r;
  66. }
  67. return(json_encode($rows));
  68. break;
  69. }
  70. }
  71. //Connect to the database
  72. $con = startdb('rw');
  73. //Get data from URL
  74. $client = mysqli_real_escape_string($con, $_GET[GET_CLIENT]);
  75. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  76. $target = strtolower(mysqli_real_escape_string($con, $_GET[GET_TARGET]));
  77. $format = strtolower(mysqli_real_escape_string($con, $_GET[GET_FORMAT]));
  78. //Initialize some variables
  79. $error = '';
  80. //Validate data
  81. if (strlen($client) < 1){
  82. $client = '';
  83. }
  84. if (strlen($user) < 1){
  85. $user = '';
  86. }
  87. if (strlen($target) < 1){
  88. $target = DEF_TARGET;
  89. }
  90. if ($target != TARGET_ALL && $action != TARGET_GM){
  91. //Bad request
  92. http_response_code(400);
  93. $error = $error . ERR_TARGET . mysqli_real_escape_string($con, $_GET[GET_TARGET]);
  94. }
  95. if (strlen($format) < 1){
  96. $format = DEF_FORMAT;
  97. }
  98. if ($format != FOR_JSON){
  99. //Bad request
  100. http_response_code(400);
  101. $error = $error . ERR_FORMAT . mysqli_real_escape_string($con, $_GET[GET_FORMAT]);
  102. }
  103. //If there has not been an error, procede
  104. if (strlen($error) == 0){
  105. echo(show_notifications($con, $target, $format));
  106. }
  107. //Log the request
  108. //log_sync(); //TODO;