WP_Filesystem_SSH2

WordPress Filesystem Class for implementing SSH2.

Defined (1)

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

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