]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-ftp.php
Wordpress 2.8.2
[autoinstalls/wordpress.git] / wp-admin / includes / class-ftp.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
15 /**
16  * Defines the newline characters, if not defined already.
17  *
18  * This can be redefined.
19  *
20  * @since 2.5
21  * @var string
22  */
23 if(!defined('CRLF')) define('CRLF',"\r\n");
24
25 /**
26  * Sets whatever to autodetect ASCII mode.
27  *
28  * This can be redefined.
29  *
30  * @since 2.5
31  * @var int
32  */
33 if(!defined("FTP_AUTOASCII")) define("FTP_AUTOASCII", -1);
34
35 /**
36  *
37  * This can be redefined.
38  * @since 2.5
39  * @var int
40  */
41 if(!defined("FTP_BINARY")) define("FTP_BINARY", 1);
42
43 /**
44  *
45  * This can be redefined.
46  * @since 2.5
47  * @var int
48  */
49 if(!defined("FTP_ASCII")) define("FTP_ASCII", 0);
50
51 /**
52  * Whether to force FTP.
53  *
54  * This can be redefined.
55  *
56  * @since 2.5
57  * @var bool
58  */
59 if(!defined('FTP_FORCE')) define('FTP_FORCE', true);
60
61 /**
62  * @since 2.5
63  * @var string
64  */
65 define('FTP_OS_Unix','u');
66
67 /**
68  * @since 2.5
69  * @var string
70  */
71 define('FTP_OS_Windows','w');
72
73 /**
74  * @since 2.5
75  * @var string
76  */
77 define('FTP_OS_Mac','m');
78
79 /**
80  * PemFTP base class
81  *
82  */
83 class ftp_base {
84         /* Public variables */
85         var $LocalEcho;
86         var $Verbose;
87         var $OS_local;
88         var $OS_remote;
89
90         /* Private variables */
91         var $_lastaction;
92         var $_errors;
93         var $_type;
94         var $_umask;
95         var $_timeout;
96         var $_passive;
97         var $_host;
98         var $_fullhost;
99         var $_port;
100         var $_datahost;
101         var $_dataport;
102         var $_ftp_control_sock;
103         var $_ftp_data_sock;
104         var $_ftp_temp_sock;
105         var $_ftp_buff_size;
106         var $_login;
107         var $_password;
108         var $_connected;
109         var $_ready;
110         var $_code;
111         var $_message;
112         var $_can_restore;
113         var $_port_available;
114         var $_curtype;
115         var $_features;
116
117         var $_error_array;
118         var $AuthorizedTransferMode;
119         var $OS_FullName;
120         var $_eol_code;
121         var $AutoAsciiExt;
122
123         /* Constructor */
124         function ftp_base($port_mode=FALSE) {
125                 $this->__construct($port_mode);
126         }
127
128         function __construct($port_mode=FALSE, $verb=FALSE, $le=FALSE) {
129                 $this->LocalEcho=$le;
130                 $this->Verbose=$verb;
131                 $this->_lastaction=NULL;
132                 $this->_error_array=array();
133                 $this->_eol_code=array(FTP_OS_Unix=>"\n", FTP_OS_Mac=>"\r", FTP_OS_Windows=>"\r\n");
134                 $this->AuthorizedTransferMode=array(FTP_AUTOASCII, FTP_ASCII, FTP_BINARY);
135                 $this->OS_FullName=array(FTP_OS_Unix => 'UNIX', FTP_OS_Windows => 'WINDOWS', FTP_OS_Mac => 'MACOS');
136                 $this->AutoAsciiExt=array("ASP","BAT","C","CPP","CSS","CSV","JS","H","HTM","HTML","SHTML","INI","LOG","PHP3","PHTML","PL","PERL","SH","SQL","TXT");
137                 $this->_port_available=($port_mode==TRUE);
138                 $this->SendMSG("Staring FTP client class".($this->_port_available?"":" without PORT mode support"));
139                 $this->_connected=FALSE;
140                 $this->_ready=FALSE;
141                 $this->_can_restore=FALSE;
142                 $this->_code=0;
143                 $this->_message="";
144                 $this->_ftp_buff_size=4096;
145                 $this->_curtype=NULL;
146                 $this->SetUmask(0022);
147                 $this->SetType(FTP_AUTOASCII);
148                 $this->SetTimeout(30);
149                 $this->Passive(!$this->_port_available);
150                 $this->_login="anonymous";
151                 $this->_password="anon@ftp.com";
152                 $this->_features=array();
153             $this->OS_local=FTP_OS_Unix;
154                 $this->OS_remote=FTP_OS_Unix;
155                 $this->features=array();
156                 if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $this->OS_local=FTP_OS_Windows;
157                 elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'MAC') $this->OS_local=FTP_OS_Mac;
158         }
159
160 // <!-- --------------------------------------------------------------------------------------- -->
161 // <!--       Public functions                                                                  -->
162 // <!-- --------------------------------------------------------------------------------------- -->
163
164         function parselisting($line) {
165                 $is_windows = ($this->OS_remote == FTP_OS_Windows);
166                 if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/",$line,$lucifer)) {
167                         $b = array();
168                         if ($lucifer[3]<70) { $lucifer[3]+=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
169                         $b['isdir'] = ($lucifer[7]=="<DIR>");
170                         if ( $b['isdir'] )
171                                 $b['type'] = 'd';
172                         else
173                                 $b['type'] = 'f';
174                         $b['size'] = $lucifer[7];
175                         $b['month'] = $lucifer[1];
176                         $b['day'] = $lucifer[2];
177                         $b['year'] = $lucifer[3];
178                         $b['hour'] = $lucifer[4];
179                         $b['minute'] = $lucifer[5];
180                         $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
181                         $b['am/pm'] = $lucifer[6];
182                         $b['name'] = $lucifer[8];
183                 } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
184                         //echo $line."\n";
185                         $lcount=count($lucifer);
186                         if ($lcount<8) return '';
187                         $b = array();
188                         $b['isdir'] = $lucifer[0]{0} === "d";
189                         $b['islink'] = $lucifer[0]{0} === "l";
190                         if ( $b['isdir'] )
191                                 $b['type'] = 'd';
192                         elseif ( $b['islink'] )
193                                 $b['type'] = 'l';
194                         else
195                                 $b['type'] = 'f';
196                         $b['perms'] = $lucifer[0];
197                         $b['number'] = $lucifer[1];
198                         $b['owner'] = $lucifer[2];
199                         $b['group'] = $lucifer[3];
200                         $b['size'] = $lucifer[4];
201                         if ($lcount==8) {
202                                 sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
203                                 sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
204                                 $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
205                                 $b['name'] = $lucifer[7];
206                         } else {
207                                 $b['month'] = $lucifer[5];
208                                 $b['day'] = $lucifer[6];
209                                 if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
210                                         $b['year'] = date("Y");
211                                         $b['hour'] = $l2[1];
212                                         $b['minute'] = $l2[2];
213                                 } else {
214                                         $b['year'] = $lucifer[7];
215                                         $b['hour'] = 0;
216                                         $b['minute'] = 0;
217                                 }
218                                 $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
219                                 $b['name'] = $lucifer[8];
220                         }
221                 }
222
223                 return $b;
224         }
225
226         function SendMSG($message = "", $crlf=true) {
227                 if ($this->Verbose) {
228                         echo $message.($crlf?CRLF:"");
229                         flush();
230                 }
231                 return TRUE;
232         }
233
234         function SetType($mode=FTP_AUTOASCII) {
235                 if(!in_array($mode, $this->AuthorizedTransferMode)) {
236                         $this->SendMSG("Wrong type");
237                         return FALSE;
238                 }
239                 $this->_type=$mode;
240                 $this->SendMSG("Transfer type: ".($this->_type==FTP_BINARY?"binary":($this->_type==FTP_ASCII?"ASCII":"auto ASCII") ) );
241                 return TRUE;
242         }
243
244         function _settype($mode=FTP_ASCII) {
245                 if($this->_ready) {
246                         if($mode==FTP_BINARY) {
247                                 if($this->_curtype!=FTP_BINARY) {
248                                         if(!$this->_exec("TYPE I", "SetType")) return FALSE;
249                                         $this->_curtype=FTP_BINARY;
250                                 }
251                         } elseif($this->_curtype!=FTP_ASCII) {
252                                 if(!$this->_exec("TYPE A", "SetType")) return FALSE;
253                                 $this->_curtype=FTP_ASCII;
254                         }
255                 } else return FALSE;
256                 return TRUE;
257         }
258
259         function Passive($pasv=NULL) {
260                 if(is_null($pasv)) $this->_passive=!$this->_passive;
261                 else $this->_passive=$pasv;
262                 if(!$this->_port_available and !$this->_passive) {
263                         $this->SendMSG("Only passive connections available!");
264                         $this->_passive=TRUE;
265                         return FALSE;
266                 }
267                 $this->SendMSG("Passive mode ".($this->_passive?"on":"off"));
268                 return TRUE;
269         }
270
271         function SetServer($host, $port=21, $reconnect=true) {
272                 if(!is_long($port)) {
273                 $this->verbose=true;
274             $this->SendMSG("Incorrect port syntax");
275                         return FALSE;
276                 } else {
277                         $ip=@gethostbyname($host);
278                 $dns=@gethostbyaddr($host);
279                 if(!$ip) $ip=$host;
280                 if(!$dns) $dns=$host;
281                         if(ip2long($ip) === -1) {
282                                 $this->SendMSG("Wrong host name/address \"".$host."\"");
283                                 return FALSE;
284                         }
285                 $this->_host=$ip;
286                 $this->_fullhost=$dns;
287                 $this->_port=$port;
288                 $this->_dataport=$port-1;
289                 }
290                 $this->SendMSG("Host \"".$this->_fullhost."(".$this->_host."):".$this->_port."\"");
291                 if($reconnect){
292                         if($this->_connected) {
293                                 $this->SendMSG("Reconnecting");
294                                 if(!$this->quit(FTP_FORCE)) return FALSE;
295                                 if(!$this->connect()) return FALSE;
296                         }
297                 }
298                 return TRUE;
299         }
300
301         function SetUmask($umask=0022) {
302                 $this->_umask=$umask;
303                 umask($this->_umask);
304                 $this->SendMSG("UMASK 0".decoct($this->_umask));
305                 return TRUE;
306         }
307
308         function SetTimeout($timeout=30) {
309                 $this->_timeout=$timeout;
310                 $this->SendMSG("Timeout ".$this->_timeout);
311                 if($this->_connected)
312                         if(!$this->_settimeout($this->_ftp_control_sock)) return FALSE;
313                 return TRUE;
314         }
315
316         function connect($server=NULL) {
317                 if(!empty($server)) {
318                         if(!$this->SetServer($server)) return false;
319                 }
320                 if($this->_ready) return true;
321             $this->SendMsg('Local OS : '.$this->OS_FullName[$this->OS_local]);
322                 if(!($this->_ftp_control_sock = $this->_connect($this->_host, $this->_port))) {
323                         $this->SendMSG("Error : Cannot connect to remote host \"".$this->_fullhost." :".$this->_port."\"");
324                         return FALSE;
325                 }
326                 $this->SendMSG("Connected to remote host \"".$this->_fullhost.":".$this->_port."\". Waiting for greeting.");
327                 do {
328                         if(!$this->_readmsg()) return FALSE;
329                         if(!$this->_checkCode()) return FALSE;
330                         $this->_lastaction=time();
331                 } while($this->_code<200);
332                 $this->_ready=true;
333                 $syst=$this->systype();
334                 if(!$syst) $this->SendMSG("Can't detect remote OS");
335                 else {
336                         if(preg_match("/win|dos|novell/i", $syst[0])) $this->OS_remote=FTP_OS_Windows;
337                         elseif(preg_match("/os/i", $syst[0])) $this->OS_remote=FTP_OS_Mac;
338                         elseif(preg_match("/(li|u)nix/i", $syst[0])) $this->OS_remote=FTP_OS_Unix;
339                         else $this->OS_remote=FTP_OS_Mac;
340                         $this->SendMSG("Remote OS: ".$this->OS_FullName[$this->OS_remote]);
341                 }
342                 if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
343                 else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
344                 return TRUE;
345         }
346
347         function quit($force=false) {
348                 if($this->_ready) {
349                         if(!$this->_exec("QUIT") and !$force) return FALSE;
350                         if(!$this->_checkCode() and !$force) return FALSE;
351                         $this->_ready=false;
352                         $this->SendMSG("Session finished");
353                 }
354                 $this->_quit();
355                 return TRUE;
356         }
357
358         function login($user=NULL, $pass=NULL) {
359                 if(!is_null($user)) $this->_login=$user;
360                 else $this->_login="anonymous";
361                 if(!is_null($pass)) $this->_password=$pass;
362                 else $this->_password="anon@anon.com";
363                 if(!$this->_exec("USER ".$this->_login, "login")) return FALSE;
364                 if(!$this->_checkCode()) return FALSE;
365                 if($this->_code!=230) {
366                         if(!$this->_exec((($this->_code==331)?"PASS ":"ACCT ").$this->_password, "login")) return FALSE;
367                         if(!$this->_checkCode()) return FALSE;
368                 }
369                 $this->SendMSG("Authentication succeeded");
370                 if(empty($this->_features)) {
371                         if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
372                         else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
373                 }
374                 return TRUE;
375         }
376
377         function pwd() {
378                 if(!$this->_exec("PWD", "pwd")) return FALSE;
379                 if(!$this->_checkCode()) return FALSE;
380                 return ereg_replace("^[0-9]{3} \"(.+)\".+", "\\1", $this->_message);
381         }
382
383         function cdup() {
384                 if(!$this->_exec("CDUP", "cdup")) return FALSE;
385                 if(!$this->_checkCode()) return FALSE;
386                 return true;
387         }
388
389         function chdir($pathname) {
390                 if(!$this->_exec("CWD ".$pathname, "chdir")) return FALSE;
391                 if(!$this->_checkCode()) return FALSE;
392                 return TRUE;
393         }
394
395         function rmdir($pathname) {
396                 if(!$this->_exec("RMD ".$pathname, "rmdir")) return FALSE;
397                 if(!$this->_checkCode()) return FALSE;
398                 return TRUE;
399         }
400
401         function mkdir($pathname) {
402                 if(!$this->_exec("MKD ".$pathname, "mkdir")) return FALSE;
403                 if(!$this->_checkCode()) return FALSE;
404                 return TRUE;
405         }
406
407         function rename($from, $to) {
408                 if(!$this->_exec("RNFR ".$from, "rename")) return FALSE;
409                 if(!$this->_checkCode()) return FALSE;
410                 if($this->_code==350) {
411                         if(!$this->_exec("RNTO ".$to, "rename")) return FALSE;
412                         if(!$this->_checkCode()) return FALSE;
413                 } else return FALSE;
414                 return TRUE;
415         }
416
417         function filesize($pathname) {
418                 if(!isset($this->_features["SIZE"])) {
419                         $this->PushError("filesize", "not supported by server");
420                         return FALSE;
421                 }
422                 if(!$this->_exec("SIZE ".$pathname, "filesize")) return FALSE;
423                 if(!$this->_checkCode()) return FALSE;
424                 return ereg_replace("^[0-9]{3} ([0-9]+)".CRLF, "\\1", $this->_message);
425         }
426
427         function abort() {
428                 if(!$this->_exec("ABOR", "abort")) return FALSE;
429                 if(!$this->_checkCode()) {
430                         if($this->_code!=426) return FALSE;
431                         if(!$this->_readmsg("abort")) return FALSE;
432                         if(!$this->_checkCode()) return FALSE;
433                 }
434                 return true;
435         }
436
437         function mdtm($pathname) {
438                 if(!isset($this->_features["MDTM"])) {
439                         $this->PushError("mdtm", "not supported by server");
440                         return FALSE;
441                 }
442                 if(!$this->_exec("MDTM ".$pathname, "mdtm")) return FALSE;
443                 if(!$this->_checkCode()) return FALSE;
444                 $mdtm = ereg_replace("^[0-9]{3} ([0-9]+)".CRLF, "\\1", $this->_message);
445                 $date = sscanf($mdtm, "%4d%2d%2d%2d%2d%2d");
446                 $timestamp = mktime($date[3], $date[4], $date[5], $date[1], $date[2], $date[0]);
447                 return $timestamp;
448         }
449
450         function systype() {
451                 if(!$this->_exec("SYST", "systype")) return FALSE;
452                 if(!$this->_checkCode()) return FALSE;
453                 $DATA = explode(" ", $this->_message);
454                 return array($DATA[1], $DATA[3]);
455         }
456
457         function delete($pathname) {
458                 if(!$this->_exec("DELE ".$pathname, "delete")) return FALSE;
459                 if(!$this->_checkCode()) return FALSE;
460                 return TRUE;
461         }
462
463         function site($command, $fnction="site") {
464                 if(!$this->_exec("SITE ".$command, $fnction)) return FALSE;
465                 if(!$this->_checkCode()) return FALSE;
466                 return TRUE;
467         }
468
469         function chmod($pathname, $mode) {
470                 if(!$this->site( sprintf('CHMOD %o %s', $mode, $pathname), "chmod")) return FALSE;
471                 return TRUE;
472         }
473
474         function restore($from) {
475                 if(!isset($this->_features["REST"])) {
476                         $this->PushError("restore", "not supported by server");
477                         return FALSE;
478                 }
479                 if($this->_curtype!=FTP_BINARY) {
480                         $this->PushError("restore", "can't restore in ASCII mode");
481                         return FALSE;
482                 }
483                 if(!$this->_exec("REST ".$from, "resore")) return FALSE;
484                 if(!$this->_checkCode()) return FALSE;
485                 return TRUE;
486         }
487
488         function features() {
489                 if(!$this->_exec("FEAT", "features")) return FALSE;
490                 if(!$this->_checkCode()) return FALSE;
491                 $f=preg_split("/[".CRLF."]+/", preg_replace("/[0-9]{3}[ -].*[".CRLF."]+/", "", $this->_message), -1, PREG_SPLIT_NO_EMPTY);
492                 $this->_features=array();
493                 foreach($f as $k=>$v) {
494                         $v=explode(" ", trim($v));
495                         $this->_features[array_shift($v)]=$v;;
496                 }
497                 return true;
498         }
499
500         function rawlist($pathname="", $arg="") {
501                 return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "LIST", "rawlist");
502         }
503
504         function nlist($pathname="") {
505                 return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "NLST", "nlist");
506         }
507
508         function is_exists($pathname) {
509                 return $this->file_exists($pathname);
510         }
511
512         function file_exists($pathname) {
513                 $exists=true;
514                 if(!$this->_exec("RNFR ".$pathname, "rename")) $exists=FALSE;
515                 else {
516                         if(!$this->_checkCode()) $exists=FALSE;
517                         $this->abort();
518                 }
519                 if($exists) $this->SendMSG("Remote file ".$pathname." exists");
520                 else $this->SendMSG("Remote file ".$pathname." does not exist");
521                 return $exists;
522         }
523
524         function fget($fp, $remotefile,$rest=0) {
525                 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
526                 $pi=pathinfo($remotefile);
527                 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
528                 else $mode=FTP_BINARY;
529                 if(!$this->_data_prepare($mode)) {
530                         return FALSE;
531                 }
532                 if($this->_can_restore and $rest!=0) $this->restore($rest);
533                 if(!$this->_exec("RETR ".$remotefile, "get")) {
534                         $this->_data_close();
535                         return FALSE;
536                 }
537                 if(!$this->_checkCode()) {
538                         $this->_data_close();
539                         return FALSE;
540                 }
541                 $out=$this->_data_read($mode, $fp);
542                 $this->_data_close();
543                 if(!$this->_readmsg()) return FALSE;
544                 if(!$this->_checkCode()) return FALSE;
545                 return $out;
546         }
547
548         function get($remotefile, $localfile=NULL, $rest=0) {
549                 if(is_null($localfile)) $localfile=$remotefile;
550                 if (@file_exists($localfile)) $this->SendMSG("Warning : local file will be overwritten");
551                 $fp = @fopen($localfile, "w");
552                 if (!$fp) {
553                         $this->PushError("get","can't open local file", "Cannot create \"".$localfile."\"");
554                         return FALSE;
555                 }
556                 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
557                 $pi=pathinfo($remotefile);
558                 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
559                 else $mode=FTP_BINARY;
560                 if(!$this->_data_prepare($mode)) {
561                         fclose($fp);
562                         return FALSE;
563                 }
564                 if($this->_can_restore and $rest!=0) $this->restore($rest);
565                 if(!$this->_exec("RETR ".$remotefile, "get")) {
566                         $this->_data_close();
567                         fclose($fp);
568                         return FALSE;
569                 }
570                 if(!$this->_checkCode()) {
571                         $this->_data_close();
572                         fclose($fp);
573                         return FALSE;
574                 }
575                 $out=$this->_data_read($mode, $fp);
576                 fclose($fp);
577                 $this->_data_close();
578                 if(!$this->_readmsg()) return FALSE;
579                 if(!$this->_checkCode()) return FALSE;
580                 return $out;
581         }
582
583         function fput($remotefile, $fp) {
584                 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
585                 $pi=pathinfo($remotefile);
586                 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
587                 else $mode=FTP_BINARY;
588                 if(!$this->_data_prepare($mode)) {
589                         return FALSE;
590                 }
591                 if($this->_can_restore and $rest!=0) $this->restore($rest);
592                 if(!$this->_exec("STOR ".$remotefile, "put")) {
593                         $this->_data_close();
594                         return FALSE;
595                 }
596                 if(!$this->_checkCode()) {
597                         $this->_data_close();
598                         return FALSE;
599                 }
600                 $ret=$this->_data_write($mode, $fp);
601                 $this->_data_close();
602                 if(!$this->_readmsg()) return FALSE;
603                 if(!$this->_checkCode()) return FALSE;
604                 return $ret;
605         }
606
607         function put($localfile, $remotefile=NULL, $rest=0) {
608                 if(is_null($remotefile)) $remotefile=$localfile;
609                 if (!file_exists($localfile)) {
610                         $this->PushError("put","can't open local file", "No such file or directory \"".$localfile."\"");
611                         return FALSE;
612                 }
613                 $fp = @fopen($localfile, "r");
614
615                 if (!$fp) {
616                         $this->PushError("put","can't open local file", "Cannot read file \"".$localfile."\"");
617                         return FALSE;
618                 }
619                 if($this->_can_restore and $rest!=0) fseek($fp, $rest);
620                 $pi=pathinfo($localfile);
621                 if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
622                 else $mode=FTP_BINARY;
623                 if(!$this->_data_prepare($mode)) {
624                         fclose($fp);
625                         return FALSE;
626                 }
627                 if($this->_can_restore and $rest!=0) $this->restore($rest);
628                 if(!$this->_exec("STOR ".$remotefile, "put")) {
629                         $this->_data_close();
630                         fclose($fp);
631                         return FALSE;
632                 }
633                 if(!$this->_checkCode()) {
634                         $this->_data_close();
635                         fclose($fp);
636                         return FALSE;
637                 }
638                 $ret=$this->_data_write($mode, $fp);
639                 fclose($fp);
640                 $this->_data_close();
641                 if(!$this->_readmsg()) return FALSE;
642                 if(!$this->_checkCode()) return FALSE;
643                 return $ret;
644         }
645
646         function mput($local=".", $remote=NULL, $continious=false) {
647                 $local=realpath($local);
648                 if(!@file_exists($local)) {
649                         $this->PushError("mput","can't open local folder", "Cannot stat folder \"".$local."\"");
650                         return FALSE;
651                 }
652                 if(!is_dir($local)) return $this->put($local, $remote);
653                 if(empty($remote)) $remote=".";
654                 elseif(!$this->file_exists($remote) and !$this->mkdir($remote)) return FALSE;
655                 if($handle = opendir($local)) {
656                         $list=array();
657                         while (false !== ($file = readdir($handle))) {
658                                 if ($file != "." && $file != "..") $list[]=$file;
659                         }
660                         closedir($handle);
661                 } else {
662                         $this->PushError("mput","can't open local folder", "Cannot read folder \"".$local."\"");
663                         return FALSE;
664                 }
665                 if(empty($list)) return TRUE;
666                 $ret=true;
667                 foreach($list as $el) {
668                         if(is_dir($local."/".$el)) $t=$this->mput($local."/".$el, $remote."/".$el);
669                         else $t=$this->put($local."/".$el, $remote."/".$el);
670                         if(!$t) {
671                                 $ret=FALSE;
672                                 if(!$continious) break;
673                         }
674                 }
675                 return $ret;
676
677         }
678
679         function mget($remote, $local=".", $continious=false) {
680                 $list=$this->rawlist($remote, "-lA");
681                 if($list===false) {
682                         $this->PushError("mget","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
683                         return FALSE;
684                 }
685                 if(empty($list)) return true;
686                 if(!@file_exists($local)) {
687                         if(!@mkdir($local)) {
688                                 $this->PushError("mget","can't create local folder", "Cannot create folder \"".$local."\"");
689                                 return FALSE;
690                         }
691                 }
692                 foreach($list as $k=>$v) {
693                         $list[$k]=$this->parselisting($v);
694                         if($list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
695                 }
696                 $ret=true;
697                 foreach($list as $el) {
698                         if($el["type"]=="d") {
699                                 if(!$this->mget($remote."/".$el["name"], $local."/".$el["name"], $continious)) {
700                                         $this->PushError("mget", "can't copy folder", "Can't copy remote folder \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
701                                         $ret=false;
702                                         if(!$continious) break;
703                                 }
704                         } else {
705                                 if(!$this->get($remote."/".$el["name"], $local."/".$el["name"])) {
706                                         $this->PushError("mget", "can't copy file", "Can't copy remote file \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
707                                         $ret=false;
708                                         if(!$continious) break;
709                                 }
710                         }
711                         @chmod($local."/".$el["name"], $el["perms"]);
712                         $t=strtotime($el["date"]);
713                         if($t!==-1 and $t!==false) @touch($local."/".$el["name"], $t);
714                 }
715                 return $ret;
716         }
717
718         function mdel($remote, $continious=false) {
719                 $list=$this->rawlist($remote, "-la");
720                 if($list===false) {
721                         $this->PushError("mdel","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
722                         return false;
723                 }
724
725                 foreach($list as $k=>$v) {
726                         $list[$k]=$this->parselisting($v);
727                         if($list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
728                 }
729                 $ret=true;
730
731                 foreach($list as $el) {
732                         if ( empty($el) )
733                                 continue;
734
735                         if($el["type"]=="d") {
736                                 if(!$this->mdel($remote."/".$el["name"], $continious)) {
737                                         $ret=false;
738                                         if(!$continious) break;
739                                 }
740                         } else {
741                                 if (!$this->delete($remote."/".$el["name"])) {
742                                         $this->PushError("mdel", "can't delete file", "Can't delete remote file \"".$remote."/".$el["name"]."\"");
743                                         $ret=false;
744                                         if(!$continious) break;
745                                 }
746                         }
747                 }
748
749                 if(!$this->rmdir($remote)) {
750                         $this->PushError("mdel", "can't delete folder", "Can't delete remote folder \"".$remote."/".$el["name"]."\"");
751                         $ret=false;
752                 }
753                 return $ret;
754         }
755
756         function mmkdir($dir, $mode = 0777) {
757                 if(empty($dir)) return FALSE;
758                 if($this->is_exists($dir) or $dir == "/" ) return TRUE;
759                 if(!$this->mmkdir(dirname($dir), $mode)) return false;
760                 $r=$this->mkdir($dir, $mode);
761                 $this->chmod($dir,$mode);
762                 return $r;
763         }
764
765         function glob($pattern, $handle=NULL) {
766                 $path=$output=null;
767                 if(PHP_OS=='WIN32') $slash='\\';
768                 else $slash='/';
769                 $lastpos=strrpos($pattern,$slash);
770                 if(!($lastpos===false)) {
771                         $path=substr($pattern,0,-$lastpos-1);
772                         $pattern=substr($pattern,$lastpos);
773                 } else $path=getcwd();
774                 if(is_array($handle) and !empty($handle)) {
775                         while($dir=each($handle)) {
776                                 if($this->glob_pattern_match($pattern,$dir))
777                                 $output[]=$dir;
778                         }
779                 } else {
780                         $handle=@opendir($path);
781                         if($handle===false) return false;
782                         while($dir=readdir($handle)) {
783                                 if($this->glob_pattern_match($pattern,$dir))
784                                 $output[]=$dir;
785                         }
786                         closedir($handle);
787                 }
788                 if(is_array($output)) return $output;
789                 return false;
790         }
791
792         function glob_pattern_match($pattern,$string) {
793                 $out=null;
794                 $chunks=explode(';',$pattern);
795                 foreach($chunks as $pattern) {
796                         $escape=array('$','^','.','{','}','(',')','[',']','|');
797                         while(strpos($pattern,'**')!==false)
798                                 $pattern=str_replace('**','*',$pattern);
799                         foreach($escape as $probe)
800                                 $pattern=str_replace($probe,"\\$probe",$pattern);
801                         $pattern=str_replace('?*','*',
802                                 str_replace('*?','*',
803                                         str_replace('*',".*",
804                                                 str_replace('?','.{1,1}',$pattern))));
805                         $out[]=$pattern;
806                 }
807                 if(count($out)==1) return($this->glob_regexp("^$out[0]$",$string));
808                 else {
809                         foreach($out as $tester)
810                                 if($this->my_regexp("^$tester$",$string)) return true;
811                 }
812                 return false;
813         }
814
815         function glob_regexp($pattern,$probe) {
816                 $sensitive=(PHP_OS!='WIN32');
817                 return ($sensitive?
818                         ereg($pattern,$probe):
819                         eregi($pattern,$probe)
820                 );
821         }
822
823         function dirlist($remote) {
824                 $list=$this->rawlist($remote, "-la");
825                 if($list===false) {
826                         $this->PushError("dirlist","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
827                         return false;
828                 }
829
830                 $dirlist = array();
831                 foreach($list as $k=>$v) {
832                         $entry=$this->parselisting($v);
833                         if ( empty($entry) )
834                                 continue;
835
836                         if($entry["name"]=="." or $entry["name"]=="..")
837                                 continue;
838
839                         $dirlist[$entry['name']] = $entry;
840                 }
841
842                 return $dirlist;
843         }
844 // <!-- --------------------------------------------------------------------------------------- -->
845 // <!--       Private functions                                                                 -->
846 // <!-- --------------------------------------------------------------------------------------- -->
847         function _checkCode() {
848                 return ($this->_code<400 and $this->_code>0);
849         }
850
851         function _list($arg="", $cmd="LIST", $fnction="_list") {
852                 if(!$this->_data_prepare()) return false;
853                 if(!$this->_exec($cmd.$arg, $fnction)) {
854                         $this->_data_close();
855                         return FALSE;
856                 }
857                 if(!$this->_checkCode()) {
858                         $this->_data_close();
859                         return FALSE;
860                 }
861                 $out="";
862                 if($this->_code<200) {
863                         $out=$this->_data_read();
864                         $this->_data_close();
865                         if(!$this->_readmsg()) return FALSE;
866                         if(!$this->_checkCode()) return FALSE;
867                         if($out === FALSE ) return FALSE;
868                         $out=preg_split("/[".CRLF."]+/", $out, -1, PREG_SPLIT_NO_EMPTY);
869 //                      $this->SendMSG(implode($this->_eol_code[$this->OS_local], $out));
870                 }
871                 return $out;
872         }
873
874 // <!-- --------------------------------------------------------------------------------------- -->
875 // <!-- Partie : gestion des erreurs                                                            -->
876 // <!-- --------------------------------------------------------------------------------------- -->
877 // Gnre une erreur pour traitement externe  la classe
878         function PushError($fctname,$msg,$desc=false){
879                 $error=array();
880                 $error['time']=time();
881                 $error['fctname']=$fctname;
882                 $error['msg']=$msg;
883                 $error['desc']=$desc;
884                 if($desc) $tmp=' ('.$desc.')'; else $tmp='';
885                 $this->SendMSG($fctname.': '.$msg.$tmp);
886                 return(array_push($this->_error_array,$error));
887         }
888
889 // Rcupre une erreur externe
890         function PopError(){
891                 if(count($this->_error_array)) return(array_pop($this->_error_array));
892                         else return(false);
893         }
894 }
895
896 $mod_sockets=TRUE;
897 if (!extension_loaded('sockets')) {
898         $prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
899         if(!@dl($prefix . 'sockets.' . PHP_SHLIB_SUFFIX)) $mod_sockets=FALSE;
900 }
901
902 require_once "class-ftp-".($mod_sockets?"sockets":"pure").".php";
903 ?>