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