0
|
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 |
|
5
|
38 |
public $link = false; |
|
39 |
/** |
|
40 |
* @var resource |
|
41 |
*/ |
|
42 |
public $sftp_link; |
|
43 |
public $keys = false; |
0
|
44 |
|
5
|
45 |
public function __construct($opt='') { |
0
|
46 |
$this->method = 'ssh2'; |
|
47 |
$this->errors = new WP_Error(); |
|
48 |
|
|
49 |
//Check if possible to use ssh2 functions. |
|
50 |
if ( ! extension_loaded('ssh2') ) { |
|
51 |
$this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available')); |
5
|
52 |
return; |
0
|
53 |
} |
|
54 |
if ( !function_exists('stream_get_contents') ) { |
|
55 |
$this->errors->add('ssh2_php_requirement', __('The ssh2 PHP extension is available, however, we require the PHP5 function <code>stream_get_contents()</code>')); |
5
|
56 |
return; |
0
|
57 |
} |
|
58 |
|
|
59 |
// Set defaults: |
|
60 |
if ( empty($opt['port']) ) |
|
61 |
$this->options['port'] = 22; |
|
62 |
else |
|
63 |
$this->options['port'] = $opt['port']; |
|
64 |
|
|
65 |
if ( empty($opt['hostname']) ) |
|
66 |
$this->errors->add('empty_hostname', __('SSH2 hostname is required')); |
|
67 |
else |
|
68 |
$this->options['hostname'] = $opt['hostname']; |
|
69 |
|
|
70 |
// Check if the options provided are OK. |
|
71 |
if ( !empty ($opt['public_key']) && !empty ($opt['private_key']) ) { |
|
72 |
$this->options['public_key'] = $opt['public_key']; |
|
73 |
$this->options['private_key'] = $opt['private_key']; |
|
74 |
|
|
75 |
$this->options['hostkey'] = array('hostkey' => 'ssh-rsa'); |
|
76 |
|
|
77 |
$this->keys = true; |
|
78 |
} elseif ( empty ($opt['username']) ) { |
|
79 |
$this->errors->add('empty_username', __('SSH2 username is required')); |
|
80 |
} |
|
81 |
|
|
82 |
if ( !empty($opt['username']) ) |
|
83 |
$this->options['username'] = $opt['username']; |
|
84 |
|
|
85 |
if ( empty ($opt['password']) ) { |
5
|
86 |
// Password can be blank if we are using keys. |
|
87 |
if ( !$this->keys ) |
0
|
88 |
$this->errors->add('empty_password', __('SSH2 password is required')); |
|
89 |
} else { |
|
90 |
$this->options['password'] = $opt['password']; |
|
91 |
} |
|
92 |
|
|
93 |
} |
|
94 |
|
5
|
95 |
public function connect() { |
0
|
96 |
if ( ! $this->keys ) { |
|
97 |
$this->link = @ssh2_connect($this->options['hostname'], $this->options['port']); |
|
98 |
} else { |
|
99 |
$this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']); |
|
100 |
} |
|
101 |
|
|
102 |
if ( ! $this->link ) { |
|
103 |
$this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port'])); |
|
104 |
return false; |
|
105 |
} |
|
106 |
|
|
107 |
if ( !$this->keys ) { |
|
108 |
if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) { |
|
109 |
$this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username'])); |
|
110 |
return false; |
|
111 |
} |
|
112 |
} else { |
|
113 |
if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) { |
|
114 |
$this->errors->add('auth', sprintf(__('Public and Private keys incorrect for %s'), $this->options['username'])); |
|
115 |
return false; |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
$this->sftp_link = ssh2_sftp($this->link); |
|
120 |
|
|
121 |
return true; |
|
122 |
} |
|
123 |
|
5
|
124 |
/** |
|
125 |
* @param string $command |
|
126 |
* @param bool $returnbool |
|
127 |
* @return bool|string |
|
128 |
*/ |
|
129 |
public function run_command( $command, $returnbool = false) { |
0
|
130 |
|
|
131 |
if ( ! $this->link ) |
|
132 |
return false; |
|
133 |
|
|
134 |
if ( ! ($stream = ssh2_exec($this->link, $command)) ) { |
|
135 |
$this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command)); |
|
136 |
} else { |
|
137 |
stream_set_blocking( $stream, true ); |
|
138 |
stream_set_timeout( $stream, FS_TIMEOUT ); |
|
139 |
$data = stream_get_contents( $stream ); |
|
140 |
fclose( $stream ); |
|
141 |
|
|
142 |
if ( $returnbool ) |
|
143 |
return ( $data === false ) ? false : '' != trim($data); |
|
144 |
else |
|
145 |
return $data; |
|
146 |
} |
|
147 |
return false; |
|
148 |
} |
|
149 |
|
5
|
150 |
/** |
|
151 |
* @param string $file |
|
152 |
* @return string|false |
|
153 |
*/ |
|
154 |
public function get_contents( $file ) { |
0
|
155 |
$file = ltrim($file, '/'); |
|
156 |
return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
157 |
} |
|
158 |
|
5
|
159 |
/** |
|
160 |
* @param string $file |
|
161 |
* @return array |
|
162 |
*/ |
|
163 |
public function get_contents_array($file) { |
0
|
164 |
$file = ltrim($file, '/'); |
|
165 |
return file('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
166 |
} |
|
167 |
|
5
|
168 |
/** |
|
169 |
* @param string $file |
|
170 |
* @param string $contents |
|
171 |
* @param bool|int $mode |
|
172 |
* @return bool |
|
173 |
*/ |
|
174 |
public function put_contents($file, $contents, $mode = false ) { |
0
|
175 |
$ret = file_put_contents( 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $file, '/' ), $contents ); |
|
176 |
|
|
177 |
if ( $ret !== strlen( $contents ) ) |
|
178 |
return false; |
|
179 |
|
|
180 |
$this->chmod($file, $mode); |
|
181 |
|
|
182 |
return true; |
|
183 |
} |
|
184 |
|
5
|
185 |
public function cwd() { |
0
|
186 |
$cwd = $this->run_command('pwd'); |
5
|
187 |
if ( $cwd ) { |
|
188 |
$cwd = trailingslashit( trim( $cwd ) ); |
|
189 |
} |
0
|
190 |
return $cwd; |
|
191 |
} |
|
192 |
|
5
|
193 |
/** |
|
194 |
* @param string $dir |
|
195 |
* @return bool|string |
|
196 |
*/ |
|
197 |
public function chdir($dir) { |
0
|
198 |
return $this->run_command('cd ' . $dir, true); |
|
199 |
} |
|
200 |
|
5
|
201 |
/** |
|
202 |
* @param string $file |
|
203 |
* @param string $group |
|
204 |
* @param bool $recursive |
|
205 |
*/ |
|
206 |
public function chgrp($file, $group, $recursive = false ) { |
0
|
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 |
|
5
|
214 |
/** |
|
215 |
* @param string $file |
|
216 |
* @param int $mode |
|
217 |
* @param bool $recursive |
|
218 |
* @return bool|string |
|
219 |
*/ |
|
220 |
public function chmod($file, $mode = false, $recursive = false) { |
0
|
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 |
* |
5
|
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. |
0
|
247 |
*/ |
5
|
248 |
public function chown( $file, $owner, $recursive = false ) { |
0
|
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 |
|
5
|
256 |
/** |
|
257 |
* @param string $file |
|
258 |
* @return string|false |
|
259 |
*/ |
|
260 |
public function owner($file) { |
0
|
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 |
} |
5
|
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 ); |
0
|
275 |
} |
|
276 |
|
5
|
277 |
/** |
|
278 |
* @param string $file |
|
279 |
* @return string|false |
|
280 |
*/ |
|
281 |
public function group($file) { |
0
|
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 |
|
5
|
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) { |
0
|
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 |
|
5
|
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 ); |
0
|
315 |
} |
|
316 |
|
5
|
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) { |
0
|
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 |
|
5
|
337 |
/** |
|
338 |
* @param string $file |
|
339 |
* @return bool |
|
340 |
*/ |
|
341 |
public function exists($file) { |
0
|
342 |
$file = ltrim($file, '/'); |
|
343 |
return file_exists('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
344 |
} |
5
|
345 |
/** |
|
346 |
* @param string $file |
|
347 |
* @return bool |
|
348 |
*/ |
|
349 |
public function is_file($file) { |
0
|
350 |
$file = ltrim($file, '/'); |
|
351 |
return is_file('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
352 |
} |
5
|
353 |
/** |
|
354 |
* @param string $path |
|
355 |
* @return bool |
|
356 |
*/ |
|
357 |
public function is_dir($path) { |
0
|
358 |
$path = ltrim($path, '/'); |
|
359 |
return is_dir('ssh2.sftp://' . $this->sftp_link . '/' . $path); |
|
360 |
} |
5
|
361 |
/** |
|
362 |
* @param string $file |
|
363 |
* @return bool |
|
364 |
*/ |
|
365 |
public function is_readable($file) { |
0
|
366 |
$file = ltrim($file, '/'); |
|
367 |
return is_readable('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
368 |
} |
5
|
369 |
/** |
|
370 |
* @param string $file |
|
371 |
* @return bool |
|
372 |
*/ |
|
373 |
public function is_writable($file) { |
0
|
374 |
$file = ltrim($file, '/'); |
|
375 |
return is_writable('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
376 |
} |
5
|
377 |
/** |
|
378 |
* @param string $file |
|
379 |
* @return int |
|
380 |
*/ |
|
381 |
public function atime($file) { |
0
|
382 |
$file = ltrim($file, '/'); |
|
383 |
return fileatime('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
384 |
} |
|
385 |
|
5
|
386 |
/** |
|
387 |
* @param string $file |
|
388 |
* @return int |
|
389 |
*/ |
|
390 |
public function mtime($file) { |
0
|
391 |
$file = ltrim($file, '/'); |
|
392 |
return filemtime('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
393 |
} |
|
394 |
|
5
|
395 |
/** |
|
396 |
* @param string $file |
|
397 |
* @return int |
|
398 |
*/ |
|
399 |
public function size($file) { |
0
|
400 |
$file = ltrim($file, '/'); |
|
401 |
return filesize('ssh2.sftp://' . $this->sftp_link . '/' . $file); |
|
402 |
} |
|
403 |
|
5
|
404 |
/** |
|
405 |
* @param string $file |
|
406 |
* @param int $time |
|
407 |
* @param int $atime |
|
408 |
*/ |
|
409 |
public function touch($file, $time = 0, $atime = 0) { |
0
|
410 |
//Not implemented. |
|
411 |
} |
|
412 |
|
5
|
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) { |
0
|
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 |
|
5
|
436 |
/** |
|
437 |
* @param string $path |
|
438 |
* @param bool $recursive |
|
439 |
* @return bool |
|
440 |
*/ |
|
441 |
public function rmdir($path, $recursive = false) { |
0
|
442 |
return $this->delete($path, $recursive); |
|
443 |
} |
|
444 |
|
5
|
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) { |
0
|
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 |
} |