cvs: pear /Net_FTP/Net FTP.php

From: Date: Sun, 14 Mar 2004 13:15:00 +0000
Subject: cvs: pear /Net_FTP/Net FTP.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-17389@lists.php.net to get a copy of this message
toby Sun Mar 14 08:15:00 2004 EDT Modified files: /pear/Net_FTP/Net FTP.php Log: * Fixed line spaces.

http://cvs.php.net/diff.php/pear/Net_FTP/Net/FTP.php?r1=1.9&r2=1.10&ty=u Index: pear/Net_FTP/Net/FTP.php diff -u pear/Net_FTP/Net/FTP.php:1.9 pear/Net_FTP/Net/FTP.php:1.10 --- pear/Net_FTP/Net/FTP.php:1.9 Sun Mar 14 08:04:29 2004 +++ pear/Net_FTP/Net/FTP.php Sun Mar 14 08:14:59 2004 @@ -14,7 +14,7 @@ // | Authors: Tobias Schlitt <toby@php.net> | // +----------------------------------------------------------------------+ // -// $Id: FTP.php,v 1.9 2004/03/14 13:04:29 toby Exp $ +// $Id: FTP.php,v 1.10 2004/03/14 13:14:59 toby Exp $ require_once 'PEAR.php'; @@ -44,6 +44,7 @@ * @access private * @var string */ + var $_hostname; /** @@ -52,6 +53,7 @@ * @access private * @var int */ + var $_port = 21; /** @@ -60,6 +62,7 @@ * @access private * @var string */ + var $_username; /** @@ -68,6 +71,7 @@ * @access private * @var string */ + var $_password; /** @@ -76,6 +80,7 @@ * @access private * @var bool */ + var $_passv; /** @@ -84,6 +89,7 @@ * @access private * @var int */ + var $_mode = FTP_BINARY; /** @@ -92,6 +98,7 @@ * @access private * @var resource */ + var $_handle; /** @@ -103,6 +110,7 @@ * @access private * @var array */ + var $_file_extensions; /** @@ -111,7 +119,9 @@ * @access private * @var string */ + var $ls_match = '/(?:(d)|.)([rwx-]+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\S+\s+\S+\s+\S+)\s+(.+)/'; + var $ls_regex_map = array('name'=>8,'size'=>6,'rights'=>2,'user'=>4,'group'=>5, 'files_inside'=>3,'date'=>7,'is_dir'=>1); @@ -123,6 +133,7 @@ * @access private * @since 1.3 */ + var $_listeners = array(); /** @@ -136,6 +147,7 @@ * @param int $port (optional) The port * @return void */ + function Net_FTP($host = null, $port = null) { $this->PEAR(); @@ -159,6 +171,7 @@ * @param int $port (optional) The Port * @return mixed True on success, otherwise PEAR::Error */ + function connect($host = null, $port = null) { if (isset($host)) { @@ -182,6 +195,7 @@ * @access public * @return void */ + function disconnect() { @ftp_close($this->_handle); @@ -197,6 +211,7 @@ * @param string $password (optional) The password to use * @return mixed True on success, otherwise PEAR::Error */ + function login($username = null, $password = null) { if (!isset($username)) { @@ -228,6 +243,7 @@ * @param string $dir The directory to go to. * @return mixed True on success, otherwise PEAR::Error */ + function cd($dir) { $erg = @ftp_chdir($this->_handle, $dir); @@ -245,6 +261,7 @@ * @access public * @return mixed The actual path or PEAR::Error */ + function pwd() { $res = @ftp_pwd($this->_handle); @@ -265,6 +282,7 @@ * @param bool $recursive (optional) Create all needed directories * @return mixed True on success, otherwise PEAR::Error */ + function mkdir($dir, $recursive = false) { $dir = $this->_construct_path($dir); @@ -301,6 +319,7 @@ * @param string $command The command to execute * @return mixed The result of the command (if successfull), otherwise PEAR::Error */ + function execute($command) { $res = @ftp_exec($this->_handle, $command); @@ -319,6 +338,7 @@ * @param string $command The command with parameters to execute * @return mixed True if successful, otherwise PEAR::Error */ + function site($command) { $res = @ftp_site($this->_handle, $command); @@ -341,6 +361,7 @@ * @param integer $permissions The mode to set the file permissions to * @return mixed True if successful, otherwise PEAR::Error */ + function chmod($target, $permissions) { // If $target is an array: Loop through it. @@ -382,6 +403,7 @@ * and file permissions to * @return mixed True if successful, otherwise PEAR::Error */ + function chmodRecursive($target, $permissions) { // If $target is an array: Loop through it @@ -469,6 +491,7 @@ * if not set, it will return a Unix timestamp * @return mixed Unix timestamp, a preformated date-string or PEAR::Error */ + function mdtm($file, $format = null) { $file = $this->_construct_path($file); @@ -496,6 +519,7 @@ * @param string $file The file to check * @return mixed Size in bytes or PEAR::Error */ + function size($file) { $file = $this->_construct_path($file); @@ -551,6 +575,7 @@ * @param int $mode (optional) The mode which types to list (files, directories or both). * @return mixed The directory list as described above or PEAR::Error on failure. */ + function ls($dir = null, $mode = NET_FTP_DIRS_FILES) { if (!isset($dir)) { @@ -597,6 +622,7 @@ * @param bool $recursive (optional) * @return mixed True on success, otherwise PEAR::Error */ + function rm($path, $recursive = false) { $path = $this->_construct_path($path); @@ -630,6 +656,7 @@ * @param int $mode (optional) Either FTP_ASCII or FTP_BINARY * @return mixed True on success, otherwise PEAR::Error */ + function get($remote_file, $local_file, $overwrite = false, $mode = null) { if (!isset($mode)) { @@ -679,6 +706,7 @@ * @param int $mode (optional) Either FTP_ASCII or FTP_BINARY * @return mixed True on success, otherwise PEAR::Error */ + function put($local_file, $remote_file, $overwrite = false, $mode = null) { if (!isset($mode)) { @@ -730,6 +758,7 @@ * @param int $mode (optional) The transfermode (either FTP_ASCII or FTP_BINARY). * @return mixed True on succes, otherwise PEAR::Error */ + function getRecursive($remote_path, $local_path, $overwrite = false, $mode = null) { $remote_path = $this->_construct_path($remote_path); @@ -789,6 +818,7 @@ * @param int $mode (optional) The transfermode (either FTP_ASCII or FTP_BINARY). * @return mixed True on succes, otherwise PEAR::Error */ + function putRecursive($local_path, $remote_path, $overwrite = false, $mode = null) { $remote_path = $this->_construct_path($remote_path); @@ -837,6 +867,7 @@ * @param string $filename The filename to be checked * @return int Either FTP_ASCII or FTP_BINARY */ + function checkFileExtension($filename) { $pattern = "/\.(.*)$/"; @@ -855,554 +886,586 @@ } /** - * Rebuild the path, if given relative + * Set the Hostname * - * @access private - * @param string $path The path to check and construct - * @return string The build path + * @access public + * @param string $host The Hostname to set */ - function _construct_path($path) + + function setHostname($host) { - if (substr($path, 0, 1) != "/") { - $actual_dir = @ftp_pwd($this->_handle); - if (substr($actual_dir, (strlen($actual_dir) - 2), 1) != "/") { - $actual_dir .= "/"; - } - $path = $actual_dir.$path; - } - return $path; + $this->_hostname = $host; } /** - * Checks, whether a given string is a directory-path (ends with "/") or not. + * Set the Port * - * @access private - * @param string $path Path to check - * @return bool True if $path is a directory, otherwise false + * @access public + * @param int $port The Port to set */ - function _check_dir($path) + + function setPort($port) { - if (substr($path, (strlen($path) - 1), 1) == "/") { - return true; - } else { - return false; - } + $this->_port = $port; } /** - * This will remove a file + * Set the Username * - * @access private - * @param string $file The file to delete - * @return mixed True on success, otherwise PEAR::Error + * @access public + * @param string $user The Username to set */ - function _rm_file($file) + + function setUsername($user) { - if (substr($file, 0, 1) == "/") { - $res = @ftp_delete($this->_handle, $file); - } else { - $actual_dir = @ftp_pwd($this->_handle); - if (substr($actual_dir, (strlen($actual_dir) - 2), 1) != "/") { - $actual_dir .= "/"; - } - $file = $actual_dir.$file; - $res = @ftp_delete($this->_handle, $file); - } - - if (!$res) { - return $this->raiseError("Could not delete file '$file'.", 0); - } else { - return true; - } + $this->_username = $user; } /** - * This will remove a dir + * Set the Password * * @access private - * @param string $dir The dir to delete - * @return mixed True on success, otherwise PEAR::Error + * @param string $password The Password to set */ - function _rm_dir($dir) + + function setPassword($password) { - if (substr($dir, (strlen($dir) - 1), 1) != "/") { - return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'", 0); - } - $res = @ftp_rmdir($this->_handle, $dir); - if (!$res) { - return $this->raiseError("Could not delete directory '$dir'.", 0); - } else { - return true; - } + $this->_password = $password; } /** - * This will remove a dir and all subdirs and -files + * Set the transfer-mode. You can use the predefined constants + * FTP_ASCII or FTP_BINARY. The mode will be stored for any further transfers. * - * @access private - * @param string $file The dir to delete recursively + * @access public + * @param int $mode The mode to set * @return mixed True on success, otherwise PEAR::Error */ - function _rm_dir_recursive($dir) + + function setMode($mode) { - if (substr($dir, (strlen($dir) - 1), 1) != "/") { - return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'", 0); - } - $file_list = $this->_ls_files($dir); - foreach ($file_list as $file) { - $file = $dir.$file["name"]; - $res = $this->rm($file); - if ($this->isError($res)) { - return $res; - } - } - $dir_list = $this->_ls_dirs($dir); - foreach ($dir_list as $new_dir) { - $new_dir = $dir.$new_dir["name"]."/"; - $res = $this->_rm_dir_recursive($new_dir); - if ($this->isError($res)) { - return $res; - } - } - $res = $this->_rm_dir($dir); - if (!$res) { - return $res; - } else { + if (($mode == FTP_ASCII) || ($mode == FTP_BINARY)) { + $this->_mode = $mode; return true; + } else { + return $this->raiseError('FTP-Mode has either to be FTP_ASCII or FTP_BINARY', 1); } } /** - * Lists up files and directories + * Set the transfer-method to passive mode * - * @access private - * @param string $dir The directory to list up - * @return array An array of dirs and files + * @access public + * @return void */ - function _ls_both($dir) + + function setPassive() { - $list_splitted = $this->_list_and_parse($dir); - if (!is_array($list_splitted["files"])) { - $list_splitted["files"] = array(); - } - if (!is_array($list_splitted["dirs"])) { - $list_splitted["dirs"] = array(); - } - $res = array(); - @array_splice($res, 0, 0, $list_splitted["files"]); - @array_splice($res, 0, 0, $list_splitted["dirs"]); - return $res; + $this->_passv = true; + @ftp_pasv($this->_handle, true); } /** - * Lists up directories + * Set the transfer-method to active mode * - * @access private - * @param string $dir The directory to list up - * @return array An array of dirs + * @access public + * @return void */ - function _ls_dirs($dir) + + function setActive() { - $list["dirs"] = array(); - $list = $this->_list_and_parse($dir); - return $list["dirs"]; + $this->_passv = false; + @ftp_pasv($this->_handle, false); } /** - * Lists up files + * Adds an extension to a mode-directory + * The mode-directory saves file-extensions coresponding to filetypes + * (ascii e.g.: 'php', 'txt', 'htm',...; binary e.g.: 'jpg', 'gif', 'exe',...). + * The extensions have to be saved without the '.'. And + * can be predefined in an external file (see: getExtensionsFile()). * - * @access private - * @param string $dir The directory to list up - * @return array An array of files + * The array is build like this: 'php' => FTP_ASCII, 'png' => FTP_BINARY + * + * To change the mode of an extension, just add it again with the new mode! + * + * @access public + * @param int $mode Either FTP_ASCII or FTP_BINARY + * @param string $ext Extension + * @return void */ - function _ls_files($dir) + + function addExtension($mode, $ext) { - $list = $this->_list_and_parse($dir); - if (!is_array($list["files"])) { - $list["files"] = array(); - } - return $list["files"]; + $this->_file_extensions[$ext] = $mode; } /** - * This lists up the directory-content and parses the items into well-formated arrays - * The results of this array are sorted (dirs on top, sorted by name; - * files below, sorted by name). + * This function removes an extension from the mode-directories + * (described above). * - * @access private - * @param string $dir The directory to parse - * @return array Lists of dirs and files + * @access public + * @param string $ext The extension to remove + * @return void */ - function _list_and_parse($dir) + + function removeExtension($ext) { - $dirs_list = array(); - $files_list = array(); - $dir_list = @ftp_rawlist($this->_handle, $dir); - foreach ($dir_list as $entry) { - if (!preg_match($this->ls_match, $entry, $m)) { - continue; - } - $entry = array(); - foreach ($this->ls_regex_map as $key=>$val) { - $entry[$key] = $m[$val]; - } - $entry['stamp'] = $this->_parse_Date($entry['date']); - - if ($entry['is_dir']) { - $dirs_list[] = $entry; - } else { - $files_list[] = $entry; - } - } - @usort($dirs_list, array("Net_FTP", "_nat_sort")); - @usort($files_list, array("Net_FTP", "_nat_sort")); - $res["dirs"] = $dirs_list; - $res["files"] = $files_list; - return $res; + unset($this->_file_extensions[$ext]); } /** - * Lists a local directory + * This get's both (ascii- and binary-mode-directories) from the given file. + * Beware, if you read a file into the mode-directory, all former set values + * will be unset! * - * @access private - * @param string $dir_path The dir to list - * @return array The list of dirs and files + * @access public + * @param string $filename The file to get from + * @return mixed True on success, otherwise PEAR::Error */ - function _ls_local($dir_path) + + function getExtensionsFile($filename) { - $dir = dir($dir_path); - $dir_list = array(); - $file_list = array(); - while (false !== ($entry = $dir->read())) { - if (($entry != '.') && ($entry != '..')) { - if (is_dir($dir_path.$entry)) { - $dir_list[] = $entry; - } else { - $file_list[] = $entry; - } - } + if (!file_exists($filename)) { + return $this->raiseError("Extensions-file '$filename' does not exist", 0); } - $dir->close(); - $res['dirs'] = $dir_list; - $res['files'] = $file_list; - return $res; + + if (!is_readable($filename)) { + return $this->raiseError("Extensions-file '$filename' is not readable", 0); + } + + $this->_file_extension = @parse_ini_file($filename); + return true; } /** - * Function for use with usort(). - * Compares the list-array-elements by name. + * Returns the Hostname * - * @access private + * @access public + * @return string The Hostname */ - function _nat_sort($item_1, $item_2) + + function getHostname() { - return strnatcmp($item_1['name'], $item_2['name']); + return $this->_hostname; } /** - * Parse dates to timestamps + * Returns the Port * - * @access private - * @param string $date Date - * @return int Timestamp + * @access public + * @return int The Port */ - function _parse_Date($date) + + function getPort() { - // Sep 10 22:06 => Sep 10, <year> 22:06 - if (preg_match('/([A-Za-z]+)[ ]+([0-9]+)[ ]+([0-9]+):([0-9]+)/', $date, $res)) { - $year = date('Y'); - $month = $res[1]; - $day = $res[2]; - $hour = $res[3]; - $minute = $res[4]; - $date = "$month $day, $year $hour:$minute"; - } - // 09-10-04 => 09/10/04 - elseif (preg_match('/^\d\d-\d\d-\d\d/',$date)) { - $date = str_replace('-','/',$date); - } - $res = strtotime($date); - if (!$res) { - return $this->raiseError('Dateconversion failed.', 0); - } - return $res; + return $this->_port; } /** - * Set the Hostname + * Returns the Username * * @access public - * @param string $host The Hostname to set + * @return string The Username */ - function setHostname($host) + + function getUsername() { - $this->_hostname = $host; + return $this->_username; } /** - * Set the Port + * Returns the Password * * @access public - * @param int $port The Port to set + * @return string The Password */ - function setPort($port) + + function getPassword() { - $this->_port = $port; + return $this->_password; } /** - * Set the Username + * Returns the Transfermode * * @access public - * @param string $user The Username to set + * @return int The transfermode, either FTP_ASCII or FTP_BINARY. */ - function setUsername($user) + + function getMode() { - $this->_username = $user; + return $this->_mode; } /** - * Set the Password + * Returns, whether the connection is set to passive mode or not * - * @access private - * @param string $password The Password to set + * @access public + * @return bool True if passive-, false if active-mode */ - function setPassword($password) + + function isPassive() { - $this->_password = $password; + return $this->_passive; } /** - * Set the transfer-mode. You can use the predefined constants - * FTP_ASCII or FTP_BINARY. The mode will be stored for any further transfers. + * Returns the mode set for a file-extension * * @access public - * @param int $mode The mode to set - * @return mixed True on success, otherwise PEAR::Error + * @param string $ext The extension you wanna ask for + * @return int Either FTP_ASCII, FTP_BINARY or NULL (if not set a mode for it) */ - function setMode($mode) + + function getExtensionMode($ext) { - if (($mode == FTP_ASCII) || ($mode == FTP_BINARY)) { - $this->_mode = $mode; - return true; - } else { - return $this->raiseError('FTP-Mode has either to be FTP_ASCII or FTP_BINARY', 1); - } + return @$this->_file_extensions[$ext]; } + /** - * Set the transfer-method to passive mode + * Adds a Net_FTP_Observer instance to the list of observers + * that are listening for messages emitted by this Net_FTP instance. * + * @param object $observer The Net_FTP_Observer instance to attach + * as a listener. + * @return boolean True if the observer is successfully attached. * @access public - * @return void + * @since 1.3 */ - function setPassive() + + function attach(&$observer) { - $this->_passv = true; - @ftp_pasv($this->_handle, true); + if (!is_a($observer, 'Net_FTP_Observer')) { + return false; + } + + $this->_listeners[$observer->getId()] = &$observer; + return true; } /** - * Set the transfer-method to active mode + * Removes a Net_FTP_Observer instance from the list of observers. * + * @param object $observer The Net_FTP_Observer instance to detach + * from the list of listeners. + * @return boolean True if the observer is successfully detached. * @access public - * @return void + * @since 1.3 */ - function setActive() + + function detach($observer) { - $this->_passv = false; - @ftp_pasv($this->_handle, false); + if (!is_a($observer, 'Net_FTP_Observer') || + !isset($this->_listeners[$observer->getId()])) { + return false; + } + + unset($this->_listeners[$observer->getId()]); + return true; } /** - * Adds an extension to a mode-directory - * The mode-directory saves file-extensions coresponding to filetypes - * (ascii e.g.: 'php', 'txt', 'htm',...; binary e.g.: 'jpg', 'gif', 'exe',...). - * The extensions have to be saved without the '.'. And - * can be predefined in an external file (see: getExtensionsFile()). - * - * The array is build like this: 'php' => FTP_ASCII, 'png' => FTP_BINARY - * - * To change the mode of an extension, just add it again with the new mode! + * Informs each registered observer instance that a new message has been + * sent. + * + * @param mixed $event A hash describing the net event. + * @access private + * @since 1.3 + */ + + function _announce($event) + { + foreach ($this->_listeners as $id => $listener) { + $this->_listeners[$id]->notify($event); + } + } + + /** + * Rebuild the path, if given relative * - * @access public - * @param int $mode Either FTP_ASCII or FTP_BINARY - * @param string $ext Extension - * @return void + * @access private + * @param string $path The path to check and construct + * @return string The build path */ - function addExtension($mode, $ext) + + function _construct_path($path) { - $this->_file_extensions[$ext] = $mode; + if (substr($path, 0, 1) != "/") { + $actual_dir = @ftp_pwd($this->_handle); + if (substr($actual_dir, (strlen($actual_dir) - 2), 1) != "/") { + $actual_dir .= "/"; + } + $path = $actual_dir.$path; + } + return $path; } /** - * This function removes an extension from the mode-directories - * (described above). + * Checks, whether a given string is a directory-path (ends with "/") or not. * - * @access public - * @param string $ext The extension to remove - * @return void + * @access private + * @param string $path Path to check + * @return bool True if $path is a directory, otherwise false */ - function removeExtension($ext) + + function _check_dir($path) { - unset($this->_file_extensions[$ext]); + if (substr($path, (strlen($path) - 1), 1) == "/") { + return true; + } else { + return false; + } } /** - * This get's both (ascii- and binary-mode-directories) from the given file. - * Beware, if you read a file into the mode-directory, all former set values - * will be unset! + * This will remove a file * - * @access public - * @param string $filename The file to get from - * @return mixed True on success, otherwise PEAR::Error + * @access private + * @param string $file The file to delete + * @return mixed True on success, otherwise PEAR::Error */ - function getExtensionsFile($filename) + + function _rm_file($file) { - if (!file_exists($filename)) { - return $this->raiseError("Extensions-file '$filename' does not exist", 0); + if (substr($file, 0, 1) == "/") { + $res = @ftp_delete($this->_handle, $file); + } else { + $actual_dir = @ftp_pwd($this->_handle); + if (substr($actual_dir, (strlen($actual_dir) - 2), 1) != "/") { + $actual_dir .= "/"; + } + $file = $actual_dir.$file; + $res = @ftp_delete($this->_handle, $file); } - if (!is_readable($filename)) { - return $this->raiseError("Extensions-file '$filename' is not readable", 0); + if (!$res) { + return $this->raiseError("Could not delete file '$file'.", 0); + } else { + return true; } - - $this->_file_extension = @parse_ini_file($filename); - return true; } /** - * Returns the Hostname + * This will remove a dir * - * @access public - * @return string The Hostname + * @access private + * @param string $dir The dir to delete + * @return mixed True on success, otherwise PEAR::Error */ - function getHostname() + + function _rm_dir($dir) { - return $this->_hostname; + if (substr($dir, (strlen($dir) - 1), 1) != "/") { + return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'", 0); + } + $res = @ftp_rmdir($this->_handle, $dir); + if (!$res) { + return $this->raiseError("Could not delete directory '$dir'.", 0); + } else { + return true; + } } /** - * Returns the Port + * This will remove a dir and all subdirs and -files * - * @access public - * @return int The Port + * @access private + * @param string $file The dir to delete recursively + * @return mixed True on success, otherwise PEAR::Error */ - function getPort() + + function _rm_dir_recursive($dir) { - return $this->_port; + if (substr($dir, (strlen($dir) - 1), 1) != "/") { + return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'", 0); + } + $file_list = $this->_ls_files($dir); + foreach ($file_list as $file) { + $file = $dir.$file["name"]; + $res = $this->rm($file); + if ($this->isError($res)) { + return $res; + } + } + $dir_list = $this->_ls_dirs($dir); + foreach ($dir_list as $new_dir) { + $new_dir = $dir.$new_dir["name"]."/"; + $res = $this->_rm_dir_recursive($new_dir); + if ($this->isError($res)) { + return $res; + } + } + $res = $this->_rm_dir($dir); + if (!$res) { + return $res; + } else { + return true; + } } /** - * Returns the Username + * Lists up files and directories * - * @access public - * @return string The Username + * @access private + * @param string $dir The directory to list up + * @return array An array of dirs and files */ - function getUsername() + + function _ls_both($dir) { - return $this->_username; + $list_splitted = $this->_list_and_parse($dir); + if (!is_array($list_splitted["files"])) { + $list_splitted["files"] = array(); + } + if (!is_array($list_splitted["dirs"])) { + $list_splitted["dirs"] = array(); + } + $res = array(); + @array_splice($res, 0, 0, $list_splitted["files"]); + @array_splice($res, 0, 0, $list_splitted["dirs"]); + return $res; } /** - * Returns the Password + * Lists up directories * - * @access public - * @return string The Password + * @access private + * @param string $dir The directory to list up + * @return array An array of dirs */ - function getPassword() + + function _ls_dirs($dir) { - return $this->_password; + $list["dirs"] = array(); + $list = $this->_list_and_parse($dir); + return $list["dirs"]; } /** - * Returns the Transfermode + * Lists up files * - * @access public - * @return int The transfermode, either FTP_ASCII or FTP_BINARY. + * @access private + * @param string $dir The directory to list up + * @return array An array of files */ - function getMode() + + function _ls_files($dir) { - return $this->_mode; + $list = $this->_list_and_parse($dir); + if (!is_array($list["files"])) { + $list["files"] = array(); + } + return $list["files"]; } /** - * Returns, whether the connection is set to passive mode or not + * This lists up the directory-content and parses the items into well-formated arrays + * The results of this array are sorted (dirs on top, sorted by name; + * files below, sorted by name). * - * @access public - * @return bool True if passive-, false if active-mode + * @access private + * @param string $dir The directory to parse + * @return array Lists of dirs and files */ - function isPassive() + + function _list_and_parse($dir) { - return $this->_passive; + $dirs_list = array(); + $files_list = array(); + $dir_list = @ftp_rawlist($this->_handle, $dir); + foreach ($dir_list as $entry) { + if (!preg_match($this->ls_match, $entry, $m)) { + continue; + } + $entry = array(); + foreach ($this->ls_regex_map as $key=>$val) { + $entry[$key] = $m[$val]; + } + $entry['stamp'] = $this->_parse_Date($entry['date']); + + if ($entry['is_dir']) { + $dirs_list[] = $entry; + } else { + $files_list[] = $entry; + } + } + @usort($dirs_list, array("Net_FTP", "_nat_sort")); + @usort($files_list, array("Net_FTP", "_nat_sort")); + $res["dirs"] = $dirs_list; + $res["files"] = $files_list; + return $res; } /** - * Returns the mode set for a file-extension + * Lists a local directory * - * @access public - * @param string $ext The extension you wanna ask for - * @return int Either FTP_ASCII, FTP_BINARY or NULL (if not set a mode for it) + * @access private + * @param string $dir_path The dir to list + * @return array The list of dirs and files */ - function getExtensionMode($ext) + + function _ls_local($dir_path) { - return @$this->_file_extensions[$ext]; + $dir = dir($dir_path); + $dir_list = array(); + $file_list = array(); + while (false !== ($entry = $dir->read())) { + if (($entry != '.') && ($entry != '..')) { + if (is_dir($dir_path.$entry)) { + $dir_list[] = $entry; + } else { + $file_list[] = $entry; + } + } + } + $dir->close(); + $res['dirs'] = $dir_list; + $res['files'] = $file_list; + return $res; } - /** - * Adds a Net_FTP_Observer instance to the list of observers - * that are listening for messages emitted by this Net_FTP instance. + * Function for use with usort(). + * Compares the list-array-elements by name. * - * @param object $observer The Net_FTP_Observer instance to attach - * as a listener. - * @return boolean True if the observer is successfully attached. - * @access public - * @since 1.3 + * @access private */ - function attach(&$observer) + + function _nat_sort($item_1, $item_2) { - if (!is_a($observer, 'Net_FTP_Observer')) { - return false; - } - - $this->_listeners[$observer->getId()] = &$observer; - return true; + return strnatcmp($item_1['name'], $item_2['name']); } /** - * Removes a Net_FTP_Observer instance from the list of observers. + * Parse dates to timestamps * - * @param object $observer The Net_FTP_Observer instance to detach - * from the list of listeners. - * @return boolean True if the observer is successfully detached. - * @access public - * @since 1.3 + * @access private + * @param string $date Date + * @return int Timestamp */ - function detach($observer) + + function _parse_Date($date) { - if (!is_a($observer, 'Net_FTP_Observer') || - !isset($this->_listeners[$observer->getId()])) { - return false; + // Sep 10 22:06 => Sep 10, <year> 22:06 + if (preg_match('/([A-Za-z]+)[ ]+([0-9]+)[ ]+([0-9]+):([0-9]+)/', $date, $res)) { + $year = date('Y'); + $month = $res[1]; + $day = $res[2]; + $hour = $res[3]; + $minute = $res[4]; + $date = "$month $day, $year $hour:$minute"; } - - unset($this->_listeners[$observer->getId()]); - return true; - } - - /** - * Informs each registered observer instance that a new message has been - * sent. - * - * @param mixed $event A hash describing the net event. - * @access private - * @since 1.3 - */ - function _announce($event) - { - foreach ($this->_listeners as $id => $listener) { - $this->_listeners[$id]->notify($event); + // 09-10-04 => 09/10/04 + elseif (preg_match('/^\d\d-\d\d-\d\d/',$date)) { + $date = str_replace('-','/',$date); + } + $res = strtotime($date); + if (!$res) { + return $this->raiseError('Dateconversion failed.', 0); } + return $res; } } -?> +?> \ No newline at end of file
« previous php.pear.cvs (#17389) next »