Alexander Torres 5 лет назад
Родитель
Сommit
cdec5ec527
3 измененных файлов: 69 добавлений и 0 удалений
  1. 23
    0
      dbcleaner.php
  2. 6
    0
      dberase.php
  3. 40
    0
      receiver.php

+ 23
- 0
dbcleaner.php Просмотреть файл

@@ -0,0 +1,23 @@
1
+<?php
2
+	//Description: When activated this script will remove any old entries from db.txt.
3
+	
4
+	date_default_timezone_set('America/Puerto_Rico'); //Changes the default timezone of the script to Puerto Rico. 
5
+	$time = date("h:i:s",strtotime('-15 seconds')); //Stores the current time minus the a certain amount of time.
6
+	$db = file('/var/www/html/db.txt'); //Access db.txt and stores each line into an array. 
7
+	$fsize = count($db); //Get the size of the db array.
8
+	$change = False; //Boolian used to indicate if the file had to be changed. 
9
+	for($i = 0;$i < $fsize; $i++) //Loop that will iterate through the db array.
10
+	{
11
+		$decode =  json_decode($db[$i]); //Gets the jsons stored in db and decodes them. 
12
+		if(date("h:i:s",strtotime($decode->inTime)) < date("h:i:s",strtotime($time))) //Compares if the inTime of stored in each json is less than the one in the time variable. 
13
+		{
14
+			unset($db[$i]);//Removes the json in db[i].
15
+			$change = True;//Indicates that their was change in db.
16
+		}
17
+	}
18
+	if ($change == True)//Check if there was a change in db.
19
+	{
20
+		file_put_contents("/var/www/html/db.txt", $db);//Rewrites the db.txt with the contents of db.
21
+	}
22
+
23
+?>

+ 6
- 0
dberase.php Просмотреть файл

@@ -0,0 +1,6 @@
1
+<?php
2
+	//Description:	When activated, this script will erase everything in db.txt. 
3
+	//		Cron will periodically activate this script everyday at 00:00:00.
4
+
5
+	file_put_contents("/var/www/html/db.txt", ""); //Deletes everything from db.txt.
6
+?>

+ 40
- 0
receiver.php Просмотреть файл

@@ -0,0 +1,40 @@
1
+<?php
2
+	//Description:	This script will receive jsons from the application.
3
+	//		After validating a json it will store it in db.txt.
4
+	//		When activated this script will also activate dbcleaner.php.
5
+	include 'dbcleaner.php'; //Will activate dbcleaner.php script everyday.
6
+
7
+	function read_and_delete_first_line($filename) //This function will delete the first line of db.txt.
8
+        {
9
+                $file = file($filename); //Makes the contents of db.txt into an array.
10
+                array_shift($file); //Removes the first element of the array.
11
+                file_put_contents($filename, $file); //Rewrites db.txt with the contents of the array.
12
+        }
13
+	date_default_timezone_set('America/Puerto_Rico'); //Changes the default time of this PHP script to Puerto Rico.
14
+        $decoded_location  = json_decode(file_get_contents('php://input')); //Receives input from app and decodes the json recived.
15
+	//$f = file("unused/testinput.txt");//For testing purposes.
16
+	//$decoded_location  = json_decode($f[0]); //For testing purposes.
17
+	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.
18
+	{
19
+		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.
20
+		{	
21
+			if(($decoded_location->latitude >= -90 && $decoded_location->latitude <= 90) && ($decoded_location->longitude >= -180 && $decoded_location->longitude <= 180)) //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.
22
+			{
23
+				$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.
24
+				$encoded_location = json_encode($decoded_location); //Reencodes the object back into a json with the new inTime property.
25
+        			$file = 'db.txt';
26
+        			$totalLines = intval(exec("wc -l '$file'")); //Counts how many lines db.txt has.
27
+        			//echo $totalLines. "\n"; //For testing purposes
28
+        			if($totalLines > 24) //If db.txt has more than a specified amount of lines, it will delete the first line of db.txt.
29
+				{
30
+					read_and_delete_first_line($file);
31
+				}
32
+				$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.
33
+				fwrite($db,$encoded_location. "\n"); //Writes the newly encoded json into db.txt.
34
+				fclose($db); //Closes db.txt
35
+        			//$totalLines = intval(exec("wc -l '$file'")); //For testing purposes
36
+				//echo $totalLines; //For testing purposes
37
+			}
38
+		}
39
+	}
40
+?>