includes/functions_transfer.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 02:39:20 +0400
changeset 18 36b05a593b41
parent 0 ceef6d64c21d
permissions -rw-r--r--
Fix username allowed characters (like Cubometry game)
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Transfer class, wrapper for ftp/sftp/ssh
indvdum@0
    21
* @package phpBB3
indvdum@0
    22
*/
indvdum@0
    23
class transfer
indvdum@0
    24
{
indvdum@0
    25
	var $connection;
indvdum@0
    26
	var $host;
indvdum@0
    27
	var $port;
indvdum@0
    28
	var $username;
indvdum@0
    29
	var $password;
indvdum@0
    30
	var $timeout;
indvdum@0
    31
	var $root_path;
indvdum@0
    32
	var $tmp_path;
indvdum@0
    33
	var $file_perms;
indvdum@0
    34
	var $dir_perms;
indvdum@0
    35
indvdum@0
    36
	/**
indvdum@0
    37
	* Constructor - init some basic values
indvdum@0
    38
	*/
indvdum@0
    39
	function transfer()
indvdum@0
    40
	{
indvdum@0
    41
		global $phpbb_root_path;
indvdum@0
    42
indvdum@0
    43
		$this->file_perms	= 0644;
indvdum@0
    44
		$this->dir_perms	= 0777;
indvdum@0
    45
indvdum@0
    46
		// We use the store directory as temporary path to circumvent open basedir restrictions
indvdum@0
    47
		$this->tmp_path = $phpbb_root_path . 'store/';
indvdum@0
    48
	}
indvdum@0
    49
indvdum@0
    50
	/**
indvdum@0
    51
	* Write file to location
indvdum@0
    52
	*/
indvdum@0
    53
	function write_file($destination_file = '', $contents = '')
indvdum@0
    54
	{
indvdum@0
    55
		global $phpbb_root_path;
indvdum@0
    56
indvdum@0
    57
		$destination_file = $this->root_path . str_replace($phpbb_root_path, '', $destination_file);
indvdum@0
    58
indvdum@0
    59
		// need to create a temp file and then move that temp file.
indvdum@0
    60
		// ftp functions can only move files around and can't create.
indvdum@0
    61
		// This means that the users will need to have access to write
indvdum@0
    62
		// temporary files or have write access on a folder within phpBB
indvdum@0
    63
		// like the cache folder. If the user can't do either, then
indvdum@0
    64
		// he/she needs to use the fsock ftp method
indvdum@0
    65
		$temp_name = tempnam($this->tmp_path, 'transfer_');
indvdum@0
    66
		@unlink($temp_name);
indvdum@0
    67
indvdum@0
    68
		$fp = @fopen($temp_name, 'w');
indvdum@0
    69
indvdum@0
    70
		if (!$fp)
indvdum@0
    71
		{
indvdum@0
    72
			trigger_error('Unable to create temporary file ' . $temp_name, E_USER_ERROR);
indvdum@0
    73
		}
indvdum@0
    74
indvdum@0
    75
		@fwrite($fp, $contents);
indvdum@0
    76
		@fclose($fp);
indvdum@0
    77
indvdum@0
    78
		$result = $this->overwrite_file($temp_name, $destination_file);
indvdum@0
    79
indvdum@0
    80
		// remove temporary file now
indvdum@0
    81
		@unlink($temp_name);
indvdum@0
    82
indvdum@0
    83
		return $result;
indvdum@0
    84
	}
indvdum@0
    85
indvdum@0
    86
	/**
indvdum@0
    87
	* Moving file into location. If the destination file already exists it gets overwritten
indvdum@0
    88
	*/
indvdum@0
    89
	function overwrite_file($source_file, $destination_file)
indvdum@0
    90
	{
indvdum@0
    91
		/**
indvdum@0
    92
		* @todo generally think about overwriting files in another way, by creating a temporary file and then renaming it
indvdum@0
    93
		* @todo check for the destination file existance too
indvdum@0
    94
		*/
indvdum@0
    95
		$this->_delete($destination_file);
indvdum@0
    96
		$result = $this->_put($source_file, $destination_file);
indvdum@0
    97
		$this->_chmod($destination_file, $this->file_perms);
indvdum@0
    98
indvdum@0
    99
		return $result;
indvdum@0
   100
	}
indvdum@0
   101
indvdum@0
   102
	/**
indvdum@0
   103
	* Create directory structure
indvdum@0
   104
	*/
indvdum@0
   105
	function make_dir($dir)
indvdum@0
   106
	{
indvdum@0
   107
		global $phpbb_root_path;
indvdum@0
   108
indvdum@0
   109
		$dir = str_replace($phpbb_root_path, '', $dir);
indvdum@0
   110
		$dir = explode('/', $dir);
indvdum@0
   111
		$dirs = '';
indvdum@0
   112
indvdum@0
   113
		for ($i = 0, $total = sizeof($dir); $i < $total; $i++)
indvdum@0
   114
		{
indvdum@0
   115
			$result = true;
indvdum@0
   116
indvdum@0
   117
			if (strpos($dir[$i], '.') === 0)
indvdum@0
   118
			{
indvdum@0
   119
				continue;
indvdum@0
   120
			}
indvdum@0
   121
			$cur_dir = $dir[$i] . '/';
indvdum@0
   122
indvdum@0
   123
			if (!file_exists($phpbb_root_path . $dirs . $cur_dir))
indvdum@0
   124
			{
indvdum@0
   125
				// create the directory
indvdum@0
   126
				$result = $this->_mkdir($dir[$i]);
indvdum@0
   127
				$this->_chmod($dir[$i], $this->dir_perms);
indvdum@0
   128
			}
indvdum@0
   129
indvdum@0
   130
			$this->_chdir($this->root_path . $dirs . $dir[$i]);
indvdum@0
   131
			$dirs .= $cur_dir;
indvdum@0
   132
		}
indvdum@0
   133
indvdum@0
   134
		$this->_chdir($this->root_path);
indvdum@0
   135
indvdum@0
   136
		/**
indvdum@0
   137
		* @todo stack result into array to make sure every path creation has been taken care of
indvdum@0
   138
		*/
indvdum@0
   139
		return $result;
indvdum@0
   140
	}
indvdum@0
   141
indvdum@0
   142
	/**
indvdum@0
   143
	* Copy file from source location to destination location
indvdum@0
   144
	*/
indvdum@0
   145
	function copy_file($from_loc, $to_loc)
indvdum@0
   146
	{
indvdum@0
   147
		global $phpbb_root_path;
indvdum@0
   148
indvdum@0
   149
		$from_loc = ((strpos($from_loc, $phpbb_root_path) !== 0) ? $phpbb_root_path : '') . $from_loc;
indvdum@0
   150
		$to_loc = $this->root_path . str_replace($phpbb_root_path, '', $to_loc);
indvdum@0
   151
indvdum@0
   152
		if (!file_exists($from_loc))
indvdum@0
   153
		{
indvdum@0
   154
			return false;
indvdum@0
   155
		}
indvdum@0
   156
indvdum@0
   157
		$result = $this->overwrite_file($from_loc, $to_loc);
indvdum@0
   158
indvdum@0
   159
		return $result;
indvdum@0
   160
	}
indvdum@0
   161
indvdum@0
   162
	/**
indvdum@0
   163
	* Remove file
indvdum@0
   164
	*/
indvdum@0
   165
	function delete_file($file)
indvdum@0
   166
	{
indvdum@0
   167
		global $phpbb_root_path;
indvdum@0
   168
indvdum@0
   169
		$file = $this->root_path . str_replace($phpbb_root_path, '', $file);
indvdum@0
   170
indvdum@0
   171
		return $this->_delete($file);
indvdum@0
   172
	}
indvdum@0
   173
indvdum@0
   174
	/**
indvdum@0
   175
	* Remove directory
indvdum@0
   176
	* @todo remove child directories?
indvdum@0
   177
	*/
indvdum@0
   178
	function remove_dir($dir)
indvdum@0
   179
	{
indvdum@0
   180
		global $phpbb_root_path;
indvdum@0
   181
indvdum@0
   182
		$dir = $this->root_path . str_replace($phpbb_root_path, '', $dir);
indvdum@0
   183
indvdum@0
   184
		return $this->_rmdir($dir);
indvdum@0
   185
	}
indvdum@0
   186
indvdum@0
   187
	/**
indvdum@0
   188
	* Rename a file or folder
indvdum@0
   189
	*/
indvdum@0
   190
	function rename($old_handle, $new_handle)
indvdum@0
   191
	{
indvdum@0
   192
		global $phpbb_root_path;
indvdum@0
   193
indvdum@0
   194
		$old_handle = $this->root_path . str_replace($phpbb_root_path, '', $old_handle);
indvdum@0
   195
indvdum@0
   196
		return $this->_rename($old_handle, $new_handle);
indvdum@0
   197
	}
indvdum@0
   198
indvdum@0
   199
	/**
indvdum@0
   200
	* Check if a specified file exist...
indvdum@0
   201
	*/
indvdum@0
   202
	function file_exists($directory, $filename)
indvdum@0
   203
	{
indvdum@0
   204
		global $phpbb_root_path;
indvdum@0
   205
indvdum@0
   206
		$directory = $this->root_path . str_replace($phpbb_root_path, '', $directory);
indvdum@0
   207
indvdum@0
   208
		$this->_chdir($directory);
indvdum@0
   209
		$result = $this->_ls();
indvdum@0
   210
indvdum@0
   211
		if ($result !== false && is_array($result))
indvdum@0
   212
		{
indvdum@0
   213
			return (in_array($filename, $result)) ? true : false;
indvdum@0
   214
		}
indvdum@0
   215
indvdum@0
   216
		return false;
indvdum@0
   217
	}
indvdum@0
   218
indvdum@0
   219
	/**
indvdum@0
   220
	* Open session
indvdum@0
   221
	*/
indvdum@0
   222
	function open_session()
indvdum@0
   223
	{
indvdum@0
   224
		return $this->_init();
indvdum@0
   225
	}
indvdum@0
   226
indvdum@0
   227
	/**
indvdum@0
   228
	* Close current session
indvdum@0
   229
	*/
indvdum@0
   230
	function close_session()
indvdum@0
   231
	{
indvdum@0
   232
		return $this->_close();
indvdum@0
   233
	}
indvdum@0
   234
indvdum@0
   235
	/**
indvdum@0
   236
	* Determine methods able to be used
indvdum@0
   237
	*/
indvdum@0
   238
	function methods()
indvdum@0
   239
	{
indvdum@0
   240
		$methods = array();
indvdum@0
   241
		$disabled_functions = explode(',', @ini_get('disable_functions'));
indvdum@0
   242
indvdum@0
   243
		if (@extension_loaded('ftp'))
indvdum@0
   244
		{
indvdum@0
   245
			$methods[] = 'ftp';
indvdum@0
   246
		}
indvdum@0
   247
indvdum@0
   248
		if (!in_array('fsockopen', $disabled_functions))
indvdum@0
   249
		{
indvdum@0
   250
			$methods[] = 'ftp_fsock';
indvdum@0
   251
		}
indvdum@0
   252
indvdum@0
   253
		return $methods;
indvdum@0
   254
	}
indvdum@0
   255
}
indvdum@0
   256
indvdum@0
   257
/**
indvdum@0
   258
* FTP transfer class
indvdum@0
   259
* @package phpBB3
indvdum@0
   260
*/
indvdum@0
   261
class ftp extends transfer
indvdum@0
   262
{
indvdum@0
   263
	/**
indvdum@0
   264
	* Standard parameters for FTP session
indvdum@0
   265
	*/
indvdum@0
   266
	function ftp($host, $username, $password, $root_path, $port = 21, $timeout = 10)
indvdum@0
   267
	{
indvdum@0
   268
		$this->host			= $host;
indvdum@0
   269
		$this->port			= $port;
indvdum@0
   270
		$this->username		= $username;
indvdum@0
   271
		$this->password		= $password;
indvdum@0
   272
		$this->timeout		= $timeout;
indvdum@0
   273
indvdum@0
   274
		// Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end)
indvdum@0
   275
		$this->root_path	= str_replace('\\', '/', $this->root_path);
indvdum@0
   276
indvdum@0
   277
		if (!empty($root_path))
indvdum@0
   278
		{
indvdum@0
   279
			$this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/');
indvdum@0
   280
		}
indvdum@0
   281
indvdum@0
   282
		// Init some needed values
indvdum@0
   283
		transfer::transfer();
indvdum@0
   284
indvdum@0
   285
		return;
indvdum@0
   286
	}
indvdum@0
   287
indvdum@0
   288
	/**
indvdum@0
   289
	* Requests data
indvdum@0
   290
	*/
indvdum@0
   291
	function data()
indvdum@0
   292
	{
indvdum@0
   293
		global $user;
indvdum@0
   294
indvdum@0
   295
		return array(
indvdum@0
   296
			'host'		=> 'localhost',
indvdum@0
   297
			'username'	=> 'anonymous',
indvdum@0
   298
			'password'	=> '',
indvdum@0
   299
			'root_path'	=> $user->page['root_script_path'],
indvdum@0
   300
			'port'		=> 21,
indvdum@0
   301
			'timeout'	=> 10
indvdum@0
   302
		);
indvdum@0
   303
	}
indvdum@0
   304
indvdum@0
   305
	/**
indvdum@0
   306
	* Init FTP Session
indvdum@0
   307
	* @access private
indvdum@0
   308
	*/
indvdum@0
   309
	function _init()
indvdum@0
   310
	{
indvdum@0
   311
		// connect to the server
indvdum@0
   312
		$this->connection = @ftp_connect($this->host, $this->port, $this->timeout);
indvdum@0
   313
indvdum@0
   314
		if (!$this->connection)
indvdum@0
   315
		{
indvdum@0
   316
			return 'ERR_CONNECTING_SERVER';
indvdum@0
   317
		}
indvdum@0
   318
indvdum@0
   319
		// login to the server
indvdum@0
   320
		if (!@ftp_login($this->connection, $this->username, $this->password))
indvdum@0
   321
		{
indvdum@0
   322
			return 'ERR_UNABLE_TO_LOGIN';
indvdum@0
   323
		}
indvdum@0
   324
indvdum@0
   325
		// attempt to turn pasv mode on
indvdum@0
   326
		@ftp_pasv($this->connection, true);
indvdum@0
   327
indvdum@0
   328
		// change to the root directory
indvdum@0
   329
		if (!$this->_chdir($this->root_path))
indvdum@0
   330
		{
indvdum@0
   331
			return 'ERR_CHANGING_DIRECTORY';
indvdum@0
   332
		}
indvdum@0
   333
indvdum@0
   334
		return true;
indvdum@0
   335
	}
indvdum@0
   336
indvdum@0
   337
	/**
indvdum@0
   338
	* Create Directory (MKDIR)
indvdum@0
   339
	* @access private
indvdum@0
   340
	*/
indvdum@0
   341
	function _mkdir($dir)
indvdum@0
   342
	{
indvdum@0
   343
		return @ftp_mkdir($this->connection, $dir);
indvdum@0
   344
	}
indvdum@0
   345
indvdum@0
   346
	/**
indvdum@0
   347
	* Remove directory (RMDIR)
indvdum@0
   348
	* @access private
indvdum@0
   349
	*/
indvdum@0
   350
	function _rmdir($dir)
indvdum@0
   351
	{
indvdum@0
   352
		return @ftp_rmdir($this->connection, $dir);
indvdum@0
   353
	}
indvdum@0
   354
indvdum@0
   355
	/**
indvdum@0
   356
	* Rename file
indvdum@0
   357
	* @access private
indvdum@0
   358
	*/
indvdum@0
   359
	function _rename($old_handle, $new_handle)
indvdum@0
   360
	{
indvdum@0
   361
		return @ftp_rename($this->connection, $old_handle, $new_handle);
indvdum@0
   362
	}
indvdum@0
   363
indvdum@0
   364
	/**
indvdum@0
   365
	* Change current working directory (CHDIR)
indvdum@0
   366
	* @access private
indvdum@0
   367
	*/
indvdum@0
   368
	function _chdir($dir = '')
indvdum@0
   369
	{
indvdum@0
   370
		if ($dir && $dir !== '/')
indvdum@0
   371
		{
indvdum@0
   372
			if (substr($dir, -1, 1) == '/')
indvdum@0
   373
			{
indvdum@0
   374
				$dir = substr($dir, 0, -1);
indvdum@0
   375
			}
indvdum@0
   376
		}
indvdum@0
   377
indvdum@0
   378
		return @ftp_chdir($this->connection, $dir);
indvdum@0
   379
	}
indvdum@0
   380
indvdum@0
   381
	/**
indvdum@0
   382
	* change file permissions (CHMOD)
indvdum@0
   383
	* @access private
indvdum@0
   384
	*/
indvdum@0
   385
	function _chmod($file, $perms)
indvdum@0
   386
	{
indvdum@0
   387
		if (function_exists('ftp_chmod'))
indvdum@0
   388
		{
indvdum@0
   389
			$err = @ftp_chmod($this->connection, $perms, $file);
indvdum@0
   390
		}
indvdum@0
   391
		else
indvdum@0
   392
		{
indvdum@0
   393
			// Unfortunatly CHMOD is not expecting an octal value...
indvdum@0
   394
			// We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
indvdum@0
   395
			$chmod_cmd = 'CHMOD ' . base_convert($perms, 10, 8) . ' ' . $file;
indvdum@0
   396
			$err = $this->_site($chmod_cmd);
indvdum@0
   397
		}
indvdum@0
   398
indvdum@0
   399
		return $err;
indvdum@0
   400
	}
indvdum@0
   401
indvdum@0
   402
	/**
indvdum@0
   403
	* Upload file to location (PUT)
indvdum@0
   404
	* @access private
indvdum@0
   405
	*/
indvdum@0
   406
	function _put($from_file, $to_file)
indvdum@0
   407
	{
indvdum@0
   408
		// get the file extension
indvdum@0
   409
		$file_extension = strtolower(substr(strrchr($to_file, '.'), 1));
indvdum@0
   410
indvdum@0
   411
		// We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
indvdum@0
   412
		$mode = FTP_BINARY;
indvdum@0
   413
indvdum@0
   414
		$to_dir = dirname($to_file);
indvdum@0
   415
		$to_file = basename($to_file);
indvdum@0
   416
		$this->_chdir($to_dir);
indvdum@0
   417
indvdum@0
   418
		$result = @ftp_put($this->connection, $to_file, $from_file, $mode);
indvdum@0
   419
		$this->_chdir($this->root_path);
indvdum@0
   420
indvdum@0
   421
		return $result;
indvdum@0
   422
	}
indvdum@0
   423
indvdum@0
   424
	/**
indvdum@0
   425
	* Delete file (DELETE)
indvdum@0
   426
	* @access private
indvdum@0
   427
	*/
indvdum@0
   428
	function _delete($file)
indvdum@0
   429
	{
indvdum@0
   430
		return @ftp_delete($this->connection, $file);
indvdum@0
   431
	}
indvdum@0
   432
indvdum@0
   433
	/**
indvdum@0
   434
	* Close ftp session (CLOSE)
indvdum@0
   435
	* @access private
indvdum@0
   436
	*/
indvdum@0
   437
	function _close()
indvdum@0
   438
	{
indvdum@0
   439
		if (!$this->connection)
indvdum@0
   440
		{
indvdum@0
   441
			return false;
indvdum@0
   442
		}
indvdum@0
   443
indvdum@0
   444
		return @ftp_quit($this->connection);
indvdum@0
   445
	}
indvdum@0
   446
indvdum@0
   447
	/**
indvdum@0
   448
	* Return current working directory (CWD)
indvdum@0
   449
	* At the moment not used by parent class
indvdum@0
   450
	* @access private
indvdum@0
   451
	*/
indvdum@0
   452
	function _cwd()
indvdum@0
   453
	{
indvdum@0
   454
		return @ftp_pwd($this->connection);
indvdum@0
   455
	}
indvdum@0
   456
indvdum@0
   457
	/**
indvdum@0
   458
	* Return list of files in a given directory (LS)
indvdum@0
   459
	* @access private
indvdum@0
   460
	*/
indvdum@0
   461
	function _ls($dir = './')
indvdum@0
   462
	{
indvdum@0
   463
		$list = @ftp_nlist($this->connection, $dir);
indvdum@0
   464
indvdum@0
   465
		// See bug #46295 - Some FTP daemons don't like './'
indvdum@0
   466
		if ($dir === './')
indvdum@0
   467
		{
indvdum@0
   468
			// Let's try some alternatives
indvdum@0
   469
			$list = (empty($list)) ? @ftp_nlist($this->connection, '.') : $list;
indvdum@0
   470
			$list = (empty($list)) ? @ftp_nlist($this->connection, '') : $list;
indvdum@0
   471
		}
indvdum@0
   472
indvdum@0
   473
		// Return on error
indvdum@0
   474
		if ($list === false)
indvdum@0
   475
		{
indvdum@0
   476
			return false;
indvdum@0
   477
		}
indvdum@0
   478
indvdum@0
   479
		// Remove path if prepended
indvdum@0
   480
		foreach ($list as $key => $item)
indvdum@0
   481
		{
indvdum@0
   482
			// Use same separator for item and dir
indvdum@0
   483
			$item = str_replace('\\', '/', $item);
indvdum@0
   484
			$dir = str_replace('\\', '/', $dir);
indvdum@0
   485
indvdum@0
   486
			if (!empty($dir) && strpos($item, $dir) === 0)
indvdum@0
   487
			{
indvdum@0
   488
				$item = substr($item, strlen($dir));
indvdum@0
   489
			}
indvdum@0
   490
indvdum@0
   491
			$list[$key] = $item;
indvdum@0
   492
		}
indvdum@0
   493
indvdum@0
   494
		return $list;
indvdum@0
   495
	}
indvdum@0
   496
indvdum@0
   497
	/**
indvdum@0
   498
	* FTP SITE command (ftp-only function)
indvdum@0
   499
	* @access private
indvdum@0
   500
	*/
indvdum@0
   501
	function _site($command)
indvdum@0
   502
	{
indvdum@0
   503
		return @ftp_site($this->connection, $command);
indvdum@0
   504
	}
indvdum@0
   505
}
indvdum@0
   506
indvdum@0
   507
/**
indvdum@0
   508
* FTP fsock transfer class
indvdum@0
   509
*
indvdum@0
   510
* @author wGEric
indvdum@0
   511
* @package phpBB3
indvdum@0
   512
*/
indvdum@0
   513
class ftp_fsock extends transfer
indvdum@0
   514
{
indvdum@0
   515
	var $data_connection;
indvdum@0
   516
indvdum@0
   517
	/**
indvdum@0
   518
	* Standard parameters for FTP session
indvdum@0
   519
	*/
indvdum@0
   520
	function ftp_fsock($host, $username, $password, $root_path, $port = 21, $timeout = 10)
indvdum@0
   521
	{
indvdum@0
   522
		$this->host			= $host;
indvdum@0
   523
		$this->port			= $port;
indvdum@0
   524
		$this->username		= $username;
indvdum@0
   525
		$this->password		= $password;
indvdum@0
   526
		$this->timeout		= $timeout;
indvdum@0
   527
indvdum@0
   528
		// Make sure $this->root_path is layed out the same way as the $user->page['root_script_path'] value (/ at the end)
indvdum@0
   529
		$this->root_path	= str_replace('\\', '/', $this->root_path);
indvdum@0
   530
indvdum@0
   531
		if (!empty($root_path))
indvdum@0
   532
		{
indvdum@0
   533
			$this->root_path = (($root_path[0] != '/' ) ? '/' : '') . $root_path . ((substr($root_path, -1, 1) == '/') ? '' : '/');
indvdum@0
   534
		}
indvdum@0
   535
indvdum@0
   536
		// Init some needed values
indvdum@0
   537
		transfer::transfer();
indvdum@0
   538
indvdum@0
   539
		return;
indvdum@0
   540
	}
indvdum@0
   541
indvdum@0
   542
	/**
indvdum@0
   543
	* Requests data
indvdum@0
   544
	*/
indvdum@0
   545
	function data()
indvdum@0
   546
	{
indvdum@0
   547
		global $user;
indvdum@0
   548
indvdum@0
   549
		return array(
indvdum@0
   550
			'host'		=> 'localhost',
indvdum@0
   551
			'username'	=> 'anonymous',
indvdum@0
   552
			'password'	=> '',
indvdum@0
   553
			'root_path'	=> $user->page['root_script_path'],
indvdum@0
   554
			'port'		=> 21,
indvdum@0
   555
			'timeout'	=> 10
indvdum@0
   556
		);
indvdum@0
   557
	}
indvdum@0
   558
indvdum@0
   559
	/**
indvdum@0
   560
	* Init FTP Session
indvdum@0
   561
	* @access private
indvdum@0
   562
	*/
indvdum@0
   563
	function _init()
indvdum@0
   564
	{
indvdum@0
   565
		$errno = 0;
indvdum@0
   566
		$errstr = '';
indvdum@0
   567
indvdum@0
   568
		// connect to the server
indvdum@0
   569
		$this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
indvdum@0
   570
indvdum@0
   571
		if (!$this->connection || !$this->_check_command())
indvdum@0
   572
		{
indvdum@0
   573
			return 'ERR_CONNECTING_SERVER';
indvdum@0
   574
		}
indvdum@0
   575
indvdum@0
   576
		@stream_set_timeout($this->connection, $this->timeout);
indvdum@0
   577
indvdum@0
   578
		// login
indvdum@0
   579
		if (!$this->_send_command('USER', $this->username))
indvdum@0
   580
		{
indvdum@0
   581
			return 'ERR_UNABLE_TO_LOGIN';
indvdum@0
   582
		}
indvdum@0
   583
indvdum@0
   584
		if (!$this->_send_command('PASS', $this->password))
indvdum@0
   585
		{
indvdum@0
   586
			return 'ERR_UNABLE_TO_LOGIN';
indvdum@0
   587
		}
indvdum@0
   588
indvdum@0
   589
		// change to the root directory
indvdum@0
   590
		if (!$this->_chdir($this->root_path))
indvdum@0
   591
		{
indvdum@0
   592
			return 'ERR_CHANGING_DIRECTORY';
indvdum@0
   593
		}
indvdum@0
   594
indvdum@0
   595
		return true;
indvdum@0
   596
	}
indvdum@0
   597
indvdum@0
   598
	/**
indvdum@0
   599
	* Create Directory (MKDIR)
indvdum@0
   600
	* @access private
indvdum@0
   601
	*/
indvdum@0
   602
	function _mkdir($dir)
indvdum@0
   603
	{
indvdum@0
   604
		return $this->_send_command('MKD', $dir);
indvdum@0
   605
	}
indvdum@0
   606
indvdum@0
   607
	/**
indvdum@0
   608
	* Remove directory (RMDIR)
indvdum@0
   609
	* @access private
indvdum@0
   610
	*/
indvdum@0
   611
	function _rmdir($dir)
indvdum@0
   612
	{
indvdum@0
   613
		return $this->_send_command('RMD', $dir);
indvdum@0
   614
	}
indvdum@0
   615
indvdum@0
   616
	/**
indvdum@0
   617
	* Rename File
indvdum@0
   618
	* @access private
indvdum@0
   619
	*/
indvdum@0
   620
	function _rename($old_handle, $new_handle)
indvdum@0
   621
	{
indvdum@0
   622
		$this->_send_command('RNFR', $old_handle);
indvdum@0
   623
		return $this->_send_command('RNTO', $new_handle);
indvdum@0
   624
	}
indvdum@0
   625
indvdum@0
   626
	/**
indvdum@0
   627
	* Change current working directory (CHDIR)
indvdum@0
   628
	* @access private
indvdum@0
   629
	*/
indvdum@0
   630
	function _chdir($dir = '')
indvdum@0
   631
	{
indvdum@0
   632
		if ($dir && $dir !== '/')
indvdum@0
   633
		{
indvdum@0
   634
			if (substr($dir, -1, 1) == '/')
indvdum@0
   635
			{
indvdum@0
   636
				$dir = substr($dir, 0, -1);
indvdum@0
   637
			}
indvdum@0
   638
		}
indvdum@0
   639
indvdum@0
   640
		return $this->_send_command('CWD', $dir);
indvdum@0
   641
	}
indvdum@0
   642
indvdum@0
   643
	/**
indvdum@0
   644
	* change file permissions (CHMOD)
indvdum@0
   645
	* @access private
indvdum@0
   646
	*/
indvdum@0
   647
	function _chmod($file, $perms)
indvdum@0
   648
	{
indvdum@0
   649
		// Unfortunatly CHMOD is not expecting an octal value...
indvdum@0
   650
		// We need to transform the integer (which was an octal) to an octal representation (to get the int) and then pass as is. ;)
indvdum@0
   651
		return $this->_send_command('SITE CHMOD', base_convert($perms, 10, 8) . ' ' . $file);
indvdum@0
   652
	}
indvdum@0
   653
indvdum@0
   654
	/**
indvdum@0
   655
	* Upload file to location (PUT)
indvdum@0
   656
	* @access private
indvdum@0
   657
	*/
indvdum@0
   658
	function _put($from_file, $to_file)
indvdum@0
   659
	{
indvdum@0
   660
		// We only use the BINARY file mode to cicumvent rewrite actions from ftp server (mostly linefeeds being replaced)
indvdum@0
   661
		// 'I' == BINARY
indvdum@0
   662
		// 'A' == ASCII
indvdum@0
   663
		if (!$this->_send_command('TYPE', 'I'))
indvdum@0
   664
		{
indvdum@0
   665
			return false;
indvdum@0
   666
		}
indvdum@0
   667
indvdum@0
   668
		// open the connection to send file over
indvdum@0
   669
		if (!$this->_open_data_connection())
indvdum@0
   670
		{
indvdum@0
   671
			return false;
indvdum@0
   672
		}
indvdum@0
   673
indvdum@0
   674
		$this->_send_command('STOR', $to_file, false);
indvdum@0
   675
indvdum@0
   676
		// send the file
indvdum@0
   677
		$fp = @fopen($from_file, 'rb');
indvdum@0
   678
		while (!@feof($fp))
indvdum@0
   679
		{
indvdum@0
   680
			@fwrite($this->data_connection, @fread($fp, 4096));
indvdum@0
   681
		}
indvdum@0
   682
		@fclose($fp);
indvdum@0
   683
indvdum@0
   684
		// close connection
indvdum@0
   685
		$this->_close_data_connection();
indvdum@0
   686
indvdum@0
   687
		return $this->_check_command();
indvdum@0
   688
	}
indvdum@0
   689
indvdum@0
   690
	/**
indvdum@0
   691
	* Delete file (DELETE)
indvdum@0
   692
	* @access private
indvdum@0
   693
	*/
indvdum@0
   694
	function _delete($file)
indvdum@0
   695
	{
indvdum@0
   696
		return $this->_send_command('DELE', $file);
indvdum@0
   697
	}
indvdum@0
   698
indvdum@0
   699
	/**
indvdum@0
   700
	* Close ftp session (CLOSE)
indvdum@0
   701
	* @access private
indvdum@0
   702
	*/
indvdum@0
   703
	function _close()
indvdum@0
   704
	{
indvdum@0
   705
		if (!$this->connection)
indvdum@0
   706
		{
indvdum@0
   707
			return false;
indvdum@0
   708
		}
indvdum@0
   709
indvdum@0
   710
		return $this->_send_command('QUIT');
indvdum@0
   711
	}
indvdum@0
   712
indvdum@0
   713
	/**
indvdum@0
   714
	* Return current working directory (CWD)
indvdum@0
   715
	* At the moment not used by parent class
indvdum@0
   716
	* @access private
indvdum@0
   717
	*/
indvdum@0
   718
	function _cwd()
indvdum@0
   719
	{
indvdum@0
   720
		$this->_send_command('PWD', '', false);
indvdum@0
   721
		return preg_replace('#^[0-9]{3} "(.+)" .+\r\n#', '\\1', $this->_check_command(true));
indvdum@0
   722
	}
indvdum@0
   723
indvdum@0
   724
	/**
indvdum@0
   725
	* Return list of files in a given directory (LS)
indvdum@0
   726
	* @access private
indvdum@0
   727
	*/
indvdum@0
   728
	function _ls($dir = './')
indvdum@0
   729
	{
indvdum@0
   730
		if (!$this->_open_data_connection())
indvdum@0
   731
		{
indvdum@0
   732
			return false;
indvdum@0
   733
		}
indvdum@0
   734
indvdum@0
   735
		$this->_send_command('NLST', $dir);
indvdum@0
   736
indvdum@0
   737
		$list = array();
indvdum@0
   738
		while (!@feof($this->data_connection))
indvdum@0
   739
		{
indvdum@0
   740
			$filename = preg_replace('#[\r\n]#', '', @fgets($this->data_connection, 512));
indvdum@0
   741
indvdum@0
   742
			if ($filename !== '')
indvdum@0
   743
			{
indvdum@0
   744
				$list[] = $filename;
indvdum@0
   745
			}
indvdum@0
   746
		}
indvdum@0
   747
		$this->_close_data_connection();
indvdum@0
   748
indvdum@0
   749
		// Clear buffer
indvdum@0
   750
		$this->_check_command();
indvdum@0
   751
indvdum@0
   752
		// See bug #46295 - Some FTP daemons don't like './'
indvdum@0
   753
		if ($dir === './' && empty($list))
indvdum@0
   754
		{
indvdum@0
   755
			// Let's try some alternatives
indvdum@0
   756
			$list = $this->_ls('.');
indvdum@0
   757
indvdum@0
   758
			if (empty($list))
indvdum@0
   759
			{
indvdum@0
   760
				$list = $this->_ls('');
indvdum@0
   761
			}
indvdum@0
   762
indvdum@0
   763
			return $list;
indvdum@0
   764
		}
indvdum@0
   765
indvdum@0
   766
		// Remove path if prepended
indvdum@0
   767
		foreach ($list as $key => $item)
indvdum@0
   768
		{
indvdum@0
   769
			// Use same separator for item and dir
indvdum@0
   770
			$item = str_replace('\\', '/', $item);
indvdum@0
   771
			$dir = str_replace('\\', '/', $dir);
indvdum@0
   772
indvdum@0
   773
			if (!empty($dir) && strpos($item, $dir) === 0)
indvdum@0
   774
			{
indvdum@0
   775
				$item = substr($item, strlen($dir));
indvdum@0
   776
			}
indvdum@0
   777
indvdum@0
   778
			$list[$key] = $item;
indvdum@0
   779
		}
indvdum@0
   780
indvdum@0
   781
		return $list;
indvdum@0
   782
	}
indvdum@0
   783
indvdum@0
   784
	/**
indvdum@0
   785
	* Send a command to server (FTP fsock only function)
indvdum@0
   786
	* @access private
indvdum@0
   787
	*/
indvdum@0
   788
	function _send_command($command, $args = '', $check = true)
indvdum@0
   789
	{
indvdum@0
   790
		if (!empty($args))
indvdum@0
   791
		{
indvdum@0
   792
			$command = "$command $args";
indvdum@0
   793
		}
indvdum@0
   794
indvdum@0
   795
		fwrite($this->connection, $command . "\r\n");
indvdum@0
   796
indvdum@0
   797
		if ($check === true && !$this->_check_command())
indvdum@0
   798
		{
indvdum@0
   799
			return false;
indvdum@0
   800
		}
indvdum@0
   801
indvdum@0
   802
		return true;
indvdum@0
   803
	}
indvdum@0
   804
indvdum@0
   805
	/**
indvdum@0
   806
	* Opens a connection to send data (FTP fosck only function)
indvdum@0
   807
	* @access private
indvdum@0
   808
	*/
indvdum@0
   809
	function _open_data_connection()
indvdum@0
   810
	{
indvdum@0
   811
		// Try to find out whether we have a IPv4 or IPv6 (control) connection
indvdum@0
   812
		if (function_exists('stream_socket_get_name'))
indvdum@0
   813
		{
indvdum@0
   814
			$socket_name = stream_socket_get_name($this->connection, true);
indvdum@0
   815
			$server_ip = substr($socket_name, 0, strrpos($socket_name, ':'));
indvdum@0
   816
		}
indvdum@0
   817
indvdum@0
   818
		if (!isset($server_ip) || preg_match(get_preg_expression('ipv4'), $server_ip))
indvdum@0
   819
		{
indvdum@0
   820
			// Passive mode
indvdum@0
   821
			$this->_send_command('PASV', '', false);
indvdum@0
   822
indvdum@0
   823
			if (!$ip_port = $this->_check_command(true))
indvdum@0
   824
			{
indvdum@0
   825
				return false;
indvdum@0
   826
			}
indvdum@0
   827
indvdum@0
   828
			// open the connection to start sending the file
indvdum@0
   829
			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))
indvdum@0
   830
			{
indvdum@0
   831
				// bad ip and port
indvdum@0
   832
				return false;
indvdum@0
   833
			}
indvdum@0
   834
indvdum@0
   835
			$temp = explode(',', $temp[0]);
indvdum@0
   836
			$server_ip = $temp[0] . '.' . $temp[1] . '.' . $temp[2] . '.' . $temp[3];
indvdum@0
   837
			$server_port = $temp[4] * 256 + $temp[5];
indvdum@0
   838
		}
indvdum@0
   839
		else
indvdum@0
   840
		{
indvdum@0
   841
			// Extended Passive Mode - RFC2428
indvdum@0
   842
			$this->_send_command('EPSV', '', false);
indvdum@0
   843
indvdum@0
   844
			if (!$epsv_response = $this->_check_command(true))
indvdum@0
   845
			{
indvdum@0
   846
				return false;
indvdum@0
   847
			}
indvdum@0
   848
indvdum@0
   849
			// Response looks like "229 Entering Extended Passive Mode (|||12345|)"
indvdum@0
   850
			// where 12345 is the tcp port for the data connection
indvdum@0
   851
			if (!preg_match('#\(\|\|\|([0-9]+)\|\)#', $epsv_response, $match))
indvdum@0
   852
			{
indvdum@0
   853
				return false;
indvdum@0
   854
			}
indvdum@0
   855
			$server_port = (int) $match[1];
indvdum@0
   856
indvdum@0
   857
			// fsockopen expects IPv6 address in square brackets
indvdum@0
   858
			$server_ip = "[$server_ip]";
indvdum@0
   859
		}
indvdum@0
   860
indvdum@0
   861
		$errno = 0;
indvdum@0
   862
		$errstr = '';
indvdum@0
   863
indvdum@0
   864
		if (!$this->data_connection = @fsockopen($server_ip, $server_port, $errno, $errstr, $this->timeout))
indvdum@0
   865
		{
indvdum@0
   866
			return false;
indvdum@0
   867
		}
indvdum@0
   868
		@stream_set_timeout($this->data_connection, $this->timeout);
indvdum@0
   869
indvdum@0
   870
		return true;
indvdum@0
   871
	}
indvdum@0
   872
indvdum@0
   873
	/**
indvdum@0
   874
	* Closes a connection used to send data
indvdum@0
   875
	* @access private
indvdum@0
   876
	*/
indvdum@0
   877
	function _close_data_connection()
indvdum@0
   878
	{
indvdum@0
   879
		return @fclose($this->data_connection);
indvdum@0
   880
	}
indvdum@0
   881
indvdum@0
   882
	/**
indvdum@0
   883
	* Check to make sure command was successful (FTP fsock only function)
indvdum@0
   884
	* @access private
indvdum@0
   885
	*/
indvdum@0
   886
	function _check_command($return = false)
indvdum@0
   887
	{
indvdum@0
   888
		$response = '';
indvdum@0
   889
indvdum@0
   890
		do
indvdum@0
   891
		{
indvdum@0
   892
			$result = @fgets($this->connection, 512);
indvdum@0
   893
			$response .= $result;
indvdum@0
   894
		}
indvdum@0
   895
		while (substr($result, 3, 1) !== ' ');
indvdum@0
   896
indvdum@0
   897
		if (!preg_match('#^[123]#', $response))
indvdum@0
   898
		{
indvdum@0
   899
			return false;
indvdum@0
   900
		}
indvdum@0
   901
indvdum@0
   902
		return ($return) ? $response : true;
indvdum@0
   903
	}
indvdum@0
   904
}
indvdum@0
   905
indvdum@0
   906
?>