Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.44 KB | None | 0 0
  1. $dbhost = "DERP FAKE HOST";
  2. $dbdata = "DERP FAKE DATABASE";
  3. $dbuser = "DERP FAKE USER";
  4. $dbpass = "DERP FAKE PASS";
  5.  
  6.  
  7.  
  8.  
  9.  
  10. // Put needed parameter names from GetPOI request in an array called $keys.
  11. $keys = array( "layerName", "lat", "lon", "radius" );
  12.  
  13. // Initialize an empty associative array.
  14. $value = array();
  15.  
  16. try {
  17.   // Retrieve parameter values using $_GET and put them in $value array with parameter name as key.
  18.   foreach( $keys as $key ) {
  19.  
  20.     if ( isset($_GET[$key]) )
  21.       $value[$key] = $_GET[$key];
  22.     else
  23.       throw new Exception($key ." parameter is not passed in GetPOI request.");
  24.   }//foreach
  25. }//try
  26. catch(Exception $e) {
  27.   echo 'Message: ' .$e->getMessage();
  28. }//catch
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. // Connect to predefined MySQl database.  
  36. $db = new PDO( "mysql:host=$dbhost; dbname=$dbdata", $dbuser, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND =>  "SET NAMES utf8") );
  37.  
  38. // set the error reporting attribute to Exception.
  39. $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  // Create an empty array named response.
  47.  $response = array();
  48.  
  49.  // Assign cooresponding values to mandatory JSON response keys.
  50.  $response["layer"] = $value["layerName"];
  51.  
  52.  // Use Gethotspots() function to retrieve POIs with in the search range.  
  53.  $response["hotspots"] = Gethotspots( $db, $value );
  54.  
  55.  // if there is no POI found, return a custom error message.
  56.  if ( empty( $response["hotspots"] ) ) {
  57.     $response["errorCode"] = 20;
  58.     $response["errorString"] = "No POI found. Please adjust the range.";
  59. }//if
  60. else {
  61.     $response["errorCode"] = 0;
  62.     $response["errorString"] = "ok";
  63. }//else
  64.  
  65.  
  66.  
  67.  
  68.  
  69. function Gethotspots( $db, $value ) {
  70.  
  71. /* Create the SQL query to retrieve POIs within the "radius" returned from GetPOI request.
  72.        Returned POIs are sorted by distance and the first 50 POIs are selected.
  73.        The distance is caculated based on the Haversine formula.
  74.        Note: this way of calculation is not scalable for querying large database.
  75. */
  76.    
  77.   // Use PDO::prepare() to prepare SQL statement.
  78.   // This statement is used due to security reasons and will help prevent general SQL injection attacks.
  79.   // ":lat1", ":lat2", ":long" and ":radius" are named parameter markers for which real values
  80.   // will be substituted when the statement is executed.
  81.   // $sql is returned as a PDO statement object.
  82.   $sql = $db->prepare( "
  83.              SELECT id,
  84.                     attribution,
  85.                     title,
  86.                     lat,
  87.                     lon,
  88.                     imageURL,
  89.                     line4,
  90.                     line3,
  91.                     line2,
  92.                     type,
  93.                     dimension,
  94.                     (((acos(sin((:lat1 * pi() / 180)) * sin((lat * pi() / 180)) +
  95.                         cos((:lat2 * pi() / 180)) * cos((lat * pi() / 180)) *
  96.                       cos((:long  - lon) * pi() / 180))
  97.                      ) * 180 / pi()) * 60 * 1.1515 * 1.609344 * 1000) as distance
  98.            FROM POI_Table
  99.            HAVING distance < :radius
  100.            ORDER BY distance ASC
  101.            LIMIT 0, 50 " );
  102.  
  103.   // PDOStatement::bindParam() binds the named parameter markers to the specified parameter values.
  104.   $sql->bindParam( ':lat1', $value['lat'], PDO::PARAM_STR );
  105.   $sql->bindParam( ':lat2', $value['lat'], PDO::PARAM_STR );
  106.   $sql->bindParam( ':long', $value['lon'], PDO::PARAM_STR );
  107.   $sql->bindParam( ':radius', $value['radius'], PDO::PARAM_INT );
  108.    
  109.   // Use PDO::execute() to execute the prepared statement $sql.
  110.   $sql->execute();
  111.    
  112.   // Iterator for the response array.
  113.   $i = 0;
  114.  
  115.   // Use fetchAll to return an array containing all of the remaining rows in the result set.
  116.   // Use PDO::FETCH_ASSOC to fetch $sql query results and return each row as an array indexed by column name.
  117.   $pois = $sql->fetchAll(PDO::FETCH_ASSOC);
  118.  
  119.   /* Process the $pois result */
  120.  
  121.   // if $pois array is empty, return empty array.
  122.   if ( empty($pois) ) {
  123.      
  124.       $response["hotspots"] = array ();
  125.    
  126.   }//if
  127.   else {
  128.      
  129.       // Put each POI information into $response["hotspots"] array.
  130.      foreach ( $pois as $poi ) {
  131.        
  132.         // If not used, return an empty actions array.
  133.         $poi["actions"] = array();
  134.        
  135.         // Store the integer value of "lat" and "lon" using predefined function ChangetoIntLoc.
  136.         $poi["lat"] = ChangetoIntLoc( $poi["lat"] );
  137.         $poi["lon"] = ChangetoIntLoc( $poi["lon"] );
  138.    
  139.          // Change to Int with function ChangetoInt.
  140.         $poi["type"] = ChangetoInt( $poi["type"] );
  141.         $poi["dimension"] = ChangetoInt( $poi["dimension"] );
  142.    
  143.         // Change to demical value with function ChangetoFloat
  144.         $poi["distance"] = ChangetoFloat( $poi["distance"] );
  145.    
  146.         // Put the poi into the response array.
  147.         $response["hotspots"][$i] = $poi;
  148.         $i++;
  149.       }//foreach
  150.  
  151.   }//else
  152.  
  153.   return $response["hotspots"];
  154. }//Gethotspots
  155.  
  156.  
  157.  
  158.  
  159. // Convert a decimal GPS latitude or longitude value to an integer by multiplying by 1000000.
  160. //
  161. // Arguments:
  162. //   value_Dec ; The decimal latitude or longitude GPS value.
  163. //
  164. // Returns:
  165. //   int ; The integer value of the latitude or longitude.
  166. //
  167. function ChangetoIntLoc( $value_Dec ) {
  168.  
  169.   return $value_Dec * 1000000;
  170.  
  171. }//ChangetoIntLoc
  172.  
  173.  
  174.  
  175.  
  176. // Change a string value to integer.
  177. //
  178. // Arguments:
  179. //   string ; A string value.
  180. //
  181. // Returns:
  182. //   Int ; If the string is empty, return NULL.
  183. //
  184. function ChangetoInt( $string ) {
  185.  
  186.   if ( strlen( trim( $string ) ) != 0 ) {
  187.  
  188.     return (int)$string;
  189.   }
  190.   else
  191.       return NULL;
  192. }//ChangetoInt
  193.  
  194.  
  195.  
  196.  
  197. // Change a string value to float
  198. //
  199. // Arguments:
  200. //   string ; A string value.
  201. //
  202. // Returns:
  203. //   float ; If the string is empty, return NULL.
  204. //
  205. function ChangetoFloat( $string ) {
  206.  
  207.   if ( strlen( trim( $string ) ) != 0 ) {
  208.  
  209.     return (float)$string;
  210.   }
  211.   else
  212.       return NULL;
  213. }//ChangetoFloat
  214.  
  215.  
  216.  
  217.  
  218. /* All data is in $response, print it into JSON format.*/
  219.    
  220.     // Put the JSON representation of $response into $jsonresponse.
  221.     $jsonresponse = json_encode( $response );
  222.    
  223.     // Declare the correct content type in HTTP response header.
  224.     header( "Content-type: application/json; charset=utf-8" );
  225.    
  226.     // Print out Json response.
  227.     echo $jsonresponse;
  228.  
  229.     /* Close the MySQL connection.*/
  230.    
  231.     // Set $db to NULL to close the database connection.
  232.     $db=null;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement