daily pastebin goal
67%
SHARE
TWEET

_ftp.php

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