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_sockets extends ftp_base {
30 function __construct($verb=FALSE, $le=FALSE) {
31 parent::__construct(true, $verb, $le);
34 // <!-- --------------------------------------------------------------------------------------- -->
35 // <!-- Private functions -->
36 // <!-- --------------------------------------------------------------------------------------- -->
38 function _settimeout($sock) {
39 if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
40 $this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock)));
44 if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
45 $this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock)));
52 function _connect($host, $port) {
53 $this->SendMSG("Creating socket");
54 if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
55 $this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock)));
58 if(!$this->_settimeout($sock)) return FALSE;
59 $this->SendMSG("Connecting to \"".$host.":".$port."\"");
60 if (!($res = @socket_connect($sock, $host, $port))) {
61 $this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock)));
65 $this->_connected=true;
69 function _readmsg($fnction="_readmsg"){
70 if(!$this->_connected) {
71 $this->PushError($fnction,'Connect first');
79 $tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ);
82 $this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock)));
84 $this->_message.=$tmp;
85 $go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs);
88 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
89 $this->_code=(int)$regs[1];
93 function _exec($cmd, $fnction="_exec") {
95 $this->PushError($fnction,'Connect first');
98 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
99 $status=@socket_write($this->_ftp_control_sock, $cmd.CRLF);
100 if($status===false) {
101 $this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream)));
104 $this->_lastaction=time();
105 if(!$this->_readmsg($fnction)) return FALSE;
109 function _data_prepare($mode=FTP_ASCII) {
110 if(!$this->_settype($mode)) return FALSE;
111 $this->SendMSG("Creating data socket");
112 $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
113 if ($this->_ftp_data_sock < 0) {
114 $this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock)));
117 if(!$this->_settimeout($this->_ftp_data_sock)) {
118 $this->_data_close();
121 if($this->_passive) {
122 if(!$this->_exec("PASV", "pasv")) {
123 $this->_data_close();
126 if(!$this->_checkCode()) {
127 $this->_data_close();
130 $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));
131 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
132 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
133 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
134 if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
135 $this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock)));
136 $this->_data_close();
139 else $this->_ftp_temp_sock=$this->_ftp_data_sock;
141 if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) {
142 $this->PushError("_data_prepare","can't get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock)));
143 $this->_data_close();
146 if(!@socket_bind($this->_ftp_data_sock,$addr)){
147 $this->PushError("_data_prepare","can't bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
148 $this->_data_close();
151 if(!@socket_listen($this->_ftp_data_sock)) {
152 $this->PushError("_data_prepare","can't listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
153 $this->_data_close();
156 if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
157 $this->PushError("_data_prepare","can't get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock)));
158 $this->_data_close();
161 if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) {
162 $this->_data_close();
165 if(!$this->_checkCode()) {
166 $this->_data_close();
173 function _data_read($mode=FTP_ASCII, $fp=NULL) {
174 $NewLine=$this->_eol_code[$this->OS_local];
175 if(is_resource($fp)) $out=0;
177 if(!$this->_passive) {
178 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
179 $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
180 if($this->_ftp_temp_sock===FALSE) {
181 $this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
182 $this->_data_close();
187 while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) {
188 if($block==="") break;
189 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
190 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
196 function _data_write($mode=FTP_ASCII, $fp=NULL) {
197 $NewLine=$this->_eol_code[$this->OS_local];
198 if(is_resource($fp)) $out=0;
200 if(!$this->_passive) {
201 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
202 $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
203 if($this->_ftp_temp_sock===FALSE) {
204 $this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
205 $this->_data_close();
209 if(is_resource($fp)) {
211 $block=fread($fp, $this->_ftp_buff_size);
212 if(!$this->_data_write_block($mode, $block)) return false;
214 } elseif(!$this->_data_write_block($mode, $fp)) return false;
218 function _data_write_block($mode, $block) {
219 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
221 if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) {
222 $this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
223 $this->_data_close();
226 $block=substr($block, $t);
227 } while(!empty($block));
231 function _data_close() {
232 @socket_close($this->_ftp_temp_sock);
233 @socket_close($this->_ftp_data_sock);
234 $this->SendMSG("Disconnected data from remote host");
239 if($this->_connected) {
240 @socket_close($this->_ftp_control_sock);
241 $this->_connected=false;
242 $this->SendMSG("Socket closed");