Advertisement
secondcoming

Untitled

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