Merge changes made in revisions #r9405 to #r9467
[phpbb.git] / phpBB / includes / functions_transfer.php
blobd7cb11cbf4ee80337805ba2c92fdadd6dc96af37
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Transfer class, wrapper for ftp/sftp/ssh
21 * @package phpBB3
23 class transfer
25 var $connection;
26 var $host;
27 var $port;
28 var $username;
29 var $password;
30 var $timeout;
31 var $root_path;
32 var $tmp_path;
33 var $file_perms;
34 var $dir_perms;
36 /**
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/';
48 /**
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_');
62 @unlink($temp_name);
64 $fp = @fopen($temp_name, 'w');
66 if (!$fp)
68 trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR);
71 @fwrite($fp, $contents);
72 @fclose($fp);
74 $result = $this->overwrite_file($temp_name, $destination_file);
76 // remove temporary file now
77 @unlink($temp_name);
79 return $result;
82 /**
83 * Moving file into location. If the destination file already exists it gets overwritten
85 public function overwrite_file($source_file, $destination_file)
87 /**
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);
95 return $result;
98 /**
99 * Create directory structure
101 public function make_dir($dir)
103 $dir = str_replace(PHPBB_ROOT_PATH, '', $dir);
104 $dir = explode('/', $dir);
105 $dirs = '';
107 for ($i = 0, $total = sizeof($dir); $i < $total; $i++)
109 $result = true;
111 if (strpos($dir[$i], '.') === 0)
113 continue;
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]);
125 $dirs .= $cur_dir;
128 $this->_chdir($this->root_path);
131 * @todo stack result into array to make sure every path creation has been taken care of
133 return $result;
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))
146 return false;
149 $result = $this->overwrite_file($from_loc, $to_loc);
151 return $result;
155 * Remove file
157 public function delete_file($file)
159 $file = $this->root_path . str_replace(PHPBB_ROOT_PATH, '', $file);
161 return $this->_delete($file);
165 * Remove directory
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;
200 return false;
204 * Open session
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()
224 $methods = array();
225 $disabled_functions = explode(',', @ini_get('disable_functions'));
227 if (@extension_loaded('ftp'))
229 $methods[] = 'ftp';
232 if (!in_array('fsockopen', $disabled_functions))
234 $methods[] = 'ftp_fsock';
237 return $methods;
242 * FTP transfer class
243 * @package phpBB3
245 class ftp extends transfer
248 * Standard parameters for FTP session
250 function __construct($host, $username, $password, $root_path, $port = 21, $timeout = 10)
252 $this->host = $host;
253 $this->port = $port;
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();
269 return;
273 * Requests data
275 private function data()
277 return array(
278 'host' => 'localhost',
279 'username' => 'anonymous',
280 'password' => '',
281 'root_path' => phpbb::$user->page['root_script_path'],
282 'port' => 21,
283 'timeout' => 10
288 * Init FTP Session
289 * @access private
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';
316 return true;
320 * Create Directory (MKDIR)
321 * @access private
323 private function _mkdir($dir)
325 return @ftp_mkdir($this->connection, $dir);
329 * Remove directory (RMDIR)
330 * @access private
332 private function _rmdir($dir)
334 return @ftp_rmdir($this->connection, $dir);
338 * Rename file
339 * @access private
341 private function _rename($old_handle, $new_handle)
343 return @ftp_rename($this->connection, $old_handle, $new_handle);
347 * Change current working directory (CHDIR)
348 * @access private
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)
365 * @access private
367 private function _chmod($file, $perms)
369 if (function_exists('ftp_chmod'))
371 $err = @ftp_chmod($this->connection, $perms, $file);
373 else
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);
381 return $err;
385 * Upload file to location (PUT)
386 * @access private
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)
394 $mode = FTP_BINARY;
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);
403 return $result;
407 * Delete file (DELETE)
408 * @access private
410 private function _delete($file)
412 return @ftp_delete($this->connection, $file);
416 * Close ftp session (CLOSE)
417 * @access private
419 private function _close()
421 if (!$this->connection)
423 return false;
426 return @ftp_quit($this->connection);
430 * Return current working directory (CWD)
431 * At the moment not used by parent class
432 * @access private
434 private function _cwd()
436 return @ftp_pwd($this->connection);
440 * Return list of files in a given directory (LS)
441 * @access private
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));
459 $list[$key] = $item;
462 return $list;
466 * FTP SITE command (ftp-only function)
467 * @access private
469 private function _site($command)
471 return @ftp_site($this->connection, $command);
476 * FTP fsock transfer class
478 * @author wGEric
479 * @package phpBB3
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)
490 $this->host = $host;
491 $this->port = $port;
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();
507 return;
511 * Requests data
513 private function data()
515 return array(
516 'host' => 'localhost',
517 'username' => 'anonymous',
518 'password' => '',
519 'root_path' => phpbb::$user->page['root_script_path'],
520 'port' => 21,
521 'timeout' => 10
526 * Init FTP Session
527 * @access private
529 private function _init()
531 $errno = 0;
532 $errstr = '';
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);
544 // login
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';
561 return true;
565 * Create Directory (MKDIR)
566 * @access private
568 private function _mkdir($dir)
570 return $this->_send_command('MKD', $dir);
574 * Remove directory (RMDIR)
575 * @access private
577 private function _rmdir($dir)
579 return $this->_send_command('RMD', $dir);
583 * Rename File
584 * @access private
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)
594 * @access private
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)
611 * @access private
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)
622 * @access private
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)
627 // 'I' == BINARY
628 // 'A' == ASCII
629 if (!$this->_send_command('TYPE', 'I'))
631 return false;
634 // open the connection to send file over
635 if (!$this->_open_data_connection())
637 return false;
640 $this->_send_command('STOR', $to_file, false);
642 // send the file
643 $fp = @fopen($from_file, 'rb');
644 while (!@feof($fp))
646 @fwrite($this->data_connection, @fread($fp, 4096));
648 @fclose($fp);
650 // close connection
651 $this->_close_data_connection();
653 return $this->_check_command();
657 * Delete file (DELETE)
658 * @access private
660 private function _delete($file)
662 return $this->_send_command('DELE', $file);
666 * Close ftp session (CLOSE)
667 * @access private
669 private function _close()
671 if (!$this->connection)
673 return false;
676 return $this->_send_command('QUIT');
680 * Return current working directory (CWD)
681 * At the moment not used by parent class
682 * @access private
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)
692 * @access private
694 private function _ls($dir = './')
696 if (!$this->_open_data_connection())
698 return false;
701 $this->_send_command('NLST', $dir);
703 $list = array();
704 while (!@feof($this->data_connection))
706 $list[] = preg_replace('#[\r\n]#', '', @fgets($this->data_connection, 512));
708 $this->_close_data_connection();
710 // Clear buffer
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));
725 $list[$key] = $item;
728 return $list;
732 * Send a command to server (FTP fsock only function)
733 * @access private
735 private function _send_command($command, $args = '', $check = true)
737 if (!empty($args))
739 $command = "$command $args";
742 fwrite($this->connection, $command . "\r\n");
744 if ($check === true && !$this->_check_command())
746 return false;
749 return true;
753 * Opens a connection to send data (FTP fosck only function)
754 * @access private
756 private function _open_data_connection()
758 $this->_send_command('PASV', '', false);
760 if (!$ip_port = $this->_check_command(true))
762 return false;
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))
768 // bad ip and port
769 return false;
772 $temp = explode(',', $temp[0]);
773 $server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
774 $server_port = $temp[4] * 256 + $temp[5];
775 $errno = 0;
776 $errstr = '';
778 if (!$this->data_connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $this->timeout))
780 return false;
782 @stream_set_timeout($this->data_connection, $this->timeout);
784 return true;
788 * Closes a connection used to send data
789 * @access private
791 private function _close_data_connection()
793 return @fclose($this->data_connection);
797 * Check to make sure command was successful (FTP fsock only function)
798 * @access private
800 private function _check_command($return = false)
802 $response = '';
806 $result = @fgets($this->connection, 512);
807 $response .= $result;
809 while (substr($response, 3, 1) != ' ');
811 if (!preg_match('#^[123]#', $response))
813 return false;
816 return ($return) ? $response : true;