3 * PemFTP - A Ftp implementation in pure PHP
9 * @copyright Alexey Dotsenko
10 * @author Alexey Dotsenko
11 * @link http://www.phpclasses.org/browse/package/1743.html Site
12 * @license LGPL http://www.opensource.org/licenses/lgpl-license.html
16 * Socket Based FTP implementation
23 * @copyright Alexey Dotsenko
24 * @author Alexey Dotsenko
25 * @link http://www.phpclasses.org/browse/package/1743.html Site
26 * @license LGPL http://www.opensource.org/licenses/lgpl-license.html
28 class ftp extends ftp_base {
30 function __construct($verb=FALSE, $le=FALSE) {
31 parent::__construct(true, $verb, $le);
34 function ftp($verb=FALSE, $le=FALSE) {
35 $this->__construct($verb, $le);
38 // <!-- --------------------------------------------------------------------------------------- -->
39 // <!-- Private functions -->
40 // <!-- --------------------------------------------------------------------------------------- -->
42 function _settimeout($sock) {
43 if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
44 $this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock)));
48 if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
49 $this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock)));
56 function _connect($host, $port) {
57 $this->SendMSG("Creating socket");
58 if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
59 $this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock)));
62 if(!$this->_settimeout($sock)) return FALSE;
63 $this->SendMSG("Connecting to \"".$host.":".$port."\"");
64 if (!($res = @socket_connect($sock, $host, $port))) {
65 $this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock)));
69 $this->_connected=true;
73 function _readmsg($fnction="_readmsg"){
74 if(!$this->_connected) {
75 $this->PushError($fnction,'Connect first');
83 $tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ);
86 $this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock)));
88 $this->_message.=$tmp;
89 $go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs);
92 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
93 $this->_code=(int)$regs[1];
97 function _exec($cmd, $fnction="_exec") {
99 $this->PushError($fnction,'Connect first');
102 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
103 $status=@socket_write($this->_ftp_control_sock, $cmd.CRLF);
104 if($status===false) {
105 $this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream)));
108 $this->_lastaction=time();
109 if(!$this->_readmsg($fnction)) return FALSE;
113 function _data_prepare($mode=FTP_ASCII) {
114 if(!$this->_settype($mode)) return FALSE;
115 $this->SendMSG("Creating data socket");
116 $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
117 if ($this->_ftp_data_sock < 0) {
118 $this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock)));
121 if(!$this->_settimeout($this->_ftp_data_sock)) {
122 $this->_data_close();
125 if($this->_passive) {
126 if(!$this->_exec("PASV", "pasv")) {
127 $this->_data_close();
130 if(!$this->_checkCode()) {
131 $this->_data_close();
134 $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));
135 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
136 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
137 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
138 if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
139 $this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock)));
140 $this->_data_close();
143 else $this->_ftp_temp_sock=$this->_ftp_data_sock;
145 if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) {
146 $this->PushError("_data_prepare","can't get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock)));
147 $this->_data_close();
150 if(!@socket_bind($this->_ftp_data_sock,$addr)){
151 $this->PushError("_data_prepare","can't bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
152 $this->_data_close();
155 if(!@socket_listen($this->_ftp_data_sock)) {
156 $this->PushError("_data_prepare","can't listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
157 $this->_data_close();
160 if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
161 $this->PushError("_data_prepare","can't get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock)));
162 $this->_data_close();
165 if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) {
166 $this->_data_close();
169 if(!$this->_checkCode()) {
170 $this->_data_close();
177 function _data_read($mode=FTP_ASCII, $fp=NULL) {
178 $NewLine=$this->_eol_code[$this->OS_local];
179 if(is_resource($fp)) $out=0;
181 if(!$this->_passive) {
182 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
183 $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
184 if($this->_ftp_temp_sock===FALSE) {
185 $this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
186 $this->_data_close();
191 while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) {
192 if($block==="") break;
193 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
194 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
200 function _data_write($mode=FTP_ASCII, $fp=NULL) {
201 $NewLine=$this->_eol_code[$this->OS_local];
202 if(is_resource($fp)) $out=0;
204 if(!$this->_passive) {
205 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
206 $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
207 if($this->_ftp_temp_sock===FALSE) {
208 $this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
209 $this->_data_close();
213 if(is_resource($fp)) {
215 $block=fread($fp, $this->_ftp_buff_size);
216 if(!$this->_data_write_block($mode, $block)) return false;
218 } elseif(!$this->_data_write_block($mode, $fp)) return false;
222 function _data_write_block($mode, $block) {
223 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
225 if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) {
226 $this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
227 $this->_data_close();
230 $block=substr($block, $t);
231 } while(!empty($block));
235 function _data_close() {
236 @socket_close($this->_ftp_temp_sock);
237 @socket_close($this->_ftp_data_sock);
238 $this->SendMSG("Disconnected data from remote host");
243 if($this->_connected) {
244 @socket_close($this->_ftp_control_sock);
245 $this->_connected=false;
246 $this->SendMSG("Socket closed");