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 * FTP implementation using fsockopen to connect.
mp-wp_genesis 17 *
mp-wp_genesis 18 * @package PemFTP
mp-wp_genesis 19 * @subpackage Pure
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(false, $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(!@stream_set_timeout($sock, $this->_timeout)) {
mp-wp_genesis 44 $this->PushError('_settimeout','socket set send timeout');
mp-wp_genesis 45 $this->_quit();
mp-wp_genesis 46 return FALSE;
mp-wp_genesis 47 }
mp-wp_genesis 48 return TRUE;
mp-wp_genesis 49 }
mp-wp_genesis 50
mp-wp_genesis 51 function _connect($host, $port) {
mp-wp_genesis 52 $this->SendMSG("Creating socket");
mp-wp_genesis 53 $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
mp-wp_genesis 54 if (!$sock) {
mp-wp_genesis 55 $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
mp-wp_genesis 56 return FALSE;
mp-wp_genesis 57 }
mp-wp_genesis 58 $this->_connected=true;
mp-wp_genesis 59 return $sock;
mp-wp_genesis 60 }
mp-wp_genesis 61
mp-wp_genesis 62 function _readmsg($fnction="_readmsg"){
mp-wp_genesis 63 if(!$this->_connected) {
mp-wp_genesis 64 $this->PushError($fnction, 'Connect first');
mp-wp_genesis 65 return FALSE;
mp-wp_genesis 66 }
mp-wp_genesis 67 $result=true;
mp-wp_genesis 68 $this->_message="";
mp-wp_genesis 69 $this->_code=0;
mp-wp_genesis 70 $go=true;
mp-wp_genesis 71 do {
mp-wp_genesis 72 $tmp=@fgets($this->_ftp_control_sock, 512);
mp-wp_genesis 73 if($tmp===false) {
mp-wp_genesis 74 $go=$result=false;
mp-wp_genesis 75 $this->PushError($fnction,'Read failed');
mp-wp_genesis 76 } else {
mp-wp_genesis 77 $this->_message.=$tmp;
mp-wp_genesis 78 if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
mp-wp_genesis 79 }
mp-wp_genesis 80 } while($go);
mp-wp_genesis 81 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
mp-wp_genesis 82 $this->_code=(int)$regs[1];
mp-wp_genesis 83 return $result;
mp-wp_genesis 84 }
mp-wp_genesis 85
mp-wp_genesis 86 function _exec($cmd, $fnction="_exec") {
mp-wp_genesis 87 if(!$this->_ready) {
mp-wp_genesis 88 $this->PushError($fnction,'Connect first');
mp-wp_genesis 89 return FALSE;
mp-wp_genesis 90 }
mp-wp_genesis 91 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
mp-wp_genesis 92 $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
mp-wp_genesis 93 if($status===false) {
mp-wp_genesis 94 $this->PushError($fnction,'socket write failed');
mp-wp_genesis 95 return FALSE;
mp-wp_genesis 96 }
mp-wp_genesis 97 $this->_lastaction=time();
mp-wp_genesis 98 if(!$this->_readmsg($fnction)) return FALSE;
mp-wp_genesis 99 return TRUE;
mp-wp_genesis 100 }
mp-wp_genesis 101
mp-wp_genesis 102 function _data_prepare($mode=FTP_ASCII) {
mp-wp_genesis 103 if(!$this->_settype($mode)) return FALSE;
mp-wp_genesis 104 if($this->_passive) {
mp-wp_genesis 105 if(!$this->_exec("PASV", "pasv")) {
mp-wp_genesis 106 $this->_data_close();
mp-wp_genesis 107 return FALSE;
mp-wp_genesis 108 }
mp-wp_genesis 109 if(!$this->_checkCode()) {
mp-wp_genesis 110 $this->_data_close();
mp-wp_genesis 111 return FALSE;
mp-wp_genesis 112 }
mp-wp_genesis 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));
mp-wp_genesis 114 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
mp-wp_genesis 115 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
mp-wp_genesis 116 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
mp-wp_genesis 117 $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout);
mp-wp_genesis 118 if(!$this->_ftp_data_sock) {
mp-wp_genesis 119 $this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")");
mp-wp_genesis 120 $this->_data_close();
mp-wp_genesis 121 return FALSE;
mp-wp_genesis 122 }
mp-wp_genesis 123 else $this->_ftp_data_sock;
mp-wp_genesis 124 } else {
mp-wp_genesis 125 $this->SendMSG("Only passive connections available!");
mp-wp_genesis 126 return FALSE;
mp-wp_genesis 127 }
mp-wp_genesis 128 return TRUE;
mp-wp_genesis 129 }
mp-wp_genesis 130
mp-wp_genesis 131 function _data_read($mode=FTP_ASCII, $fp=NULL) {
mp-wp_genesis 132 if(is_resource($fp)) $out=0;
mp-wp_genesis 133 else $out="";
mp-wp_genesis 134 if(!$this->_passive) {
mp-wp_genesis 135 $this->SendMSG("Only passive connections available!");
mp-wp_genesis 136 return FALSE;
mp-wp_genesis 137 }
mp-wp_genesis 138 while (!feof($this->_ftp_data_sock)) {
mp-wp_genesis 139 $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size);
mp-wp_genesis 140 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
mp-wp_genesis 141 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
mp-wp_genesis 142 else $out.=$block;
mp-wp_genesis 143 }
mp-wp_genesis 144 return $out;
mp-wp_genesis 145 }
mp-wp_genesis 146
mp-wp_genesis 147 function _data_write($mode=FTP_ASCII, $fp=NULL) {
mp-wp_genesis 148 if(is_resource($fp)) $out=0;
mp-wp_genesis 149 else $out="";
mp-wp_genesis 150 if(!$this->_passive) {
mp-wp_genesis 151 $this->SendMSG("Only passive connections available!");
mp-wp_genesis 152 return FALSE;
mp-wp_genesis 153 }
mp-wp_genesis 154 if(is_resource($fp)) {
mp-wp_genesis 155 while(!feof($fp)) {
mp-wp_genesis 156 $block=fread($fp, $this->_ftp_buff_size);
mp-wp_genesis 157 if(!$this->_data_write_block($mode, $block)) return false;
mp-wp_genesis 158 }
mp-wp_genesis 159 } elseif(!$this->_data_write_block($mode, $fp)) return false;
mp-wp_genesis 160 return TRUE;
mp-wp_genesis 161 }
mp-wp_genesis 162
mp-wp_genesis 163 function _data_write_block($mode, $block) {
mp-wp_genesis 164 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
mp-wp_genesis 165 do {
mp-wp_genesis 166 if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
mp-wp_genesis 167 $this->PushError("_data_write","Can't write to socket");
mp-wp_genesis 168 return FALSE;
mp-wp_genesis 169 }
mp-wp_genesis 170 $block=substr($block, $t);
mp-wp_genesis 171 } while(!empty($block));
mp-wp_genesis 172 return true;
mp-wp_genesis 173 }
mp-wp_genesis 174
mp-wp_genesis 175 function _data_close() {
mp-wp_genesis 176 @fclose($this->_ftp_data_sock);
mp-wp_genesis 177 $this->SendMSG("Disconnected data from remote host");
mp-wp_genesis 178 return TRUE;
mp-wp_genesis 179 }
mp-wp_genesis 180
mp-wp_genesis 181 function _quit($force=FALSE) {
mp-wp_genesis 182 if($this->_connected or $force) {
mp-wp_genesis 183 @fclose($this->_ftp_control_sock);
mp-wp_genesis 184 $this->_connected=false;
mp-wp_genesis 185 $this->SendMSG("Socket closed");
mp-wp_genesis 186 }
mp-wp_genesis 187 }
mp-wp_genesis 188 }
mp-wp_genesis 189
mp-wp_genesis 190 ?>