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