| Conditions | 210 |
| Paths | > 20000 |
| Total Lines | 1390 |
| Code Lines | 798 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 307 | function updateSettingsFile($config_vars, $keep_quotes = null, $rebuild = false) |
||
| 308 | { |
||
| 309 | // In this function we intentionally don't declare any global variables. |
||
| 310 | // This allows us to work with everything cleanly. |
||
| 311 | |||
| 312 | static $mtime; |
||
| 313 | |||
| 314 | // Should we try to unescape the strings? |
||
| 315 | if (empty($keep_quotes)) |
||
| 316 | { |
||
| 317 | foreach ($config_vars as $var => $val) |
||
| 318 | { |
||
| 319 | if (is_string($val) && ($keep_quotes === false || strpos($val, '\'') === 0 && strrpos($val, '\'') === strlen($val) - 1)) |
||
| 320 | $config_vars[$var] = trim(stripcslashes($val), '\''); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | // Updating the db_last_error, then don't mess around with Settings.php |
||
| 325 | if (isset($config_vars['db_last_error'])) |
||
| 326 | { |
||
| 327 | updateDbLastError($config_vars['db_last_error']); |
||
| 328 | |||
| 329 | if (count($config_vars) === 1 && empty($rebuild)) |
||
| 330 | return true; |
||
| 331 | |||
| 332 | // Make sure we delete this from Settings.php, if present. |
||
| 333 | $config_vars['db_last_error'] = 0; |
||
| 334 | } |
||
| 335 | |||
| 336 | // Rebuilding should not be undertaken lightly, so we're picky about the parameter. |
||
| 337 | if (!is_bool($rebuild)) |
||
| 338 | $rebuild = false; |
||
| 339 | |||
| 340 | $mtime = isset($mtime) ? (int) $mtime : (defined('TIME_START') ? TIME_START : $_SERVER['REQUEST_TIME']); |
||
| 341 | |||
| 342 | /***************** |
||
| 343 | * PART 1: Setup * |
||
| 344 | *****************/ |
||
| 345 | |||
| 346 | // Typically Settings.php is in $boarddir, but maybe this is a custom setup... |
||
| 347 | foreach (get_included_files() as $settingsFile) |
||
| 348 | if (basename($settingsFile) === 'Settings.php') |
||
| 349 | break; |
||
| 350 | |||
| 351 | // Fallback in case Settings.php isn't loaded (e.g. while installing) |
||
| 352 | if (basename($settingsFile) !== 'Settings.php') |
||
| 353 | $settingsFile = (!empty($GLOBALS['boarddir']) && @realpath($GLOBALS['boarddir']) ? $GLOBALS['boarddir'] : (!empty($_SERVER['SCRIPT_FILENAME']) ? dirname($_SERVER['SCRIPT_FILENAME']) : dirname(__DIR__))) . '/Settings.php'; |
||
| 354 | |||
| 355 | // File not found? Attempt an emergency on-the-fly fix! |
||
| 356 | if (!file_exists($settingsFile)) |
||
| 357 | @touch($settingsFile); |
||
| 358 | |||
| 359 | // When was Settings.php last changed? |
||
| 360 | $last_settings_change = filemtime($settingsFile); |
||
| 361 | |||
| 362 | // Get the current values of everything in Settings.php. |
||
| 363 | $settings_vars = get_current_settings($mtime, $settingsFile); |
||
| 364 | |||
| 365 | // If Settings.php is empty for some reason, see if we can use the backup. |
||
| 366 | if (empty($settings_vars) && file_exists(dirname($settingsFile) . '/Settings_bak.php')) |
||
| 367 | $settings_vars = get_current_settings($mtime, dirname($settingsFile) . '/Settings_bak.php'); |
||
| 368 | |||
| 369 | // False means there was a problem with the file and we can't safely continue. |
||
| 370 | if ($settings_vars === false) |
||
| 371 | return false; |
||
| 372 | |||
| 373 | // It works best to set everything afresh. |
||
| 374 | $new_settings_vars = array_merge($settings_vars, $config_vars); |
||
| 375 | |||
| 376 | // Are we using UTF-8? |
||
| 377 | $utf8 = isset($GLOBALS['context']['utf8']) ? $GLOBALS['context']['utf8'] : (isset($GLOBALS['utf8']) ? $GLOBALS['utf8'] : (isset($settings_vars['db_character_set']) ? $settings_vars['db_character_set'] === 'utf8' : false)); |
||
| 378 | |||
| 379 | /* |
||
| 380 | * A big, fat array to define properties of all the Settings.php variables. |
||
| 381 | * |
||
| 382 | * - String keys are used to identify actual variables. |
||
| 383 | * |
||
| 384 | * - Integer keys are used for content not connected to any particular |
||
| 385 | * variable, such as code blocks or the license block. |
||
| 386 | * |
||
| 387 | * - The content of the 'text' element is simply printed out, if it is used |
||
| 388 | * at all. Use it for comments or to insert code blocks, etc. |
||
| 389 | * |
||
| 390 | * - The 'default' element, not surprisingly, gives a default value for |
||
| 391 | * the variable. |
||
| 392 | * |
||
| 393 | * - The 'type' element defines the expected variable type or types. If |
||
| 394 | * more than one type is allowed, this should be an array listing them. |
||
| 395 | * Types should match the possible types returned by gettype(). |
||
| 396 | * |
||
| 397 | * - If 'raw_default' is true, the default should be printed directly, |
||
| 398 | * rather than being handled as a string. Use it if the default contains |
||
| 399 | * code, e.g. 'dirname(__FILE__)' |
||
| 400 | * |
||
| 401 | * - If 'required' is true and a value for the variable is undefined, |
||
| 402 | * the update will be aborted. (The only exception is during the SMF |
||
| 403 | * installation process.) |
||
| 404 | * |
||
| 405 | * - If 'auto_delete' is 1 or true and the variable is empty, the variable |
||
| 406 | * will be deleted from Settings.php. If 'auto_delete' is 0/false/null, |
||
| 407 | * the variable will never be deleted. If 'auto_delete' is 2, behaviour |
||
| 408 | * depends on $rebuild: if $rebuild is true, 'auto_delete' == 2 behaves |
||
| 409 | * like 'auto_delete' == 1; if $rebuild is false, 'auto_delete' == 2 |
||
| 410 | * behaves like 'auto_delete' == 0. |
||
| 411 | * |
||
| 412 | * - The optional 'search_pattern' element defines a custom regular |
||
| 413 | * expression to search for the existing entry in the file. This is |
||
| 414 | * primarily useful for code blocks rather than variables. |
||
| 415 | * |
||
| 416 | * - The optional 'replace_pattern' element defines a custom regular |
||
| 417 | * expression to decide where the replacement entry should be inserted. |
||
| 418 | * Note: 'replace_pattern' should be avoided unless ABSOLUTELY necessary. |
||
| 419 | */ |
||
| 420 | $settings_defs = array( |
||
| 421 | array( |
||
| 422 | 'text' => implode("\n", array( |
||
| 423 | '', |
||
| 424 | '/**', |
||
| 425 | ' * The settings file contains all of the basic settings that need to be present when a database/cache is not available.', |
||
| 426 | ' *', |
||
| 427 | ' * Simple Machines Forum (SMF)', |
||
| 428 | ' *', |
||
| 429 | ' * @package SMF', |
||
| 430 | ' * @author Simple Machines https://www.simplemachines.org', |
||
| 431 | ' * @copyright ' . SMF_SOFTWARE_YEAR . ' Simple Machines and individual contributors', |
||
| 432 | ' * @license https://www.simplemachines.org/about/smf/license.php BSD', |
||
| 433 | ' *', |
||
| 434 | ' * @version ' . SMF_VERSION, |
||
| 435 | ' */', |
||
| 436 | '', |
||
| 437 | )), |
||
| 438 | 'search_pattern' => '~/\*\*.*?@package\h+SMF\b.*?\*/\n{0,2}~s', |
||
| 439 | ), |
||
| 440 | 'maintenance' => array( |
||
| 441 | 'text' => implode("\n", array( |
||
| 442 | '', |
||
| 443 | '########## Maintenance ##########', |
||
| 444 | '/**', |
||
| 445 | ' * The maintenance "mode"', |
||
| 446 | ' * Set to 1 to enable Maintenance Mode, 2 to make the forum untouchable. (you\'ll have to make it 0 again manually!)', |
||
| 447 | ' * 0 is default and disables maintenance mode.', |
||
| 448 | ' *', |
||
| 449 | ' * @var int 0, 1, 2', |
||
| 450 | ' * @global int $maintenance', |
||
| 451 | ' */', |
||
| 452 | )), |
||
| 453 | 'default' => 0, |
||
| 454 | 'type' => 'integer', |
||
| 455 | ), |
||
| 456 | 'mtitle' => array( |
||
| 457 | 'text' => implode("\n", array( |
||
| 458 | '/**', |
||
| 459 | ' * Title for the Maintenance Mode message.', |
||
| 460 | ' *', |
||
| 461 | ' * @var string', |
||
| 462 | ' * @global int $mtitle', |
||
| 463 | ' */', |
||
| 464 | )), |
||
| 465 | 'default' => 'Maintenance Mode', |
||
| 466 | 'type' => 'string', |
||
| 467 | ), |
||
| 468 | 'mmessage' => array( |
||
| 469 | 'text' => implode("\n", array( |
||
| 470 | '/**', |
||
| 471 | ' * Description of why the forum is in maintenance mode.', |
||
| 472 | ' *', |
||
| 473 | ' * @var string', |
||
| 474 | ' * @global string $mmessage', |
||
| 475 | ' */', |
||
| 476 | )), |
||
| 477 | 'default' => 'Okay faithful users...we\'re attempting to restore an older backup of the database...news will be posted once we\'re back!', |
||
| 478 | 'type' => 'string', |
||
| 479 | ), |
||
| 480 | 'mbname' => array( |
||
| 481 | 'text' => implode("\n", array( |
||
| 482 | '', |
||
| 483 | '########## Forum Info ##########', |
||
| 484 | '/**', |
||
| 485 | ' * The name of your forum.', |
||
| 486 | ' *', |
||
| 487 | ' * @var string', |
||
| 488 | ' */', |
||
| 489 | )), |
||
| 490 | 'default' => 'My Community', |
||
| 491 | 'type' => 'string', |
||
| 492 | ), |
||
| 493 | 'language' => array( |
||
| 494 | 'text' => implode("\n", array( |
||
| 495 | '/**', |
||
| 496 | ' * The default language file set for the forum.', |
||
| 497 | ' *', |
||
| 498 | ' * @var string', |
||
| 499 | ' */', |
||
| 500 | )), |
||
| 501 | 'default' => 'english', |
||
| 502 | 'type' => 'string', |
||
| 503 | ), |
||
| 504 | 'boardurl' => array( |
||
| 505 | 'text' => implode("\n", array( |
||
| 506 | '/**', |
||
| 507 | ' * URL to your forum\'s folder. (without the trailing /!)', |
||
| 508 | ' *', |
||
| 509 | ' * @var string', |
||
| 510 | ' */', |
||
| 511 | )), |
||
| 512 | 'default' => 'http://127.0.0.1/smf', |
||
| 513 | 'type' => 'string', |
||
| 514 | ), |
||
| 515 | 'webmaster_email' => array( |
||
| 516 | 'text' => implode("\n", array( |
||
| 517 | '/**', |
||
| 518 | ' * Email address to send emails from. (like [email protected].)', |
||
| 519 | ' *', |
||
| 520 | ' * @var string', |
||
| 521 | ' */', |
||
| 522 | )), |
||
| 523 | 'default' => '[email protected]', |
||
| 524 | 'type' => 'string', |
||
| 525 | ), |
||
| 526 | 'cookiename' => array( |
||
| 527 | 'text' => implode("\n", array( |
||
| 528 | '/**', |
||
| 529 | ' * Name of the cookie to set for authentication.', |
||
| 530 | ' *', |
||
| 531 | ' * @var string', |
||
| 532 | ' */', |
||
| 533 | )), |
||
| 534 | 'default' => 'SMFCookie11', |
||
| 535 | 'type' => 'string', |
||
| 536 | ), |
||
| 537 | 'auth_secret' => array( |
||
| 538 | 'text' => implode("\n", array( |
||
| 539 | '/**', |
||
| 540 | ' * Secret key used to create and verify cookies, tokens, etc.', |
||
| 541 | ' * Do not change this unless absolutely necessary, and NEVER share it.', |
||
| 542 | ' *', |
||
| 543 | ' * Note: Changing this will immediately log out all members of your forum', |
||
| 544 | ' * and break the token-based links in all previous email notifications,', |
||
| 545 | ' * among other possible effects.', |
||
| 546 | ' *', |
||
| 547 | ' * @var string', |
||
| 548 | ' */', |
||
| 549 | )), |
||
| 550 | 'default' => null, |
||
| 551 | 'auto_delete' => 1, |
||
| 552 | 'type' => 'string', |
||
| 553 | ), |
||
| 554 | 'db_type' => array( |
||
| 555 | 'text' => implode("\n", array( |
||
| 556 | '', |
||
| 557 | '########## Database Info ##########', |
||
| 558 | '/**', |
||
| 559 | ' * The database type', |
||
| 560 | ' * Default options: mysql, postgresql', |
||
| 561 | ' *', |
||
| 562 | ' * @var string', |
||
| 563 | ' */', |
||
| 564 | )), |
||
| 565 | 'default' => 'mysql', |
||
| 566 | 'type' => 'string', |
||
| 567 | ), |
||
| 568 | 'db_port' => array( |
||
| 569 | 'text' => implode("\n", array( |
||
| 570 | '/**', |
||
| 571 | ' * The database port', |
||
| 572 | ' * 0 to use default port for the database type', |
||
| 573 | ' *', |
||
| 574 | ' * @var int', |
||
| 575 | ' */', |
||
| 576 | )), |
||
| 577 | 'default' => 0, |
||
| 578 | 'type' => 'integer', |
||
| 579 | ), |
||
| 580 | 'db_server' => array( |
||
| 581 | 'text' => implode("\n", array( |
||
| 582 | '/**', |
||
| 583 | ' * The server to connect to (or a Unix socket)', |
||
| 584 | ' *', |
||
| 585 | ' * @var string', |
||
| 586 | ' */', |
||
| 587 | )), |
||
| 588 | 'default' => 'localhost', |
||
| 589 | 'required' => true, |
||
| 590 | 'type' => 'string', |
||
| 591 | ), |
||
| 592 | 'db_name' => array( |
||
| 593 | 'text' => implode("\n", array( |
||
| 594 | '/**', |
||
| 595 | ' * The database name', |
||
| 596 | ' *', |
||
| 597 | ' * @var string', |
||
| 598 | ' */', |
||
| 599 | )), |
||
| 600 | 'default' => 'smf', |
||
| 601 | 'required' => true, |
||
| 602 | 'type' => 'string', |
||
| 603 | ), |
||
| 604 | 'db_user' => array( |
||
| 605 | 'text' => implode("\n", array( |
||
| 606 | '/**', |
||
| 607 | ' * Database username', |
||
| 608 | ' *', |
||
| 609 | ' * @var string', |
||
| 610 | ' */', |
||
| 611 | )), |
||
| 612 | 'default' => 'root', |
||
| 613 | 'required' => true, |
||
| 614 | 'type' => 'string', |
||
| 615 | ), |
||
| 616 | 'db_passwd' => array( |
||
| 617 | 'text' => implode("\n", array( |
||
| 618 | '/**', |
||
| 619 | ' * Database password', |
||
| 620 | ' *', |
||
| 621 | ' * @var string', |
||
| 622 | ' */', |
||
| 623 | )), |
||
| 624 | 'default' => '', |
||
| 625 | 'required' => true, |
||
| 626 | 'type' => 'string', |
||
| 627 | ), |
||
| 628 | 'ssi_db_user' => array( |
||
| 629 | 'text' => implode("\n", array( |
||
| 630 | '/**', |
||
| 631 | ' * Database user for when connecting with SSI', |
||
| 632 | ' *', |
||
| 633 | ' * @var string', |
||
| 634 | ' */', |
||
| 635 | )), |
||
| 636 | 'default' => '', |
||
| 637 | 'type' => 'string', |
||
| 638 | ), |
||
| 639 | 'ssi_db_passwd' => array( |
||
| 640 | 'text' => implode("\n", array( |
||
| 641 | '/**', |
||
| 642 | ' * Database password for when connecting with SSI', |
||
| 643 | ' *', |
||
| 644 | ' * @var string', |
||
| 645 | ' */', |
||
| 646 | )), |
||
| 647 | 'default' => '', |
||
| 648 | 'type' => 'string', |
||
| 649 | ), |
||
| 650 | 'db_prefix' => array( |
||
| 651 | 'text' => implode("\n", array( |
||
| 652 | '/**', |
||
| 653 | ' * A prefix to put in front of your table names.', |
||
| 654 | ' * This helps to prevent conflicts', |
||
| 655 | ' *', |
||
| 656 | ' * @var string', |
||
| 657 | ' */', |
||
| 658 | )), |
||
| 659 | 'default' => 'smf_', |
||
| 660 | 'required' => true, |
||
| 661 | 'type' => 'string', |
||
| 662 | ), |
||
| 663 | 'db_persist' => array( |
||
| 664 | 'text' => implode("\n", array( |
||
| 665 | '/**', |
||
| 666 | ' * Use a persistent database connection', |
||
| 667 | ' *', |
||
| 668 | ' * @var bool', |
||
| 669 | ' */', |
||
| 670 | )), |
||
| 671 | 'default' => false, |
||
| 672 | 'type' => 'boolean', |
||
| 673 | ), |
||
| 674 | 'db_error_send' => array( |
||
| 675 | 'text' => implode("\n", array( |
||
| 676 | '/**', |
||
| 677 | ' * Send emails on database connection error', |
||
| 678 | ' *', |
||
| 679 | ' * @var bool', |
||
| 680 | ' */', |
||
| 681 | )), |
||
| 682 | 'default' => false, |
||
| 683 | 'type' => 'boolean', |
||
| 684 | ), |
||
| 685 | 'db_mb4' => array( |
||
| 686 | 'text' => implode("\n", array( |
||
| 687 | '/**', |
||
| 688 | ' * Override the default behavior of the database layer for mb4 handling', |
||
| 689 | ' * null keep the default behavior untouched', |
||
| 690 | ' *', |
||
| 691 | ' * @var null|bool', |
||
| 692 | ' */', |
||
| 693 | )), |
||
| 694 | 'default' => null, |
||
| 695 | 'type' => array('NULL', 'boolean'), |
||
| 696 | ), |
||
| 697 | 'cache_accelerator' => array( |
||
| 698 | 'text' => implode("\n", array( |
||
| 699 | '', |
||
| 700 | '########## Cache Info ##########', |
||
| 701 | '/**', |
||
| 702 | ' * Select a cache system. You want to leave this up to the cache area of the admin panel for', |
||
| 703 | ' * proper detection of apc, memcached, output_cache, smf, or xcache', |
||
| 704 | ' * (you can add more with a mod).', |
||
| 705 | ' *', |
||
| 706 | ' * @var string', |
||
| 707 | ' */', |
||
| 708 | )), |
||
| 709 | 'default' => '', |
||
| 710 | 'type' => 'string', |
||
| 711 | ), |
||
| 712 | 'cache_enable' => array( |
||
| 713 | 'text' => implode("\n", array( |
||
| 714 | '/**', |
||
| 715 | ' * The level at which you would like to cache. Between 0 (off) through 3 (cache a lot).', |
||
| 716 | ' *', |
||
| 717 | ' * @var int', |
||
| 718 | ' */', |
||
| 719 | )), |
||
| 720 | 'default' => 0, |
||
| 721 | 'type' => 'integer', |
||
| 722 | ), |
||
| 723 | 'cache_memcached' => array( |
||
| 724 | 'text' => implode("\n", array( |
||
| 725 | '/**', |
||
| 726 | ' * This is only used for memcache / memcached. Should be a string of \'server:port,server:port\'', |
||
| 727 | ' *', |
||
| 728 | ' * @var array', |
||
| 729 | ' */', |
||
| 730 | )), |
||
| 731 | 'default' => '', |
||
| 732 | 'type' => 'string', |
||
| 733 | ), |
||
| 734 | 'cachedir' => array( |
||
| 735 | 'text' => implode("\n", array( |
||
| 736 | '/**', |
||
| 737 | ' * This is only for the \'smf\' file cache system. It is the path to the cache directory.', |
||
| 738 | ' * It is also recommended that you place this in /tmp/ if you are going to use this.', |
||
| 739 | ' *', |
||
| 740 | ' * @var string', |
||
| 741 | ' */', |
||
| 742 | )), |
||
| 743 | 'default' => 'dirname(__FILE__) . \'/cache\'', |
||
| 744 | 'raw_default' => true, |
||
| 745 | 'type' => 'string', |
||
| 746 | ), |
||
| 747 | 'image_proxy_enabled' => array( |
||
| 748 | 'text' => implode("\n", array( |
||
| 749 | '', |
||
| 750 | '########## Image Proxy ##########', |
||
| 751 | '# This is done entirely in Settings.php to avoid loading the DB while serving the images', |
||
| 752 | '/**', |
||
| 753 | ' * Whether the proxy is enabled or not', |
||
| 754 | ' *', |
||
| 755 | ' * @var bool', |
||
| 756 | ' */', |
||
| 757 | )), |
||
| 758 | 'default' => true, |
||
| 759 | 'type' => 'boolean', |
||
| 760 | ), |
||
| 761 | 'image_proxy_secret' => array( |
||
| 762 | 'text' => implode("\n", array( |
||
| 763 | '/**', |
||
| 764 | ' * Secret key to be used by the proxy', |
||
| 765 | ' *', |
||
| 766 | ' * @var string', |
||
| 767 | ' */', |
||
| 768 | )), |
||
| 769 | 'default' => 'smfisawesome', |
||
| 770 | 'type' => 'string', |
||
| 771 | ), |
||
| 772 | 'image_proxy_maxsize' => array( |
||
| 773 | 'text' => implode("\n", array( |
||
| 774 | '/**', |
||
| 775 | ' * Maximum file size (in KB) for individual files', |
||
| 776 | ' *', |
||
| 777 | ' * @var int', |
||
| 778 | ' */', |
||
| 779 | )), |
||
| 780 | 'default' => 5192, |
||
| 781 | 'type' => 'integer', |
||
| 782 | ), |
||
| 783 | 'boarddir' => array( |
||
| 784 | 'text' => implode("\n", array( |
||
| 785 | '', |
||
| 786 | '########## Directories/Files ##########', |
||
| 787 | '# Note: These directories do not have to be changed unless you move things.', |
||
| 788 | '/**', |
||
| 789 | ' * The absolute path to the forum\'s folder. (not just \'.\'!)', |
||
| 790 | ' *', |
||
| 791 | ' * @var string', |
||
| 792 | ' */', |
||
| 793 | )), |
||
| 794 | 'default' => 'dirname(__FILE__)', |
||
| 795 | 'raw_default' => true, |
||
| 796 | 'type' => 'string', |
||
| 797 | ), |
||
| 798 | 'sourcedir' => array( |
||
| 799 | 'text' => implode("\n", array( |
||
| 800 | '/**', |
||
| 801 | ' * Path to the Sources directory.', |
||
| 802 | ' *', |
||
| 803 | ' * @var string', |
||
| 804 | ' */', |
||
| 805 | )), |
||
| 806 | 'default' => 'dirname(__FILE__) . \'/Sources\'', |
||
| 807 | 'raw_default' => true, |
||
| 808 | 'type' => 'string', |
||
| 809 | ), |
||
| 810 | 'packagesdir' => array( |
||
| 811 | 'text' => implode("\n", array( |
||
| 812 | '/**', |
||
| 813 | ' * Path to the Packages directory.', |
||
| 814 | ' *', |
||
| 815 | ' * @var string', |
||
| 816 | ' */', |
||
| 817 | )), |
||
| 818 | 'default' => 'dirname(__FILE__) . \'/Packages\'', |
||
| 819 | 'raw_default' => true, |
||
| 820 | 'type' => 'string', |
||
| 821 | ), |
||
| 822 | 'tasksdir' => array( |
||
| 823 | 'text' => implode("\n", array( |
||
| 824 | '/**', |
||
| 825 | ' * Path to the tasks directory.', |
||
| 826 | ' *', |
||
| 827 | ' * @var string', |
||
| 828 | ' */', |
||
| 829 | )), |
||
| 830 | 'default' => '$sourcedir . \'/tasks\'', |
||
| 831 | 'raw_default' => true, |
||
| 832 | 'type' => 'string', |
||
| 833 | ), |
||
| 834 | array( |
||
| 835 | 'text' => implode("\n", array( |
||
| 836 | '', |
||
| 837 | '# Make sure the paths are correct... at least try to fix them.', |
||
| 838 | 'if (!is_dir(realpath($boarddir)) && file_exists(dirname(__FILE__) . \'/agreement.txt\'))', |
||
| 839 | ' $boarddir = dirname(__FILE__);', |
||
| 840 | 'if (!is_dir(realpath($sourcedir)) && is_dir($boarddir . \'/Sources\'))', |
||
| 841 | ' $sourcedir = $boarddir . \'/Sources\';', |
||
| 842 | 'if (!is_dir(realpath($tasksdir)) && is_dir($sourcedir . \'/tasks\'))', |
||
| 843 | ' $tasksdir = $sourcedir . \'/tasks\';', |
||
| 844 | 'if (!is_dir(realpath($packagesdir)) && is_dir($boarddir . \'/Packages\'))', |
||
| 845 | ' $packagesdir = $boarddir . \'/Packages\';', |
||
| 846 | 'if (!is_dir(realpath($cachedir)) && is_dir($boarddir . \'/cache\'))', |
||
| 847 | ' $cachedir = $boarddir . \'/cache\';', |
||
| 848 | )), |
||
| 849 | 'search_pattern' => '~\n?(#[^\n]+)?(?:\n\h*if\s*\((?:\!file_exists\(\$(?'.'>boarddir|sourcedir|tasksdir|packagesdir|cachedir)\)|\!is_dir\(realpath\(\$(?'.'>boarddir|sourcedir|tasksdir|packagesdir|cachedir)\)\))[^;]+\n\h*\$(?'.'>boarddir|sourcedir|tasksdir|packagesdir|cachedir)[^\n]+;)+~sm', |
||
| 850 | ), |
||
| 851 | 'db_character_set' => array( |
||
| 852 | 'text' => implode("\n", array( |
||
| 853 | '', |
||
| 854 | '######### Legacy Settings #########', |
||
| 855 | '# UTF-8 is now the only character set supported in 2.1.', |
||
| 856 | )), |
||
| 857 | 'default' => 'utf8', |
||
| 858 | 'type' => 'string', |
||
| 859 | ), |
||
| 860 | 'db_show_debug' => array( |
||
| 861 | 'text' => implode("\n", array( |
||
| 862 | '', |
||
| 863 | '######### Developer Settings #########', |
||
| 864 | '# Show debug info.', |
||
| 865 | )), |
||
| 866 | 'default' => false, |
||
| 867 | 'auto_delete' => 2, |
||
| 868 | 'type' => 'boolean', |
||
| 869 | ), |
||
| 870 | array( |
||
| 871 | 'text' => implode("\n", array( |
||
| 872 | '', |
||
| 873 | '########## Error-Catching ##########', |
||
| 874 | '# Note: You shouldn\'t touch these settings.', |
||
| 875 | 'if (file_exists((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\'))', |
||
| 876 | ' include((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\');', |
||
| 877 | '', |
||
| 878 | 'if (!isset($db_last_error))', |
||
| 879 | '{', |
||
| 880 | ' // File does not exist so lets try to create it', |
||
| 881 | ' file_put_contents((isset($cachedir) ? $cachedir : dirname(__FILE__)) . \'/db_last_error.php\', \'<\' . \'?\' . "php\n" . \'$db_last_error = 0;\' . "\n" . \'?\' . \'>\');', |
||
| 882 | ' $db_last_error = 0;', |
||
| 883 | '}', |
||
| 884 | )), |
||
| 885 | // Designed to match both 2.0 and 2.1 versions of this code. |
||
| 886 | 'search_pattern' => '~\n?#+ Error.Catching #+\n[^\n]*?settings\.\n(?:\$db_last_error = \d{1,11};|if \(file_exists.*?\$db_last_error = 0;(?' . '>\s*}))(?=\n|\?' . '>|$)~s', |
||
| 887 | ), |
||
| 888 | // Temporary variable used during the upgrade process. |
||
| 889 | 'upgradeData' => array( |
||
| 890 | 'default' => '', |
||
| 891 | 'auto_delete' => 1, |
||
| 892 | 'type' => 'string', |
||
| 893 | ), |
||
| 894 | // This should be removed if found. |
||
| 895 | 'db_last_error' => array( |
||
| 896 | 'default' => 0, |
||
| 897 | 'auto_delete' => 1, |
||
| 898 | 'type' => 'integer', |
||
| 899 | ), |
||
| 900 | ); |
||
| 901 | |||
| 902 | // Allow mods the option to define comments, defaults, etc., for their settings. |
||
| 903 | // Check if function exists, in case we are calling from installer or upgrader. |
||
| 904 | if (function_exists('call_integration_hook')) |
||
| 905 | call_integration_hook('integrate_update_settings_file', array(&$settings_defs)); |
||
| 906 | |||
| 907 | // If Settings.php is empty or invalid, try to recover using whatever is in $GLOBALS. |
||
| 908 | if ($settings_vars === array()) |
||
| 909 | { |
||
| 910 | foreach ($settings_defs as $var => $setting_def) |
||
| 911 | if (isset($GLOBALS[$var])) |
||
| 912 | $settings_vars[$var] = $GLOBALS[$var]; |
||
| 913 | |||
| 914 | $new_settings_vars = array_merge($settings_vars, $config_vars); |
||
| 915 | } |
||
| 916 | |||
| 917 | // During install/upgrade, don't set anything until we're ready for it. |
||
| 918 | if (defined('SMF_INSTALLING') && empty($rebuild)) |
||
| 919 | { |
||
| 920 | foreach ($settings_defs as $var => $setting_def) |
||
| 921 | if (!in_array($var, array_keys($new_settings_vars)) && !is_int($var)) |
||
| 922 | unset($settings_defs[$var]); |
||
| 923 | } |
||
| 924 | |||
| 925 | /******************************* |
||
| 926 | * PART 2: Build substitutions * |
||
| 927 | *******************************/ |
||
| 928 | |||
| 929 | $type_regex = array( |
||
| 930 | 'string' => |
||
| 931 | '(?:' . |
||
| 932 | // match the opening quotation mark... |
||
| 933 | '(["\'])' . |
||
| 934 | // then any number of other characters or escaped quotation marks... |
||
| 935 | '(?:.(?!\\1)|\\\(?=\\1))*.?' . |
||
| 936 | // then the closing quotation mark. |
||
| 937 | '\\1' . |
||
| 938 | // Maybe there's a second string concatenated to this one. |
||
| 939 | '(?:\s*\.\s*)*' . |
||
| 940 | ')+', |
||
| 941 | // Some numeric values might have been stored as strings. |
||
| 942 | 'integer' => '["\']?[+-]?\d+["\']?', |
||
| 943 | 'double' => '["\']?[+-]?\d+\.\d+([Ee][+-]\d+)?["\']?', |
||
| 944 | // Some boolean values might have been stored as integers. |
||
| 945 | 'boolean' => '(?i:TRUE|FALSE|(["\']?)[01]\b\\1)', |
||
| 946 | 'NULL' => '(?i:NULL)', |
||
| 947 | // These use a PCRE subroutine to match nested arrays. |
||
| 948 | 'array' => 'array\s*(\((?'.'>[^()]|(?1))*\))', |
||
| 949 | 'object' => '\w+::__set_state\(array\s*(\((?'.'>[^()]|(?1))*\))\)', |
||
| 950 | ); |
||
| 951 | |||
| 952 | /* |
||
| 953 | * The substitutions take place in one of two ways: |
||
| 954 | * |
||
| 955 | * 1: The search_pattern regex finds a string in Settings.php, which is |
||
| 956 | * temporarily replaced by a placeholder. Once all the placeholders |
||
| 957 | * have been inserted, each is replaced by the final replacement string |
||
| 958 | * that we want to use. This is the standard method. |
||
| 959 | * |
||
| 960 | * 2: The search_pattern regex finds a string in Settings.php, which is |
||
| 961 | * then deleted by replacing it with an empty placeholder. Then after |
||
| 962 | * all the real placeholders have been dealt with, the replace_pattern |
||
| 963 | * regex finds where to insert the final replacement string that we |
||
| 964 | * want to use. This method is for special cases. |
||
| 965 | */ |
||
| 966 | $prefix = mt_rand() . '-'; |
||
| 967 | $neg_index = -1; |
||
| 968 | $substitutions = array( |
||
| 969 | $neg_index-- => array( |
||
| 970 | 'search_pattern' => '~^\s*<\?(php\b)?\n?~', |
||
| 971 | 'placeholder' => '', |
||
| 972 | 'replace_pattern' => '~^~', |
||
| 973 | 'replacement' => '<' . "?php\n", |
||
| 974 | ), |
||
| 975 | $neg_index-- => array( |
||
| 976 | 'search_pattern' => '~\S\K\s*(\?' . '>)?\s*$~', |
||
| 977 | 'placeholder' => "\n" . md5($prefix . '?' . '>'), |
||
| 978 | 'replacement' => "\n\n?" . '>', |
||
| 979 | ), |
||
| 980 | // Remove the code that redirects to the installer. |
||
| 981 | $neg_index-- => array( |
||
| 982 | 'search_pattern' => '~^if\s*\(file_exists\(dirname\(__FILE__\)\s*\.\s*\'/install\.php\'\)\)\s*(?:({(?'.'>[^{}]|(?1))*})\h*|header(\((?' . '>[^()]|(?2))*\));\n)~m', |
||
| 983 | 'placeholder' => '', |
||
| 984 | ), |
||
| 985 | ); |
||
| 986 | |||
| 987 | if (defined('SMF_INSTALLING')) |
||
| 988 | $substitutions[$neg_index--] = array( |
||
| 989 | 'search_pattern' => '~/\*.*?SMF\s+1\.\d.*?\*/~s', |
||
| 990 | 'placeholder' => '', |
||
| 991 | ); |
||
| 992 | |||
| 993 | foreach ($settings_defs as $var => $setting_def) |
||
| 994 | { |
||
| 995 | $placeholder = md5($prefix . $var); |
||
| 996 | $replacement = ''; |
||
| 997 | |||
| 998 | if (!empty($setting_def['text'])) |
||
| 999 | { |
||
| 1000 | // Special handling for the license block: always at the beginning. |
||
| 1001 | if (strpos($setting_def['text'], "* @package SMF\n") !== false) |
||
| 1002 | { |
||
| 1003 | $substitutions[$var]['search_pattern'] = $setting_def['search_pattern']; |
||
| 1004 | $substitutions[$var]['placeholder'] = ''; |
||
| 1005 | $substitutions[-1]['replacement'] .= $setting_def['text'] . "\n"; |
||
| 1006 | } |
||
| 1007 | // Special handling for the Error-Catching block: always at the end. |
||
| 1008 | elseif (strpos($setting_def['text'], 'Error-Catching') !== false) |
||
| 1009 | { |
||
| 1010 | $errcatch_var = $var; |
||
| 1011 | $substitutions[$var]['search_pattern'] = $setting_def['search_pattern']; |
||
| 1012 | $substitutions[$var]['placeholder'] = ''; |
||
| 1013 | $substitutions[-2]['replacement'] = "\n" . $setting_def['text'] . $substitutions[-2]['replacement']; |
||
| 1014 | } |
||
| 1015 | // The text is the whole thing (code blocks, etc.) |
||
| 1016 | elseif (is_int($var)) |
||
| 1017 | { |
||
| 1018 | // Remember the path correcting code for later. |
||
| 1019 | if (strpos($setting_def['text'], '# Make sure the paths are correct') !== false) |
||
| 1020 | $pathcode_var = $var; |
||
| 1021 | |||
| 1022 | if (!empty($setting_def['search_pattern'])) |
||
| 1023 | $substitutions[$var]['search_pattern'] = $setting_def['search_pattern']; |
||
| 1024 | else |
||
| 1025 | $substitutions[$var]['search_pattern'] = '~' . preg_quote($setting_def['text'], '~') . '~'; |
||
| 1026 | |||
| 1027 | $substitutions[$var]['placeholder'] = $placeholder; |
||
| 1028 | |||
| 1029 | $replacement .= $setting_def['text'] . "\n"; |
||
| 1030 | } |
||
| 1031 | // We only include comments when rebuilding. |
||
| 1032 | elseif (!empty($rebuild)) |
||
| 1033 | $replacement .= $setting_def['text'] . "\n"; |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | if (is_string($var)) |
||
| 1037 | { |
||
| 1038 | // Ensure the value is good. |
||
| 1039 | if (in_array($var, array_keys($new_settings_vars))) |
||
| 1040 | { |
||
| 1041 | // Objects without a __set_state method need a fallback. |
||
| 1042 | if (is_object($new_settings_vars[$var]) && !method_exists($new_settings_vars[$var], '__set_state')) |
||
| 1043 | { |
||
| 1044 | if (method_exists($new_settings_vars[$var], '__toString')) |
||
| 1045 | $new_settings_vars[$var] = (string) $new_settings_vars[$var]; |
||
| 1046 | else |
||
| 1047 | $new_settings_vars[$var] = (array) $new_settings_vars[$var]; |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | // Normalize the type if necessary. |
||
| 1051 | if (isset($setting_def['type'])) |
||
| 1052 | { |
||
| 1053 | $expected_types = (array) $setting_def['type']; |
||
| 1054 | $var_type = gettype($new_settings_vars[$var]); |
||
| 1055 | |||
| 1056 | // Variable is not of an expected type. |
||
| 1057 | if (!in_array($var_type, $expected_types)) |
||
| 1058 | { |
||
| 1059 | // Passed in an unexpected array. |
||
| 1060 | if ($var_type == 'array') |
||
| 1061 | { |
||
| 1062 | $temp = reset($new_settings_vars[$var]); |
||
| 1063 | |||
| 1064 | // Use the first element if there's only one and it is a scalar. |
||
| 1065 | if (count($new_settings_vars[$var]) === 1 && is_scalar($temp)) |
||
| 1066 | $new_settings_vars[$var] = $temp; |
||
| 1067 | |||
| 1068 | // Or keep the old value, if that is good. |
||
| 1069 | elseif (isset($settings_vars[$var]) && in_array(gettype($settings_vars[$var]), $expected_types)) |
||
| 1070 | $new_settings_vars[$var] = $settings_vars[$var]; |
||
| 1071 | |||
| 1072 | // Fall back to the default |
||
| 1073 | else |
||
| 1074 | $new_settings_vars[$var] = $setting_def['default']; |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | // Cast it to whatever type was expected. |
||
| 1078 | // Note: the order of the types in this loop matters. |
||
| 1079 | foreach (array('boolean', 'integer', 'double', 'string', 'array') as $to_type) |
||
| 1080 | { |
||
| 1081 | if (in_array($to_type, $expected_types)) |
||
| 1082 | { |
||
| 1083 | settype($new_settings_vars[$var], $to_type); |
||
| 1084 | break; |
||
| 1085 | } |
||
| 1086 | } |
||
| 1087 | } |
||
| 1088 | } |
||
| 1089 | } |
||
| 1090 | // Abort if a required one is undefined (unless we're installing). |
||
| 1091 | elseif (!empty($setting_def['required']) && !defined('SMF_INSTALLING')) |
||
| 1092 | return false; |
||
| 1093 | |||
| 1094 | // Create the search pattern. |
||
| 1095 | if (!empty($setting_def['search_pattern'])) |
||
| 1096 | $substitutions[$var]['search_pattern'] = $setting_def['search_pattern']; |
||
| 1097 | else |
||
| 1098 | { |
||
| 1099 | $var_pattern = array(); |
||
| 1100 | |||
| 1101 | if (isset($setting_def['type'])) |
||
| 1102 | { |
||
| 1103 | foreach ((array) $setting_def['type'] as $type) |
||
| 1104 | $var_pattern[] = $type_regex[$type]; |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | if (in_array($var, array_keys($config_vars))) |
||
| 1108 | { |
||
| 1109 | $var_pattern[] = @$type_regex[gettype($config_vars[$var])]; |
||
| 1110 | |||
| 1111 | if (is_string($config_vars[$var]) && strpos($config_vars[$var], dirname($settingsFile)) === 0) |
||
| 1112 | $var_pattern[] = '(?:__DIR__|dirname\(__FILE__\)) . \'' . (preg_quote(str_replace(dirname($settingsFile), '', $config_vars[$var]), '~')) . '\''; |
||
| 1113 | } |
||
| 1114 | |||
| 1115 | if (in_array($var, array_keys($settings_vars))) |
||
| 1116 | { |
||
| 1117 | $var_pattern[] = @$type_regex[gettype($settings_vars[$var])]; |
||
| 1118 | |||
| 1119 | if (is_string($settings_vars[$var]) && strpos($settings_vars[$var], dirname($settingsFile)) === 0) |
||
| 1120 | $var_pattern[] = '(?:__DIR__|dirname\(__FILE__\)) . \'' . (preg_quote(str_replace(dirname($settingsFile), '', $settings_vars[$var]), '~')) . '\''; |
||
| 1121 | } |
||
| 1122 | |||
| 1123 | if (!empty($setting_def['raw_default']) && $setting_def['default'] !== '') |
||
| 1124 | { |
||
| 1125 | $var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote($setting_def['default'], '~')); |
||
| 1126 | |||
| 1127 | if (strpos($setting_def['default'], 'dirname(__FILE__)') !== false) |
||
| 1128 | $var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote(str_replace('dirname(__FILE__)', '__DIR__', $setting_def['default']), '~')); |
||
| 1129 | |||
| 1130 | if (strpos($setting_def['default'], '__DIR__') !== false) |
||
| 1131 | $var_pattern[] = preg_replace('/\s+/', '\s+', preg_quote(str_replace('__DIR__', 'dirname(__FILE__)', $setting_def['default']), '~')); |
||
| 1132 | } |
||
| 1133 | |||
| 1134 | $var_pattern = array_unique($var_pattern); |
||
| 1135 | |||
| 1136 | $var_pattern = count($var_pattern) > 1 ? '(?:' . (implode('|', $var_pattern)) . ')' : $var_pattern[0]; |
||
| 1137 | |||
| 1138 | $substitutions[$var]['search_pattern'] = '~(?<=^|\s)\h*\$' . preg_quote($var, '~') . '\s*=\s*' . $var_pattern . ';~' . (!empty($context['utf8']) ? 'u' : ''); |
||
| 1139 | } |
||
| 1140 | |||
| 1141 | // Next create the placeholder or replace_pattern. |
||
| 1142 | if (!empty($setting_def['replace_pattern'])) |
||
| 1143 | $substitutions[$var]['replace_pattern'] = $setting_def['replace_pattern']; |
||
| 1144 | else |
||
| 1145 | $substitutions[$var]['placeholder'] = $placeholder; |
||
| 1146 | |||
| 1147 | // Now create the replacement. |
||
| 1148 | // A setting to delete. |
||
| 1149 | if (!empty($setting_def['auto_delete']) && empty($new_settings_vars[$var])) |
||
| 1150 | { |
||
| 1151 | if ($setting_def['auto_delete'] === 2 && empty($rebuild) && in_array($var, array_keys($new_settings_vars))) |
||
| 1152 | { |
||
| 1153 | $replacement .= '$' . $var . ' = ' . ($new_settings_vars[$var] === $setting_def['default'] && !empty($setting_def['raw_default']) ? sprintf($new_settings_vars[$var]) : smf_var_export($new_settings_vars[$var], true)) . ";"; |
||
| 1154 | } |
||
| 1155 | else |
||
| 1156 | { |
||
| 1157 | $replacement = ''; |
||
| 1158 | |||
| 1159 | // This is just for cosmetic purposes. Removes the blank line. |
||
| 1160 | $substitutions[$var]['search_pattern'] = str_replace('(?<=^|\s)', '\n?', $substitutions[$var]['search_pattern']); |
||
| 1161 | } |
||
| 1162 | } |
||
| 1163 | // Add this setting's value. |
||
| 1164 | elseif (in_array($var, array_keys($new_settings_vars))) |
||
| 1165 | { |
||
| 1166 | $replacement .= '$' . $var . ' = ' . ($new_settings_vars[$var] === $setting_def['default'] && !empty($setting_def['raw_default']) ? sprintf($new_settings_vars[$var]) : smf_var_export($new_settings_vars[$var], true)) . ";"; |
||
| 1167 | } |
||
| 1168 | // Fall back to the default value. |
||
| 1169 | elseif (isset($setting_def['default'])) |
||
| 1170 | { |
||
| 1171 | $replacement .= '$' . $var . ' = ' . (!empty($setting_def['raw_default']) ? sprintf($setting_def['default']) : smf_var_export($setting_def['default'], true)) . ';'; |
||
| 1172 | } |
||
| 1173 | // This shouldn't happen, but we've got nothing. |
||
| 1174 | else |
||
| 1175 | $replacement .= '$' . $var . ' = null;'; |
||
| 1176 | } |
||
| 1177 | |||
| 1178 | $substitutions[$var]['replacement'] = $replacement; |
||
| 1179 | |||
| 1180 | // We're done with this one. |
||
| 1181 | unset($new_settings_vars[$var]); |
||
| 1182 | } |
||
| 1183 | |||
| 1184 | // Any leftovers to deal with? |
||
| 1185 | foreach ($new_settings_vars as $var => $val) |
||
| 1186 | { |
||
| 1187 | $var_pattern = array(); |
||
| 1188 | |||
| 1189 | if (in_array($var, array_keys($config_vars))) |
||
| 1190 | $var_pattern[] = $type_regex[gettype($config_vars[$var])]; |
||
| 1191 | |||
| 1192 | if (in_array($var, array_keys($settings_vars))) |
||
| 1193 | $var_pattern[] = $type_regex[gettype($settings_vars[$var])]; |
||
| 1194 | |||
| 1195 | $var_pattern = array_unique($var_pattern); |
||
| 1196 | |||
| 1197 | $var_pattern = count($var_pattern) > 1 ? '(?:' . (implode('|', $var_pattern)) . ')' : $var_pattern[0]; |
||
| 1198 | |||
| 1199 | $placeholder = md5($prefix . $var); |
||
| 1200 | |||
| 1201 | $substitutions[$var]['search_pattern'] = '~(?<=^|\s)\h*\$' . preg_quote($var, '~') . '\s*=\s*' . $var_pattern . ';~' . (!empty($context['utf8']) ? 'u' : ''); |
||
| 1202 | $substitutions[$var]['placeholder'] = $placeholder; |
||
| 1203 | $substitutions[$var]['replacement'] = '$' . $var . ' = ' . smf_var_export($val, true) . ";"; |
||
| 1204 | } |
||
| 1205 | |||
| 1206 | // During an upgrade, some of the path variables may not have been declared yet. |
||
| 1207 | if (defined('SMF_INSTALLING') && empty($rebuild)) |
||
| 1208 | { |
||
| 1209 | preg_match_all('~^\h*\$(\w+)\s*=\s*~m', $substitutions[$pathcode_var]['replacement'], $matches); |
||
| 1210 | $missing_pathvars = array_diff($matches[1], array_keys($substitutions)); |
||
| 1211 | |||
| 1212 | if (!empty($missing_pathvars)) |
||
| 1213 | { |
||
| 1214 | foreach ($missing_pathvars as $var) |
||
| 1215 | { |
||
| 1216 | $substitutions[$pathcode_var]['replacement'] = preg_replace('~\nif[^\n]+\$' . $var . '[^\n]+\n\h*\$' . $var . ' = [^\n]+~', '', $substitutions[$pathcode_var]['replacement']); |
||
| 1217 | } |
||
| 1218 | } |
||
| 1219 | } |
||
| 1220 | |||
| 1221 | // It's important to do the numbered ones before the named ones, or messes happen. |
||
| 1222 | uksort($substitutions, function($a, $b) { |
||
| 1223 | if (is_int($a) && is_int($b)) |
||
| 1224 | return $a > $b; |
||
| 1225 | elseif (is_int($a)) |
||
| 1226 | return -1; |
||
| 1227 | elseif (is_int($b)) |
||
| 1228 | return 1; |
||
| 1229 | else |
||
| 1230 | return strcasecmp($b, $a); |
||
| 1231 | }); |
||
| 1232 | |||
| 1233 | /****************************** |
||
| 1234 | * PART 3: Content processing * |
||
| 1235 | ******************************/ |
||
| 1236 | |||
| 1237 | /* 3.a: Get the content of Settings.php and make sure it is good. */ |
||
| 1238 | |||
| 1239 | // Retrieve the contents of Settings.php and normalize the line endings. |
||
| 1240 | $settingsText = trim(strtr(file_get_contents($settingsFile), array("\r\n" => "\n", "\r" => "\n"))); |
||
| 1241 | |||
| 1242 | // If Settings.php is empty or corrupt for some reason, see if we can recover. |
||
| 1243 | if ($settingsText == '' || substr($settingsText, 0, 5) !== '<' . '?php') |
||
| 1244 | { |
||
| 1245 | // Try restoring from the backup. |
||
| 1246 | if (file_exists(dirname($settingsFile) . '/Settings_bak.php')) |
||
| 1247 | $settingsText = strtr(file_get_contents(dirname($settingsFile) . '/Settings_bak.php'), array("\r\n" => "\n", "\r" => "\n")); |
||
| 1248 | |||
| 1249 | // Backup is bad too? Our only option is to create one from scratch. |
||
| 1250 | if ($settingsText == '' || substr($settingsText, 0, 5) !== '<' . '?php' || substr($settingsText, -2) !== '?' . '>') |
||
| 1251 | { |
||
| 1252 | $settingsText = '<' . "?php\n"; |
||
| 1253 | foreach ($settings_defs as $var => $setting_def) |
||
| 1254 | { |
||
| 1255 | if (!empty($setting_def['text']) && strpos($substitutions[$var]['replacement'], $setting_def['text']) === false) |
||
| 1256 | $substitutions[$var]['replacement'] = $setting_def['text'] . "\n" . $substitutions[$var]['replacement']; |
||
| 1257 | |||
| 1258 | $settingsText .= $substitutions[$var]['replacement'] . "\n"; |
||
| 1259 | } |
||
| 1260 | $settingsText .= "\n\n?" . '>'; |
||
| 1261 | $rebuild = true; |
||
| 1262 | } |
||
| 1263 | } |
||
| 1264 | |||
| 1265 | // Settings.php is unlikely to contain any heredocs, but just in case... |
||
| 1266 | if (preg_match_all('/<<<(\'?)(\w+)\'?\n(.*?)\n\2;$/m', $settingsText, $matches)) |
||
| 1267 | { |
||
| 1268 | foreach ($matches[0] as $mkey => $heredoc) |
||
| 1269 | { |
||
| 1270 | if (!empty($matches[1][$mkey])) |
||
| 1271 | $heredoc_replacements[$heredoc] = var_export($matches[3][$mkey], true) . ';'; |
||
| 1272 | else |
||
| 1273 | $heredoc_replacements[$heredoc] = '"' . strtr(substr(var_export($matches[3][$mkey], true), 1, -1), array("\\'" => "'", '"' => '\"')) . '";'; |
||
| 1274 | } |
||
| 1275 | |||
| 1276 | $settingsText = strtr($settingsText, $heredoc_replacements); |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | /* 3.b: Loop through all our substitutions to insert placeholders, etc. */ |
||
| 1280 | |||
| 1281 | $last_var = null; |
||
| 1282 | $bare_settingsText = $settingsText; |
||
| 1283 | $force_before_pathcode = array(); |
||
| 1284 | foreach ($substitutions as $var => $substitution) |
||
| 1285 | { |
||
| 1286 | $placeholders[$var] = $substitution['placeholder']; |
||
| 1287 | |||
| 1288 | if (!empty($substitution['placeholder'])) |
||
| 1289 | { |
||
| 1290 | $simple_replacements[$substitution['placeholder']] = $substitution['replacement']; |
||
| 1291 | } |
||
| 1292 | elseif (!empty($substitution['replace_pattern'])) |
||
| 1293 | { |
||
| 1294 | $replace_patterns[$var] = $substitution['replace_pattern']; |
||
| 1295 | $replace_strings[$var] = $substitution['replacement']; |
||
| 1296 | } |
||
| 1297 | |||
| 1298 | if (strpos($substitutions[$pathcode_var]['replacement'], '$' . $var . ' = ') !== false) |
||
| 1299 | $force_before_pathcode[] = $var; |
||
| 1300 | |||
| 1301 | // Look before you leap. |
||
| 1302 | preg_match_all($substitution['search_pattern'], $bare_settingsText, $matches); |
||
| 1303 | |||
| 1304 | if ((is_string($var) || $var === $pathcode_var) && count($matches[0]) !== 1 && $substitution['replacement'] !== '') |
||
| 1305 | { |
||
| 1306 | // More than one instance of the variable = not good. |
||
| 1307 | if (count($matches[0]) > 1) |
||
| 1308 | { |
||
| 1309 | if (is_string($var)) |
||
| 1310 | { |
||
| 1311 | // Maybe we can try something more interesting? |
||
| 1312 | $sp = substr($substitution['search_pattern'], 1); |
||
| 1313 | |||
| 1314 | if (strpos($sp, '(?<=^|\s)') === 0) |
||
| 1315 | $sp = substr($sp, 9); |
||
| 1316 | |||
| 1317 | if (strpos($sp, '^') === 0 || strpos($sp, '(?<') === 0) |
||
| 1318 | return false; |
||
| 1319 | |||
| 1320 | // See if we can exclude `if` blocks, etc., to narrow down the matches. |
||
| 1321 | // @todo Multiple layers of nested brackets might confuse this. |
||
| 1322 | $sp = '~(?:^|//[^\n]+c\n|\*/|[;}]|' . implode('|', array_filter($placeholders)) . ')\s*' . (strpos($sp, '\K') === false ? '\K' : '') . $sp; |
||
| 1323 | |||
| 1324 | preg_match_all($sp, $settingsText, $matches); |
||
| 1325 | } |
||
| 1326 | else |
||
| 1327 | $sp = $substitution['search_pattern']; |
||
| 1328 | |||
| 1329 | // Found at least some that are simple assignment statements. |
||
| 1330 | if (count($matches[0]) > 0) |
||
| 1331 | { |
||
| 1332 | // Remove any duplicates. |
||
| 1333 | if (count($matches[0]) > 1) |
||
| 1334 | $settingsText = preg_replace($sp, '', $settingsText, count($matches[0]) - 1); |
||
| 1335 | |||
| 1336 | // Insert placeholder for the last one. |
||
| 1337 | $settingsText = preg_replace($sp, $substitution['placeholder'], $settingsText, 1); |
||
| 1338 | } |
||
| 1339 | |||
| 1340 | // All instances are inside more complex code structures. |
||
| 1341 | else |
||
| 1342 | { |
||
| 1343 | // Only safe option at this point is to skip it. |
||
| 1344 | unset($substitutions[$var], $new_settings_vars[$var], $settings_defs[$var], $simple_replacements[$substitution['placeholder']], $replace_patterns[$var], $replace_strings[$var]); |
||
| 1345 | |||
| 1346 | continue; |
||
| 1347 | } |
||
| 1348 | } |
||
| 1349 | // No matches found. |
||
| 1350 | elseif (count($matches[0]) === 0) |
||
| 1351 | { |
||
| 1352 | $found = false; |
||
| 1353 | $in_c = in_array($var, array_keys($config_vars)); |
||
| 1354 | $in_s = in_array($var, array_keys($settings_vars)); |
||
| 1355 | |||
| 1356 | // Is it in there at all? |
||
| 1357 | if (!preg_match('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*~', $bare_settingsText)) |
||
| 1358 | { |
||
| 1359 | // It's defined by Settings.php, but not by code in the file. |
||
| 1360 | // Probably done via an include or something. Skip it. |
||
| 1361 | if ($in_s) |
||
| 1362 | unset($substitutions[$var], $settings_defs[$var]); |
||
| 1363 | |||
| 1364 | // Admin is explicitly trying to set this one, so we'll handle |
||
| 1365 | // it as if it were a new custom setting being added. |
||
| 1366 | elseif ($in_c) |
||
| 1367 | $new_settings_vars[$var] = $config_vars[$var]; |
||
| 1368 | |||
| 1369 | continue; |
||
| 1370 | } |
||
| 1371 | |||
| 1372 | // It's in there somewhere, so check if the value changed type. |
||
| 1373 | foreach (array('scalar', 'object', 'array') as $type) |
||
| 1374 | { |
||
| 1375 | // Try all the other scalar types first. |
||
| 1376 | if ($type == 'scalar') |
||
| 1377 | $sp = '(?:' . (implode('|', array_diff_key($type_regex, array($in_c ? gettype($config_vars[$var]) : ($in_s ? gettype($settings_vars[$var]) : PHP_INT_MAX) => '', 'array' => '', 'object' => '')))) . ')'; |
||
| 1378 | |||
| 1379 | // Maybe it's an object? (Probably not, but we should check.) |
||
| 1380 | elseif ($type == 'object') |
||
| 1381 | { |
||
| 1382 | if (strpos($settingsText, '__set_state') === false) |
||
| 1383 | continue; |
||
| 1384 | |||
| 1385 | $sp = $type_regex['object']; |
||
| 1386 | } |
||
| 1387 | |||
| 1388 | // Maybe it's an array? |
||
| 1389 | else |
||
| 1390 | $sp = $type_regex['array']; |
||
| 1391 | |||
| 1392 | if (preg_match('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*' . $sp . '~', $bare_settingsText, $derp)) |
||
| 1393 | { |
||
| 1394 | $settingsText = preg_replace('~(^|\s)\$' . preg_quote($var, '~') . '\s*=\s*' . $sp . '~', $substitution['placeholder'], $settingsText); |
||
| 1395 | $found = true; |
||
| 1396 | break; |
||
| 1397 | } |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | // Something weird is going on. Better just leave it alone. |
||
| 1401 | if (!$found) |
||
| 1402 | { |
||
| 1403 | // $var? What $var? Never heard of it. |
||
| 1404 | unset($substitutions[$var], $new_settings_vars[$var], $settings_defs[$var], $simple_replacements[$substitution['placeholder']], $replace_patterns[$var], $replace_strings[$var]); |
||
| 1405 | continue; |
||
| 1406 | } |
||
| 1407 | } |
||
| 1408 | } |
||
| 1409 | // Good to go, so insert our placeholder. |
||
| 1410 | else |
||
| 1411 | $settingsText = preg_replace($substitution['search_pattern'], $substitution['placeholder'], $settingsText); |
||
| 1412 | |||
| 1413 | // Once the code blocks are done, we want to compare to a version without comments. |
||
| 1414 | if (is_int($last_var) && is_string($var)) |
||
| 1415 | $bare_settingsText = strip_php_comments($settingsText); |
||
| 1416 | |||
| 1417 | $last_var = $var; |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | // Rebuilding requires more work. |
||
| 1421 | if (!empty($rebuild)) |
||
| 1422 | { |
||
| 1423 | // Strip out the leading and trailing placeholders to prevent duplication. |
||
| 1424 | $settingsText = str_replace(array($substitutions[-1]['placeholder'], $substitutions[-2]['placeholder']), '', $settingsText); |
||
| 1425 | |||
| 1426 | // Strip out all our standard comments. |
||
| 1427 | foreach ($settings_defs as $var => $setting_def) |
||
| 1428 | { |
||
| 1429 | if (isset($setting_def['text'])) |
||
| 1430 | $settingsText = strtr($settingsText, array($setting_def['text'] . "\n" => '', $setting_def['text'] => '',)); |
||
| 1431 | } |
||
| 1432 | |||
| 1433 | // We need to refresh $bare_settingsText at this point. |
||
| 1434 | $bare_settingsText = strip_php_comments($settingsText); |
||
| 1435 | |||
| 1436 | // Fix up whitespace to make comparison easier. |
||
| 1437 | foreach ($placeholders as $placeholder) |
||
| 1438 | { |
||
| 1439 | $bare_settingsText = str_replace(array($placeholder . "\n\n", $placeholder), $placeholder . "\n", $bare_settingsText); |
||
| 1440 | } |
||
| 1441 | $bare_settingsText = preg_replace('/\h+$/m', '', rtrim($bare_settingsText)); |
||
| 1442 | |||
| 1443 | /* |
||
| 1444 | * Divide the existing content into sections. |
||
| 1445 | * The idea here is to make sure we don't mess with the relative position |
||
| 1446 | * of any code blocks in the file, since that could break things. Within |
||
| 1447 | * each section, however, we'll reorganize the content to match the |
||
| 1448 | * default layout as closely as we can. |
||
| 1449 | */ |
||
| 1450 | $sections = array(array()); |
||
| 1451 | $section_num = 0; |
||
| 1452 | $trimmed_placeholders = array_filter(array_map('trim', $placeholders)); |
||
| 1453 | $newsection_placeholders = array(); |
||
| 1454 | $all_custom_content = ''; |
||
| 1455 | foreach ($substitutions as $var => $substitution) |
||
| 1456 | { |
||
| 1457 | if (is_int($var) && ($var === -2 || $var > 0) && isset($trimmed_placeholders[$var]) && strpos($bare_settingsText, $trimmed_placeholders[$var]) !== false) |
||
| 1458 | $newsection_placeholders[$var] = $trimmed_placeholders[$var]; |
||
| 1459 | } |
||
| 1460 | foreach (preg_split('~(?<=' . implode('|', $trimmed_placeholders) . ')|(?=' . implode('|', $trimmed_placeholders) . ')~', $bare_settingsText) as $part) |
||
| 1461 | { |
||
| 1462 | $part = trim($part); |
||
| 1463 | |||
| 1464 | if (empty($part)) |
||
| 1465 | continue; |
||
| 1466 | |||
| 1467 | // Build a list of placeholders for this section. |
||
| 1468 | if (in_array($part, $trimmed_placeholders) && !in_array($part, $newsection_placeholders)) |
||
| 1469 | { |
||
| 1470 | $sections[$section_num][] = $part; |
||
| 1471 | } |
||
| 1472 | // Custom content and newsection_placeholders get their own sections. |
||
| 1473 | else |
||
| 1474 | { |
||
| 1475 | if (!empty($sections[$section_num])) |
||
| 1476 | ++$section_num; |
||
| 1477 | |||
| 1478 | $sections[$section_num][] = $part; |
||
| 1479 | |||
| 1480 | ++$section_num; |
||
| 1481 | |||
| 1482 | if (!in_array($part, $trimmed_placeholders)) |
||
| 1483 | $all_custom_content .= "\n" . $part; |
||
| 1484 | } |
||
| 1485 | } |
||
| 1486 | |||
| 1487 | // And now, rebuild the content! |
||
| 1488 | $new_settingsText = ''; |
||
| 1489 | $done_defs = array(); |
||
| 1490 | $sectionkeys = array_keys($sections); |
||
| 1491 | foreach ($sections as $sectionkey => $section) |
||
| 1492 | { |
||
| 1493 | // Custom content needs to be preserved. |
||
| 1494 | if (count($section) === 1 && !in_array($section[0], $trimmed_placeholders)) |
||
| 1495 | { |
||
| 1496 | $prev_section_end = $sectionkey < 1 ? 0 : strpos($settingsText, end($sections[$sectionkey - 1])) + strlen(end($sections[$sectionkey - 1])); |
||
| 1497 | $next_section_start = $sectionkey == end($sectionkeys) ? strlen($settingsText) : strpos($settingsText, $sections[$sectionkey + 1][0]); |
||
| 1498 | |||
| 1499 | $new_settingsText .= "\n" . substr($settingsText, $prev_section_end, $next_section_start - $prev_section_end) . "\n"; |
||
| 1500 | } |
||
| 1501 | // Put the placeholders in this section into canonical order. |
||
| 1502 | else |
||
| 1503 | { |
||
| 1504 | $section_parts = array_flip($section); |
||
| 1505 | $pathcode_reached = false; |
||
| 1506 | foreach ($settings_defs as $var => $setting_def) |
||
| 1507 | { |
||
| 1508 | if ($var === $pathcode_var) |
||
| 1509 | $pathcode_reached = true; |
||
| 1510 | |||
| 1511 | // Already did this setting, so move on to the next. |
||
| 1512 | if (in_array($var, $done_defs)) |
||
| 1513 | continue; |
||
| 1514 | |||
| 1515 | // Stop when we hit a setting definition that will start a later section. |
||
| 1516 | if (isset($newsection_placeholders[$var]) && count($section) !== 1) |
||
| 1517 | break; |
||
| 1518 | |||
| 1519 | // Stop when everything in this section is done, unless it's the last. |
||
| 1520 | // This helps maintain the relative position of any custom content. |
||
| 1521 | if (empty($section_parts) && $sectionkey < (count($sections) - 1)) |
||
| 1522 | break; |
||
| 1523 | |||
| 1524 | $p = trim($substitutions[$var]['placeholder']); |
||
| 1525 | |||
| 1526 | // Can't do anything with an empty placeholder. |
||
| 1527 | if ($p === '') |
||
| 1528 | continue; |
||
| 1529 | |||
| 1530 | // Does this need to be inserted before the path correction code? |
||
| 1531 | if (strpos($new_settingsText, trim($substitutions[$pathcode_var]['placeholder'])) !== false && in_array($var, $force_before_pathcode)) |
||
| 1532 | { |
||
| 1533 | $new_settingsText = strtr($new_settingsText, array($substitutions[$pathcode_var]['placeholder'] => $p . "\n" . $substitutions[$pathcode_var]['placeholder'])); |
||
| 1534 | |||
| 1535 | $bare_settingsText .= "\n" . $substitutions[$var]['placeholder']; |
||
| 1536 | $done_defs[] = $var; |
||
| 1537 | unset($section_parts[trim($substitutions[$var]['placeholder'])]); |
||
| 1538 | } |
||
| 1539 | |||
| 1540 | // If it's in this section, add it to the new text now. |
||
| 1541 | elseif (in_array($p, $section)) |
||
| 1542 | { |
||
| 1543 | $new_settingsText .= "\n" . $substitutions[$var]['placeholder']; |
||
| 1544 | $done_defs[] = $var; |
||
| 1545 | unset($section_parts[trim($substitutions[$var]['placeholder'])]); |
||
| 1546 | } |
||
| 1547 | |||
| 1548 | // Perhaps it is safe to reposition it anyway. |
||
| 1549 | elseif (is_string($var) && strpos($new_settingsText, $p) === false && strpos($all_custom_content, '$' . $var) === false) |
||
| 1550 | { |
||
| 1551 | $new_settingsText .= "\n" . $substitutions[$var]['placeholder']; |
||
| 1552 | $done_defs[] = $var; |
||
| 1553 | unset($section_parts[trim($substitutions[$var]['placeholder'])]); |
||
| 1554 | } |
||
| 1555 | |||
| 1556 | // If this setting is missing entirely, fix it. |
||
| 1557 | elseif (strpos($bare_settingsText, $p) === false) |
||
| 1558 | { |
||
| 1559 | // Special case if the path code is missing. Put it near the end, |
||
| 1560 | // and also anything else that is missing that normally follows it. |
||
| 1561 | if (!isset($newsection_placeholders[$pathcode_var]) && $pathcode_reached === true && $sectionkey < (count($sections) - 1)) |
||
| 1562 | break; |
||
| 1563 | |||
| 1564 | $new_settingsText .= "\n" . $substitutions[$var]['placeholder']; |
||
| 1565 | $bare_settingsText .= "\n" . $substitutions[$var]['placeholder']; |
||
| 1566 | $done_defs[] = $var; |
||
| 1567 | unset($section_parts[trim($substitutions[$var]['placeholder'])]); |
||
| 1568 | } |
||
| 1569 | } |
||
| 1570 | } |
||
| 1571 | } |
||
| 1572 | $settingsText = $new_settingsText; |
||
| 1573 | |||
| 1574 | // Restore the leading and trailing placeholders as necessary. |
||
| 1575 | foreach (array(-1, -2) as $var) |
||
| 1576 | { |
||
| 1577 | if (!empty($substitutions[$var]['placeholder']) && strpos($settingsText, $substitutions[$var]['placeholder']) === false); |
||
| 1578 | { |
||
| 1579 | $settingsText = ($var == -1 ? $substitutions[$var]['placeholder'] : '') . $settingsText . ($var == -2 ? $substitutions[$var]['placeholder'] : ''); |
||
| 1580 | } |
||
| 1581 | } |
||
| 1582 | } |
||
| 1583 | // Even if not rebuilding, there are a few variables that may need to be moved around. |
||
| 1584 | else |
||
| 1585 | { |
||
| 1586 | $pathcode_pos = strpos($settingsText, $substitutions[$pathcode_var]['placeholder']); |
||
| 1587 | |||
| 1588 | if ($pathcode_pos !== false) |
||
| 1589 | { |
||
| 1590 | foreach ($force_before_pathcode as $var) |
||
| 1591 | { |
||
| 1592 | if (!empty($substitutions[$var]['placeholder']) && strpos($settingsText, $substitutions[$var]['placeholder']) > $pathcode_pos) |
||
| 1593 | { |
||
| 1594 | $settingsText = strtr($settingsText, array( |
||
| 1595 | $substitutions[$var]['placeholder'] => '', |
||
| 1596 | $substitutions[$pathcode_var]['placeholder'] => $substitutions[$var]['placeholder'] . "\n" . $substitutions[$pathcode_var]['placeholder'], |
||
| 1597 | )); |
||
| 1598 | } |
||
| 1599 | } |
||
| 1600 | } |
||
| 1601 | } |
||
| 1602 | |||
| 1603 | /* 3.c: Replace the placeholders with the final values */ |
||
| 1604 | |||
| 1605 | // Where possible, perform simple substitutions. |
||
| 1606 | $settingsText = strtr($settingsText, $simple_replacements); |
||
| 1607 | |||
| 1608 | // Deal with any complicated ones. |
||
| 1609 | if (!empty($replace_patterns)) |
||
| 1610 | $settingsText = preg_replace($replace_patterns, $replace_strings, $settingsText); |
||
| 1611 | |||
| 1612 | // Make absolutely sure that the path correction code is included. |
||
| 1613 | if (strpos($settingsText, $substitutions[$pathcode_var]['replacement']) === false) |
||
| 1614 | $settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', "\n" . $substitutions[$pathcode_var]['replacement'] . "\n", $settingsText); |
||
| 1615 | |||
| 1616 | // If we did not rebuild, do just enough to make sure the thing is viable. |
||
| 1617 | if (empty($rebuild)) |
||
| 1618 | { |
||
| 1619 | // We need to refresh $bare_settingsText again, and remove the code blocks from it. |
||
| 1620 | $bare_settingsText = $settingsText; |
||
| 1621 | foreach ($substitutions as $var => $substitution) |
||
| 1622 | { |
||
| 1623 | if (!is_int($var)) |
||
| 1624 | break; |
||
| 1625 | |||
| 1626 | if (isset($substitution['replacement'])) |
||
| 1627 | $bare_settingsText = str_replace($substitution['replacement'], '', $bare_settingsText); |
||
| 1628 | } |
||
| 1629 | $bare_settingsText = strip_php_comments($bare_settingsText); |
||
| 1630 | |||
| 1631 | // Now insert any defined settings that are missing. |
||
| 1632 | $pathcode_reached = false; |
||
| 1633 | foreach ($settings_defs as $var => $setting_def) |
||
| 1634 | { |
||
| 1635 | if ($var === $pathcode_var) |
||
| 1636 | $pathcode_reached = true; |
||
| 1637 | |||
| 1638 | if (is_int($var)) |
||
| 1639 | continue; |
||
| 1640 | |||
| 1641 | // Do nothing if it is already in there. |
||
| 1642 | if (preg_match($substitutions[$var]['search_pattern'], $bare_settingsText)) |
||
| 1643 | continue; |
||
| 1644 | |||
| 1645 | // Insert it either before or after the path correction code, whichever is appropriate. |
||
| 1646 | if (!$pathcode_reached || in_array($var, $force_before_pathcode)) |
||
| 1647 | { |
||
| 1648 | $settingsText = preg_replace($substitutions[$pathcode_var]['search_pattern'], $substitutions[$var]['replacement'] . "\n$0", $settingsText); |
||
| 1649 | } |
||
| 1650 | else |
||
| 1651 | { |
||
| 1652 | $settingsText = preg_replace($substitutions[$pathcode_var]['search_pattern'], "$0\n" . $substitutions[$var]['replacement'], $settingsText); |
||
| 1653 | } |
||
| 1654 | } |
||
| 1655 | } |
||
| 1656 | |||
| 1657 | // If we have any brand new settings to add, do so. |
||
| 1658 | foreach ($new_settings_vars as $var => $val) |
||
| 1659 | { |
||
| 1660 | if (isset($substitutions[$var]) && !preg_match($substitutions[$var]['search_pattern'], $settingsText)) |
||
| 1661 | { |
||
| 1662 | if (!isset($settings_defs[$var]) && strpos($settingsText, '# Custom Settings #') === false) |
||
| 1663 | $settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', "\n\n######### Custom Settings #########\n", $settingsText); |
||
| 1664 | |||
| 1665 | $settingsText = preg_replace('~(?=\n#+ Error.Catching #+)~', $substitutions[$var]['replacement'] . "\n", $settingsText); |
||
| 1666 | } |
||
| 1667 | } |
||
| 1668 | |||
| 1669 | // This is just cosmetic. Get rid of extra lines of whitespace. |
||
| 1670 | $settingsText = preg_replace('~\n\s*\n~', "\n\n", $settingsText); |
||
| 1671 | |||
| 1672 | /************************************** |
||
| 1673 | * PART 4: Check syntax before saving * |
||
| 1674 | **************************************/ |
||
| 1675 | |||
| 1676 | $temp_sfile = tempnam(sm_temp_dir(), md5($prefix . 'Settings.php')); |
||
| 1677 | file_put_contents($temp_sfile, $settingsText); |
||
| 1678 | |||
| 1679 | $result = get_current_settings(filemtime($temp_sfile), $temp_sfile); |
||
| 1680 | |||
| 1681 | unlink($temp_sfile); |
||
| 1682 | |||
| 1683 | // If the syntax is borked, try rebuilding to see if that fixes it. |
||
| 1684 | if ($result === false) |
||
| 1685 | return empty($rebuild) ? updateSettingsFile($config_vars, $keep_quotes, true) : false; |
||
| 1686 | |||
| 1687 | /****************************************** |
||
| 1688 | * PART 5: Write updated settings to file * |
||
| 1689 | ******************************************/ |
||
| 1690 | |||
| 1691 | $success = safe_file_write($settingsFile, $settingsText, dirname($settingsFile) . '/Settings_bak.php', $last_settings_change); |
||
| 1692 | |||
| 1693 | // Remember this in case updateSettingsFile is called twice. |
||
| 1694 | $mtime = filemtime($settingsFile); |
||
| 1695 | |||
| 1696 | return $success; |
||
| 1697 | } |
||
| 2299 | ?> |