sync.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // Gasteizko Margolariak API v1 //
  3. define('DEF_FORMAT', 1);
  4. define('FOR_JSON', 1);
  5. define('SEC_ALL', 0);
  6. define('SEC_BLOG', 1);
  7. define('SEC_ACTIVITIES', 2);
  8. define('SEC_GALLERY', 3);
  9. define('SEC_LABLANCA', 4);
  10. /****************************************************
  11. * Echoes the version of one or more of the sections *
  12. * of the database, or the global section. *
  13. * *
  14. * @params: *
  15. * con: (MySQL server connection) RO mode enough. *
  16. * section: (string): 'blog', 'activities', *
  17. * 'gallery', 'lablanca', 'global'. None *
  18. * to get them all. *
  19. ****************************************************/
  20. function get_version($con, $section = SEC_ALL){
  21. //TODO
  22. return(30);
  23. }
  24. function sync($con, $section = SEC_ALL, $version = 0, $format = DEF_FORMAT){
  25. //Skip if current version equal or higher than the one in the database.
  26. if ($version >= get_version($con, $section)){
  27. //TODO: print something?
  28. return;
  29. }
  30. switch ($section){
  31. case SEC_BLOG:
  32. $tables = [ 'post', 'post_comment', 'post_image', 'post_tag' ];
  33. break;
  34. case SEC_ACTIVITIES:
  35. $tables = [ 'activity', 'activity_comment', 'activity_image', 'activity_tag', 'activity_itinerary', 'location', 'album', 'photo', 'photo_album', 'photo_comment', 'place' ];
  36. break;
  37. case SEC_GALLERY:
  38. $tables = [ 'album', 'photo', 'photo_album', 'photo_comment', 'place' ];
  39. break;
  40. case SEC_LABLANCA:
  41. $tables = [ 'festival', 'festival_day', 'festival_event', 'festival_event_image', 'festival_offer', 'place', 'people' ];
  42. break;
  43. case SEC_ALL:
  44. $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' ];
  45. break;
  46. default:
  47. return;
  48. }
  49. switch ($format){
  50. case FOR_JSON:
  51. $db = array();
  52. foreach($tables as $table){
  53. $db[] = [ $table => get_table($con, $table, $format) ];
  54. }
  55. return(json_encode($db)); //FIXME: Calling this here too escapes doblequotes
  56. break;
  57. }
  58. }
  59. /****************************************************
  60. * Echoes the contents of a table from the database. *
  61. * Inaccessible or sensitive tables or fields are *
  62. * not printed. *
  63. * *
  64. * @params: *
  65. * con: (MySQL server connection) RO mode enough. *
  66. * table (string): The name of the table. *
  67. * format: (int): 1: json (default). *
  68. ****************************************************/
  69. function get_table($con, $table, $format = DEF_FORMAT){
  70. //TODO: table to lowercase
  71. switch (table){
  72. case "activity":
  73. $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;");
  74. break;
  75. case "activity_comment":
  76. $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;");
  77. break;
  78. case "album":
  79. $q = mysqli_query($con, "SELECT id, permalink, title_es, title_en, title_eu, description_es, description_en, description_eu, open;");
  80. break;
  81. case "photo":
  82. $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;");
  83. break;
  84. case "post":
  85. $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;");
  86. break;
  87. case "post_comment":
  88. $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;");
  89. break;
  90. //Other cases:
  91. default:
  92. //If the table is a public one and has not been listed above, all of its fields are public.
  93. 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'])){
  94. $q = mysqli_query($con, "SELECT * FROM $table;");
  95. }
  96. //If forbidden table
  97. else{
  98. //TODO
  99. return;
  100. }
  101. }
  102. //Create result array
  103. $rows = array();
  104. while($r = mysqli_fetch_assoc($q)) {
  105. $rows[] = $r;
  106. }
  107. return $rows;
  108. //Print array
  109. //return(json_encode($rows));
  110. }
  111. //TODO: Remove when tested
  112. include("../../functions.php");
  113. $con = startdb('ro');
  114. //get_table($con, 'festival');
  115. echo(sync($con, SEC_ALL));
  116. ?>