indvdum@0: file_perms = 0644; indvdum@0: $this->dir_perms = 0777; indvdum@0: indvdum@0: // We use the store directory as temporary path to circumvent open basedir restrictions indvdum@0: $this->tmp_path = $phpbb_root_path . 'store/'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Write file to location indvdum@0: */ indvdum@0: function write_file($destination_file = '', $contents = '') indvdum@0: { indvdum@0: global $phpbb_root_path; indvdum@0: indvdum@0: $destination_file = $this->root_path . str_replace($phpbb_root_path, '', $destination_file); indvdum@0: indvdum@0: // need to create a temp file and then move that temp file. indvdum@0: // ftp functions can only move files around and can't create. indvdum@0: // This means that the users will need to have access to write indvdum@0: // temporary files or have write access on a folder within phpBB indvdum@0: // like the cache folder. If the user can't do either, then indvdum@0: // he/she needs to use the fsock ftp method indvdum@0: $temp_name = tempnam($this->tmp_path, 'transfer_'); indvdum@0: @unlink($temp_name); indvdum@0: indvdum@0: $fp = @fopen($temp_name, 'w'); indvdum@0: indvdum@0: if (!$fp) indvdum@0: { indvdum@0: trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR); indvdum@0: } indvdum@0: indvdum@0: @fwrite($fp, $contents); indvdum@0: @fclose($fp); indvdum@0: indvdum@0: $result = $this->overwrite_file($temp_name, $destination_file); indvdum@0: indvdum@0: // remove temporary file now indvdum@0: @unlink($temp_name); indvdum@0: indvdum@0: return $result; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Moving file into location. If the destination file already exists it gets overwritten indvdum@0: */ indvdum@0: function overwrite_file($source_file, $destination_file) indvdum@0: { indvdum@0: /** indvdum@0: * @todo generally think about overwriting files in another way, by creating a temporary file and then renaming it indvdum@0: * @todo check for the destination file existance too indvdum@0: */ indvdum@0: $this->_delete($destination_file); indvdum@0: $result = $this->_put($source_file, $destination_file); indvdum@0: $this->_chmod($destination_file, $this->file_perms); indvdum@0: indvdum@0: return $result; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Create directory structure indvdum@0: */ indvdum@0: function make_dir($dir) indvdum@0: { indvdum@0: global $phpbb_root_path; indvdum@0: indvdum@0: $dir = str_replace($phpbb_root_path, '', $dir); indvdum@0: $dir = explode('/', $dir); indvdum@0: $dirs = ''; indvdum@0: indvdum@0: for ($i = 0, $total = sizeof($dir); $i < $total; $i++) indvdum@0: { indvdum@0: $result = true; indvdum@0: indvdum@0: if (strpos($dir[$i], '.') === 0) indvdum@0: { indvdum@0: continue; indvdum@0: } indvdum@0: $cur_dir = $dir[$i] . '/'; indvdum@0: indvdum@0: if (!file_exists($phpbb_root_path . $dirs . $cur_dir)) indvdum@0: { indvdum@0: // create the directory indvdum@0: $result = $this->_mkdir($dir[$i]); indvdum@0: $this->_chmod($dir[$i], $this->dir_perms); indvdum@0: } indvdum@0: indvdum@0: $this->_chdir($this->root_path . $dirs . $dir[$i]); indvdum@0: $dirs .= $cur_dir; indvdum@0: } indvdum@0: indvdum@0: $this->_chdir($this->root_path); indvdum@0: indvdum@0: /** indvdum@0: * @todo stack result into array to make sure every path creation has been taken care of indvdum@0: */ indvdum@0: return $result; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Copy file from source location to destination location indvdum@0: */ indvdum@0: function copy_file($from_loc, $to_loc) indvdum@0: { indvdum@0: global $phpbb_root_path; indvdum@0: indvdum@0: $from_loc = ((strpos($from_loc, $phpbb_root_path) !== 0) ? $phpbb_root_path : '') . $from_loc; indvdum@0: $to_loc = $this->root_path . str_replace($phpbb_root_path, '', $to_loc); indvdum@0: indvdum@0: if (!file_exists($from_loc)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $result = $this->overwrite_file($from_loc, $to_loc); indvdum@0: indvdum@0: return $result; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Remove file indvdum@0: */ indvdum@0: function delete_file($file) indvdum@0: { indvdum@0: global $phpbb_root_path; indvdum@0: indvdum@0: $file = $this->root_path . str_replace($phpbb_root_path, '', $file); indvdum@0: indvdum@0: return $this->_delete($file); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Remove directory indvdum@0: * @todo remove child directories? indvdum@0: */ indvdum@0: function remove_dir($dir) indvdum@0: { indvdum@0: global $phpbb_root_path; indvdum@0: indvdum@0: $dir = $this->root_path . str_replace($phpbb_root_path, '', $dir); indvdum@0: indvdum@0: return $this->_rmdir($dir); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Rename a file or folder indvdum@0: */ indvdum@0: function rename($old_handle, $new_handle) indvdum@0: { indvdum@0: global $phpbb_root_path; indvdum@0: indvdum@0: $old_handle = $this->root_path . str_replace($phpbb_root_path, '', $old_handle); indvdum@0: indvdum@0: return $this->_rename($old_handle, $new_handle); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Check if a specified file exist... indvdum@0: */ indvdum@0: function file_exists($directory, $filename) indvdum@0: { indvdum@0: global $phpbb_root_path; indvdum@0: indvdum@0: $directory = $this->root_path . str_replace($phpbb_root_path, '', $directory); indvdum@0: indvdum@0: $this->_chdir($directory); indvdum@0: $result = $this->_ls(); indvdum@0: indvdum@0: if ($result !== false && is_array($result)) indvdum@0: { indvdum@0: return (in_array($filename, $result)) ? true : false; indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Open session indvdum@0: */ indvdum@0: function open_session() indvdum@0: { indvdum@0: return $this->_init(); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Close current session indvdum@0: */ indvdum@0: function close_session() indvdum@0: { indvdum@0: return $this->_close(); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Determine methods able to be used indvdum@0: */ indvdum@0: function methods() indvdum@0: { indvdum@0: $methods = array(); indvdum@0: $disabled_functions = explode(',', @ini_get('disable_functions')); indvdum@0: indvdum@0: if (@extension_loaded('ftp')) indvdum@0: { indvdum@0: $methods[] = 'ftp'; indvdum@0: } indvdum@0: indvdum@0: if (!in_array('fsockopen', $disabled_functions)) indvdum@0: { indvdum@0: $methods[] = 'ftp_fsock'; indvdum@0: } indvdum@0: indvdum@0: return $methods; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * FTP transfer class indvdum@0: * @package phpBB3 indvdum@0: */ indvdum@0: class ftp extends transfer indvdum@0: { indvdum@0: /** indvdum@0: * Standard parameters for FTP session indvdum@0: */ indvdum@0: function ftp($host, $username, $password, $root_path, $port = 21, $timeout = 10) indvdum@0: { indvdum@0: $this->host = $host; indvdum@0: $this->port = $port; indvdum@0: $this->username = $username; indvdum@0: $this->password = $password; indvdum@0: $this->timeout = $timeout; indvdum@0: indvdum@0: // Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end) indvdum@0: $this->root_path = str_replace('\\', '/', $this->root_path); indvdum@0: indvdum@0: if (!empty($root_path)) indvdum@0: { indvdum@0: $this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/'); indvdum@0: } indvdum@0: indvdum@0: // Init some needed values indvdum@0: transfer::transfer(); indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Requests data indvdum@0: */ indvdum@0: function data() indvdum@0: { indvdum@0: global $user; indvdum@0: indvdum@0: return array( indvdum@0: 'host' => 'localhost', indvdum@0: 'username' => 'anonymous', indvdum@0: 'password' => '', indvdum@0: 'root_path' => $user->page['root_script_path'], indvdum@0: 'port' => 21, indvdum@0: 'timeout' => 10 indvdum@0: ); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Init FTP Session indvdum@0: * @access private indvdum@0: */ indvdum@0: function _init() indvdum@0: { indvdum@0: // connect to the server indvdum@0: $this->connection = @ftp_connect($this->host, $this->port, $this->timeout); indvdum@0: indvdum@0: if (!$this->connection) indvdum@0: { indvdum@0: return 'ERR_CONNECTING_SERVER'; indvdum@0: } indvdum@0: indvdum@0: // login to the server indvdum@0: if (!@ftp_login($this->connection, $this->username, $this->password)) indvdum@0: { indvdum@0: return 'ERR_UNABLE_TO_LOGIN'; indvdum@0: } indvdum@0: indvdum@0: // attempt to turn pasv mode on indvdum@0: @ftp_pasv($this->connection, true); indvdum@0: indvdum@0: // change to the root directory indvdum@0: if (!$this->_chdir($this->root_path)) indvdum@0: { indvdum@0: return 'ERR_CHANGING_DIRECTORY'; indvdum@0: } indvdum@0: indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Create Directory (MKDIR) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _mkdir($dir) indvdum@0: { indvdum@0: return @ftp_mkdir($this->connection, $dir); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Remove directory (RMDIR) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _rmdir($dir) indvdum@0: { indvdum@0: return @ftp_rmdir($this->connection, $dir); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Rename file indvdum@0: * @access private indvdum@0: */ indvdum@0: function _rename($old_handle, $new_handle) indvdum@0: { indvdum@0: return @ftp_rename($this->connection, $old_handle, $new_handle); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Change current working directory (CHDIR) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _chdir($dir = '') indvdum@0: { indvdum@0: if ($dir && $dir !== '/') indvdum@0: { indvdum@0: if (substr($dir, -1, 1) == '/') indvdum@0: { indvdum@0: $dir = substr($dir, 0, -1); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return @ftp_chdir($this->connection, $dir); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * change file permissions (CHMOD) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _chmod($file, $perms) indvdum@0: { indvdum@0: if (function_exists('ftp_chmod')) indvdum@0: { indvdum@0: $err = @ftp_chmod($this->connection, $perms, $file); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Unfortunatly CHMOD is not expecting an octal value... indvdum@0: // We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;) indvdum@0: $chmod_cmd = 'CHMOD ' . base_convert($perms, 10, 8) . ' ' . $file; indvdum@0: $err = $this->_site($chmod_cmd); indvdum@0: } indvdum@0: indvdum@0: return $err; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Upload file to location (PUT) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _put($from_file, $to_file) indvdum@0: { indvdum@0: // get the file extension indvdum@0: $file_extension = strtolower(substr(strrchr($to_file, '.'), 1)); indvdum@0: indvdum@0: // We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced) indvdum@0: $mode = FTP_BINARY; indvdum@0: indvdum@0: $to_dir = dirname($to_file); indvdum@0: $to_file = basename($to_file); indvdum@0: $this->_chdir($to_dir); indvdum@0: indvdum@0: $result = @ftp_put($this->connection, $to_file, $from_file, $mode); indvdum@0: $this->_chdir($this->root_path); indvdum@0: indvdum@0: return $result; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Delete file (DELETE) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _delete($file) indvdum@0: { indvdum@0: return @ftp_delete($this->connection, $file); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Close ftp session (CLOSE) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _close() indvdum@0: { indvdum@0: if (!$this->connection) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: return @ftp_quit($this->connection); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Return current working directory (CWD) indvdum@0: * At the moment not used by parent class indvdum@0: * @access private indvdum@0: */ indvdum@0: function _cwd() indvdum@0: { indvdum@0: return @ftp_pwd($this->connection); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Return list of files in a given directory (LS) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _ls($dir = './') indvdum@0: { indvdum@0: $list = @ftp_nlist($this->connection, $dir); indvdum@0: indvdum@0: // See bug #46295 - Some FTP daemons don't like './' indvdum@0: if ($dir === './') indvdum@0: { indvdum@0: // Let's try some alternatives indvdum@0: $list = (empty($list)) ? @ftp_nlist($this->connection, '.') : $list; indvdum@0: $list = (empty($list)) ? @ftp_nlist($this->connection, '') : $list; indvdum@0: } indvdum@0: indvdum@0: // Return on error indvdum@0: if ($list === false) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // Remove path if prepended indvdum@0: foreach ($list as $key => $item) indvdum@0: { indvdum@0: // Use same separator for item and dir indvdum@0: $item = str_replace('\\', '/', $item); indvdum@0: $dir = str_replace('\\', '/', $dir); indvdum@0: indvdum@0: if (!empty($dir) && strpos($item, $dir) === 0) indvdum@0: { indvdum@0: $item = substr($item, strlen($dir)); indvdum@0: } indvdum@0: indvdum@0: $list[$key] = $item; indvdum@0: } indvdum@0: indvdum@0: return $list; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * FTP SITE command (ftp-only function) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _site($command) indvdum@0: { indvdum@0: return @ftp_site($this->connection, $command); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * FTP fsock transfer class indvdum@0: * indvdum@0: * @author wGEric indvdum@0: * @package phpBB3 indvdum@0: */ indvdum@0: class ftp_fsock extends transfer indvdum@0: { indvdum@0: var $data_connection; indvdum@0: indvdum@0: /** indvdum@0: * Standard parameters for FTP session indvdum@0: */ indvdum@0: function ftp_fsock($host, $username, $password, $root_path, $port = 21, $timeout = 10) indvdum@0: { indvdum@0: $this->host = $host; indvdum@0: $this->port = $port; indvdum@0: $this->username = $username; indvdum@0: $this->password = $password; indvdum@0: $this->timeout = $timeout; indvdum@0: indvdum@0: // Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end) indvdum@0: $this->root_path = str_replace('\\', '/', $this->root_path); indvdum@0: indvdum@0: if (!empty($root_path)) indvdum@0: { indvdum@0: $this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/'); indvdum@0: } indvdum@0: indvdum@0: // Init some needed values indvdum@0: transfer::transfer(); indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Requests data indvdum@0: */ indvdum@0: function data() indvdum@0: { indvdum@0: global $user; indvdum@0: indvdum@0: return array( indvdum@0: 'host' => 'localhost', indvdum@0: 'username' => 'anonymous', indvdum@0: 'password' => '', indvdum@0: 'root_path' => $user->page['root_script_path'], indvdum@0: 'port' => 21, indvdum@0: 'timeout' => 10 indvdum@0: ); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Init FTP Session indvdum@0: * @access private indvdum@0: */ indvdum@0: function _init() indvdum@0: { indvdum@0: $errno = 0; indvdum@0: $errstr = ''; indvdum@0: indvdum@0: // connect to the server indvdum@0: $this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); indvdum@0: indvdum@0: if (!$this->connection || !$this->_check_command()) indvdum@0: { indvdum@0: return 'ERR_CONNECTING_SERVER'; indvdum@0: } indvdum@0: indvdum@0: @stream_set_timeout($this->connection, $this->timeout); indvdum@0: indvdum@0: // login indvdum@0: if (!$this->_send_command('USER', $this->username)) indvdum@0: { indvdum@0: return 'ERR_UNABLE_TO_LOGIN'; indvdum@0: } indvdum@0: indvdum@0: if (!$this->_send_command('PASS', $this->password)) indvdum@0: { indvdum@0: return 'ERR_UNABLE_TO_LOGIN'; indvdum@0: } indvdum@0: indvdum@0: // change to the root directory indvdum@0: if (!$this->_chdir($this->root_path)) indvdum@0: { indvdum@0: return 'ERR_CHANGING_DIRECTORY'; indvdum@0: } indvdum@0: indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Create Directory (MKDIR) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _mkdir($dir) indvdum@0: { indvdum@0: return $this->_send_command('MKD', $dir); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Remove directory (RMDIR) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _rmdir($dir) indvdum@0: { indvdum@0: return $this->_send_command('RMD', $dir); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Rename File indvdum@0: * @access private indvdum@0: */ indvdum@0: function _rename($old_handle, $new_handle) indvdum@0: { indvdum@0: $this->_send_command('RNFR', $old_handle); indvdum@0: return $this->_send_command('RNTO', $new_handle); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Change current working directory (CHDIR) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _chdir($dir = '') indvdum@0: { indvdum@0: if ($dir && $dir !== '/') indvdum@0: { indvdum@0: if (substr($dir, -1, 1) == '/') indvdum@0: { indvdum@0: $dir = substr($dir, 0, -1); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return $this->_send_command('CWD', $dir); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * change file permissions (CHMOD) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _chmod($file, $perms) indvdum@0: { indvdum@0: // Unfortunatly CHMOD is not expecting an octal value... indvdum@0: // We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;) indvdum@0: return $this->_send_command('SITE CHMOD', base_convert($perms, 10, 8) . ' ' . $file); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Upload file to location (PUT) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _put($from_file, $to_file) indvdum@0: { indvdum@0: // We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced) indvdum@0: // 'I' == BINARY indvdum@0: // 'A' == ASCII indvdum@0: if (!$this->_send_command('TYPE', 'I')) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // open the connection to send file over indvdum@0: if (!$this->_open_data_connection()) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $this->_send_command('STOR', $to_file, false); indvdum@0: indvdum@0: // send the file indvdum@0: $fp = @fopen($from_file, 'rb'); indvdum@0: while (!@feof($fp)) indvdum@0: { indvdum@0: @fwrite($this->data_connection, @fread($fp, 4096)); indvdum@0: } indvdum@0: @fclose($fp); indvdum@0: indvdum@0: // close connection indvdum@0: $this->_close_data_connection(); indvdum@0: indvdum@0: return $this->_check_command(); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Delete file (DELETE) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _delete($file) indvdum@0: { indvdum@0: return $this->_send_command('DELE', $file); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Close ftp session (CLOSE) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _close() indvdum@0: { indvdum@0: if (!$this->connection) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: return $this->_send_command('QUIT'); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Return current working directory (CWD) indvdum@0: * At the moment not used by parent class indvdum@0: * @access private indvdum@0: */ indvdum@0: function _cwd() indvdum@0: { indvdum@0: $this->_send_command('PWD', '', false); indvdum@0: return preg_replace('#^[0-9]{3} "(.+)" .+\r\n#', '\\1', $this->_check_command(true)); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Return list of files in a given directory (LS) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _ls($dir = './') indvdum@0: { indvdum@0: if (!$this->_open_data_connection()) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $this->_send_command('NLST', $dir); indvdum@0: indvdum@0: $list = array(); indvdum@0: while (!@feof($this->data_connection)) indvdum@0: { indvdum@0: $filename = preg_replace('#[\r\n]#', '', @fgets($this->data_connection, 512)); indvdum@0: indvdum@0: if ($filename !== '') indvdum@0: { indvdum@0: $list[] = $filename; indvdum@0: } indvdum@0: } indvdum@0: $this->_close_data_connection(); indvdum@0: indvdum@0: // Clear buffer indvdum@0: $this->_check_command(); indvdum@0: indvdum@0: // See bug #46295 - Some FTP daemons don't like './' indvdum@0: if ($dir === './' && empty($list)) indvdum@0: { indvdum@0: // Let's try some alternatives indvdum@0: $list = $this->_ls('.'); indvdum@0: indvdum@0: if (empty($list)) indvdum@0: { indvdum@0: $list = $this->_ls(''); indvdum@0: } indvdum@0: indvdum@0: return $list; indvdum@0: } indvdum@0: indvdum@0: // Remove path if prepended indvdum@0: foreach ($list as $key => $item) indvdum@0: { indvdum@0: // Use same separator for item and dir indvdum@0: $item = str_replace('\\', '/', $item); indvdum@0: $dir = str_replace('\\', '/', $dir); indvdum@0: indvdum@0: if (!empty($dir) && strpos($item, $dir) === 0) indvdum@0: { indvdum@0: $item = substr($item, strlen($dir)); indvdum@0: } indvdum@0: indvdum@0: $list[$key] = $item; indvdum@0: } indvdum@0: indvdum@0: return $list; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Send a command to server (FTP fsock only function) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _send_command($command, $args = '', $check = true) indvdum@0: { indvdum@0: if (!empty($args)) indvdum@0: { indvdum@0: $command = "$command $args"; indvdum@0: } indvdum@0: indvdum@0: fwrite($this->connection, $command . "\r\n"); indvdum@0: indvdum@0: if ($check === true && !$this->_check_command()) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Opens a connection to send data (FTP fosck only function) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _open_data_connection() indvdum@0: { indvdum@0: // Try to find out whether we have a IPv4 or IPv6 (control) connection indvdum@0: if (function_exists('stream_socket_get_name')) indvdum@0: { indvdum@0: $socket_name = stream_socket_get_name($this->connection, true); indvdum@0: $server_ip = substr($socket_name, 0, strrpos($socket_name, ':')); indvdum@0: } indvdum@0: indvdum@0: if (!isset($server_ip) || preg_match(get_preg_expression('ipv4'), $server_ip)) indvdum@0: { indvdum@0: // Passive mode indvdum@0: $this->_send_command('PASV', '', false); indvdum@0: indvdum@0: if (!$ip_port = $this->_check_command(true)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // open the connection to start sending the file indvdum@0: if (!preg_match('#[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+#', $ip_port, $temp)) indvdum@0: { indvdum@0: // bad ip and port indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $temp = explode(',', $temp[0]); indvdum@0: $server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3]; indvdum@0: $server_port = $temp[4] * 256 + $temp[5]; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Extended Passive Mode - RFC2428 indvdum@0: $this->_send_command('EPSV', '', false); indvdum@0: indvdum@0: if (!$epsv_response = $this->_check_command(true)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // Response looks like "229 Entering Extended Passive Mode (|||12345|)" indvdum@0: // where 12345 is the tcp port for the data connection indvdum@0: if (!preg_match('#\(\|\|\|([0-9]+)\|\)#', $epsv_response, $match)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: $server_port = (int) $match[1]; indvdum@0: indvdum@0: // fsockopen expects IPv6 address in square brackets indvdum@0: $server_ip = "[$server_ip]"; indvdum@0: } indvdum@0: indvdum@0: $errno = 0; indvdum@0: $errstr = ''; indvdum@0: indvdum@0: if (!$this->data_connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $this->timeout)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: @stream_set_timeout($this->data_connection, $this->timeout); indvdum@0: indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Closes a connection used to send data indvdum@0: * @access private indvdum@0: */ indvdum@0: function _close_data_connection() indvdum@0: { indvdum@0: return @fclose($this->data_connection); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Check to make sure command was successful (FTP fsock only function) indvdum@0: * @access private indvdum@0: */ indvdum@0: function _check_command($return = false) indvdum@0: { indvdum@0: $response = ''; indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $result = @fgets($this->connection, 512); indvdum@0: $response .= $result; indvdum@0: } indvdum@0: while (substr($result, 3, 1) !== ' '); indvdum@0: indvdum@0: if (!preg_match('#^[123]#', $response)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: return ($return) ? $response : true; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: ?>