mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * PemFTP - A Ftp implementation in pure PHP
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package PemFTP
mp-wp_genesis 6 * @since 2.5
mp-wp_genesis 7 *
mp-wp_genesis 8 * @version 1.0
mp-wp_genesis 9 * @copyright Alexey Dotsenko
mp-wp_genesis 10 * @author Alexey Dotsenko
mp-wp_genesis 11 * @link http://www.phpclasses.org/browse/package/1743.html Site
mp-wp_genesis 12 * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
mp-wp_genesis 13 */
mp-wp_genesis 14
mp-wp_genesis 15 /**
mp-wp_genesis 16 * Socket Based FTP implementation
mp-wp_genesis 17 *
mp-wp_genesis 18 * @package PemFTP
mp-wp_genesis 19 * @subpackage Socket
mp-wp_genesis 20 * @since 2.5
mp-wp_genesis 21 *
mp-wp_genesis 22 * @version 1.0
mp-wp_genesis 23 * @copyright Alexey Dotsenko
mp-wp_genesis 24 * @author Alexey Dotsenko
mp-wp_genesis 25 * @link http://www.phpclasses.org/browse/package/1743.html Site
mp-wp_genesis 26 * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
mp-wp_genesis 27 */
mp-wp_genesis 28 class ftp extends ftp_base {
mp-wp_genesis 29
mp-wp_genesis 30 function ftp($verb=FALSE, $le=FALSE) {
mp-wp_genesis 31 $this->__construct($verb, $le);
mp-wp_genesis 32 }
mp-wp_genesis 33
mp-wp_genesis 34 function __construct($verb=FALSE, $le=FALSE) {
mp-wp_genesis 35 parent::__construct(true, $verb, $le);
mp-wp_genesis 36 }
mp-wp_genesis 37
mp-wp_genesis 38 // <!-- --------------------------------------------------------------------------------------- -->
mp-wp_genesis 39 // <!-- Private functions -->
mp-wp_genesis 40 // <!-- --------------------------------------------------------------------------------------- -->
mp-wp_genesis 41
mp-wp_genesis 42 function _settimeout($sock) {
mp-wp_genesis 43 if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
mp-wp_genesis 44 $this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock)));
mp-wp_genesis 45 @socket_close($sock);
mp-wp_genesis 46 return FALSE;
mp-wp_genesis 47 }
mp-wp_genesis 48 if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
mp-wp_genesis 49 $this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock)));
mp-wp_genesis 50 @socket_close($sock);
mp-wp_genesis 51 return FALSE;
mp-wp_genesis 52 }
mp-wp_genesis 53 return true;
mp-wp_genesis 54 }
mp-wp_genesis 55
mp-wp_genesis 56 function _connect($host, $port) {
mp-wp_genesis 57 $this->SendMSG("Creating socket");
mp-wp_genesis 58 if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
mp-wp_genesis 59 $this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock)));
mp-wp_genesis 60 return FALSE;
mp-wp_genesis 61 }
mp-wp_genesis 62 if(!$this->_settimeout($sock)) return FALSE;
mp-wp_genesis 63 $this->SendMSG("Connecting to \"".$host.":".$port."\"");
mp-wp_genesis 64 if (!($res = @socket_connect($sock, $host, $port))) {
mp-wp_genesis 65 $this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock)));
mp-wp_genesis 66 @socket_close($sock);
mp-wp_genesis 67 return FALSE;
mp-wp_genesis 68 }
mp-wp_genesis 69 $this->_connected=true;
mp-wp_genesis 70 return $sock;
mp-wp_genesis 71 }
mp-wp_genesis 72
mp-wp_genesis 73 function _readmsg($fnction="_readmsg"){
mp-wp_genesis 74 if(!$this->_connected) {
mp-wp_genesis 75 $this->PushError($fnction,'Connect first');
mp-wp_genesis 76 return FALSE;
mp-wp_genesis 77 }
mp-wp_genesis 78 $result=true;
mp-wp_genesis 79 $this->_message="";
mp-wp_genesis 80 $this->_code=0;
mp-wp_genesis 81 $go=true;
mp-wp_genesis 82 do {
mp-wp_genesis 83 $tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ);
mp-wp_genesis 84 if($tmp===false) {
mp-wp_genesis 85 $go=$result=false;
mp-wp_genesis 86 $this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock)));
mp-wp_genesis 87 } else {
mp-wp_genesis 88 $this->_message.=$tmp;
mp-wp_genesis 89 $go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs);
mp-wp_genesis 90 }
mp-wp_genesis 91 } while($go);
mp-wp_genesis 92 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
mp-wp_genesis 93 $this->_code=(int)$regs[1];
mp-wp_genesis 94 return $result;
mp-wp_genesis 95 }
mp-wp_genesis 96
mp-wp_genesis 97 function _exec($cmd, $fnction="_exec") {
mp-wp_genesis 98 if(!$this->_ready) {
mp-wp_genesis 99 $this->PushError($fnction,'Connect first');
mp-wp_genesis 100 return FALSE;
mp-wp_genesis 101 }
mp-wp_genesis 102 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
mp-wp_genesis 103 $status=@socket_write($this->_ftp_control_sock, $cmd.CRLF);
mp-wp_genesis 104 if($status===false) {
mp-wp_genesis 105 $this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream)));
mp-wp_genesis 106 return FALSE;
mp-wp_genesis 107 }
mp-wp_genesis 108 $this->_lastaction=time();
mp-wp_genesis 109 if(!$this->_readmsg($fnction)) return FALSE;
mp-wp_genesis 110 return TRUE;
mp-wp_genesis 111 }
mp-wp_genesis 112
mp-wp_genesis 113 function _data_prepare($mode=FTP_ASCII) {
mp-wp_genesis 114 if(!$this->_settype($mode)) return FALSE;
mp-wp_genesis 115 $this->SendMSG("Creating data socket");
mp-wp_genesis 116 $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
mp-wp_genesis 117 if ($this->_ftp_data_sock < 0) {
mp-wp_genesis 118 $this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock)));
mp-wp_genesis 119 return FALSE;
mp-wp_genesis 120 }
mp-wp_genesis 121 if(!$this->_settimeout($this->_ftp_data_sock)) {
mp-wp_genesis 122 $this->_data_close();
mp-wp_genesis 123 return FALSE;
mp-wp_genesis 124 }
mp-wp_genesis 125 if($this->_passive) {
mp-wp_genesis 126 if(!$this->_exec("PASV", "pasv")) {
mp-wp_genesis 127 $this->_data_close();
mp-wp_genesis 128 return FALSE;
mp-wp_genesis 129 }
mp-wp_genesis 130 if(!$this->_checkCode()) {
mp-wp_genesis 131 $this->_data_close();
mp-wp_genesis 132 return FALSE;
mp-wp_genesis 133 }
mp-wp_genesis 134 $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));
mp-wp_genesis 135 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
mp-wp_genesis 136 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
mp-wp_genesis 137 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
mp-wp_genesis 138 if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
mp-wp_genesis 139 $this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock)));
mp-wp_genesis 140 $this->_data_close();
mp-wp_genesis 141 return FALSE;
mp-wp_genesis 142 }
mp-wp_genesis 143 else $this->_ftp_temp_sock=$this->_ftp_data_sock;
mp-wp_genesis 144 } else {
mp-wp_genesis 145 if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) {
mp-wp_genesis 146 $this->PushError("_data_prepare","can't get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock)));
mp-wp_genesis 147 $this->_data_close();
mp-wp_genesis 148 return FALSE;
mp-wp_genesis 149 }
mp-wp_genesis 150 if(!@socket_bind($this->_ftp_data_sock,$addr)){
mp-wp_genesis 151 $this->PushError("_data_prepare","can't bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
mp-wp_genesis 152 $this->_data_close();
mp-wp_genesis 153 return FALSE;
mp-wp_genesis 154 }
mp-wp_genesis 155 if(!@socket_listen($this->_ftp_data_sock)) {
mp-wp_genesis 156 $this->PushError("_data_prepare","can't listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
mp-wp_genesis 157 $this->_data_close();
mp-wp_genesis 158 return FALSE;
mp-wp_genesis 159 }
mp-wp_genesis 160 if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
mp-wp_genesis 161 $this->PushError("_data_prepare","can't get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock)));
mp-wp_genesis 162 $this->_data_close();
mp-wp_genesis 163 return FALSE;
mp-wp_genesis 164 }
mp-wp_genesis 165 if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) {
mp-wp_genesis 166 $this->_data_close();
mp-wp_genesis 167 return FALSE;
mp-wp_genesis 168 }
mp-wp_genesis 169 if(!$this->_checkCode()) {
mp-wp_genesis 170 $this->_data_close();
mp-wp_genesis 171 return FALSE;
mp-wp_genesis 172 }
mp-wp_genesis 173 }
mp-wp_genesis 174 return TRUE;
mp-wp_genesis 175 }
mp-wp_genesis 176
mp-wp_genesis 177 function _data_read($mode=FTP_ASCII, $fp=NULL) {
mp-wp_genesis 178 $NewLine=$this->_eol_code[$this->OS_local];
mp-wp_genesis 179 if(is_resource($fp)) $out=0;
mp-wp_genesis 180 else $out="";
mp-wp_genesis 181 if(!$this->_passive) {
mp-wp_genesis 182 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
mp-wp_genesis 183 $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
mp-wp_genesis 184 if($this->_ftp_temp_sock===FALSE) {
mp-wp_genesis 185 $this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
mp-wp_genesis 186 $this->_data_close();
mp-wp_genesis 187 return FALSE;
mp-wp_genesis 188 }
mp-wp_genesis 189 }
mp-wp_genesis 190
mp-wp_genesis 191 while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) {
mp-wp_genesis 192 if($block==="") break;
mp-wp_genesis 193 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
mp-wp_genesis 194 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
mp-wp_genesis 195 else $out.=$block;
mp-wp_genesis 196 }
mp-wp_genesis 197 return $out;
mp-wp_genesis 198 }
mp-wp_genesis 199
mp-wp_genesis 200 function _data_write($mode=FTP_ASCII, $fp=NULL) {
mp-wp_genesis 201 $NewLine=$this->_eol_code[$this->OS_local];
mp-wp_genesis 202 if(is_resource($fp)) $out=0;
mp-wp_genesis 203 else $out="";
mp-wp_genesis 204 if(!$this->_passive) {
mp-wp_genesis 205 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
mp-wp_genesis 206 $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
mp-wp_genesis 207 if($this->_ftp_temp_sock===FALSE) {
mp-wp_genesis 208 $this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
mp-wp_genesis 209 $this->_data_close();
mp-wp_genesis 210 return false;
mp-wp_genesis 211 }
mp-wp_genesis 212 }
mp-wp_genesis 213 if(is_resource($fp)) {
mp-wp_genesis 214 while(!feof($fp)) {
mp-wp_genesis 215 $block=fread($fp, $this->_ftp_buff_size);
mp-wp_genesis 216 if(!$this->_data_write_block($mode, $block)) return false;
mp-wp_genesis 217 }
mp-wp_genesis 218 } elseif(!$this->_data_write_block($mode, $fp)) return false;
mp-wp_genesis 219 return true;
mp-wp_genesis 220 }
mp-wp_genesis 221
mp-wp_genesis 222 function _data_write_block($mode, $block) {
mp-wp_genesis 223 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
mp-wp_genesis 224 do {
mp-wp_genesis 225 if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) {
mp-wp_genesis 226 $this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
mp-wp_genesis 227 $this->_data_close();
mp-wp_genesis 228 return FALSE;
mp-wp_genesis 229 }
mp-wp_genesis 230 $block=substr($block, $t);
mp-wp_genesis 231 } while(!empty($block));
mp-wp_genesis 232 return true;
mp-wp_genesis 233 }
mp-wp_genesis 234
mp-wp_genesis 235 function _data_close() {
mp-wp_genesis 236 @socket_close($this->_ftp_temp_sock);
mp-wp_genesis 237 @socket_close($this->_ftp_data_sock);
mp-wp_genesis 238 $this->SendMSG("Disconnected data from remote host");
mp-wp_genesis 239 return TRUE;
mp-wp_genesis 240 }
mp-wp_genesis 241
mp-wp_genesis 242 function _quit() {
mp-wp_genesis 243 if($this->_connected) {
mp-wp_genesis 244 @socket_close($this->_ftp_control_sock);
mp-wp_genesis 245 $this->_connected=false;
mp-wp_genesis 246 $this->SendMSG("Socket closed");
mp-wp_genesis 247 }
mp-wp_genesis 248 }
mp-wp_genesis 249 }
mp-wp_genesis 250 ?>