sync.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // Gasteizko Margolariak API v1 //
  3. //TODO: Include another JSON structure with database section versions.
  4. //TODO: Read and parse $_GET params.
  5. //TODO: Register the call on the database.
  6. //List of available data formatting
  7. define('FOR_JSON', 1);
  8. //Default info format
  9. define('DEF_FORMAT', 1);
  10. //Database section identifiers
  11. define('SEC_ALL', 0);
  12. define('SEC_BLOG', 1);
  13. define('SEC_ACTIVITIES', 2);
  14. define('SEC_GALLERY', 3);
  15. define('SEC_LABLANCA', 4);
  16. /****************************************************
  17. * Echoes the version of one or more of the sections *
  18. * of the database, or the global section. *
  19. * *
  20. * @params: *
  21. * con: (MySQL server connection) RO mode enough. *
  22. * section: (string): 'blog', 'activities', *
  23. * 'gallery', 'lablanca', 'global'. None *
  24. * to get them all. *
  25. ****************************************************/
  26. function get_version($con, $section = SEC_ALL){
  27. if ($section == SEC_ALL){
  28. $q = mysqli_query($con, "SELECT SUM(version) AS version FROM version;");
  29. }
  30. else{
  31. $q = mysqli_query($con, "SELECT version FROM version WHERE section = '$section';");
  32. }
  33. if (mysqli_num_rows == 0){
  34. //'Bad request' status code
  35. var_dump(http_response_code(400));
  36. return 0;
  37. }
  38. else{
  39. $r = mysqli_fetch_array($q);
  40. return($r['version']);
  41. }
  42. }
  43. /****************************************************
  44. * Prepares the info of the database or a portion of *
  45. * it in the selected format. *
  46. * *
  47. * @params: *
  48. * con: (MySQL server connection) RO mode enough. *
  49. * section: (string): 'blog', 'activities', *
  50. * 'gallery', 'lablanca', 'global'. None *
  51. * to get them all. *
  52. * format: (int): 1: json (default). *
  53. ****************************************************/
  54. function sync($con, $section = SEC_ALL, $version = 0, $format = DEF_FORMAT){
  55. //Skip if current version equal or higher than the one in the database.
  56. if ($version >= get_version($con, $section)){
  57. //'No content' status code
  58. var_dump(http_response_code(204));
  59. return;
  60. }
  61. switch ($section){
  62. case SEC_BLOG:
  63. $tables = [ 'post', 'post_comment', 'post_image', 'post_tag' ];
  64. break;
  65. case SEC_ACTIVITIES:
  66. $tables = [ 'activity', 'activity_comment', 'activity_image', 'activity_tag', 'activity_itinerary', 'location', 'album', 'photo', 'photo_album', 'photo_comment', 'place' ];
  67. break;
  68. case SEC_GALLERY:
  69. $tables = [ 'album', 'photo', 'photo_album', 'photo_comment', 'place' ];
  70. break;
  71. case SEC_LABLANCA:
  72. $tables = [ 'festival', 'festival_day', 'festival_event', 'festival_event_image', 'festival_offer', 'place', 'people' ];
  73. break;
  74. case SEC_ALL:
  75. $tables = [ 'activity', 'activity_comment', 'activity_image', 'activity_tag', 'album', 'photo', 'festival', 'festival_day', 'festival_event', 'festival_event_image', 'festival_offer', 'place', 'post', 'post_comment', 'post_image', 'post_tag', 'settings', 'sponsor' ];
  76. break;
  77. default:
  78. //'Bad request' staus code
  79. var_dump(http_response_code(400));
  80. return;
  81. }
  82. switch ($format){
  83. case FOR_JSON:
  84. $db = array();
  85. foreach($tables as $table){
  86. $db[] = [ $table => get_table($con, $table, $format) ];
  87. }
  88. return(json_encode($db));
  89. break;
  90. }
  91. }
  92. /****************************************************
  93. * Echoes the contents of a table from the database. *
  94. * Inaccessible or sensitive tables or fields are *
  95. * not printed. *
  96. * *
  97. * @params: *
  98. * con: (MySQL server connection) RO mode enough. *
  99. * table (string): The name of the table. *
  100. ****************************************************/
  101. function get_table($con, $table){
  102. $table = strtolower($table);
  103. switch ($table){
  104. case "activity":
  105. $q = mysqli_query($con, "SELECT id, permalink, date, city, title_es, title_en, title_eu, text_es, text_eu, text_en, after_es, after_en, after_eu, price, inscription, max_people, album FROM activity WHERE visible = 1;");
  106. break;
  107. case "activity_comment":
  108. $q = mysqli_query($con, "SELECT activity_comment.id AS id, activity, text, dtime, CONCAT(user.username, user) AS user, lang FROM activity_comment, user WHERE activity_comment.user = user.id AND approved = 1;");
  109. break;
  110. case "album":
  111. $q = mysqli_query($con, "SELECT id, permalink, title_es, title_en, title_eu, description_es, description_en, description_eu, open;");
  112. break;
  113. case "photo":
  114. $q = mysqli_query($con, "SELECT photo.id AS id, file, permalink, text_es, text_en, text_eu, description_es, description_en, description_eu, uploaded, place, width, height, size, CONCAT(username, user) AS user FROM photo, user WHERE user.id = photo.user AND approved = 1;");
  115. break;
  116. case "post":
  117. $q = mysqli_query($con, "SELECT post.id AS id, permalink, title_es, title_en, title_eu, text_es, text_en, text_eu, username, dtime FROM post, user WHERE user.id = user AND visible = 1;");
  118. break;
  119. case "post_comment":
  120. $q = mysqli_query($con, "SELECT post_comment.id AS id, post, text, dtime, CONCAT(user.username, user) AS user, lang FROM post_comment, user WHERE post_comment.user = user.id AND approved = 1;");
  121. break;
  122. //Other cases:
  123. default:
  124. //If the table is a public one and has not been listed above, all of its fields are public.
  125. if (in_array($table, ['activity_image', 'activity_tag', 'festival', 'festival_day', 'festival_event', 'festival_event_image', 'festival_offer', 'place', 'post_image', 'post_tag', 'settings', 'sponsor'])){
  126. $q = mysqli_query($con, "SELECT * FROM $table;");
  127. }
  128. //If forbidden table
  129. else{
  130. //'Forbidden' status code
  131. var_dump(http_response_code(403));
  132. return;
  133. }
  134. }
  135. //Create result array
  136. $rows = array();
  137. while($r = mysqli_fetch_assoc($q)) {
  138. $rows[] = $r;
  139. }
  140. return $rows;
  141. //Print array
  142. //return(json_encode($rows));
  143. }
  144. //TEST: Remove when tested
  145. //include("../../functions.php");
  146. //$con = startdb('ro');
  147. //echo(get_table($con, 'post'));
  148. //echo(sync($con, SEC_ALL));
  149. ?>