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