25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

632 lines
15 KiB

  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. * @access public
  39. */
  40. public $link = false;
  41. /**
  42. * @access public
  43. * @var resource
  44. */
  45. public $sftp_link;
  46. public $keys = false;
  47. /**
  48. * @access public
  49. *
  50. * @param array $opt
  51. */
  52. public function __construct( $opt = '' ) {
  53. $this->method = 'ssh2';
  54. $this->errors = new WP_Error();
  55. //Check if possible to use ssh2 functions.
  56. if ( ! extension_loaded('ssh2') ) {
  57. $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
  58. return;
  59. }
  60. if ( !function_exists('stream_get_contents') ) {
  61. $this->errors->add(
  62. 'ssh2_php_requirement',
  63. sprintf(
  64. /* translators: %s: stream_get_contents() */
  65. __( 'The ssh2 PHP extension is available, however, we require the PHP5 function %s' ),
  66. '<code>stream_get_contents()</code>'
  67. )
  68. );
  69. return;
  70. }
  71. // Set defaults:
  72. if ( empty($opt['port']) )
  73. $this->options['port'] = 22;
  74. else
  75. $this->options['port'] = $opt['port'];
  76. if ( empty($opt['hostname']) )
  77. $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
  78. else
  79. $this->options['hostname'] = $opt['hostname'];
  80. // Check if the options provided are OK.
  81. if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
  82. $this->options['public_key'] = $opt['public_key'];
  83. $this->options['private_key'] = $opt['private_key'];
  84. $this->options['hostkey'] = array('hostkey' => 'ssh-rsa');
  85. $this->keys = true;
  86. } elseif ( empty ($opt['username']) ) {
  87. $this->errors->add('empty_username', __('SSH2 username is required'));
  88. }
  89. if ( !empty($opt['username']) )
  90. $this->options['username'] = $opt['username'];
  91. if ( empty ($opt['password']) ) {
  92. // Password can be blank if we are using keys.
  93. if ( !$this->keys )
  94. $this->errors->add('empty_password', __('SSH2 password is required'));
  95. } else {
  96. $this->options['password'] = $opt['password'];
  97. }
  98. }
  99. /**
  100. * @access public
  101. *
  102. * @return bool
  103. */
  104. public function connect() {
  105. if ( ! $this->keys ) {
  106. $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
  107. } else {
  108. $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
  109. }
  110. if ( ! $this->link ) {
  111. $this->errors->add( 'connect',
  112. /* translators: %s: hostname:port */
  113. sprintf( __( 'Failed to connect to SSH2 Server %s' ),
  114. $this->options['hostname'] . ':' . $this->options['port']
  115. )
  116. );
  117. return false;
  118. }
  119. if ( !$this->keys ) {
  120. if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
  121. $this->errors->add( 'auth',
  122. /* translators: %s: username */
  123. sprintf( __( 'Username/Password incorrect for %s' ),
  124. $this->options['username']
  125. )
  126. );
  127. return false;
  128. }
  129. } else {
  130. if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
  131. $this->errors->add( 'auth',
  132. /* translators: %s: username */
  133. sprintf( __( 'Public and Private keys incorrect for %s' ),
  134. $this->options['username']
  135. )
  136. );
  137. return false;
  138. }
  139. }
  140. $this->sftp_link = ssh2_sftp( $this->link );
  141. if ( ! $this->sftp_link ) {
  142. $this->errors->add( 'connect',
  143. /* translators: %s: hostname:port */
  144. sprintf( __( 'Failed to initialize a SFTP subsystem session with the SSH2 Server %s' ),
  145. $this->options['hostname'] . ':' . $this->options['port']
  146. )
  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. }
  171. return 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $path, '/' );
  172. }
  173. /**
  174. * @access public
  175. *
  176. * @param string $command
  177. * @param bool $returnbool
  178. * @return bool|string True on success, false on failure. String if the command was executed, `$returnbool`
  179. * is false (default), and data from the resulting stream was retrieved.
  180. */
  181. public function run_command( $command, $returnbool = false ) {
  182. if ( ! $this->link )
  183. return false;
  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. );
  191. } else {
  192. stream_set_blocking( $stream, true );
  193. stream_set_timeout( $stream, FS_TIMEOUT );
  194. $data = stream_get_contents( $stream );
  195. fclose( $stream );
  196. if ( $returnbool )
  197. return ( $data === false ) ? false : '' != trim($data);
  198. else
  199. return $data;
  200. }
  201. return false;
  202. }
  203. /**
  204. * @access public
  205. *
  206. * @param string $file
  207. * @return string|false
  208. */
  209. public function get_contents( $file ) {
  210. return file_get_contents( $this->sftp_path( $file ) );
  211. }
  212. /**
  213. * @access public
  214. *
  215. * @param string $file
  216. * @return array
  217. */
  218. public function get_contents_array($file) {
  219. return file( $this->sftp_path( $file ) );
  220. }
  221. /**
  222. * @access public
  223. *
  224. * @param string $file
  225. * @param string $contents
  226. * @param bool|int $mode
  227. * @return bool
  228. */
  229. public function put_contents($file, $contents, $mode = false ) {
  230. $ret = file_put_contents( $this->sftp_path( $file ), $contents );
  231. if ( $ret !== strlen( $contents ) )
  232. return false;
  233. $this->chmod($file, $mode);
  234. return true;
  235. }
  236. /**
  237. * @access public
  238. *
  239. * @return bool
  240. */
  241. public function cwd() {
  242. $cwd = ssh2_sftp_realpath( $this->sftp_link, '.' );
  243. if ( $cwd ) {
  244. $cwd = trailingslashit( trim( $cwd ) );
  245. }
  246. return $cwd;
  247. }
  248. /**
  249. * @access public
  250. *
  251. * @param string $dir
  252. * @return bool|string
  253. */
  254. public function chdir($dir) {
  255. return $this->run_command('cd ' . $dir, true);
  256. }
  257. /**
  258. * @access public
  259. *
  260. * @param string $file
  261. * @param string $group
  262. * @param bool $recursive
  263. *
  264. * @return bool
  265. */
  266. public function chgrp($file, $group, $recursive = false ) {
  267. if ( ! $this->exists($file) )
  268. return false;
  269. if ( ! $recursive || ! $this->is_dir($file) )
  270. return $this->run_command(sprintf('chgrp %s %s', escapeshellarg($group), escapeshellarg($file)), true);
  271. return $this->run_command(sprintf('chgrp -R %s %s', escapeshellarg($group), escapeshellarg($file)), true);
  272. }
  273. /**
  274. * @access public
  275. *
  276. * @param string $file
  277. * @param int $mode
  278. * @param bool $recursive
  279. * @return bool|string
  280. */
  281. public function chmod($file, $mode = false, $recursive = false) {
  282. if ( ! $this->exists($file) )
  283. return false;
  284. if ( ! $mode ) {
  285. if ( $this->is_file($file) )
  286. $mode = FS_CHMOD_FILE;
  287. elseif ( $this->is_dir($file) )
  288. $mode = FS_CHMOD_DIR;
  289. else
  290. return false;
  291. }
  292. if ( ! $recursive || ! $this->is_dir($file) )
  293. return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
  294. return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
  295. }
  296. /**
  297. * Change the ownership of a file / folder.
  298. *
  299. * @access public
  300. *
  301. * @param string $file Path to the file.
  302. * @param string|int $owner A user name or number.
  303. * @param bool $recursive Optional. If set True changes file owner recursivly. Default False.
  304. * @return bool True on success or false on failure.
  305. */
  306. public function chown( $file, $owner, $recursive = false ) {
  307. if ( ! $this->exists($file) )
  308. return false;
  309. if ( ! $recursive || ! $this->is_dir($file) )
  310. return $this->run_command(sprintf('chown %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
  311. return $this->run_command(sprintf('chown -R %s %s', escapeshellarg($owner), escapeshellarg($file)), true);
  312. }
  313. /**
  314. * @access public
  315. *
  316. * @param string $file
  317. * @return string|false
  318. */
  319. public function owner($file) {
  320. $owneruid = @fileowner( $this->sftp_path( $file ) );
  321. if ( ! $owneruid )
  322. return false;
  323. if ( ! function_exists('posix_getpwuid') )
  324. return $owneruid;
  325. $ownerarray = posix_getpwuid($owneruid);
  326. return $ownerarray['name'];
  327. }
  328. /**
  329. * @access public
  330. *
  331. * @param string $file
  332. * @return string
  333. */
  334. public function getchmod($file) {
  335. return substr( decoct( @fileperms( $this->sftp_path( $file ) ) ), -3 );
  336. }
  337. /**
  338. * @access public
  339. *
  340. * @param string $file
  341. * @return string|false
  342. */
  343. public function group($file) {
  344. $gid = @filegroup( $this->sftp_path( $file ) );
  345. if ( ! $gid )
  346. return false;
  347. if ( ! function_exists('posix_getgrgid') )
  348. return $gid;
  349. $grouparray = posix_getgrgid($gid);
  350. return $grouparray['name'];
  351. }
  352. /**
  353. * @access public
  354. *
  355. * @param string $source
  356. * @param string $destination
  357. * @param bool $overwrite
  358. * @param int|bool $mode
  359. * @return bool
  360. */
  361. public function copy($source, $destination, $overwrite = false, $mode = false) {
  362. if ( ! $overwrite && $this->exists($destination) )
  363. return false;
  364. $content = $this->get_contents($source);
  365. if ( false === $content)
  366. return false;
  367. return $this->put_contents($destination, $content, $mode);
  368. }
  369. /**
  370. * @access public
  371. *
  372. * @param string $source
  373. * @param string $destination
  374. * @param bool $overwrite
  375. * @return bool
  376. */
  377. public function move($source, $destination, $overwrite = false) {
  378. return @ssh2_sftp_rename( $this->sftp_link, $source, $destination );
  379. }
  380. /**
  381. * @access public
  382. *
  383. * @param string $file
  384. * @param bool $recursive
  385. * @param string|bool $type
  386. * @return bool
  387. */
  388. public function delete($file, $recursive = false, $type = false) {
  389. if ( 'f' == $type || $this->is_file($file) )
  390. return ssh2_sftp_unlink($this->sftp_link, $file);
  391. if ( ! $recursive )
  392. return ssh2_sftp_rmdir($this->sftp_link, $file);
  393. $filelist = $this->dirlist($file);
  394. if ( is_array($filelist) ) {
  395. foreach ( $filelist as $filename => $fileinfo) {
  396. $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
  397. }
  398. }
  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. if ( ! $chmod )
  498. $chmod = FS_CHMOD_DIR;
  499. if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
  500. return false;
  501. if ( $chown )
  502. $this->chown($path, $chown);
  503. if ( $chgrp )
  504. $this->chgrp($path, $chgrp);
  505. return true;
  506. }
  507. /**
  508. * @access public
  509. *
  510. * @param string $path
  511. * @param bool $recursive
  512. * @return bool
  513. */
  514. public function rmdir($path, $recursive = false) {
  515. return $this->delete($path, $recursive);
  516. }
  517. /**
  518. * @access public
  519. *
  520. * @param string $path
  521. * @param bool $include_hidden
  522. * @param bool $recursive
  523. * @return bool|array
  524. */
  525. public function dirlist($path, $include_hidden = true, $recursive = false) {
  526. if ( $this->is_file($path) ) {
  527. $limit_file = basename($path);
  528. $path = dirname($path);
  529. } else {
  530. $limit_file = false;
  531. }
  532. if ( ! $this->is_dir($path) )
  533. return false;
  534. $ret = array();
  535. $dir = @dir( $this->sftp_path( $path ) );
  536. if ( ! $dir )
  537. return false;
  538. while (false !== ($entry = $dir->read()) ) {
  539. $struc = array();
  540. $struc['name'] = $entry;
  541. if ( '.' == $struc['name'] || '..' == $struc['name'] )
  542. continue; //Do not care about these folders.
  543. if ( ! $include_hidden && '.' == $struc['name'][0] )
  544. continue;
  545. if ( $limit_file && $struc['name'] != $limit_file )
  546. continue;
  547. $struc['perms'] = $this->gethchmod($path.'/'.$entry);
  548. $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
  549. $struc['number'] = false;
  550. $struc['owner'] = $this->owner($path.'/'.$entry);
  551. $struc['group'] = $this->group($path.'/'.$entry);
  552. $struc['size'] = $this->size($path.'/'.$entry);
  553. $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
  554. $struc['lastmod'] = date('M j',$struc['lastmodunix']);
  555. $struc['time'] = date('h:i:s',$struc['lastmodunix']);
  556. $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
  557. if ( 'd' == $struc['type'] ) {
  558. if ( $recursive )
  559. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  560. else
  561. $struc['files'] = array();
  562. }
  563. $ret[ $struc['name'] ] = $struc;
  564. }
  565. $dir->close();
  566. unset($dir);
  567. return $ret;
  568. }
  569. }