sync.php 8.9 KB

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