sync.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. //Default action
  17. define('DEF_ACTION', ACTION_SYNC);
  18. //$_GET valid parameters
  19. define('GET_CLIENT', 'client');
  20. define('GET_USER', 'user');
  21. define('GET_ACTION', 'action');
  22. define('GET_SECTION', 'section');
  23. define('GET_VERSION', 'version');
  24. define('GET_FOREGROUND', 'foreground');
  25. define('GET_FORMAT', 'json');
  26. //Error messages
  27. define('ERR_ACTION', '-ACTION:');
  28. define('ERR_SECTION', '-SECTION:');
  29. define('ERR_VERSION', '-VERSION:');
  30. define('ERR_FOREGROUND', '-FOREGROUND:');
  31. define('ERR_FORMAT', '-FORMAT:');
  32. /****************************************************
  33. * This function is called from almost everywhere at *
  34. * the beggining of the page. It initializes the *
  35. * session variables, connect to the db, enabling *
  36. * the variable $con for futher use everywhere in *
  37. * the php code, and populates the arrays $user *
  38. * and $permission, with info about the user. *
  39. * *
  40. * @return: (db connection): The connection handler. *
  41. ****************************************************/
  42. function startdb(){
  43. //Include the db configuration file. It's somehow like this
  44. /*
  45. <?php
  46. $host = 'XXXX';
  47. $db_name = 'XXXX';
  48. $username_ro = 'XXXX';
  49. $username_rw = 'XXXX';
  50. $pass_ro = 'XXXX';
  51. $pass_rw = 'XXXX';
  52. ?>
  53. */
  54. include('../../.htpasswd');
  55. //Connect to to database
  56. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  57. //Set encoding options
  58. mysqli_set_charset($con, 'utf-8');
  59. header('Content-Type: text/html; charset=utf8');
  60. mysqli_query($con, 'SET NAMES utf8;');
  61. //Return the db connection
  62. return $con;
  63. }
  64. /****************************************************
  65. * Echoes the version of one or more of the sections *
  66. * of the database, or the global section. *
  67. * *
  68. * @params: *
  69. * con: (MySQL server connection) RO mode enough. *
  70. * section: (string): 'blog', 'activities', *
  71. * 'gallery', 'lablanca', 'global'. None *
  72. * to get them all. *
  73. * @return: (int): The version of the db section. *
  74. ****************************************************/
  75. function get_version($con, $section = SEC_ALL){
  76. if ($section == SEC_ALL){
  77. $q = mysqli_query($con, "SELECT SUM(version) AS version FROM version;");
  78. }
  79. else{
  80. $q = mysqli_query($con, "SELECT version FROM version WHERE section = '$section';");
  81. }
  82. if (mysqli_num_rows == 0){
  83. //'Bad request' status code
  84. http_response_code(400);
  85. return 0;
  86. }
  87. else{
  88. $r = mysqli_fetch_array($q);
  89. return($r['version']);
  90. }
  91. }
  92. /****************************************************
  93. * Prepares the info of the database or a portion of *
  94. * it in the selected format. *
  95. * *
  96. * @params: *
  97. * con: (MySQL server connection) RO mode enough. *
  98. * section: (string): 'blog', 'activities', *
  99. * 'gallery', 'lablanca', 'global'. None *
  100. * to get them all. *
  101. * format: (int): 1: json (default). *
  102. * @return: (String): data in the desired format. *
  103. ****************************************************/
  104. function sync($con, $section = SEC_ALL, $format = DEF_FORMAT){
  105. switch ($section){
  106. case SEC_BLOG:
  107. $tables = [ 'post', 'post_comment', 'post_image', 'post_tag' ];
  108. break;
  109. case SEC_ACTIVITIES:
  110. $tables = [ 'activity', 'activity_comment', 'activity_image', 'activity_tag', 'activity_itinerary', 'location', 'album', 'photo', 'photo_album', 'photo_comment', 'place' ];
  111. break;
  112. case SEC_GALLERY:
  113. $tables = [ 'album', 'photo', 'photo_album', 'photo_comment', 'place' ];
  114. break;
  115. case SEC_LABLANCA:
  116. $tables = [ 'festival', 'festival_day', 'festival_event', 'festival_event_image', 'festival_offer', 'place', 'people' ];
  117. break;
  118. case SEC_ALL:
  119. $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' ];
  120. break;
  121. default:
  122. //'Bad request' staus code
  123. http_response_code(400);
  124. return;
  125. }
  126. switch ($format){
  127. case FOR_JSON:
  128. $db = array();
  129. foreach($tables as $table){
  130. $db[] = [ $table => get_table($con, $table, $format) ];
  131. }
  132. return(json_encode($db));
  133. break;
  134. }
  135. }
  136. /****************************************************
  137. * Echoes the contents of a table from the database. *
  138. * Inaccessible or sensitive tables or fields are *
  139. * not printed. *
  140. * *
  141. * @params: *
  142. * con: (MySQL server connection) RO mode enough. *
  143. * table (string): The name of the table. *
  144. * @return: (Assoc Array): Data in the table. *
  145. ****************************************************/
  146. function get_table($con, $table){
  147. $table = strtolower($table);
  148. switch ($table){
  149. case "activity":
  150. $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;");
  151. break;
  152. case "activity_comment":
  153. $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;");
  154. break;
  155. case "album":
  156. $q = mysqli_query($con, "SELECT id, permalink, title_es, title_en, title_eu, description_es, description_en, description_eu, open FROM album;");
  157. break;
  158. case "photo":
  159. $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;");
  160. break;
  161. case "post":
  162. $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;");
  163. break;
  164. case "post_comment":
  165. $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;");
  166. break;
  167. //Other cases:
  168. default:
  169. //If the table is a public one and has not been listed above, all of its fields are public.
  170. 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'])){
  171. $q = mysqli_query($con, "SELECT * FROM $table;");
  172. }
  173. //If forbidden table
  174. else{
  175. //'Forbidden' status code
  176. //TODO: I dont want execution stopping here
  177. http_response_code(403);
  178. return;
  179. }
  180. }
  181. //Create result array
  182. $rows = array();
  183. while($r = mysqli_fetch_assoc($q)) {
  184. $rows[] = $r;
  185. }
  186. return $rows;
  187. }
  188. /****************************************************
  189. * gets the IP address of the client. *
  190. * *
  191. * @return: (String): Client IP address. *
  192. ****************************************************/
  193. function get_user_ip(){
  194. $client = @$_SERVER['HTTP_CLIENT_IP'];
  195. $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  196. $remote = $_SERVER['REMOTE_ADDR'];
  197. if(filter_var($client, FILTER_VALIDATE_IP)){
  198. $ip = $client;
  199. }
  200. elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  201. $ip = $forward;
  202. }
  203. else{
  204. $ip = $remote;
  205. }
  206. return $ip;
  207. }
  208. /****************************************************
  209. * Registers the request in the database. *
  210. * *
  211. * @params: *
  212. * con: (MySQL server connection) RO mode enough. *
  213. * client: (string): The client identifier. *
  214. * user: (string): A unique end user identifier. *
  215. * action: (string): Requested action. *
  216. * section: (string): Requested database section. *
  217. * version: (int): Version of the client db. *
  218. * new_version: (int): Returned version. *
  219. * foreground: (int): 1 for fg syncs, 0 for bg. *
  220. * format: (string): Requested format. *
  221. * error: (string): Error message to store. *
  222. ****************************************************/
  223. function log_sync($con, $client, $user, $action, $section, $version, $new_version, $foreground, $format, $error){
  224. $ip = get_user_ip();
  225. $browser_data = get_browser(null, true);
  226. $os = $browser_data['platform'];
  227. $browser = $browser_data['browser'];
  228. $uagent = $browser_data['browser_name_pattern'];
  229. mysqli_query($con, "INSERT INTO sync (client, user, action, section, version_from, version_to, fg, format, error, ip, os, uagent) VALUES ('$client', '$user', '$action', '$section', $version, $new_version, $foreground, '$format', '$error', '$ip', '$os', '$uagent');");
  230. error_log("INSERT INTO sync (client, user, action, section, version_from, version_to, fg, format, error, ip, os, uagent) VALUES ('$client', '$user', '$action', '$section', $version, $new_version, $foreground, '$format', '$error', '$ip', '$os', '$uagent');");
  231. }
  232. //Connect to the database
  233. $con = startdb('rw');
  234. //Get data from URL
  235. $client = mysqli_real_escape_string($con, $_GET[GET_CLIENT]);
  236. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  237. $action = strtolower(mysqli_real_escape_string($con, $_GET[GET_ACTION]));
  238. $section = strtolower(mysqli_real_escape_string($con, $_GET[GET_SECTION]));
  239. $version = mysqli_real_escape_string($con, $_GET[GET_VERSION]);
  240. $foregroud = mysqli_real_escape_string($con, $_GET[GET_FOREGROUND]);
  241. $format = strtolower(mysqli_real_escape_string($con, $_GET[GET_FORMAT]));
  242. //Initialize some variables
  243. $error = '';
  244. $new_version = -1;
  245. //Validate data
  246. if (strlen($client) < 1){
  247. $client = '';
  248. }
  249. if (strlen($user) < 1){
  250. $user = '';
  251. }
  252. if (strlen($action) < 1){
  253. $action = DEF_ACTION
  254. }
  255. if ($action != ACTION_SYNC && $action != ACTION_VERSION){
  256. //Bad request
  257. http_response_code(400);
  258. $error = $error . ERR_ACTION . mysqli_real_escape_string($con, $_GET[GET_ACTION]);
  259. }
  260. if (strlen($section) > 0 && $section != SEC_ALL && $section != SEC_BLOG && $section != SEC_ACTIVITIES && $section != SEC_GALLERY && $section != SEC_LABLANCA ){
  261. //Bad request
  262. http_response_code(400);
  263. $error = $error . ERR_SECTION . mysqli_real_escape_string($con, $_GET[GET_SECTION]);
  264. }
  265. if (strlen($section) == 0){
  266. $section = SEC_ALL;
  267. }
  268. if (strlen($version) == 0){
  269. $version = -1;
  270. }
  271. if (is_int($version) == false){
  272. //Bad request
  273. http_response_code(400);
  274. $error = $error . ERR_VERSION . mysqli_real_escape_string($con, $_GET[GET_VERSION]);
  275. $version = -1;
  276. }
  277. if (strlen($foregroud) < 1){
  278. $foreground = 1;
  279. }
  280. if ($foreground != 0 && $foregroud != 0){
  281. //Bad request
  282. http_response_code(400);
  283. $error = $error . ERR_FOREGROUND . mysqli_real_escape_string($con, $_GET[GET_BACKGROUND]);
  284. }
  285. if (strlen($format) < 1){
  286. $format = DEF_FORMAT;
  287. }
  288. if ($format != FOR_JSON){
  289. //Bad request
  290. http_response_code(400);
  291. $error = $error . ERR_FORMAT . mysqli_real_escape_string($con, $_GET[GET_FORMAT]);
  292. }
  293. //If there has not been an error, procede
  294. if (strlen($error) == 0){
  295. //If the client just needs to know the version number
  296. if ($action == ACTION_VERSION){
  297. $new_version = get_version($con, $section);
  298. echo ($new_version);
  299. }
  300. //If the clients wants to actually perform a sync
  301. else{
  302. //If the client version is up to date, send a no content status
  303. $new_version = get_version($con, $section);
  304. if ($version >= $new_version){
  305. //No content
  306. http_response_code(204);
  307. }
  308. //If the client needs an update
  309. else{
  310. echo(sync($con, $section));
  311. }
  312. }
  313. }
  314. //Log the sync in the database
  315. log_sync($con, $client, $user, $action, $section, $version, $new_version, $foreground, $format, $error);
  316. ?>