3 define(
"FTP_TIMEOUT",90);
6 define(
"FTP_COMMAND_OK",200);
7 define(
"FTP_FILE_ACTION_OK",250);
8 define(
"FTP_FILE_TRANSFER_OK",226);
9 define(
"FTP_COMMAND_NOT_IMPLEMENTED",502);
10 define(
"FTP_FILE_STATUS",213);
11 define(
"FTP_NAME_SYSTEM_TYPE",215);
12 define(
"FTP_PASSIVE_MODE",227);
13 define(
"FTP_PATHNAME",257);
14 define(
"FTP_SERVICE_READY",220);
15 define(
"FTP_USER_LOGGED_IN",230);
16 define(
"FTP_PASSWORD_NEEDED",331);
17 define(
"FTP_USER_NOT_LOGGED_IN",530);
19 if (!defined(
"FTP_ASCII")) define(
"FTP_ASCII",0);
20 if (!defined(
"FTP_BINARY")) define(
"FTP_BINARY",1);
43 $this->controlSocket = @fsockopen(
$host,
$port, $err_no, $err_msg, $timeout) or $this->
_setError(-1,
"fsockopen failed");
44 if ($err_no<>0) $this->setError($err_no,$err_msg);
48 @socket_set_timeout($this->controlSocket,$timeout) or $this->
_setError(-1,
"socket_set_timeout failed");
58 return $this->controlSocket != NULL;
63 @fclose($this->controlSocket);
107 if ($this->
_isError())
return FALSE;
120 return $this->
site(
"CHMOD $mode $filename");
123 function delete($filename) {
129 if ($this->
_isError())
return FALSE;
134 return $this->
site(
"EXEC $cmd");
137 function fget(
$fp,$remote,$mode=FTP_BINARY,$resumepos=0) {
141 if ($mode==FTP_ASCII) $type =
"A";
146 if ($this->
_isError())
return FALSE;
155 function fput($remote,$resource,$mode=FTP_BINARY,$startpos=0) {
159 if ($mode==FTP_ASCII) $type =
"A";
164 if ($this->
_isError())
return FALSE;
166 if ($startpos>0) fseek($resource,$startpos);
176 case "PHP_FTP_OPT_AUTOSEEK" :
return FALSE;
178 setError(-1,
"Unknown option: $option");
182 function get($locale,$remote,$mode=FTP_BINARY,$resumepos=0) {
183 if (!(
$fp = @fopen($locale,
"wb")))
return FALSE;
186 if (!
$result) @unlink($locale);
195 if ($this->
_isError())
return FALSE;
197 $subject = trim(substr($this->lastLine,4));
199 if (preg_match(
"/([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])/",$subject,$lucifer))
return mktime($lucifer[4],$lucifer[5],$lucifer[6],$lucifer[2],$lucifer[3],$lucifer[1],0);
209 if ($this->
_isError())
return FALSE;
238 function nlist($remote_filespec=
"") {
246 $this->
_setError(
"Active (PORT) mode is not supported");
252 function put($remote,$local,$mode=FTP_BINARY,$startpos=0) {
253 if (!(
$fp = @fopen($local,
"rb")))
return FALSE;
265 if ($this->
_isError())
return FALSE;
267 $subject = trim(substr($this->lastLine,4));
269 if (preg_match(
"/\"(.*)\"/",$subject,$lucifer))
return $lucifer[1];
283 return array($this->lastLine);
292 function ls($remote_filespec=
"") {
293 $a = $this->
rawlist($remote_filespec);
296 $is_windows = stristr($systype,
"WIN")!==FALSE;
298 while (list($i,$line) = each($a)) {
299 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)) {
301 if ($lucifer[3]<70) { $lucifer[3]+=2000; }
else { $lucifer[3]+=1900; }
302 $b[$i][
'isdir'] = ($lucifer[7]==
"<DIR>");
303 $b[$i][
'size'] = $lucifer[7];
304 $b[$i][
'month'] = $lucifer[1];
305 $b[$i][
'day'] = $lucifer[2];
306 $b[$i][
'year'] = $lucifer[3];
307 $b[$i][
'hour'] = $lucifer[4];
308 $b[$i][
'minute'] = $lucifer[5];
309 $b[$i][
'time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],
"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
310 $b[$i][
'am/pm'] = $lucifer[6];
311 $b[$i][
'name'] = $lucifer[8];
312 }
else if (!$is_windows && $lucifer=preg_split(
"/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
314 $lcount=count($lucifer);
315 if ($lcount<8)
continue;
317 $b[$i][
'isdir'] = $lucifer[0]{0} ===
"d";
318 $b[$i][
'islink'] = $lucifer[0]{0} ===
"l";
319 $b[$i][
'perms'] = $lucifer[0];
320 $b[$i][
'number'] = $lucifer[1];
321 $b[$i][
'owner'] = $lucifer[2];
322 $b[$i][
'group'] = $lucifer[3];
323 $b[$i][
'size'] = $lucifer[4];
325 sscanf($lucifer[5],
"%d-%d-%d",$b[$i][
'year'],$b[$i][
'month'],$b[$i][
'day']);
326 sscanf($lucifer[6],
"%d:%d",$b[$i][
'hour'],$b[$i][
'minute']);
327 $b[$i][
'time'] = @mktime($b[$i][
'hour'],$b[$i][
'minute'],0,$b[$i][
'month'],$b[$i][
'day'],$b[$i][
'year']);
328 $b[$i][
'name'] = $lucifer[7];
330 $b[$i][
'month'] = $lucifer[5];
331 $b[$i][
'day'] = $lucifer[6];
332 if (preg_match(
"/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
333 $b[$i][
'year'] = date(
"Y");
334 $b[$i][
'hour'] = $l2[1];
335 $b[$i][
'minute'] = $l2[2];
337 $b[$i][
'year'] = $lucifer[7];
339 $b[$i][
'minute'] = 0;
341 $b[$i][
'time'] = strtotime(sprintf(
"%d %s %d %02d:%02d",$b[$i][
'day'],$b[$i][
'month'],$b[$i][
'year'],$b[$i][
'hour'],$b[$i][
'minute']));
342 $b[$i][
'name'] = $lucifer[8];
355 if ($this->
_isError())
return FALSE;
359 if ($this->
_isError())
return FALSE;
369 if ($this->
_isError())
return FALSE;
375 $this->
_setError(-1,
"set_option not supported");
385 if ($this->
_isError())
return FALSE;
395 if ($this->
_isError())
return FALSE;
401 $this->
_setError(-1,
"ssl_connect not supported");
411 if ($this->
_isError())
return FALSE;
416 $this->newResult = FALSE;
432 $line = fgets($this->controlSocket);
433 if ($line === FALSE) {
434 $this->
_setError(-1,
"fgets failed in _readln");
437 if (strlen($line)==0)
return $line;
440 if (preg_match(
"/^[0-9][0-9][0-9] /",$line,$lucifer)) {
442 $this->lastResult = intval($lucifer[0]);
443 $this->newResult = TRUE;
444 if (substr($lucifer[0],0,1)==
'5') {
445 $this->
_setError($this->lastResult,trim(substr($line,4)));
449 $this->lastLine = trim($line);
450 $this->lastLines[] =
"< ".trim($line);
456 $this->lastLines[] =
"> ".$line;
457 fwrite($this->controlSocket,$line.
"\r\n");
458 fflush($this->controlSocket);
467 if ($this->
_isError())
return FALSE;
469 $subject = trim(substr($this->lastLine,4));
471 if (preg_match(
"/\\((\d{1,3}),(\d{1,3}),(\d{1,3}),(\d{1,3}),(\d{1,3}),(\d{1,3})\\)/",$subject,$lucifer)) {
472 $this->pasvAddr=$lucifer;
474 $host = sprintf(
"%d.%d.%d.%d",$lucifer[1],$lucifer[2],$lucifer[3],$lucifer[4]);
475 $port = $lucifer[5]*256 + $lucifer[6];
485 return $passiveConnection;
492 if (!($passiveConnection = $this->
_pasv()))
return FALSE;
498 while (!feof($passiveConnection)) {
499 $result .= fgets($passiveConnection);
501 fclose($passiveConnection);
506 fclose($passiveConnection);
513 if (!($passiveConnection = $this->
_pasv()))
return FALSE;
519 while (!feof($resource)) {
520 $buf = fread($resource,1024);
521 fwrite($passiveConnection,$buf);
523 fclose($passiveConnection);
528 fclose($passiveConnection);
535 $this->error_no = NULL;
536 $this->error_msg = NULL;
541 if (is_array($this->error_no)) {
542 $this->error_no[] = $no;
543 $this->error_msg[] = $msg;
544 }
else if ($this->error_no!=NULL) {
545 $this->error_no = array($this->error_no,$no);
546 $this->error_msg = array($this->error_msg,$msg);
548 $this->error_no = $no;
549 $this->error_msg = $msg;
555 return ($this->error_no != NULL) && ($this->error_no !== 0);