source: trunk/www.guidonia.net/wp/wp-admin/includes/class-wp-filesystem-ssh2.php@ 44

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