- added confirm box to ucp zebra (adding fried/foe)
[phpbb.git] / phpBB / includes / functions_transfer.php
blob883c9a7177b460d8ff1feb2654157acc8f438888
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 * Transfer class, wrapper for ftp/sftp/ssh
13 * @package phpBB3
15 class transfer
17 var $connection;
18 var $host;
19 var $port;
20 var $username;
21 var $password;
22 var $timeout;
23 var $root_path;
24 var $tmp_path;
25 var $file_perms;
26 var $dir_perms;
28 /**
29 * Constructor - init some basic values
31 function transfer()
33 global $phpbb_root_path;
35 $this->file_perms = '0644';
36 $this->dir_perms = '0777';
38 // We use the store directory as temporary path to circumvent open basedir restrictions
39 $this->tmp_path = $phpbb_root_path . 'store/';
42 /**
43 * Write file to location
45 function write_file($destination_file = '', $contents = '')
47 global $phpbb_root_path;
49 $destination_file = $this->root_path . str_replace($phpbb_root_path, '', $destination_file);
51 // need to create a temp file and then move that temp file.
52 // ftp functions can only move files around and can't create.
53 // This means that the users will need to have access to write
54 // temporary files or have write access on a folder within phpBB
55 // like the cache folder. If the user can't do either, then
56 // he/she needs to use the fsock ftp method
57 $temp_name = tempnam($this->tmp_path, 'transfer_');
58 @unlink($temp_name);
60 $fp = @fopen($temp_name, 'w');
62 if (!$fp)
64 trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR);
67 @fwrite($fp, $contents);
68 @fclose($fp);
70 $result = $this->overwrite_file($temp_name, $destination_file);
72 // remove temporary file now
73 @unlink($temp_name);
75 return $result;
78 /**
79 * Moving file into location. If the destination file already exists it gets overwritten
81 function overwrite_file($source_file, $destination_file)
83 /**
84 * @todo generally think about overwriting files in another way, by creating a temporary file and then renaming it
85 * @todo check for the destination file existance too
87 $this->_delete($destination_file);
88 $result = $this->_put($source_file, $destination_file);
89 $this->_chmod($destination_file, $this->file_perms);
91 return $result;
94 /**
95 * Create directory structure
97 function make_dir($dir)
99 global $phpbb_root_path;
101 $dir = str_replace($phpbb_root_path, '', $dir);
102 $dir = explode('/', $dir);
103 $dirs = '';
105 for ($i = 0, $total = sizeof($dir); $i < $total; $i++)
107 $result = true;
109 if (strpos($dir[$i], '.') === 0)
111 continue;
113 $cur_dir = $dir[$i] . '/';
115 if (!file_exists($phpbb_root_path . $dirs . $cur_dir))
117 // create the directory
118 $result = $this->_mkdir($dir[$i]);
119 $this->_chmod($dir[$i], $this->dir_perms);
122 $this->_chdir($this->root_path . $dirs . $dir[$i]);
123 $dirs .= $cur_dir;
126 $this->_chdir($this->root_path);
129 * @todo stack result into array to make sure every path creation has been taken care of
131 return $result;
135 * Copy file from source location to destination location
137 function copy_file($from_loc, $to_loc)
139 global $phpbb_root_path;
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 function delete_file($file)
159 global $phpbb_root_path;
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 function remove_dir($dir)
172 global $phpbb_root_path;
174 $dir = $this->root_path . str_replace($phpbb_root_path, '', $dir);
176 return $this->_rmdir($dir);
180 * Rename a file or folder
182 function rename($old_handle, $new_handle)
184 global $phpbb_root_path;
186 $old_handle = $this->root_path . str_replace($phpbb_root_path, '', $old_handle);
188 return $this->_rename($old_handle, $new_handle);
192 * Check if a specified file exist...
194 function file_exists($directory, $filename)
196 global $phpbb_root_path;
198 $directory = $this->root_path . str_replace($phpbb_root_path, '', $directory);
200 $this->_chdir($directory);
201 $result = $this->_ls('');
203 if ($result !== false && is_array($result))
205 return (in_array($filename, $result)) ? true : false;
208 return false;
212 * Open session
214 function open_session()
216 return $this->_init();
220 * Close current session
222 function close_session()
224 return $this->_close();
228 * Determine methods able to be used
230 function methods()
232 $methods = array();
233 $disabled_functions = explode(',', @ini_get('disable_functions'));
235 if (@extension_loaded('ftp'))
237 $methods[] = 'ftp';
240 if (!in_array('fsockopen', $disabled_functions))
242 $methods[] = 'ftp_fsock';
245 return $methods;
250 * FTP transfer class
251 * @package phpBB3
253 class ftp extends transfer
256 * Standard parameters for FTP session
258 function ftp($host, $username, $password, $root_path, $port = 21, $timeout = 10)
260 $this->host = $host;
261 $this->port = $port;
262 $this->username = $username;
263 $this->password = $password;
264 $this->timeout = $timeout;
266 // Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end)
267 $this->root_path = str_replace('\\', '/', $this->root_path);
269 if (!empty($root_path))
271 $this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/');
274 // Init some needed values
275 transfer::transfer();
277 return;
281 * Requests data
283 function data()
285 global $user;
287 return array(
288 'host' => 'localhost',
289 'username' => 'anonymous',
290 'password' => '',
291 'root_path' => $user->page['root_script_path'],
292 'port' => 21,
293 'timeout' => 10
298 * Init FTP Session
299 * @access private
301 function _init()
303 // connect to the server
304 $this->connection = @ftp_connect($this->host, $this->port, $this->timeout);
306 if (!$this->connection)
308 return 'ERR_CONNECTING_SERVER';
311 // attempt to turn pasv mode on
312 @ftp_pasv($this->connection, true);
314 // login to the server
315 if (!@ftp_login($this->connection, $this->username, $this->password))
317 return 'ERR_UNABLE_TO_LOGIN';
320 // change to the root directory
321 if (!$this->_chdir($this->root_path))
323 return 'ERR_CHANGING_DIRECTORY';
326 return true;
330 * Create Directory (MKDIR)
331 * @access private
333 function _mkdir($dir)
335 return @ftp_mkdir($this->connection, $dir);
339 * Remove directory (RMDIR)
340 * @access private
342 function _rmdir($dir)
344 return @ftp_rmdir($this->connection, $dir);
348 * Rename file
349 * @access private
351 function _rename($old_handle, $new_handle)
353 return @ftp_rename($this->connection, $old_handle, $new_handle);
357 * Change current working directory (CHDIR)
358 * @access private
360 function _chdir($dir = '')
362 if ($dir && $dir !== '/')
364 if (substr($dir, -1, 1) == '/')
366 $dir = substr($dir, 0, -1);
370 return @ftp_chdir($this->connection, $dir);
374 * change file permissions (CHMOD)
375 * @access private
377 function _chmod($file, $perms)
379 if (function_exists('ftp_chmod'))
381 $err = @ftp_chmod($this->connection, $perms, $file);
383 else
385 $chmod_cmd = 'CHMOD ' . $perms . ' ' . $file;
386 $err = $this->_site($chmod_cmd);
389 return $err;
393 * Upload file to location (PUT)
394 * @access private
396 function _put($from_file, $to_file)
398 // get the file extension
399 $file_extension = strtolower(substr(strrchr($to_file, '.'), 1));
401 // We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
402 $mode = FTP_BINARY;
404 $to_dir = dirname($to_file);
405 $to_file = basename($to_file);
406 $this->_chdir($to_dir);
408 $result = @ftp_put($this->connection, $to_file, $from_file, $mode);
409 $this->_chdir($this->root_path);
411 return $result;
415 * Delete file (DELETE)
416 * @access private
418 function _delete($file)
420 return @ftp_delete($this->connection, $file);
424 * Close ftp session (CLOSE)
425 * @access private
427 function _close()
429 if (!$this->connection)
431 return false;
434 return @ftp_quit($this->connection);
438 * Return current working directory (CWD)
439 * At the moment not used by parent class
440 * @access private
442 function _cwd()
444 return @ftp_pwd($this->connection);
448 * Return list of files in a given directory (LS)
449 * @access private
451 function _ls($dir = './')
453 return @ftp_nlist($this->connection, $dir);
457 * FTP SITE command (ftp-only function)
458 * @access private
460 function _site($command)
462 return @ftp_site($this->connection, $command);
467 * FTP fsock transfer class
469 * @author wGEric
470 * @package phpBB3
472 class ftp_fsock extends transfer
474 var $data_connection;
477 * Standard parameters for FTP session
479 function ftp_fsock($host, $username, $password, $root_path, $port = 21, $timeout = 10)
481 $this->host = $host;
482 $this->port = $port;
483 $this->username = $username;
484 $this->password = $password;
485 $this->timeout = $timeout;
487 // Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end)
488 $this->root_path = str_replace('\\', '/', $this->root_path);
490 if (!empty($root_path))
492 $this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/');
495 // Init some needed values
496 transfer::transfer();
498 return;
502 * Requests data
504 function data()
506 global $user;
508 return array(
509 'host' => 'localhost',
510 'username' => 'anonymous',
511 'password' => '',
512 'root_path' => $user->page['root_script_path'],
513 'port' => 21,
514 'timeout' => 10
519 * Init FTP Session
520 * @access private
522 function _init()
524 $errno = 0;
525 $errstr = '';
527 // connect to the server
528 $this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
530 if (!$this->connection || !$this->_check_command())
532 return 'ERR_CONNECTING_SERVER';
535 @stream_set_timeout($this->connection, $this->timeout);
537 // login
538 if (!$this->_send_command('USER', $this->username))
540 return 'ERR_UNABLE_TO_LOGIN';
543 if (!$this->_send_command('PASS', $this->password))
545 return 'ERR_UNABLE_TO_LOGIN';
548 // change to the root directory
549 if (!$this->_chdir($this->root_path))
551 return 'ERR_CHANGING_DIRECTORY';
554 return true;
558 * Create Directory (MKDIR)
559 * @access private
561 function _mkdir($dir)
563 return $this->_send_command('MKD', $dir);
567 * Remove directory (RMDIR)
568 * @access private
570 function _rmdir($dir)
572 return $this->_send_command('RMD', $dir);
576 * Rename File
577 * @access private
579 function _rename($old_handle, $new_handle)
581 $this->_send_command('RNFR', $old_handle);
582 return $this->_send_command('RNTO', $new_handle);
586 * Change current working directory (CHDIR)
587 * @access private
589 function _chdir($dir = '')
591 if ($dir && $dir !== '/')
593 if (substr($dir, -1, 1) == '/')
595 $dir = substr($dir, 0, -1);
599 return $this->_send_command('CWD', $dir);
603 * change file permissions (CHMOD)
604 * @access private
606 function _chmod($file, $perms)
608 return $this->_send_command('SITE CHMOD', $perms . ' ' . $file);
612 * Upload file to location (PUT)
613 * @access private
615 function _put($from_file, $to_file)
617 // We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
618 // 'I' == BINARY
619 // 'A' == ASCII
620 if (!$this->_send_command('TYPE', 'I'))
622 return false;
625 // open the connection to send file over
626 if (!$this->_open_data_connection())
628 return false;
631 $this->_send_command('STOR', $to_file, false);
633 // send the file
634 $fp = @fopen($from_file, 'rb');
635 while (!@feof($fp))
637 @fwrite($this->data_connection, @fread($fp, 4096));
639 @fclose($fp);
641 // close connection
642 $this->_close_data_connection();
644 return $this->_check_command();
648 * Delete file (DELETE)
649 * @access private
651 function _delete($file)
653 return $this->_send_command('DELE', $file);
657 * Close ftp session (CLOSE)
658 * @access private
660 function _close()
662 if (!$this->connection)
664 return false;
667 return $this->_send_command('QUIT');
671 * Return current working directory (CWD)
672 * At the moment not used by parent class
673 * @access private
675 function _cwd()
677 $this->_send_command('PWD', '', false);
678 return preg_replace('#^[0-9]{3} "(.+)" .+\r\n#', '\\1', $this->_check_command(true));
682 * Return list of files in a given directory (LS)
683 * @access private
685 function _ls($dir = './')
687 if (!$this->_open_data_connection())
689 return false;
692 $this->_send_command('NLST', $dir);
694 $list = array();
695 while (!@feof($this->data_connection))
697 $list[] = preg_replace('#[\r\n]#', '', @fgets($this->data_connection, 512));
699 $this->_close_data_connection();
701 return $list;
705 * Send a command to server (FTP fsock only function)
706 * @access private
708 function _send_command($command, $args = '', $check = true)
710 if (!empty($args))
712 $command = "$command $args";
715 fwrite($this->connection, $command . "\r\n");
717 if ($check === true && !$this->_check_command())
719 return false;
722 return true;
726 * Opens a connection to send data (FTP fosck only function)
727 * @access private
729 function _open_data_connection()
731 $this->_send_command('PASV', '', false);
733 if (!$ip_port = $this->_check_command(true))
735 return false;
738 // open the connection to start sending the file
739 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))
741 // bad ip and port
742 return false;
745 $temp = explode(',', $temp[0]);
746 $server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
747 $server_port = $temp[4] * 256 + $temp[5];
748 $errno = 0;
749 $errstr = '';
751 if (!$this->data_connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $this->timeout))
753 return false;
755 @stream_set_timeout($this->data_connection, $this->timeout);
757 return true;
761 * Closes a connection used to send data
762 * @access private
764 function _close_data_connection()
766 return @fclose($this->data_connection);
770 * Check to make sure command was successful (FTP fsock only function)
771 * @access private
773 function _check_command($return = false)
775 $response = '';
779 $result = @fgets($this->connection, 512);
780 $response .= $result;
782 while (substr($response, 3, 1) != ' ');
784 if (!preg_match('#^[123]#', $response))
786 return false;
789 return ($return) ? $response : true;