6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
19 require_once PHPBB_ROOT_PATH
. 'includes/libraries/sftp/sftp.' . PHP_EXT
;
22 * Transfer class, wrapper for ftp/sftp/ssh
39 * Constructor - init some basic values
41 function __construct()
43 $this->file_perms
= 0644;
44 $this->dir_perms
= 0777;
46 // We use the store directory as temporary path to circumvent open basedir restrictions
47 $this->tmp_path
= PHPBB_ROOT_PATH
. 'store/';
51 * Write file to location
53 public function write_file($destination_file = '', $contents = '')
55 $destination_file = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $destination_file);
57 // need to create a temp file and then move that temp file.
58 // ftp functions can only move files around and can't create.
59 // This means that the users will need to have access to write
60 // temporary files or have write access on a folder within phpBB
61 // like the cache folder. If the user can't do either, then
62 // he/she needs to use the fsock ftp method
63 $temp_name = tempnam($this->tmp_path
, 'transfer_');
66 $fp = @fopen
($temp_name, 'w');
70 trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR
);
73 @fwrite
($fp, $contents);
76 $result = $this->overwrite_file($temp_name, $destination_file);
78 // remove temporary file now
85 * Moving file into location. If the destination file already exists it gets overwritten
87 public function overwrite_file($source_file, $destination_file)
90 * @todo generally think about overwriting files in another way, by creating a temporary file and then renaming it
91 * @todo check for the destination file existance too
93 $this->_delete($destination_file);
94 $result = $this->_put($source_file, $destination_file);
95 $this->_chmod($destination_file, $this->file_perms
);
101 * Create directory structure
103 public function make_dir($dir)
105 $dir = str_replace(PHPBB_ROOT_PATH
, '', $dir);
106 $dir = explode('/', $dir);
109 for ($i = 0, $total = sizeof($dir); $i < $total; $i++
)
113 if (strpos($dir[$i], '.') === 0)
117 $cur_dir = $dir[$i] . '/';
119 if (!file_exists(PHPBB_ROOT_PATH
. $dirs . $cur_dir))
121 // create the directory
122 $result = $this->_mkdir($dir[$i]);
123 $this->_chmod($dir[$i], $this->dir_perms
);
126 $this->_chdir($this->root_path
. $dirs . $dir[$i]);
130 $this->_chdir($this->root_path
);
133 * @todo stack result into array to make sure every path creation has been taken care of
139 * Copy file from source location to destination location
141 public function copy_file($from_loc, $to_loc)
143 $from_loc = ((strpos($from_loc, PHPBB_ROOT_PATH
) !== 0) ? PHPBB_ROOT_PATH
: '') . $from_loc;
144 $to_loc = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $to_loc);
146 if (!file_exists($from_loc))
151 $result = $this->overwrite_file($from_loc, $to_loc);
159 public function delete_file($file)
161 $file = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $file);
163 return $this->_delete($file);
168 * @todo remove child directories?
170 public function remove_dir($dir)
172 $dir = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $dir);
174 return $this->_rmdir($dir);
178 * Rename a file or folder
180 public function rename($old_handle, $new_handle)
182 $old_handle = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $old_handle);
184 return $this->_rename($old_handle, $new_handle);
188 * Check if a specified file exist...
190 public function file_exists($directory, $filename)
192 $directory = $this->root_path
. str_replace(PHPBB_ROOT_PATH
, '', $directory);
194 $this->_chdir($directory);
195 $result = $this->_ls('');
197 if ($result !== false && is_array($result))
199 return (in_array($filename, $result)) ?
true : false;
208 public function open_session()
210 return $this->_init();
214 * Close current session
216 public function close_session()
218 return $this->_close();
222 * Determine methods able to be used
224 public static function methods()
227 $disabled_functions = explode(',', @ini_get
('disable_functions'));
229 if (@extension_loaded
('ftp'))
234 if (!in_array('fsockopen', $disabled_functions))
236 $methods[] = 'ftp_fsock';
248 class ftp
extends transfer
251 * Standard parameters for FTP session
253 function __construct($host, $username, $password, $root_path, $port = 21, $timeout = 10)
257 $this->username
= $username;
258 $this->password
= $password;
259 $this->timeout
= $timeout;
261 // Make sure $this->root_path is layed out the same way as the phpbb::$user->page['root_script_path'] value (/ at the end)
262 $this->root_path
= str_replace('\\', '/', $this->root_path
);
264 if (!empty($root_path))
266 $this->root_path
= (($root_path[0] != '/' ) ?
'/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ?
'' : '/');
269 // Init some needed values
270 parent
::__construct();
278 public static function data()
280 $root_path = phpbb
::$url->realpath(PHPBB_ROOT_PATH
);
282 'host' => 'localhost',
283 'username' => preg_match('#^/home/([^/]+)#', $root_path, $matches) ?
$matches[1] : 'anonymous',
285 'root_path' => phpbb
::$user->page
['root_script_path'],
295 protected function _init()
297 // connect to the server
298 $this->connection
= @ftp_connect
($this->host
, $this->port
, $this->timeout
);
300 if (!$this->connection
)
302 return 'ERR_CONNECTING_SERVER';
305 // attempt to turn pasv mode on
306 @ftp_pasv
($this->connection
, true);
308 // login to the server
309 if (!@ftp_login
($this->connection
, $this->username
, $this->password
))
311 return 'ERR_UNABLE_TO_LOGIN';
314 // change to the root directory
315 if (!$this->_chdir($this->root_path
))
317 return 'ERR_CHANGING_DIRECTORY';
324 * Create Directory (MKDIR)
327 protected function _mkdir($dir)
329 return @ftp_mkdir
($this->connection
, $dir);
333 * Remove directory (RMDIR)
336 protected function _rmdir($dir)
338 return @ftp_rmdir
($this->connection
, $dir);
345 protected function _rename($old_handle, $new_handle)
347 return @ftp_rename
($this->connection
, $old_handle, $new_handle);
351 * Change current working directory (CHDIR)
354 protected function _chdir($dir = '')
356 if ($dir && $dir !== '/')
358 if (substr($dir, -1, 1) == '/')
360 $dir = substr($dir, 0, -1);
364 return @ftp_chdir
($this->connection
, $dir);
368 * change file permissions (CHMOD)
371 protected function _chmod($file, $perms)
373 if (function_exists('ftp_chmod'))
375 $err = @ftp_chmod
($this->connection
, $perms, $file);
379 // Unfortunatly CHMOD is not expecting an octal value...
380 // We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
381 $chmod_cmd = 'CHMOD ' . base_convert($perms, 10, 8) . ' ' . $file;
382 $err = $this->_site($chmod_cmd);
389 * Upload file to location (PUT)
392 protected function _put($from_file, $to_file)
394 // get the file extension
395 $file_extension = strtolower(substr(strrchr($to_file, '.'), 1));
397 // We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
400 $to_dir = dirname($to_file);
401 $to_file = basename($to_file);
402 $this->_chdir($to_dir);
404 $result = @ftp_put
($this->connection
, $to_file, $from_file, $mode);
405 $this->_chdir($this->root_path
);
411 * Delete file (DELETE)
414 protected function _delete($file)
416 return @ftp_delete
($this->connection
, $file);
420 * Close ftp session (CLOSE)
423 protected function _close()
425 if (!$this->connection
)
430 return @ftp_quit
($this->connection
);
434 * Return current working directory (CWD)
435 * At the moment not used by parent class
438 protected function _cwd()
440 return @ftp_pwd
($this->connection
);
444 * Return list of files in a given directory (LS)
447 protected function _ls($dir = './')
449 $list = @ftp_nlist
($this->connection
, $dir);
451 // Remove path if prepended
452 foreach ($list as $key => $item)
454 // Use same separator for item and dir
455 $item = str_replace('\\', '/', $item);
456 $dir = str_replace('\\', '/', $dir);
458 if (!empty($dir) && strpos($item, $dir) === 0)
460 $item = substr($item, strlen($dir));
470 * FTP SITE command (ftp-only function)
473 private function _site($command)
475 return @ftp_site
($this->connection
, $command);
480 * FTP fsock transfer class
485 class ftp_fsock
extends transfer
487 var $data_connection;
490 * Standard parameters for FTP session
492 function __construct($host, $username, $password, $root_path, $port = 21, $timeout = 10)
496 $this->username
= $username;
497 $this->password
= $password;
498 $this->timeout
= $timeout;
500 // Make sure $this->root_path is layed out the same way as the phpbb::$user->page['root_script_path'] value (/ at the end)
501 $this->root_path
= str_replace('\\', '/', $this->root_path
);
503 if (!empty($root_path))
505 $this->root_path
= (($root_path[0] != '/' ) ?
'/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ?
'' : '/');
508 // Init some needed values
509 parent
::__construct();
517 public static function data()
519 $root_path = phpbb
::$url->realpath(PHPBB_ROOT_PATH
);
521 'host' => 'localhost',
522 'username' => preg_match('#^/home/([^/]+)#', $root_path, $matches) ?
$matches[1] : 'anonymous',
524 'root_path' => phpbb
::$user->page
['root_script_path'],
534 protected function _init()
539 // connect to the server
540 $this->connection
= @fsockopen
($this->host
, $this->port
, $errno, $errstr, $this->timeout
);
542 if (!$this->connection ||
!$this->_check_command())
544 return 'ERR_CONNECTING_SERVER';
547 @stream_set_timeout
($this->connection
, $this->timeout
);
550 if (!$this->_send_command('USER', $this->username
))
552 return 'ERR_UNABLE_TO_LOGIN';
555 if (!$this->_send_command('PASS', $this->password
))
557 return 'ERR_UNABLE_TO_LOGIN';
560 // change to the root directory
561 if (!$this->_chdir($this->root_path
))
563 return 'ERR_CHANGING_DIRECTORY';
570 * Create Directory (MKDIR)
573 protected function _mkdir($dir)
575 return $this->_send_command('MKD', $dir);
579 * Remove directory (RMDIR)
582 protected function _rmdir($dir)
584 return $this->_send_command('RMD', $dir);
591 protected function _rename($old_handle, $new_handle)
593 $this->_send_command('RNFR', $old_handle);
594 return $this->_send_command('RNTO', $new_handle);
598 * Change current working directory (CHDIR)
601 protected function _chdir($dir = '')
603 if ($dir && $dir !== '/')
605 if (substr($dir, -1, 1) == '/')
607 $dir = substr($dir, 0, -1);
611 return $this->_send_command('CWD', $dir);
615 * change file permissions (CHMOD)
618 protected function _chmod($file, $perms)
620 // Unfortunatly CHMOD is not expecting an octal value...
621 // We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
622 return $this->_send_command('SITE CHMOD', base_convert($perms, 10, 8) . ' ' . $file);
626 * Upload file to location (PUT)
629 protected function _put($from_file, $to_file)
631 // We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
634 if (!$this->_send_command('TYPE', 'I'))
639 // open the connection to send file over
640 if (!$this->_open_data_connection())
645 $this->_send_command('STOR', $to_file, false);
648 $fp = @fopen
($from_file, 'rb');
651 @fwrite
($this->data_connection
, @fread
($fp, 4096));
656 $this->_close_data_connection();
658 return $this->_check_command();
662 * Delete file (DELETE)
665 protected function _delete($file)
667 return $this->_send_command('DELE', $file);
671 * Close ftp session (CLOSE)
674 protected function _close()
676 if (!$this->connection
)
681 return $this->_send_command('QUIT');
685 * Return current working directory (CWD)
686 * At the moment not used by parent class
689 protected function _cwd()
691 $this->_send_command('PWD', '', false);
692 return preg_replace('#^[0-9]{3} "(.+)" .+\r\n#', '\\1', $this->_check_command(true));
696 * Return list of files in a given directory (LS)
699 protected function _ls($dir = './')
701 if (!$this->_open_data_connection())
706 $this->_send_command('NLST', $dir);
709 while (!@feof
($this->data_connection
))
711 $list[] = preg_replace('#[\r\n]#', '', @fgets
($this->data_connection
, 512));
713 $this->_close_data_connection();
716 $this->_check_command();
718 // Remove path if prepended
719 foreach ($list as $key => $item)
721 // Use same separator for item and dir
722 $item = str_replace('\\', '/', $item);
723 $dir = str_replace('\\', '/', $dir);
725 if (strpos($item, $dir) === 0)
727 $item = substr($item, strlen($dir));
737 * Send a command to server (FTP fsock only function)
740 private function _send_command($command, $args = '', $check = true)
744 $command = "$command $args";
747 fwrite($this->connection
, $command . "\r\n");
749 if ($check === true && !$this->_check_command())
758 * Opens a connection to send data (FTP fosck only function)
761 private function _open_data_connection()
763 $this->_send_command('PASV', '', false);
765 if (!$ip_port = $this->_check_command(true))
770 // open the connection to start sending the file
771 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))
777 $temp = explode(',', $temp[0]);
778 $server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
779 $server_port = $temp[4] * 256 +
$temp[5];
783 if (!$this->data_connection
= @fsockopen
($server_ip, $server_port, $errno, $errstr, $this->timeout
))
787 @stream_set_timeout
($this->data_connection
, $this->timeout
);
793 * Closes a connection used to send data
796 private function _close_data_connection()
798 return @fclose
($this->data_connection
);
802 * Check to make sure command was successful (FTP fsock only function)
805 private function _check_command($return = false)
811 $result = @fgets
($this->connection
, 512);
812 $response .= $result;
814 while (substr($response, 3, 1) != ' ');
816 if (!preg_match('#^[123]#', $response))
821 return ($return) ?
$response : true;
826 * SFTP transfer class
829 class sftp
extends transfer
834 * Standard parameters for SFTP session
836 function __construct($host, $username, $password, $root_path, $port = 22, $timeout = 10)
840 $this->username
= $username;
841 $this->password
= $password;
842 $this->timeout
= $timeout;
844 // Make sure $this->root_path is layed out the same way as the phpbb::$user->page['root_script_path'] value (/ at the end)
845 $this->root_path
= str_replace('\\', '/', $this->root_path
);
847 if (!empty($root_path))
849 $this->root_path
= (($root_path[0] != '/' ) ?
'/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ?
'' : '/');
852 // Init some needed values
853 parent
::__construct();
861 public static function data()
863 $root_path = phpbb
::$url->realpath(PHPBB_ROOT_PATH
);
865 'host' => 'localhost',
866 'username' => preg_match('#^/home/([^/]+)#', $root_path, $matches) ?
$matches[1] : 'anonymous',
868 'root_path' => $root_path,
877 protected function _init()
879 // connect to the server
880 $this->connection
= new ssh2_sftp($this->host
, $this->port
, $this->timeout
);
882 if (!$this->connection
->login($this->username
, $this->password
))
884 return 'ERR_CONNECTING_SERVER';
887 // change to the root directory
888 if (!$this->_chdir($this->root_path
))
890 return 'ERR_CHANGING_DIRECTORY';
897 * Create Directory (MKDIR)
900 protected function _mkdir($dir)
902 return $this->connection
->mkdir($dir);
906 * Remove directory (RMDIR)
909 protected function _rmdir($dir)
911 return $this->connection
->rmdir($dir);
918 protected function _rename($old_handle, $new_handle)
920 return $this->connection
->rename($old_handle, $new_handle);
924 * Change current working directory (CHDIR)
927 protected function _chdir($dir = '')
929 return $this->connection
->chdir($dir);
933 * change file permissions (CHMOD)
936 protected function _chmod($file, $perms)
938 return $this->connection
->chmod($file, $perms);
942 * Upload file to location (PUT)
945 protected function _put($from_file, $to_file)
947 return $this->connection
->put($to_file, $from_file, NET_SFTP_LOCAL_FILE
);
951 * Delete file (DELETE)
954 protected function _delete($file)
956 return $this->connection
->delete($file);
960 * Close ftp session (CLOSE)
963 protected function _close()
965 if (!$this->connection
)
970 return $this->connection
->disconnect();
974 * Return current working directory (CWD)
975 * At the moment not used by parent class
978 protected function _cwd()
980 return $this->connection
->pwd();
984 * Return list of files in a given directory (LS)
987 protected function _ls($dir = './')
989 $list = $this->connection
->nlist($dir);
991 // Remove path if prepended
992 foreach ($list as $key => $item)
994 // Use same separator for item and dir
995 $item = str_replace('\\', '/', $item);
996 $dir = str_replace('\\', '/', $dir);
998 if (strpos($item, $dir) === 0)
1000 $item = substr($item, strlen($dir));
1003 $list[$key] = $item;