Repositorio del curso CCOM4030 el semestre B91 del proyecto Trolley

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