mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress SSH2 Filesystem.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Filesystem
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * WordPress Filesystem Class for implementing SSH2.
mp-wp_genesis 11 *
mp-wp_genesis 12 * To use this class you must follow these steps for PHP 5.2.6+
mp-wp_genesis 13 *
mp-wp_genesis 14 * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
mp-wp_genesis 15 *
mp-wp_genesis 16 * Complie libssh2 (Note: Only 0.14 is officaly working with PHP 5.2.6+ right now.)
mp-wp_genesis 17 *
mp-wp_genesis 18 * cd /usr/src
mp-wp_genesis 19 * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
mp-wp_genesis 20 * tar -zxvf libssh2-0.14.tar.gz
mp-wp_genesis 21 * cd libssh2-0.14/
mp-wp_genesis 22 * ./configure
mp-wp_genesis 23 * make all install
mp-wp_genesis 24 *
mp-wp_genesis 25 * Note: No not leave the directory yet!
mp-wp_genesis 26 *
mp-wp_genesis 27 * Enter: pecl install -f ssh2
mp-wp_genesis 28 *
mp-wp_genesis 29 * Copy the ssh.so file it creates to your PHP Module Directory.
mp-wp_genesis 30 * Open up your PHP.INI file and look for where extensions are placed.
mp-wp_genesis 31 * Add in your PHP.ini file: extension=ssh2.so
mp-wp_genesis 32 *
mp-wp_genesis 33 * Restart Apache!
mp-wp_genesis 34 * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp exist.
mp-wp_genesis 35 *
mp-wp_genesis 36 *
mp-wp_genesis 37 * @since 2.7
mp-wp_genesis 38 * @package WordPress
mp-wp_genesis 39 * @subpackage Filesystem
mp-wp_genesis 40 * @uses WP_Filesystem_Base Extends class
mp-wp_genesis 41 */
mp-wp_genesis 42 class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
mp-wp_genesis 43
mp-wp_genesis 44 var $debugtest = false; // set this to true only if your a debuging your connection
mp-wp_genesis 45
mp-wp_genesis 46 var $link = null;
mp-wp_genesis 47 var $sftp_link = null;
mp-wp_genesis 48 var $keys = false;
mp-wp_genesis 49 /*
mp-wp_genesis 50 * This is the timeout value for ssh results to comeback.
mp-wp_genesis 51 * Slower servers might need this incressed, but this number otherwise should not change.
mp-wp_genesis 52 *
mp-wp_genesis 53 * @parm $timeout int
mp-wp_genesis 54 *
mp-wp_genesis 55 */
mp-wp_genesis 56 var $timeout = 15;
mp-wp_genesis 57 var $errors = array();
mp-wp_genesis 58 var $options = array();
mp-wp_genesis 59
mp-wp_genesis 60 var $permission = 0644;
mp-wp_genesis 61
mp-wp_genesis 62 function WP_Filesystem_SSH2($opt='') {
mp-wp_genesis 63 $this->method = 'ssh2';
mp-wp_genesis 64 $this->errors = new WP_Error();
mp-wp_genesis 65
mp-wp_genesis 66 //Check if possible to use ssh2 functions.
mp-wp_genesis 67 if ( ! extension_loaded('ssh2') ) {
mp-wp_genesis 68 $this->errors->add('no_ssh2_ext', __('The ssh2 PHP extension is not available'));
mp-wp_genesis 69 return false;
mp-wp_genesis 70 }
mp-wp_genesis 71
mp-wp_genesis 72 // Set defaults:
mp-wp_genesis 73 if ( empty($opt['port']) )
mp-wp_genesis 74 $this->options['port'] = 22;
mp-wp_genesis 75 else
mp-wp_genesis 76 $this->options['port'] = $opt['port'];
mp-wp_genesis 77
mp-wp_genesis 78 if ( empty($opt['hostname']) )
mp-wp_genesis 79 $this->errors->add('empty_hostname', __('SSH2 hostname is required'));
mp-wp_genesis 80 else
mp-wp_genesis 81 $this->options['hostname'] = $opt['hostname'];
mp-wp_genesis 82
mp-wp_genesis 83 if ( isset($opt['base']) && ! empty($opt['base']) )
mp-wp_genesis 84 $this->wp_base = $opt['base'];
mp-wp_genesis 85
mp-wp_genesis 86 // Check if the options provided are OK.
mp-wp_genesis 87 if ( empty ($opt['username']) )
mp-wp_genesis 88 $this->errors->add('empty_username', __('SSH2 username is required'));
mp-wp_genesis 89 else
mp-wp_genesis 90 $this->options['username'] = $opt['username'];
mp-wp_genesis 91
mp-wp_genesis 92 if ( ( !empty ($opt['public_key']) ) && ( !empty ($opt['private_key']) ) ) {
mp-wp_genesis 93 $this->options['public_key'] = $opt['public_key'];
mp-wp_genesis 94 $this->options['private_key'] = $opt['private_key'];
mp-wp_genesis 95
mp-wp_genesis 96 $this->options['hostkey'] = array("hostkey" => "ssh-rsa");
mp-wp_genesis 97
mp-wp_genesis 98 $this->keys = true;
mp-wp_genesis 99 }
mp-wp_genesis 100
mp-wp_genesis 101
mp-wp_genesis 102 if ( empty ($opt['password']) ) {
mp-wp_genesis 103 if ( !$this->keys ) // password can be blank if we are using keys
mp-wp_genesis 104 $this->errors->add('empty_password', __('SSH2 password is required'));
mp-wp_genesis 105 } else {
mp-wp_genesis 106 $this->options['password'] = $opt['password'];
mp-wp_genesis 107 }
mp-wp_genesis 108
mp-wp_genesis 109 }
mp-wp_genesis 110
mp-wp_genesis 111 function connect() {
mp-wp_genesis 112 $this->debug("connect();");
mp-wp_genesis 113
mp-wp_genesis 114 if ( ! $this->keys ) {
mp-wp_genesis 115 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']);
mp-wp_genesis 116 } else {
mp-wp_genesis 117 $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']);
mp-wp_genesis 118 }
mp-wp_genesis 119
mp-wp_genesis 120 if ( ! $this->link ) {
mp-wp_genesis 121 $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port']));
mp-wp_genesis 122 return false;
mp-wp_genesis 123 }
mp-wp_genesis 124
mp-wp_genesis 125 if ( !$this->keys ) {
mp-wp_genesis 126 if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) {
mp-wp_genesis 127 $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username']));
mp-wp_genesis 128 return false;
mp-wp_genesis 129 }
mp-wp_genesis 130 } else {
mp-wp_genesis 131 if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) {
mp-wp_genesis 132 $this->errors->add('auth', sprintf(__('Public and Private keys incorrent for %s'), $this->options['username']));
mp-wp_genesis 133 return false;
mp-wp_genesis 134 }
mp-wp_genesis 135 }
mp-wp_genesis 136
mp-wp_genesis 137 $this->sftp_link = ssh2_sftp($this->link);
mp-wp_genesis 138
mp-wp_genesis 139 return true;
mp-wp_genesis 140 }
mp-wp_genesis 141
mp-wp_genesis 142 function run_command($link, $command, $returnbool = false) {
mp-wp_genesis 143 $this->debug("run_command();");
mp-wp_genesis 144 if(!($stream = @ssh2_exec( $link, $command . "; echo \"__COMMAND_FINISHED__\";"))) {
mp-wp_genesis 145 $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command));
mp-wp_genesis 146 } else {
mp-wp_genesis 147 stream_set_blocking( $stream, true );
mp-wp_genesis 148 $time_start = time();
mp-wp_genesis 149 $data = null;
mp-wp_genesis 150 while( true ) {
mp-wp_genesis 151 if (strpos($data,"__COMMAND_FINISHED__") !== false){
mp-wp_genesis 152 break; // the command has finshed!
mp-wp_genesis 153 }
mp-wp_genesis 154 if( (time()-$time_start) > $this->timeout ){
mp-wp_genesis 155 $this->errors->add('command', sprintf(__('Connection to the server has timeout after %s seconds.'), $this->timeout));
mp-wp_genesis 156 unset($this->link);
mp-wp_genesis 157 unset($this->sftp_link); // close connections
mp-wp_genesis 158 return false;
mp-wp_genesis 159 }
mp-wp_genesis 160 while( $buf = fread( $stream, strlen($stream) ) )
mp-wp_genesis 161 $data .= $buf;
mp-wp_genesis 162 }
mp-wp_genesis 163 fclose($stream);
mp-wp_genesis 164 $data = trim(str_replace("__COMMAND_FINISHED__", "", $data));
mp-wp_genesis 165 if (($returnbool) && ( (int) $data )) {
mp-wp_genesis 166 return true;
mp-wp_genesis 167 } elseif (($returnbool) && (! (int) $data )) {
mp-wp_genesis 168 return false;
mp-wp_genesis 169 } else {
mp-wp_genesis 170 return $data;
mp-wp_genesis 171 }
mp-wp_genesis 172 }
mp-wp_genesis 173 return false;
mp-wp_genesis 174 }
mp-wp_genesis 175
mp-wp_genesis 176 function debug($text)
mp-wp_genesis 177 {
mp-wp_genesis 178 if ($this->debugtest)
mp-wp_genesis 179 {
mp-wp_genesis 180 echo "<br/>" . $text . "<br/>";
mp-wp_genesis 181 }
mp-wp_genesis 182 }
mp-wp_genesis 183
mp-wp_genesis 184 function setDefaultPermissions($perm) {
mp-wp_genesis 185 $this->debug("setDefaultPermissions();");
mp-wp_genesis 186 if ( $perm )
mp-wp_genesis 187 $this->permission = $perm;
mp-wp_genesis 188 }
mp-wp_genesis 189
mp-wp_genesis 190 function get_contents($file, $type = '', $resumepos = 0 ) {
mp-wp_genesis 191 $this->debug("get_contents();");
mp-wp_genesis 192 $tempfile = wp_tempnam( $file );
mp-wp_genesis 193 if ( ! $tempfile )
mp-wp_genesis 194 return false;
mp-wp_genesis 195 if( ! ssh2_scp_recv($this->link, $file, $tempfile) )
mp-wp_genesis 196 return false;
mp-wp_genesis 197 $contents = file_get_contents($tempfile);
mp-wp_genesis 198 unlink($tempfile);
mp-wp_genesis 199 return $contents;
mp-wp_genesis 200 }
mp-wp_genesis 201
mp-wp_genesis 202 function get_contents_array($file) {
mp-wp_genesis 203 $this->debug("get_contents_array();");
mp-wp_genesis 204 return explode("\n", $this->get_contents($file));
mp-wp_genesis 205 }
mp-wp_genesis 206
mp-wp_genesis 207 function put_contents($file, $contents, $type = '' ) {
mp-wp_genesis 208 $this->debug("put_contents($file);");
mp-wp_genesis 209 $tempfile = wp_tempnam( $file );
mp-wp_genesis 210 $temp = fopen($tempfile, 'w');
mp-wp_genesis 211 if ( ! $temp )
mp-wp_genesis 212 return false;
mp-wp_genesis 213 fwrite($temp, $contents);
mp-wp_genesis 214 fclose($temp);
mp-wp_genesis 215 $ret = ssh2_scp_send($this->link, $tempfile, $file, $this->permission);
mp-wp_genesis 216 unlink($tempfile);
mp-wp_genesis 217 return $ret;
mp-wp_genesis 218 }
mp-wp_genesis 219
mp-wp_genesis 220 function cwd() {
mp-wp_genesis 221 $this->debug("cwd();");
mp-wp_genesis 222 $cwd = $this->run_command($this->link, 'pwd');
mp-wp_genesis 223 if( $cwd )
mp-wp_genesis 224 $cwd = trailingslashit($cwd);
mp-wp_genesis 225 return $cwd;
mp-wp_genesis 226 }
mp-wp_genesis 227
mp-wp_genesis 228 function chdir($dir) {
mp-wp_genesis 229 $this->debug("chdir();");
mp-wp_genesis 230 return $this->run_command($this->link, 'cd ' . $dir, true);
mp-wp_genesis 231 }
mp-wp_genesis 232
mp-wp_genesis 233 function chgrp($file, $group, $recursive = false ) {
mp-wp_genesis 234 $this->debug("chgrp();");
mp-wp_genesis 235 if ( ! $this->exists($file) )
mp-wp_genesis 236 return false;
mp-wp_genesis 237 if ( ! $recursive || ! $this->is_dir($file) )
mp-wp_genesis 238 return $this->run_command($this->link, sprintf('chgrp %o %s', $mode, $file), true);
mp-wp_genesis 239 return $this->run_command($this->link, sprintf('chgrp -R %o %s', $mode, $file), true);
mp-wp_genesis 240 }
mp-wp_genesis 241
mp-wp_genesis 242 function chmod($file, $mode = false, $recursive = false) {
mp-wp_genesis 243 $this->debug("chmod();");
mp-wp_genesis 244 if( ! $mode )
mp-wp_genesis 245 $mode = $this->permission;
mp-wp_genesis 246 if( ! $mode )
mp-wp_genesis 247 return false;
mp-wp_genesis 248 if ( ! $this->exists($file) )
mp-wp_genesis 249 return false;
mp-wp_genesis 250 if ( ! $recursive || ! $this->is_dir($file) )
mp-wp_genesis 251 return $this->run_command($this->link, sprintf('chmod %o %s', $mode, $file), true);
mp-wp_genesis 252 return $this->run_command($this->link, sprintf('chmod -R %o %s', $mode, $file), true);
mp-wp_genesis 253 }
mp-wp_genesis 254
mp-wp_genesis 255 function chown($file, $owner, $recursive = false ) {
mp-wp_genesis 256 $this->debug("chown();");
mp-wp_genesis 257 if ( ! $this->exists($file) )
mp-wp_genesis 258 return false;
mp-wp_genesis 259 if ( ! $recursive || ! $this->is_dir($file) )
mp-wp_genesis 260 return $this->run_command($this->link, sprintf('chown %o %s', $mode, $file), true);
mp-wp_genesis 261 return $this->run_command($this->link, sprintf('chown -R %o %s', $mode, $file), true);
mp-wp_genesis 262 }
mp-wp_genesis 263
mp-wp_genesis 264 function owner($file) {
mp-wp_genesis 265 $this->debug("owner();");
mp-wp_genesis 266 $dir = $this->dirlist($file);
mp-wp_genesis 267 return $dir[$file]['owner'];
mp-wp_genesis 268 }
mp-wp_genesis 269
mp-wp_genesis 270 function getchmod($file) {
mp-wp_genesis 271 $this->debug("getchmod();");
mp-wp_genesis 272 $dir = $this->dirlist($file);
mp-wp_genesis 273 return $dir[$file]['permsn'];
mp-wp_genesis 274 }
mp-wp_genesis 275
mp-wp_genesis 276 function group($file) {
mp-wp_genesis 277 $this->debug("group();");
mp-wp_genesis 278 $dir = $this->dirlist($file);
mp-wp_genesis 279 return $dir[$file]['group'];
mp-wp_genesis 280 }
mp-wp_genesis 281
mp-wp_genesis 282 function copy($source, $destination, $overwrite = false ) {
mp-wp_genesis 283 $this->debug("copy();");
mp-wp_genesis 284 if( ! $overwrite && $this->exists($destination) )
mp-wp_genesis 285 return false;
mp-wp_genesis 286 $content = $this->get_contents($source);
mp-wp_genesis 287 if( false === $content)
mp-wp_genesis 288 return false;
mp-wp_genesis 289 return $this->put_contents($destination, $content);
mp-wp_genesis 290 }
mp-wp_genesis 291
mp-wp_genesis 292 function move($source, $destination, $overwrite = false) {
mp-wp_genesis 293 $this->debug("move();");
mp-wp_genesis 294 return @ssh2_sftp_rename($this->link, $source, $destination);
mp-wp_genesis 295 }
mp-wp_genesis 296
mp-wp_genesis 297 function delete($file, $recursive = false) {
mp-wp_genesis 298 $this->debug("delete();");
mp-wp_genesis 299 if ( $this->is_file($file) )
mp-wp_genesis 300 return ssh2_sftp_unlink($this->sftp_link, $file);
mp-wp_genesis 301 if ( ! $recursive )
mp-wp_genesis 302 return ssh2_sftp_rmdir($this->sftp_link, $file);
mp-wp_genesis 303 $filelist = $this->dirlist($file);
mp-wp_genesis 304 if ( is_array($filelist) ) {
mp-wp_genesis 305 foreach ( $filelist as $filename => $fileinfo) {
mp-wp_genesis 306 $this->delete($file . '/' . $filename, $recursive);
mp-wp_genesis 307 }
mp-wp_genesis 308 }
mp-wp_genesis 309 return ssh2_sftp_rmdir($this->sftp_link, $file);
mp-wp_genesis 310 }
mp-wp_genesis 311
mp-wp_genesis 312 function exists($file) {
mp-wp_genesis 313 $this->debug("exists();");
mp-wp_genesis 314 return $this->run_command($this->link, sprintf('ls -lad %s', $file), true);
mp-wp_genesis 315 }
mp-wp_genesis 316
mp-wp_genesis 317 function is_file($file) {
mp-wp_genesis 318 $this->debug("is_file();");
mp-wp_genesis 319 //DO NOT RELY ON dirlist()!
mp-wp_genesis 320 $list = $this->run_command($this->link, sprintf('ls -lad %s', $file));
mp-wp_genesis 321 $list = $this->parselisting($list);
mp-wp_genesis 322 if ( ! $list )
mp-wp_genesis 323 return false;
mp-wp_genesis 324 else
mp-wp_genesis 325 return ( !$list['isdir'] && !$list['islink'] ); //ie. not a file or link, yet exists, must be file.
mp-wp_genesis 326 }
mp-wp_genesis 327
mp-wp_genesis 328 function is_dir($path) {
mp-wp_genesis 329 $this->debug("is_dir();");
mp-wp_genesis 330 //DO NOT RELY ON dirlist()!
mp-wp_genesis 331 $list = $this->parselisting($this->run_command($this->link, sprintf('ls -lad %s', untrailingslashit($path))));
mp-wp_genesis 332 if ( ! $list )
mp-wp_genesis 333 return false;
mp-wp_genesis 334 else
mp-wp_genesis 335 return $list['isdir'];
mp-wp_genesis 336 }
mp-wp_genesis 337
mp-wp_genesis 338 function is_readable($file) {
mp-wp_genesis 339 //Not implmented.
mp-wp_genesis 340 }
mp-wp_genesis 341
mp-wp_genesis 342 function is_writable($file) {
mp-wp_genesis 343 //Not implmented.
mp-wp_genesis 344 }
mp-wp_genesis 345
mp-wp_genesis 346 function atime($file) {
mp-wp_genesis 347 //Not implmented.
mp-wp_genesis 348 }
mp-wp_genesis 349
mp-wp_genesis 350 function mtime($file) {
mp-wp_genesis 351 //Not implmented.
mp-wp_genesis 352 }
mp-wp_genesis 353
mp-wp_genesis 354 function size($file) {
mp-wp_genesis 355 //Not implmented.
mp-wp_genesis 356 }
mp-wp_genesis 357
mp-wp_genesis 358 function touch($file, $time = 0, $atime = 0) {
mp-wp_genesis 359 //Not implmented.
mp-wp_genesis 360 }
mp-wp_genesis 361
mp-wp_genesis 362 function mkdir($path, $chmod = null, $chown = false, $chgrp = false) {
mp-wp_genesis 363 $this->debug("mkdir();");
mp-wp_genesis 364 $path = untrailingslashit($path);
mp-wp_genesis 365 if( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
mp-wp_genesis 366 return false;
mp-wp_genesis 367 if( $chown )
mp-wp_genesis 368 $this->chown($path, $chown);
mp-wp_genesis 369 if( $chgrp )
mp-wp_genesis 370 $this->chgrp($path, $chgrp);
mp-wp_genesis 371 return true;
mp-wp_genesis 372 }
mp-wp_genesis 373
mp-wp_genesis 374 function rmdir($path, $recursive = false) {
mp-wp_genesis 375 $this->debug("rmdir();");
mp-wp_genesis 376 return $this->delete($path, $recursive);
mp-wp_genesis 377 }
mp-wp_genesis 378
mp-wp_genesis 379 function parselisting($line) {
mp-wp_genesis 380 $this->debug("parselisting();");
mp-wp_genesis 381 $is_windows = ($this->OS_remote == FTP_OS_Windows);
mp-wp_genesis 382 if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/", $line, $lucifer)) {
mp-wp_genesis 383 $b = array();
mp-wp_genesis 384 if ($lucifer[3]<70) { $lucifer[3] +=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
mp-wp_genesis 385 $b['isdir'] = ($lucifer[7]=="<DIR>");
mp-wp_genesis 386 if ( $b['isdir'] )
mp-wp_genesis 387 $b['type'] = 'd';
mp-wp_genesis 388 else
mp-wp_genesis 389 $b['type'] = 'f';
mp-wp_genesis 390 $b['size'] = $lucifer[7];
mp-wp_genesis 391 $b['month'] = $lucifer[1];
mp-wp_genesis 392 $b['day'] = $lucifer[2];
mp-wp_genesis 393 $b['year'] = $lucifer[3];
mp-wp_genesis 394 $b['hour'] = $lucifer[4];
mp-wp_genesis 395 $b['minute'] = $lucifer[5];
mp-wp_genesis 396 $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
mp-wp_genesis 397 $b['am/pm'] = $lucifer[6];
mp-wp_genesis 398 $b['name'] = $lucifer[8];
mp-wp_genesis 399 } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
mp-wp_genesis 400 //echo $line."\n";
mp-wp_genesis 401 $lcount=count($lucifer);
mp-wp_genesis 402 if ($lcount<8) return '';
mp-wp_genesis 403 $b = array();
mp-wp_genesis 404 $b['isdir'] = $lucifer[0]{0} === "d";
mp-wp_genesis 405 $b['islink'] = $lucifer[0]{0} === "l";
mp-wp_genesis 406 if ( $b['isdir'] )
mp-wp_genesis 407 $b['type'] = 'd';
mp-wp_genesis 408 elseif ( $b['islink'] )
mp-wp_genesis 409 $b['type'] = 'l';
mp-wp_genesis 410 else
mp-wp_genesis 411 $b['type'] = 'f';
mp-wp_genesis 412 $b['perms'] = $lucifer[0];
mp-wp_genesis 413 $b['number'] = $lucifer[1];
mp-wp_genesis 414 $b['owner'] = $lucifer[2];
mp-wp_genesis 415 $b['group'] = $lucifer[3];
mp-wp_genesis 416 $b['size'] = $lucifer[4];
mp-wp_genesis 417 if ($lcount==8) {
mp-wp_genesis 418 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
mp-wp_genesis 419 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
mp-wp_genesis 420 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
mp-wp_genesis 421 $b['name'] = $lucifer[7];
mp-wp_genesis 422 } else {
mp-wp_genesis 423 $b['month'] = $lucifer[5];
mp-wp_genesis 424 $b['day'] = $lucifer[6];
mp-wp_genesis 425 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
mp-wp_genesis 426 $b['year'] = date("Y");
mp-wp_genesis 427 $b['hour'] = $l2[1];
mp-wp_genesis 428 $b['minute'] = $l2[2];
mp-wp_genesis 429 } else {
mp-wp_genesis 430 $b['year'] = $lucifer[7];
mp-wp_genesis 431 $b['hour'] = 0;
mp-wp_genesis 432 $b['minute'] = 0;
mp-wp_genesis 433 }
mp-wp_genesis 434 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
mp-wp_genesis 435 $b['name'] = $lucifer[8];
mp-wp_genesis 436 }
mp-wp_genesis 437 }
mp-wp_genesis 438
mp-wp_genesis 439 return $b;
mp-wp_genesis 440 }
mp-wp_genesis 441
mp-wp_genesis 442 function dirlist($path = '.', $incdot = false, $recursive = false) {
mp-wp_genesis 443 $this->debug("dirlist();");
mp-wp_genesis 444 if( $this->is_file($path) ) {
mp-wp_genesis 445 $limitFile = basename($path);
mp-wp_genesis 446 $path = trailingslashit(dirname($path));
mp-wp_genesis 447 } else {
mp-wp_genesis 448 $limitFile = false;
mp-wp_genesis 449 }
mp-wp_genesis 450
mp-wp_genesis 451 $list = $this->run_command($this->link, sprintf('ls -la %s', $path));
mp-wp_genesis 452
mp-wp_genesis 453 if ( $list === false )
mp-wp_genesis 454 return false;
mp-wp_genesis 455
mp-wp_genesis 456 $list = explode("\n", $list);
mp-wp_genesis 457
mp-wp_genesis 458 $dirlist = array();
mp-wp_genesis 459 foreach ( (array)$list as $k => $v ) {
mp-wp_genesis 460 $entry = $this->parselisting($v);
mp-wp_genesis 461 if ( empty($entry) )
mp-wp_genesis 462 continue;
mp-wp_genesis 463
mp-wp_genesis 464 if ( '.' == $entry['name'] || '..' == $entry['name'] )
mp-wp_genesis 465 continue;
mp-wp_genesis 466
mp-wp_genesis 467 $dirlist[ $entry['name'] ] = $entry;
mp-wp_genesis 468 }
mp-wp_genesis 469
mp-wp_genesis 470 if ( ! $dirlist )
mp-wp_genesis 471 return false;
mp-wp_genesis 472
mp-wp_genesis 473 if ( empty($dirlist) )
mp-wp_genesis 474 return array();
mp-wp_genesis 475
mp-wp_genesis 476 $ret = array();
mp-wp_genesis 477 foreach ( $dirlist as $struc ) {
mp-wp_genesis 478
mp-wp_genesis 479 if ( 'd' == $struc['type'] ) {
mp-wp_genesis 480 $struc['files'] = array();
mp-wp_genesis 481
mp-wp_genesis 482 if ( $incdot ){
mp-wp_genesis 483 //We're including the doted starts
mp-wp_genesis 484 if( '.' != $struc['name'] && '..' != $struc['name'] ){ //Ok, It isnt a special folder
mp-wp_genesis 485 if ($recursive)
mp-wp_genesis 486 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
mp-wp_genesis 487 }
mp-wp_genesis 488 } else { //No dots
mp-wp_genesis 489 if ( $recursive )
mp-wp_genesis 490 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
mp-wp_genesis 491 }
mp-wp_genesis 492 }
mp-wp_genesis 493 //File
mp-wp_genesis 494 $ret[$struc['name']] = $struc;
mp-wp_genesis 495 }
mp-wp_genesis 496 return $ret;
mp-wp_genesis 497 }
mp-wp_genesis 498 function __destruct() {
mp-wp_genesis 499 $this->debug("__destruct();");
mp-wp_genesis 500 if ( $this->link )
mp-wp_genesis 501 unset($this->link);
mp-wp_genesis 502 if ( $this->sftp_link )
mp-wp_genesis 503 unset($this->sftp_link);
mp-wp_genesis 504 }
mp-wp_genesis 505 }
mp-wp_genesis 506
mp-wp_genesis 507 ?>