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

