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