PageRenderTime 30ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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