6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Transfer class, wrapper for ftp/sftp/ssh
37 * Constructor - init some basic values
39 function __construct()
41 $this->file_perms
= 0644;
42 $this->dir_perms
= 0777;
44 // We use the store directory as temporary path to circumvent open basedir restrictions
45 $this->tmp_path
= PHPBB_ROOT_PATH
. 'store/';
49 * Write file to location
51 public function write_file($destination_file = '', $contents = '')
53 $destination_file = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $destination_file);
55 // need to create a temp file and then move that temp file.
56 // ftp functions can only move files around and can't create.
57 // This means that the users will need to have access to write
58 // temporary files or have write access on a folder within phpBB
59 // like the cache folder. If the user can't do either, then
60 // he/she needs to use the fsock ftp method
61 $temp_name = tempnam($this->tmp_path
, 'transfer_');
64 $fp = @fopen
($temp_name, 'w');
68 trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR
);
71 @fwrite
($fp, $contents);
74 $result = $this->overwrite_file($temp_name, $destination_file);
76 // remove temporary file now
83 * Moving file into location. If the destination file already exists it gets overwritten
85 public function overwrite_file($source_file, $destination_file)
88 * @todo generally think about overwriting files in another way, by creating a temporary file and then renaming it
89 * @todo check for the destination file existance too
91 $this->_delete($destination_file);
92 $result = $this->_put($source_file, $destination_file);
93 $this->_chmod($destination_file, $this->file_perms
);
99 * Create directory structure
101 public function make_dir($dir)
103 $dir = str_replace(PHPBB_ROOT_PATH
, '', $dir);
104 $dir = explode('/', $dir);
107 for ($i = 0, $total = sizeof($dir); $i < $total; $i++
)
111 if (strpos($dir[$i], '.') === 0)
115 $cur_dir = $dir[$i] . '/';
117 if (!file_exists(PHPBB_ROOT_PATH
. $dirs . $cur_dir))
119 // create the directory
120 $result = $this->_mkdir($dir[$i]);
121 $this->_chmod($dir[$i], $this->dir_perms
);
124 $this->_chdir($this->root_path
. $dirs . $dir[$i]);
128 $this->_chdir($this->root_path
);
131 * @todo stack result into array to make sure every path creation has been taken care of
137 * Copy file from source location to destination location
139 public function copy_file($from_loc, $to_loc)
141 $from_loc = ((strpos($from_loc, PHPBB_ROOT_PATH
) !== 0) ? PHPBB_ROOT_PATH
: '') . $from_loc;
142 $to_loc = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $to_loc);
144 if (!file_exists($from_loc))
149 $result = $this->overwrite_file($from_loc, $to_loc);
157 public function delete_file($file)
159 $file = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $file);
161 return $this->_delete($file);
166 * @todo remove child directories?
168 public function remove_dir($dir)
170 $dir = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $dir);
172 return $this->_rmdir($dir);
176 * Rename a file or folder
178 public function rename($old_handle, $new_handle)
180 $old_handle = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $old_handle);
182 return $this->_rename($old_handle, $new_handle);
186 * Check if a specified file exist...
188 public function file_exists($directory, $filename)
190 $directory = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $directory);
192 $this->_chdir($directory);
193 $result = $this->_ls();
195 if ($result !== false && is_array($result))
197 return (in_array($filename, $result)) ?
true : false;
206 public function open_session()
208 return $this->_init();
212 * Close current session
214 public function close_session()
216 return $this->_close();
220 * Determine methods able to be used
222 public static function methods()
225 $disabled_functions = explode(',', @ini_get
('disable_functions'));
227 if (@extension_loaded
('ftp'))
232 if (!in_array('fsockopen', $disabled_functions))
234 $methods[] = 'ftp_fsock';
245 class ftp
extends transfer
248 * Standard parameters for FTP session
250 function __construct($host, $username, $password, $root_path, $port = 21, $timeout = 10)
254 $this->username
= $username;
255 $this->password
= $password;
256 $this->timeout
= $timeout;
258 // Make sure $this->root_path is layed out the same way as the phpbb::$user->page['root_script_path'] value (/ at the end)
259 $this->root_path
= str_replace('\\', '/', $this->root_path
);
261 if (!empty($root_path))
263 $this->root_path
= (($root_path[0] != '/' ) ?
'/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ?
'' : '/');
266 // Init some needed values
267 parent
::__construct();
275 private function data()
278 'host' => 'localhost',
279 'username' => 'anonymous',
281 'root_path' => phpbb
::$user->page
['root_script_path'],
291 private function _init()
293 // connect to the server
294 $this->connection
= @ftp_connect
($this->host
, $this->port
, $this->timeout
);
296 if (!$this->connection
)
298 return 'ERR_CONNECTING_SERVER';
301 // attempt to turn pasv mode on
302 @ftp_pasv
($this->connection
, true);
304 // login to the server
305 if (!@ftp_login
($this->connection
, $this->username
, $this->password
))
307 return 'ERR_UNABLE_TO_LOGIN';
310 // change to the root directory
311 if (!$this->_chdir($this->root_path
))
313 return 'ERR_CHANGING_DIRECTORY';
320 * Create Directory (MKDIR)
323 private function _mkdir($dir)
325 return @ftp_mkdir
($this->connection
, $dir);
329 * Remove directory (RMDIR)
332 private function _rmdir($dir)
334 return @ftp_rmdir
($this->connection
, $dir);
341 private function _rename($old_handle, $new_handle)
343 return @ftp_rename
($this->connection
, $old_handle, $new_handle);
347 * Change current working directory (CHDIR)
350 private function _chdir($dir = '')
352 if ($dir && $dir !== '/')
354 if (substr($dir, -1, 1) == '/')
356 $dir = substr($dir, 0, -1);
360 return @ftp_chdir
($this->connection
, $dir);
364 * change file permissions (CHMOD)
367 private function _chmod($file, $perms)
369 if (function_exists('ftp_chmod'))
371 $err = @ftp_chmod
($this->connection
, $perms, $file);
375 // Unfortunatly CHMOD is not expecting an octal value...
376 // We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
377 $chmod_cmd = 'CHMOD ' . base_convert($perms, 10, 8) . ' ' . $file;
378 $err = $this->_site($chmod_cmd);
385 * Upload file to location (PUT)
388 private function _put($from_file, $to_file)
390 // get the file extension
391 $file_extension = strtolower(substr(strrchr($to_file, '.'), 1));
393 // We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
396 $to_dir = dirname($to_file);
397 $to_file = basename($to_file);
398 $this->_chdir($to_dir);
400 $result = @ftp_put
($this->connection
, $to_file, $from_file, $mode);
401 $this->_chdir($this->root_path
);
407 * Delete file (DELETE)
410 private function _delete($file)
412 return @ftp_delete
($this->connection
, $file);
416 * Close ftp session (CLOSE)
419 private function _close()
421 if (!$this->connection
)
426 return @ftp_quit
($this->connection
);
430 * Return current working directory (CWD)
431 * At the moment not used by parent class
434 private function _cwd()
436 return @ftp_pwd
($this->connection
);
440 * Return list of files in a given directory (LS)
443 private function _ls($dir = './')
445 $list = @ftp_nlist
($this->connection
, $dir);
447 // Remove path if prepended
448 foreach ($list as $key => $item)
450 // Use same separator for item and dir
451 $item = str_replace('\\', '/', $item);
452 $dir = str_replace('\\', '/', $dir);
454 if (strpos($item, $dir) === 0)
456 $item = substr($item, strlen($dir));
466 * FTP SITE command (ftp-only function)
469 private function _site($command)
471 return @ftp_site
($this->connection
, $command);
476 * FTP fsock transfer class
481 class ftp_fsock
extends transfer
483 var $data_connection;
486 * Standard parameters for FTP session
488 function __construct($host, $username, $password, $root_path, $port = 21, $timeout = 10)
492 $this->username
= $username;
493 $this->password
= $password;
494 $this->timeout
= $timeout;
496 // Make sure $this->root_path is layed out the same way as the phpbb::$user->page['root_script_path'] value (/ at the end)
497 $this->root_path
= str_replace('\\', '/', $this->root_path
);
499 if (!empty($root_path))
501 $this->root_path
= (($root_path[0] != '/' ) ?
'/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ?
'' : '/');
504 // Init some needed values
505 parent
::__construct();
513 private function data()
516 'host' => 'localhost',
517 'username' => 'anonymous',
519 'root_path' => phpbb
::$user->page
['root_script_path'],
529 private function _init()
534 // connect to the server
535 $this->connection
= @fsockopen
($this->host
, $this->port
, $errno, $errstr, $this->timeout
);
537 if (!$this->connection ||
!$this->_check_command())
539 return 'ERR_CONNECTING_SERVER';
542 @stream_set_timeout
($this->connection
, $this->timeout
);
545 if (!$this->_send_command('USER', $this->username
))
547 return 'ERR_UNABLE_TO_LOGIN';
550 if (!$this->_send_command('PASS', $this->password
))
552 return 'ERR_UNABLE_TO_LOGIN';
555 // change to the root directory
556 if (!$this->_chdir($this->root_path
))
558 return 'ERR_CHANGING_DIRECTORY';
565 * Create Directory (MKDIR)
568 private function _mkdir($dir)
570 return $this->_send_command('MKD', $dir);
574 * Remove directory (RMDIR)
577 private function _rmdir($dir)
579 return $this->_send_command('RMD', $dir);
586 private function _rename($old_handle, $new_handle)
588 $this->_send_command('RNFR', $old_handle);
589 return $this->_send_command('RNTO', $new_handle);
593 * Change current working directory (CHDIR)
596 function _chdir($dir = '')
598 if ($dir && $dir !== '/')
600 if (substr($dir, -1, 1) == '/')
602 $dir = substr($dir, 0, -1);
606 return $this->_send_command('CWD', $dir);
610 * change file permissions (CHMOD)
613 private function _chmod($file, $perms)
615 // Unfortunatly CHMOD is not expecting an octal value...
616 // We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
617 return $this->_send_command('SITE CHMOD', base_convert($perms, 10, 8) . ' ' . $file);
621 * Upload file to location (PUT)
624 private function _put($from_file, $to_file)
626 // We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
629 if (!$this->_send_command('TYPE', 'I'))
634 // open the connection to send file over
635 if (!$this->_open_data_connection())
640 $this->_send_command('STOR', $to_file, false);
643 $fp = @fopen
($from_file, 'rb');
646 @fwrite
($this->data_connection
, @fread
($fp, 4096));
651 $this->_close_data_connection();
653 return $this->_check_command();
657 * Delete file (DELETE)
660 private function _delete($file)
662 return $this->_send_command('DELE', $file);
666 * Close ftp session (CLOSE)
669 private function _close()
671 if (!$this->connection
)
676 return $this->_send_command('QUIT');
680 * Return current working directory (CWD)
681 * At the moment not used by parent class
684 private function _cwd()
686 $this->_send_command('PWD', '', false);
687 return preg_replace('#^[0-9]{3} "(.+)" .+\r\n#', '\\1', $this->_check_command(true));
691 * Return list of files in a given directory (LS)
694 private function _ls($dir = './')
696 if (!$this->_open_data_connection())
701 $this->_send_command('NLST', $dir);
704 while (!@feof
($this->data_connection
))
706 $list[] = preg_replace('#[\r\n]#', '', @fgets
($this->data_connection
, 512));
708 $this->_close_data_connection();
711 $this->_check_command();
713 // Remove path if prepended
714 foreach ($list as $key => $item)
716 // Use same separator for item and dir
717 $item = str_replace('\\', '/', $item);
718 $dir = str_replace('\\', '/', $dir);
720 if (strpos($item, $dir) === 0)
722 $item = substr($item, strlen($dir));
732 * Send a command to server (FTP fsock only function)
735 private function _send_command($command, $args = '', $check = true)
739 $command = "$command $args";
742 fwrite($this->connection
, $command . "\r\n");
744 if ($check === true && !$this->_check_command())
753 * Opens a connection to send data (FTP fosck only function)
756 private function _open_data_connection()
758 $this->_send_command('PASV', '', false);
760 if (!$ip_port = $this->_check_command(true))
765 // open the connection to start sending the file
766 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))
772 $temp = explode(',', $temp[0]);
773 $server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
774 $server_port = $temp[4] * 256 +
$temp[5];
778 if (!$this->data_connection
= @fsockopen
($server_ip, $server_port, $errno, $errstr, $this->timeout
))
782 @stream_set_timeout
($this->data_connection
, $this->timeout
);
788 * Closes a connection used to send data
791 private function _close_data_connection()
793 return @fclose
($this->data_connection
);
797 * Check to make sure command was successful (FTP fsock only function)
800 private function _check_command($return = false)
806 $result = @fgets
($this->connection
, 512);
807 $response .= $result;
809 while (substr($response, 3, 1) != ' ');
811 if (!preg_match('#^[123]#', $response))
816 return ($return) ?
$response : true;