Guest User

Untitled

a guest
Nov 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.93 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Zde je konfigurace připojení a nalezení projektu.
  5. * Cesty zapisujte relativně v závislosti na pozici programu Updater.
  6. * Updater musí být umístěn v rootu projektu
  7. * Všechny adresy uvádějte bez konečného lomítka:
  8. * Napříkad: "/www/projekt1/trunk" ne takhle: "/www/projekt1/thrunk/"!!!!!!!!!!!
  9. */
  10. //link na FPT
  11. $source_ftp = "ftp.promitanivlukavci.unas.cz";
  12. //$source_ftp = "guardian.ebozp.cz";
  13. //usrname na server na FTP
  14. $source_usrname = "promitanivlukavci.unas.cz";
  15. //$source_usrname = "guardian.ebozp.cz";
  16. //heslo na FTP
  17. $source_password = "m67ukmsc";
  18. //$source_password = "vykoureny4pera";
  19. //folder na FTP
  20. $remote_folder = "test";
  21.  
  22. //logovaci soubor
  23. $source_log = "updater_log";
  24.  
  25. //ingnor list
  26. $source_ignore_list = "updater_ignorelist";
  27.  
  28. //zdrojova hashovaci tabulka
  29. $source_hash_table = "updater_hashtable";
  30.  
  31. /*
  32. * Začátek samotného programu
  33. * Inicializuji si třídu FTP, která vše vykoná
  34. */
  35. $ftp = new ftp($source_ftp, $source_usrname, $source_password, $source_ignore_list, $source_log, $remote_folder, $source_hash_table);
  36. if ($ftp->connect()) {
  37. $ftp->write_log("Přihlášen na FTP!");
  38. //jdu do dir na webu
  39. $ftp->chdir($ftp->remote_folder);
  40.  
  41. //inituju deletelist
  42. $ftp->init_delete_list($ftp->dir_list($ftp->current_dir()));
  43.  
  44. $cykly = 0;
  45.  
  46. //projizdim locale adresar
  47. for ($i = 0; $i + 1 <= sizeof($ftp->directory_todo); $i++) {
  48. //osetreni deletovani adresaru na netu
  49. if ($i != 0) {
  50. $ftp->chdir("/" . $ftp->remote_folder . "/" . $ftp->directory_todo[$i]);
  51. $ftp->init_delete_list($ftp->dir_list($ftp->current_dir()));
  52. }
  53. //start rootu localu
  54. $directory = $ftp->dir_list_local($ftp->directory_todo[$i]);
  55.  
  56. //projizdim adresáře
  57. foreach ($directory as $key => $value) {
  58. $cykly++;
  59. echo "DIR:" . $ftp->directory_todo[$i];
  60. echo "-> TODO:" . $value;
  61. $value_ke_kopceni_souboru = $value;
  62.  
  63. $ftp->chdir("/" . $ftp->remote_folder . "/" . $ftp->directory_todo[$i]);
  64.  
  65. //osetreni zanorenych adresářů
  66. if ($value != "." && $value != ".." && $i != 0)
  67. $value = $ftp->directory_todo[$i] . $value;
  68.  
  69. //relevantni adresare pridavam do fronty
  70. if ($ftp->is_relevant_dir_local($value) && !$ftp->is_ignored($value)) {
  71. $ftp->directory_todo[] = $value . "/";
  72. echo " - davam do fronty";
  73.  
  74. //directory nemazu
  75. $ftp->deletelist["/" . $ftp->remote_folder . "/" . $value] = false;
  76.  
  77. $x = strrchr($value, "/");
  78. $y = substr($value, $x - 1);
  79. if ($ftp->make_dir($y)) {
  80. $ftp->make_folder += 1;
  81. }
  82. echo ": make";
  83. }
  84. //relevantni soubory ke kopceni
  85. elseif ($ftp->is_relevant_file_local($value) && !$ftp->is_ignored($value)) {
  86. echo " - zkousim";
  87.  
  88. //soubor nemazu
  89. $ftp->deletelist["/" . $ftp->remote_folder . "/" . $value] = false;
  90.  
  91. if ($ftp->compare($value)) {
  92. echo ": stejne";
  93. } else {
  94. echo ": jine - kopiruju: " . $value . " ";
  95. $ftp->copy_file += 1;
  96. $ftp->copy_file_byte += ( filesize($value) / 1024);
  97. echo "(" . $value . ":" . $value_ke_kopceni_souboru . ") ->";
  98. if ($ftp->put($value, $value_ke_kopceni_souboru))
  99. echo "true";
  100. else
  101. echo "false";
  102. }
  103. }
  104. else
  105. echo " -ignore";
  106. echo "<br />";
  107. }
  108. }
  109.  
  110. //mazu server files
  111. $ftp->delete_remote_files();
  112. //savuje hashtable
  113. $ftp->save_hashtable();
  114.  
  115.  
  116. echo "<br>Počet prozkoušených adresářů:" . $i;
  117. echo "<br>Počet prozkoušených INODů:" . $cykly;
  118. $ftp->write_log("Operace proběhla úspěšně! Na serveru vytvořeno " . $ftp->make_folder . " adresářů a zkopírováno " . $ftp->copy_file . " souborů o velikosti " . $ftp->copy_file_byte . " KB.");
  119. } else {
  120. $ftp->write_log("Nepodařilo se přihlásit na FTP!");
  121. die();
  122. }
  123. /*
  124. * Definice tříd
  125. */
  126.  
  127. class ftp {
  128.  
  129. private $conn_id;
  130. private $host;
  131. private $username;
  132. private $password;
  133. private $port;
  134. public $directory_todo;
  135. public $ignorelist;
  136. public $source_ignorelist;
  137. public $hashtable;
  138. public $new_hashtable;
  139. public $source_hashtable;
  140. public $source_log;
  141. public $timeout = 90;
  142. public $passive = false;
  143. public $ssl = false;
  144. public $system_type = '';
  145. public $remote_folder;
  146. public $deletelist;
  147. //pocitadla
  148. public $delete_folder = 0;
  149. public $delete_file = 0;
  150. public $make_folder = 0;
  151. public $copy_file = 0;
  152. public $copy_file_byte = 0;
  153. private $fstream_log;
  154.  
  155. public function __construct($host, $username, $password, $source_ignore_list, $source_log, $remote_folder, $source_hash_table, $port = 21) {
  156. $this->host = $host;
  157. $this->username = $username;
  158. $this->password = $password;
  159. $this->port = $port;
  160. $this->directory_todo[0] = __DIR__;
  161. $this->ignorelist = $this->load_ignore_list($source_ignore_list);
  162. $this->source_ignorelist = $source_ignore_list;
  163. $this->source_log = $source_log;
  164. $this->source_hashtable = $source_hash_table;
  165. $this->hashtable = $this->load_hash_table($source_hash_table);
  166.  
  167. $this->fstream_log = fopen($source_log, "a");
  168.  
  169. $this->remote_folder = $remote_folder;
  170. }
  171.  
  172. public function connect() {
  173. if ($this->ssl == false) {
  174. $this->conn_id = ftp_connect($this->host, $this->port);
  175. } else {
  176. if (function_exists('ftp_ssl_connect')) {
  177. $this->conn_id = ftp_ssl_connect($this->host, $this->port);
  178. } else {
  179. return false;
  180. }
  181. }
  182.  
  183. $result = ftp_login($this->conn_id, $this->username, $this->password);
  184.  
  185. if ($result == true) {
  186. ftp_set_option($this->conn_id, FTP_TIMEOUT_SEC, $this->timeout);
  187.  
  188. if ($this->passive == true) {
  189. ftp_pasv($this->conn_id, true);
  190. } else {
  191. ftp_pasv($this->conn_id, false);
  192. }
  193.  
  194. $this->system_type = ftp_systype($this->conn_id);
  195.  
  196. return true;
  197. } else {
  198. return false;
  199. }
  200. }
  201.  
  202. public function put($local_file_path, $remote_file_path, $mode = FTP_ASCII) {
  203. if (ftp_put($this->conn_id, $remote_file_path, $local_file_path, $mode)) {
  204. return true;
  205. } else {
  206. return false;
  207. }
  208. }
  209.  
  210. public function get($local_file_path, $remote_file_path, $mode = FTP_ASCII) {
  211. if (ftp_get($this->conn_id, $local_file_path, $remote_file_path, $mode)) {
  212. return true;
  213. } else {
  214. return false;
  215. }
  216. }
  217.  
  218. public function chmod($permissions, $remote_filename) {
  219. if ($this->is_octal($permissions)) {
  220. $result = ftp_chmod($this->conn_id, $permissions, $remote_filename);
  221. if ($result) {
  222. return true;
  223. } else {
  224. return false;
  225. }
  226. } else {
  227. throw new Exception('$permissions must be an octal number');
  228. }
  229. }
  230.  
  231. public function chdir($directory) {
  232. ftp_chdir($this->conn_id, $directory);
  233. }
  234.  
  235. public function delete($remote_file_path) {
  236. if (ftp_delete($this->conn_id, $remote_file_path)) {
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }
  242.  
  243. public function make_dir($directory) {
  244. if (ftp_mkdir($this->conn_id, $directory)) {
  245. return true;
  246. } else {
  247. return false;
  248. }
  249. }
  250.  
  251. public function rename($old_name, $new_name) {
  252. if (ftp_rename($this->conn_id, $old_name, $new_name)) {
  253. return true;
  254. } else {
  255. return false;
  256. }
  257. }
  258.  
  259. public function remove_dir($directory) {
  260. if (ftp_rmdir($this->conn_id, $directory)) {
  261. return true;
  262. } else {
  263. return false;
  264. }
  265. }
  266.  
  267. public function dir_list($directory) {
  268. $contents = ftp_nlist($this->conn_id, $directory);
  269. return $contents;
  270. }
  271.  
  272. public function cdup() {
  273. ftp_cdup($this->conn_id);
  274. }
  275.  
  276. public function current_dir() {
  277. return ftp_pwd($this->conn_id);
  278. }
  279.  
  280. private function is_octal($i) {
  281. return decoct(octdec($i)) == $i;
  282. }
  283.  
  284. public function __destruct() {
  285. if ($this->conn_id)
  286. ftp_close($this->conn_id);
  287. fclose($this->fstream_log);
  288. }
  289.  
  290. public function compare($value) {
  291. $local = md5(fread(fopen($value, r), filesize($value)));
  292. if ($this->hashtable[$value] == $local) {
  293. $this->new_hashtable[$value] = $local;
  294. return true;
  295. } else {
  296. $this->new_hashtable[$value] = $local;
  297. return false;
  298. }
  299. }
  300.  
  301. public function dir_list_local($directory) {
  302. if ($handle = opendir($directory)) {
  303. while ($file = readdir($handle)) {
  304. $polozky[count($polozky)] = $file;
  305. }
  306. closedir($handle);
  307. sort($polozky);
  308. return $polozky;
  309. }
  310. }
  311.  
  312. public function is_relevant_dir_local($directory) {
  313. if (is_dir($directory) && $directory != "." && $directory != "..")
  314. return true;
  315. else
  316. return false;
  317. }
  318.  
  319. public function is_relevant_file_local($directory) {
  320. if (!is_dir($directory) && ($directory != "updater.php" && $directory != "updater_tmp" && $directory != $this->source_ignorelist && $directory != $this->source_log && $directory != $this->source_hashtable))
  321. return true;
  322. else
  323. return false;
  324. }
  325.  
  326. public function load_ignore_list($ignorelist) {
  327. if (File_Exists($ignorelist)) {
  328. $data = file($ignorelist);
  329. return $data;
  330. }
  331. }
  332.  
  333. public function load_hash_table($ignorelist) {
  334. if (File_Exists($ignorelist)) {
  335. $data = file($ignorelist);
  336.  
  337. foreach ($data as $key => $value) {
  338. $split = explode("**", $value);
  339. $parcsplit = chop($split[0]);
  340. $return[$parcsplit] = chop($split[1]);
  341. }
  342. return $return;
  343. }
  344. }
  345.  
  346. public function save_hashtable() {
  347. $fstream = fopen($this->source_hashtable, "w");
  348. foreach ($this->new_hashtable as $key => $value) {
  349. fwrite($fstream, $key . "**" . $value . "\n");
  350. }
  351. fclose($fstream);
  352. }
  353.  
  354. public function is_ignored($file) {
  355. foreach ($this->ignorelist as $key => $value) {
  356. if ($file == chop($value))
  357. return true;
  358. }
  359. return false;
  360. }
  361.  
  362. public function init_delete_list($list) {
  363. foreach ($list as $key => $value) {
  364. if (!array_key_exists($value, $this->deletelist))
  365. $this->deletelist[$value] = true;
  366. }
  367. $this->deletelist[$this->current_dir() . "/."] = false;
  368. $this->deletelist[$this->current_dir() . "/.."] = false;
  369. }
  370.  
  371. public function delete_remote_files() {
  372. echo "***MAZANI***<br />";
  373. foreach ($this->deletelist as $key => $value) {
  374.  
  375. if ($value == true) {
  376. echo "MAZU: \"" . $key . "\"";
  377. if($this->recursive_remove_directory($key) || $this->delete($key)) echo "-> true";
  378. else echo "-> false";
  379. }
  380. else
  381. echo "NEMAZU: \"" . $key . "\"";
  382. echo "<br />";
  383. }
  384. }
  385.  
  386. public function recursive_remove_directory($directory, $empty=FALSE) {
  387. if (substr($directory, -1) == '/')
  388. $directory = substr($directory, 0, -1);
  389.  
  390. $handle = $this->dir_list($directory);
  391. foreach ($handle as $key => $value) {
  392. if (substr($value, -1) == '.')
  393. $value = substr($value, 0, -2);
  394. if (substr($value, -2) == '..')
  395. $value = substr($value, 0, -3);
  396.  
  397. $path = $value;
  398. //echo "*" . $path . "*!" . $path . "! ";
  399. $this->delete($path);
  400. $this->recursive_remove_directory($path);
  401.  
  402. }
  403.  
  404. if ($empty == FALSE) {
  405. if ($this->remove_dir($directory))
  406. return FALSE;
  407. }
  408. return TRUE;
  409. }
  410.  
  411. public function write_log($string) {
  412. fwrite($this->fstream_log, date("H:i:s j.n.Y") . ": " . $string . "\n");
  413. }
  414.  
  415. }
  416. ?>
Add Comment
Please, Sign In to add comment