WP_Filesystem_ftpsockets

WordPress Filesystem Class for implementing FTP Sockets.

Defined (1)

The class is defined in the following location(s).

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