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 License http://www.opensource.org/licenses/lgpl-license.html
14 class ftp extends ftp_base {
16 function ftp($verb=FALSE, $le=FALSE) {
17 $this->__construct($verb, $le);
20 function __construct($verb=FALSE, $le=FALSE) {
21 parent::__construct(false, $verb, $le);
24 // <!-- --------------------------------------------------------------------------------------- -->
25 // <!-- Private functions -->
26 // <!-- --------------------------------------------------------------------------------------- -->
28 function _settimeout($sock) {
29 if(!@stream_set_timeout($sock, $this->_timeout)) {
30 $this->PushError('_settimeout','socket set send timeout');
37 function _connect($host, $port) {
38 $this->SendMSG("Creating socket");
39 $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
41 $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
44 $this->_connected=true;
48 function _readmsg($fnction="_readmsg"){
49 if(!$this->_connected) {
50 $this->PushError($fnction, 'Connect first');
58 $tmp=@fgets($this->_ftp_control_sock, 512);
61 $this->PushError($fnction,'Read failed');
63 $this->_message.=$tmp;
64 if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
67 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
68 $this->_code=(int)$regs[1];
72 function _exec($cmd, $fnction="_exec") {
74 $this->PushError($fnction,'Connect first');
77 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
78 $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
80 $this->PushError($fnction,'socket write failed');
83 $this->_lastaction=time();
84 if(!$this->_readmsg($fnction)) return FALSE;
88 function _data_prepare($mode=FTP_ASCII) {
89 if(!$this->_settype($mode)) return FALSE;
91 if(!$this->_exec("PASV", "pasv")) {
95 if(!$this->_checkCode()) {
99 $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));
100 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
101 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
102 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
103 $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout);
104 if(!$this->_ftp_data_sock) {
105 $this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")");
106 $this->_data_close();
109 else $this->_ftp_data_sock;
111 $this->SendMSG("Only passive connections available!");
117 function _data_read($mode=FTP_ASCII, $fp=NULL) {
118 if(is_resource($fp)) $out=0;
120 if(!$this->_passive) {
121 $this->SendMSG("Only passive connections available!");
124 while (!feof($this->_ftp_data_sock)) {
125 $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size);
126 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
127 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
133 function _data_write($mode=FTP_ASCII, $fp=NULL) {
134 if(is_resource($fp)) $out=0;
136 if(!$this->_passive) {
137 $this->SendMSG("Only passive connections available!");
140 if(is_resource($fp)) {
142 $block=fread($fp, $this->_ftp_buff_size);
143 if(!$this->_data_write_block($mode, $block)) return false;
145 } elseif(!$this->_data_write_block($mode, $fp)) return false;
149 function _data_write_block($mode, $block) {
150 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
152 if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
153 $this->PushError("_data_write","Can't write to socket");
156 $block=substr($block, $t);
157 } while(!empty($block));
161 function _data_close() {
162 @fclose($this->_ftp_data_sock);
163 $this->SendMSG("Disconnected data from remote host");
167 function _quit($force=FALSE) {
168 if($this->_connected or $force) {
169 @fclose($this->_ftp_control_sock);
170 $this->_connected=false;
171 $this->SendMSG("Socket closed");