_resetError(); $err_no = 0; $err_msg = ""; $this->controlSocket = @fsockopen($host, $port, $err_no, $err_msg, $timeout) or $this->_setError(-1,"fsockopen failed"); if ($err_no<>0) $this->setError($err_no,$err_msg); if ($this->_isError()) return false; @socket_set_timeout($this->controlSocket,$timeout) or $this->_setError(-1,"socket_set_timeout failed"); if ($this->_isError()) return false; $this->_waitForResult(); if ($this->_isError()) return false; return $this->getLastResult() == FTP_SERVICE_READY; } function isConnected() { return $this->controlSocket != NULL; } function disconnect() { if (!$this->isConnected()) return; @fclose($this->controlSocket); } function close() { //Closes an FTP connection $this->disconnect(); } function login($user, $pass) { //Logs in to an FTP connection $this->_resetError(); $this->_printCommand("USER $user"); if ($this->_isError()) return false; $this->_waitForResult(); if ($this->_isError()) return false; if ($this->getLastResult() == FTP_PASSWORD_NEEDED){ $this->_printCommand("PASS $pass"); if ($this->_isError()) return FALSE; $this->_waitForResult(); if ($this->_isError()) return FALSE; } $result = $this->getLastResult() == FTP_USER_LOGGED_IN; return $result; } function cdup() { //Changes to the parent directory $this->_resetError(); $this->_printCommand("CDUP"); $this->_waitForResult(); $lr = $this->getLastResult(); if ($this->_isError()) return FALSE; return ($lr==FTP_FILE_ACTION_OK || $lr==FTP_COMMAND_OK); } function cwd($path) { $this->_resetError(); $this->_printCommand("CWD $path"); $this->_waitForResult(); $lr = $this->getLastResult(); if ($this->_isError()) return FALSE; return ($lr==FTP_FILE_ACTION_OK || $lr==FTP_COMMAND_OK); } function cd($path) { return $this->cwd($path); } function chdir($path) { //Changes directories on a FTP server return $this->cwd($path); } function chmod($mode,$filename) { //Set permissions on a file via FTP return $this->site("CHMOD $mode $filename"); } function delete($filename) { //Deletes a file on the FTP server $this->_resetError(); $this->_printCommand("DELE $filename"); $this->_waitForResult(); $lr = $this->getLastResult(); if ($this->_isError()) return FALSE; return ($lr==FTP_FILE_ACTION_OK || $lr==FTP_COMMAND_OK); } function exec($cmd) { //Requests execution of a program on the FTP server return $this->site("EXEC $cmd"); } function fget($fp,$remote,$mode=FTP_BINARY,$resumepos=0) { //Downloads a file from the FTP server and saves to an open file $this->_resetError(); $type = "I"; if ($mode==FTP_ASCII) $type = "A"; $this->_printCommand("TYPE $type"); $this->_waitForResult(); $lr = $this->getLastResult(); if ($this->_isError()) return FALSE; $result = $this->_download("RETR $remote"); if ($result) { fwrite($fp,$result); } return $result; } function fput($remote,$resource,$mode=FTP_BINARY,$startpos=0) { //Uploads from an open file to the FTP server $this->_resetError(); $type = "I"; if ($mode==FTP_ASCII) $type = "A"; $this->_printCommand("TYPE $type"); $this->_waitForResult(); $lr = $this->getLastResult(); if ($this->_isError()) return FALSE; if ($startpos>0) fseek($resource,$startpos); $result = $this->_uploadResource("STOR $remote",$resource); return $result; } function get_option($option) { //Retrieves various runtime behaviours of the current FTP stream $this->_resetError(); switch ($option) { case "FTP_TIMEOUT_SEC" : return FTP_TIMEOUT; case "PHP_FTP_OPT_AUTOSEEK" : return FALSE; } setError(-1,"Unknown option: $option"); return false; } function get($locale,$remote,$mode=FTP_BINARY,$resumepos=0) { //Downloads a file from the FTP server if (!($fp = @fopen($locale,"wb"))) return FALSE; $result = $this->fget($fp,$remote,$mode,$resumepos); @fclose($fp); if (!$result) @unlink($locale); return $result; } function mdtm($name) { //Returns the last modified time of the given file $this->_resetError(); $this->_printCommand("MDTM $name"); $this->_waitForResult(); $lr = $this->getLastResult(); if ($this->_isError()) return FALSE; if ($lr!=FTP_FILE_STATUS) return FALSE; $subject = trim(substr($this->lastLine,4)); $lucifer = array(); 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); return FALSE; } function mkdir($name) { //Creates a directory $this->_resetError(); $this->_printCommand("MKD $name"); $this->_waitForResult(); $lr = $this->getLastResult(); if ($this->_isError()) return FALSE; return ($lr==FTP_PATHNAME || $lr==FTP_FILE_ACTION_OK || $lr==FTP_COMMAND_OK); } function nb_continue() { //Continues retrieving/sending a file (non-blocking) $this->_resetError(); // todo } function nb_fget() { //Retrieves a file from the FTP server and writes it to an open file (non-blocking) $this->_resetError(); // todo } function nb_fput() { //Stores a file from an open file to the FTP server (non-blocking) $this->_resetError(); // todo } function nb_get() { //Retrieves a file from the FTP server and writes it to a local file (non-blocking) $this->_resetError(); // todo } function nb_put() { //Stores a file on the FTP server (non-blocking) $this->_resetError(); // todo } function nlist($remote_filespec="") { //Returns a list of files in the given directory $this->_resetError(); $result = $this->_download(trim("NLST $remote_filespec")); return ($result !== FALSE) ? explode("\n",str_replace("\r","",trim($result))) : $result; } function pasv($pasv) { //Turns passive mode on or off if (!$pasv) { $this->_setError("Active (PORT) mode is not supported"); return false; } return true; } function put($remote,$local,$mode=FTP_BINARY,$startpos=0) { //Uploads a file to the FTP server if (!($fp = @fopen($local,"rb"))) return FALSE; $result = $this->fput($remote,$fp,$mode,$startpos); @fclose($fp); return $result; } function pwd() { //Returns the current directory name $this->_resetError(); $this->_printCommand("PWD"); $this->_waitForResult(); $lr = $this->getLastResult(); if ($this->_isError()) return FALSE; if ($lr!=FTP_PATHNAME) return FALSE; $subject = trim(substr($this->lastLine,4)); $lucifer = array(); if (preg_match("/\"(.*)\"/",$subject,$lucifer)) return $lucifer[1]; return FALSE; } function quit() { //Alias of close $this->close(); } function raw($cmd) { //Sends an arbitrary command to an FTP server $this->_resetError(); $this->_printCommand($cmd); $this->_waitForResult(); $this->getLastResult(); return array($this->lastLine); } function rawlist($remote_filespec="") { //Returns a detailed list of files in the given directory $this->_resetError(); $result = $this->_download(trim("LIST $remote_filespec")); return ($result !== FALSE) ? explode("\n",str_replace("\r","",trim($result))) : $result; } function ls($remote_filespec="") { //Returns a parsed rawlist in an assoc array $a = $this->rawlist($remote_filespec); if (!$a) return $a; $systype = $this->systype(); $is_windows = stristr($systype,"WIN")!==FALSE; $b = array(); while (list($i,$line) = each($a)) { if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|