瀏覽代碼

Manejar y storage 2 rutas

Alexander Torres 4 年之前
父節點
當前提交
99c35818db
共有 3 個檔案被更改,包括 92 行新增0 行删除
  1. 29
    0
      dbcleaner.php
  2. 6
    0
      dberase.php
  3. 57
    0
      receiver.php

+ 29
- 0
dbcleaner.php 查看文件

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

+ 6
- 0
dberase.php 查看文件

@@ -0,0 +1,6 @@
1
+<?php
2
+//Description: Erases everything in both db.txt, db2.txt.
3
+
4
+file_put_contents("/var/www/html/db.txt", "");
5
+file_put_contents("/var/www/html/db2.txt", "");
6
+?>

+ 57
- 0
receiver.php 查看文件

@@ -0,0 +1,57 @@
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 every time this script is called.
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))
22
+			{
23
+				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.
24
+				{
25
+					$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.
26
+					$encoded_location = json_encode($decoded_location); //Reencodes the object back into a json with the new inTime property.
27
+        				$file = 'db.txt';
28
+        				$totalLines = intval(exec("wc -l '$file'")); //Counts how many lines db.txt has.
29
+        				//echo $totalLines. "\n"; //For testing purposes
30
+        				if($totalLines > 24) //If db.txt has more than a specified amount of lines, it will delete the first line of db.txt.
31
+					{
32
+						read_and_delete_first_line($file);
33
+					}
34
+					$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.
35
+					fwrite($db,$encoded_location. "\n"); //Writes the newly encoded json into db.txt.
36
+					fclose($db); //Closes db.txt
37
+        				//$totalLines = intval(exec("wc -l '$file'")); //For testing purposes
38
+					//echo $totalLines; //For testing purposes
39
+				}
40
+				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)))
41
+				{
42
+					$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.
43
+			        	$encoded_location = json_encode($decoded_location); //Reencodes the object back into a json with the new inTime property.
44
+			        	$file = 'db2.txt';
45
+					$totalLines = intval(exec("wc -l '$file'")); //Counts how many lines db.txt has.					//echo $totalLines. "\n"; //For testing purposes
46
+					if($totalLines > 24) //If db.txt has more than a specified amount of lines, it will delete the first line of db.txt.
47
+					{
48
+						read_and_delete_first_line($file);
49
+					}
50
+					$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.
51
+					fwrite($db,$encoded_location. "\n"); //Writes the newly encoded json into db.txt.
52
+					fclose($db); //Closes db.txt
53
+				}
54
+			}
55
+		}
56
+	}
57
+?>