PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/W3/Cdn/Ftp.php

https://gitlab.com/endomorphosis/jeffersonsmithmayor
PHP | 373 lines | 215 code | 95 blank | 63 comment | 35 complexity | 96a3bb56cc03efdf6225d4441aa6a3e0 MD5 | raw file
  1. <?php
  2. /**
  3. * W3 CDN FTP Class
  4. */
  5. if (!defined('W3TC')) {
  6. die();
  7. }
  8. define('W3TC_CDN_FTP_CONNECT_TIMEOUT', 30);
  9. require_once W3TC_LIB_W3_DIR . '/Cdn/Base.php';
  10. /**
  11. * Class W3_Cdn_Ftp
  12. */
  13. class W3_Cdn_Ftp extends W3_Cdn_Base {
  14. /**
  15. * FTP resource
  16. *
  17. * @var resource
  18. */
  19. var $_ftp = null;
  20. /**
  21. * PHP5 Constructor
  22. *
  23. * @param array $config
  24. */
  25. function __construct($config = array()) {
  26. $config = array_merge(array(
  27. 'host' => '',
  28. 'port' => 21,
  29. 'user' => '',
  30. 'pass' => '',
  31. 'path' => '',
  32. 'pasv' => false,
  33. 'domain' => array(),
  34. ), $config);
  35. parent::__construct($config);
  36. }
  37. /**
  38. * PHP4 Constructor
  39. *
  40. * @param array $config
  41. */
  42. function W3_Cdn_Ftp($config = array()) {
  43. $this->__construct($config);
  44. }
  45. /**
  46. * Connects to FTP server
  47. *
  48. * @param string $error
  49. * @return boolean
  50. */
  51. function _connect(&$error) {
  52. if (empty($this->_config['host'])) {
  53. $error = 'Empty host.';
  54. return false;
  55. }
  56. if (empty($this->_config['port'])) {
  57. $this->_config['port'] = 21;
  58. }
  59. $this->_set_error_handler();
  60. $this->_ftp = @ftp_connect($this->_config['host'], (int) $this->_config['port'], W3TC_CDN_FTP_CONNECT_TIMEOUT);
  61. if (!$this->_ftp) {
  62. $error = sprintf('Unable to connect to %s:%d (%s).', $this->_config['host'], $this->_config['port'], $this->_get_last_error());
  63. $this->_restore_error_handler();
  64. return false;
  65. }
  66. if (!@ftp_login($this->_ftp, $this->_config['user'], $this->_config['pass'])) {
  67. $error = sprintf('Incorrect login or password (%s).', $this->_get_last_error());
  68. $this->_restore_error_handler();
  69. $this->_disconnect();
  70. return false;
  71. }
  72. if (!@ftp_pasv($this->_ftp, $this->_config['pasv'])) {
  73. $error = sprintf('Unable to change mode to passive (%s).', $this->_get_last_error());
  74. $this->_restore_error_handler();
  75. $this->_disconnect();
  76. return false;
  77. }
  78. if (!empty($this->_config['path']) && !@ftp_chdir($this->_ftp, $this->_config['path'])) {
  79. $error = sprintf('Unable to change directory to: %s (%s).', $this->_config['path'], $this->_get_last_error());
  80. $this->_restore_error_handler();
  81. $this->_disconnect();
  82. return false;
  83. }
  84. $this->_restore_error_handler();
  85. return true;
  86. }
  87. /**
  88. * Disconnects from FTP server
  89. */
  90. function _disconnect() {
  91. @ftp_close($this->_ftp);
  92. }
  93. /**
  94. * Sends MDTM command
  95. *
  96. * @param string $remote_file
  97. * @param integer $mtime
  98. * @return boolean
  99. */
  100. function _mdtm($remote_file, $mtime) {
  101. $command = sprintf('MDTM %s %s', date('YmdHis', $mtime), $remote_file);
  102. return @ftp_raw($this->_ftp, $command);
  103. }
  104. /**
  105. * Uploads files to FTP
  106. *
  107. * @param array $files
  108. * @param array $results
  109. * @param boolean $force_rewrite
  110. * @return boolean
  111. */
  112. function upload($files, &$results, $force_rewrite = false) {
  113. $error = null;
  114. if (!$this->_connect($error)) {
  115. $results = $this->_get_results($files, W3TC_CDN_RESULT_HALT, $error);
  116. return false;
  117. }
  118. $this->_set_error_handler();
  119. $home = @ftp_pwd($this->_ftp);
  120. if ($home === false) {
  121. $results = $this->_get_results($files, W3TC_CDN_RESULT_HALT, sprintf('Unable to get current directory (%s).', $this->_get_last_error()));
  122. $this->_restore_error_handler();
  123. $this->_disconnect();
  124. return false;
  125. }
  126. foreach ($files as $local_path => $remote_path) {
  127. if (!file_exists($local_path)) {
  128. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'Source file not found.');
  129. continue;
  130. }
  131. @ftp_chdir($this->_ftp, $home);
  132. $remote_dir = dirname($remote_path);
  133. $remote_dirs = preg_split('~\\/+~', $remote_dir);
  134. foreach ($remote_dirs as $dir) {
  135. if (!@ftp_chdir($this->_ftp, $dir)) {
  136. if (!@ftp_mkdir($this->_ftp, $dir)) {
  137. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to create directory (%s).', $this->_get_last_error()));
  138. continue 2;
  139. }
  140. if (!@ftp_chdir($this->_ftp, $dir)) {
  141. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to change directory (%s).', $this->_get_last_error()));
  142. continue 2;
  143. }
  144. }
  145. }
  146. $remote_file = basename($remote_path);
  147. $mtime = @filemtime($local_path);
  148. if (!$force_rewrite) {
  149. $size = @filesize($local_path);
  150. $ftp_size = @ftp_size($this->_ftp, $remote_file);
  151. $ftp_mtime = @ftp_mdtm($this->_ftp, $remote_file);
  152. if ($size === $ftp_size && $mtime === $ftp_mtime) {
  153. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'File up-to-date.');
  154. continue;
  155. }
  156. }
  157. $result = @ftp_put($this->_ftp, $remote_file, $local_path, FTP_BINARY);
  158. if ($result) {
  159. $this->_mdtm($remote_file, $mtime);
  160. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'OK');
  161. } else {
  162. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to upload file (%s).', $this->_get_last_error()));
  163. }
  164. }
  165. $this->_restore_error_handler();
  166. $this->_disconnect();
  167. return !$this->_is_error($results);
  168. }
  169. /**
  170. * Deletes files from FTP
  171. *
  172. * @param array $files
  173. * @param array $results
  174. * @return boolean
  175. */
  176. function delete($files, &$results) {
  177. $error = null;
  178. if (!$this->_connect($error)) {
  179. $results = $this->_get_results($files, W3TC_CDN_RESULT_HALT, $error);
  180. return false;
  181. }
  182. $this->_set_error_handler();
  183. foreach ($files as $local_path => $remote_path) {
  184. $result = @ftp_delete($this->_ftp, $remote_path);
  185. if ($result) {
  186. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'OK');
  187. } else {
  188. $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to delete file (%s).', $this->_get_last_error()));
  189. }
  190. while (true) {
  191. $remote_path = dirname($remote_path);
  192. if ($remote_path == '.' || !@ftp_rmdir($this->_ftp, $remote_path)) {
  193. break;
  194. }
  195. }
  196. }
  197. $this->_restore_error_handler();
  198. $this->_disconnect();
  199. return !$this->_is_error($results);
  200. }
  201. /**
  202. * Tests FTP server
  203. *
  204. * @param string $error
  205. * @return boolean
  206. */
  207. function test(&$error) {
  208. if (!parent::test($error)) {
  209. return false;
  210. }
  211. $rand = md5(time());
  212. $tmp_dir = 'test_dir_' . $rand;
  213. $tmp_file = 'test_file_' . $rand;
  214. $tmp_path = W3TC_TMP_DIR . '/' . $tmp_file;
  215. if (!@file_put_contents($tmp_path, $rand)) {
  216. $error = sprintf('Unable to create file: %s.', $tmp_path);
  217. return false;
  218. }
  219. if (!$this->_connect($error)) {
  220. return false;
  221. }
  222. $this->_set_error_handler();
  223. if (!@ftp_mkdir($this->_ftp, $tmp_dir)) {
  224. $error = sprintf('Unable to make directory: %s (%s).', $tmp_dir, $this->_get_last_error());
  225. @unlink($tmp_path);
  226. $this->_restore_error_handler();
  227. $this->_disconnect();
  228. return false;
  229. }
  230. if (!@ftp_chdir($this->_ftp, $tmp_dir)) {
  231. $error = sprintf('Unable to change directory to: %s (%s).', $tmp_dir, $this->_get_last_error());
  232. @unlink($tmp_path);
  233. @ftp_rmdir($this->_ftp, $tmp_dir);
  234. $this->_restore_error_handler();
  235. $this->_disconnect();
  236. return false;
  237. }
  238. if (!@ftp_put($this->_ftp, $tmp_file, $tmp_path, FTP_BINARY)) {
  239. $error = sprintf('Unable to upload file: %s (%s).', $tmp_path, $this->_get_last_error());
  240. @unlink($tmp_path);
  241. @ftp_cdup($this->_ftp);
  242. @ftp_rmdir($this->_ftp, $tmp_dir);
  243. $this->_restore_error_handler();
  244. $this->_disconnect();
  245. return false;
  246. }
  247. @unlink($tmp_path);
  248. if (!@ftp_delete($this->_ftp, $tmp_file)) {
  249. $error = sprintf('Unable to delete file: %s (%s).', $tmp_path, $this->_get_last_error());
  250. @ftp_cdup($this->_ftp);
  251. @ftp_rmdir($this->_ftp, $tmp_dir);
  252. $this->_restore_error_handler();
  253. $this->_disconnect();
  254. return false;
  255. }
  256. @ftp_cdup($this->_ftp);
  257. if (!@ftp_rmdir($this->_ftp, $tmp_dir)) {
  258. $error = sprintf('Unable to remove directory: %s (%s).', $tmp_dir, $this->_get_last_error());
  259. $this->_restore_error_handler();
  260. $this->_disconnect();
  261. return false;
  262. }
  263. $this->_restore_error_handler();
  264. $this->_disconnect();
  265. return true;
  266. }
  267. /**
  268. * Returns array of CDN domains
  269. *
  270. * @return array
  271. */
  272. function get_domains() {
  273. if (!empty($this->_config['domain'])) {
  274. return (array) $this->_config['domain'];
  275. }
  276. return array();
  277. }
  278. }