-
+ A9F2D1D00D44F7DCCB725F62945671E8DB11E372B171AF53A05B97DAFDA5C17FF3F88C76152EBED3BBE32C708E3E8F406E0CE7AD72CA237FBB287F8670DDFBFFmp-wp/wp-admin/includes/class-ftp-pure.php(0 . 0)(1 . 190)
23050 <?php
23051 /**
23052 * PemFTP - A Ftp implementation in pure PHP
23053 *
23054 * @package PemFTP
23055 * @since 2.5
23056 *
23057 * @version 1.0
23058 * @copyright Alexey Dotsenko
23059 * @author Alexey Dotsenko
23060 * @link http://www.phpclasses.org/browse/package/1743.html Site
23061 * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
23062 */
23063
23064 /**
23065 * FTP implementation using fsockopen to connect.
23066 *
23067 * @package PemFTP
23068 * @subpackage Pure
23069 * @since 2.5
23070 *
23071 * @version 1.0
23072 * @copyright Alexey Dotsenko
23073 * @author Alexey Dotsenko
23074 * @link http://www.phpclasses.org/browse/package/1743.html Site
23075 * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
23076 */
23077 class ftp extends ftp_base {
23078
23079 function ftp($verb=FALSE, $le=FALSE) {
23080 $this->__construct($verb, $le);
23081 }
23082
23083 function __construct($verb=FALSE, $le=FALSE) {
23084 parent::__construct(false, $verb, $le);
23085 }
23086
23087 // <!-- --------------------------------------------------------------------------------------- -->
23088 // <!-- Private functions -->
23089 // <!-- --------------------------------------------------------------------------------------- -->
23090
23091 function _settimeout($sock) {
23092 if(!@stream_set_timeout($sock, $this->_timeout)) {
23093 $this->PushError('_settimeout','socket set send timeout');
23094 $this->_quit();
23095 return FALSE;
23096 }
23097 return TRUE;
23098 }
23099
23100 function _connect($host, $port) {
23101 $this->SendMSG("Creating socket");
23102 $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
23103 if (!$sock) {
23104 $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
23105 return FALSE;
23106 }
23107 $this->_connected=true;
23108 return $sock;
23109 }
23110
23111 function _readmsg($fnction="_readmsg"){
23112 if(!$this->_connected) {
23113 $this->PushError($fnction, 'Connect first');
23114 return FALSE;
23115 }
23116 $result=true;
23117 $this->_message="";
23118 $this->_code=0;
23119 $go=true;
23120 do {
23121 $tmp=@fgets($this->_ftp_control_sock, 512);
23122 if($tmp===false) {
23123 $go=$result=false;
23124 $this->PushError($fnction,'Read failed');
23125 } else {
23126 $this->_message.=$tmp;
23127 if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
23128 }
23129 } while($go);
23130 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
23131 $this->_code=(int)$regs[1];
23132 return $result;
23133 }
23134
23135 function _exec($cmd, $fnction="_exec") {
23136 if(!$this->_ready) {
23137 $this->PushError($fnction,'Connect first');
23138 return FALSE;
23139 }
23140 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
23141 $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
23142 if($status===false) {
23143 $this->PushError($fnction,'socket write failed');
23144 return FALSE;
23145 }
23146 $this->_lastaction=time();
23147 if(!$this->_readmsg($fnction)) return FALSE;
23148 return TRUE;
23149 }
23150
23151 function _data_prepare($mode=FTP_ASCII) {
23152 if(!$this->_settype($mode)) return FALSE;
23153 if($this->_passive) {
23154 if(!$this->_exec("PASV", "pasv")) {
23155 $this->_data_close();
23156 return FALSE;
23157 }
23158 if(!$this->_checkCode()) {
23159 $this->_data_close();
23160 return FALSE;
23161 }
23162 $ip_port = explode(",", ereg_replace("^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*".CRLF."$", "\\1", $this->_message));
23163 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
23164 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
23165 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
23166 $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout);
23167 if(!$this->_ftp_data_sock) {
23168 $this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")");
23169 $this->_data_close();
23170 return FALSE;
23171 }
23172 else $this->_ftp_data_sock;
23173 } else {
23174 $this->SendMSG("Only passive connections available!");
23175 return FALSE;
23176 }
23177 return TRUE;
23178 }
23179
23180 function _data_read($mode=FTP_ASCII, $fp=NULL) {
23181 if(is_resource($fp)) $out=0;
23182 else $out="";
23183 if(!$this->_passive) {
23184 $this->SendMSG("Only passive connections available!");
23185 return FALSE;
23186 }
23187 while (!feof($this->_ftp_data_sock)) {
23188 $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size);
23189 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
23190 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
23191 else $out.=$block;
23192 }
23193 return $out;
23194 }
23195
23196 function _data_write($mode=FTP_ASCII, $fp=NULL) {
23197 if(is_resource($fp)) $out=0;
23198 else $out="";
23199 if(!$this->_passive) {
23200 $this->SendMSG("Only passive connections available!");
23201 return FALSE;
23202 }
23203 if(is_resource($fp)) {
23204 while(!feof($fp)) {
23205 $block=fread($fp, $this->_ftp_buff_size);
23206 if(!$this->_data_write_block($mode, $block)) return false;
23207 }
23208 } elseif(!$this->_data_write_block($mode, $fp)) return false;
23209 return TRUE;
23210 }
23211
23212 function _data_write_block($mode, $block) {
23213 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
23214 do {
23215 if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
23216 $this->PushError("_data_write","Can't write to socket");
23217 return FALSE;
23218 }
23219 $block=substr($block, $t);
23220 } while(!empty($block));
23221 return true;
23222 }
23223
23224 function _data_close() {
23225 @fclose($this->_ftp_data_sock);
23226 $this->SendMSG("Disconnected data from remote host");
23227 return TRUE;
23228 }
23229
23230 function _quit($force=FALSE) {
23231 if($this->_connected or $force) {
23232 @fclose($this->_ftp_control_sock);
23233 $this->_connected=false;
23234 $this->SendMSG("Socket closed");
23235 }
23236 }
23237 }
23238
23239 ?>