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