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

  1. <?php 
  2. /** 
  3. * WordPress Filesystem Class for implementing SSH2 
  4. * 
  5. * To use this class you must follow these steps for PHP 5.2.6+ 
  6. * 
  7. * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes 
  8. * 
  9. * Complie libssh2 (Note: Only 0.14 is officaly working with PHP 5.2.6+ right now, But many users have found the latest versions work) 
  10. * 
  11. * cd /usr/src 
  12. * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz 
  13. * tar -zxvf libssh2-0.14.tar.gz 
  14. * cd libssh2-0.14/ 
  15. * ./configure 
  16. * make all install 
  17. * 
  18. * Note: Do not leave the directory yet! 
  19. * 
  20. * Enter: pecl install -f ssh2 
  21. * 
  22. * Copy the ssh.so file it creates to your PHP Module Directory. 
  23. * Open up your PHP.INI file and look for where extensions are placed. 
  24. * Add in your PHP.ini file: extension=ssh2.so 
  25. * 
  26. * Restart Apache! 
  27. * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp exist. 
  28. * 
  29. * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents' 
  30. * 
  31. * @since 2.7.0 
  32. * 
  33. * @package WordPress 
  34. * @subpackage Filesystem 
  35. */ 
  36. class WP_Filesystem_SSH2 extends WP_Filesystem_Base { 
  37.  
  38. /** 
  39. * @access public 
  40. */ 
  41. public $link = false; 
  42.  
  43. /** 
  44. * @access public 
  45. * @var resource 
  46. */ 
  47. public $sftp_link; 
  48. public $keys = false; 
  49.  
  50. /** 
  51. * @access public 
  52. * 
  53. * @param array $opt 
  54. */ 
  55. public function __construct( $opt = '' ) { 
  56. $this->method = 'ssh2'; 
  57. $this->errors = new WP_Error(); 
  58.  
  59. //Check if possible to use ssh2 functions. 
  60. if ( ! extension_loaded('ssh2') ) { 
  61. $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available')); 
  62. return; 
  63. if ( !function_exists('stream_get_contents') ) { 
  64. $this->errors->add( 
  65. 'ssh2_php_requirement',  
  66. sprintf( 
  67. /** translators: %s: stream_get_contents() */ 
  68. __( 'The ssh2 PHP extension is available, however, we require the PHP5 function %s' ),  
  69. '<code>stream_get_contents()</code>' 
  70. ); 
  71. return; 
  72.  
  73. // Set defaults: 
  74. if ( empty($opt['port']) ) 
  75. $this->options['port'] = 22; 
  76. else 
  77. $this->options['port'] = $opt['port']; 
  78.  
  79. if ( empty($opt['hostname']) ) 
  80. $this->errors->add('empty_hostname', __('SSH2 hostname is required')); 
  81. else 
  82. $this->options['hostname'] = $opt['hostname']; 
  83.  
  84. // Check if the options provided are OK. 
  85. if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) { 
  86. $this->options['public_key'] = $opt['public_key']; 
  87. $this->options['private_key'] = $opt['private_key']; 
  88.  
  89. $this->options['hostkey'] = array('hostkey' => 'ssh-rsa'); 
  90.  
  91. $this->keys = true; 
  92. } elseif ( empty ($opt['username']) ) { 
  93. $this->errors->add('empty_username', __('SSH2 username is required')); 
  94.  
  95. if ( !empty($opt['username']) ) 
  96. $this->options['username'] = $opt['username']; 
  97.  
  98. if ( empty ($opt['password']) ) { 
  99. // Password can be blank if we are using keys. 
  100. if ( !$this->keys ) 
  101. $this->errors->add('empty_password', __('SSH2 password is required')); 
  102. } else { 
  103. $this->options['password'] = $opt['password']; 
  104.  
  105. /** 
  106. * @access public 
  107. * 
  108. * @return bool 
  109. */ 
  110. public function connect() { 
  111. if ( ! $this->keys ) { 
  112. $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']); 
  113. } else { 
  114. $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']); 
  115.  
  116. if ( ! $this->link ) { 
  117. $this->errors->add( 'connect',  
  118. /** translators: %s: hostname:port */ 
  119. sprintf( __( 'Failed to connect to SSH2 Server %s' ),  
  120. $this->options['hostname'] . ':' . $this->options['port'] 
  121. ); 
  122. return false; 
  123.  
  124. if ( !$this->keys ) { 
  125. if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) { 
  126. $this->errors->add( 'auth',  
  127. /** translators: %s: username */ 
  128. sprintf( __( 'Username/Password incorrect for %s' ),  
  129. $this->options['username'] 
  130. ); 
  131. return false; 
  132. } else { 
  133. if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) { 
  134. $this->errors->add( 'auth',  
  135. /** translators: %s: username */ 
  136. sprintf( __( 'Public and Private keys incorrect for %s' ),  
  137. $this->options['username'] 
  138. ); 
  139. return false; 
  140.  
  141. $this->sftp_link = ssh2_sftp( $this->link ); 
  142. if ( ! $this->sftp_link ) { 
  143. $this->errors->add( 'connect',  
  144. /** translators: %s: hostname:port */ 
  145. sprintf( __( 'Failed to initialize a SFTP subsystem session with the SSH2 Server %s' ),  
  146. $this->options['hostname'] . ':' . $this->options['port'] 
  147. ); 
  148. return false; 
  149.  
  150. return true; 
  151.  
  152. /** 
  153. * Gets the ssh2.sftp PHP stream wrapper path to open for the given file. 
  154. * 
  155. * This method also works around a PHP bug where the root directory (/) cannot 
  156. * be opened by PHP functions, causing a false failure. In order to work around 
  157. * this, the path is converted to /./ which is semantically the same as / 
  158. * See https://bugs.php.net/bug.php?id=64169 for more details. 
  159. * 
  160. * @access public 
  161. * 
  162. * @since 4.4.0 
  163. * 
  164. * @param string $path The File/Directory path on the remote server to return 
  165. * @return string The ssh2.sftp:// wrapped path to use. 
  166. */ 
  167. public function sftp_path( $path ) { 
  168. if ( '/' === $path ) { 
  169. $path = '/./'; 
  170. return 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $path, '/' ); 
  171.  
  172. /** 
  173. * @access public 
  174. *  
  175. * @param string $command 
  176. * @param bool $returnbool 
  177. * @return bool|string True on success, false on failure. String if the command was executed, `$returnbool` 
  178. * is false (default), and data from the resulting stream was retrieved. 
  179. */ 
  180. public function run_command( $command, $returnbool = false ) { 
  181. if ( ! $this->link ) 
  182. return false; 
  183.  
  184. if ( ! ($stream = ssh2_exec($this->link, $command)) ) { 
  185. $this->errors->add( 'command',  
  186. /** translators: %s: command */ 
  187. sprintf( __( 'Unable to perform command: %s'),  
  188. $command 
  189. ); 
  190. } else { 
  191. stream_set_blocking( $stream, true ); 
  192. stream_set_timeout( $stream, FS_TIMEOUT ); 
  193. $data = stream_get_contents( $stream ); 
  194. fclose( $stream ); 
  195.  
  196. if ( $returnbool ) 
  197. return ( $data === false ) ? false : '' != trim($data); 
  198. else 
  199. return $data; 
  200. return false; 
  201.  
  202. /** 
  203. * @access public 
  204. * 
  205. * @param string $file 
  206. * @return string|false 
  207. */ 
  208. public function get_contents( $file ) { 
  209. return file_get_contents( $this->sftp_path( $file ) ); 
  210.  
  211. /** 
  212. * @access public 
  213. * 
  214. * @param string $file 
  215. * @return array 
  216. */ 
  217. public function get_contents_array($file) { 
  218. return file( $this->sftp_path( $file ) ); 
  219.  
  220. /** 
  221. * @access public 
  222. * 
  223. * @param string $file 
  224. * @param string $contents 
  225. * @param bool|int $mode 
  226. * @return bool 
  227. */ 
  228. public function put_contents($file, $contents, $mode = false ) { 
  229. $ret = file_put_contents( $this->sftp_path( $file ), $contents ); 
  230.  
  231. if ( $ret !== strlen( $contents ) ) 
  232. return false; 
  233.  
  234. $this->chmod($file, $mode); 
  235.  
  236. return true; 
  237.  
  238. /** 
  239. * @access public 
  240. * 
  241. * @return bool 
  242. */ 
  243. public function cwd() { 
  244. $cwd = ssh2_sftp_realpath( $this->sftp_link, '.' ); 
  245. if ( $cwd ) { 
  246. $cwd = trailingslashit( trim( $cwd ) ); 
  247. return $cwd; 
  248.  
  249. /** 
  250. * @access public 
  251. * 
  252. * @param string $dir 
  253. * @return bool|string 
  254. */ 
  255. public function chdir($dir) { 
  256. return $this->run_command('cd ' . $dir, true); 
  257.  
  258. /** 
  259. * @access public 
  260. * 
  261. * @param string $file 
  262. * @param string $group 
  263. * @param bool $recursive 
  264. * 
  265. * @return bool 
  266. */ 
  267. public function chgrp($file, $group, $recursive = false ) { 
  268. if ( ! $this->exists($file) ) 
  269. return false; 
  270. if ( ! $recursive || ! $this->is_dir($file) ) 
  271. return $this->run_command(sprintf('chgrp %s %s', escapeshellarg($group), escapeshellarg($file)), true); 
  272. return $this->run_command(sprintf('chgrp -R %s %s', escapeshellarg($group), escapeshellarg($file)), true); 
  273.  
  274. /** 
  275. * @access public 
  276. * 
  277. * @param string $file 
  278. * @param int $mode 
  279. * @param bool $recursive 
  280. * @return bool|string 
  281. */ 
  282. public function chmod($file, $mode = false, $recursive = false) { 
  283. if ( ! $this->exists($file) ) 
  284. return false; 
  285.  
  286. if ( ! $mode ) { 
  287. if ( $this->is_file($file) ) 
  288. $mode = FS_CHMOD_FILE; 
  289. elseif ( $this->is_dir($file) ) 
  290. $mode = FS_CHMOD_DIR; 
  291. else 
  292. return false; 
  293.  
  294. if ( ! $recursive || ! $this->is_dir($file) ) 
  295. return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true); 
  296. return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true); 
  297.  
  298. /** 
  299. * Change the ownership of a file / folder. 
  300. * 
  301. * @access public 
  302. * 
  303. * @param string $file Path to the file. 
  304. * @param string|int $owner A user name or number. 
  305. * @param bool $recursive Optional. If set True changes file owner recursivly. Default False. 
  306. * @return bool True on success or false on failure. 
  307. */ 
  308. public function chown( $file, $owner, $recursive = false ) { 
  309. if ( ! $this->exists($file) ) 
  310. return false; 
  311. if ( ! $recursive || ! $this->is_dir($file) ) 
  312. return $this->run_command(sprintf('chown %s %s', escapeshellarg($owner), escapeshellarg($file)), true); 
  313. return $this->run_command(sprintf('chown -R %s %s', escapeshellarg($owner), escapeshellarg($file)), true); 
  314.  
  315. /** 
  316. * @access public 
  317. * 
  318. * @param string $file 
  319. * @return string|false 
  320. */ 
  321. public function owner($file) { 
  322. $owneruid = @fileowner( $this->sftp_path( $file ) ); 
  323. if ( ! $owneruid ) 
  324. return false; 
  325. if ( ! function_exists('posix_getpwuid') ) 
  326. return $owneruid; 
  327. $ownerarray = posix_getpwuid($owneruid); 
  328. return $ownerarray['name']; 
  329.  
  330. /** 
  331. * @access public 
  332. * 
  333. * @param string $file 
  334. * @return string 
  335. */ 
  336. public function getchmod($file) { 
  337. return substr( decoct( @fileperms( $this->sftp_path( $file ) ) ), -3 ); 
  338.  
  339. /** 
  340. * @access public 
  341. * 
  342. * @param string $file 
  343. * @return string|false 
  344. */ 
  345. public function group($file) { 
  346. $gid = @filegroup( $this->sftp_path( $file ) ); 
  347. if ( ! $gid ) 
  348. return false; 
  349. if ( ! function_exists('posix_getgrgid') ) 
  350. return $gid; 
  351. $grouparray = posix_getgrgid($gid); 
  352. return $grouparray['name']; 
  353.  
  354. /** 
  355. * @access public 
  356. * 
  357. * @param string $source 
  358. * @param string $destination 
  359. * @param bool $overwrite 
  360. * @param int|bool $mode 
  361. * @return bool 
  362. */ 
  363. public function copy($source, $destination, $overwrite = false, $mode = false) { 
  364. if ( ! $overwrite && $this->exists($destination) ) 
  365. return false; 
  366. $content = $this->get_contents($source); 
  367. if ( false === $content) 
  368. return false; 
  369. return $this->put_contents($destination, $content, $mode); 
  370.  
  371. /** 
  372. * @access public 
  373. * 
  374. * @param string $source 
  375. * @param string $destination 
  376. * @param bool $overwrite 
  377. * @return bool 
  378. */ 
  379. public function move($source, $destination, $overwrite = false) { 
  380. return @ssh2_sftp_rename( $this->sftp_link, $source, $destination ); 
  381.  
  382. /** 
  383. * @access public 
  384. * 
  385. * @param string $file 
  386. * @param bool $recursive 
  387. * @param string|bool $type 
  388. * @return bool 
  389. */ 
  390. public function delete($file, $recursive = false, $type = false) { 
  391. if ( 'f' == $type || $this->is_file($file) ) 
  392. return ssh2_sftp_unlink($this->sftp_link, $file); 
  393. if ( ! $recursive ) 
  394. return ssh2_sftp_rmdir($this->sftp_link, $file); 
  395. $filelist = $this->dirlist($file); 
  396. if ( is_array($filelist) ) { 
  397. foreach ( $filelist as $filename => $fileinfo) { 
  398. $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']); 
  399. return ssh2_sftp_rmdir($this->sftp_link, $file); 
  400.  
  401. /** 
  402. * @access public 
  403. * 
  404. * @param string $file 
  405. * @return bool 
  406. */ 
  407. public function exists($file) { 
  408. return file_exists( $this->sftp_path( $file ) ); 
  409.  
  410. /** 
  411. * @access public 
  412. * 
  413. * @param string $file 
  414. * @return bool 
  415. */ 
  416. public function is_file($file) { 
  417. return is_file( $this->sftp_path( $file ) ); 
  418.  
  419. /** 
  420. * @access public 
  421. * 
  422. * @param string $path 
  423. * @return bool 
  424. */ 
  425. public function is_dir($path) { 
  426. return is_dir( $this->sftp_path( $path ) ); 
  427.  
  428. /** 
  429. * @access public 
  430. * 
  431. * @param string $file 
  432. * @return bool 
  433. */ 
  434. public function is_readable($file) { 
  435. return is_readable( $this->sftp_path( $file ) ); 
  436.  
  437. /** 
  438. * @access public 
  439. * 
  440. * @param string $file 
  441. * @return bool 
  442. */ 
  443. public function is_writable($file) { 
  444. // PHP will base it's writable checks on system_user === file_owner, not ssh_user === file_owner 
  445. return true; 
  446.  
  447. /** 
  448. * @access public 
  449. * 
  450. * @param string $file 
  451. * @return int 
  452. */ 
  453. public function atime($file) { 
  454. return fileatime( $this->sftp_path( $file ) ); 
  455.  
  456. /** 
  457. * @access public 
  458. * 
  459. * @param string $file 
  460. * @return int 
  461. */ 
  462. public function mtime($file) { 
  463. return filemtime( $this->sftp_path( $file ) ); 
  464.  
  465. /** 
  466. * @access public 
  467. * 
  468. * @param string $file 
  469. * @return int 
  470. */ 
  471. public function size($file) { 
  472. return filesize( $this->sftp_path( $file ) ); 
  473.  
  474. /** 
  475. * @access public 
  476. * 
  477. * @param string $file 
  478. * @param int $time 
  479. * @param int $atime 
  480. */ 
  481. public function touch($file, $time = 0, $atime = 0) { 
  482. //Not implemented. 
  483.  
  484. /** 
  485. * @access public 
  486. * 
  487. * @param string $path 
  488. * @param mixed $chmod 
  489. * @param mixed $chown 
  490. * @param mixed $chgrp 
  491. * @return bool 
  492. */ 
  493. public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) { 
  494. $path = untrailingslashit($path); 
  495. if ( empty($path) ) 
  496. return false; 
  497.  
  498. if ( ! $chmod ) 
  499. $chmod = FS_CHMOD_DIR; 
  500. if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) ) 
  501. return false; 
  502. if ( $chown ) 
  503. $this->chown($path, $chown); 
  504. if ( $chgrp ) 
  505. $this->chgrp($path, $chgrp); 
  506. return true; 
  507.  
  508. /** 
  509. * @access public 
  510. * 
  511. * @param string $path 
  512. * @param bool $recursive 
  513. * @return bool 
  514. */ 
  515. public function rmdir($path, $recursive = false) { 
  516. return $this->delete($path, $recursive); 
  517.  
  518. /** 
  519. * @access public 
  520. * 
  521. * @param string $path 
  522. * @param bool $include_hidden 
  523. * @param bool $recursive 
  524. * @return bool|array 
  525. */ 
  526. public function dirlist($path, $include_hidden = true, $recursive = false) { 
  527. if ( $this->is_file($path) ) { 
  528. $limit_file = basename($path); 
  529. $path = dirname($path); 
  530. } else { 
  531. $limit_file = false; 
  532.  
  533. if ( ! $this->is_dir($path) ) 
  534. return false; 
  535.  
  536. $ret = array(); 
  537. $dir = @dir( $this->sftp_path( $path ) ); 
  538.  
  539. if ( ! $dir ) 
  540. return false; 
  541.  
  542. while (false !== ($entry = $dir->read()) ) { 
  543. $struc = array(); 
  544. $struc['name'] = $entry; 
  545.  
  546. if ( '.' == $struc['name'] || '..' == $struc['name'] ) 
  547. continue; //Do not care about these folders. 
  548.  
  549. if ( ! $include_hidden && '.' == $struc['name'][0] ) 
  550. continue; 
  551.  
  552. if ( $limit_file && $struc['name'] != $limit_file ) 
  553. continue; 
  554.  
  555. $struc['perms'] = $this->gethchmod($path.'/'.$entry); 
  556. $struc['permsn'] = $this->getnumchmodfromh($struc['perms']); 
  557. $struc['number'] = false; 
  558. $struc['owner'] = $this->owner($path.'/'.$entry); 
  559. $struc['group'] = $this->group($path.'/'.$entry); 
  560. $struc['size'] = $this->size($path.'/'.$entry); 
  561. $struc['lastmodunix']= $this->mtime($path.'/'.$entry); 
  562. $struc['lastmod'] = date('M j', $struc['lastmodunix']); 
  563. $struc['time'] = date('h:i:s', $struc['lastmodunix']); 
  564. $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f'; 
  565.  
  566. if ( 'd' == $struc['type'] ) { 
  567. if ( $recursive ) 
  568. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive); 
  569. else 
  570. $struc['files'] = array(); 
  571.  
  572. $ret[ $struc['name'] ] = $struc; 
  573. $dir->close(); 
  574. unset($dir); 
  575. return $ret; 
.