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