コード例 #1
0
ファイル: Load.php プロジェクト: KeiroD/Elkarte
/**
 * Load the $modSettings array and many necessary forum settings.
 *
 * What it does:
 * - load the settings from cache if available, otherwse from the database.
 * - sets the timezone
 * - checks the load average settings if available.
 * - check whether post moderation is enabled.
 * - calls add_integration_function
 * - calls integrate_pre_include, integrate_pre_load,
 *
 * @global array $modSettings is a giant array of all of the forum-wide settings and statistics.
 */
function reloadSettings()
{
    global $modSettings;
    $db = database();
    // Try to load it from the cache first; it'll never get cached if the setting is off.
    if (($modSettings = cache_get_data('modSettings', 90)) == null) {
        $request = $db->query('', '
			SELECT variable, value
			FROM {db_prefix}settings', array());
        $modSettings = array();
        if (!$request) {
            display_db_error();
        }
        while ($row = $db->fetch_row($request)) {
            $modSettings[$row[0]] = $row[1];
        }
        $db->free_result($request);
        // Do a few things to protect against missing settings or settings with invalid values...
        if (empty($modSettings['defaultMaxTopics']) || $modSettings['defaultMaxTopics'] <= 0 || $modSettings['defaultMaxTopics'] > 999) {
            $modSettings['defaultMaxTopics'] = 20;
        }
        if (empty($modSettings['defaultMaxMessages']) || $modSettings['defaultMaxMessages'] <= 0 || $modSettings['defaultMaxMessages'] > 999) {
            $modSettings['defaultMaxMessages'] = 15;
        }
        if (empty($modSettings['defaultMaxMembers']) || $modSettings['defaultMaxMembers'] <= 0 || $modSettings['defaultMaxMembers'] > 999) {
            $modSettings['defaultMaxMembers'] = 30;
        }
        if (empty($modSettings['subject_length'])) {
            $modSettings['subject_length'] = 24;
        }
        $modSettings['warning_enable'] = $modSettings['warning_settings'][0];
        if (!empty($modSettings['cache_enable'])) {
            cache_put_data('modSettings', $modSettings, 90);
        }
    }
    // Setting the timezone is a requirement for some functions in PHP >= 5.1.
    if (isset($modSettings['default_timezone'])) {
        date_default_timezone_set($modSettings['default_timezone']);
    }
    // Check the load averages?
    if (!empty($modSettings['loadavg_enable'])) {
        if (($modSettings['load_average'] = cache_get_data('loadavg', 90)) == null) {
            $modSettings['load_average'] = detectServerLoad();
            cache_put_data('loadavg', $modSettings['load_average'], 90);
        }
        if ($modSettings['load_average'] !== false) {
            call_integration_hook('integrate_load_average', array($modSettings['load_average']));
        }
        // Let's have at least a zero
        if (empty($modSettings['loadavg_forum']) || $modSettings['load_average'] === false) {
            $modSettings['current_load'] = 0;
        } else {
            $modSettings['current_load'] = $modSettings['load_average'];
        }
        if (!empty($modSettings['loadavg_forum']) && $modSettings['current_load'] >= $modSettings['loadavg_forum']) {
            display_loadavg_error();
        }
    } else {
        $modSettings['current_load'] = 0;
    }
    // Is post moderation alive and well?
    $modSettings['postmod_active'] = isset($modSettings['admin_features']) ? in_array('pm', explode(',', $modSettings['admin_features'])) : true;
    // Here to justify the name of this function. :P
    // It should be added to the install and upgrade scripts.
    // But since the convertors need to be updated also. This is easier.
    if (empty($modSettings['currentAttachmentUploadDir'])) {
        updateSettings(array('attachmentUploadDir' => serialize(array(1 => $modSettings['attachmentUploadDir'])), 'currentAttachmentUploadDir' => 1));
    }
    // Integration is cool.
    if (defined('ELK_INTEGRATION_SETTINGS')) {
        $integration_settings = unserialize(ELK_INTEGRATION_SETTINGS);
        foreach ($integration_settings as $hook => $function) {
            add_integration_function($hook, $function);
        }
    }
    // Any files to pre include?
    call_integration_include_hook('integrate_pre_include');
    // Call pre load integration functions.
    call_integration_hook('integrate_pre_load');
}
コード例 #2
0
ファイル: Load.php プロジェクト: Glyph13/SMF2.1
/**
 * Load the $modSettings array.
 *
 * @todo okay question of the day: why a function for loading settings is called reloadSettings()
 *
 * @global array $modSettings is a giant array of all of the forum-wide settings and statistics.
 */
function reloadSettings()
{
    global $modSettings, $boarddir, $smcFunc, $txt, $db_character_set, $context, $sourcedir;
    // Most database systems have not set UTF-8 as their default input charset.
    if (!empty($db_character_set)) {
        $smcFunc['db_query']('set_character_set', '
			SET NAMES ' . $db_character_set, array());
    }
    // Try to load it from the cache first; it'll never get cached if the setting is off.
    if (($modSettings = cache_get_data('modSettings', 90)) == null) {
        $request = $smcFunc['db_query']('', '
			SELECT variable, value
			FROM {db_prefix}settings', array());
        $modSettings = array();
        if (!$request) {
            display_db_error();
        }
        while ($row = $smcFunc['db_fetch_row']($request)) {
            $modSettings[$row[0]] = $row[1];
        }
        $smcFunc['db_free_result']($request);
        // Do a few things to protect against missing settings or settings with invalid values...
        if (empty($modSettings['defaultMaxTopics']) || $modSettings['defaultMaxTopics'] <= 0 || $modSettings['defaultMaxTopics'] > 999) {
            $modSettings['defaultMaxTopics'] = 20;
        }
        if (empty($modSettings['defaultMaxMessages']) || $modSettings['defaultMaxMessages'] <= 0 || $modSettings['defaultMaxMessages'] > 999) {
            $modSettings['defaultMaxMessages'] = 15;
        }
        if (empty($modSettings['defaultMaxMembers']) || $modSettings['defaultMaxMembers'] <= 0 || $modSettings['defaultMaxMembers'] > 999) {
            $modSettings['defaultMaxMembers'] = 30;
        }
        if (!empty($modSettings['cache_enable'])) {
            cache_put_data('modSettings', $modSettings, 90);
        }
    }
    // UTF-8 in regular expressions is unsupported on PHP(win) versions < 4.2.3.
    $utf8 = (empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']) === 'UTF-8';
    // Set a list of common functions.
    $ent_list = empty($modSettings['disableEntityCheck']) ? '&(#\\d{1,7}|quot|amp|lt|gt|nbsp);' : '&(#021|quot|amp|lt|gt|nbsp);';
    $ent_check = empty($modSettings['disableEntityCheck']) ? array('preg_replace(\'~(&#(\\d{1,7}|x[0-9a-fA-F]{1,6});)~e\', \'$smcFunc[\\\'entity_fix\\\'](\\\'\\2\\\')\', ', ')') : array('', '');
    // Preg_replace can handle complex characters only for higher PHP versions.
    $space_chars = $utf8 ? '\\x{A0}\\x{AD}\\x{2000}-\\x{200F}\\x{201F}\\x{202F}\\x{3000}\\x{FEFF}' : '\\x00-\\x08\\x0B\\x0C\\x0E-\\x19\\xA0';
    // global array of anonymous helper functions, used mosly to properly handle multi byte strings
    $smcFunc += array('entity_fix' => create_function('$string', '
			$num = $string[0] === \'x\' ? hexdec(substr($string, 1)) : (int) $string;
			return $num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num == 0x202E ? \'\' : \'&#\' . $num . \';\';'), 'htmlspecialchars' => create_function('$string, $quote_style = ENT_COMPAT, $charset = \'ISO-8859-1\'', '
			global $smcFunc;
			return ' . strtr($ent_check[0], array('&' => '&amp;')) . 'htmlspecialchars($string, $quote_style, ' . ($utf8 ? '\'UTF-8\'' : '$charset') . ')' . $ent_check[1] . ';'), 'htmltrim' => create_function('$string', '
			global $smcFunc;
			return preg_replace(\'~^(?:[ \\t\\n\\r\\x0B\\x00' . $space_chars . ']|&nbsp;)+|(?:[ \\t\\n\\r\\x0B\\x00' . $space_chars . ']|&nbsp;)+$~' . ($utf8 ? 'u' : '') . '\', \'\', ' . implode('$string', $ent_check) . ');'), 'strlen' => create_function('$string', '
			global $smcFunc;
			return strlen(preg_replace(\'~' . $ent_list . ($utf8 ? '|.~u' : '~') . '\', \'_\', ' . implode('$string', $ent_check) . '));'), 'strpos' => create_function('$haystack, $needle, $offset = 0', '
			global $smcFunc;
			$haystack_arr = preg_split(\'~(&#' . (empty($modSettings['disableEntityCheck']) ? '\\d{1,7}' : '021') . ';|&quot;|&amp;|&lt;|&gt;|&nbsp;|.)~' . ($utf8 ? 'u' : '') . '\', ' . implode('$haystack', $ent_check) . ', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
			$haystack_size = count($haystack_arr);
			if (strlen($needle) === 1)
			{
				$result = array_search($needle, array_slice($haystack_arr, $offset));
				return is_int($result) ? $result + $offset : false;
			}
			else
			{
				$needle_arr = preg_split(\'~(&#' . (empty($modSettings['disableEntityCheck']) ? '\\d{1,7}' : '021') . ';|&quot;|&amp;|&lt;|&gt;|&nbsp;|.)~' . ($utf8 ? 'u' : '') . '\',  ' . implode('$needle', $ent_check) . ', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
				$needle_size = count($needle_arr);

				$result = array_search($needle_arr[0], array_slice($haystack_arr, $offset));
				while ((int) $result === $result)
				{
					$offset += $result;
					if (array_slice($haystack_arr, $offset, $needle_size) === $needle_arr)
						return $offset;
					$result = array_search($needle_arr[0], array_slice($haystack_arr, ++$offset));
				}
				return false;
			}'), 'substr' => create_function('$string, $start, $length = null', '
			global $smcFunc;
			$ent_arr = preg_split(\'~(&#' . (empty($modSettings['disableEntityCheck']) ? '\\d{1,7}' : '021') . ';|&quot;|&amp;|&lt;|&gt;|&nbsp;|.)~' . ($utf8 ? 'u' : '') . '\', ' . implode('$string', $ent_check) . ', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
			return $length === null ? implode(\'\', array_slice($ent_arr, $start)) : implode(\'\', array_slice($ent_arr, $start, $length));'), 'strtolower' => $utf8 ? function_exists('mb_strtolower') ? create_function('$string', '
			return mb_strtolower($string, \'UTF-8\');') : create_function('$string', '
			global $sourcedir;
			require_once($sourcedir . \'/Subs-Charset.php\');
			return utf8_strtolower($string);') : 'strtolower', 'strtoupper' => $utf8 ? function_exists('mb_strtoupper') ? create_function('$string', '
			return mb_strtoupper($string, \'UTF-8\');') : create_function('$string', '
			global $sourcedir;
			require_once($sourcedir . \'/Subs-Charset.php\');
			return utf8_strtoupper($string);') : 'strtoupper', 'truncate' => create_function('$string, $length', (empty($modSettings['disableEntityCheck']) ? '
			global $smcFunc;
			$string = ' . implode('$string', $ent_check) . ';' : '') . '
			preg_match(\'~^(' . $ent_list . '|.){\' . $smcFunc[\'strlen\'](substr($string, 0, $length)) . \'}~' . ($utf8 ? 'u' : '') . '\', $string, $matches);
			$string = $matches[0];
			while (strlen($string) > $length)
				$string = preg_replace(\'~(?:' . $ent_list . '|.)$~' . ($utf8 ? 'u' : '') . '\', \'\', $string);
			return $string;'), 'ucfirst' => $utf8 ? create_function('$string', '
			global $smcFunc;
			return $smcFunc[\'strtoupper\']($smcFunc[\'substr\']($string, 0, 1)) . $smcFunc[\'substr\']($string, 1);') : 'ucfirst', 'ucwords' => $utf8 ? create_function('$string', '
			global $smcFunc;
			$words = preg_split(\'~([\\s\\r\\n\\t]+)~\', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
			for ($i = 0, $n = count($words); $i < $n; $i += 2)
				$words[$i] = $smcFunc[\'ucfirst\']($words[$i]);
			return implode(\'\', $words);') : 'ucwords');
    // Setting the timezone is a requirement for some functions in PHP >= 5.1.
    if (isset($modSettings['default_timezone']) && function_exists('date_default_timezone_set')) {
        date_default_timezone_set($modSettings['default_timezone']);
    }
    // Check the load averages?
    if (!empty($modSettings['loadavg_enable'])) {
        if (($modSettings['load_average'] = cache_get_data('loadavg', 90)) == null) {
            $modSettings['load_average'] = @file_get_contents('/proc/loadavg');
            if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) != 0) {
                $modSettings['load_average'] = (double) $matches[1];
            } elseif (($modSettings['load_average'] = @`uptime`) != null && preg_match('~load average[s]?: (\\d+\\.\\d+), (\\d+\\.\\d+), (\\d+\\.\\d+)~i', $modSettings['load_average'], $matches) != 0) {
                $modSettings['load_average'] = (double) $matches[1];
            } else {
                unset($modSettings['load_average']);
            }
            if (!empty($modSettings['load_average'])) {
                cache_put_data('loadavg', $modSettings['load_average'], 90);
            }
        }
        if (!empty($modSettings['load_average'])) {
            call_integration_hook('integrate_load_average', array($modSettings['load_average']));
        }
        if (!empty($modSettings['loadavg_forum']) && !empty($modSettings['load_average']) && $modSettings['load_average'] >= $modSettings['loadavg_forum']) {
            display_loadavg_error();
        }
    }
    // Is post moderation alive and well?
    $modSettings['postmod_active'] = isset($modSettings['admin_features']) ? in_array('pm', explode(',', $modSettings['admin_features'])) : true;
    // Here to justify the name of this function. :P
    // It should be added to the install and upgrade scripts.
    // But since the convertors need to be updated also. This is easier.
    if (empty($modSettings['currentAttachmentUploadDir'])) {
        updateSettings(array('attachmentUploadDir' => serialize(array(1 => $modSettings['attachmentUploadDir'])), 'currentAttachmentUploadDir' => 1));
    }
    // Integration is cool.
    if (defined('SMF_INTEGRATION_SETTINGS')) {
        $integration_settings = unserialize(SMF_INTEGRATION_SETTINGS);
        foreach ($integration_settings as $hook => $function) {
            add_integration_function($hook, $function, false);
        }
    }
    // Any files to pre include?
    if (!empty($modSettings['integrate_pre_include'])) {
        $pre_includes = explode(',', $modSettings['integrate_pre_include']);
        foreach ($pre_includes as $include) {
            $include = strtr(trim($include), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir));
            if (file_exists($include)) {
                require_once $include;
            }
        }
    }
    // Call pre load integration functions.
    call_integration_hook('integrate_pre_load');
}