sync.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. // Gasteizko Margolariak API v1 //
  3. //TODO: Register the call on the database.
  4. //List of available data formatting
  5. define('FOR_JSON', 'json');
  6. //Default info format
  7. define('DEF_FORMAT', FOR_JSON);
  8. //Database section identifiers
  9. define('SEC_ALL', 'all');
  10. define('SEC_BLOG', 'blog');
  11. define('SEC_ACTIVITIES', 'activities');
  12. define('SEC_GALLERY', 'gallery');
  13. define('SEC_LABLANCA', 'lablanca');
  14. //Posible actions
  15. define('ACTION_SYNC', 'sync');
  16. define('ACTION_VERSION', 'version');
  17. //$_GET valid parameters
  18. define('GET_CLIENT', 'client');
  19. define('GET_ACTION', 'action');
  20. define('GET_SECTION', 'section');
  21. define('GET_VERSION', 'version');
  22. define('GET_FOREGROUND', 'foreground');
  23. define('GET_FORMAT', 'json')
  24. /****************************************************
  25. * Echoes the version of one or more of the sections *
  26. * of the database, or the global section. *
  27. * *
  28. * @params: *
  29. * con: (MySQL server connection) RO mode enough. *
  30. * section: (string): 'blog', 'activities', *
  31. * 'gallery', 'lablanca', 'global'. None *
  32. * to get them all. *
  33. ****************************************************/
  34. function get_version($con, $section = SEC_ALL){
  35. if ($section == SEC_ALL){
  36. $q = mysqli_query($con, "SELECT SUM(version) AS version FROM version;");
  37. }
  38. else{
  39. $q = mysqli_query($con, "SELECT version FROM version WHERE section = '$section';");
  40. }
  41. if (mysqli_num_rows == 0){
  42. //'Bad request' status code
  43. var_dump(http_response_code(400));
  44. return 0;
  45. }
  46. else{
  47. $r = mysqli_fetch_array($q);
  48. return($r['version']);
  49. }
  50. }
  51. /****************************************************
  52. * Prepares the info of the database or a portion of *
  53. * it in the selected format. *
  54. * *
  55. * @params: *
  56. * con: (MySQL server connection) RO mode enough. *
  57. * section: (string): 'blog', 'activities', *
  58. * 'gallery', 'lablanca', 'global'. None *
  59. * to get them all. *
  60. * format: (int): 1: json (default). *
  61. ****************************************************/
  62. function sync($con, $section = SEC_ALL, $format = DEF_FORMAT){
  63. switch ($section){
  64. case SEC_BLOG:
  65. $tables = [ 'post', 'post_comment', 'post_image', 'post_tag' ];
  66. break;
  67. case SEC_ACTIVITIES:
  68. $tables = [ 'activity', 'activity_comment', 'activity_image', 'activity_tag', 'activity_itinerary', 'location', 'album', 'photo', 'photo_album', 'photo_comment', 'place' ];
  69. break;
  70. case SEC_GALLERY:
  71. $tables = [ 'album', 'photo', 'photo_album', 'photo_comment', 'place' ];
  72. break;
  73. case SEC_LABLANCA:
  74. $tables = [ 'festival', 'festival_day', 'festival_event', 'festival_event_image', 'festival_offer', 'place', 'people' ];
  75. break;
  76. case SEC_ALL:
  77. $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' ];
  78. break;
  79. default:
  80. //'Bad request' staus code
  81. var_dump(http_response_code(400));
  82. return;
  83. }
  84. switch ($format){
  85. case FOR_JSON:
  86. $db = array();
  87. foreach($tables as $table){
  88. $db[] = [ $table => get_table($con, $table, $format) ];
  89. }
  90. return(json_encode($db));
  91. break;
  92. }
  93. }
  94. /****************************************************
  95. * Echoes the contents of a table from the database. *
  96. * Inaccessible or sensitive tables or fields are *
  97. * not printed. *
  98. * *
  99. * @params: *
  100. * con: (MySQL server connection) RO mode enough. *
  101. * table (string): The name of the table. *
  102. ****************************************************/
  103. function get_table($con, $table){
  104. $table = strtolower($table);
  105. switch ($table){
  106. case "activity":
  107. $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;");
  108. break;
  109. case "activity_comment":
  110. $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;");
  111. break;
  112. case "album":
  113. $q = mysqli_query($con, "SELECT id, permalink, title_es, title_en, title_eu, description_es, description_en, description_eu, open;");
  114. break;
  115. case "photo":
  116. $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;");
  117. break;
  118. case "post":
  119. $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;");
  120. break;
  121. case "post_comment":
  122. $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;");
  123. break;
  124. //Other cases:
  125. default:
  126. //If the table is a public one and has not been listed above, all of its fields are public.
  127. 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'])){
  128. $q = mysqli_query($con, "SELECT * FROM $table;");
  129. }
  130. //If forbidden table
  131. else{
  132. //'Forbidden' status code
  133. var_dump(http_response_code(403));
  134. return;
  135. }
  136. }
  137. //Create result array
  138. $rows = array();
  139. while($r = mysqli_fetch_assoc($q)) {
  140. $rows[] = $r;
  141. }
  142. return $rows;
  143. }
  144. //Connect to the database
  145. $con = startdb('rw');
  146. //Get data from URL
  147. $client = mysqli_real_escape_string($con, $_GET[GET_CLIENT]);
  148. $action = strtolower(mysqli_real_escape_string($con, $_GET[GET_ACTION]));
  149. $section = strtolower(mysqli_real_escape_string($con, $_GET[GET_SECTION]));
  150. $version = mysqli_real_escape_string($con, $_GET[GET_VERSION]);
  151. $foregroud = mysqli_real_escape_string($con, $_GET[GET_FOREGROUND]);
  152. $format = strtolower(mysqli_real_escape_string($con, $_GET[GET_FORMAT]));
  153. //Validate data
  154. if (strlen($client) < 1){
  155. $client = '';
  156. }
  157. if ($action != ACTION_SYNC && $action != ACTION_VERSION){
  158. //Bad request
  159. var_dump(http_response_code(400));
  160. exit(-1);
  161. }
  162. if (strlen($section) > 0 && $section != SEC_ALL && $section != SEC_BLOG && $section != SEC_ACTIVITIES && $section != SEC_GALLERY && $section != SEC_LABLANCA ){
  163. //Bad request
  164. var_dump(http_response_code(400));
  165. exit(-1);
  166. }
  167. if (strlen($section) == 0){
  168. $section = SEC_ALL;
  169. }
  170. if (is_int($version) == false){
  171. //Bad request
  172. var_dump(http_response_code(400));
  173. exit(-1);
  174. }
  175. if (strlen($foregroud) < 1){
  176. $foreground = 1;
  177. }
  178. if ($foreground != 0 && $foregroud != 0){
  179. //Bad request
  180. var_dump(http_response_code(400));
  181. exit(-1);
  182. }
  183. if (strlen($format) < 1){
  184. $format = DEF_FORMAT;
  185. }
  186. if ($format != FOR_JSON){
  187. //Bad request
  188. var_dump(http_response_code(400));
  189. exit(-1);
  190. }
  191. //If the client just needs to know the version number
  192. if ($action == ACTION_VERSION){
  193. echo (get_version($con, $section));
  194. }
  195. //If the clients wants to actually perform a sync
  196. else{
  197. //If the client version is up to date, send a no content status
  198. if ($version >= get_version($con, $section)){
  199. //No content
  200. var_dump(http_response_code(204));
  201. exit(0);
  202. }
  203. //If the client needs an update
  204. else{
  205. echo(sync($con, $section));
  206. }
  207. }
  208. ?>