sync.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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, connect to the db, enabling *
  52. * the variable $con for futher use everywhere in *
  53. * the php code, and populates the arrays $user *
  54. * and $permission, with info about the user. *
  55. * *
  56. * @return: (db connection): The connection handler. *
  57. *****************************************************/
  58. function startdb(){
  59. //Include the db configuration file. It's somehow like this
  60. /*
  61. <?php
  62. $host = 'XXXX';
  63. $db_name = 'XXXX';
  64. $username_ro = 'XXXX';
  65. $username_rw = 'XXXX';
  66. $pass_ro = 'XXXX';
  67. $pass_rw = 'XXXX';
  68. ?>
  69. */
  70. include('../../.htpasswd');
  71. //Connect to to database
  72. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  73. //Set encoding options
  74. mysqli_set_charset($con, 'utf-8');
  75. header('Content-Type: text/html; charset=utf8');
  76. mysqli_query($con, 'SET NAMES utf8;');
  77. //Return the db connection
  78. return $con;
  79. }
  80. /*****************************************************
  81. * Selects the value of a parameter from the list of *
  82. * GET arguments. It also sanitizes it to prevent *
  83. * SQL injections. *
  84. * *
  85. * @params: *
  86. * get: (string array) Contains the GET *
  87. * parameters. *
  88. * param: (string) Name of the parameter. *
  89. * @return: (string): Value of the parameter or an *
  90. * empty string if it was not passed. *
  91. *****************************************************/
  92. function extract_param($con, $get, $param){
  93. if(isset($_GET[$param])){
  94. return mysqli_real_escape_string($con, $_GET[$param]);
  95. }
  96. else{
  97. return "";
  98. }
  99. }
  100. /*****************************************************
  101. * Gets information about the API call and the *
  102. * assocciated client. If some mandatory parameter *
  103. * is not provided, a error log entry is registered *
  104. * *
  105. * @params: *
  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. * get: (string array) Contains the GET *
  143. * parameters. *
  144. * @return: (int array): Array with the version of *
  145. * the tables in the user app, keyed with *
  146. * the table names. *
  147. *****************************************************/
  148. function get_user_versions($con, $get){
  149. global $tab_list;
  150. $versions = array();
  151. foreach($tab_list as $tab){
  152. $versions[$tab] = intval(extract_param($con, $get, $tab));
  153. }
  154. return $versions;
  155. }
  156. /*****************************************************
  157. * Reads the version of the tables reported by the *
  158. * user as GET parameters. *
  159. * *
  160. * @params: *
  161. * con: (MySQL server connection) RO mode enough. *
  162. * parameters. *
  163. * @return: (int array): Array with the version of *
  164. * the tables in the server, keyed with *
  165. * the table names. *
  166. *****************************************************/
  167. function get_server_versions($con){
  168. $versions = array();
  169. $q = mysqli_query($con, "SELECT section, version FROM version;");
  170. while($r = mysqli_fetch_array($q)){
  171. $versions[$r['section']] = $r['version'];
  172. }
  173. return $versions;
  174. }
  175. /*****************************************************
  176. * Select the tables that need to be synced. *
  177. * *
  178. * @params: *
  179. * user: (int array) Versions of tables in the *
  180. * user app. *
  181. * server: (int array) Versions of tables in the *
  182. * server. *
  183. * @return: (string array): Array with the names of *
  184. * the tables that need to be synced. *
  185. *****************************************************/
  186. function select_tables($user, $server){
  187. global $tab_list;
  188. $tables = array();
  189. foreach($tab_list as $table){
  190. if ((int) $user[$table] < (int) $server[$table]){
  191. array_push($tables, $table);
  192. }
  193. }
  194. return $tables;
  195. }
  196. /*****************************************************
  197. * Formats the contents of ther 'versions' table, *
  198. * Only for the tables that will be synced. *
  199. * *
  200. * @params: *
  201. * con: (MySQL server connection) RO mode enough. *
  202. * tables (String array): List of table. *
  203. * @return: (Assoc Array): Data in the table. *
  204. ****************************************************/
  205. function get_table_version($con, $tables){
  206. // Build query, showing only tables to sync
  207. $s = "SELECT * FROM version WHERE ";
  208. foreach($tables as $table){
  209. $s = $s . "section = '$table' OR ";
  210. }
  211. $s = $s . "1 = 2;";
  212. $q = mysqli_query($con, $s);
  213. //If no rows, return
  214. if (mysqli_num_rows($q) == 0){
  215. return "";
  216. }
  217. //Create result array
  218. $str = "";
  219. $str = $str. "\"version\":[";
  220. while($r = mysqli_fetch_assoc($q)) {
  221. $str = $str . json_encode($r) . ",";
  222. }
  223. $str = rtrim($str,',');
  224. $str = $str . "]";
  225. return $str;
  226. }
  227. /*****************************************************
  228. * Formats the contents of a table in the database. *
  229. * Inaccessible or sensitive tables or fields are *
  230. * not printed. *
  231. * *
  232. * @params: *
  233. * con: (MySQL server connection) RO mode enough. *
  234. * table (string): The name of the table. *
  235. * @return: (Assoc Array): Data in the table. *
  236. ****************************************************/
  237. function get_table($con, $table){
  238. $table = strtolower($table);
  239. switch ($table){
  240. case TAB_ACTIVITY:
  241. $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;");
  242. break;
  243. case TAB_ACTIVITY_COMMENT:
  244. $q = mysqli_query($con, "SELECT id, activity, text, dtime, username, lang FROM activity_comment WHERE approved = 1;");
  245. break;
  246. case TAB_ALBUM:
  247. $q = mysqli_query($con, "SELECT id, permalink, title_es, title_en, title_eu, description_es, description_en, description_eu, open FROM album;");
  248. break;
  249. case TAB_PHOTO:
  250. $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;");
  251. break;
  252. case TAB_POST:
  253. $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;");
  254. break;
  255. case TAB_POST_COMMENT:
  256. $q = mysqli_query($con, "SELECT post_comment.id AS id, post, text, dtime, username, lang FROM post_comment WHERE approved = 1;");
  257. break;
  258. case TAB_PHOTO_COMMENT:
  259. $q = mysqli_query($con, "SELECT photo_comment.id AS id, post, text, dtime, username, lang FROM photo_comment WHERE approved = 1;");
  260. break;
  261. case TAB_SPONSOR:
  262. $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;");
  263. break;
  264. case TAB_SETTINGS:
  265. $q = mysqli_query($con, "SELECT name, value FROM settings;");
  266. break;
  267. //Other cases:
  268. default:
  269. $q = mysqli_query($con, "SELECT * FROM $table;");
  270. }
  271. //If no rows, return
  272. if (mysqli_num_rows($q) == 0){
  273. return "";
  274. }
  275. //Create result array
  276. $str = "";
  277. $str = $str. "\"$table\":[";
  278. while($r = mysqli_fetch_assoc($q)) {
  279. $str = $str . json_encode($r) . ",";
  280. }
  281. $str = rtrim($str,',');
  282. $str = $str . "]";
  283. return $str;
  284. }
  285. /*****************************************************
  286. * Prints out required tables. *
  287. * *
  288. * @return: (String): Client IP address. *
  289. *****************************************************/
  290. function sync($con, $tables){
  291. $str = "";
  292. if(sizeof($tables) > 0){
  293. $str = "{" . get_table_version($con, $tables);
  294. foreach($tables as $table){
  295. $str = $str . get_table($con, $table) . ",";
  296. }
  297. $str = rtrim($str,',');
  298. $str = $str . "}";
  299. echo($str);
  300. return true;
  301. }
  302. return false;
  303. }
  304. /*****************************************************
  305. * Gets the IP address of the client. *
  306. * *
  307. * @return: (String): Client IP address. *
  308. *****************************************************/
  309. function get_user_ip(){
  310. $client = @$_SERVER['HTTP_CLIENT_IP'];
  311. $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  312. $remote = $_SERVER['REMOTE_ADDR'];
  313. if(filter_var($client, FILTER_VALIDATE_IP)){
  314. $ip = $client;
  315. }
  316. elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  317. $ip = $forward;
  318. }
  319. else{
  320. $ip = $remote;
  321. }
  322. return $ip;
  323. }
  324. /****************************************************
  325. * Registers the request in the database. *
  326. * *
  327. * @params: *
  328. * con: (MySQL server connection) RO mode enough. *
  329. * client: (string): The client identifier. *
  330. * user: (string): A unique end user identifier. *
  331. * action: (string): Requested action. *
  332. * section: (string): Requested database section. *
  333. * version: (int): Version of the client db. *
  334. * new_version: (int): Returned version. *
  335. * foreground: (int): 1 for fg syncs, 0 for bg. *
  336. * format: (string): Requested format. *
  337. * error: (string): Error message to store. *
  338. ****************************************************/
  339. function log_sync($con, $user, $synced){
  340. 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]');");
  341. }
  342. function log_error($con, $user){
  343. 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]');");
  344. }
  345. // Connect to the database
  346. $con = startdb('rw');
  347. // Get info about the user
  348. $user = get_user_info($con, $_GET);
  349. if(strlen($user["error"]) > 0){
  350. log_error($con, $user);
  351. http_response_code(400);
  352. exit(-1);
  353. }
  354. // Get tables to sync
  355. $v_user = get_user_versions($con, $_GET);
  356. $v_server = get_server_versions($con);
  357. $tables = select_tables($v_user, $v_server);
  358. $synced = sync($con, $tables);
  359. //Log the sync in the database
  360. log_sync($con, $user, $synced);
  361. ?>