Guest User

Untitled

a guest
Oct 29th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. <?php
  2.  
  3. class vmfs
  4. {
  5.  
  6. protected $databasePersist = FALSE;
  7. protected $databaseResource = null;
  8. protected $databaseServer = null;
  9. protected $databaseUsername = null;
  10. protected $databasePassword = null;
  11. protected $databaseName = null;
  12. protected $localHost = null;
  13. protected $localPointer = null;
  14.  
  15. // Load local configuration from disk
  16. public function __construct()
  17. {
  18. $config = CentrixConfig();
  19.  
  20. foreach ($config as $k => $v)
  21. {
  22. if (isset($this->$k))
  23. {
  24. $this->k = $v;
  25. }
  26. }
  27. }
  28.  
  29. // Connect to database
  30. public function connect()
  31. {
  32. if ($this->databaseResource instanceOf LOL_MySQLresourcetype)
  33. {
  34. return $this->databaseResource;
  35. }
  36.  
  37. if ($this->databasePersist)
  38. {
  39. if (!$link = mysql_pconnect())
  40. {
  41. throw new Exception('Could not create persistant connection');
  42. }
  43. } else
  44. {
  45. if (!$link = mysql_connect())
  46. {
  47. throw new Exception('Could not create connection');
  48. }
  49. }
  50.  
  51. if (!mysql_select_db($this->databaseName, $link))
  52. {
  53. throw new Exception('Could not select database');
  54. }
  55.  
  56. return $link;
  57. }
  58.  
  59. /**
  60. * Write to the central database that this server has a new or modified file
  61. * @param string $path The path on disk
  62. * @return bool
  63. */
  64. public function push($path)
  65. {
  66. return $this->report($path, 1);
  67. }
  68.  
  69. /**
  70. * Fetch a file from another host
  71. * @param string $path The path on disk
  72. * @param integer $host Target host
  73. * @param string $hash Expected hash
  74. * @return bool
  75. */
  76. public function pull($path, $host, $hash = null)
  77. {
  78. // SCP a file. Maybe write it to a temporary location and check its hash
  79. // before copying it to the local FS.
  80.  
  81. return TRUE;
  82. }
  83.  
  84. /**
  85. * Write to the central database that this file has been deleted
  86. * @param string $path The path on disk
  87. * @return bool
  88. */
  89. public function delete($path)
  90. {
  91. return $this->report($path, 0);
  92. }
  93.  
  94. /**
  95. * Get a list of files from the database
  96. * @param string $pointer OPTIONAL The pointer to begin from
  97. * @return array
  98. */
  99. public function getList($pointer = null)
  100. {
  101. if (is_null($pointer))
  102. {
  103. $pointer = $this->pointer;
  104. }
  105.  
  106. // $list = SELECT FROM files WHERE pointer >= $pointer;
  107.  
  108. return $list;
  109. }
  110.  
  111. private function findHighestPointer($array)
  112. {
  113. // Find highest pointer in array of files
  114.  
  115. return $pointer;
  116. }
  117.  
  118. /**
  119. * Writes a record to the database
  120. * @param string $path The path to the local file on disk
  121. * @param integer $state 1 if file should exist, 0 if it should not
  122. * @param string $path The path to the local file on disk
  123. * @return string
  124. */
  125. private function report($path, $state)
  126. {
  127. //INSERT INTO files SET path = $path, state = $state, host = $host, hash = $hash
  128. // ON DUPLICATE KEY UPDATE
  129. //SET state = $state, host = $this->localHost, hash = $this->hash($path)
  130.  
  131. return TRUE;
  132. }
  133.  
  134. /**
  135. * Calculates a file hash
  136. * @param string $path The path to the local file on disk
  137. * @return string
  138. */
  139. public function hash($path)
  140. {
  141. return crc32($this->localBase . $path);
  142. }
  143.  
  144. /**
  145. * Tests whether a file exists or not
  146. * @param string $path The path to the local file on disk
  147. * @param string $hash OPTIONAL A hash to compare the file to
  148. * @return bool
  149. */
  150. public function exists($path, $hash = null)
  151. {
  152. // Return false if the file does not exist
  153. if (!file_exists($this->localBase . $path))
  154. {
  155. return false;
  156. }
  157.  
  158. // Return false if the file hash does not match the passed value
  159. if (!is_null($hash))
  160. {
  161. return ($hash == $this->hash($path));
  162. }
  163.  
  164. return true;
  165. }
  166.  
  167. }
  168.  
  169.  
  170.  
  171. $vmfs = new vmfs();
  172.  
  173. echo $vmfs->databasePersist;
  174. echo $vmfs->databaseResource;
  175. echo $vmfs->databaseServer;
  176. echo $vmfs->databaseUsername;
  177. echo $vmfs->databasePassword;
  178. echo $vmfs->databaseName;
  179. echo $vmfs->localHost;
  180. echo $vmfs->localPointer;
  181.  
  182. try
  183. {
  184. $vmfs->connect();
  185. } catch (Exception $e)
  186. {
  187. // CRIT - Could not connect to database
  188. }
  189.  
  190. try
  191. {
  192. $list = $vmfs->getList();
  193. } catch (Exception $e)
  194. {
  195. // CRIT - Could not get list
  196. }
  197.  
  198. foreach ($list as $file)
  199. {
  200. try
  201. {
  202. // A file we already have
  203. if ($list['state'] == 1 and file_exists($list['path']) && $list['hash'] == $vmfs->hash($path))
  204. {
  205. continue;
  206. }
  207.  
  208. // A deleted file we don't have
  209. if ($list['state'] == 0 and !file_exists($list['path']))
  210. {
  211. continue;
  212. }
  213.  
  214. // A file we need to fetch
  215. if ($list['state'] == 1)
  216. {
  217. $vmfs->pull($path);
  218.  
  219. continue;
  220. }
  221.  
  222. // A file we need to delete
  223. if ($list['state'] == 0)
  224. {
  225. unlink($vmfs->localBase . $list['path']);
  226. continue;
  227. }
  228. } catch (Exception $e)
  229. {
  230. // CRIT - Very bad
  231. continue;
  232. }
  233. }
  234.  
  235. $pointer = $vmfs->findHighestPointer($list);
  236.  
  237. $this->pointer = $pointer;
Add Comment
Please, Sign In to add comment