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_pure extends ftp_base {
30 function __construct($verb=FALSE, $le=FALSE) {
31 parent::__construct(false, $verb, $le);
34 // <!-- --------------------------------------------------------------------------------------- -->
35 // <!-- Private functions -->
36 // <!-- --------------------------------------------------------------------------------------- -->
38 function _settimeout($sock) {
39 if(!@stream_set_timeout($sock, $this->_timeout)) {
40 $this->PushError('_settimeout','socket set send timeout');
47 function _connect($host, $port) {
48 $this->SendMSG("Creating socket");
49 $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
51 $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
54 $this->_connected=true;
58 function _readmsg($fnction="_readmsg"){
59 if(!$this->_connected) {
60 $this->PushError($fnction, 'Connect first');
68 $tmp=@fgets($this->_ftp_control_sock, 512);
71 $this->PushError($fnction,'Read failed');
73 $this->_message.=$tmp;
74 if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
77 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
78 $this->_code=(int)$regs[1];
82 function _exec($cmd, $fnction="_exec") {
84 $this->PushError($fnction,'Connect first');
87 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
88 $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
90 $this->PushError($fnction,'socket write failed');
93 $this->_lastaction=time();
94 if(!$this->_readmsg($fnction)) return FALSE;
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();
105 if(!$this->_checkCode()) {
106 $this->_data_close();
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();
119 else $this->_ftp_data_sock;
121 $this->SendMSG("Only passive connections available!");
127 function _data_read($mode=FTP_ASCII, $fp=NULL) {
128 if(is_resource($fp)) $out=0;
130 if(!$this->_passive) {
131 $this->SendMSG("Only passive connections available!");
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));
143 function _data_write($mode=FTP_ASCII, $fp=NULL) {
144 if(is_resource($fp)) $out=0;
146 if(!$this->_passive) {
147 $this->SendMSG("Only passive connections available!");
150 if(is_resource($fp)) {
152 $block=fread($fp, $this->_ftp_buff_size);
153 if(!$this->_data_write_block($mode, $block)) return false;
155 } elseif(!$this->_data_write_block($mode, $fp)) return false;
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);
162 if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
163 $this->PushError("_data_write","Can't write to socket");
166 $block=substr($block, $t);
167 } while(!empty($block));
171 function _data_close() {
172 @fclose($this->_ftp_data_sock);
173 $this->SendMSG("Disconnected data from remote host");
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");