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