|
@@ -1,5 +1,17 @@
|
|
|
<?php
|
|
<?php
|
|
|
- // Gasteizko Margolariak API v3 //
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gasteizko Margolariak API v3 - Fast Sync
|
|
|
|
|
+ *
|
|
|
|
|
+ * Used to sync data with apps with persistent storage (i.e. no web apps).
|
|
|
|
|
+ * This file is to be called directly from a URL request.
|
|
|
|
|
+ *
|
|
|
|
|
+ * It is intended to sync only the most important tables, as in an app
|
|
|
|
|
+ * initial sync.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @link https://margolariak.com/API/v3/help/
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ */
|
|
|
|
|
|
|
|
//Database section identifiers
|
|
//Database section identifiers
|
|
|
define('SEC_ALL', 'all');
|
|
define('SEC_ALL', 'all');
|
|
@@ -36,7 +48,12 @@
|
|
|
//Error messages
|
|
//Error messages
|
|
|
define('ERR_CLIENT', 'CLIENT');
|
|
define('ERR_CLIENT', 'CLIENT');
|
|
|
|
|
|
|
|
- //List of all tables to sync, sorted by priority.
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * List of all tables that can be synced, sorted by priority / dependencies.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var string $tab_list
|
|
|
|
|
+ */
|
|
|
$tab_list = array(TAB_SETTINGS, TAB_PLACE, TAB_ROUTE_POINT,
|
|
$tab_list = array(TAB_SETTINGS, TAB_PLACE, TAB_ROUTE_POINT,
|
|
|
TAB_ROUTE, TAB_PEOPLE, TAB_FESTIVAL_EVENT_GM,
|
|
TAB_ROUTE, TAB_PEOPLE, TAB_FESTIVAL_EVENT_GM,
|
|
|
TAB_FESTIVAL, TAB_FESTIVAL_DAY, TAB_FESTIVAL_OFFER,
|
|
TAB_FESTIVAL, TAB_FESTIVAL_DAY, TAB_FESTIVAL_OFFER,
|
|
@@ -44,24 +61,38 @@
|
|
|
TAB_ACTIVITY_ITINERARY, TAB_SPONSOR, TAB_ALBUM,
|
|
TAB_ACTIVITY_ITINERARY, TAB_SPONSOR, TAB_ALBUM,
|
|
|
TAB_PHOTO, TAB_PHOTO_ALBUM, TAB_POST,
|
|
TAB_PHOTO, TAB_PHOTO_ALBUM, TAB_POST,
|
|
|
TAB_POST_IMAGE);
|
|
TAB_POST_IMAGE);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * List of tables with content considered important.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var string $tab_list
|
|
|
|
|
+ */
|
|
|
$fast_tables = array(TAB_FESTIVAL_EVENT_GM, TAB_FESTIVAL, TAB_FESTIVAL_DAY,
|
|
$fast_tables = array(TAB_FESTIVAL_EVENT_GM, TAB_FESTIVAL, TAB_FESTIVAL_DAY,
|
|
|
TAB_FESTIVAL_OFFER, TAB_FESTIVAL_EVENT_CITY, TAB_ACTIVITY,
|
|
TAB_FESTIVAL_OFFER, TAB_FESTIVAL_EVENT_CITY, TAB_ACTIVITY,
|
|
|
TAB_ACTIVITY_IMAGE, TAB_ACTIVITY_ITINERARY, TAB_PHOTO,
|
|
TAB_ACTIVITY_IMAGE, TAB_ACTIVITY_ITINERARY, TAB_PHOTO,
|
|
|
TAB_PHOTO_ALBUM, TAB_POST, TAB_POST_IMAGE);
|
|
TAB_PHOTO_ALBUM, TAB_POST, TAB_POST_IMAGE);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * List of tables with not-so-much relevant data.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var string $tab_list
|
|
|
|
|
+ */
|
|
|
$slow_tables = array(TAB_SETTINGS, TAB_PLACE, TAB_ROUTE_POINT,
|
|
$slow_tables = array(TAB_SETTINGS, TAB_PLACE, TAB_ROUTE_POINT,
|
|
|
TAB_ROUTE, TAB_PEOPLE, TAB_SPONSOR,
|
|
TAB_ROUTE, TAB_PEOPLE, TAB_SPONSOR,
|
|
|
TAB_ALBUM);
|
|
TAB_ALBUM);
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * This function is called from almost everywhere at *
|
|
|
|
|
- * the beggining of the page. It initializes the *
|
|
|
|
|
- * session variables and connects to the db. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @return: (MySQL server connection): The *
|
|
|
|
|
- * connection handler. *
|
|
|
|
|
- ****************************************************/
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Initializes the MySQL database connection.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Called at the beggining of the script. It connects to the database using the
|
|
|
|
|
+ * parameters in the .htpasswd file. It also sets database and page encodings.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @return object Database connection.
|
|
|
|
|
+ */
|
|
|
function startdb(){
|
|
function startdb(){
|
|
|
//Include the db configuration file. It's somehow like this
|
|
//Include the db configuration file. It's somehow like this
|
|
|
/*
|
|
/*
|
|
@@ -87,19 +118,19 @@
|
|
|
//Return the db connection
|
|
//Return the db connection
|
|
|
return $con;
|
|
return $con;
|
|
|
}
|
|
}
|
|
|
- /*****************************************************
|
|
|
|
|
- * Selects the value of a parameter from the list of *
|
|
|
|
|
- * GET arguments. It also sanitizes it to prevent *
|
|
|
|
|
- * SQL injections. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) Db connector. *
|
|
|
|
|
- * get: (string array) Contains the GET *
|
|
|
|
|
- * parameters. *
|
|
|
|
|
- * param: (string) Name of the parameter. *
|
|
|
|
|
- * @return: (string): Value of the parameter or an *
|
|
|
|
|
- * empty string if it was not passed. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Extracts a request parameter.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Extracts the value of a parameter from the list of GET parameters,
|
|
|
|
|
+ * sanitizing it to prevent SQL injections.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param string $param Key of the parameter to retrieve.
|
|
|
|
|
+ * @return string Value of the parameter or an empty string if it was not found.
|
|
|
|
|
+ */
|
|
|
function extract_param($con, $get, $param){
|
|
function extract_param($con, $get, $param){
|
|
|
if(isset($_GET[$param])){
|
|
if(isset($_GET[$param])){
|
|
|
return mysqli_real_escape_string($con, $_GET[$param]);
|
|
return mysqli_real_escape_string($con, $_GET[$param]);
|
|
@@ -109,24 +140,30 @@
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Gets information about the API call and the *
|
|
|
|
|
- * assocciated client. If some mandatory parameter *
|
|
|
|
|
- * is not provided, a error log entry is registered *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) Db connector. *
|
|
|
|
|
- * get: (string array) Contains the GET *
|
|
|
|
|
- * parameters. *
|
|
|
|
|
- * @return: (string array): Array with the keys *
|
|
|
|
|
- * 'client', 'user', 'foreground', 'ip', *
|
|
|
|
|
- * 'os', 'browser', 'uagent' and 'error'. *
|
|
|
|
|
- * 'error' will contain the key of a *
|
|
|
|
|
- * mandatory value if it has not been *
|
|
|
|
|
- * provided, or will be empty if there *
|
|
|
|
|
- * were no problem. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
- function get_user_info($con, $get){
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets request info.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Gets information about the request by reading it's parameters.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param array $get Optional. Array with the request parameters. Default
|
|
|
|
|
+ * is $_GET.
|
|
|
|
|
+ * @return array {
|
|
|
|
|
+ * @type string client Client identifier. Empty if not provided.
|
|
|
|
|
+ * @type string user User identifier. Empty if not provided.
|
|
|
|
|
+ * @type int foreground 1 if the sync is being made in the app
|
|
|
|
|
+ * foreground, 0 otherwise.
|
|
|
|
|
+ * @type string ip Client IP.
|
|
|
|
|
+ * @type string os Client operating system identifier. Empty if not
|
|
|
|
|
+ * found.
|
|
|
|
|
+ * @type string browser Client browser identifier. Empty if not found.
|
|
|
|
|
+ * @type string uagent Client user agent. Empty if not found.
|
|
|
|
|
+ * @type string error Will contain ERR_CLIENT if the client was not
|
|
|
|
|
+ * specified, empty otherwise.
|
|
|
|
|
+ * }
|
|
|
|
|
+ */
|
|
|
|
|
+ function get_user_info($con, $get = $GET){
|
|
|
$info = array();
|
|
$info = array();
|
|
|
$error = "";
|
|
$error = "";
|
|
|
$info["client"] = extract_param($con, $get, GET_CLIENT);
|
|
$info["client"] = extract_param($con, $get, GET_CLIENT);
|
|
@@ -148,19 +185,24 @@
|
|
|
return $info;
|
|
return $info;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Reads the version of the tables reported by the *
|
|
|
|
|
- * user as GET parameters. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) Db connector. *
|
|
|
|
|
- * get: (string array) Contains the GET *
|
|
|
|
|
- * parameters. *
|
|
|
|
|
- * @return: (int array): Array with the version of *
|
|
|
|
|
- * the tables in the user app, keyed with *
|
|
|
|
|
- * the table names. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
- function get_user_versions($con, $get){
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads the table version in the client app.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Reads the version of the tables reported by the user as GET parameters.
|
|
|
|
|
+ * Those parameters must be the same as the table names listed in
|
|
|
|
|
+ * {@see $tab_list}.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @global array $tab_list Array with the names of the tables to sync.
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param array $get Optional. Array with the request parameters. Default
|
|
|
|
|
+ * is $_GET.
|
|
|
|
|
+ * @return array Integer array with the version of the tables reported in
|
|
|
|
|
+ * the request, keyed with the table names. If no table
|
|
|
|
|
+ * version was specified, the array will be empty.
|
|
|
|
|
+ */
|
|
|
|
|
+ function get_user_versions($con, $get = $_GET){
|
|
|
global $tab_list;
|
|
global $tab_list;
|
|
|
$versions = array();
|
|
$versions = array();
|
|
|
foreach($tab_list as $tab){
|
|
foreach($tab_list as $tab){
|
|
@@ -169,21 +211,21 @@
|
|
|
return $versions;
|
|
return $versions;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Reads the version of the tables reported by the *
|
|
|
|
|
- * user as GET parameters. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) RO mode enough. *
|
|
|
|
|
- * parameters. *
|
|
|
|
|
- * @return: (int array): Array with the version of *
|
|
|
|
|
- * the tables in the server, keyed with *
|
|
|
|
|
- * the table names. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
- function get_server_versions($con){
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads the table version in the server.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Reads from the database the version of the tables that sync with the
|
|
|
|
|
+ * clients and have important data.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @global array $fast_tables Array with the names of the tables to sync.
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @return array Integer array with the version of the tables in the
|
|
|
|
|
+ * database, keyed with the table names.
|
|
|
|
|
+ */
|
|
|
|
|
+ function get_server_versions($con){
|
|
|
global $fast_tables;
|
|
global $fast_tables;
|
|
|
-
|
|
|
|
|
$versions = array();
|
|
$versions = array();
|
|
|
$q = mysqli_query($con, "SELECT section, version FROM version;");
|
|
$q = mysqli_query($con, "SELECT section, version FROM version;");
|
|
|
|
|
|
|
@@ -198,17 +240,24 @@
|
|
|
return $versions;
|
|
return $versions;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Select the tables that need to be synced. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * user: (int array) Versions of tables in the *
|
|
|
|
|
- * user app. *
|
|
|
|
|
- * server: (int array) Versions of tables in the *
|
|
|
|
|
- * server. *
|
|
|
|
|
- * @return: (string array): Array with the names of *
|
|
|
|
|
- * the tables that need to be synced. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Select the tables that need to be synced.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Determines the tables in {@see $tab_list} that are out of sync between
|
|
|
|
|
+ * the server and the client.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @global array $tab_list Array with the names of the tables that sync
|
|
|
|
|
+ * whit clients.
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param array $user Integer array with the version of the tables reported
|
|
|
|
|
+ * in the request, keyed with the table names.
|
|
|
|
|
+ * @param array $server Integer array with the version of the tables in the
|
|
|
|
|
+ * database, keyed with the table names.
|
|
|
|
|
+ * @return array String array with the name of the tables present in $user whose
|
|
|
|
|
+ * versions are lower than the ones in $server.
|
|
|
|
|
+ */
|
|
|
function select_tables($user, $server){
|
|
function select_tables($user, $server){
|
|
|
global $tab_list;
|
|
global $tab_list;
|
|
|
$tables = array();
|
|
$tables = array();
|
|
@@ -220,20 +269,26 @@
|
|
|
return $tables;
|
|
return $tables;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Formats the contents of ther 'versions' table, *
|
|
|
|
|
- * Only for the tables that will be synced. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) Db connector. *
|
|
|
|
|
- * tables (String array): List of table. *
|
|
|
|
|
- * @return: (Assoc Array): Data in the table. *
|
|
|
|
|
- ****************************************************/
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * JSON-izes the versions of the tables to sync.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Generates a JSON-formatted string with the versions of all the tables
|
|
|
|
|
+ * to sync.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @global array $fast_tables Array with the names of the tables that sync
|
|
|
|
|
+ * whit clients and have important data.
|
|
|
|
|
+ * @global array $slow_tables Array with the names of the tables that sync
|
|
|
|
|
+ * whit clients and have less important data.
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param array $tables String array with the names of the tables.
|
|
|
|
|
+ * @return string JSON-formatted string with the version of the tables.
|
|
|
|
|
+ * Empty string if no valid table names were passes in $tables.
|
|
|
|
|
+ */
|
|
|
function get_table_version($con, $tables){
|
|
function get_table_version($con, $tables){
|
|
|
-
|
|
|
|
|
global $fast_tables;
|
|
global $fast_tables;
|
|
|
global $slow_tables;
|
|
global $slow_tables;
|
|
|
-
|
|
|
|
|
// Build query, showing only tables to sync
|
|
// Build query, showing only tables to sync
|
|
|
$s = "SELECT section, version FROM version WHERE ";
|
|
$s = "SELECT section, version FROM version WHERE ";
|
|
|
foreach($tables as $table){
|
|
foreach($tables as $table){
|
|
@@ -242,14 +297,12 @@
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
$s = $s . "1 = 2 ";
|
|
$s = $s . "1 = 2 ";
|
|
|
-
|
|
|
|
|
$s = $s . "UNION SELECT section, 0 AS version FROM version WHERE ";
|
|
$s = $s . "UNION SELECT section, 0 AS version FROM version WHERE ";
|
|
|
foreach($tables as $table){
|
|
foreach($tables as $table){
|
|
|
if (in_array($table, $fast_tables)){
|
|
if (in_array($table, $fast_tables)){
|
|
|
$s = $s . "section = '$table' OR ";
|
|
$s = $s . "section = '$table' OR ";
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
$s = $s . "1 = 2;";
|
|
$s = $s . "1 = 2;";
|
|
|
$q = mysqli_query($con, $s);
|
|
$q = mysqli_query($con, $s);
|
|
|
|
|
|
|
@@ -266,21 +319,24 @@
|
|
|
}
|
|
}
|
|
|
$str = rtrim($str,',');
|
|
$str = rtrim($str,',');
|
|
|
$str = $str . "],";
|
|
$str = $str . "],";
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return $str;
|
|
return $str;
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Formats the contents of a table in the database. *
|
|
|
|
|
- * Inaccessible or sensitive tables or fields are *
|
|
|
|
|
- * not printed. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) RO mode enough. *
|
|
|
|
|
- * table (string): The name of the table. *
|
|
|
|
|
- * @return: (Assoc Array): Data in the table. *
|
|
|
|
|
- ****************************************************/
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * JSON-izes the data in a table.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Generates a JSON-formatted string with the data in a table. Inaccessible
|
|
|
|
|
+ * or sensitive tables or fields are not returned.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param string $table Table name.
|
|
|
|
|
+ * @return string JSON-formatted string with the data in the table. Empty
|
|
|
|
|
+ * string if $table was not a valid table name.
|
|
|
|
|
+ */
|
|
|
function get_table($con, $table){
|
|
function get_table($con, $table){
|
|
|
$year = date("Y");
|
|
$year = date("Y");
|
|
|
$table = strtolower($table);
|
|
$table = strtolower($table);
|
|
@@ -329,7 +385,7 @@
|
|
|
break;
|
|
break;
|
|
|
case TAB_POST_IMAGE:
|
|
case TAB_POST_IMAGE:
|
|
|
$q = mysqli_query($con, "SELECT post_image.id AS id, post, image, idx FROM post_image, post WHERE post = post.id AND year(dtime) = $year;");
|
|
$q = mysqli_query($con, "SELECT post_image.id AS id, post, image, idx FROM post_image, post WHERE post = post.id AND year(dtime) = $year;");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
//Other cases:
|
|
//Other cases:
|
|
|
default:
|
|
default:
|
|
|
$q = mysqli_query($con, "SELECT * FROM $table;");
|
|
$q = mysqli_query($con, "SELECT * FROM $table;");
|
|
@@ -351,14 +407,21 @@
|
|
|
return $str;
|
|
return $str;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Prints out required tables. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) Db connector. *
|
|
|
|
|
- * tables: (String array) List of tables to sync. *
|
|
|
|
|
- * @return: (String): Client IP address. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the data on the requested tables.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Builds a JSON string with the data in all the requested tables.
|
|
|
|
|
+ * Inaccessible or sensitive tables or fields are not returned.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @see get_table($con, $table)
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param array $tables String array with the names of the tables to sync.
|
|
|
|
|
+ * @return string JSON-formatted string with the data in the requested
|
|
|
|
|
+ * tables. Empty string if no valid table names were
|
|
|
|
|
+ * provided in $tables.
|
|
|
|
|
+ */
|
|
|
function sync($con, $tables){
|
|
function sync($con, $tables){
|
|
|
$str = "";
|
|
$str = "";
|
|
|
if(sizeof($tables) > 0){
|
|
if(sizeof($tables) > 0){
|
|
@@ -375,11 +438,12 @@
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Gets the IP address of the client. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @return: (String): Client IP address. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the user IP address.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 3.0.0
|
|
|
|
|
+ * @return string User IP address.
|
|
|
|
|
+ */
|
|
|
function get_user_ip(){
|
|
function get_user_ip(){
|
|
|
$client = @$_SERVER['HTTP_CLIENT_IP'];
|
|
$client = @$_SERVER['HTTP_CLIENT_IP'];
|
|
|
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
@@ -396,39 +460,59 @@
|
|
|
return $ip;
|
|
return $ip;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Registers the request in the database. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) RO mode enough. *
|
|
|
|
|
- * user: (String array): Array with, at least, *
|
|
|
|
|
- * the keys 'client', 'user', 'foreground', *
|
|
|
|
|
- * 'ip', 'os', 'browser', 'uagent', with *
|
|
|
|
|
- * info about the calling app. *
|
|
|
|
|
- * synced: (Int): 1 if a sync content was sent, 0 *
|
|
|
|
|
- * otherwise. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Logs a request to the database.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Creates an entry in the table 'sync' with the details of the request.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param array $user {
|
|
|
|
|
+ * @type string client Client identifier. Empty if not provided.
|
|
|
|
|
+ * @type string user User identifier. Empty if not provided.
|
|
|
|
|
+ * @type int foreground 1 if the sync is being made in the app
|
|
|
|
|
+ * foreground, 0 otherwise.
|
|
|
|
|
+ * @type string ip Client IP.
|
|
|
|
|
+ * @type string os Client operating system identifier. Empty if not
|
|
|
|
|
+ * found.
|
|
|
|
|
+ * @type string browser Client browser identifier. Empty if not found.
|
|
|
|
|
+ * @type string uagent Client user agent. Empty if not found.
|
|
|
|
|
+ * }
|
|
|
|
|
+ * @param int synced 1 if sync data was finally sent, 0 otherwise.
|
|
|
|
|
+ */
|
|
|
function log_sync($con, $user, $synced){
|
|
function log_sync($con, $user, $synced){
|
|
|
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]');");
|
|
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]');");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*****************************************************
|
|
|
|
|
- * Registers a failed request in the database. *
|
|
|
|
|
- * *
|
|
|
|
|
- * @params: *
|
|
|
|
|
- * con: (MySQL server connection) RO mode enough. *
|
|
|
|
|
- * user: (String array): Array with, at least, *
|
|
|
|
|
- * the keys 'client', 'user', 'foreground', *
|
|
|
|
|
- * 'ip', 'os', 'browser', 'uagent', and *
|
|
|
|
|
- * 'error', with info about the calling *
|
|
|
|
|
- * app. The 'error' key will contain an *
|
|
|
|
|
- * error description. *
|
|
|
|
|
- *****************************************************/
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Logs a failed request to the database.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Creates an entry in the table 'sync' with the details of the failed request.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ * @param object $con Open database connection.
|
|
|
|
|
+ * @param array $user {
|
|
|
|
|
+ * @type string client Client identifier. Empty if not provided.
|
|
|
|
|
+ * @type string user User identifier. Empty if not provided.
|
|
|
|
|
+ * @type int foreground 1 if the sync is being made in the app
|
|
|
|
|
+ * foreground, 0 otherwise.
|
|
|
|
|
+ * @type string ip Client IP.
|
|
|
|
|
+ * @type string os Client operating system identifier. Empty if not
|
|
|
|
|
+ * found.
|
|
|
|
|
+ * @type string browser Client browser identifier. Empty if not found.
|
|
|
|
|
+ * @type string uagent Client user agent. Empty if not found.
|
|
|
|
|
+ * @type string error Error code.
|
|
|
|
|
+ * }
|
|
|
|
|
+ */
|
|
|
function log_error($con, $user){
|
|
function log_error($con, $user){
|
|
|
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]');");
|
|
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]');");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ // SCRIPT START
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// Connect to the database
|
|
// Connect to the database
|
|
|
$con = startdb('rw');
|
|
$con = startdb('rw');
|