]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-ftp-pure.php
Wordpress 2.5.1-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-ftp-pure.php
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 class ftp extends ftp_base {
15
16         function ftp($verb=FALSE, $le=FALSE) {
17                 $this->__construct($verb, $le);
18         }
19
20         function __construct($verb=FALSE, $le=FALSE) {
21                 parent::__construct(false, $verb, $le);
22         }
23
24 // <!-- --------------------------------------------------------------------------------------- -->
25 // <!--       Private functions                                                                 -->
26 // <!-- --------------------------------------------------------------------------------------- -->
27
28         function _settimeout($sock) {
29                 if(!@stream_set_timeout($sock, $this->_timeout)) {
30                         $this->PushError('_settimeout','socket set send timeout');
31                         $this->_quit();
32                         return FALSE;
33                 }
34                 return TRUE;
35         }
36
37         function _connect($host, $port) {
38                 $this->SendMSG("Creating socket");
39                 $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
40                 if (!$sock) {
41                         $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
42                         return FALSE;
43                 }
44                 $this->_connected=true;
45                 return $sock;
46         }
47
48         function _readmsg($fnction="_readmsg"){
49                 if(!$this->_connected) {
50                         $this->PushError($fnction, 'Connect first');
51                         return FALSE;
52                 }
53                 $result=true;
54                 $this->_message="";
55                 $this->_code=0;
56                 $go=true;
57                 do {
58                         $tmp=@fgets($this->_ftp_control_sock, 512);
59                         if($tmp===false) {
60                                 $go=$result=false;
61                                 $this->PushError($fnction,'Read failed');
62                         } else {
63                                 $this->_message.=$tmp;
64                                 if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
65                         }
66                 } while($go);
67                 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
68                 $this->_code=(int)$regs[1];
69                 return $result;
70         }
71
72         function _exec($cmd, $fnction="_exec") {
73                 if(!$this->_ready) {
74                         $this->PushError($fnction,'Connect first');
75                         return FALSE;
76                 }
77                 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
78                 $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
79                 if($status===false) {
80                         $this->PushError($fnction,'socket write failed');
81                         return FALSE;
82                 }
83                 $this->_lastaction=time();
84                 if(!$this->_readmsg($fnction)) return FALSE;
85                 return TRUE;
86         }
87
88         function _data_prepare($mode=FTP_ASCII) {
89                 if(!$this->_settype($mode)) return FALSE;
90                 if($this->_passive) {
91                         if(!$this->_exec("PASV", "pasv")) {
92                                 $this->_data_close();
93                                 return FALSE;
94                         }
95                         if(!$this->_checkCode()) {
96                                 $this->_data_close();
97                                 return FALSE;
98                         }
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();
107                                 return FALSE;
108                         }
109                         else $this->_ftp_data_sock;
110                 } else {
111                         $this->SendMSG("Only passive connections available!");
112                         return FALSE;
113                 }
114                 return TRUE;
115         }
116
117         function _data_read($mode=FTP_ASCII, $fp=NULL) {
118                 if(is_resource($fp)) $out=0;
119                 else $out="";
120                 if(!$this->_passive) {
121                         $this->SendMSG("Only passive connections available!");
122                         return FALSE;
123                 }
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));
128                         else $out.=$block;
129                 }
130                 return $out;
131         }
132
133         function _data_write($mode=FTP_ASCII, $fp=NULL) {
134                 if(is_resource($fp)) $out=0;
135                 else $out="";
136                 if(!$this->_passive) {
137                         $this->SendMSG("Only passive connections available!");
138                         return FALSE;
139                 }
140                 if(is_resource($fp)) {
141                         while(!feof($fp)) {
142                                 $block=fread($fp, $this->_ftp_buff_size);
143                                 if(!$this->_data_write_block($mode, $block)) return false;
144                         }
145                 } elseif(!$this->_data_write_block($mode, $fp)) return false;
146                 return TRUE;
147         }
148
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);
151                 do {
152                         if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
153                                 $this->PushError("_data_write","Can't write to socket");
154                                 return FALSE;
155                         }
156                         $block=substr($block, $t);
157                 } while(!empty($block));
158                 return true;
159         }
160
161         function _data_close() {
162                 @fclose($this->_ftp_data_sock);
163                 $this->SendMSG("Disconnected data from remote host");
164                 return TRUE;
165         }
166
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");
172                 }
173         }
174 }
175 ?>