PageRenderTime 62ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/floppyxyz/musical
PHP | 386 lines | 279 code | 63 blank | 44 comment | 70 complexity | 3de1191f97d437526cc421829c544c2e MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * WordPress SSH2 Filesystem.
  4. *
  5. * @package WordPress
  6. * @subpackage Filesystem
  7. */
  8. /**
  9. * WordPress Filesystem Class for implementing SSH2.
  10. *
  11. * To use this class you must follow these steps for PHP 5.2.6+
  12. *
  13. * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
  14. *
  15. * 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)
  16. *
  17. * cd /usr/src
  18. * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
  19. * tar -zxvf libssh2-0.14.tar.gz
  20. * cd libssh2-0.14/
  21. * ./configure
  22. * make all install
  23. *
  24. * Note: Do not leave the directory yet!
  25. *
  26. * Enter: pecl install -f ssh2
  27. *
  28. * Copy the ssh.so file it creates to your PHP Module Directory.
  29. * Open up your PHP.INI file and look for where extensions are placed.
  30. * Add in your PHP.ini file: extension=ssh2.so
  31. *
  32. * Restart Apache!
  33. * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp exist.
  34. *
  35. * Note: as of WordPress 2.8, This utilises the PHP5+ function 'stream_get_contents'
  36. *
  37. * @since 2.7
  38. * @package WordPress
  39. * @subpackage Filesystem
  40. * @uses WP_Filesystem_Base Extends class
  41. */
  42. class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
  43. var $link = false;
  44. var $sftp_link = false;
  45. var $keys = false;
  46. var $errors = array();
  47. var $options = array();
  48. function __construct($opt='') {
  49. $this->method = 'ssh2';
  50. $this->errors = new WP_Error();
  51. //Check if possible to use ssh2 functions.
  52. if ( ! extension_loaded('ssh2') ) {
  53. $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
  54. return false;
  55. }
  56. if ( !function_exists('stream_get_contents') ) {
  57. $this->errors->add('ssh2_php_requirement', __('The ssh2 PHP extension is available, however, we require the PHP5 function <code>stream_get_contents()</code>'));
  58. return false;
  59. }
  60. // Set defaults:
  61. if ( empty($opt['port']) )
  62. $this->options['port'] = 22;
  63. else
  64. $this->options['port'] = $opt['port'];
  65. if ( empty($opt['hostname']) )
  66. $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
  67. else
  68. $this->options['hostname'] = $opt['hostname'];
  69. if ( ! empty($opt['base']) )
  70. $this->wp_base = $opt['base'];
  71. // Check if the options provided are OK.
  72. if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) {
  73. $this->options['public_key'] = $opt['public_key'];
  74. $this->options['private_key'] = $opt['private_key'];
  75. $this->options['hostkey'] = array('hostkey' => 'ssh-rsa');
  76. $this->keys = true;
  77. } elseif ( empty ($opt['username']) ) {
  78. $this->errors->add('empty_username', __('SSH2 username is required'));
  79. }
  80. if ( !empty($opt['username']) )
  81. $this->options['username'] = $opt['username'];
  82. if ( empty ($opt['password']) ) {
  83. if ( !$this->keys ) //password can be blank if we are using keys
  84. $this->errors->add('empty_password', __('SSH2 password is required'));
  85. } else {
  86. $this->options['password'] = $opt['password'];
  87. }
  88. }
  89. function connect() {
  90. if ( ! $this->keys ) {
  91. $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
  92. } else {
  93. $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
  94. }
  95. if ( ! $this->link ) {
  96. $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
  97. return false;
  98. }
  99. if ( !$this->keys ) {
  100. if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
  101. $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
  102. return false;
  103. }
  104. } else {
  105. if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
  106. $this->errors->add('auth', sprintf(__('Public and Private keys incorrect for %s'), $this->options['username']));
  107. return false;
  108. }
  109. }
  110. $this->sftp_link = ssh2_sftp($this->link);
  111. return true;
  112. }
  113. function run_command( $command, $returnbool = false) {
  114. if ( ! $this->link )
  115. return false;
  116. if ( ! ($stream = ssh2_exec($this->link, $command)) ) {
  117. $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
  118. } else {
  119. stream_set_blocking( $stream, true );
  120. stream_set_timeout( $stream, FS_TIMEOUT );
  121. $data = stream_get_contents( $stream );
  122. fclose( $stream );
  123. if ( $returnbool )
  124. return ( $data === false ) ? false : '' != trim($data);
  125. else
  126. return $data;
  127. }
  128. return false;
  129. }
  130. function get_contents($file, $type = '', $resumepos = 0 ) {
  131. $file = ltrim($file, '/');
  132. return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  133. }
  134. function get_contents_array($file) {
  135. $file = ltrim($file, '/');
  136. return file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  137. }
  138. function put_contents($file, $contents, $mode = false ) {
  139. $file = ltrim($file, '/');
  140. $ret = file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
  141. $this->chmod($file, $mode);
  142. return false !== $ret;
  143. }
  144. function cwd() {
  145. $cwd = $this->run_command('pwd');
  146. if ( $cwd )
  147. $cwd = trailingslashit($cwd);
  148. return $cwd;
  149. }
  150. function chdir($dir) {
  151. return $this->run_command('cd ' . $dir, true);
  152. }
  153. function chgrp($file, $group, $recursive = false ) {
  154. if ( ! $this->exists($file) )
  155. return false;
  156. if ( ! $recursive || ! $this->is_dir($file) )
  157. return $this->run_command(sprintf('chgrp %o %s', $mode, escapeshellarg($file)), true);
  158. return $this->run_command(sprintf('chgrp -R %o %s', $mode, escapeshellarg($file)), true);
  159. }
  160. function chmod($file, $mode = false, $recursive = false) {
  161. if ( ! $this->exists($file) )
  162. return false;
  163. if ( ! $mode ) {
  164. if ( $this->is_file($file) )
  165. $mode = FS_CHMOD_FILE;
  166. elseif ( $this->is_dir($file) )
  167. $mode = FS_CHMOD_DIR;
  168. else
  169. return false;
  170. }
  171. if ( ! $recursive || ! $this->is_dir($file) )
  172. return $this->run_command(sprintf('chmod %o %s', $mode, escapeshellarg($file)), true);
  173. return $this->run_command(sprintf('chmod -R %o %s', $mode, escapeshellarg($file)), true);
  174. }
  175. function chown($file, $owner, $recursive = false ) {
  176. if ( ! $this->exists($file) )
  177. return false;
  178. if ( ! $recursive || ! $this->is_dir($file) )
  179. return $this->run_command(sprintf('chown %o %s', $mode, escapeshellarg($file)), true);
  180. return $this->run_command(sprintf('chown -R %o %s', $mode, escapeshellarg($file)), true);
  181. }
  182. function owner($file) {
  183. $owneruid = @fileowner('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
  184. if ( ! $owneruid )
  185. return false;
  186. if ( ! function_exists('posix_getpwuid') )
  187. return $owneruid;
  188. $ownerarray = posix_getpwuid($owneruid);
  189. return $ownerarray['name'];
  190. }
  191. function getchmod($file) {
  192. return substr(decoct(@fileperms( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/') )),3);
  193. }
  194. function group($file) {
  195. $gid = @filegroup('ssh2.sftp://' . $this->sftp_link . '/' . ltrim($file, '/'));
  196. if ( ! $gid )
  197. return false;
  198. if ( ! function_exists('posix_getgrgid') )
  199. return $gid;
  200. $grouparray = posix_getgrgid($gid);
  201. return $grouparray['name'];
  202. }
  203. function copy($source, $destination, $overwrite = false, $mode = false) {
  204. if ( ! $overwrite && $this->exists($destination) )
  205. return false;
  206. $content = $this->get_contents($source);
  207. if ( false === $content)
  208. return false;
  209. return $this->put_contents($destination, $content, $mode);
  210. }
  211. function move($source, $destination, $overwrite = false) {
  212. return @ssh2_sftp_rename($this->link, $source, $destination);
  213. }
  214. function delete($file, $recursive = false, $type = false) {
  215. if ( 'f' == $type || $this->is_file($file) )
  216. return ssh2_sftp_unlink($this->sftp_link, $file);
  217. if ( ! $recursive )
  218. return ssh2_sftp_rmdir($this->sftp_link, $file);
  219. $filelist = $this->dirlist($file);
  220. if ( is_array($filelist) ) {
  221. foreach ( $filelist as $filename => $fileinfo) {
  222. $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
  223. }
  224. }
  225. return ssh2_sftp_rmdir($this->sftp_link, $file);
  226. }
  227. function exists($file) {
  228. $file = ltrim($file, '/');
  229. return file_exists('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  230. }
  231. function is_file($file) {
  232. $file = ltrim($file, '/');
  233. return is_file('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  234. }
  235. function is_dir($path) {
  236. $path = ltrim($path, '/');
  237. return is_dir('ssh2.sftp://' . $this->sftp_link . '/' . $path);
  238. }
  239. function is_readable($file) {
  240. $file = ltrim($file, '/');
  241. return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  242. }
  243. function is_writable($file) {
  244. $file = ltrim($file, '/');
  245. return is_writable('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  246. }
  247. function atime($file) {
  248. $file = ltrim($file, '/');
  249. return fileatime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  250. }
  251. function mtime($file) {
  252. $file = ltrim($file, '/');
  253. return filemtime('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  254. }
  255. function size($file) {
  256. $file = ltrim($file, '/');
  257. return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file);
  258. }
  259. function touch($file, $time = 0, $atime = 0) {
  260. //Not implemented.
  261. }
  262. function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
  263. $path = untrailingslashit($path);
  264. if ( empty($path) )
  265. return false;
  266. if ( ! $chmod )
  267. $chmod = FS_CHMOD_DIR;
  268. if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
  269. return false;
  270. if ( $chown )
  271. $this->chown($path, $chown);
  272. if ( $chgrp )
  273. $this->chgrp($path, $chgrp);
  274. return true;
  275. }
  276. function rmdir($path, $recursive = false) {
  277. return $this->delete($path, $recursive);
  278. }
  279. function dirlist($path, $include_hidden = true, $recursive = false) {
  280. if ( $this->is_file($path) ) {
  281. $limit_file = basename($path);
  282. $path = dirname($path);
  283. } else {
  284. $limit_file = false;
  285. }
  286. if ( ! $this->is_dir($path) )
  287. return false;
  288. $ret = array();
  289. $dir = @dir('ssh2.sftp://' . $this->sftp_link .'/' . ltrim($path, '/') );
  290. if ( ! $dir )
  291. return false;
  292. while (false !== ($entry = $dir->read()) ) {
  293. $struc = array();
  294. $struc['name'] = $entry;
  295. if ( '.' == $struc['name'] || '..' == $struc['name'] )
  296. continue; //Do not care about these folders.
  297. if ( ! $include_hidden && '.' == $struc['name'][0] )
  298. continue;
  299. if ( $limit_file && $struc['name'] != $limit_file )
  300. continue;
  301. $struc['perms'] = $this->gethchmod($path.'/'.$entry);
  302. $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
  303. $struc['number'] = false;
  304. $struc['owner'] = $this->owner($path.'/'.$entry);
  305. $struc['group'] = $this->group($path.'/'.$entry);
  306. $struc['size'] = $this->size($path.'/'.$entry);
  307. $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
  308. $struc['lastmod'] = date('M j',$struc['lastmodunix']);
  309. $struc['time'] = date('h:i:s',$struc['lastmodunix']);
  310. $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
  311. if ( 'd' == $struc['type'] ) {
  312. if ( $recursive )
  313. $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  314. else
  315. $struc['files'] = array();
  316. }
  317. $ret[ $struc['name'] ] = $struc;
  318. }
  319. $dir->close();
  320. unset($dir);
  321. return $ret;
  322. }
  323. }