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 * FTP implementation using fsockopen to connect.
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 ftp($verb=FALSE, $le=FALSE) {
31 $this->__construct($verb, $le);
34 function __construct($verb=FALSE, $le=FALSE) {
35 parent::__construct(false, $verb, $le);
38 // <!-- --------------------------------------------------------------------------------------- -->
39 // <!-- Private functions -->
40 // <!-- --------------------------------------------------------------------------------------- -->
42 function _settimeout($sock) {
43 if(!@stream_set_timeout($sock, $this->_timeout)) {
44 $this->PushError('_settimeout','socket set send timeout');
51 function _connect($host, $port) {
52 $this->SendMSG("Creating socket");
53 $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
55 $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
58 $this->_connected=true;
62 function _readmsg($fnction="_readmsg"){
63 if(!$this->_connected) {
64 $this->PushError($fnction, 'Connect first');
72 $tmp=@fgets($this->_ftp_control_sock, 512);
75 $this->PushError($fnction,'Read failed');
77 $this->_message.=$tmp;
78 if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
81 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
82 $this->_code=(int)$regs[1];
86 function _exec($cmd, $fnction="_exec") {
88 $this->PushError($fnction,'Connect first');
91 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
92 $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
94 $this->PushError($fnction,'socket write failed');
97 $this->_lastaction=time();
98 if(!$this->_readmsg($fnction)) return FALSE;
102 function _data_prepare($mode=FTP_ASCII) {
103 if(!$this->_settype($mode)) return FALSE;
104 if($this->_passive) {
105 if(!$this->_exec("PASV", "pasv")) {
106 $this->_data_close();
109 if(!$this->_checkCode()) {
110 $this->_data_close();
113 $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));
114 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
115 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
116 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
117 $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout);
118 if(!$this->_ftp_data_sock) {
119 $this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")");
120 $this->_data_close();
123 else $this->_ftp_data_sock;
125 $this->SendMSG("Only passive connections available!");
131 function _data_read($mode=FTP_ASCII, $fp=NULL) {
132 if(is_resource($fp)) $out=0;
134 if(!$this->_passive) {
135 $this->SendMSG("Only passive connections available!");
138 while (!feof($this->_ftp_data_sock)) {
139 $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size);
140 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
141 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
147 function _data_write($mode=FTP_ASCII, $fp=NULL) {
148 if(is_resource($fp)) $out=0;
150 if(!$this->_passive) {
151 $this->SendMSG("Only passive connections available!");
154 if(is_resource($fp)) {
156 $block=fread($fp, $this->_ftp_buff_size);
157 if(!$this->_data_write_block($mode, $block)) return false;
159 } elseif(!$this->_data_write_block($mode, $fp)) return false;
163 function _data_write_block($mode, $block) {
164 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
166 if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
167 $this->PushError("_data_write","Can't write to socket");
170 $block=substr($block, $t);
171 } while(!empty($block));
175 function _data_close() {
176 @fclose($this->_ftp_data_sock);
177 $this->SendMSG("Disconnected data from remote host");
181 function _quit($force=FALSE) {
182 if($this->_connected or $force) {
183 @fclose($this->_ftp_control_sock);
184 $this->_connected=false;
185 $this->SendMSG("Socket closed");