Sfoglia il codice sorgente

Implemented the location V3 API. Comments for the Sync V3 API.

Iñigo Valentin 9 anni fa
parent
commit
6b4c141e9f
2 ha cambiato i file con 99 aggiunte e 79 eliminazioni
  1. 63 56
      www/API/v3/location.php
  2. 36 23
      www/API/v3/sync.php

+ 63 - 56
www/API/v3/location.php

@@ -1,25 +1,14 @@
 <?php
 <?php
-    // Gasteizko Margolariak API v1 //
-        
-    //List of available data formatting
-    define('FOR_JSON', 'json');
-    
-    //Default info format
-    define('DEF_FORMAT', FOR_JSON);
-    
-    //Errors
-    define('ERR_FORMAT', '-FORMAT:');
-    
-    /****************************************************
-    * This function is called from almost everywhere at *
-    * the beggining of the page. It initializes the     *
-    * session variables, connect to the db, enabling    *
-    * the variable $con for futher use everywhere in    *
-    * the php code, and populates the arrays $user      *
-    * and $permission, with info about the user.        *
-    *                                                   *
-    * @return: (db connection): The connection handler. *
-    ****************************************************/
+    // Gasteizko Margolariak API v3 //
+
+    /*****************************************************
+     * 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.                     *
+     *****************************************************/
     function startdb(){
     function startdb(){
         //Include the db configuration file. It's somehow like this
         //Include the db configuration file. It's somehow like this
         /*
         /*
@@ -33,55 +22,73 @@
         ?>
         ?>
         */
         */
         include('../../.htpasswd');
         include('../../.htpasswd');
-        
+
         //Connect to to database
         //Connect to to database
         $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
         $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
-        
+
         //Set encoding options
         //Set encoding options
         mysqli_set_charset($con, 'utf-8');
         mysqli_set_charset($con, 'utf-8');
         header('Content-Type: text/html; charset=utf8');
         header('Content-Type: text/html; charset=utf8');
         mysqli_query($con, 'SET NAMES utf8;');
         mysqli_query($con, 'SET NAMES utf8;');
-        
+
         //Return the db connection
         //Return the db connection
         return $con;
         return $con;
     }
     }
-    
-    //Connect to the database
-    $con = startdb('rw');
-    
-    //Get data from the url
-    $format = strtolower(mysqli_real_escape_string($con, $_GET[GET_FORMAT]));
-    
-    //Validate input
-    if (strlen($format) < 1){
-        $format = DEF_FORMAT;
+
+    /*****************************************************
+     * Retrieves the last reported location in the db,   *
+     * only if it was reported in the last 30 mins.      *
+     *                                                   *
+     * @params:                                          *
+     *    con: (MySQL server connection) Db connector.   *
+     * @return: (Double array): array with the 'lat',    *
+     *          'lon' and 'dtime' keys. null if nothing  *
+     *           to report.                              *
+     *****************************************************/
+    function get_location($con){
+        $q = mysqli_query($con, "SELECT lat, lon, dtime FROM location WHERE action <> 'F' AND lat IS NOT null AND lon IS NOT null AND dtime > NOW() - INTERVAL 30 MINUTE ORDER BY dtime DESC LIMIT 1;");
+        if (mysqli_num_rows($q) > 0){
+            $r = mysqli_fetch_array($q);
+            $loc = array();
+            $loc["lat"] = $r["lat"];
+            $loc["lon"] = $r["lon"];
+            $loc["dtime"] = $r["dtime"];
+            return $loc;
+        }
+        return null;
     }
     }
-    if ($format != FOR_JSON){
-        //Bad request
-        http_response_code(400);
-        $error = $error . ERR_FORMAT . mysqli_real_escape_string($con, $_GET[GET_FORMAT]);
-        error_log($error);
-        exit(-1);
+
+    /*****************************************************
+     * Prints info about the last location report, in    *
+     * the format:                                       *
+     * [{"lat":"<LAT>","lon":"<LON>","dtime":"<DTIME>"}] *
+     * where <LAT> and <LON> are doubles and <dtime> is  *
+     * the datetime of the report in format:             *
+     * 'YYYY-MM-DD hh:mm:ss'.                            *
+     *                                                   *
+     * @params:                                          *
+     *    con: (MySQL server connection) Db connector.   *
+     * @return: (Double array): array with the 'lat' and *
+     *          'lon' keys. null if nothing to report.   *
+     *****************************************************/
+    function print_location($location){
+        echo("[{\"lat\":\"$location[lat]\",\"lon\":\"$location[lon]\",\"dtime\":\"$location[dtime]\"}]");
     }
     }
-    
+
+
+
+    //Connect to the database
+    $con = startdb('rw');
+
     //Get location
     //Get location
-    $q = mysqli_query($con, "SELECT lat, lon, dtime FROM location WHERE action <> 'F' AND lat IS NOT null AND lon IS NOT null AND dtime > NOW() - INTERVAL 30 MINUTE ORDER BY dtime DESC LIMIT 1;");
-    if (mysqli_num_rows($q) > 0){
-        $r = mysqli_fetch_array($q);
-        switch ($format){
-            case FOR_JSON:
-                echo("[{\"lat\":\"$r[lat]\",\"lon\":\"$r[lon]\",\"dtime\":\"$r[dtime]\"}]");
-                break;
-            default:
-                http_response_code(400);
-                $error = $error . ERR_FORMAT . mysqli_real_escape_string($con, $_GET[GET_FORMAT]);
-                error_log($error);
-                exit(-2);
-        }
-    }
-    else{
+    $location = get_location($con);
+    if($location == null){
         //No content
         //No content
         http_response_code(204);
         http_response_code(204);
         exit(0);
         exit(0);
     }
     }
+    else{
+        // Print the location
+        print_location($location);
+    }
 ?>
 ?>

+ 36 - 23
www/API/v3/sync.php

@@ -51,16 +51,14 @@
                  TAB_POST_IMAGE,           TAB_PHOTO_COMMENT,  TAB_POST_COMMENT,
                  TAB_POST_IMAGE,           TAB_PHOTO_COMMENT,  TAB_POST_COMMENT,
                  TAB_ACTIVITY_COMMENT,     TAB_ACTIVITY_TAG,   TAB_POST_TAG];
                  TAB_ACTIVITY_COMMENT,     TAB_ACTIVITY_TAG,   TAB_POST_TAG];
 
 
-    /****************************************************
+    /*****************************************************
      * This function is called from almost everywhere at *
      * This function is called from almost everywhere at *
      * the beggining of the page. It initializes the     *
      * the beggining of the page. It initializes the     *
-     * session variables, connect to the db, enabling    *
-     * the variable $con for futher use everywhere in    *
-     * the php code, and populates the arrays $user      *
-     * and $permission, with info about the user.        *
+     * session variables and connects to the db.         *
      *                                                   *
      *                                                   *
-     * @return: (db connection): The connection handler. *
-     *****************************************************/
+     * @return: (MySQL server connection): The           *
+     *           connection handler.                     *
+     ****************************************************/
     function startdb(){
     function startdb(){
         //Include the db configuration file. It's somehow like this
         //Include the db configuration file. It's somehow like this
         /*
         /*
@@ -92,6 +90,7 @@
      * SQL injections.                                   *
      * SQL injections.                                   *
      *                                                   *
      *                                                   *
      * @params:                                          *
      * @params:                                          *
+     *    con: (MySQL server connection) Db connector.   *
      *    get: (string array) Contains the GET           *
      *    get: (string array) Contains the GET           *
      *         parameters.                               *
      *         parameters.                               *
      *    param: (string) Name of the parameter.         *
      *    param: (string) Name of the parameter.         *
@@ -113,6 +112,7 @@
      * is not provided, a error log entry is registered  *
      * is not provided, a error log entry is registered  *
      *                                                   *
      *                                                   *
      * @params:                                          *
      * @params:                                          *
+     *    con: (MySQL server connection) Db connector.   *
      *    get: (string array) Contains the GET           *
      *    get: (string array) Contains the GET           *
      *         parameters.                               *
      *         parameters.                               *
      * @return: (string array): Array with the keys      *
      * @return: (string array): Array with the keys      *
@@ -150,6 +150,7 @@
      * user as GET parameters.                           *
      * user as GET parameters.                           *
      *                                                   *
      *                                                   *
      * @params:                                          *
      * @params:                                          *
+     *    con: (MySQL server connection) Db connector.   *
      *    get: (string array) Contains the GET           *
      *    get: (string array) Contains the GET           *
      *         parameters.                               *
      *         parameters.                               *
      * @return: (int array): Array with the version of   *
      * @return: (int array): Array with the version of   *
@@ -212,7 +213,7 @@
      * Only for the tables that will be synced.          *
      * Only for the tables that will be synced.          *
      *                                                   *
      *                                                   *
      * @params:                                          *
      * @params:                                          *
-     *    con: (MySQL server connection) RO mode enough. *
+     *    con: (MySQL server connection) Db connector.   *
      *    tables (String array): List of table.          *
      *    tables (String array): List of table.          *
      * @return: (Assoc Array): Data in the table.        *
      * @return: (Assoc Array): Data in the table.        *
      ****************************************************/
      ****************************************************/
@@ -307,6 +308,9 @@
     /*****************************************************
     /*****************************************************
      * Prints out required tables.                       *
      * Prints out required tables.                       *
      *                                                   *
      *                                                   *
+     * @params:                                          *
+     *    con: (MySQL server connection) Db connector.   *
+     *    tables: (String array) List of tables to sync. *
      * @return: (String): Client IP address.             *
      * @return: (String): Client IP address.             *
      *****************************************************/
      *****************************************************/
     function sync($con, $tables){
     function sync($con, $tables){
@@ -345,25 +349,34 @@
         return $ip;
         return $ip;
     }
     }
 
 
-    /****************************************************
-    * Registers the request in the database.            *
-    *                                                   *
-    * @params:                                          *
-    *    con: (MySQL server connection) RO mode enough. *
-    *    client: (string): The client identifier.       *
-    *    user: (string): A unique end user identifier.  *
-    *    action: (string): Requested action.            *
-    *    section: (string): Requested database section. *
-    *    version: (int): Version of the client db.      *
-    *    new_version: (int): Returned version.          *
-    *    foreground: (int): 1 for fg syncs, 0 for bg.   *
-    *    format: (string): Requested format.            *
-    *    error: (string): Error message to store.       *
-    ****************************************************/
+    /*****************************************************
+     * 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.                             *
+     *****************************************************/
     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.                      *
+     *****************************************************/
     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]');");
     }
     }