| <?php |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (!defined('IN_PHPBB')) |
| { |
| exit; |
| } |
| |
| |
| |
| |
| |
| class transfer |
| { |
| var $connection; |
| var $host; |
| var $port; |
| var $username; |
| var $password; |
| var $timeout; |
| var $root_path; |
| var $tmp_path; |
| var $file_perms; |
| var $dir_perms; |
| |
| |
| |
| |
| function transfer() |
| { |
| global $phpbb_root_path; |
| |
| $this->file_perms = 0644; |
| $this->dir_perms = 0777; |
| |
| |
| $this->tmp_path = $phpbb_root_path . 'store/'; |
| } |
| |
| |
| |
| |
| function write_file($destination_file = '', $contents = '') |
| { |
| global $phpbb_root_path; |
| |
| $destination_file = $this->root_path . str_replace($phpbb_root_path, '', $destination_file); |
| |
| |
| |
| |
| |
| |
| |
| $temp_name = tempnam($this->tmp_path, 'transfer_'); |
| @unlink($temp_name); |
| |
| $fp = @fopen($temp_name, 'w'); |
| |
| if (!$fp) |
| { |
| trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR); |
| } |
| |
| @fwrite($fp, $contents); |
| @fclose($fp); |
| |
| $result = $this->overwrite_file($temp_name, $destination_file); |
| |
| |
| @unlink($temp_name); |
| |
| return $result; |
| } |
| |
| |
| |
| |
| function overwrite_file($source_file, $destination_file) |
| { |
| |
| |
| |
| |
| $this->_delete($destination_file); |
| $result = $this->_put($source_file, $destination_file); |
| $this->_chmod($destination_file, $this->file_perms); |
| |
| return $result; |
| } |
| |
| |
| |
| |
| function make_dir($dir) |
| { |
| global $phpbb_root_path; |
| |
| $dir = str_replace($phpbb_root_path, '', $dir); |
| $dir = explode('/', $dir); |
| $dirs = ''; |
| |
| for ($i = 0, $total = sizeof($dir); $i < $total; $i++) |
| { |
| $result = true; |
| |
| if (strpos($dir[$i], '.') === 0) |
| { |
| continue; |
| } |
| $cur_dir = $dir[$i] . '/'; |
| |
| if (!file_exists($phpbb_root_path . $dirs . $cur_dir)) |
| { |
| |
| $result = $this->_mkdir($dir[$i]); |
| $this->_chmod($dir[$i], $this->dir_perms); |
| } |
| |
| $this->_chdir($this->root_path . $dirs . $dir[$i]); |
| $dirs .= $cur_dir; |
| } |
| |
| $this->_chdir($this->root_path); |
| |
| |
| |
| |
| return $result; |
| } |
| |
| |
| |
| |
| function copy_file($from_loc, $to_loc) |
| { |
| global $phpbb_root_path; |
| |
| $from_loc = ((strpos($from_loc, $phpbb_root_path) !== 0) ? $phpbb_root_path : '') . $from_loc; |
| $to_loc = $this->root_path . str_replace($phpbb_root_path, '', $to_loc); |
| |
| if (!file_exists($from_loc)) |
| { |
| return false; |
| } |
| |
| $result = $this->overwrite_file($from_loc, $to_loc); |
| |
| return $result; |
| } |
| |
| |
| |
| |
| function delete_file($file) |
| { |
| global $phpbb_root_path; |
| |
| $file = $this->root_path . str_replace($phpbb_root_path, '', $file); |
| |
| return $this->_delete($file); |
| } |
| |
| |
| |
| |
| |
| function remove_dir($dir) |
| { |
| global $phpbb_root_path; |
| |
| $dir = $this->root_path . str_replace($phpbb_root_path, '', $dir); |
| |
| return $this->_rmdir($dir); |
| } |
| |
| |
| |
| |
| function rename($old_handle, $new_handle) |
| { |
| global $phpbb_root_path; |
| |
| $old_handle = $this->root_path . str_replace($phpbb_root_path, '', $old_handle); |
| |
| return $this->_rename($old_handle, $new_handle); |
| } |
| |
| |
| |
| |
| function file_exists($directory, $filename) |
| { |
| global $phpbb_root_path; |
| |
| $directory = $this->root_path . str_replace($phpbb_root_path, '', $directory); |
| |
| $this->_chdir($directory); |
| $result = $this->_ls(''); |
| |
| if ($result !== false && is_array($result)) |
| { |
| return (in_array($filename, $result)) ? true : false; |
| } |
| |
| return false; |
| } |
| |
| |
| |
| |
| function open_session() |
| { |
| return $this->_init(); |
| } |
| |
| |
| |
| |
| function close_session() |
| { |
| return $this->_close(); |
| } |
| |
| |
| |
| |
| function methods() |
| { |
| $methods = array(); |
| $disabled_functions = explode(',', @ini_get('disable_functions')); |
| |
| if (@extension_loaded('ftp')) |
| { |
| $methods[] = 'ftp'; |
| } |
| |
| if (!in_array('fsockopen', $disabled_functions)) |
| { |
| $methods[] = 'ftp_fsock'; |
| } |
| |
| return $methods; |
| } |
| } |
| |
| |
| |
| |
| |
| class ftp extends transfer |
| { |
| |
| |
| |
| function ftp($host, $username, $password, $root_path, $port = 21, $timeout = 10) |
| { |
| $this->host = $host; |
| $this->port = $port; |
| $this->username = $username; |
| $this->password = $password; |
| $this->timeout = $timeout; |
| |
| |
| $this->root_path = str_replace('\\', '/', $this->root_path); |
| |
| if (!empty($root_path)) |
| { |
| $this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/'); |
| } |
| |
| |
| transfer::transfer(); |
| |
| return; |
| } |
| |
| |
| |
| |
| function data() |
| { |
| global $user; |
| |
| return array( |
| 'host' => 'localhost', |
| 'username' => 'anonymous', |
| 'password' => '', |
| 'root_path' => $user->page['root_script_path'], |
| 'port' => 21, |
| 'timeout' => 10 |
| ); |
| } |
| |
| |
| |
| |
| |
| function _init() |
| { |
| |
| $this->connection = @ftp_connect($this->host, $this->port, $this->timeout); |
| |
| if (!$this->connection) |
| { |
| return 'ERR_CONNECTING_SERVER'; |
| } |
| |
| |
| @ftp_pasv($this->connection, true); |
| |
| |
| if (!@ftp_login($this->connection, $this->username, $this->password)) |
| { |
| return 'ERR_UNABLE_TO_LOGIN'; |
| } |
| |
| |
| if (!$this->_chdir($this->root_path)) |
| { |
| return 'ERR_CHANGING_DIRECTORY'; |
| } |
| |
| return true; |
| } |
| |
| |
| |
| |
| |
| function _mkdir($dir) |
| { |
| return @ftp_mkdir($this->connection, $dir); |
| } |
| |
| |
| |
| |
| |
| function _rmdir($dir) |
| { |
| return @ftp_rmdir($this->connection, $dir); |
| } |
| |
| |
| |
| |
| |
| function _rename($old_handle, $new_handle) |
| { |
| return @ftp_rename($this->connection, $old_handle, $new_handle); |
| } |
| |
| |
| |
| |
| |
| function _chdir($dir = '') |
| { |
| if ($dir && $dir !== '/') |
| { |
| if (substr($dir, -1, 1) == '/') |
| { |
| $dir = substr($dir, 0, -1); |
| } |
| } |
| |
| return @ftp_chdir($this->connection, $dir); |
| } |
| |
| |
| |
| |
| |
| function _chmod($file, $perms) |
| { |
| if (function_exists('ftp_chmod')) |
| { |
| $err = @ftp_chmod($this->connection, $perms, $file); |
| } |
| else |
| { |
| |
| |
| $chmod_cmd = 'CHMOD ' . base_convert($perms, 10, 8) . ' ' . $file; |
| $err = $this->_site($chmod_cmd); |
| } |
| |
| return $err; |
| } |
| |
| |
| |
| |
| |
| function _put($from_file, $to_file) |
| { |
| |
| $file_extension = strtolower(substr(strrchr($to_file, '.'), 1)); |
| |
| |
| $mode = FTP_BINARY; |
| |
| $to_dir = dirname($to_file); |
| $to_file = basename($to_file); |
| $this->_chdir($to_dir); |
| |
| $result = @ftp_put($this->connection, $to_file, $from_file, $mode); |
| $this->_chdir($this->root_path); |
| |
| return $result; |
| } |
| |
| |
| |
| |
| |
| function _delete($file) |
| { |
| return @ftp_delete($this->connection, $file); |
| } |
| |
| |
| |
| |
| |
| function _close() |
| { |
| if (!$this->connection) |
| { |
| return false; |
| } |
| |
| return @ftp_quit($this->connection); |
| } |
| |
| |
| |
| |
| |
| |
| function _cwd() |
| { |
| return @ftp_pwd($this->connection); |
| } |
| |
| |
| |
| |
| |
| function _ls($dir = './') |
| { |
| return @ftp_nlist($this->connection, $dir); |
| } |
| |
| |
| |
| |
| |
| function _site($command) |
| { |
| return @ftp_site($this->connection, $command); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| class ftp_fsock extends transfer |
| { |
| var $data_connection; |
| |
| |
| |
| |
| function ftp_fsock($host, $username, $password, $root_path, $port = 21, $timeout = 10) |
| { |
| $this->host = $host; |
| $this->port = $port; |
| $this->username = $username; |
| $this->password = $password; |
| $this->timeout = $timeout; |
| |
| |
| $this->root_path = str_replace('\\', '/', $this->root_path); |
| |
| if (!empty($root_path)) |
| { |
| $this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/'); |
| } |
| |
| |
| transfer::transfer(); |
| |
| return; |
| } |
| |
| |
| |
| |
| function data() |
| { |
| global $user; |
| |
| return array( |
| 'host' => 'localhost', |
| 'username' => 'anonymous', |
| 'password' => '', |
| 'root_path' => $user->page['root_script_path'], |
| 'port' => 21, |
| 'timeout' => 10 |
| ); |
| } |
| |
| |
| |
| |
| |
| function _init() |
| { |
| $errno = 0; |
| $errstr = ''; |
| |
| |
| $this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); |
| |
| if (!$this->connection || !$this->_check_command()) |
| { |
| return 'ERR_CONNECTING_SERVER'; |
| } |
| |
| @stream_set_timeout($this->connection, $this->timeout); |
| |
| |
| if (!$this->_send_command('USER', $this->username)) |
| { |
| return 'ERR_UNABLE_TO_LOGIN'; |
| } |
| |
| if (!$this->_send_command('PASS', $this->password)) |
| { |
| return 'ERR_UNABLE_TO_LOGIN'; |
| } |
| |
| |
| if (!$this->_chdir($this->root_path)) |
| { |
| return 'ERR_CHANGING_DIRECTORY'; |
| } |
| |
| return true; |
| } |
| |
| |
| |
| |
| |
| function _mkdir($dir) |
| { |
| return $this->_send_command('MKD', $dir); |
| } |
| |
| |
| |
| |
| |
| function _rmdir($dir) |
| { |
| return $this->_send_command('RMD', $dir); |
| } |
| |
| |
| |
| |
| |
| function _rename($old_handle, $new_handle) |
| { |
| $this->_send_command('RNFR', $old_handle); |
| return $this->_send_command('RNTO', $new_handle); |
| } |
| |
| |
| |
| |
| |
| function _chdir($dir = '') |
| { |
| if ($dir && $dir !== '/') |
| { |
| if (substr($dir, -1, 1) == '/') |
| { |
| $dir = substr($dir, 0, -1); |
| } |
| } |
| |
| return $this->_send_command('CWD', $dir); |
| } |
| |
| |
| |
| |
| |
| function _chmod($file, $perms) |
| { |
| |
| |
| return $this->_send_command('SITE CHMOD', base_convert($perms, 10, 8) . ' ' . $file); |
| } |
| |
| |
| |
| |
| |
| function _put($from_file, $to_file) |
| { |
| |
| |
| |
| if (!$this->_send_command('TYPE', 'I')) |
| { |
| return false; |
| } |
| |
| |
| if (!$this->_open_data_connection()) |
| { |
| return false; |
| } |
| |
| $this->_send_command('STOR', $to_file, false); |
| |
| |
| $fp = @fopen($from_file, 'rb'); |
| while (!@feof($fp)) |
| { |
| @fwrite($this->data_connection, @fread($fp, 4096)); |
| } |
| @fclose($fp); |
| |
| |
| $this->_close_data_connection(); |
| |
| return $this->_check_command(); |
| } |
| |
| |
| |
| |
| |
| function _delete($file) |
| { |
| return $this->_send_command('DELE', $file); |
| } |
| |
| |
| |
| |
| |
| function _close() |
| { |
| if (!$this->connection) |
| { |
| return false; |
| } |
| |
| return $this->_send_command('QUIT'); |
| } |
| |
| |
| |
| |
| |
| |
| function _cwd() |
| { |
| $this->_send_command('PWD', '', false); |
| return preg_replace('#^[0-9]{3} "(.+)" .+\r\n#', '\\1', $this->_check_command(true)); |
| } |
| |
| |
| |
| |
| |
| function _ls($dir = './') |
| { |
| if (!$this->_open_data_connection()) |
| { |
| return false; |
| } |
| |
| $this->_send_command('NLST', $dir); |
| |
| $list = array(); |
| while (!@feof($this->data_connection)) |
| { |
| $list[] = preg_replace('#[\r\n]#', '', @fgets($this->data_connection, 512)); |
| } |
| $this->_close_data_connection(); |
| |
| return $list; |
| } |
| |
| |
| |
| |
| |
| function _send_command($command, $args = '', $check = true) |
| { |
| if (!empty($args)) |
| { |
| $command = "$command $args"; |
| } |
| |
| fwrite($this->connection, $command . "\r\n"); |
| |
| if ($check === true && !$this->_check_command()) |
| { |
| return false; |
| } |
| |
| return true; |
| } |
| |
| |
| |
| |
| |
| function _open_data_connection() |
| { |
| $this->_send_command('PASV', '', false); |
| |
| if (!$ip_port = $this->_check_command(true)) |
| { |
| return false; |
| } |
| |
| |
| 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)) |
| { |
| |
| return false; |
| } |
| |
| $temp = explode(',', $temp[0]); |
| $server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3]; |
| $server_port = $temp[4] * 256 + $temp[5]; |
| $errno = 0; |
| $errstr = ''; |
| |
| if (!$this->data_connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $this->timeout)) |
| { |
| return false; |
| } |
| @stream_set_timeout($this->data_connection, $this->timeout); |
| |
| return true; |
| } |
| |
| |
| |
| |
| |
| function _close_data_connection() |
| { |
| return @fclose($this->data_connection); |
| } |
| |
| |
| |
| |
| |
| function _check_command($return = false) |
| { |
| $response = ''; |
| |
| do |
| { |
| $result = @fgets($this->connection, 512); |
| $response .= $result; |
| } |
| while (substr($response, 3, 1) != ' '); |
| |
| if (!preg_match('#^[123]#', $response)) |
| { |
| return false; |
| } |
| |
| return ($return) ? $response : true; |
| } |
| } |
| |
| ?> |