wp/wp-admin/includes/class-ftp.php
changeset 19 3d72ae0968f4
parent 16 a86126ab1dd4
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   332 			if(!$this->_checkCode()) return FALSE;
   332 			if(!$this->_checkCode()) return FALSE;
   333 			$this->_lastaction=time();
   333 			$this->_lastaction=time();
   334 		} while($this->_code<200);
   334 		} while($this->_code<200);
   335 		$this->_ready=true;
   335 		$this->_ready=true;
   336 		$syst=$this->systype();
   336 		$syst=$this->systype();
   337 		if(!$syst) $this->SendMSG("Can't detect remote OS");
   337 		if(!$syst) $this->SendMSG("Cannot detect remote OS");
   338 		else {
   338 		else {
   339 			if(preg_match("/win|dos|novell/i", $syst[0])) $this->OS_remote=FTP_OS_Windows;
   339 			if(preg_match("/win|dos|novell/i", $syst[0])) $this->OS_remote=FTP_OS_Windows;
   340 			elseif(preg_match("/os/i", $syst[0])) $this->OS_remote=FTP_OS_Mac;
   340 			elseif(preg_match("/os/i", $syst[0])) $this->OS_remote=FTP_OS_Mac;
   341 			elseif(preg_match("/(li|u)nix/i", $syst[0])) $this->OS_remote=FTP_OS_Unix;
   341 			elseif(preg_match("/(li|u)nix/i", $syst[0])) $this->OS_remote=FTP_OS_Unix;
   342 			else $this->OS_remote=FTP_OS_Mac;
   342 			else $this->OS_remote=FTP_OS_Mac;
   343 			$this->SendMSG("Remote OS: ".$this->OS_FullName[$this->OS_remote]);
   343 			$this->SendMSG("Remote OS: ".$this->OS_FullName[$this->OS_remote]);
   344 		}
   344 		}
   345 		if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
   345 		if(!$this->features()) $this->SendMSG("Cannot get features list. All supported - disabled");
   346 		else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
   346 		else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
   347 		return TRUE;
   347 		return TRUE;
   348 	}
   348 	}
   349 
   349 
   350 	function quit($force=false) {
   350 	function quit($force=false) {
   369 			if(!$this->_exec((($this->_code==331)?"PASS ":"ACCT ").$this->_password, "login")) return FALSE;
   369 			if(!$this->_exec((($this->_code==331)?"PASS ":"ACCT ").$this->_password, "login")) return FALSE;
   370 			if(!$this->_checkCode()) return FALSE;
   370 			if(!$this->_checkCode()) return FALSE;
   371 		}
   371 		}
   372 		$this->SendMSG("Authentication succeeded");
   372 		$this->SendMSG("Authentication succeeded");
   373 		if(empty($this->_features)) {
   373 		if(empty($this->_features)) {
   374 			if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
   374 			if(!$this->features()) $this->SendMSG("Cannot get features list. All supported - disabled");
   375 			else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
   375 			else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
   376 		}
   376 		}
   377 		return TRUE;
   377 		return TRUE;
   378 	}
   378 	}
   379 
   379 
   478 		if(!isset($this->_features["REST"])) {
   478 		if(!isset($this->_features["REST"])) {
   479 			$this->PushError("restore", "not supported by server");
   479 			$this->PushError("restore", "not supported by server");
   480 			return FALSE;
   480 			return FALSE;
   481 		}
   481 		}
   482 		if($this->_curtype!=FTP_BINARY) {
   482 		if($this->_curtype!=FTP_BINARY) {
   483 			$this->PushError("restore", "can't restore in ASCII mode");
   483 			$this->PushError("restore", "cannot restore in ASCII mode");
   484 			return FALSE;
   484 			return FALSE;
   485 		}
   485 		}
   486 		if(!$this->_exec("REST ".$from, "resore")) return FALSE;
   486 		if(!$this->_exec("REST ".$from, "resore")) return FALSE;
   487 		if(!$this->_checkCode()) return FALSE;
   487 		if(!$this->_checkCode()) return FALSE;
   488 		return TRUE;
   488 		return TRUE;
   551 	function get($remotefile, $localfile=NULL, $rest=0) {
   551 	function get($remotefile, $localfile=NULL, $rest=0) {
   552 		if(is_null($localfile)) $localfile=$remotefile;
   552 		if(is_null($localfile)) $localfile=$remotefile;
   553 		if (@file_exists($localfile)) $this->SendMSG("Warning : local file will be overwritten");
   553 		if (@file_exists($localfile)) $this->SendMSG("Warning : local file will be overwritten");
   554 		$fp = @fopen($localfile, "w");
   554 		$fp = @fopen($localfile, "w");
   555 		if (!$fp) {
   555 		if (!$fp) {
   556 			$this->PushError("get","can't open local file", "Cannot create \"".$localfile."\"");
   556 			$this->PushError("get","cannot open local file", "Cannot create \"".$localfile."\"");
   557 			return FALSE;
   557 			return FALSE;
   558 		}
   558 		}
   559 		if($this->_can_restore and $rest!=0) fseek($fp, $rest);
   559 		if($this->_can_restore and $rest!=0) fseek($fp, $rest);
   560 		$pi=pathinfo($remotefile);
   560 		$pi=pathinfo($remotefile);
   561 		if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
   561 		if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
   608 	}
   608 	}
   609 
   609 
   610 	function put($localfile, $remotefile=NULL, $rest=0) {
   610 	function put($localfile, $remotefile=NULL, $rest=0) {
   611 		if(is_null($remotefile)) $remotefile=$localfile;
   611 		if(is_null($remotefile)) $remotefile=$localfile;
   612 		if (!file_exists($localfile)) {
   612 		if (!file_exists($localfile)) {
   613 			$this->PushError("put","can't open local file", "No such file or directory \"".$localfile."\"");
   613 			$this->PushError("put","cannot open local file", "No such file or directory \"".$localfile."\"");
   614 			return FALSE;
   614 			return FALSE;
   615 		}
   615 		}
   616 		$fp = @fopen($localfile, "r");
   616 		$fp = @fopen($localfile, "r");
   617 
   617 
   618 		if (!$fp) {
   618 		if (!$fp) {
   619 			$this->PushError("put","can't open local file", "Cannot read file \"".$localfile."\"");
   619 			$this->PushError("put","cannot open local file", "Cannot read file \"".$localfile."\"");
   620 			return FALSE;
   620 			return FALSE;
   621 		}
   621 		}
   622 		if($this->_can_restore and $rest!=0) fseek($fp, $rest);
   622 		if($this->_can_restore and $rest!=0) fseek($fp, $rest);
   623 		$pi=pathinfo($localfile);
   623 		$pi=pathinfo($localfile);
   624 		if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
   624 		if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
   647 	}
   647 	}
   648 
   648 
   649 	function mput($local=".", $remote=NULL, $continious=false) {
   649 	function mput($local=".", $remote=NULL, $continious=false) {
   650 		$local=realpath($local);
   650 		$local=realpath($local);
   651 		if(!@file_exists($local)) {
   651 		if(!@file_exists($local)) {
   652 			$this->PushError("mput","can't open local folder", "Cannot stat folder \"".$local."\"");
   652 			$this->PushError("mput","cannot open local folder", "Cannot stat folder \"".$local."\"");
   653 			return FALSE;
   653 			return FALSE;
   654 		}
   654 		}
   655 		if(!is_dir($local)) return $this->put($local, $remote);
   655 		if(!is_dir($local)) return $this->put($local, $remote);
   656 		if(empty($remote)) $remote=".";
   656 		if(empty($remote)) $remote=".";
   657 		elseif(!$this->file_exists($remote) and !$this->mkdir($remote)) return FALSE;
   657 		elseif(!$this->file_exists($remote) and !$this->mkdir($remote)) return FALSE;
   660 			while (false !== ($file = readdir($handle))) {
   660 			while (false !== ($file = readdir($handle))) {
   661 				if ($file != "." && $file != "..") $list[]=$file;
   661 				if ($file != "." && $file != "..") $list[]=$file;
   662 			}
   662 			}
   663 			closedir($handle);
   663 			closedir($handle);
   664 		} else {
   664 		} else {
   665 			$this->PushError("mput","can't open local folder", "Cannot read folder \"".$local."\"");
   665 			$this->PushError("mput","cannot open local folder", "Cannot read folder \"".$local."\"");
   666 			return FALSE;
   666 			return FALSE;
   667 		}
   667 		}
   668 		if(empty($list)) return TRUE;
   668 		if(empty($list)) return TRUE;
   669 		$ret=true;
   669 		$ret=true;
   670 		foreach($list as $el) {
   670 		foreach($list as $el) {
   680 	}
   680 	}
   681 
   681 
   682 	function mget($remote, $local=".", $continious=false) {
   682 	function mget($remote, $local=".", $continious=false) {
   683 		$list=$this->rawlist($remote, "-lA");
   683 		$list=$this->rawlist($remote, "-lA");
   684 		if($list===false) {
   684 		if($list===false) {
   685 			$this->PushError("mget","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
   685 			$this->PushError("mget","cannot read remote folder list", "Cannot read remote folder \"".$remote."\" contents");
   686 			return FALSE;
   686 			return FALSE;
   687 		}
   687 		}
   688 		if(empty($list)) return true;
   688 		if(empty($list)) return true;
   689 		if(!@file_exists($local)) {
   689 		if(!@file_exists($local)) {
   690 			if(!@mkdir($local)) {
   690 			if(!@mkdir($local)) {
   691 				$this->PushError("mget","can't create local folder", "Cannot create folder \"".$local."\"");
   691 				$this->PushError("mget","cannot create local folder", "Cannot create folder \"".$local."\"");
   692 				return FALSE;
   692 				return FALSE;
   693 			}
   693 			}
   694 		}
   694 		}
   695 		foreach($list as $k=>$v) {
   695 		foreach($list as $k=>$v) {
   696 			$list[$k]=$this->parselisting($v);
   696 			$list[$k]=$this->parselisting($v);
   698 		}
   698 		}
   699 		$ret=true;
   699 		$ret=true;
   700 		foreach($list as $el) {
   700 		foreach($list as $el) {
   701 			if($el["type"]=="d") {
   701 			if($el["type"]=="d") {
   702 				if(!$this->mget($remote."/".$el["name"], $local."/".$el["name"], $continious)) {
   702 				if(!$this->mget($remote."/".$el["name"], $local."/".$el["name"], $continious)) {
   703 					$this->PushError("mget", "can't copy folder", "Can't copy remote folder \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
   703 					$this->PushError("mget", "cannot copy folder", "Cannot copy remote folder \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
   704 					$ret=false;
   704 					$ret=false;
   705 					if(!$continious) break;
   705 					if(!$continious) break;
   706 				}
   706 				}
   707 			} else {
   707 			} else {
   708 				if(!$this->get($remote."/".$el["name"], $local."/".$el["name"])) {
   708 				if(!$this->get($remote."/".$el["name"], $local."/".$el["name"])) {
   709 					$this->PushError("mget", "can't copy file", "Can't copy remote file \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
   709 					$this->PushError("mget", "cannot copy file", "Cannot copy remote file \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
   710 					$ret=false;
   710 					$ret=false;
   711 					if(!$continious) break;
   711 					if(!$continious) break;
   712 				}
   712 				}
   713 			}
   713 			}
   714 			@chmod($local."/".$el["name"], $el["perms"]);
   714 			@chmod($local."/".$el["name"], $el["perms"]);
   719 	}
   719 	}
   720 
   720 
   721 	function mdel($remote, $continious=false) {
   721 	function mdel($remote, $continious=false) {
   722 		$list=$this->rawlist($remote, "-la");
   722 		$list=$this->rawlist($remote, "-la");
   723 		if($list===false) {
   723 		if($list===false) {
   724 			$this->PushError("mdel","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
   724 			$this->PushError("mdel","cannot read remote folder list", "Cannot read remote folder \"".$remote."\" contents");
   725 			return false;
   725 			return false;
   726 		}
   726 		}
   727 
   727 
   728 		foreach($list as $k=>$v) {
   728 		foreach($list as $k=>$v) {
   729 			$list[$k]=$this->parselisting($v);
   729 			$list[$k]=$this->parselisting($v);
   740 					$ret=false;
   740 					$ret=false;
   741 					if(!$continious) break;
   741 					if(!$continious) break;
   742 				}
   742 				}
   743 			} else {
   743 			} else {
   744 				if (!$this->delete($remote."/".$el["name"])) {
   744 				if (!$this->delete($remote."/".$el["name"])) {
   745 					$this->PushError("mdel", "can't delete file", "Can't delete remote file \"".$remote."/".$el["name"]."\"");
   745 					$this->PushError("mdel", "cannot delete file", "Cannot delete remote file \"".$remote."/".$el["name"]."\"");
   746 					$ret=false;
   746 					$ret=false;
   747 					if(!$continious) break;
   747 					if(!$continious) break;
   748 				}
   748 				}
   749 			}
   749 			}
   750 		}
   750 		}
   751 
   751 
   752 		if(!$this->rmdir($remote)) {
   752 		if(!$this->rmdir($remote)) {
   753 			$this->PushError("mdel", "can't delete folder", "Can't delete remote folder \"".$remote."/".$el["name"]."\"");
   753 			$this->PushError("mdel", "cannot delete folder", "Cannot delete remote folder \"".$remote."/".$el["name"]."\"");
   754 			$ret=false;
   754 			$ret=false;
   755 		}
   755 		}
   756 		return $ret;
   756 		return $ret;
   757 	}
   757 	}
   758 
   758 
   790 		}
   790 		}
   791 		if(is_array($output)) return $output;
   791 		if(is_array($output)) return $output;
   792 		return false;
   792 		return false;
   793 	}
   793 	}
   794 
   794 
   795 	function glob_pattern_match($pattern,$string) {
   795 	function glob_pattern_match($pattern,$subject) {
   796 		$out=null;
   796 		$out=null;
   797 		$chunks=explode(';',$pattern);
   797 		$chunks=explode(';',$pattern);
   798 		foreach($chunks as $pattern) {
   798 		foreach($chunks as $pattern) {
   799 			$escape=array('$','^','.','{','}','(',')','[',']','|');
   799 			$escape=array('$','^','.','{','}','(',')','[',']','|');
   800 			while(strpos($pattern,'**')!==false)
   800 			while(strpos($pattern,'**')!==false)
   805 				str_replace('*?','*',
   805 				str_replace('*?','*',
   806 					str_replace('*',".*",
   806 					str_replace('*',".*",
   807 						str_replace('?','.{1,1}',$pattern))));
   807 						str_replace('?','.{1,1}',$pattern))));
   808 			$out[]=$pattern;
   808 			$out[]=$pattern;
   809 		}
   809 		}
   810 		if(count($out)==1) return($this->glob_regexp("^$out[0]$",$string));
   810 		if(count($out)==1) return($this->glob_regexp("^$out[0]$",$subject));
   811 		else {
   811 		else {
   812 			foreach($out as $tester)
   812 			foreach($out as $tester)
   813 				if($this->my_regexp("^$tester$",$string)) return true;
   813 				// TODO: This should probably be glob_regexp(), but needs tests.
       
   814 				if($this->my_regexp("^$tester$",$subject)) return true;
   814 		}
   815 		}
   815 		return false;
   816 		return false;
   816 	}
   817 	}
   817 
   818 
   818 	function glob_regexp($pattern,$probe) {
   819 	function glob_regexp($pattern,$subject) {
   819 		$sensitive=(PHP_OS!='WIN32');
   820 		$sensitive=(PHP_OS!='WIN32');
   820 		return ($sensitive?
   821 		return ($sensitive?
   821 			preg_match( '/' . preg_quote( $pattern, '/' ) . '/', $probe ) :
   822 			preg_match( '/' . preg_quote( $pattern, '/' ) . '/', $subject ) :
   822 			preg_match( '/' . preg_quote( $pattern, '/' ) . '/i', $probe )
   823 			preg_match( '/' . preg_quote( $pattern, '/' ) . '/i', $subject )
   823 		);
   824 		);
   824 	}
   825 	}
   825 
   826 
   826 	function dirlist($remote) {
   827 	function dirlist($remote) {
   827 		$list=$this->rawlist($remote, "-la");
   828 		$list=$this->rawlist($remote, "-la");
   828 		if($list===false) {
   829 		if($list===false) {
   829 			$this->PushError("dirlist","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
   830 			$this->PushError("dirlist","cannot read remote folder list", "Cannot read remote folder \"".$remote."\" contents");
   830 			return false;
   831 			return false;
   831 		}
   832 		}
   832 
   833 
   833 		$dirlist = array();
   834 		$dirlist = array();
   834 		foreach($list as $k=>$v) {
   835 		foreach($list as $k=>$v) {