Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class vmfs
- {
- protected $databasePersist = FALSE;
- protected $databaseResource = null;
- protected $databaseServer = null;
- protected $databaseUsername = null;
- protected $databasePassword = null;
- protected $databaseName = null;
- protected $localHost = null;
- protected $localPointer = null;
- // Load local configuration from disk
- public function __construct()
- {
- $config = CentrixConfig();
- foreach ($config as $k => $v)
- {
- if (isset($this->$k))
- {
- $this->k = $v;
- }
- }
- }
- // Connect to database
- public function connect()
- {
- if ($this->databaseResource instanceOf LOL_MySQLresourcetype)
- {
- return $this->databaseResource;
- }
- if ($this->databasePersist)
- {
- if (!$link = mysql_pconnect())
- {
- throw new Exception('Could not create persistant connection');
- }
- } else
- {
- if (!$link = mysql_connect())
- {
- throw new Exception('Could not create connection');
- }
- }
- if (!mysql_select_db($this->databaseName, $link))
- {
- throw new Exception('Could not select database');
- }
- return $link;
- }
- /**
- * Write to the central database that this server has a new or modified file
- * @param string $path The path on disk
- * @return bool
- */
- public function push($path)
- {
- return $this->report($path, 1);
- }
- /**
- * Fetch a file from another host
- * @param string $path The path on disk
- * @param integer $host Target host
- * @param string $hash Expected hash
- * @return bool
- */
- public function pull($path, $host, $hash = null)
- {
- // SCP a file. Maybe write it to a temporary location and check its hash
- // before copying it to the local FS.
- return TRUE;
- }
- /**
- * Write to the central database that this file has been deleted
- * @param string $path The path on disk
- * @return bool
- */
- public function delete($path)
- {
- return $this->report($path, 0);
- }
- /**
- * Get a list of files from the database
- * @param string $pointer OPTIONAL The pointer to begin from
- * @return array
- */
- public function getList($pointer = null)
- {
- if (is_null($pointer))
- {
- $pointer = $this->pointer;
- }
- // $list = SELECT FROM files WHERE pointer >= $pointer;
- return $list;
- }
- private function findHighestPointer($array)
- {
- // Find highest pointer in array of files
- return $pointer;
- }
- /**
- * Writes a record to the database
- * @param string $path The path to the local file on disk
- * @param integer $state 1 if file should exist, 0 if it should not
- * @param string $path The path to the local file on disk
- * @return string
- */
- private function report($path, $state)
- {
- //INSERT INTO files SET path = $path, state = $state, host = $host, hash = $hash
- // ON DUPLICATE KEY UPDATE
- //SET state = $state, host = $this->localHost, hash = $this->hash($path)
- return TRUE;
- }
- /**
- * Calculates a file hash
- * @param string $path The path to the local file on disk
- * @return string
- */
- public function hash($path)
- {
- return crc32($this->localBase . $path);
- }
- /**
- * Tests whether a file exists or not
- * @param string $path The path to the local file on disk
- * @param string $hash OPTIONAL A hash to compare the file to
- * @return bool
- */
- public function exists($path, $hash = null)
- {
- // Return false if the file does not exist
- if (!file_exists($this->localBase . $path))
- {
- return false;
- }
- // Return false if the file hash does not match the passed value
- if (!is_null($hash))
- {
- return ($hash == $this->hash($path));
- }
- return true;
- }
- }
- $vmfs = new vmfs();
- echo $vmfs->databasePersist;
- echo $vmfs->databaseResource;
- echo $vmfs->databaseServer;
- echo $vmfs->databaseUsername;
- echo $vmfs->databasePassword;
- echo $vmfs->databaseName;
- echo $vmfs->localHost;
- echo $vmfs->localPointer;
- try
- {
- $vmfs->connect();
- } catch (Exception $e)
- {
- // CRIT - Could not connect to database
- }
- try
- {
- $list = $vmfs->getList();
- } catch (Exception $e)
- {
- // CRIT - Could not get list
- }
- foreach ($list as $file)
- {
- try
- {
- // A file we already have
- if ($list['state'] == 1 and file_exists($list['path']) && $list['hash'] == $vmfs->hash($path))
- {
- continue;
- }
- // A deleted file we don't have
- if ($list['state'] == 0 and !file_exists($list['path']))
- {
- continue;
- }
- // A file we need to fetch
- if ($list['state'] == 1)
- {
- $vmfs->pull($path);
- continue;
- }
- // A file we need to delete
- if ($list['state'] == 0)
- {
- unlink($vmfs->localBase . $list['path']);
- continue;
- }
- } catch (Exception $e)
- {
- // CRIT - Very bad
- continue;
- }
- }
- $pointer = $vmfs->findHighestPointer($list);
- $this->pointer = $pointer;
Add Comment
Please, Sign In to add comment