-
+ 8185D683B8A515BF775BF085FC12821072FD3378A6168C8F652AA59595EDD0B9632716741009A1AF0E120F6F4EB027C8D9A9A1576D6C5CA0BF322328B26668D9
mp-wp/wp-admin/includes/class-ftp.php
(0 . 0)(1 . 903)
23498 <?php
23499 /**
23500 * PemFTP - A Ftp implementation in pure PHP
23501 *
23502 * @package PemFTP
23503 * @since 2.5
23504 *
23505 * @version 1.0
23506 * @copyright Alexey Dotsenko
23507 * @author Alexey Dotsenko
23508 * @link http://www.phpclasses.org/browse/package/1743.html Site
23509 * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
23510 */
23511
23512 /**
23513 * Defines the newline characters, if not defined already.
23514 *
23515 * This can be redefined.
23516 *
23517 * @since 2.5
23518 * @var string
23519 */
23520 if(!defined('CRLF')) define('CRLF',"\r\n");
23521
23522 /**
23523 * Sets whatever to autodetect ASCII mode.
23524 *
23525 * This can be redefined.
23526 *
23527 * @since 2.5
23528 * @var int
23529 */
23530 if(!defined("FTP_AUTOASCII")) define("FTP_AUTOASCII", -1);
23531
23532 /**
23533 *
23534 * This can be redefined.
23535 * @since 2.5
23536 * @var int
23537 */
23538 if(!defined("FTP_BINARY")) define("FTP_BINARY", 1);
23539
23540 /**
23541 *
23542 * This can be redefined.
23543 * @since 2.5
23544 * @var int
23545 */
23546 if(!defined("FTP_ASCII")) define("FTP_ASCII", 0);
23547
23548 /**
23549 * Whether to force FTP.
23550 *
23551 * This can be redefined.
23552 *
23553 * @since 2.5
23554 * @var bool
23555 */
23556 if(!defined('FTP_FORCE')) define('FTP_FORCE', true);
23557
23558 /**
23559 * @since 2.5
23560 * @var string
23561 */
23562 define('FTP_OS_Unix','u');
23563
23564 /**
23565 * @since 2.5
23566 * @var string
23567 */
23568 define('FTP_OS_Windows','w');
23569
23570 /**
23571 * @since 2.5
23572 * @var string
23573 */
23574 define('FTP_OS_Mac','m');
23575
23576 /**
23577 * PemFTP base class
23578 *
23579 */
23580 class ftp_base {
23581 /* Public variables */
23582 var $LocalEcho;
23583 var $Verbose;
23584 var $OS_local;
23585 var $OS_remote;
23586
23587 /* Private variables */
23588 var $_lastaction;
23589 var $_errors;
23590 var $_type;
23591 var $_umask;
23592 var $_timeout;
23593 var $_passive;
23594 var $_host;
23595 var $_fullhost;
23596 var $_port;
23597 var $_datahost;
23598 var $_dataport;
23599 var $_ftp_control_sock;
23600 var $_ftp_data_sock;
23601 var $_ftp_temp_sock;
23602 var $_ftp_buff_size;
23603 var $_login;
23604 var $_password;
23605 var $_connected;
23606 var $_ready;
23607 var $_code;
23608 var $_message;
23609 var $_can_restore;
23610 var $_port_available;
23611 var $_curtype;
23612 var $_features;
23613
23614 var $_error_array;
23615 var $AuthorizedTransferMode;
23616 var $OS_FullName;
23617 var $_eol_code;
23618 var $AutoAsciiExt;
23619
23620 /* Constructor */
23621 function ftp_base($port_mode=FALSE) {
23622 $this->__construct($port_mode);
23623 }
23624
23625 function __construct($port_mode=FALSE, $verb=FALSE, $le=FALSE) {
23626 $this->LocalEcho=$le;
23627 $this->Verbose=$verb;
23628 $this->_lastaction=NULL;
23629 $this->_error_array=array();
23630 $this->_eol_code=array(FTP_OS_Unix=>"\n", FTP_OS_Mac=>"\r", FTP_OS_Windows=>"\r\n");
23631 $this->AuthorizedTransferMode=array(FTP_AUTOASCII, FTP_ASCII, FTP_BINARY);
23632 $this->OS_FullName=array(FTP_OS_Unix => 'UNIX', FTP_OS_Windows => 'WINDOWS', FTP_OS_Mac => 'MACOS');
23633 $this->AutoAsciiExt=array("ASP","BAT","C","CPP","CSS","CSV","JS","H","HTM","HTML","SHTML","INI","LOG","PHP3","PHTML","PL","PERL","SH","SQL","TXT");
23634 $this->_port_available=($port_mode==TRUE);
23635 $this->SendMSG("Staring FTP client class".($this->_port_available?"":" without PORT mode support"));
23636 $this->_connected=FALSE;
23637 $this->_ready=FALSE;
23638 $this->_can_restore=FALSE;
23639 $this->_code=0;
23640 $this->_message="";
23641 $this->_ftp_buff_size=4096;
23642 $this->_curtype=NULL;
23643 $this->SetUmask(0022);
23644 $this->SetType(FTP_AUTOASCII);
23645 $this->SetTimeout(30);
23646 $this->Passive(!$this->_port_available);
23647 $this->_login="anonymous";
23648 $this->_password="anon@ftp.com";
23649 $this->_features=array();
23650 $this->OS_local=FTP_OS_Unix;
23651 $this->OS_remote=FTP_OS_Unix;
23652 $this->features=array();
23653 if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $this->OS_local=FTP_OS_Windows;
23654 elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'MAC') $this->OS_local=FTP_OS_Mac;
23655 }
23656
23657 // <!-- --------------------------------------------------------------------------------------- -->
23658 // <!-- Public functions -->
23659 // <!-- --------------------------------------------------------------------------------------- -->
23660
23661 function parselisting($line) {
23662 $is_windows = ($this->OS_remote == FTP_OS_Windows);
23663 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)) {
23664 $b = array();
23665 if ($lucifer[3]<70) { $lucifer[3]+=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
23666 $b['isdir'] = ($lucifer[7]=="<DIR>");
23667 if ( $b['isdir'] )
23668 $b['type'] = 'd';
23669 else
23670 $b['type'] = 'f';
23671 $b['size'] = $lucifer[7];
23672 $b['month'] = $lucifer[1];
23673 $b['day'] = $lucifer[2];
23674 $b['year'] = $lucifer[3];
23675 $b['hour'] = $lucifer[4];
23676 $b['minute'] = $lucifer[5];
23677 $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
23678 $b['am/pm'] = $lucifer[6];
23679 $b['name'] = $lucifer[8];
23680 } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
23681 //echo $line."\n";
23682 $lcount=count($lucifer);
23683 if ($lcount<8) return '';
23684 $b = array();
23685 $b['isdir'] = $lucifer[0]{0} === "d";
23686 $b['islink'] = $lucifer[0]{0} === "l";
23687 if ( $b['isdir'] )
23688 $b['type'] = 'd';
23689 elseif ( $b['islink'] )
23690 $b['type'] = 'l';
23691 else
23692 $b['type'] = 'f';
23693 $b['perms'] = $lucifer[0];
23694 $b['number'] = $lucifer[1];
23695 $b['owner'] = $lucifer[2];
23696 $b['group'] = $lucifer[3];
23697 $b['size'] = $lucifer[4];
23698 if ($lcount==8) {
23699 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
23700 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
23701 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
23702 $b['name'] = $lucifer[7];
23703 } else {
23704 $b['month'] = $lucifer[5];
23705 $b['day'] = $lucifer[6];
23706 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
23707 $b['year'] = date("Y");
23708 $b['hour'] = $l2[1];
23709 $b['minute'] = $l2[2];
23710 } else {
23711 $b['year'] = $lucifer[7];
23712 $b['hour'] = 0;
23713 $b['minute'] = 0;
23714 }
23715 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
23716 $b['name'] = $lucifer[8];
23717 }
23718 }
23719
23720 return $b;
23721 }
23722
23723 function SendMSG($message = "", $crlf=true) {
23724 if ($this->Verbose) {
23725 echo $message.($crlf?CRLF:"");
23726 flush();
23727 }
23728 return TRUE;
23729 }
23730
23731 function SetType($mode=FTP_AUTOASCII) {
23732 if(!in_array($mode, $this->AuthorizedTransferMode)) {
23733 $this->SendMSG("Wrong type");
23734 return FALSE;
23735 }
23736 $this->_type=$mode;
23737 $this->SendMSG("Transfer type: ".($this->_type==FTP_BINARY?"binary":($this->_type==FTP_ASCII?"ASCII":"auto ASCII") ) );
23738 return TRUE;
23739 }
23740
23741 function _settype($mode=FTP_ASCII) {
23742 if($this->_ready) {
23743 if($mode==FTP_BINARY) {
23744 if($this->_curtype!=FTP_BINARY) {
23745 if(!$this->_exec("TYPE I", "SetType")) return FALSE;
23746 $this->_curtype=FTP_BINARY;
23747 }
23748 } elseif($this->_curtype!=FTP_ASCII) {
23749 if(!$this->_exec("TYPE A", "SetType")) return FALSE;
23750 $this->_curtype=FTP_ASCII;
23751 }
23752 } else return FALSE;
23753 return TRUE;
23754 }
23755
23756 function Passive($pasv=NULL) {
23757 if(is_null($pasv)) $this->_passive=!$this->_passive;
23758 else $this->_passive=$pasv;
23759 if(!$this->_port_available and !$this->_passive) {
23760 $this->SendMSG("Only passive connections available!");
23761 $this->_passive=TRUE;
23762 return FALSE;
23763 }
23764 $this->SendMSG("Passive mode ".($this->_passive?"on":"off"));
23765 return TRUE;
23766 }
23767
23768 function SetServer($host, $port=21, $reconnect=true) {
23769 if(!is_long($port)) {
23770 $this->verbose=true;
23771 $this->SendMSG("Incorrect port syntax");
23772 return FALSE;
23773 } else {
23774 $ip=@gethostbyname($host);
23775 $dns=@gethostbyaddr($host);
23776 if(!$ip) $ip=$host;
23777 if(!$dns) $dns=$host;
23778 if(ip2long($ip) === -1) {
23779 $this->SendMSG("Wrong host name/address \"".$host."\"");
23780 return FALSE;
23781 }
23782 $this->_host=$ip;
23783 $this->_fullhost=$dns;
23784 $this->_port=$port;
23785 $this->_dataport=$port-1;
23786 }
23787 $this->SendMSG("Host \"".$this->_fullhost."(".$this->_host."):".$this->_port."\"");
23788 if($reconnect){
23789 if($this->_connected) {
23790 $this->SendMSG("Reconnecting");
23791 if(!$this->quit(FTP_FORCE)) return FALSE;
23792 if(!$this->connect()) return FALSE;
23793 }
23794 }
23795 return TRUE;
23796 }
23797
23798 function SetUmask($umask=0022) {
23799 $this->_umask=$umask;
23800 umask($this->_umask);
23801 $this->SendMSG("UMASK 0".decoct($this->_umask));
23802 return TRUE;
23803 }
23804
23805 function SetTimeout($timeout=30) {
23806 $this->_timeout=$timeout;
23807 $this->SendMSG("Timeout ".$this->_timeout);
23808 if($this->_connected)
23809 if(!$this->_settimeout($this->_ftp_control_sock)) return FALSE;
23810 return TRUE;
23811 }
23812
23813 function connect($server=NULL) {
23814 if(!empty($server)) {
23815 if(!$this->SetServer($server)) return false;
23816 }
23817 if($this->_ready) return true;
23818 $this->SendMsg('Local OS : '.$this->OS_FullName[$this->OS_local]);
23819 if(!($this->_ftp_control_sock = $this->_connect($this->_host, $this->_port))) {
23820 $this->SendMSG("Error : Cannot connect to remote host \"".$this->_fullhost." :".$this->_port."\"");
23821 return FALSE;
23822 }
23823 $this->SendMSG("Connected to remote host \"".$this->_fullhost.":".$this->_port."\". Waiting for greeting.");
23824 do {
23825 if(!$this->_readmsg()) return FALSE;
23826 if(!$this->_checkCode()) return FALSE;
23827 $this->_lastaction=time();
23828 } while($this->_code<200);
23829 $this->_ready=true;
23830 $syst=$this->systype();
23831 if(!$syst) $this->SendMSG("Can't detect remote OS");
23832 else {
23833 if(preg_match("/win|dos|novell/i", $syst[0])) $this->OS_remote=FTP_OS_Windows;
23834 elseif(preg_match("/os/i", $syst[0])) $this->OS_remote=FTP_OS_Mac;
23835 elseif(preg_match("/(li|u)nix/i", $syst[0])) $this->OS_remote=FTP_OS_Unix;
23836 else $this->OS_remote=FTP_OS_Mac;
23837 $this->SendMSG("Remote OS: ".$this->OS_FullName[$this->OS_remote]);
23838 }
23839 if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
23840 else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
23841 return TRUE;
23842 }
23843
23844 function quit($force=false) {
23845 if($this->_ready) {
23846 if(!$this->_exec("QUIT") and !$force) return FALSE;
23847 if(!$this->_checkCode() and !$force) return FALSE;
23848 $this->_ready=false;
23849 $this->SendMSG("Session finished");
23850 }
23851 $this->_quit();
23852 return TRUE;
23853 }
23854
23855 function login($user=NULL, $pass=NULL) {
23856 if(!is_null($user)) $this->_login=$user;
23857 else $this->_login="anonymous";
23858 if(!is_null($pass)) $this->_password=$pass;
23859 else $this->_password="anon@anon.com";
23860 if(!$this->_exec("USER ".$this->_login, "login")) return FALSE;
23861 if(!$this->_checkCode()) return FALSE;
23862 if($this->_code!=230) {
23863 if(!$this->_exec((($this->_code==331)?"PASS ":"ACCT ").$this->_password, "login")) return FALSE;
23864 if(!$this->_checkCode()) return FALSE;
23865 }
23866 $this->SendMSG("Authentication succeeded");
23867 if(empty($this->_features)) {
23868 if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
23869 else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
23870 }
23871 return TRUE;
23872 }
23873
23874 function pwd() {
23875 if(!$this->_exec("PWD", "pwd")) return FALSE;
23876 if(!$this->_checkCode()) return FALSE;
23877 return ereg_replace("^[0-9]{3} \"(.+)\".+", "\\1", $this->_message);
23878 }
23879
23880 function cdup() {
23881 if(!$this->_exec("CDUP", "cdup")) return FALSE;
23882 if(!$this->_checkCode()) return FALSE;
23883 return true;
23884 }
23885
23886 function chdir($pathname) {
23887 if(!$this->_exec("CWD ".$pathname, "chdir")) return FALSE;
23888 if(!$this->_checkCode()) return FALSE;
23889 return TRUE;
23890 }
23891
23892 function rmdir($pathname) {
23893 if(!$this->_exec("RMD ".$pathname, "rmdir")) return FALSE;
23894 if(!$this->_checkCode()) return FALSE;
23895 return TRUE;
23896 }
23897
23898 function mkdir($pathname) {
23899 if(!$this->_exec("MKD ".$pathname, "mkdir")) return FALSE;
23900 if(!$this->_checkCode()) return FALSE;
23901 return TRUE;
23902 }
23903
23904 function rename($from, $to) {
23905 if(!$this->_exec("RNFR ".$from, "rename")) return FALSE;
23906 if(!$this->_checkCode()) return FALSE;
23907 if($this->_code==350) {
23908 if(!$this->_exec("RNTO ".$to, "rename")) return FALSE;
23909 if(!$this->_checkCode()) return FALSE;
23910 } else return FALSE;
23911 return TRUE;
23912 }
23913
23914 function filesize($pathname) {
23915 if(!isset($this->_features["SIZE"])) {
23916 $this->PushError("filesize", "not supported by server");
23917 return FALSE;
23918 }
23919 if(!$this->_exec("SIZE ".$pathname, "filesize")) return FALSE;
23920 if(!$this->_checkCode()) return FALSE;
23921 return ereg_replace("^[0-9]{3} ([0-9]+)".CRLF, "\\1", $this->_message);
23922 }
23923
23924 function abort() {
23925 if(!$this->_exec("ABOR", "abort")) return FALSE;
23926 if(!$this->_checkCode()) {
23927 if($this->_code!=426) return FALSE;
23928 if(!$this->_readmsg("abort")) return FALSE;
23929 if(!$this->_checkCode()) return FALSE;
23930 }
23931 return true;
23932 }
23933
23934 function mdtm($pathname) {
23935 if(!isset($this->_features["MDTM"])) {
23936 $this->PushError("mdtm", "not supported by server");
23937 return FALSE;
23938 }
23939 if(!$this->_exec("MDTM ".$pathname, "mdtm")) return FALSE;
23940 if(!$this->_checkCode()) return FALSE;
23941 $mdtm = ereg_replace("^[0-9]{3} ([0-9]+)".CRLF, "\\1", $this->_message);
23942 $date = sscanf($mdtm, "%4d%2d%2d%2d%2d%2d");
23943 $timestamp = mktime($date[3], $date[4], $date[5], $date[1], $date[2], $date[0]);
23944 return $timestamp;
23945 }
23946
23947 function systype() {
23948 if(!$this->_exec("SYST", "systype")) return FALSE;
23949 if(!$this->_checkCode()) return FALSE;
23950 $DATA = explode(" ", $this->_message);
23951 return array($DATA[1], $DATA[3]);
23952 }
23953
23954 function delete($pathname) {
23955 if(!$this->_exec("DELE ".$pathname, "delete")) return FALSE;
23956 if(!$this->_checkCode()) return FALSE;
23957 return TRUE;
23958 }
23959
23960 function site($command, $fnction="site") {
23961 if(!$this->_exec("SITE ".$command, $fnction)) return FALSE;
23962 if(!$this->_checkCode()) return FALSE;
23963 return TRUE;
23964 }
23965
23966 function chmod($pathname, $mode) {
23967 if(!$this->site( sprintf('CHMOD %o %s', $mode, $pathname), "chmod")) return FALSE;
23968 return TRUE;
23969 }
23970
23971 function restore($from) {
23972 if(!isset($this->_features["REST"])) {
23973 $this->PushError("restore", "not supported by server");
23974 return FALSE;
23975 }
23976 if($this->_curtype!=FTP_BINARY) {
23977 $this->PushError("restore", "can't restore in ASCII mode");
23978 return FALSE;
23979 }
23980 if(!$this->_exec("REST ".$from, "resore")) return FALSE;
23981 if(!$this->_checkCode()) return FALSE;
23982 return TRUE;
23983 }
23984
23985 function features() {
23986 if(!$this->_exec("FEAT", "features")) return FALSE;
23987 if(!$this->_checkCode()) return FALSE;
23988 $f=preg_split("/[".CRLF."]+/", preg_replace("/[0-9]{3}[ -].*[".CRLF."]+/", "", $this->_message), -1, PREG_SPLIT_NO_EMPTY);
23989 $this->_features=array();
23990 foreach($f as $k=>$v) {
23991 $v=explode(" ", trim($v));
23992 $this->_features[array_shift($v)]=$v;;
23993 }
23994 return true;
23995 }
23996
23997 function rawlist($pathname="", $arg="") {
23998 return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "LIST", "rawlist");
23999 }
24000
24001 function nlist($pathname="") {
24002 return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "NLST", "nlist");
24003 }
24004
24005 function is_exists($pathname) {
24006 return $this->file_exists($pathname);
24007 }
24008
24009 function file_exists($pathname) {
24010 $exists=true;
24011 if(!$this->_exec("RNFR ".$pathname, "rename")) $exists=FALSE;
24012 else {
24013 if(!$this->_checkCode()) $exists=FALSE;
24014 $this->abort();
24015 }
24016 if($exists) $this->SendMSG("Remote file ".$pathname." exists");
24017 else $this->SendMSG("Remote file ".$pathname." does not exist");
24018 return $exists;
24019 }
24020
24021 function fget($fp, $remotefile,$rest=0) {
24022 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
24023 $pi=pathinfo($remotefile);
24024 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
24025 else $mode=FTP_BINARY;
24026 if(!$this->_data_prepare($mode)) {
24027 return FALSE;
24028 }
24029 if($this->_can_restore and $rest!=0) $this->restore($rest);
24030 if(!$this->_exec("RETR ".$remotefile, "get")) {
24031 $this->_data_close();
24032 return FALSE;
24033 }
24034 if(!$this->_checkCode()) {
24035 $this->_data_close();
24036 return FALSE;
24037 }
24038 $out=$this->_data_read($mode, $fp);
24039 $this->_data_close();
24040 if(!$this->_readmsg()) return FALSE;
24041 if(!$this->_checkCode()) return FALSE;
24042 return $out;
24043 }
24044
24045 function get($remotefile, $localfile=NULL, $rest=0) {
24046 if(is_null($localfile)) $localfile=$remotefile;
24047 if (@file_exists($localfile)) $this->SendMSG("Warning : local file will be overwritten");
24048 $fp = @fopen($localfile, "w");
24049 if (!$fp) {
24050 $this->PushError("get","can't open local file", "Cannot create \"".$localfile."\"");
24051 return FALSE;
24052 }
24053 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
24054 $pi=pathinfo($remotefile);
24055 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
24056 else $mode=FTP_BINARY;
24057 if(!$this->_data_prepare($mode)) {
24058 fclose($fp);
24059 return FALSE;
24060 }
24061 if($this->_can_restore and $rest!=0) $this->restore($rest);
24062 if(!$this->_exec("RETR ".$remotefile, "get")) {
24063 $this->_data_close();
24064 fclose($fp);
24065 return FALSE;
24066 }
24067 if(!$this->_checkCode()) {
24068 $this->_data_close();
24069 fclose($fp);
24070 return FALSE;
24071 }
24072 $out=$this->_data_read($mode, $fp);
24073 fclose($fp);
24074 $this->_data_close();
24075 if(!$this->_readmsg()) return FALSE;
24076 if(!$this->_checkCode()) return FALSE;
24077 return $out;
24078 }
24079
24080 function fput($remotefile, $fp) {
24081 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
24082 $pi=pathinfo($remotefile);
24083 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
24084 else $mode=FTP_BINARY;
24085 if(!$this->_data_prepare($mode)) {
24086 return FALSE;
24087 }
24088 if($this->_can_restore and $rest!=0) $this->restore($rest);
24089 if(!$this->_exec("STOR ".$remotefile, "put")) {
24090 $this->_data_close();
24091 return FALSE;
24092 }
24093 if(!$this->_checkCode()) {
24094 $this->_data_close();
24095 return FALSE;
24096 }
24097 $ret=$this->_data_write($mode, $fp);
24098 $this->_data_close();
24099 if(!$this->_readmsg()) return FALSE;
24100 if(!$this->_checkCode()) return FALSE;
24101 return $ret;
24102 }
24103
24104 function put($localfile, $remotefile=NULL, $rest=0) {
24105 if(is_null($remotefile)) $remotefile=$localfile;
24106 if (!file_exists($localfile)) {
24107 $this->PushError("put","can't open local file", "No such file or directory \"".$localfile."\"");
24108 return FALSE;
24109 }
24110 $fp = @fopen($localfile, "r");
24111
24112 if (!$fp) {
24113 $this->PushError("put","can't open local file", "Cannot read file \"".$localfile."\"");
24114 return FALSE;
24115 }
24116 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
24117 $pi=pathinfo($localfile);
24118 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
24119 else $mode=FTP_BINARY;
24120 if(!$this->_data_prepare($mode)) {
24121 fclose($fp);
24122 return FALSE;
24123 }
24124 if($this->_can_restore and $rest!=0) $this->restore($rest);
24125 if(!$this->_exec("STOR ".$remotefile, "put")) {
24126 $this->_data_close();
24127 fclose($fp);
24128 return FALSE;
24129 }
24130 if(!$this->_checkCode()) {
24131 $this->_data_close();
24132 fclose($fp);
24133 return FALSE;
24134 }
24135 $ret=$this->_data_write($mode, $fp);
24136 fclose($fp);
24137 $this->_data_close();
24138 if(!$this->_readmsg()) return FALSE;
24139 if(!$this->_checkCode()) return FALSE;
24140 return $ret;
24141 }
24142
24143 function mput($local=".", $remote=NULL, $continious=false) {
24144 $local=realpath($local);
24145 if(!@file_exists($local)) {
24146 $this->PushError("mput","can't open local folder", "Cannot stat folder \"".$local."\"");
24147 return FALSE;
24148 }
24149 if(!is_dir($local)) return $this->put($local, $remote);
24150 if(empty($remote)) $remote=".";
24151 elseif(!$this->file_exists($remote) and !$this->mkdir($remote)) return FALSE;
24152 if($handle = opendir($local)) {
24153 $list=array();
24154 while (false !== ($file = readdir($handle))) {
24155 if ($file != "." && $file != "..") $list[]=$file;
24156 }
24157 closedir($handle);
24158 } else {
24159 $this->PushError("mput","can't open local folder", "Cannot read folder \"".$local."\"");
24160 return FALSE;
24161 }
24162 if(empty($list)) return TRUE;
24163 $ret=true;
24164 foreach($list as $el) {
24165 if(is_dir($local."/".$el)) $t=$this->mput($local."/".$el, $remote."/".$el);
24166 else $t=$this->put($local."/".$el, $remote."/".$el);
24167 if(!$t) {
24168 $ret=FALSE;
24169 if(!$continious) break;
24170 }
24171 }
24172 return $ret;
24173
24174 }
24175
24176 function mget($remote, $local=".", $continious=false) {
24177 $list=$this->rawlist($remote, "-lA");
24178 if($list===false) {
24179 $this->PushError("mget","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
24180 return FALSE;
24181 }
24182 if(empty($list)) return true;
24183 if(!@file_exists($local)) {
24184 if(!@mkdir($local)) {
24185 $this->PushError("mget","can't create local folder", "Cannot create folder \"".$local."\"");
24186 return FALSE;
24187 }
24188 }
24189 foreach($list as $k=>$v) {
24190 $list[$k]=$this->parselisting($v);
24191 if($list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
24192 }
24193 $ret=true;
24194 foreach($list as $el) {
24195 if($el["type"]=="d") {
24196 if(!$this->mget($remote."/".$el["name"], $local."/".$el["name"], $continious)) {
24197 $this->PushError("mget", "can't copy folder", "Can't copy remote folder \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
24198 $ret=false;
24199 if(!$continious) break;
24200 }
24201 } else {
24202 if(!$this->get($remote."/".$el["name"], $local."/".$el["name"])) {
24203 $this->PushError("mget", "can't copy file", "Can't copy remote file \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
24204 $ret=false;
24205 if(!$continious) break;
24206 }
24207 }
24208 @chmod($local."/".$el["name"], $el["perms"]);
24209 $t=strtotime($el["date"]);
24210 if($t!==-1 and $t!==false) @touch($local."/".$el["name"], $t);
24211 }
24212 return $ret;
24213 }
24214
24215 function mdel($remote, $continious=false) {
24216 $list=$this->rawlist($remote, "-la");
24217 if($list===false) {
24218 $this->PushError("mdel","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
24219 return false;
24220 }
24221
24222 foreach($list as $k=>$v) {
24223 $list[$k]=$this->parselisting($v);
24224 if($list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
24225 }
24226 $ret=true;
24227
24228 foreach($list as $el) {
24229 if ( empty($el) )
24230 continue;
24231
24232 if($el["type"]=="d") {
24233 if(!$this->mdel($remote."/".$el["name"], $continious)) {
24234 $ret=false;
24235 if(!$continious) break;
24236 }
24237 } else {
24238 if (!$this->delete($remote."/".$el["name"])) {
24239 $this->PushError("mdel", "can't delete file", "Can't delete remote file \"".$remote."/".$el["name"]."\"");
24240 $ret=false;
24241 if(!$continious) break;
24242 }
24243 }
24244 }
24245
24246 if(!$this->rmdir($remote)) {
24247 $this->PushError("mdel", "can't delete folder", "Can't delete remote folder \"".$remote."/".$el["name"]."\"");
24248 $ret=false;
24249 }
24250 return $ret;
24251 }
24252
24253 function mmkdir($dir, $mode = 0777) {
24254 if(empty($dir)) return FALSE;
24255 if($this->is_exists($dir) or $dir == "/" ) return TRUE;
24256 if(!$this->mmkdir(dirname($dir), $mode)) return false;
24257 $r=$this->mkdir($dir, $mode);
24258 $this->chmod($dir,$mode);
24259 return $r;
24260 }
24261
24262 function glob($pattern, $handle=NULL) {
24263 $path=$output=null;
24264 if(PHP_OS=='WIN32') $slash='\\';
24265 else $slash='/';
24266 $lastpos=strrpos($pattern,$slash);
24267 if(!($lastpos===false)) {
24268 $path=substr($pattern,0,-$lastpos-1);
24269 $pattern=substr($pattern,$lastpos);
24270 } else $path=getcwd();
24271 if(is_array($handle) and !empty($handle)) {
24272 while($dir=each($handle)) {
24273 if($this->glob_pattern_match($pattern,$dir))
24274 $output[]=$dir;
24275 }
24276 } else {
24277 $handle=@opendir($path);
24278 if($handle===false) return false;
24279 while($dir=readdir($handle)) {
24280 if($this->glob_pattern_match($pattern,$dir))
24281 $output[]=$dir;
24282 }
24283 closedir($handle);
24284 }
24285 if(is_array($output)) return $output;
24286 return false;
24287 }
24288
24289 function glob_pattern_match($pattern,$string) {
24290 $out=null;
24291 $chunks=explode(';',$pattern);
24292 foreach($chunks as $pattern) {
24293 $escape=array('$','^','.','{','}','(',')','[',']','|');
24294 while(strpos($pattern,'**')!==false)
24295 $pattern=str_replace('**','*',$pattern);
24296 foreach($escape as $probe)
24297 $pattern=str_replace($probe,"\\$probe",$pattern);
24298 $pattern=str_replace('?*','*',
24299 str_replace('*?','*',
24300 str_replace('*',".*",
24301 str_replace('?','.{1,1}',$pattern))));
24302 $out[]=$pattern;
24303 }
24304 if(count($out)==1) return($this->glob_regexp("^$out[0]$",$string));
24305 else {
24306 foreach($out as $tester)
24307 if($this->my_regexp("^$tester$",$string)) return true;
24308 }
24309 return false;
24310 }
24311
24312 function glob_regexp($pattern,$probe) {
24313 $sensitive=(PHP_OS!='WIN32');
24314 return ($sensitive?
24315 ereg($pattern,$probe):
24316 eregi($pattern,$probe)
24317 );
24318 }
24319
24320 function dirlist($remote) {
24321 $list=$this->rawlist($remote, "-la");
24322 if($list===false) {
24323 $this->PushError("dirlist","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
24324 return false;
24325 }
24326
24327 $dirlist = array();
24328 foreach($list as $k=>$v) {
24329 $entry=$this->parselisting($v);
24330 if ( empty($entry) )
24331 continue;
24332
24333 if($entry["name"]=="." or $entry["name"]=="..")
24334 continue;
24335
24336 $dirlist[$entry['name']] = $entry;
24337 }
24338
24339 return $dirlist;
24340 }
24341 // <!-- --------------------------------------------------------------------------------------- -->
24342 // <!-- Private functions -->
24343 // <!-- --------------------------------------------------------------------------------------- -->
24344 function _checkCode() {
24345 return ($this->_code<400 and $this->_code>0);
24346 }
24347
24348 function _list($arg="", $cmd="LIST", $fnction="_list") {
24349 if(!$this->_data_prepare()) return false;
24350 if(!$this->_exec($cmd.$arg, $fnction)) {
24351 $this->_data_close();
24352 return FALSE;
24353 }
24354 if(!$this->_checkCode()) {
24355 $this->_data_close();
24356 return FALSE;
24357 }
24358 $out="";
24359 if($this->_code<200) {
24360 $out=$this->_data_read();
24361 $this->_data_close();
24362 if(!$this->_readmsg()) return FALSE;
24363 if(!$this->_checkCode()) return FALSE;
24364 if($out === FALSE ) return FALSE;
24365 $out=preg_split("/[".CRLF."]+/", $out, -1, PREG_SPLIT_NO_EMPTY);
24366 // $this->SendMSG(implode($this->_eol_code[$this->OS_local], $out));
24367 }
24368 return $out;
24369 }
24370
24371 // <!-- --------------------------------------------------------------------------------------- -->
24372 // <!-- Partie : gestion des erreurs -->
24373 // <!-- --------------------------------------------------------------------------------------- -->
24374 // Gnre une erreur pour traitement externe la classe
24375 function PushError($fctname,$msg,$desc=false){
24376 $error=array();
24377 $error['time']=time();
24378 $error['fctname']=$fctname;
24379 $error['msg']=$msg;
24380 $error['desc']=$desc;
24381 if($desc) $tmp=' ('.$desc.')'; else $tmp='';
24382 $this->SendMSG($fctname.': '.$msg.$tmp);
24383 return(array_push($this->_error_array,$error));
24384 }
24385
24386 // Rcupre une erreur externe
24387 function PopError(){
24388 if(count($this->_error_array)) return(array_pop($this->_error_array));
24389 else return(false);
24390 }
24391 }
24392
24393 $mod_sockets=TRUE;
24394 if (!extension_loaded('sockets')) {
24395 $prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
24396 if(!@dl($prefix . 'sockets.' . PHP_SHLIB_SUFFIX)) $mod_sockets=FALSE;
24397 }
24398
24399 require_once "class-ftp-".($mod_sockets?"sockets":"pure").".php";
24400 ?>