[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * PemFTP - A Ftp implementation in pure PHP
|
---|
| 4 | *
|
---|
| 5 | * @package PemFTP
|
---|
| 6 | * @since 2.5
|
---|
| 7 | *
|
---|
| 8 | * @version 1.0
|
---|
| 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
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * FTP implementation using fsockopen to connect.
|
---|
| 17 | *
|
---|
| 18 | * @package PemFTP
|
---|
| 19 | * @subpackage Pure
|
---|
| 20 | * @since 2.5
|
---|
| 21 | *
|
---|
| 22 | * @version 1.0
|
---|
| 23 | * @copyright Alexey Dotsenko
|
---|
| 24 | * @author Alexey Dotsenko
|
---|
| 25 | * @link http://www.phpclasses.org/browse/package/1743.html Site
|
---|
| 26 | * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
|
---|
| 27 | */
|
---|
| 28 | class ftp extends ftp_base {
|
---|
| 29 |
|
---|
| 30 | function ftp($verb=FALSE, $le=FALSE) {
|
---|
| 31 | $this->__construct($verb, $le);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | function __construct($verb=FALSE, $le=FALSE) {
|
---|
| 35 | parent::__construct(false, $verb, $le);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | // <!-- --------------------------------------------------------------------------------------- -->
|
---|
| 39 | // <!-- Private functions -->
|
---|
| 40 | // <!-- --------------------------------------------------------------------------------------- -->
|
---|
| 41 |
|
---|
| 42 | function _settimeout($sock) {
|
---|
| 43 | if(!@stream_set_timeout($sock, $this->_timeout)) {
|
---|
| 44 | $this->PushError('_settimeout','socket set send timeout');
|
---|
| 45 | $this->_quit();
|
---|
| 46 | return FALSE;
|
---|
| 47 | }
|
---|
| 48 | return TRUE;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | function _connect($host, $port) {
|
---|
| 52 | $this->SendMSG("Creating socket");
|
---|
| 53 | $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
|
---|
| 54 | if (!$sock) {
|
---|
| 55 | $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
|
---|
| 56 | return FALSE;
|
---|
| 57 | }
|
---|
| 58 | $this->_connected=true;
|
---|
| 59 | return $sock;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | function _readmsg($fnction="_readmsg"){
|
---|
| 63 | if(!$this->_connected) {
|
---|
| 64 | $this->PushError($fnction, 'Connect first');
|
---|
| 65 | return FALSE;
|
---|
| 66 | }
|
---|
| 67 | $result=true;
|
---|
| 68 | $this->_message="";
|
---|
| 69 | $this->_code=0;
|
---|
| 70 | $go=true;
|
---|
| 71 | do {
|
---|
| 72 | $tmp=@fgets($this->_ftp_control_sock, 512);
|
---|
| 73 | if($tmp===false) {
|
---|
| 74 | $go=$result=false;
|
---|
| 75 | $this->PushError($fnction,'Read failed');
|
---|
| 76 | } else {
|
---|
| 77 | $this->_message.=$tmp;
|
---|
| 78 | if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
|
---|
| 79 | }
|
---|
| 80 | } while($go);
|
---|
| 81 | if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
|
---|
| 82 | $this->_code=(int)$regs[1];
|
---|
| 83 | return $result;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | function _exec($cmd, $fnction="_exec") {
|
---|
| 87 | if(!$this->_ready) {
|
---|
| 88 | $this->PushError($fnction,'Connect first');
|
---|
| 89 | return FALSE;
|
---|
| 90 | }
|
---|
| 91 | if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
|
---|
| 92 | $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
|
---|
| 93 | if($status===false) {
|
---|
| 94 | $this->PushError($fnction,'socket write failed');
|
---|
| 95 | return FALSE;
|
---|
| 96 | }
|
---|
| 97 | $this->_lastaction=time();
|
---|
| 98 | if(!$this->_readmsg($fnction)) return FALSE;
|
---|
| 99 | return TRUE;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 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();
|
---|
| 107 | return FALSE;
|
---|
| 108 | }
|
---|
| 109 | if(!$this->_checkCode()) {
|
---|
| 110 | $this->_data_close();
|
---|
| 111 | return FALSE;
|
---|
| 112 | }
|
---|
| 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();
|
---|
| 121 | return FALSE;
|
---|
| 122 | }
|
---|
| 123 | else $this->_ftp_data_sock;
|
---|
| 124 | } else {
|
---|
| 125 | $this->SendMSG("Only passive connections available!");
|
---|
| 126 | return FALSE;
|
---|
| 127 | }
|
---|
| 128 | return TRUE;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | function _data_read($mode=FTP_ASCII, $fp=NULL) {
|
---|
| 132 | if(is_resource($fp)) $out=0;
|
---|
| 133 | else $out="";
|
---|
| 134 | if(!$this->_passive) {
|
---|
| 135 | $this->SendMSG("Only passive connections available!");
|
---|
| 136 | return FALSE;
|
---|
| 137 | }
|
---|
| 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));
|
---|
| 142 | else $out.=$block;
|
---|
| 143 | }
|
---|
| 144 | return $out;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | function _data_write($mode=FTP_ASCII, $fp=NULL) {
|
---|
| 148 | if(is_resource($fp)) $out=0;
|
---|
| 149 | else $out="";
|
---|
| 150 | if(!$this->_passive) {
|
---|
| 151 | $this->SendMSG("Only passive connections available!");
|
---|
| 152 | return FALSE;
|
---|
| 153 | }
|
---|
| 154 | if(is_resource($fp)) {
|
---|
| 155 | while(!feof($fp)) {
|
---|
| 156 | $block=fread($fp, $this->_ftp_buff_size);
|
---|
| 157 | if(!$this->_data_write_block($mode, $block)) return false;
|
---|
| 158 | }
|
---|
| 159 | } elseif(!$this->_data_write_block($mode, $fp)) return false;
|
---|
| 160 | return TRUE;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 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);
|
---|
| 165 | do {
|
---|
| 166 | if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
|
---|
| 167 | $this->PushError("_data_write","Can't write to socket");
|
---|
| 168 | return FALSE;
|
---|
| 169 | }
|
---|
| 170 | $block=substr($block, $t);
|
---|
| 171 | } while(!empty($block));
|
---|
| 172 | return true;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | function _data_close() {
|
---|
| 176 | @fclose($this->_ftp_data_sock);
|
---|
| 177 | $this->SendMSG("Disconnected data from remote host");
|
---|
| 178 | return TRUE;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 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");
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | ?>
|
---|