Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / functions_transfer.php
blob351f9517c5442b0e8a8866ef5262726466faa920
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 require_once PHPBB_ROOT_PATH . 'includes/libraries/sftp/sftp.' . PHP_EXT;
21 /**
22 * Transfer class, wrapper for ftp/sftp/ssh
23 * @package phpBB3
25 class transfer
27 var $connection;
28 var $host;
29 var $port;
30 var $username;
31 var $password;
32 var $timeout;
33 var $root_path;
34 var $tmp_path;
35 var $file_perms;
36 var $dir_perms;
38 /**
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/';
50 /**
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_');
64 @unlink($temp_name);
66 $fp = @fopen($temp_name, 'w');
68 if (!$fp)
70 trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR);
73 @fwrite($fp, $contents);
74 @fclose($fp);
76 $result = $this->overwrite_file($temp_name, $destination_file);
78 // remove temporary file now
79 @unlink($temp_name);
81 return $result;
84 /**
85 * Moving file into location. If the destination file already exists it gets overwritten
87 public function overwrite_file($source_file, $destination_file)
89 /**
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);
97 return $result;
101 * Create directory structure
103 public function make_dir($dir)
105 $dir = str_replace(PHPBB_ROOT_PATH, '', $dir);
106 $dir = explode('/', $dir);
107 $dirs = '';
109 for ($i = 0, $total = sizeof($dir); $i < $total; $i++)
111 $result = true;
113 if (strpos($dir[$i], '.') === 0)
115 continue;
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]);
127 $dirs .= $cur_dir;
130 $this->_chdir($this->root_path);
133 * @todo stack result into array to make sure every path creation has been taken care of
135 return $result;
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))
148 return false;
151 $result = $this->overwrite_file($from_loc, $to_loc);
153 return $result;
157 * Remove file
159 public function delete_file($file)
161 $file = $this->root_path . str_replace(PHPBB_ROOT_PATH, '', $file);
163 return $this->_delete($file);
167 * Remove directory
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;
202 return false;
206 * Open session
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()
226 $methods = array();
227 $disabled_functions = explode(',', @ini_get('disable_functions'));
229 if (@extension_loaded('ftp'))
231 $methods[] = 'ftp';
234 if (!in_array('fsockopen', $disabled_functions))
236 $methods[] = 'ftp_fsock';
237 $methods[] = 'sftp';
240 return $methods;
245 * FTP transfer class
246 * @package phpBB3
248 class ftp extends transfer
251 * Standard parameters for FTP session
253 function __construct($host, $username, $password, $root_path, $port = 21, $timeout = 10)
255 $this->host = $host;
256 $this->port = $port;
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();
272 return;
276 * Requests data
278 public static function data()
280 $root_path = phpbb::$url->realpath(PHPBB_ROOT_PATH);
281 return array(
282 'host' => 'localhost',
283 'username' => preg_match('#^/home/([^/]+)#', $root_path, $matches) ? $matches[1] : 'anonymous',
284 'password' => '',
285 'root_path' => phpbb::$user->page['root_script_path'],
286 'port' => 21,
287 'timeout' => 10
292 * Init FTP Session
293 * @access private
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';
320 return true;
324 * Create Directory (MKDIR)
325 * @access private
327 protected function _mkdir($dir)
329 return @ftp_mkdir($this->connection, $dir);
333 * Remove directory (RMDIR)
334 * @access private
336 protected function _rmdir($dir)
338 return @ftp_rmdir($this->connection, $dir);
342 * Rename file
343 * @access private
345 protected function _rename($old_handle, $new_handle)
347 return @ftp_rename($this->connection, $old_handle, $new_handle);
351 * Change current working directory (CHDIR)
352 * @access private
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)
369 * @access private
371 protected function _chmod($file, $perms)
373 if (function_exists('ftp_chmod'))
375 $err = @ftp_chmod($this->connection, $perms, $file);
377 else
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);
385 return $err;
389 * Upload file to location (PUT)
390 * @access private
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)
398 $mode = FTP_BINARY;
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);
407 return $result;
411 * Delete file (DELETE)
412 * @access private
414 protected function _delete($file)
416 return @ftp_delete($this->connection, $file);
420 * Close ftp session (CLOSE)
421 * @access private
423 protected function _close()
425 if (!$this->connection)
427 return false;
430 return @ftp_quit($this->connection);
434 * Return current working directory (CWD)
435 * At the moment not used by parent class
436 * @access private
438 protected function _cwd()
440 return @ftp_pwd($this->connection);
444 * Return list of files in a given directory (LS)
445 * @access private
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));
463 $list[$key] = $item;
466 return $list;
470 * FTP SITE command (ftp-only function)
471 * @access private
473 private function _site($command)
475 return @ftp_site($this->connection, $command);
480 * FTP fsock transfer class
482 * @author wGEric
483 * @package phpBB3
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)
494 $this->host = $host;
495 $this->port = $port;
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();
511 return;
515 * Requests data
517 public static function data()
519 $root_path = phpbb::$url->realpath(PHPBB_ROOT_PATH);
520 return array(
521 'host' => 'localhost',
522 'username' => preg_match('#^/home/([^/]+)#', $root_path, $matches) ? $matches[1] : 'anonymous',
523 'password' => '',
524 'root_path' => phpbb::$user->page['root_script_path'],
525 'port' => 21,
526 'timeout' => 10
531 * Init FTP Session
532 * @access private
534 protected function _init()
536 $errno = 0;
537 $errstr = '';
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);
549 // login
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';
566 return true;
570 * Create Directory (MKDIR)
571 * @access private
573 protected function _mkdir($dir)
575 return $this->_send_command('MKD', $dir);
579 * Remove directory (RMDIR)
580 * @access private
582 protected function _rmdir($dir)
584 return $this->_send_command('RMD', $dir);
588 * Rename File
589 * @access private
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)
599 * @access private
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)
616 * @access private
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)
627 * @access private
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)
632 // 'I' == BINARY
633 // 'A' == ASCII
634 if (!$this->_send_command('TYPE', 'I'))
636 return false;
639 // open the connection to send file over
640 if (!$this->_open_data_connection())
642 return false;
645 $this->_send_command('STOR', $to_file, false);
647 // send the file
648 $fp = @fopen($from_file, 'rb');
649 while (!@feof($fp))
651 @fwrite($this->data_connection, @fread($fp, 4096));
653 @fclose($fp);
655 // close connection
656 $this->_close_data_connection();
658 return $this->_check_command();
662 * Delete file (DELETE)
663 * @access private
665 protected function _delete($file)
667 return $this->_send_command('DELE', $file);
671 * Close ftp session (CLOSE)
672 * @access private
674 protected function _close()
676 if (!$this->connection)
678 return false;
681 return $this->_send_command('QUIT');
685 * Return current working directory (CWD)
686 * At the moment not used by parent class
687 * @access private
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)
697 * @access private
699 protected function _ls($dir = './')
701 if (!$this->_open_data_connection())
703 return false;
706 $this->_send_command('NLST', $dir);
708 $list = array();
709 while (!@feof($this->data_connection))
711 $list[] = preg_replace('#[\r\n]#', '', @fgets($this->data_connection, 512));
713 $this->_close_data_connection();
715 // Clear buffer
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));
730 $list[$key] = $item;
733 return $list;
737 * Send a command to server (FTP fsock only function)
738 * @access private
740 private function _send_command($command, $args = '', $check = true)
742 if (!empty($args))
744 $command = "$command $args";
747 fwrite($this->connection, $command . "\r\n");
749 if ($check === true && !$this->_check_command())
751 return false;
754 return true;
758 * Opens a connection to send data (FTP fosck only function)
759 * @access private
761 private function _open_data_connection()
763 $this->_send_command('PASV', '', false);
765 if (!$ip_port = $this->_check_command(true))
767 return false;
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))
773 // bad ip and port
774 return false;
777 $temp = explode(',', $temp[0]);
778 $server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
779 $server_port = $temp[4] * 256 + $temp[5];
780 $errno = 0;
781 $errstr = '';
783 if (!$this->data_connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $this->timeout))
785 return false;
787 @stream_set_timeout($this->data_connection, $this->timeout);
789 return true;
793 * Closes a connection used to send data
794 * @access private
796 private function _close_data_connection()
798 return @fclose($this->data_connection);
802 * Check to make sure command was successful (FTP fsock only function)
803 * @access private
805 private function _check_command($return = false)
807 $response = '';
811 $result = @fgets($this->connection, 512);
812 $response .= $result;
814 while (substr($response, 3, 1) != ' ');
816 if (!preg_match('#^[123]#', $response))
818 return false;
821 return ($return) ? $response : true;
826 * SFTP transfer class
827 * @package phpBB3
829 class sftp extends transfer
831 var $current_path;
834 * Standard parameters for SFTP session
836 function __construct($host, $username, $password, $root_path, $port = 22, $timeout = 10)
838 $this->host = $host;
839 $this->port = $port;
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();
855 return;
859 * Requests data
861 public static function data()
863 $root_path = phpbb::$url->realpath(PHPBB_ROOT_PATH);
864 return array(
865 'host' => 'localhost',
866 'username' => preg_match('#^/home/([^/]+)#', $root_path, $matches) ? $matches[1] : 'anonymous',
867 'password' => '',
868 'root_path' => $root_path,
869 'port' => 22,
870 'timeout' => 10
874 * Init SFTP Session
875 * @access private
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';
893 return true;
897 * Create Directory (MKDIR)
898 * @access private
900 protected function _mkdir($dir)
902 return $this->connection->mkdir($dir);
906 * Remove directory (RMDIR)
907 * @access private
909 protected function _rmdir($dir)
911 return $this->connection->rmdir($dir);
915 * Rename File
916 * @access private
918 protected function _rename($old_handle, $new_handle)
920 return $this->connection->rename($old_handle, $new_handle);
924 * Change current working directory (CHDIR)
925 * @access private
927 protected function _chdir($dir = '')
929 return $this->connection->chdir($dir);
933 * change file permissions (CHMOD)
934 * @access private
936 protected function _chmod($file, $perms)
938 return $this->connection->chmod($file, $perms);
942 * Upload file to location (PUT)
943 * @access private
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)
952 * @access private
954 protected function _delete($file)
956 return $this->connection->delete($file);
960 * Close ftp session (CLOSE)
961 * @access private
963 protected function _close()
965 if (!$this->connection)
967 return false;
970 return $this->connection->disconnect();
974 * Return current working directory (CWD)
975 * At the moment not used by parent class
976 * @access private
978 protected function _cwd()
980 return $this->connection->pwd();
984 * Return list of files in a given directory (LS)
985 * @access private
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;
1006 return $list;