mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * PemFTP - A Ftp implementation in pure PHP
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package PemFTP
mp-wp_genesis 6 * @since 2.5
mp-wp_genesis 7 *
mp-wp_genesis 8 * @version 1.0
mp-wp_genesis 9 * @copyright Alexey Dotsenko
mp-wp_genesis 10 * @author Alexey Dotsenko
mp-wp_genesis 11 * @link http://www.phpclasses.org/browse/package/1743.html Site
mp-wp_genesis 12 * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
mp-wp_genesis 13 */
mp-wp_genesis 14
mp-wp_genesis 15 /**
mp-wp_genesis 16 * Defines the newline characters, if not defined already.
mp-wp_genesis 17 *
mp-wp_genesis 18 * This can be redefined.
mp-wp_genesis 19 *
mp-wp_genesis 20 * @since 2.5
mp-wp_genesis 21 * @var string
mp-wp_genesis 22 */
mp-wp_genesis 23 if(!defined('CRLF')) define('CRLF',"\r\n");
mp-wp_genesis 24
mp-wp_genesis 25 /**
mp-wp_genesis 26 * Sets whatever to autodetect ASCII mode.
mp-wp_genesis 27 *
mp-wp_genesis 28 * This can be redefined.
mp-wp_genesis 29 *
mp-wp_genesis 30 * @since 2.5
mp-wp_genesis 31 * @var int
mp-wp_genesis 32 */
mp-wp_genesis 33 if(!defined("FTP_AUTOASCII")) define("FTP_AUTOASCII", -1);
mp-wp_genesis 34
mp-wp_genesis 35 /**
mp-wp_genesis 36 *
mp-wp_genesis 37 * This can be redefined.
mp-wp_genesis 38 * @since 2.5
mp-wp_genesis 39 * @var int
mp-wp_genesis 40 */
mp-wp_genesis 41 if(!defined("FTP_BINARY")) define("FTP_BINARY", 1);
mp-wp_genesis 42
mp-wp_genesis 43 /**
mp-wp_genesis 44 *
mp-wp_genesis 45 * This can be redefined.
mp-wp_genesis 46 * @since 2.5
mp-wp_genesis 47 * @var int
mp-wp_genesis 48 */
mp-wp_genesis 49 if(!defined("FTP_ASCII")) define("FTP_ASCII", 0);
mp-wp_genesis 50
mp-wp_genesis 51 /**
mp-wp_genesis 52 * Whether to force FTP.
mp-wp_genesis 53 *
mp-wp_genesis 54 * This can be redefined.
mp-wp_genesis 55 *
mp-wp_genesis 56 * @since 2.5
mp-wp_genesis 57 * @var bool
mp-wp_genesis 58 */
mp-wp_genesis 59 if(!defined('FTP_FORCE')) define('FTP_FORCE', true);
mp-wp_genesis 60
mp-wp_genesis 61 /**
mp-wp_genesis 62 * @since 2.5
mp-wp_genesis 63 * @var string
mp-wp_genesis 64 */
mp-wp_genesis 65 define('FTP_OS_Unix','u');
mp-wp_genesis 66
mp-wp_genesis 67 /**
mp-wp_genesis 68 * @since 2.5
mp-wp_genesis 69 * @var string
mp-wp_genesis 70 */
mp-wp_genesis 71 define('FTP_OS_Windows','w');
mp-wp_genesis 72
mp-wp_genesis 73 /**
mp-wp_genesis 74 * @since 2.5
mp-wp_genesis 75 * @var string
mp-wp_genesis 76 */
mp-wp_genesis 77 define('FTP_OS_Mac','m');
mp-wp_genesis 78
mp-wp_genesis 79 /**
mp-wp_genesis 80 * PemFTP base class
mp-wp_genesis 81 *
mp-wp_genesis 82 */
mp-wp_genesis 83 class ftp_base {
mp-wp_genesis 84 /* Public variables */
mp-wp_genesis 85 var $LocalEcho;
mp-wp_genesis 86 var $Verbose;
mp-wp_genesis 87 var $OS_local;
mp-wp_genesis 88 var $OS_remote;
mp-wp_genesis 89
mp-wp_genesis 90 /* Private variables */
mp-wp_genesis 91 var $_lastaction;
mp-wp_genesis 92 var $_errors;
mp-wp_genesis 93 var $_type;
mp-wp_genesis 94 var $_umask;
mp-wp_genesis 95 var $_timeout;
mp-wp_genesis 96 var $_passive;
mp-wp_genesis 97 var $_host;
mp-wp_genesis 98 var $_fullhost;
mp-wp_genesis 99 var $_port;
mp-wp_genesis 100 var $_datahost;
mp-wp_genesis 101 var $_dataport;
mp-wp_genesis 102 var $_ftp_control_sock;
mp-wp_genesis 103 var $_ftp_data_sock;
mp-wp_genesis 104 var $_ftp_temp_sock;
mp-wp_genesis 105 var $_ftp_buff_size;
mp-wp_genesis 106 var $_login;
mp-wp_genesis 107 var $_password;
mp-wp_genesis 108 var $_connected;
mp-wp_genesis 109 var $_ready;
mp-wp_genesis 110 var $_code;
mp-wp_genesis 111 var $_message;
mp-wp_genesis 112 var $_can_restore;
mp-wp_genesis 113 var $_port_available;
mp-wp_genesis 114 var $_curtype;
mp-wp_genesis 115 var $_features;
mp-wp_genesis 116
mp-wp_genesis 117 var $_error_array;
mp-wp_genesis 118 var $AuthorizedTransferMode;
mp-wp_genesis 119 var $OS_FullName;
mp-wp_genesis 120 var $_eol_code;
mp-wp_genesis 121 var $AutoAsciiExt;
mp-wp_genesis 122
mp-wp_genesis 123 /* Constructor */
mp-wp_genesis 124 function ftp_base($port_mode=FALSE) {
mp-wp_genesis 125 $this->__construct($port_mode);
mp-wp_genesis 126 }
mp-wp_genesis 127
mp-wp_genesis 128 function __construct($port_mode=FALSE, $verb=FALSE, $le=FALSE) {
mp-wp_genesis 129 $this->LocalEcho=$le;
mp-wp_genesis 130 $this->Verbose=$verb;
mp-wp_genesis 131 $this->_lastaction=NULL;
mp-wp_genesis 132 $this->_error_array=array();
mp-wp_genesis 133 $this->_eol_code=array(FTP_OS_Unix=>"\n", FTP_OS_Mac=>"\r", FTP_OS_Windows=>"\r\n");
mp-wp_genesis 134 $this->AuthorizedTransferMode=array(FTP_AUTOASCII, FTP_ASCII, FTP_BINARY);
mp-wp_genesis 135 $this->OS_FullName=array(FTP_OS_Unix => 'UNIX', FTP_OS_Windows => 'WINDOWS', FTP_OS_Mac => 'MACOS');
mp-wp_genesis 136 $this->AutoAsciiExt=array("ASP","BAT","C","CPP","CSS","CSV","JS","H","HTM","HTML","SHTML","INI","LOG","PHP3","PHTML","PL","PERL","SH","SQL","TXT");
mp-wp_genesis 137 $this->_port_available=($port_mode==TRUE);
mp-wp_genesis 138 $this->SendMSG("Staring FTP client class".($this->_port_available?"":" without PORT mode support"));
mp-wp_genesis 139 $this->_connected=FALSE;
mp-wp_genesis 140 $this->_ready=FALSE;
mp-wp_genesis 141 $this->_can_restore=FALSE;
mp-wp_genesis 142 $this->_code=0;
mp-wp_genesis 143 $this->_message="";
mp-wp_genesis 144 $this->_ftp_buff_size=4096;
mp-wp_genesis 145 $this->_curtype=NULL;
mp-wp_genesis 146 $this->SetUmask(0022);
mp-wp_genesis 147 $this->SetType(FTP_AUTOASCII);
mp-wp_genesis 148 $this->SetTimeout(30);
mp-wp_genesis 149 $this->Passive(!$this->_port_available);
mp-wp_genesis 150 $this->_login="anonymous";
mp-wp_genesis 151 $this->_password="anon@ftp.com";
mp-wp_genesis 152 $this->_features=array();
mp-wp_genesis 153 $this->OS_local=FTP_OS_Unix;
mp-wp_genesis 154 $this->OS_remote=FTP_OS_Unix;
mp-wp_genesis 155 $this->features=array();
mp-wp_genesis 156 if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $this->OS_local=FTP_OS_Windows;
mp-wp_genesis 157 elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'MAC') $this->OS_local=FTP_OS_Mac;
mp-wp_genesis 158 }
mp-wp_genesis 159
mp-wp_genesis 160 // <!-- --------------------------------------------------------------------------------------- -->
mp-wp_genesis 161 // <!-- Public functions -->
mp-wp_genesis 162 // <!-- --------------------------------------------------------------------------------------- -->
mp-wp_genesis 163
mp-wp_genesis 164 function parselisting($line) {
mp-wp_genesis 165 $is_windows = ($this->OS_remote == FTP_OS_Windows);
mp-wp_genesis 166 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 167 $b = array();
mp-wp_genesis 168 if ($lucifer[3]<70) { $lucifer[3]+=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
mp-wp_genesis 169 $b['isdir'] = ($lucifer[7]=="<DIR>");
mp-wp_genesis 170 if ( $b['isdir'] )
mp-wp_genesis 171 $b['type'] = 'd';
mp-wp_genesis 172 else
mp-wp_genesis 173 $b['type'] = 'f';
mp-wp_genesis 174 $b['size'] = $lucifer[7];
mp-wp_genesis 175 $b['month'] = $lucifer[1];
mp-wp_genesis 176 $b['day'] = $lucifer[2];
mp-wp_genesis 177 $b['year'] = $lucifer[3];
mp-wp_genesis 178 $b['hour'] = $lucifer[4];
mp-wp_genesis 179 $b['minute'] = $lucifer[5];
mp-wp_genesis 180 $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
mp-wp_genesis 181 $b['am/pm'] = $lucifer[6];
mp-wp_genesis 182 $b['name'] = $lucifer[8];
mp-wp_genesis 183 } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
mp-wp_genesis 184 //echo $line."\n";
mp-wp_genesis 185 $lcount=count($lucifer);
mp-wp_genesis 186 if ($lcount<8) return '';
mp-wp_genesis 187 $b = array();
mp-wp_genesis 188 $b['isdir'] = $lucifer[0]{0} === "d";
mp-wp_genesis 189 $b['islink'] = $lucifer[0]{0} === "l";
mp-wp_genesis 190 if ( $b['isdir'] )
mp-wp_genesis 191 $b['type'] = 'd';
mp-wp_genesis 192 elseif ( $b['islink'] )
mp-wp_genesis 193 $b['type'] = 'l';
mp-wp_genesis 194 else
mp-wp_genesis 195 $b['type'] = 'f';
mp-wp_genesis 196 $b['perms'] = $lucifer[0];
mp-wp_genesis 197 $b['number'] = $lucifer[1];
mp-wp_genesis 198 $b['owner'] = $lucifer[2];
mp-wp_genesis 199 $b['group'] = $lucifer[3];
mp-wp_genesis 200 $b['size'] = $lucifer[4];
mp-wp_genesis 201 if ($lcount==8) {
mp-wp_genesis 202 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
mp-wp_genesis 203 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
mp-wp_genesis 204 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
mp-wp_genesis 205 $b['name'] = $lucifer[7];
mp-wp_genesis 206 } else {
mp-wp_genesis 207 $b['month'] = $lucifer[5];
mp-wp_genesis 208 $b['day'] = $lucifer[6];
mp-wp_genesis 209 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
mp-wp_genesis 210 $b['year'] = date("Y");
mp-wp_genesis 211 $b['hour'] = $l2[1];
mp-wp_genesis 212 $b['minute'] = $l2[2];
mp-wp_genesis 213 } else {
mp-wp_genesis 214 $b['year'] = $lucifer[7];
mp-wp_genesis 215 $b['hour'] = 0;
mp-wp_genesis 216 $b['minute'] = 0;
mp-wp_genesis 217 }
mp-wp_genesis 218 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
mp-wp_genesis 219 $b['name'] = $lucifer[8];
mp-wp_genesis 220 }
mp-wp_genesis 221 }
mp-wp_genesis 222
mp-wp_genesis 223 return $b;
mp-wp_genesis 224 }
mp-wp_genesis 225
mp-wp_genesis 226 function SendMSG($message = "", $crlf=true) {
mp-wp_genesis 227 if ($this->Verbose) {
mp-wp_genesis 228 echo $message.($crlf?CRLF:"");
mp-wp_genesis 229 flush();
mp-wp_genesis 230 }
mp-wp_genesis 231 return TRUE;
mp-wp_genesis 232 }
mp-wp_genesis 233
mp-wp_genesis 234 function SetType($mode=FTP_AUTOASCII) {
mp-wp_genesis 235 if(!in_array($mode, $this->AuthorizedTransferMode)) {
mp-wp_genesis 236 $this->SendMSG("Wrong type");
mp-wp_genesis 237 return FALSE;
mp-wp_genesis 238 }
mp-wp_genesis 239 $this->_type=$mode;
mp-wp_genesis 240 $this->SendMSG("Transfer type: ".($this->_type==FTP_BINARY?"binary":($this->_type==FTP_ASCII?"ASCII":"auto ASCII") ) );
mp-wp_genesis 241 return TRUE;
mp-wp_genesis 242 }
mp-wp_genesis 243
mp-wp_genesis 244 function _settype($mode=FTP_ASCII) {
mp-wp_genesis 245 if($this->_ready) {
mp-wp_genesis 246 if($mode==FTP_BINARY) {
mp-wp_genesis 247 if($this->_curtype!=FTP_BINARY) {
mp-wp_genesis 248 if(!$this->_exec("TYPE I", "SetType")) return FALSE;
mp-wp_genesis 249 $this->_curtype=FTP_BINARY;
mp-wp_genesis 250 }
mp-wp_genesis 251 } elseif($this->_curtype!=FTP_ASCII) {
mp-wp_genesis 252 if(!$this->_exec("TYPE A", "SetType")) return FALSE;
mp-wp_genesis 253 $this->_curtype=FTP_ASCII;
mp-wp_genesis 254 }
mp-wp_genesis 255 } else return FALSE;
mp-wp_genesis 256 return TRUE;
mp-wp_genesis 257 }
mp-wp_genesis 258
mp-wp_genesis 259 function Passive($pasv=NULL) {
mp-wp_genesis 260 if(is_null($pasv)) $this->_passive=!$this->_passive;
mp-wp_genesis 261 else $this->_passive=$pasv;
mp-wp_genesis 262 if(!$this->_port_available and !$this->_passive) {
mp-wp_genesis 263 $this->SendMSG("Only passive connections available!");
mp-wp_genesis 264 $this->_passive=TRUE;
mp-wp_genesis 265 return FALSE;
mp-wp_genesis 266 }
mp-wp_genesis 267 $this->SendMSG("Passive mode ".($this->_passive?"on":"off"));
mp-wp_genesis 268 return TRUE;
mp-wp_genesis 269 }
mp-wp_genesis 270
mp-wp_genesis 271 function SetServer($host, $port=21, $reconnect=true) {
mp-wp_genesis 272 if(!is_long($port)) {
mp-wp_genesis 273 $this->verbose=true;
mp-wp_genesis 274 $this->SendMSG("Incorrect port syntax");
mp-wp_genesis 275 return FALSE;
mp-wp_genesis 276 } else {
mp-wp_genesis 277 $ip=@gethostbyname($host);
mp-wp_genesis 278 $dns=@gethostbyaddr($host);
mp-wp_genesis 279 if(!$ip) $ip=$host;
mp-wp_genesis 280 if(!$dns) $dns=$host;
mp-wp_genesis 281 if(ip2long($ip) === -1) {
mp-wp_genesis 282 $this->SendMSG("Wrong host name/address \"".$host."\"");
mp-wp_genesis 283 return FALSE;
mp-wp_genesis 284 }
mp-wp_genesis 285 $this->_host=$ip;
mp-wp_genesis 286 $this->_fullhost=$dns;
mp-wp_genesis 287 $this->_port=$port;
mp-wp_genesis 288 $this->_dataport=$port-1;
mp-wp_genesis 289 }
mp-wp_genesis 290 $this->SendMSG("Host \"".$this->_fullhost."(".$this->_host."):".$this->_port."\"");
mp-wp_genesis 291 if($reconnect){
mp-wp_genesis 292 if($this->_connected) {
mp-wp_genesis 293 $this->SendMSG("Reconnecting");
mp-wp_genesis 294 if(!$this->quit(FTP_FORCE)) return FALSE;
mp-wp_genesis 295 if(!$this->connect()) return FALSE;
mp-wp_genesis 296 }
mp-wp_genesis 297 }
mp-wp_genesis 298 return TRUE;
mp-wp_genesis 299 }
mp-wp_genesis 300
mp-wp_genesis 301 function SetUmask($umask=0022) {
mp-wp_genesis 302 $this->_umask=$umask;
mp-wp_genesis 303 umask($this->_umask);
mp-wp_genesis 304 $this->SendMSG("UMASK 0".decoct($this->_umask));
mp-wp_genesis 305 return TRUE;
mp-wp_genesis 306 }
mp-wp_genesis 307
mp-wp_genesis 308 function SetTimeout($timeout=30) {
mp-wp_genesis 309 $this->_timeout=$timeout;
mp-wp_genesis 310 $this->SendMSG("Timeout ".$this->_timeout);
mp-wp_genesis 311 if($this->_connected)
mp-wp_genesis 312 if(!$this->_settimeout($this->_ftp_control_sock)) return FALSE;
mp-wp_genesis 313 return TRUE;
mp-wp_genesis 314 }
mp-wp_genesis 315
mp-wp_genesis 316 function connect($server=NULL) {
mp-wp_genesis 317 if(!empty($server)) {
mp-wp_genesis 318 if(!$this->SetServer($server)) return false;
mp-wp_genesis 319 }
mp-wp_genesis 320 if($this->_ready) return true;
mp-wp_genesis 321 $this->SendMsg('Local OS : '.$this->OS_FullName[$this->OS_local]);
mp-wp_genesis 322 if(!($this->_ftp_control_sock = $this->_connect($this->_host, $this->_port))) {
mp-wp_genesis 323 $this->SendMSG("Error : Cannot connect to remote host \"".$this->_fullhost." :".$this->_port."\"");
mp-wp_genesis 324 return FALSE;
mp-wp_genesis 325 }
mp-wp_genesis 326 $this->SendMSG("Connected to remote host \"".$this->_fullhost.":".$this->_port."\". Waiting for greeting.");
mp-wp_genesis 327 do {
mp-wp_genesis 328 if(!$this->_readmsg()) return FALSE;
mp-wp_genesis 329 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 330 $this->_lastaction=time();
mp-wp_genesis 331 } while($this->_code<200);
mp-wp_genesis 332 $this->_ready=true;
mp-wp_genesis 333 $syst=$this->systype();
mp-wp_genesis 334 if(!$syst) $this->SendMSG("Can't detect remote OS");
mp-wp_genesis 335 else {
mp-wp_genesis 336 if(preg_match("/win|dos|novell/i", $syst[0])) $this->OS_remote=FTP_OS_Windows;
mp-wp_genesis 337 elseif(preg_match("/os/i", $syst[0])) $this->OS_remote=FTP_OS_Mac;
mp-wp_genesis 338 elseif(preg_match("/(li|u)nix/i", $syst[0])) $this->OS_remote=FTP_OS_Unix;
mp-wp_genesis 339 else $this->OS_remote=FTP_OS_Mac;
mp-wp_genesis 340 $this->SendMSG("Remote OS: ".$this->OS_FullName[$this->OS_remote]);
mp-wp_genesis 341 }
mp-wp_genesis 342 if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
mp-wp_genesis 343 else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
mp-wp_genesis 344 return TRUE;
mp-wp_genesis 345 }
mp-wp_genesis 346
mp-wp_genesis 347 function quit($force=false) {
mp-wp_genesis 348 if($this->_ready) {
mp-wp_genesis 349 if(!$this->_exec("QUIT") and !$force) return FALSE;
mp-wp_genesis 350 if(!$this->_checkCode() and !$force) return FALSE;
mp-wp_genesis 351 $this->_ready=false;
mp-wp_genesis 352 $this->SendMSG("Session finished");
mp-wp_genesis 353 }
mp-wp_genesis 354 $this->_quit();
mp-wp_genesis 355 return TRUE;
mp-wp_genesis 356 }
mp-wp_genesis 357
mp-wp_genesis 358 function login($user=NULL, $pass=NULL) {
mp-wp_genesis 359 if(!is_null($user)) $this->_login=$user;
mp-wp_genesis 360 else $this->_login="anonymous";
mp-wp_genesis 361 if(!is_null($pass)) $this->_password=$pass;
mp-wp_genesis 362 else $this->_password="anon@anon.com";
mp-wp_genesis 363 if(!$this->_exec("USER ".$this->_login, "login")) return FALSE;
mp-wp_genesis 364 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 365 if($this->_code!=230) {
mp-wp_genesis 366 if(!$this->_exec((($this->_code==331)?"PASS ":"ACCT ").$this->_password, "login")) return FALSE;
mp-wp_genesis 367 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 368 }
mp-wp_genesis 369 $this->SendMSG("Authentication succeeded");
mp-wp_genesis 370 if(empty($this->_features)) {
mp-wp_genesis 371 if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
mp-wp_genesis 372 else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
mp-wp_genesis 373 }
mp-wp_genesis 374 return TRUE;
mp-wp_genesis 375 }
mp-wp_genesis 376
mp-wp_genesis 377 function pwd() {
mp-wp_genesis 378 if(!$this->_exec("PWD", "pwd")) return FALSE;
mp-wp_genesis 379 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 380 return ereg_replace("^[0-9]{3} \"(.+)\".+", "\\1", $this->_message);
mp-wp_genesis 381 }
mp-wp_genesis 382
mp-wp_genesis 383 function cdup() {
mp-wp_genesis 384 if(!$this->_exec("CDUP", "cdup")) return FALSE;
mp-wp_genesis 385 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 386 return true;
mp-wp_genesis 387 }
mp-wp_genesis 388
mp-wp_genesis 389 function chdir($pathname) {
mp-wp_genesis 390 if(!$this->_exec("CWD ".$pathname, "chdir")) return FALSE;
mp-wp_genesis 391 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 392 return TRUE;
mp-wp_genesis 393 }
mp-wp_genesis 394
mp-wp_genesis 395 function rmdir($pathname) {
mp-wp_genesis 396 if(!$this->_exec("RMD ".$pathname, "rmdir")) return FALSE;
mp-wp_genesis 397 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 398 return TRUE;
mp-wp_genesis 399 }
mp-wp_genesis 400
mp-wp_genesis 401 function mkdir($pathname) {
mp-wp_genesis 402 if(!$this->_exec("MKD ".$pathname, "mkdir")) return FALSE;
mp-wp_genesis 403 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 404 return TRUE;
mp-wp_genesis 405 }
mp-wp_genesis 406
mp-wp_genesis 407 function rename($from, $to) {
mp-wp_genesis 408 if(!$this->_exec("RNFR ".$from, "rename")) return FALSE;
mp-wp_genesis 409 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 410 if($this->_code==350) {
mp-wp_genesis 411 if(!$this->_exec("RNTO ".$to, "rename")) return FALSE;
mp-wp_genesis 412 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 413 } else return FALSE;
mp-wp_genesis 414 return TRUE;
mp-wp_genesis 415 }
mp-wp_genesis 416
mp-wp_genesis 417 function filesize($pathname) {
mp-wp_genesis 418 if(!isset($this->_features["SIZE"])) {
mp-wp_genesis 419 $this->PushError("filesize", "not supported by server");
mp-wp_genesis 420 return FALSE;
mp-wp_genesis 421 }
mp-wp_genesis 422 if(!$this->_exec("SIZE ".$pathname, "filesize")) return FALSE;
mp-wp_genesis 423 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 424 return ereg_replace("^[0-9]{3} ([0-9]+)".CRLF, "\\1", $this->_message);
mp-wp_genesis 425 }
mp-wp_genesis 426
mp-wp_genesis 427 function abort() {
mp-wp_genesis 428 if(!$this->_exec("ABOR", "abort")) return FALSE;
mp-wp_genesis 429 if(!$this->_checkCode()) {
mp-wp_genesis 430 if($this->_code!=426) return FALSE;
mp-wp_genesis 431 if(!$this->_readmsg("abort")) return FALSE;
mp-wp_genesis 432 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 433 }
mp-wp_genesis 434 return true;
mp-wp_genesis 435 }
mp-wp_genesis 436
mp-wp_genesis 437 function mdtm($pathname) {
mp-wp_genesis 438 if(!isset($this->_features["MDTM"])) {
mp-wp_genesis 439 $this->PushError("mdtm", "not supported by server");
mp-wp_genesis 440 return FALSE;
mp-wp_genesis 441 }
mp-wp_genesis 442 if(!$this->_exec("MDTM ".$pathname, "mdtm")) return FALSE;
mp-wp_genesis 443 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 444 $mdtm = ereg_replace("^[0-9]{3} ([0-9]+)".CRLF, "\\1", $this->_message);
mp-wp_genesis 445 $date = sscanf($mdtm, "%4d%2d%2d%2d%2d%2d");
mp-wp_genesis 446 $timestamp = mktime($date[3], $date[4], $date[5], $date[1], $date[2], $date[0]);
mp-wp_genesis 447 return $timestamp;
mp-wp_genesis 448 }
mp-wp_genesis 449
mp-wp_genesis 450 function systype() {
mp-wp_genesis 451 if(!$this->_exec("SYST", "systype")) return FALSE;
mp-wp_genesis 452 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 453 $DATA = explode(" ", $this->_message);
mp-wp_genesis 454 return array($DATA[1], $DATA[3]);
mp-wp_genesis 455 }
mp-wp_genesis 456
mp-wp_genesis 457 function delete($pathname) {
mp-wp_genesis 458 if(!$this->_exec("DELE ".$pathname, "delete")) return FALSE;
mp-wp_genesis 459 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 460 return TRUE;
mp-wp_genesis 461 }
mp-wp_genesis 462
mp-wp_genesis 463 function site($command, $fnction="site") {
mp-wp_genesis 464 if(!$this->_exec("SITE ".$command, $fnction)) return FALSE;
mp-wp_genesis 465 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 466 return TRUE;
mp-wp_genesis 467 }
mp-wp_genesis 468
mp-wp_genesis 469 function chmod($pathname, $mode) {
mp-wp_genesis 470 if(!$this->site( sprintf('CHMOD %o %s', $mode, $pathname), "chmod")) return FALSE;
mp-wp_genesis 471 return TRUE;
mp-wp_genesis 472 }
mp-wp_genesis 473
mp-wp_genesis 474 function restore($from) {
mp-wp_genesis 475 if(!isset($this->_features["REST"])) {
mp-wp_genesis 476 $this->PushError("restore", "not supported by server");
mp-wp_genesis 477 return FALSE;
mp-wp_genesis 478 }
mp-wp_genesis 479 if($this->_curtype!=FTP_BINARY) {
mp-wp_genesis 480 $this->PushError("restore", "can't restore in ASCII mode");
mp-wp_genesis 481 return FALSE;
mp-wp_genesis 482 }
mp-wp_genesis 483 if(!$this->_exec("REST ".$from, "resore")) return FALSE;
mp-wp_genesis 484 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 485 return TRUE;
mp-wp_genesis 486 }
mp-wp_genesis 487
mp-wp_genesis 488 function features() {
mp-wp_genesis 489 if(!$this->_exec("FEAT", "features")) return FALSE;
mp-wp_genesis 490 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 491 $f=preg_split("/[".CRLF."]+/", preg_replace("/[0-9]{3}[ -].*[".CRLF."]+/", "", $this->_message), -1, PREG_SPLIT_NO_EMPTY);
mp-wp_genesis 492 $this->_features=array();
mp-wp_genesis 493 foreach($f as $k=>$v) {
mp-wp_genesis 494 $v=explode(" ", trim($v));
mp-wp_genesis 495 $this->_features[array_shift($v)]=$v;;
mp-wp_genesis 496 }
mp-wp_genesis 497 return true;
mp-wp_genesis 498 }
mp-wp_genesis 499
mp-wp_genesis 500 function rawlist($pathname="", $arg="") {
mp-wp_genesis 501 return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "LIST", "rawlist");
mp-wp_genesis 502 }
mp-wp_genesis 503
mp-wp_genesis 504 function nlist($pathname="") {
mp-wp_genesis 505 return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "NLST", "nlist");
mp-wp_genesis 506 }
mp-wp_genesis 507
mp-wp_genesis 508 function is_exists($pathname) {
mp-wp_genesis 509 return $this->file_exists($pathname);
mp-wp_genesis 510 }
mp-wp_genesis 511
mp-wp_genesis 512 function file_exists($pathname) {
mp-wp_genesis 513 $exists=true;
mp-wp_genesis 514 if(!$this->_exec("RNFR ".$pathname, "rename")) $exists=FALSE;
mp-wp_genesis 515 else {
mp-wp_genesis 516 if(!$this->_checkCode()) $exists=FALSE;
mp-wp_genesis 517 $this->abort();
mp-wp_genesis 518 }
mp-wp_genesis 519 if($exists) $this->SendMSG("Remote file ".$pathname." exists");
mp-wp_genesis 520 else $this->SendMSG("Remote file ".$pathname." does not exist");
mp-wp_genesis 521 return $exists;
mp-wp_genesis 522 }
mp-wp_genesis 523
mp-wp_genesis 524 function fget($fp, $remotefile,$rest=0) {
mp-wp_genesis 525 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
mp-wp_genesis 526 $pi=pathinfo($remotefile);
mp-wp_genesis 527 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
mp-wp_genesis 528 else $mode=FTP_BINARY;
mp-wp_genesis 529 if(!$this->_data_prepare($mode)) {
mp-wp_genesis 530 return FALSE;
mp-wp_genesis 531 }
mp-wp_genesis 532 if($this->_can_restore and $rest!=0) $this->restore($rest);
mp-wp_genesis 533 if(!$this->_exec("RETR ".$remotefile, "get")) {
mp-wp_genesis 534 $this->_data_close();
mp-wp_genesis 535 return FALSE;
mp-wp_genesis 536 }
mp-wp_genesis 537 if(!$this->_checkCode()) {
mp-wp_genesis 538 $this->_data_close();
mp-wp_genesis 539 return FALSE;
mp-wp_genesis 540 }
mp-wp_genesis 541 $out=$this->_data_read($mode, $fp);
mp-wp_genesis 542 $this->_data_close();
mp-wp_genesis 543 if(!$this->_readmsg()) return FALSE;
mp-wp_genesis 544 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 545 return $out;
mp-wp_genesis 546 }
mp-wp_genesis 547
mp-wp_genesis 548 function get($remotefile, $localfile=NULL, $rest=0) {
mp-wp_genesis 549 if(is_null($localfile)) $localfile=$remotefile;
mp-wp_genesis 550 if (@file_exists($localfile)) $this->SendMSG("Warning : local file will be overwritten");
mp-wp_genesis 551 $fp = @fopen($localfile, "w");
mp-wp_genesis 552 if (!$fp) {
mp-wp_genesis 553 $this->PushError("get","can't open local file", "Cannot create \"".$localfile."\"");
mp-wp_genesis 554 return FALSE;
mp-wp_genesis 555 }
mp-wp_genesis 556 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
mp-wp_genesis 557 $pi=pathinfo($remotefile);
mp-wp_genesis 558 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
mp-wp_genesis 559 else $mode=FTP_BINARY;
mp-wp_genesis 560 if(!$this->_data_prepare($mode)) {
mp-wp_genesis 561 fclose($fp);
mp-wp_genesis 562 return FALSE;
mp-wp_genesis 563 }
mp-wp_genesis 564 if($this->_can_restore and $rest!=0) $this->restore($rest);
mp-wp_genesis 565 if(!$this->_exec("RETR ".$remotefile, "get")) {
mp-wp_genesis 566 $this->_data_close();
mp-wp_genesis 567 fclose($fp);
mp-wp_genesis 568 return FALSE;
mp-wp_genesis 569 }
mp-wp_genesis 570 if(!$this->_checkCode()) {
mp-wp_genesis 571 $this->_data_close();
mp-wp_genesis 572 fclose($fp);
mp-wp_genesis 573 return FALSE;
mp-wp_genesis 574 }
mp-wp_genesis 575 $out=$this->_data_read($mode, $fp);
mp-wp_genesis 576 fclose($fp);
mp-wp_genesis 577 $this->_data_close();
mp-wp_genesis 578 if(!$this->_readmsg()) return FALSE;
mp-wp_genesis 579 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 580 return $out;
mp-wp_genesis 581 }
mp-wp_genesis 582
mp-wp_genesis 583 function fput($remotefile, $fp) {
mp-wp_genesis 584 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
mp-wp_genesis 585 $pi=pathinfo($remotefile);
mp-wp_genesis 586 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
mp-wp_genesis 587 else $mode=FTP_BINARY;
mp-wp_genesis 588 if(!$this->_data_prepare($mode)) {
mp-wp_genesis 589 return FALSE;
mp-wp_genesis 590 }
mp-wp_genesis 591 if($this->_can_restore and $rest!=0) $this->restore($rest);
mp-wp_genesis 592 if(!$this->_exec("STOR ".$remotefile, "put")) {
mp-wp_genesis 593 $this->_data_close();
mp-wp_genesis 594 return FALSE;
mp-wp_genesis 595 }
mp-wp_genesis 596 if(!$this->_checkCode()) {
mp-wp_genesis 597 $this->_data_close();
mp-wp_genesis 598 return FALSE;
mp-wp_genesis 599 }
mp-wp_genesis 600 $ret=$this->_data_write($mode, $fp);
mp-wp_genesis 601 $this->_data_close();
mp-wp_genesis 602 if(!$this->_readmsg()) return FALSE;
mp-wp_genesis 603 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 604 return $ret;
mp-wp_genesis 605 }
mp-wp_genesis 606
mp-wp_genesis 607 function put($localfile, $remotefile=NULL, $rest=0) {
mp-wp_genesis 608 if(is_null($remotefile)) $remotefile=$localfile;
mp-wp_genesis 609 if (!file_exists($localfile)) {
mp-wp_genesis 610 $this->PushError("put","can't open local file", "No such file or directory \"".$localfile."\"");
mp-wp_genesis 611 return FALSE;
mp-wp_genesis 612 }
mp-wp_genesis 613 $fp = @fopen($localfile, "r");
mp-wp_genesis 614
mp-wp_genesis 615 if (!$fp) {
mp-wp_genesis 616 $this->PushError("put","can't open local file", "Cannot read file \"".$localfile."\"");
mp-wp_genesis 617 return FALSE;
mp-wp_genesis 618 }
mp-wp_genesis 619 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
mp-wp_genesis 620 $pi=pathinfo($localfile);
mp-wp_genesis 621 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
mp-wp_genesis 622 else $mode=FTP_BINARY;
mp-wp_genesis 623 if(!$this->_data_prepare($mode)) {
mp-wp_genesis 624 fclose($fp);
mp-wp_genesis 625 return FALSE;
mp-wp_genesis 626 }
mp-wp_genesis 627 if($this->_can_restore and $rest!=0) $this->restore($rest);
mp-wp_genesis 628 if(!$this->_exec("STOR ".$remotefile, "put")) {
mp-wp_genesis 629 $this->_data_close();
mp-wp_genesis 630 fclose($fp);
mp-wp_genesis 631 return FALSE;
mp-wp_genesis 632 }
mp-wp_genesis 633 if(!$this->_checkCode()) {
mp-wp_genesis 634 $this->_data_close();
mp-wp_genesis 635 fclose($fp);
mp-wp_genesis 636 return FALSE;
mp-wp_genesis 637 }
mp-wp_genesis 638 $ret=$this->_data_write($mode, $fp);
mp-wp_genesis 639 fclose($fp);
mp-wp_genesis 640 $this->_data_close();
mp-wp_genesis 641 if(!$this->_readmsg()) return FALSE;
mp-wp_genesis 642 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 643 return $ret;
mp-wp_genesis 644 }
mp-wp_genesis 645
mp-wp_genesis 646 function mput($local=".", $remote=NULL, $continious=false) {
mp-wp_genesis 647 $local=realpath($local);
mp-wp_genesis 648 if(!@file_exists($local)) {
mp-wp_genesis 649 $this->PushError("mput","can't open local folder", "Cannot stat folder \"".$local."\"");
mp-wp_genesis 650 return FALSE;
mp-wp_genesis 651 }
mp-wp_genesis 652 if(!is_dir($local)) return $this->put($local, $remote);
mp-wp_genesis 653 if(empty($remote)) $remote=".";
mp-wp_genesis 654 elseif(!$this->file_exists($remote) and !$this->mkdir($remote)) return FALSE;
mp-wp_genesis 655 if($handle = opendir($local)) {
mp-wp_genesis 656 $list=array();
mp-wp_genesis 657 while (false !== ($file = readdir($handle))) {
mp-wp_genesis 658 if ($file != "." && $file != "..") $list[]=$file;
mp-wp_genesis 659 }
mp-wp_genesis 660 closedir($handle);
mp-wp_genesis 661 } else {
mp-wp_genesis 662 $this->PushError("mput","can't open local folder", "Cannot read folder \"".$local."\"");
mp-wp_genesis 663 return FALSE;
mp-wp_genesis 664 }
mp-wp_genesis 665 if(empty($list)) return TRUE;
mp-wp_genesis 666 $ret=true;
mp-wp_genesis 667 foreach($list as $el) {
mp-wp_genesis 668 if(is_dir($local."/".$el)) $t=$this->mput($local."/".$el, $remote."/".$el);
mp-wp_genesis 669 else $t=$this->put($local."/".$el, $remote."/".$el);
mp-wp_genesis 670 if(!$t) {
mp-wp_genesis 671 $ret=FALSE;
mp-wp_genesis 672 if(!$continious) break;
mp-wp_genesis 673 }
mp-wp_genesis 674 }
mp-wp_genesis 675 return $ret;
mp-wp_genesis 676
mp-wp_genesis 677 }
mp-wp_genesis 678
mp-wp_genesis 679 function mget($remote, $local=".", $continious=false) {
mp-wp_genesis 680 $list=$this->rawlist($remote, "-lA");
mp-wp_genesis 681 if($list===false) {
mp-wp_genesis 682 $this->PushError("mget","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
mp-wp_genesis 683 return FALSE;
mp-wp_genesis 684 }
mp-wp_genesis 685 if(empty($list)) return true;
mp-wp_genesis 686 if(!@file_exists($local)) {
mp-wp_genesis 687 if(!@mkdir($local)) {
mp-wp_genesis 688 $this->PushError("mget","can't create local folder", "Cannot create folder \"".$local."\"");
mp-wp_genesis 689 return FALSE;
mp-wp_genesis 690 }
mp-wp_genesis 691 }
mp-wp_genesis 692 foreach($list as $k=>$v) {
mp-wp_genesis 693 $list[$k]=$this->parselisting($v);
mp-wp_genesis 694 if($list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
mp-wp_genesis 695 }
mp-wp_genesis 696 $ret=true;
mp-wp_genesis 697 foreach($list as $el) {
mp-wp_genesis 698 if($el["type"]=="d") {
mp-wp_genesis 699 if(!$this->mget($remote."/".$el["name"], $local."/".$el["name"], $continious)) {
mp-wp_genesis 700 $this->PushError("mget", "can't copy folder", "Can't copy remote folder \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
mp-wp_genesis 701 $ret=false;
mp-wp_genesis 702 if(!$continious) break;
mp-wp_genesis 703 }
mp-wp_genesis 704 } else {
mp-wp_genesis 705 if(!$this->get($remote."/".$el["name"], $local."/".$el["name"])) {
mp-wp_genesis 706 $this->PushError("mget", "can't copy file", "Can't copy remote file \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
mp-wp_genesis 707 $ret=false;
mp-wp_genesis 708 if(!$continious) break;
mp-wp_genesis 709 }
mp-wp_genesis 710 }
mp-wp_genesis 711 @chmod($local."/".$el["name"], $el["perms"]);
mp-wp_genesis 712 $t=strtotime($el["date"]);
mp-wp_genesis 713 if($t!==-1 and $t!==false) @touch($local."/".$el["name"], $t);
mp-wp_genesis 714 }
mp-wp_genesis 715 return $ret;
mp-wp_genesis 716 }
mp-wp_genesis 717
mp-wp_genesis 718 function mdel($remote, $continious=false) {
mp-wp_genesis 719 $list=$this->rawlist($remote, "-la");
mp-wp_genesis 720 if($list===false) {
mp-wp_genesis 721 $this->PushError("mdel","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
mp-wp_genesis 722 return false;
mp-wp_genesis 723 }
mp-wp_genesis 724
mp-wp_genesis 725 foreach($list as $k=>$v) {
mp-wp_genesis 726 $list[$k]=$this->parselisting($v);
mp-wp_genesis 727 if($list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
mp-wp_genesis 728 }
mp-wp_genesis 729 $ret=true;
mp-wp_genesis 730
mp-wp_genesis 731 foreach($list as $el) {
mp-wp_genesis 732 if ( empty($el) )
mp-wp_genesis 733 continue;
mp-wp_genesis 734
mp-wp_genesis 735 if($el["type"]=="d") {
mp-wp_genesis 736 if(!$this->mdel($remote."/".$el["name"], $continious)) {
mp-wp_genesis 737 $ret=false;
mp-wp_genesis 738 if(!$continious) break;
mp-wp_genesis 739 }
mp-wp_genesis 740 } else {
mp-wp_genesis 741 if (!$this->delete($remote."/".$el["name"])) {
mp-wp_genesis 742 $this->PushError("mdel", "can't delete file", "Can't delete remote file \"".$remote."/".$el["name"]."\"");
mp-wp_genesis 743 $ret=false;
mp-wp_genesis 744 if(!$continious) break;
mp-wp_genesis 745 }
mp-wp_genesis 746 }
mp-wp_genesis 747 }
mp-wp_genesis 748
mp-wp_genesis 749 if(!$this->rmdir($remote)) {
mp-wp_genesis 750 $this->PushError("mdel", "can't delete folder", "Can't delete remote folder \"".$remote."/".$el["name"]."\"");
mp-wp_genesis 751 $ret=false;
mp-wp_genesis 752 }
mp-wp_genesis 753 return $ret;
mp-wp_genesis 754 }
mp-wp_genesis 755
mp-wp_genesis 756 function mmkdir($dir, $mode = 0777) {
mp-wp_genesis 757 if(empty($dir)) return FALSE;
mp-wp_genesis 758 if($this->is_exists($dir) or $dir == "/" ) return TRUE;
mp-wp_genesis 759 if(!$this->mmkdir(dirname($dir), $mode)) return false;
mp-wp_genesis 760 $r=$this->mkdir($dir, $mode);
mp-wp_genesis 761 $this->chmod($dir,$mode);
mp-wp_genesis 762 return $r;
mp-wp_genesis 763 }
mp-wp_genesis 764
mp-wp_genesis 765 function glob($pattern, $handle=NULL) {
mp-wp_genesis 766 $path=$output=null;
mp-wp_genesis 767 if(PHP_OS=='WIN32') $slash='\\';
mp-wp_genesis 768 else $slash='/';
mp-wp_genesis 769 $lastpos=strrpos($pattern,$slash);
mp-wp_genesis 770 if(!($lastpos===false)) {
mp-wp_genesis 771 $path=substr($pattern,0,-$lastpos-1);
mp-wp_genesis 772 $pattern=substr($pattern,$lastpos);
mp-wp_genesis 773 } else $path=getcwd();
mp-wp_genesis 774 if(is_array($handle) and !empty($handle)) {
mp-wp_genesis 775 while($dir=each($handle)) {
mp-wp_genesis 776 if($this->glob_pattern_match($pattern,$dir))
mp-wp_genesis 777 $output[]=$dir;
mp-wp_genesis 778 }
mp-wp_genesis 779 } else {
mp-wp_genesis 780 $handle=@opendir($path);
mp-wp_genesis 781 if($handle===false) return false;
mp-wp_genesis 782 while($dir=readdir($handle)) {
mp-wp_genesis 783 if($this->glob_pattern_match($pattern,$dir))
mp-wp_genesis 784 $output[]=$dir;
mp-wp_genesis 785 }
mp-wp_genesis 786 closedir($handle);
mp-wp_genesis 787 }
mp-wp_genesis 788 if(is_array($output)) return $output;
mp-wp_genesis 789 return false;
mp-wp_genesis 790 }
mp-wp_genesis 791
mp-wp_genesis 792 function glob_pattern_match($pattern,$string) {
mp-wp_genesis 793 $out=null;
mp-wp_genesis 794 $chunks=explode(';',$pattern);
mp-wp_genesis 795 foreach($chunks as $pattern) {
mp-wp_genesis 796 $escape=array('$','^','.','{','}','(',')','[',']','|');
mp-wp_genesis 797 while(strpos($pattern,'**')!==false)
mp-wp_genesis 798 $pattern=str_replace('**','*',$pattern);
mp-wp_genesis 799 foreach($escape as $probe)
mp-wp_genesis 800 $pattern=str_replace($probe,"\\$probe",$pattern);
mp-wp_genesis 801 $pattern=str_replace('?*','*',
mp-wp_genesis 802 str_replace('*?','*',
mp-wp_genesis 803 str_replace('*',".*",
mp-wp_genesis 804 str_replace('?','.{1,1}',$pattern))));
mp-wp_genesis 805 $out[]=$pattern;
mp-wp_genesis 806 }
mp-wp_genesis 807 if(count($out)==1) return($this->glob_regexp("^$out[0]$",$string));
mp-wp_genesis 808 else {
mp-wp_genesis 809 foreach($out as $tester)
mp-wp_genesis 810 if($this->my_regexp("^$tester$",$string)) return true;
mp-wp_genesis 811 }
mp-wp_genesis 812 return false;
mp-wp_genesis 813 }
mp-wp_genesis 814
mp-wp_genesis 815 function glob_regexp($pattern,$probe) {
mp-wp_genesis 816 $sensitive=(PHP_OS!='WIN32');
mp-wp_genesis 817 return ($sensitive?
mp-wp_genesis 818 ereg($pattern,$probe):
mp-wp_genesis 819 eregi($pattern,$probe)
mp-wp_genesis 820 );
mp-wp_genesis 821 }
mp-wp_genesis 822
mp-wp_genesis 823 function dirlist($remote) {
mp-wp_genesis 824 $list=$this->rawlist($remote, "-la");
mp-wp_genesis 825 if($list===false) {
mp-wp_genesis 826 $this->PushError("dirlist","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
mp-wp_genesis 827 return false;
mp-wp_genesis 828 }
mp-wp_genesis 829
mp-wp_genesis 830 $dirlist = array();
mp-wp_genesis 831 foreach($list as $k=>$v) {
mp-wp_genesis 832 $entry=$this->parselisting($v);
mp-wp_genesis 833 if ( empty($entry) )
mp-wp_genesis 834 continue;
mp-wp_genesis 835
mp-wp_genesis 836 if($entry["name"]=="." or $entry["name"]=="..")
mp-wp_genesis 837 continue;
mp-wp_genesis 838
mp-wp_genesis 839 $dirlist[$entry['name']] = $entry;
mp-wp_genesis 840 }
mp-wp_genesis 841
mp-wp_genesis 842 return $dirlist;
mp-wp_genesis 843 }
mp-wp_genesis 844 // <!-- --------------------------------------------------------------------------------------- -->
mp-wp_genesis 845 // <!-- Private functions -->
mp-wp_genesis 846 // <!-- --------------------------------------------------------------------------------------- -->
mp-wp_genesis 847 function _checkCode() {
mp-wp_genesis 848 return ($this->_code<400 and $this->_code>0);
mp-wp_genesis 849 }
mp-wp_genesis 850
mp-wp_genesis 851 function _list($arg="", $cmd="LIST", $fnction="_list") {
mp-wp_genesis 852 if(!$this->_data_prepare()) return false;
mp-wp_genesis 853 if(!$this->_exec($cmd.$arg, $fnction)) {
mp-wp_genesis 854 $this->_data_close();
mp-wp_genesis 855 return FALSE;
mp-wp_genesis 856 }
mp-wp_genesis 857 if(!$this->_checkCode()) {
mp-wp_genesis 858 $this->_data_close();
mp-wp_genesis 859 return FALSE;
mp-wp_genesis 860 }
mp-wp_genesis 861 $out="";
mp-wp_genesis 862 if($this->_code<200) {
mp-wp_genesis 863 $out=$this->_data_read();
mp-wp_genesis 864 $this->_data_close();
mp-wp_genesis 865 if(!$this->_readmsg()) return FALSE;
mp-wp_genesis 866 if(!$this->_checkCode()) return FALSE;
mp-wp_genesis 867 if($out === FALSE ) return FALSE;
mp-wp_genesis 868 $out=preg_split("/[".CRLF."]+/", $out, -1, PREG_SPLIT_NO_EMPTY);
mp-wp_genesis 869 // $this->SendMSG(implode($this->_eol_code[$this->OS_local], $out));
mp-wp_genesis 870 }
mp-wp_genesis 871 return $out;
mp-wp_genesis 872 }
mp-wp_genesis 873
mp-wp_genesis 874 // <!-- --------------------------------------------------------------------------------------- -->
mp-wp_genesis 875 // <!-- Partie : gestion des erreurs -->
mp-wp_genesis 876 // <!-- --------------------------------------------------------------------------------------- -->
mp-wp_genesis 877 // Gnre une erreur pour traitement externe la classe
mp-wp_genesis 878 function PushError($fctname,$msg,$desc=false){
mp-wp_genesis 879 $error=array();
mp-wp_genesis 880 $error['time']=time();
mp-wp_genesis 881 $error['fctname']=$fctname;
mp-wp_genesis 882 $error['msg']=$msg;
mp-wp_genesis 883 $error['desc']=$desc;
mp-wp_genesis 884 if($desc) $tmp=' ('.$desc.')'; else $tmp='';
mp-wp_genesis 885 $this->SendMSG($fctname.': '.$msg.$tmp);
mp-wp_genesis 886 return(array_push($this->_error_array,$error));
mp-wp_genesis 887 }
mp-wp_genesis 888
mp-wp_genesis 889 // Rcupre une erreur externe
mp-wp_genesis 890 function PopError(){
mp-wp_genesis 891 if(count($this->_error_array)) return(array_pop($this->_error_array));
mp-wp_genesis 892 else return(false);
mp-wp_genesis 893 }
mp-wp_genesis 894 }
mp-wp_genesis 895
mp-wp_genesis 896 $mod_sockets=TRUE;
mp-wp_genesis 897 if (!extension_loaded('sockets')) {
mp-wp_genesis 898 $prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
mp-wp_genesis 899 if(!@dl($prefix . 'sockets.' . PHP_SHLIB_SUFFIX)) $mod_sockets=FALSE;
mp-wp_genesis 900 }
mp-wp_genesis 901
mp-wp_genesis 902 require_once "class-ftp-".($mod_sockets?"sockets":"pure").".php";
mp-wp_genesis 903 ?>