6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * Transfer class, wrapper for ftp/sftp/ssh
29 * Constructor - init some basic values
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/';
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_');
60 $fp = @fopen
($temp_name, 'w');
64 trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR
);
67 @fwrite
($fp, $contents);
70 $result = $this->overwrite_file($temp_name, $destination_file);
72 // remove temporary file now
79 * Moving file into location. If the destination file already exists it gets overwritten
81 function overwrite_file($source_file, $destination_file)
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
);
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);
105 for ($i = 0, $total = sizeof($dir); $i < $total; $i++
)
109 if (strpos($dir[$i], '.') === 0)
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]);
126 $this->_chdir($this->root_path
);
129 * @todo stack result into array to make sure every path creation has been taken care of
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))
149 $result = $this->overwrite_file($from_loc, $to_loc);
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);
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;
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
233 $disabled_functions = explode(',', @ini_get
('disable_functions'));
235 if (@extension_loaded
('ftp'))
240 if (!in_array('fsockopen', $disabled_functions))
242 $methods[] = 'ftp_fsock';
253 class ftp
extends transfer
256 * Standard parameters for FTP session
258 function ftp($host, $username, $password, $root_path, $port = 21, $timeout = 10)
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();
288 'host' => 'localhost',
289 'username' => 'anonymous',
291 'root_path' => $user->page
['root_script_path'],
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';
330 * Create Directory (MKDIR)
333 function _mkdir($dir)
335 return @ftp_mkdir
($this->connection
, $dir);
339 * Remove directory (RMDIR)
342 function _rmdir($dir)
344 return @ftp_rmdir
($this->connection
, $dir);
351 function _rename($old_handle, $new_handle)
353 return @ftp_rename
($this->connection
, $old_handle, $new_handle);
357 * Change current working directory (CHDIR)
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)
377 function _chmod($file, $perms)
379 if (function_exists('ftp_chmod'))
381 $err = @ftp_chmod
($this->connection
, $perms, $file);
385 $chmod_cmd = 'CHMOD ' . $perms . ' ' . $file;
386 $err = $this->_site($chmod_cmd);
393 * Upload file to location (PUT)
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)
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
);
415 * Delete file (DELETE)
418 function _delete($file)
420 return @ftp_delete
($this->connection
, $file);
424 * Close ftp session (CLOSE)
429 if (!$this->connection
)
434 return @ftp_quit
($this->connection
);
438 * Return current working directory (CWD)
439 * At the moment not used by parent class
444 return @ftp_pwd
($this->connection
);
448 * Return list of files in a given directory (LS)
451 function _ls($dir = './')
453 return @ftp_nlist
($this->connection
, $dir);
457 * FTP SITE command (ftp-only function)
460 function _site($command)
462 return @ftp_site
($this->connection
, $command);
467 * FTP fsock transfer class
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)
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();
509 'host' => 'localhost',
510 'username' => 'anonymous',
512 'root_path' => $user->page
['root_script_path'],
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
);
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';
558 * Create Directory (MKDIR)
561 function _mkdir($dir)
563 return $this->_send_command('MKD', $dir);
567 * Remove directory (RMDIR)
570 function _rmdir($dir)
572 return $this->_send_command('RMD', $dir);
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)
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)
606 function _chmod($file, $perms)
608 return $this->_send_command('SITE CHMOD', $perms . ' ' . $file);
612 * Upload file to location (PUT)
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)
620 if (!$this->_send_command('TYPE', 'I'))
625 // open the connection to send file over
626 if (!$this->_open_data_connection())
631 $this->_send_command('STOR', $to_file, false);
634 $fp = @fopen
($from_file, 'rb');
637 @fwrite
($this->data_connection
, @fread
($fp, 4096));
642 $this->_close_data_connection();
644 return $this->_check_command();
648 * Delete file (DELETE)
651 function _delete($file)
653 return $this->_send_command('DELE', $file);
657 * Close ftp session (CLOSE)
662 if (!$this->connection
)
667 return $this->_send_command('QUIT');
671 * Return current working directory (CWD)
672 * At the moment not used by parent class
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)
685 function _ls($dir = './')
687 if (!$this->_open_data_connection())
692 $this->_send_command('NLST', $dir);
695 while (!@feof
($this->data_connection
))
697 $list[] = preg_replace('#[\r\n]#', '', @fgets
($this->data_connection
, 512));
699 $this->_close_data_connection();
705 * Send a command to server (FTP fsock only function)
708 function _send_command($command, $args = '', $check = true)
712 $command = "$command $args";
715 fwrite($this->connection
, $command . "\r\n");
717 if ($check === true && !$this->_check_command())
726 * Opens a connection to send data (FTP fosck only function)
729 function _open_data_connection()
731 $this->_send_command('PASV', '', false);
733 if (!$ip_port = $this->_check_command(true))
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))
745 $temp = explode(',', $temp[0]);
746 $server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
747 $server_port = $temp[4] * 256 +
$temp[5];
751 if (!$this->data_connection
= @fsockopen
($server_ip, $server_port, $errno, $errstr, $this->timeout
))
755 @stream_set_timeout
($this->data_connection
, $this->timeout
);
761 * Closes a connection used to send data
764 function _close_data_connection()
766 return @fclose
($this->data_connection
);
770 * Check to make sure command was successful (FTP fsock only function)
773 function _check_command($return = false)
779 $result = @fgets
($this->connection
, 512);
780 $response .= $result;
782 while (substr($response, 3, 1) != ' ');
784 if (!preg_match('#^[123]#', $response))
789 return ($return) ?
$response : true;