Przeglądaj źródła

Implemented location API v1

seavenois 9 lat temu
rodzic
commit
08297aff07
2 zmienionych plików z 84 dodań i 3 usunięć
  1. 82 0
      www/API/v1/location.php
  2. 2 3
      www/API/v1/notifications.php

+ 82 - 0
www/API/v1/location.php

@@ -0,0 +1,82 @@
+<?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. *
+	****************************************************/
+	function startdb(){
+		//Include the db configuration file. It's somehow like this
+		/*
+		<?php
+			$host = 'XXXX';
+			$db_name = 'XXXX';
+			$username_ro = 'XXXX';
+			$username_rw = 'XXXX';
+			$pass_ro = 'XXXX';
+			$pass_rw = 'XXXX';
+		?>
+		*/
+		include('../../.htpasswd');
+		
+		//Connect to to database
+		$con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
+		
+		//Set encoding options
+		mysqli_set_charset($con, 'utf-8');
+		header('Content-Type: text/html; charset=utf8');
+		mysqli_query($con, 'SET NAMES utf8;');
+		
+		//Return the db connection
+		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;
+	}
+	if ($format != FOR_JSON){
+		//Bad request
+		http_response_code(400);
+		$error = $error . ERR_FORMAT . mysqli_real_escape_string($con, $_GET[GET_FORMAT]);
+	}
+	
+	//Get location
+	$q = mysqli_query($con, "SELECT lat, lon, dtime FROM location WHERE action = 'report' 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]);
+		}
+	}
+	else{
+		//No content
+		http_response_code(204);
+	}
+?>

+ 2 - 3
www/API/v1/notifications.php

@@ -121,6 +121,5 @@
 	if (strlen($error) == 0){
 		echo(show_notifications($con, $target, $format));
 	}
-	
-	//Log the request
-	//log_sync(); //TODO;
+
+?>