123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- //Description: This script will receive jsons from the application.
- // After validating a json it will store it in db.txt.
- // When activated this script will also activate dbcleaner.php.
- include 'dbcleaner.php'; //Will activate dbcleaner.php script every time this script is called.
-
- function read_and_delete_first_line($filename) //This function will delete the first line of db.txt.
- {
- $file = file($filename); //Makes the contents of db.txt into an array.
- array_shift($file); //Removes the first element of the array.
- file_put_contents($filename, $file); //Rewrites db.txt with the contents of the array.
- }
- date_default_timezone_set('America/Puerto_Rico'); //Changes the default time of this PHP script to Puerto Rico.
- $decoded_location = json_decode(file_get_contents('php://input')); //Receives input from app and decodes the json recived.
- //$f = file("unused/testinput.txt"); //For testing purposes.
- //$decoded_location = json_decode($f[0]); //For testing purposes.
- if(isset($decoded_location->latitude) && isset($decoded_location->longitude)) //Checks if the decoded json contains latitude and longitude keys. If false the json will be discarted.
- {
- if(is_float($decoded_location->latitude) && is_float($decoded_location->longitude)) //Checks if the values of latitudes and longitudes are floating numbers. If false the json will be discarted.
- {
- if(($decoded_location->latitude >= -90 && $decoded_location->latitude <= 90) && ($decoded_location->longitude >= -180 && $decoded_location->longitude <= 180))
- {
- if((($decoded_location->latitude >= 18.405912 && $decoded_location->latitude <= 18.407361) && ($decoded_location->longitude >= -66.050604 && $decoded_location->longitude <= -66.041167)) || (($decoded_location->latitude >= 18.403842 && $decoded_location->latitude <= 18.407778) && ($decoded_location->longitude >= -66.048252 && $decoded_location->longitude <= -66.046486))) //Checks if the latitude and longitude values are in the range of real latitudes and longitudes. A latitude cannot exceed from the range (-90,90); A longitude cannot exceed from the range (-180,180). If false the json will be discarted.
- {
- $decoded_location->inTime = date('h:i:s a',time()); //Adds a new property to the object we get after decoding the json and stores the current time.
- $encoded_location = json_encode($decoded_location); //Reencodes the object back into a json with the new inTime property.
- $file = 'db.txt';
- $totalLines = intval(exec("wc -l '$file'")); //Counts how many lines db.txt has.
- //echo $totalLines. "\n"; //For testing purposes
- if($totalLines > 24) //If db.txt has more than a specified amount of lines, it will delete the first line of db.txt.
- {
- read_and_delete_first_line($file);
- }
- $db = fopen("db.txt","a") or die("FAILED TO ACCESS DATABASE"); //Opens db.txt in append mode. If it somehow fails to open the database, an error message will be displayed.
- fwrite($db,$encoded_location. "\n"); //Writes the newly encoded json into db.txt.
- fclose($db); //Closes db.txt
- //$totalLines = intval(exec("wc -l '$file'")); //For testing purposes
- //echo $totalLines; //For testing purposes
- }
- elseif((($decoded_location->latitude >= 18.402011 && $decoded_location->latitude <= 18.404062) && ($decoded_location->longitude <= -66.042888 && $decoded_location->longitude >= -66.051350)) || (($decoded_location->latitude >= 18.401272 && $decoded_location->latitude <= 18.404016) && ($decoded_location->longitude >= -66.050826 && $decoded_location->longitude <= -66.043554)))
- {
- $decoded_location->inTime = date('h:i:s a',time()); //Adds a new property to the object we get after decoding the json and stores the current time.
- $encoded_location = json_encode($decoded_location); //Reencodes the object back into a json with the new inTime property.
- $file = 'db2.txt';
- $totalLines = intval(exec("wc -l '$file'")); //Counts how many lines db.txt has. //echo $totalLines. "\n"; //For testing purposes
- if($totalLines > 24) //If db.txt has more than a specified amount of lines, it will delete the first line of db.txt.
- {
- read_and_delete_first_line($file);
- }
- $db = fopen("db2.txt","a") or die("FAILED TO ACCESS DATABASE"); //Opens db.txt in append mode. If it somehow fails to open the database, an error message will be displayed.
- fwrite($db,$encoded_location. "\n"); //Writes the newly encoded json into db.txt.
- fclose($db); //Closes db.txt
- }
- }
- }
- }
- ?>
|