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