Advertisement
secondcoming

delete_abandoned_vehicles.php

Jul 22nd, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.25 KB | None | 0 0
  1. <?php
  2.  
  3.     echo "================================\nDatabase Cleanup Started:\n================================\n";
  4.    
  5.         // Database Connection Setup
  6.     // -------------------------------------------------------
  7.     $dbhost = '127.0.0.1';
  8.     $dbname = 'exile';
  9.     $dbuser = 'user';
  10.     $dbpass = 'password';
  11.     // -------------------------------------------------------
  12.     $db_local = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  13.  
  14.     $filePath = 'C:\\xampp\\htdocs\\logs\\';
  15.    
  16.     if (!file_exists($filePath))
  17.     {
  18.         mkdir($filePath, 0777, true);
  19.     }
  20.    
  21.     // -------------------------------------------------------
  22.  
  23.     $time = date('Y-m-d G:i:s');
  24.     $msg = "\n\n";
  25.     $msg .= "======================================================\n";
  26.     $msg .= "Delete abandoned vehicles: $time\n";
  27.     $msg .= "======================================================\n";
  28.    
  29.    
  30.     // delete Vehicles not used in 48 hours and not parked in a territory
  31.     $sql = "SELECT * FROM vehicle WHERE last_updated_at < NOW() - INTERVAL 48 HOUR AND last_updated_at <> '0000-00-00 00:00:00'";
  32.     $result = mysqli_query($db_local, $sql);
  33.     $vehicleCount = 0;
  34.     while($row = mysqli_fetch_object($result))
  35.     {
  36.         $VehicleID = $row->id; 
  37.         $VehicleClass = $row->class;
  38.         $VehicleX = $row->position_x;
  39.         $VehicleY = $row->position_y;
  40.         $VehicleOwnerUID = $row->account_uid;
  41.         $IsLocked = -1;
  42.         $VehicleLastUpdated = $row->last_updated_at;
  43.  
  44.        
  45.         // Create array of territories
  46.         $territoryArray = [];
  47.        
  48.         $sql3 = "SELECT position_x, position_y, name FROM territory ORDER BY name";
  49.         $result3 = mysqli_query($db_local, $sql3);
  50.         while($row3 = mysqli_fetch_object($result3))
  51.         {
  52.             $position_x = $row3->position_x;
  53.             $position_y = $row3->position_y;
  54.             $territoryName = $row3->name;
  55.             array_push($territoryArray,$position_x.','.$position_y.',100,'.$territoryName);
  56.         }
  57.  
  58.         $InTerritoryRange = 0;
  59.        
  60.         foreach ($territoryArray as $coords)
  61.         {
  62.             $territoryCoords = explode(",", $coords);
  63.             $territoryX = $territoryCoords[0];
  64.             $territoryY = $territoryCoords[1];
  65.             $territoryRadius = $territoryCoords[2];
  66.             $territoryName = $territoryCoords[3];
  67.            
  68.             if ((($VehicleX-$territoryX)**2 + ($VehicleY-$territoryY)**2 <= $territoryRadius**2))     // inside territory
  69.             {
  70.                 $InTerritoryRange = 1;
  71.             }          
  72.         }
  73.         if($InTerritoryRange == 0) // not in range of a territory
  74.         {
  75.             $vehicleCount = $vehicleCount + 1;
  76.             $sql2 = "SELECT name FROM account WHERE uid = '$VehicleOwnerUID'";
  77.             $result2 = mysqli_query($db_local, $sql2);
  78.             $row2 = mysqli_fetch_object($result2);
  79.             $OwnerName = $row2->name;
  80.            
  81.             $sql2 = "DELETE FROM vehicle WHERE id = '$VehicleID'";
  82.             $result2 = mysqli_query($db_local, $sql2);
  83.             $IsLocked = 0;
  84.             $msg .=  "$VehicleClass ($VehicleID) abandoned by $OwnerName ($VehicleOwnerUID) at $VehicleLastUpdated has been deleted\n";            
  85.         }
  86.     }
  87.     $msg .= "\n=========================================\n$vehicleCount vehicles deleted\n=========================================";
  88.  
  89.     echo $msg;
  90.     LogChanges($msg,$filePath.'Abandoned.log');
  91.     exit();
  92.  
  93.     function LogChanges($text,$filename)
  94.     {
  95.       // open log file
  96.  
  97.       //echo '<hr>'.$filename.'<hr>';
  98.       $fh = fopen($filename, "a") or die("Could not open log file.");
  99.       fwrite($fh, "$text") or die("Could not write file!");
  100.       fclose($fh);
  101.     }
  102. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement