sync.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. // Gasteizko Margolariak API v3 //
  3. //Database section identifiers
  4. define('SEC_ALL', 'all');
  5. define('SEC_BLOG', 'blog');
  6. define('SEC_ACTIVITIES', 'activities');
  7. define('SEC_GALLERY', 'gallery');
  8. define('SEC_LABLANCA', 'lablanca');
  9. define('TAB_ACTIVITY', 'activity');
  10. define('TAB_ACTIVITY_COMMENT', 'activity_comment');
  11. define('TAB_ACTIVITY_IMAGE', 'activity_image');
  12. define('TAB_ACTIVITY_ITINERARY', 'activity_itinerary');
  13. define('TAB_ACTIVITY_TAG', 'activity_tag');
  14. define('TAB_ALBUM', 'album');
  15. define('TAB_FESTIVAL', 'festival');
  16. define('TAB_FESTIVAL_DAY', 'festival_day');
  17. define('TAB_FESTIVAL_EVENT_CITY', 'festival_event_city');
  18. define('TAB_FESTIVAL_EVENT_GM', 'festival_event_gm');
  19. define('TAB_FESTIVAL_OFFER', 'festival_offer');
  20. define('TAB_PEOPLE', 'people');
  21. define('TAB_PHOTO', 'photo');
  22. define('TAB_PHOTO_ALBUM', 'photo_album');
  23. define('TAB_PHOTO_COMMENT', 'photo_comment');
  24. define('TAB_PLACE', 'place');
  25. define('TAB_POST', 'post');
  26. define('TAB_POST_COMMENT', 'post_comment');
  27. define('TAB_POST_IMAGE', 'post_image');
  28. define('TAB_POST_TAG', 'post_tag');
  29. define('TAB_ROUTE', 'route');
  30. define('TAB_ROUTE_POINT', 'route_point');
  31. define('TAB_SETTINGS', 'settings');
  32. define('TAB_SPONSOR', 'sponsor');
  33. //$_GET valid parameters
  34. define('GET_CLIENT', 'client');
  35. define('GET_USER', 'user');
  36. define('GET_FOREGROUND', 'foreground');
  37. //Error messages
  38. define('ERR_CLIENT', 'CLIENT');
  39. //List of all tables to sync, sorted by priority.
  40. $tab_list = [TAB_SETTINGS, TAB_PLACE, TAB_ROUTE_POINT,
  41. TAB_ROUTE, TAB_PEOPLE, TAB_FESTIVAL_EVENT_GM,
  42. TAB_FESTIVAL, TAB_FESTIVAL_DAY, TAB_FESTIVAL_OFFER,
  43. TAB_FESTIVAL_EVENT_CITY, TAB_ACTIVITY, TAB_ACTIVITY_IMAGE,
  44. TAB_ACTIVITY_ITINERARY, TAB_SPONSOR, TAB_ALBUM,
  45. TAB_PHOTO, TAB_PHOTO_ALBUM, TAB_POST,
  46. TAB_POST_IMAGE, TAB_PHOTO_COMMENT, TAB_POST_COMMENT,
  47. TAB_ACTIVITY_COMMENT, TAB_ACTIVITY_TAG, TAB_POST_TAG];
  48. /*****************************************************
  49. * This function is called from almost everywhere at *
  50. * the beggining of the page. It initializes the *
  51. * session variables and connects to the db. *
  52. * *
  53. * @return: (MySQL server connection): The *
  54. * connection handler. *
  55. ****************************************************/
  56. function startdb(){
  57. //Include the db configuration file. It's somehow like this
  58. /*
  59. <?php
  60. $host = 'XXXX';
  61. $db_name = 'XXXX';
  62. $username_ro = 'XXXX';
  63. $username_rw = 'XXXX';
  64. $pass_ro = 'XXXX';
  65. $pass_rw = 'XXXX';
  66. ?>
  67. */
  68. include('../../.htpasswd');
  69. //Connect to to database
  70. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  71. //Set encoding options
  72. mysqli_set_charset($con, 'utf-8');
  73. header('Content-Type: text/html; charset=utf8');
  74. mysqli_query($con, 'SET NAMES utf8;');
  75. //Return the db connection
  76. return $con;
  77. }
  78. /*****************************************************
  79. * Selects the value of a parameter from the list of *
  80. * GET arguments. It also sanitizes it to prevent *
  81. * SQL injections. *
  82. * *
  83. * @params: *
  84. * con: (MySQL server connection) Db connector. *
  85. * get: (string array) Contains the GET *
  86. * parameters. *
  87. * param: (string) Name of the parameter. *
  88. * @return: (string): Value of the parameter or an *
  89. * empty string if it was not passed. *
  90. *****************************************************/
  91. function extract_param($con, $get, $param){
  92. if(isset($_GET[$param])){
  93. return mysqli_real_escape_string($con, $_GET[$param]);
  94. }
  95. else{
  96. return "";
  97. }
  98. }
  99. /*****************************************************
  100. * Gets information about the API call and the *
  101. * assocciated client. If some mandatory parameter *
  102. * is not provided, a error log entry is registered *
  103. * *
  104. * @params: *
  105. * con: (MySQL server connection) Db connector. *
  106. * get: (string array) Contains the GET *
  107. * parameters. *
  108. * @return: (string array): Array with the keys *
  109. * 'client', 'user', 'foreground', 'ip', *
  110. * 'os', 'browser', 'uagent' and 'error'. *
  111. * 'error' will contain the key of a *
  112. * mandatory value if it has not been *
  113. * provided, or will be empty if there *
  114. * were no problem. *
  115. *****************************************************/
  116. function get_user_info($con, $get){
  117. $info = array();
  118. $error = "";
  119. $info["client"] = extract_param($con, $get, GET_CLIENT);
  120. if(strlen($info["client"]) == 0) {
  121. error_log("SYNC ERROR: Trying to sync with no client name.");
  122. $error = ERR_CLIENT;
  123. }
  124. $info["user"] = extract_param($con, $get, GET_USER);
  125. $info["foreground"] = (int) extract_param($con, $get, GET_FOREGROUND);
  126. if($info["foreground"] != 1){
  127. $info["foreground"] = 0;
  128. }
  129. $info["ip"] = get_user_ip();
  130. $browser_data = get_browser(null, true);
  131. $info["os"] = $browser_data['platform'];
  132. $info["browser"] = $browser_data['browser'];
  133. $info["uagent"] = $browser_data['browser_name_pattern'];
  134. $info["error"] = $error;
  135. return $info;
  136. }
  137. /*****************************************************
  138. * Reads the version of the tables reported by the *
  139. * user as GET parameters. *
  140. * *
  141. * @params: *
  142. * con: (MySQL server connection) Db connector. *
  143. * get: (string array) Contains the GET *
  144. * parameters. *
  145. * @return: (int array): Array with the version of *
  146. * the tables in the user app, keyed with *
  147. * the table names. *
  148. *****************************************************/
  149. function get_user_versions($con, $get){
  150. global $tab_list;
  151. $versions = array();
  152. foreach($tab_list as $tab){
  153. $versions[$tab] = intval(extract_param($con, $get, $tab));
  154. }
  155. return $versions;
  156. }
  157. /*****************************************************
  158. * Reads the version of the tables reported by the *
  159. * user as GET parameters. *
  160. * *
  161. * @params: *
  162. * con: (MySQL server connection) RO mode enough. *
  163. * parameters. *
  164. * @return: (int array): Array with the version of *
  165. * the tables in the server, keyed with *
  166. * the table names. *
  167. *****************************************************/
  168. function get_server_versions($con){
  169. $versions = array();
  170. $q = mysqli_query($con, "SELECT section, version FROM version;");
  171. while($r = mysqli_fetch_array($q)){
  172. $versions[$r['section']] = $r['version'];
  173. }
  174. return $versions;
  175. }
  176. /*****************************************************
  177. * Select the tables that need to be synced. *
  178. * *
  179. * @params: *
  180. * user: (int array) Versions of tables in the *
  181. * user app. *
  182. * server: (int array) Versions of tables in the *
  183. * server. *
  184. * @return: (string array): Array with the names of *
  185. * the tables that need to be synced. *
  186. *****************************************************/
  187. function select_tables($user, $server){
  188. global $tab_list;
  189. $tables = array();
  190. foreach($tab_list as $table){
  191. if ((int) $user[$table] < (int) $server[$table]){
  192. array_push($tables, $table);
  193. }
  194. }
  195. return $tables;
  196. }
  197. /*****************************************************
  198. * Formats the contents of ther 'versions' table, *
  199. * Only for the tables that will be synced. *
  200. * *
  201. * @params: *
  202. * con: (MySQL server connection) Db connector. *
  203. * tables (String array): List of table. *
  204. * @return: (Assoc Array): Data in the table. *
  205. ****************************************************/
  206. function get_table_version($con, $tables){
  207. // Build query, showing only tables to sync
  208. $s = "SELECT * FROM version WHERE ";
  209. foreach($tables as $table){
  210. $s = $s . "section = '$table' OR ";
  211. }
  212. $s = $s . "1 = 2;";
  213. $q = mysqli_query($con, $s);
  214. //If no rows, return
  215. if (mysqli_num_rows($q) == 0){
  216. return "";
  217. }
  218. //Create result array
  219. $str = "";
  220. $str = $str. "\"version\":[";
  221. while($r = mysqli_fetch_assoc($q)) {
  222. $str = $str . json_encode($r) . ",";
  223. }
  224. $str = rtrim($str,',');
  225. $str = $str . "]";
  226. return $str;
  227. }
  228. /*****************************************************
  229. * Formats the contents of a table in the database. *
  230. * Inaccessible or sensitive tables or fields are *
  231. * not printed. *
  232. * *
  233. * @params: *
  234. * con: (MySQL server connection) RO mode enough. *
  235. * table (string): The name of the table. *
  236. * @return: (Assoc Array): Data in the table. *
  237. ****************************************************/
  238. function get_table($con, $table){
  239. $table = strtolower($table);
  240. switch ($table){
  241. case TAB_ACTIVITY:
  242. $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;");
  243. break;
  244. case TAB_ACTIVITY_COMMENT:
  245. $q = mysqli_query($con, "SELECT id, activity, text, dtime, username, lang FROM activity_comment WHERE approved = 1;");
  246. break;
  247. case TAB_ALBUM:
  248. $q = mysqli_query($con, "SELECT id, permalink, title_es, title_en, title_eu, description_es, description_en, description_eu, open FROM album;");
  249. break;
  250. case TAB_PHOTO:
  251. $q = mysqli_query($con, "SELECT photo.id AS id, file, permalink, title_es, title_en, title_eu, description_es, description_en, description_eu, uploaded, place, width, height, size, CONCAT(photo.username, user) AS username FROM photo, user WHERE user.id = photo.user AND approved = 1;");
  252. break;
  253. case TAB_POST:
  254. $q = mysqli_query($con, "SELECT post.id AS id, permalink, title_es, title_en, title_eu, text_es, text_en, text_eu, comments, username, dtime FROM post, user WHERE user.id = user AND visible = 1;");
  255. break;
  256. case TAB_POST_COMMENT:
  257. $q = mysqli_query($con, "SELECT post_comment.id AS id, post, text, dtime, username, lang FROM post_comment WHERE approved = 1;");
  258. break;
  259. case TAB_PHOTO_COMMENT:
  260. $q = mysqli_query($con, "SELECT photo_comment.id AS id, post, text, dtime, username, lang FROM photo_comment WHERE approved = 1;");
  261. break;
  262. case TAB_SPONSOR:
  263. $q = mysqli_query($con, "SELECT id, name_es, name_en, name_eu, text_es, text_en, text_eu, image, address_es, address_en, address_eu, link, lat, lon FROM sponsor;");
  264. break;
  265. case TAB_SETTINGS:
  266. $q = mysqli_query($con, "SELECT name, value FROM settings;");
  267. break;
  268. //Other cases:
  269. default:
  270. $q = mysqli_query($con, "SELECT * FROM $table;");
  271. }
  272. //If no rows, return
  273. if (mysqli_num_rows($q) == 0){
  274. return "";
  275. }
  276. //Create result array
  277. $str = "";
  278. $str = $str. "\"$table\":[";
  279. while($r = mysqli_fetch_assoc($q)) {
  280. $str = $str . json_encode($r) . ",";
  281. }
  282. $str = rtrim($str,',');
  283. $str = $str . "]";
  284. return $str;
  285. }
  286. /*****************************************************
  287. * Prints out required tables. *
  288. * *
  289. * @params: *
  290. * con: (MySQL server connection) Db connector. *
  291. * tables: (String array) List of tables to sync. *
  292. * @return: (String): Client IP address. *
  293. *****************************************************/
  294. function sync($con, $tables){
  295. $str = "";
  296. if(sizeof($tables) > 0){
  297. $str = "{" . get_table_version($con, $tables);
  298. foreach($tables as $table){
  299. $str = $str . get_table($con, $table) . ",";
  300. }
  301. $str = rtrim($str,',');
  302. $str = $str . "}";
  303. echo($str);
  304. return true;
  305. }
  306. return false;
  307. }
  308. /*****************************************************
  309. * Gets the IP address of the client. *
  310. * *
  311. * @return: (String): Client IP address. *
  312. *****************************************************/
  313. function get_user_ip(){
  314. $client = @$_SERVER['HTTP_CLIENT_IP'];
  315. $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  316. $remote = $_SERVER['REMOTE_ADDR'];
  317. if(filter_var($client, FILTER_VALIDATE_IP)){
  318. $ip = $client;
  319. }
  320. elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  321. $ip = $forward;
  322. }
  323. else{
  324. $ip = $remote;
  325. }
  326. return $ip;
  327. }
  328. /*****************************************************
  329. * Registers the request in the database. *
  330. * *
  331. * @params: *
  332. * con: (MySQL server connection) RO mode enough. *
  333. * user: (String array): Array with, at least, *
  334. * the keys 'client', 'user', 'foreground', *
  335. * 'ip', 'os', 'browser', 'uagent', with *
  336. * info about the calling app. *
  337. * synced: (Int): 1 if a sync content was sent, 0 *
  338. * otherwise. *
  339. *****************************************************/
  340. function log_sync($con, $user, $synced){
  341. mysqli_query($con, "INSERT INTO sync (client, user, fg, synced, ip, os, uagent) VALUES ('$user[client]', '$user[user]', $user[foreground], $synced, '$user[ip]', '$user[os]', '$user[uagent]');");
  342. }
  343. /*****************************************************
  344. * Registers a failed request in the database. *
  345. * *
  346. * @params: *
  347. * con: (MySQL server connection) RO mode enough. *
  348. * user: (String array): Array with, at least, *
  349. * the keys 'client', 'user', 'foreground', *
  350. * 'ip', 'os', 'browser', 'uagent', and *
  351. * 'error', with info about the calling *
  352. * app. The 'error' key will contain an *
  353. * error description. *
  354. *****************************************************/
  355. function log_error($con, $user){
  356. mysqli_query($con, "INSERT INTO sync (client, user, fg, error, ip, os, uagent) VALUES ('$user[client]', '$user[user]', $user[foreground], $user[error], '$user[ip]', '$user[os]', '$user[uagent]');");
  357. }
  358. // Connect to the database
  359. $con = startdb('rw');
  360. // Get info about the user
  361. $user = get_user_info($con, $_GET);
  362. if(strlen($user["error"]) > 0){
  363. log_error($con, $user);
  364. http_response_code(400);
  365. exit(-1);
  366. }
  367. // Get tables to sync
  368. $v_user = get_user_versions($con, $_GET);
  369. $v_server = get_server_versions($con);
  370. $tables = select_tables($v_user, $v_server);
  371. $synced = sync($con, $tables);
  372. //Log the sync in the database
  373. log_sync($con, $user, $synced);
  374. ?>