[ 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 * FTP implementation using fsockopen to connect. 17 * 18 * @package PemFTP 19 * @subpackage Pure 20 * @since 2.5.0 21 * 22 * @version 1.0 23 * @copyright Alexey Dotsenko 24 * @author Alexey Dotsenko 25 * @link https://www.phpclasses.org/package/1743-PHP-FTP-client-in-pure-PHP.html 26 * @license LGPL https://opensource.org/licenses/lgpl-license.html 27 */ 28 class ftp_pure extends ftp_base { 29 30 function __construct($verb=FALSE, $le=FALSE) { 31 parent::__construct(false, $verb, $le); 32 } 33 34 // <!-- --------------------------------------------------------------------------------------- --> 35 // <!-- Private functions --> 36 // <!-- --------------------------------------------------------------------------------------- --> 37 38 function _settimeout($sock) { 39 if(!@stream_set_timeout($sock, $this->_timeout)) { 40 $this->PushError('_settimeout','socket set send timeout'); 41 $this->_quit(); 42 return FALSE; 43 } 44 return TRUE; 45 } 46 47 function _connect($host, $port) { 48 $this->SendMSG("Creating socket"); 49 $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout); 50 if (!$sock) { 51 $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")"); 52 return FALSE; 53 } 54 $this->_connected=true; 55 return $sock; 56 } 57 58 function _readmsg($fnction="_readmsg"){ 59 if(!$this->_connected) { 60 $this->PushError($fnction, 'Connect first'); 61 return FALSE; 62 } 63 $result=true; 64 $this->_message=""; 65 $this->_code=0; 66 $go=true; 67 do { 68 $tmp=@fgets($this->_ftp_control_sock, 512); 69 if($tmp===false) { 70 $go=$result=false; 71 $this->PushError($fnction,'Read failed'); 72 } else { 73 $this->_message.=$tmp; 74 if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false; 75 } 76 } while($go); 77 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF; 78 $this->_code=(int)$regs[1]; 79 return $result; 80 } 81 82 function _exec($cmd, $fnction="_exec") { 83 if(!$this->_ready) { 84 $this->PushError($fnction,'Connect first'); 85 return FALSE; 86 } 87 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF; 88 $status=@fputs($this->_ftp_control_sock, $cmd.CRLF); 89 if($status===false) { 90 $this->PushError($fnction,'socket write failed'); 91 return FALSE; 92 } 93 $this->_lastaction=time(); 94 if(!$this->_readmsg($fnction)) return FALSE; 95 return TRUE; 96 } 97 98 function _data_prepare($mode=FTP_ASCII) { 99 if(!$this->_settype($mode)) return FALSE; 100 if($this->_passive) { 101 if(!$this->_exec("PASV", "pasv")) { 102 $this->_data_close(); 103 return FALSE; 104 } 105 if(!$this->_checkCode()) { 106 $this->_data_close(); 107 return FALSE; 108 } 109 $ip_port = explode(",", preg_replace("/^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*$/s", "\\1", $this->_message)); 110 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3]; 111 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]); 112 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); 113 $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout); 114 if(!$this->_ftp_data_sock) { 115 $this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")"); 116 $this->_data_close(); 117 return FALSE; 118 } 119 else $this->_ftp_data_sock; 120 } else { 121 $this->SendMSG("Only passive connections available!"); 122 return FALSE; 123 } 124 return TRUE; 125 } 126 127 function _data_read($mode=FTP_ASCII, $fp=NULL) { 128 if(is_resource($fp)) $out=0; 129 else $out=""; 130 if(!$this->_passive) { 131 $this->SendMSG("Only passive connections available!"); 132 return FALSE; 133 } 134 while (!feof($this->_ftp_data_sock)) { 135 $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size); 136 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block); 137 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block)); 138 else $out.=$block; 139 } 140 return $out; 141 } 142 143 function _data_write($mode=FTP_ASCII, $fp=NULL) { 144 if(is_resource($fp)) $out=0; 145 else $out=""; 146 if(!$this->_passive) { 147 $this->SendMSG("Only passive connections available!"); 148 return FALSE; 149 } 150 if(is_resource($fp)) { 151 while(!feof($fp)) { 152 $block=fread($fp, $this->_ftp_buff_size); 153 if(!$this->_data_write_block($mode, $block)) return false; 154 } 155 } elseif(!$this->_data_write_block($mode, $fp)) return false; 156 return TRUE; 157 } 158 159 function _data_write_block($mode, $block) { 160 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block); 161 do { 162 if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) { 163 $this->PushError("_data_write","Can't write to socket"); 164 return FALSE; 165 } 166 $block=substr($block, $t); 167 } while(!empty($block)); 168 return true; 169 } 170 171 function _data_close() { 172 @fclose($this->_ftp_data_sock); 173 $this->SendMSG("Disconnected data from remote host"); 174 return TRUE; 175 } 176 177 function _quit($force=FALSE) { 178 if($this->_connected or $force) { 179 @fclose($this->_ftp_control_sock); 180 $this->_connected=false; 181 $this->SendMSG("Socket closed"); 182 } 183 } 184 } 185 186 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |