Esempio n. 1
0
/**
 * Load in some server settings, time, load averages, etc.
 */
function get_linux_data()
{
    global $context;
    $context['current_time'] = strftime('%B %d, %Y, %I:%M:%S %p');
    $context['load_averages'] = detectServerLoad();
    if (empty($context['load_averages'])) {
        unset($context['load_averages']);
    }
    // How about the cpu and speed ?
    $context['cpu_info'] = array('frequency' => 'MHz');
    $cpuinfo = @implode('', @get_file_data('/proc/cpuinfo'));
    if (!empty($cpuinfo)) {
        // This only gets the first CPU!
        if (preg_match('~model name\\s+:\\s*([^\\n]+)~i', $cpuinfo, $match) != 0) {
            $context['cpu_info']['model'] = $match[1];
        }
        if (preg_match('~cpu mhz\\s+:\\s*([^\\n]+)~i', $cpuinfo, $match) != 0) {
            $context['cpu_info']['hz'] = $match[1];
        }
    } else {
        // Solaris, perhaps?
        $cpuinfo = @`psrinfo -pv 2>/dev/null`;
        if (!empty($cpuinfo)) {
            if (preg_match('~clock (\\d+)~', $cpuinfo, $match) != 0) {
                $context['cpu_info']['hz'] = $match[1];
            }
            $cpuinfo = explode("\n", $cpuinfo);
            if (isset($cpuinfo[2])) {
                $context['cpu_info']['model'] = trim($cpuinfo[2]);
            }
        } else {
            // Mac OS X?
            if (strpos(strtolower(PHP_OS), 'darwin') === 0) {
                $cpuinfo = @`sysctl machdep.cpu.brand_string 2>/dev/null`;
                if (preg_match('~machdep\\.cpu\\.brand_string:(.+)@([\\s\\d\\.]+)(.+)~', $cpuinfo, $match) != 0) {
                    $context['cpu_info']['model'] = trim($match[1]);
                    $context['cpu_info']['hz'] = trim($match[2]);
                    $context['cpu_info']['frequency'] = strtolower(trim($match[3])) == 'ghz' ? 'GHz' : 'MHz';
                }
            }
            // BSD?
            $cpuinfo = @`sysctl hw.model 2>/dev/null`;
            if (empty($context['cpu_info']['model']) && preg_match('~hw\\.model:(.+)~', $cpuinfo, $match) != 0) {
                $context['cpu_info']['model'] = trim($match[1]);
            }
            $cpuinfo = @`sysctl dev.cpu.0.freq 2>/dev/null`;
            if (empty($context['cpu_info']['hz']) && preg_match('~dev\\.cpu\\.0\\.freq:(.+)~', $cpuinfo, $match) != 0) {
                $context['cpu_info']['hz'] = trim($match[1]);
            }
        }
    }
    // Memory usage is also good to know
    $context['memory_usage'] = array();
    $meminfo = @get_file_data('/proc/meminfo');
    if (!empty($meminfo)) {
        if (preg_match('~:\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)~', $meminfo[1], $matches) != 0) {
            $context['memory_usage']['total'] = $matches[1];
            $context['memory_usage']['used'] = $matches[2];
            $context['memory_usage']['free'] = $matches[3];
            /* $context['memory_usage']['shared'] = $matches[4] / 1024;
            	  $context['memory_usage']['buffers'] = $matches[5] / 1024;
            	  $context['memory_usage']['cached'] = $matches[6] / 1024; */
        } else {
            $mem = implode('', $meminfo);
            if (preg_match('~memtotal:\\s*(\\d+ [kmgb])~i', $mem, $match) != 0) {
                $context['memory_usage']['total'] = memory_ReturnBytes($match[1]);
            }
            if (preg_match('~memfree:\\s*(\\d+ [kmgb])~i', $mem, $match) != 0) {
                $context['memory_usage']['free'] = memory_ReturnBytes($match[1]);
            }
            if (isset($context['memory_usage']['total'], $context['memory_usage']['free'])) {
                $context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
            }
            /* if (preg_match('~buffers:\s*(\d+ [kmgb])~i', $mem, $match) != 0)
            	  $context['memory_usage']['buffers'] = memory_ReturnBytes($match[1]);
            	  if (preg_match('~cached:\s*(\d+ [kmgb])~i', $mem, $match) != 0)
            	  $context['memory_usage']['cached'] = memory_ReturnBytes($match[1]); */
            if (preg_match('~swaptotal:\\s*(\\d+ [kmgb])~i', $mem, $match) != 0) {
                $context['memory_usage']['swap_total'] = memory_ReturnBytes($match[1]);
            }
            if (preg_match('~swapfree:\\s*(\\d+ [kmgb])~i', $mem, $match) != 0) {
                $context['memory_usage']['swap_free'] = memory_ReturnBytes($match[1]);
            }
            if (isset($context['memory_usage']['swap_total'], $context['memory_usage']['swap_free'])) {
                $context['memory_usage']['swap_used'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_free'];
            }
        }
        if (preg_match('~:\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)~', $meminfo[2], $matches) != 0) {
            $context['memory_usage']['swap_total'] = $matches[1];
            $context['memory_usage']['swap_used'] = $matches[2];
            $context['memory_usage']['swap_free'] = $matches[3];
        }
        $meminfo = false;
    } elseif (empty($context['memory_usage'])) {
        $meminfo = explode("\n", @`free -k 2>/dev/null | awk '{ if (\$2 * 1 > 0) print \$2, \$3, \$4; }'`);
        if (!empty($meminfo[0])) {
            $meminfo[0] = explode(' ', $meminfo[0]);
            $meminfo[1] = explode(' ', $meminfo[1]);
            $context['memory_usage']['total'] = $meminfo[0][0];
            $context['memory_usage']['used'] = $meminfo[0][1];
            $context['memory_usage']['free'] = $meminfo[0][2];
            $context['memory_usage']['swap_total'] = $meminfo[1][0];
            $context['memory_usage']['swap_used'] = $meminfo[1][1];
            $context['memory_usage']['swap_free'] = $meminfo[1][2];
        }
    }
    // Solaris, Mac OS X, or FreeBSD?
    if (empty($context['memory_usage'])) {
        // Well, Solaris will have kstat.
        $meminfo = explode("\n", @`kstat -p unix:0:system_pages:physmem unix:0:system_pages:freemem 2>/dev/null | awk '{ print \$2 }'`);
        if (!empty($meminfo[0])) {
            $pagesize = `/usr/bin/pagesize`;
            $context['memory_usage']['total'] = memory_ReturnBytes($meminfo[0] * $pagesize);
            $context['memory_usage']['free'] = memory_ReturnBytes($meminfo[1] * $pagesize);
            $context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
            $meminfo = explode("\n", @`swap -l 2>/dev/null | awk '{ if (\$4 * 1 > 0) print \$4, \$5; }'`);
            $context['memory_usage']['swap_total'] = 0;
            $context['memory_usage']['swap_free'] = 0;
            foreach ($meminfo as $memline) {
                $memline = explode(' ', $memline);
                if (empty($memline[0])) {
                    continue;
                }
                $context['memory_usage']['swap_total'] += $memline[0];
                $context['memory_usage']['swap_free'] += $memline[1];
            }
            $context['memory_usage']['swap_used'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_free'];
        }
    }
    if (empty($context['memory_usage'])) {
        // FreeBSD should have hw.physmem.
        $meminfo = @`sysctl hw.physmem 2>/dev/null`;
        if (strpos(strtolower(PHP_OS), 'darwin') !== 0 && !empty($meminfo) && preg_match('~hw\\.physmem: (\\d+)~i', $meminfo, $match) != 0) {
            $context['memory_usage']['total'] = memory_ReturnBytes($match[1]);
            $meminfo = @`sysctl hw.pagesize vm.stats.vm.v_free_count 2>/dev/null`;
            if (!empty($meminfo) && preg_match('~hw\\.pagesize: (\\d+)~i', $meminfo, $match1) != 0 && preg_match('~vm\\.stats\\.vm\\.v_free_count: (\\d+)~i', $meminfo, $match2) != 0) {
                $context['memory_usage']['free'] = $match1[1] * $match2[1];
                $context['memory_usage']['used'] = $context['memory_usage']['total'] - $context['memory_usage']['free'];
            }
            $meminfo = @`swapinfo 2>/dev/null | awk '{ print \$2, \$4; }'`;
            if (preg_match('~(\\d+) (\\d+)~', $meminfo, $match) != 0) {
                $context['memory_usage']['swap_total'] = $match[1];
                $context['memory_usage']['swap_free'] = $match[2];
                $context['memory_usage']['swap_used'] = $context['memory_usage']['swap_total'] - $context['memory_usage']['swap_free'];
            }
        } else {
            $meminfo = @`top -l1 2>/dev/null`;
            if (!empty($meminfo) && preg_match('~PhysMem:\\s+(?:.+?)\\s+([\\d\\.]+\\w) used,\\s+([\\d\\.]+\\w) free~', $meminfo, $match) != 0) {
                $context['memory_usage']['used'] = memory_ReturnBytes($match[1]);
                $context['memory_usage']['free'] = memory_ReturnBytes($match[2]);
                $context['memory_usage']['total'] = $context['memory_usage']['used'] + $context['memory_usage']['free'];
            }
        }
    }
    // Can we obtain an uptime?
    $lastreboot = @get_file_data('/proc/uptime');
    if (!empty($lastreboot)) {
        $lastreboot = explode(' ', $lastreboot[0]);
        $context['lastserverreboot'] = time() - trim($lastreboot[0]);
    }
    // Mac OS X and others?
    if (empty($context['lastserverreboot'])) {
        $lastreboot = @`sysctl kern.boottime 2>/dev/null`;
        if (!empty($lastreboot) && preg_match('~kern\\.boottime: { sec\\s+=\\s+(\\d+),~', $lastreboot, $match) != 0) {
            $context['lastserverreboot'] = $match[1];
        }
    }
    // What OS are we running ?
    $context['operating_system']['type'] = 'unix';
    $check_release = array('centos', 'fedora', 'gentoo', 'redhat', 'slackware', 'yellowdog');
    foreach ($check_release as $os) {
        if (@file_exists('/etc/' . $os . '-release')) {
            $context['operating_system']['name'] = implode('', get_file_data('/etc/' . $os . '-release'));
        }
    }
    if (isset($context['operating_system']['name'])) {
        true;
    } elseif (@file_exists('/etc/SuSE-release')) {
        $temp = get_file_data('/etc/SuSE-release');
        $context['operating_system']['name'] = trim($temp[0]);
    } elseif (@file_exists('/etc/release')) {
        $temp = get_file_data('/etc/release');
        $context['operating_system']['name'] = trim($temp[0]);
    } elseif (@file_exists('/etc/os-release')) {
        $temp = get_file_data('/etc/os-release');
        foreach ($temp as $info) {
            if (strpos($info, 'PRETTY_NAME=') !== false) {
                $context['operating_system']['name'] = trim(trim(str_replace('PRETTY_NAME=', '', $info)), '"');
                break;
            }
        }
    } elseif (@file_exists('/etc/debian_version')) {
        $context['operating_system']['name'] = 'Debian ' . implode('', get_file_data('/etc/debian_version'));
    }
    // Nothing found?
    if (empty($context['operating_system']['name'])) {
        $context['operating_system']['name'] = trim(@`uname -s -r 2>/dev/null`);
    }
    // How many processes are running?
    $context['running_processes'] = array();
    $processes = @`ps auxc 2>/dev/null | awk '{ print \$2, \$3, \$4, \$8, \$11, \$12 }'`;
    if (empty($processes)) {
        $processes = @`ps aux 2>/dev/null | awk '{ print \$2, \$3, \$4, \$8, \$11, \$12 }'`;
    }
    // Maybe it's Solaris?
    if (empty($processes)) {
        $processes = @`ps -eo pid,pcpu,pmem,s,fname 2>/dev/null | awk '{ print \$1, \$2, \$3, \$4, \$5, \$6 }'`;
    }
    // Okay, how about QNX?
    if (empty($processes)) {
        $processes = @`ps -eo pid,pcpu,comm 2>/dev/null | awk '{ print \$1, \$2, 0, "", \$5, \$6 }'`;
    }
    // If we found them, lets process them to something useful
    if (!empty($processes)) {
        $processes = explode("\n", $processes);
        $context['num_zombie_processes'] = 0;
        $context['num_sleeping_processes'] = 0;
        $context['num_running_processes'] = 0;
        for ($i = 1, $n = count($processes) - 1; $i < $n; $i++) {
            $proc = explode(' ', $processes[$i], 5);
            $additional = @implode('', @get_file_data('/proc/' . $proc[0] . '/statm'));
            if ($proc[4][0] != '[' && strpos($proc[4], ' ') !== false) {
                $proc[4] = strtok($proc[4], ' ');
            }
            $context['running_processes'][$proc[0]] = array('id' => $proc[0], 'cpu' => $proc[1], 'mem' => $proc[2], 'title' => $proc[4]);
            if (strpos($proc[3], 'Z') !== false) {
                $context['num_zombie_processes']++;
            } elseif (strpos($proc[3], 'S') !== false) {
                $context['num_sleeping_processes']++;
            } else {
                $context['num_running_processes']++;
            }
            if (!empty($additional)) {
                $additional = explode(' ', $additional);
                $context['running_processes'][$proc[0]]['mem_usage'] = $additional[0];
            }
        }
        $context['top_memory_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
        $context['top_cpu_usage'] = array('(other)' => array('name' => '(other)', 'percent' => 0, 'number' => 0));
        foreach ($context['running_processes'] as $proc) {
            $id = basename($proc['title']);
            if (!isset($context['top_memory_usage'][$id])) {
                $context['top_memory_usage'][$id] = array('name' => $id, 'percent' => $proc['mem'], 'number' => 1);
            } else {
                $context['top_memory_usage'][$id]['percent'] += $proc['mem'];
                $context['top_memory_usage'][$id]['number']++;
            }
            if (!isset($context['top_cpu_usage'][$id])) {
                $context['top_cpu_usage'][$id] = array('name' => $id, 'percent' => $proc['cpu'], 'number' => 1);
            } else {
                $context['top_cpu_usage'][$id]['percent'] += $proc['cpu'];
                $context['top_cpu_usage'][$id]['number']++;
            }
        }
        // @todo shared memory?
        foreach ($context['top_memory_usage'] as $proc) {
            if ($proc['percent'] >= 1 || $proc['name'] == '(other)') {
                continue;
            }
            unset($context['top_memory_usage'][$proc['name']]);
            $context['top_memory_usage']['(other)']['percent'] += $proc['percent'];
            $context['top_memory_usage']['(other)']['number']++;
        }
        foreach ($context['top_cpu_usage'] as $proc) {
            if ($proc['percent'] >= 0.6 || $proc['name'] == '(other)') {
                continue;
            }
            unset($context['top_cpu_usage'][$proc['name']]);
            $context['top_cpu_usage']['(other)']['percent'] += $proc['percent'];
            $context['top_cpu_usage']['(other)']['number']++;
        }
    }
}
Esempio n. 2
0
/**
 * Get a list of versions that are currently installed on the server.
 *
 * @package Admin
 * @param string[] $checkFor
 */
function getServerVersions($checkFor)
{
    global $txt, $memcached, $modSettings;
    $db = database();
    loadLanguage('Admin');
    $versions = array();
    // Is GD available?  If it is, we should show version information for it too.
    if (in_array('gd', $checkFor) && function_exists('gd_info')) {
        $temp = gd_info();
        $versions['gd'] = array('title' => $txt['support_versions_gd'], 'version' => $temp['GD Version']);
    }
    // Why not have a look at ImageMagick? If it is, we should show version information for it too.
    if (in_array('imagick', $checkFor) && class_exists('Imagick')) {
        $temp = new Imagick();
        $temp2 = $temp->getVersion();
        $versions['imagick'] = array('title' => $txt['support_versions_imagick'], 'version' => $temp2['versionString']);
    }
    // Now lets check for the Database.
    if (in_array('db_server', $checkFor)) {
        $conn = $db->connection();
        if (empty($conn)) {
            trigger_error('getServerVersions(): you need to be connected to the database in order to get its server version', E_USER_NOTICE);
        } else {
            $versions['db_server'] = array('title' => sprintf($txt['support_versions_db'], $db->db_title()), 'version' => '');
            $versions['db_server']['version'] = $db->db_server_version();
        }
    }
    // If we're using memcache we need the server info.
    if (empty($memcached) && function_exists('memcache_get') && isset($modSettings['cache_memcached']) && trim($modSettings['cache_memcached']) != '') {
        require_once SUBSDIR . '/Cache.subs.php';
        get_memcached_server();
    } else {
        if (($key = array_search('memcache', $checkFor)) !== false) {
            unset($checkFor[$key]);
        }
    }
    // Check to see if we have any accelerators installed...
    if (in_array('mmcache', $checkFor) && defined('MMCACHE_VERSION')) {
        $versions['mmcache'] = array('title' => 'Turck MMCache', 'version' => MMCACHE_VERSION);
    }
    if (in_array('eaccelerator', $checkFor) && defined('EACCELERATOR_VERSION')) {
        $versions['eaccelerator'] = array('title' => 'eAccelerator', 'version' => EACCELERATOR_VERSION);
    }
    if (in_array('zend', $checkFor) && function_exists('zend_shm_cache_store')) {
        $versions['zend'] = array('title' => 'Zend SHM Accelerator', 'version' => zend_version());
    }
    if (in_array('apc', $checkFor) && extension_loaded('apc')) {
        $versions['apc'] = array('title' => 'Alternative PHP Cache', 'version' => phpversion('apc'));
    }
    if (in_array('memcache', $checkFor) && function_exists('memcache_set')) {
        $versions['memcache'] = array('title' => 'Memcached', 'version' => empty($memcached) ? '???' : memcache_get_version($memcached));
    }
    if (in_array('xcache', $checkFor) && function_exists('xcache_set')) {
        $versions['xcache'] = array('title' => 'XCache', 'version' => XCACHE_VERSION);
    }
    if (in_array('opcache', $checkFor) && extension_loaded('Zend OPcache')) {
        $opcache_config = @opcache_get_configuration();
        if (!empty($opcache_config['directives']['opcache.enable'])) {
            $versions['opcache'] = array('title' => $opcache_config['version']['opcache_product_name'], 'version' => $opcache_config['version']['version']);
        }
    }
    // PHP Version
    if (in_array('php', $checkFor)) {
        $versions['php'] = array('title' => 'PHP', 'version' => PHP_VERSION . ' (' . php_sapi_name() . ')', 'more' => '?action=admin;area=serversettings;sa=phpinfo');
    }
    // Server info
    if (in_array('server', $checkFor)) {
        $req = request();
        $versions['server'] = array('title' => $txt['support_versions_server'], 'version' => $req->server_software());
        // Compute some system info, if we can
        $versions['server_name'] = array('title' => $txt['support_versions'], 'version' => php_uname());
        $loading = detectServerLoad();
        if ($loading !== false) {
            $versions['server_load'] = array('title' => $txt['load_balancing_settings'], 'version' => $loading);
        }
    }
    return $versions;
}
Esempio n. 3
0
/**
 * This function shows the debug information tracked when $db_show_debug = true
 * in Settings.php
 */
function displayDebug()
{
    global $context, $scripturl, $modSettings;
    global $db_cache, $db_count, $db_show_debug, $cache_count, $cache_hits, $txt, $rusage_start;
    // Add to Settings.php if you want to show the debugging information.
    if (!isset($db_show_debug) || $db_show_debug !== true || isset($_GET['action']) && $_GET['action'] == 'viewquery' || isset($_GET['api'])) {
        return;
    }
    if (empty($_SESSION['view_queries'])) {
        $_SESSION['view_queries'] = 0;
    }
    if (empty($context['debug']['language_files'])) {
        $context['debug']['language_files'] = array();
    }
    if (empty($context['debug']['sheets'])) {
        $context['debug']['sheets'] = array();
    }
    $files = get_included_files();
    $total_size = 0;
    for ($i = 0, $n = count($files); $i < $n; $i++) {
        if (file_exists($files[$i])) {
            $total_size += filesize($files[$i]);
        }
        $files[$i] = strtr($files[$i], array(BOARDDIR => '.'));
    }
    $warnings = 0;
    if (!empty($db_cache)) {
        foreach ($db_cache as $q => $qq) {
            if (!empty($qq['w'])) {
                $warnings += count($qq['w']);
            }
        }
        $_SESSION['debug'] =& $db_cache;
    }
    // Gotta have valid HTML ;).
    $temp = ob_get_contents();
    ob_clean();
    // Compute some system info, if we can
    $context['system'] = php_uname();
    $context['server_load'] = detectServerLoad();
    $context['memory_usage'] = round(memory_get_peak_usage() / 1024 / 1024, 2) . 'MB';
    // getrusage() information is CPU time, not wall clock time like microtime, *nix only
    if (function_exists('getrusage')) {
        $rusage_end = getrusage();
        $context['user_time'] = $rusage_end['ru_utime.tv_sec'] - $rusage_start['ru_utime.tv_sec'] + $rusage_end['ru_utime.tv_usec'] / 1000000;
        $context['system_time'] = $rusage_end['ru_stime.tv_sec'] - $rusage_start['ru_stime.tv_sec'] + $rusage_end['ru_stime.tv_usec'] / 1000000;
    }
    echo preg_replace('~</body>\\s*</html>~', '', $temp), '
	<div id="debug_logging_wrapper">
		<div id="debug_logging" class="smalltext">
			', $txt['debug_system_type'], $context['system'], '<br />
			', !empty($context['server_load']) ? $txt['debug_server_load'] . $context['server_load'] . '<br />' : '', '
			', !empty($context['memory_usage']) ? $txt['debug_script_mem_load'] . $context['memory_usage'] . '<br />' : '', '
			', !empty($context['user_time']) ? $txt['debug_script_cpu_load'] . $context['user_time'] . ' / ' . $context['system_time'] . '<br />' : '', '
			', $txt['debug_browser'], $context['browser_body_id'], ' <em>(', implode('</em>, <em>', array_reverse(array_keys($context['browser'], true))), ')</em><br />
			', $txt['debug_templates'], count($context['debug']['templates']), ': <em>', implode('</em>, <em>', $context['debug']['templates']), '</em>.<br />
			', $txt['debug_subtemplates'], count($context['debug']['sub_templates']), ': <em>', implode('</em>, <em>', $context['debug']['sub_templates']), '</em>.<br />
			', $txt['debug_language_files'], count($context['debug']['language_files']), ': <em>', implode('</em>, <em>', $context['debug']['language_files']), '</em>.<br />
			', $txt['debug_stylesheets'], count($context['debug']['sheets']), ': <em>', implode('</em>, <em>', $context['debug']['sheets']), '</em>.<br />
			', $txt['debug_javascript'], !empty($context['debug']['javascript']) ? count($context['debug']['javascript']) . ': <em>' . implode('</em>, <em>', $context['debug']['javascript']) . '</em>.<br />' : '', '
			', $txt['debug_hooks'], empty($context['debug']['hooks']) ? 0 : count($context['debug']['hooks']) . ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_hooks\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_hooks" style="display: none;"><em>' . implode('</em>, <em>', $context['debug']['hooks']), '</em></span>)', '<br />
			', $txt['debug_files_included'], count($files), ' - ', round($total_size / 1024), $txt['debug_kb'], ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_include_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_include_info" style="display: none;"><em>', implode('</em>, <em>', $files), '</em></span>)<br />';
    // What tokens are active?
    if (isset($_SESSION['token'])) {
        $token_list = array_keys($_SESSION['token']);
        echo '
			', $txt['debug_tokens'] . '<em>' . implode(',</em> <em>', $token_list), '</em>.<br />';
    }
    // If the cache is on, how successful was it?
    if (!empty($modSettings['cache_enable']) && !empty($cache_hits)) {
        $entries = array();
        $total_t = 0;
        $total_s = 0;
        foreach ($cache_hits as $cache_hit) {
            $entries[] = $cache_hit['d'] . ' ' . $cache_hit['k'] . ': ' . sprintf($txt['debug_cache_seconds_bytes'], comma_format($cache_hit['t'], 5), $cache_hit['s']);
            $total_t += $cache_hit['t'];
            $total_s += $cache_hit['s'];
        }
        echo '
			', $txt['debug_cache_hits'], $cache_count, ': ', sprintf($txt['debug_cache_seconds_bytes_total'], comma_format($total_t, 5), comma_format($total_s)), ' (<a href="javascript:void(0);" onclick="document.getElementById(\'debug_cache_info\').style.display = \'inline\'; this.style.display = \'none\'; return false;">', $txt['debug_show'], '</a><span id="debug_cache_info" style="display: none;"><em>', implode('</em>, <em>', $entries), '</em></span>)<br />';
    }
    // Want to see the querys in a new windows?
    echo '
			<a href="', $scripturl, '?action=viewquery" target="_blank" class="new_win">', $warnings == 0 ? sprintf($txt['debug_queries_used'], (int) $db_count) : sprintf($txt['debug_queries_used_and_warnings'], (int) $db_count, $warnings), '</a><br />';
    if ($_SESSION['view_queries'] == 1 && !empty($db_cache)) {
        foreach ($db_cache as $q => $qq) {
            $is_select = strpos(trim($qq['q']), 'SELECT') === 0 || preg_match('~^INSERT(?: IGNORE)? INTO \\w+(?:\\s+\\([^)]+\\))?\\s+SELECT .+$~s', trim($qq['q'])) != 0;
            // Temporary tables created in earlier queries are not explainable.
            if ($is_select) {
                foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp) {
                    if (strpos(trim($qq['q']), $tmp) !== false) {
                        $is_select = false;
                        break;
                    }
                }
            } elseif (preg_match('~^CREATE TEMPORARY TABLE .+?SELECT .+$~s', trim($qq['q'])) != 0) {
                $is_select = true;
            }
            // Make the filenames look a bit better.
            if (isset($qq['f'])) {
                $qq['f'] = preg_replace('~^' . preg_quote(BOARDDIR, '~') . '~', '...', $qq['f']);
            }
            echo '
	<strong>', $is_select ? '<a href="' . $scripturl . '?action=viewquery;qq=' . ($q + 1) . '#qq' . $q . '" target="_blank" class="new_win" style="text-decoration: none;">' : '', nl2br(str_replace("\t", '&nbsp;&nbsp;&nbsp;', htmlspecialchars(ltrim($qq['q'], "\n\r"), ENT_COMPAT, 'UTF-8'))) . ($is_select ? '</a></strong>' : '</strong>') . '<br />
	&nbsp;&nbsp;&nbsp;';
            if (!empty($qq['f']) && !empty($qq['l'])) {
                echo sprintf($txt['debug_query_in_line'], $qq['f'], $qq['l']);
            }
            if (isset($qq['s'], $qq['t']) && isset($txt['debug_query_which_took_at'])) {
                echo sprintf($txt['debug_query_which_took_at'], round($qq['t'], 8), round($qq['s'], 8)) . '<br />';
            } elseif (isset($qq['t'])) {
                echo sprintf($txt['debug_query_which_took'], round($qq['t'], 8)) . '<br />';
            }
            echo '
	<br />';
        }
    }
    // Or show/hide the querys in line with all of this data
    echo '
			<a href="' . $scripturl . '?action=viewquery;sa=hide">', $txt['debug_' . (empty($_SESSION['view_queries']) ? 'show' : 'hide') . '_queries'], '</a>
		</div>
	</div>
</body></html>';
}
Esempio n. 4
0
/**
 * 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');
}
Esempio n. 5
0
 /**
  * This little function returns load balancing settings.
  */
 private function _balancingSettings()
 {
     global $txt, $modSettings, $context;
     // Initialize settings for the form to show, disabled by default.
     $disabled = true;
     $context['settings_message'] = $txt['loadavg_disabled_conf'];
     // Don't say you're using that win-thing, no cookies for you :P
     if (stripos(PHP_OS, 'win') === 0) {
         $context['settings_message'] = $txt['loadavg_disabled_windows'];
     } else {
         $modSettings['load_average'] = detectServerLoad();
         if ($modSettings['load_average'] !== false) {
             $disabled = false;
             $context['settings_message'] = sprintf($txt['loadavg_warning'], $modSettings['load_average']);
         }
     }
     // Start with a simple checkbox.
     $config_vars = array(array('check', 'loadavg_enable', 'disabled' => $disabled));
     // Set the default values for each option.
     $default_values = array('loadavg_auto_opt' => '1.0', 'loadavg_search' => '2.5', 'loadavg_allunread' => '2.0', 'loadavg_unreadreplies' => '3.5', 'loadavg_show_posts' => '2.0', 'loadavg_userstats' => '10.0', 'loadavg_bbc' => '30.0', 'loadavg_forum' => '40.0');
     // Loop through the settings.
     foreach ($default_values as $name => $value) {
         // Use the default value if the setting isn't set yet.
         $value = !isset($modSettings[$name]) ? $value : $modSettings[$name];
         $config_vars[] = array('text', $name, 'value' => $value, 'disabled' => $disabled);
     }
     // Notify the integration that we're preparing to mess with balancing settings...
     call_integration_hook('integrate_modify_loadavg_settings', array(&$config_vars));
     return $config_vars;
 }