PageRenderTime 98ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/class-wp-filesystem-ftpsockets.php

https://github.com/mhoofman/wordpress-heroku
PHP | 456 lines | 252 code | 72 blank | 132 comment | 57 complexity | 242cf53d30c5475db6d02a6429724143 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress FTP Sockets Filesystem.
  4. *
  5. * @package WordPress
  6. * @subpackage Filesystem
  7. */
  8. /**
  9. * WordPress Filesystem Class for implementing FTP Sockets.
  10. *
  11. * @since 2.5.0
  12. * @package WordPress
  13. * @subpackage Filesystem
  14. * @uses WP_Filesystem_Base Extends class
  15. */
  16. class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
  17. public $ftp = false;
  18. public $errors = null;
  19. public $options = array();
  20. public function __construct($opt = '') {
  21. $this->method = 'ftpsockets';
  22. $this->errors = new WP_Error();
  23. // Check if possible to use ftp functions.
  24. if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
  25. return false;
  26. }
  27. $this->ftp = new ftp();
  28. if ( empty($opt['port']) )
  29. $this->options['port'] = 21;
  30. else
  31. $this->options['port'] = $opt['port'];
  32. if ( empty($opt['hostname']) )
  33. $this->errors->add('empty_hostname', __('FTP hostname is required'));
  34. else
  35. $this->options['hostname'] = $opt['hostname'];
  36. if ( ! empty($opt['base']) )
  37. $this->wp_base = $opt['base'];
  38. // Check if the options provided are OK.
  39. if ( empty ($opt['username']) )
  40. $this->errors->add('empty_username', __('FTP username is required'));
  41. else
  42. $this->options['username'] = $opt['username'];
  43. if ( empty ($opt['password']) )
  44. $this->errors->add('empty_password', __('FTP password is required'));
  45. else
  46. $this->options['password'] = $opt['password'];
  47. }
  48. public function connect() {
  49. if ( ! $this->ftp )
  50. return false;
  51. $this->ftp->setTimeout(FS_CONNECT_TIMEOUT);
  52. if ( ! $this->ftp->SetServer($this->options['hostname'], $this->options['port']) ) {
  53. $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  54. return false;
  55. }
  56. if ( ! $this->ftp->connect() ) {
  57. $this->errors->add('connect', sprintf(__('Failed to connect to FTP Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  58. return false;
  59. }
  60. if ( ! $this->ftp->login($this->options['username'], $this->options['password']) ) {
  61. $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
  62. return false;
  63. }
  64. $this->ftp->SetType( FTP_BINARY );
  65. $this->ftp->Passive( true );
  66. $this->ftp->setTimeout( FS_TIMEOUT );
  67. return true;
  68. }
  69. /**
  70. * @param string $file
  71. * @return bool|string
  72. */
  73. public function get_contents( $file ) {
  74. if ( ! $this->exists($file) )
  75. return false;
  76. $temp = wp_tempnam( $file );
  77. if ( ! $temphandle = fopen($temp, 'w+') )
  78. return false;
  79. mbstring_binary_safe_encoding();
  80. if ( ! $this->ftp->fget($temphandle, $file) ) {
  81. fclose($temphandle);
  82. unlink($temp);
  83. reset_mbstring_encoding();
  84. return ''; // Blank document, File does exist, It's just blank.
  85. }
  86. reset_mbstring_encoding();
  87. fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
  88. $contents = '';
  89. while ( ! feof($temphandle) )
  90. $contents .= fread($temphandle, 8192);
  91. fclose($temphandle);
  92. unlink($temp);
  93. return $contents;
  94. }
  95. /**
  96. * @param string $file
  97. * @return array
  98. */
  99. public function get_contents_array($file) {
  100. return explode("\n", $this->get_contents($file) );
  101. }
  102. /**
  103. * @param string $file
  104. * @param string $contents
  105. * @param int|bool $mode
  106. * @return bool
  107. */
  108. public function put_contents($file, $contents, $mode = false ) {
  109. $temp = wp_tempnam( $file );
  110. if ( ! $temphandle = @fopen($temp, 'w+') ) {
  111. unlink($temp);
  112. return false;
  113. }
  114. // The FTP class uses string functions internally during file download/upload
  115. mbstring_binary_safe_encoding();
  116. $bytes_written = fwrite( $temphandle, $contents );
  117. if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
  118. fclose( $temphandle );
  119. unlink( $temp );
  120. reset_mbstring_encoding();
  121. return false;
  122. }
  123. fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
  124. $ret = $this->ftp->fput($file, $temphandle);
  125. reset_mbstring_encoding();
  126. fclose($temphandle);
  127. unlink($temp);
  128. $this->chmod($file, $mode);
  129. return $ret;
  130. }
  131. public function cwd() {
  132. $cwd = $this->ftp->pwd();
  133. if ( $cwd )
  134. $cwd = trailingslashit($cwd);
  135. return $cwd;
  136. }
  137. public function chdir($file) {
  138. return $this->ftp->chdir($file);
  139. }
  140. /**
  141. * @param string $file
  142. * @param bool $group
  143. * @param bool $recursive
  144. */
  145. public function chgrp($file, $group, $recursive = false ) {
  146. return false;
  147. }
  148. /**
  149. * @param string $file
  150. * @param int|bool $mode
  151. * @param bool $recursive
  152. * @return bool
  153. */
  154. public function chmod($file, $mode = false, $recursive = false ) {
  155. if ( ! $mode ) {
  156. if ( $this->is_file($file) )
  157. $mode = FS_CHMOD_FILE;
  158. elseif ( $this->is_dir($file) )
  159. $mode = FS_CHMOD_DIR;
  160. else
  161. return false;
  162. }
  163. // chmod any sub-objects if recursive.
  164. if ( $recursive && $this->is_dir($file) ) {
  165. $filelist = $this->dirlist($file);
  166. foreach ( (array)$filelist as $filename => $filemeta )
  167. $this->chmod($file . '/' . $filename, $mode, $recursive);
  168. }
  169. // chmod the file or directory
  170. return $this->ftp->chmod($file, $mode);
  171. }
  172. /**
  173. * @param string $file
  174. * @return string
  175. */
  176. public function owner($file) {
  177. $dir = $this->dirlist($file);
  178. return $dir[$file]['owner'];
  179. }
  180. /**
  181. * @param string $file
  182. * @return string
  183. */
  184. public function getchmod($file) {
  185. $dir = $this->dirlist($file);
  186. return $dir[$file]['permsn'];
  187. }
  188. /**
  189. * @param string $file
  190. * @return string
  191. */
  192. public function group($file) {
  193. $dir = $this->dirlist($file);
  194. return $dir[$file]['group'];
  195. }
  196. /**
  197. * @param string $source
  198. * @param string $destination
  199. * @param bool $overwrite
  200. * @param int|bool $mode
  201. * @return bool
  202. */
  203. public function copy($source, $destination, $overwrite = false, $mode = false) {
  204. if ( ! $overwrite && $this->exists($destination) )
  205. return false;
  206. $content = $this->get_contents($source);
  207. if ( false === $content )
  208. return false;
  209. return $this->put_contents($destination, $content, $mode);
  210. }
  211. /**
  212. * @param string $source
  213. * @param string $destination
  214. * @param bool $overwrite
  215. * @return bool
  216. */
  217. public function move($source, $destination, $overwrite = false ) {
  218. return $this->ftp->rename($source, $destination);
  219. }
  220. /**
  221. * @param string $file
  222. * @param bool $recursive
  223. * @param string $type
  224. * @return bool
  225. */
  226. public function delete($file, $recursive = false, $type = false) {
  227. if ( empty($file) )
  228. return false;
  229. if ( 'f' == $type || $this->is_file($file) )
  230. return $this->ftp->delete($file);
  231. if ( !$recursive )
  232. return $this->ftp->rmdir($file);
  233. return $this->ftp->mdel($file);
  234. }
  235. /**
  236. * @param string $file
  237. * @return bool
  238. */
  239. public function exists( $file ) {
  240. $list = $this->ftp->nlist( $file );
  241. return !empty( $list ); //empty list = no file, so invert.
  242. // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
  243. }
  244. /**
  245. * @param string $file
  246. * @return bool
  247. */
  248. public function is_file($file) {
  249. if ( $this->is_dir($file) )
  250. return false;
  251. if ( $this->exists($file) )
  252. return true;
  253. return false;
  254. }
  255. /**
  256. * @param string $path
  257. * @return bool
  258. */
  259. public function is_dir($path) {
  260. $cwd = $this->cwd();
  261. if ( $this->chdir($path) ) {
  262. $this->chdir($cwd);
  263. return true;
  264. }
  265. return false;
  266. }
  267. /**
  268. * @param string $file
  269. * @return bool
  270. */
  271. public function is_readable($file) {
  272. return true;
  273. }
  274. /**
  275. * @param string $file
  276. * @return bool
  277. */
  278. public function is_writable($file) {
  279. return true;
  280. }
  281. /**
  282. * @param string $file
  283. * @return bool
  284. */
  285. public function atime($file) {
  286. return false;
  287. }
  288. /**
  289. * @param string $file
  290. * @return int
  291. */
  292. public function mtime($file) {
  293. return $this->ftp->mdtm($file);
  294. }
  295. /**
  296. * @param string $file
  297. * @return int
  298. */
  299. public function size($file) {
  300. return $this->ftp->filesize($file);
  301. }
  302. /**
  303. * @param string $file
  304. * @param int $time
  305. * @param int $atime
  306. * @return bool
  307. */
  308. public function touch($file, $time = 0, $atime = 0 ) {
  309. return false;
  310. }
  311. /**
  312. * @param string $path
  313. * @param mixed $chmod
  314. * @param mixed $chown
  315. * @param mixed $chgrp
  316. * @return bool
  317. */
  318. public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) {
  319. $path = untrailingslashit($path);
  320. if ( empty($path) )
  321. return false;
  322. if ( ! $this->ftp->mkdir($path) )
  323. return false;
  324. if ( ! $chmod )
  325. $chmod = FS_CHMOD_DIR;
  326. $this->chmod($path, $chmod);
  327. if ( $chown )
  328. $this->chown($path, $chown);
  329. if ( $chgrp )
  330. $this->chgrp($path, $chgrp);
  331. return true;
  332. }
  333. /**
  334. * @param sting $path
  335. * @param bool $recursive
  336. */
  337. public function rmdir($path, $recursive = false ) {
  338. $this->delete($path, $recursive);
  339. }
  340. /**
  341. * @param string $path
  342. * @param bool $include_hidden
  343. * @param bool $recursive
  344. * @return bool|array
  345. */
  346. public function dirlist($path = '.', $include_hidden = true, $recursive = false ) {
  347. if ( $this->is_file($path) ) {
  348. $limit_file = basename($path);
  349. $path = dirname($path) . '/';
  350. } else {
  351. $limit_file = false;
  352. }
  353. mbstring_binary_safe_encoding();
  354. $list = $this->ftp->dirlist($path);
  355. if ( empty( $list ) && ! $this->exists( $path ) ) {
  356. reset_mbstring_encoding();
  357. return false;
  358. }
  359. $ret = array();
  360. foreach ( $list as $struc ) {
  361. if ( '.' == $struc['name'] || '..' == $struc['name'] )
  362. continue;
  363. if ( ! $include_hidden && '.' == $struc['name'][0] )
  364. continue;
  365. if ( $limit_file && $struc['name'] != $limit_file )
  366. continue;
  367. if ( 'd' == $struc['type'] ) {
  368. if ( $recursive )
  369. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  370. else
  371. $struc['files'] = array();
  372. }
  373. // Replace symlinks formatted as "source -> target" with just the source name
  374. if ( $struc['islink'] )
  375. $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
  376. $ret[ $struc['name'] ] = $struc;
  377. }
  378. reset_mbstring_encoding();
  379. return $ret;
  380. }
  381. public function __destruct() {
  382. $this->ftp->quit();
  383. }
  384. }