Exemplo n.º 1
0
function load_site_stats()
{
    list($hour, $minute) = explode('-', date('H-i', time() - 3540));
    $minute_offset = ($hour * MINUTES_PER_HOUR + $minute) * RECORD_SIZE_STATS;
    $end_of_file = (HOURS_PER_DAY + MINUTES_PER_DAY) * RECORD_SIZE_STATS;
    $stats = array();
    $rt24 = array_fill(1, STATS_PER_RECORD, 0);
    $rt60 = array_fill(1, STATS_PER_RECORD, 0);
    $trades = array_merge(get_trades(), get_system_trades());
    foreach ($trades as $trade) {
        $file = get_trade_stats_dir($trade['domain']) . '/' . $trade['domain'];
        $fp = fopen($file, 'r');
        for ($i = 0; $i < HOURS_PER_DAY; $i++) {
            $r = unpack('L' . STATS_PER_RECORD, fread($fp, RECORD_SIZE_STATS));
            foreach ($r as $j => $k) {
                $rt24[$j] += $k;
            }
        }
        fseek($fp, $minute_offset, SEEK_CUR);
        for ($i = 0; $i < MINUTES_PER_HOUR; $i++) {
            $minute++;
            $r = unpack('L' . STATS_PER_RECORD, fread($fp, RECORD_SIZE_STATS));
            foreach ($r as $j => $k) {
                $rt60[$j] += $k;
            }
            // Wrap around midnight with a seek
            if (ftell($fp) == $end_of_file) {
                fseek($fp, HOURS_PER_DAY * RECORD_SIZE_STATS, SEEK_SET);
            }
        }
        fclose($fp);
    }
    return new StatsOverall($trade, $rt60, $rt24);
}
Exemplo n.º 2
0
function build_toplist($toplist)
{
    global $g_stats, $g_time, $compiler, $C;
    require_once 'dirdb.php';
    require_once 'compiler.php';
    require_once 'template.php';
    require_once 'stats.php';
    if (empty($toplist)) {
        return;
    }
    // Check that source file exists and is readable
    if ($toplist['source'] == TOPLIST_SOURCE_FILE) {
        if (!is_file($toplist['infile']) || !is_readable($toplist['infile'])) {
            return log_toplist_error("Input file '{$toplist['infile']}' does not exist or is not readable");
        }
        $toplist['template'] = file_get_contents($toplist['infile']);
    }
    // Check that the destination file exists and is writeable, or the directory exists and is writeable
    if (!file_exists($toplist['outfile'])) {
        $directory = dirname($toplist['outfile']);
        if (!is_dir($directory) || !is_writeable($directory)) {
            return log_toplist_error("Output directory '{$directory}' does not exist or is not writeable");
        }
    } else {
        if (!is_writeable($toplist['outfile'])) {
            return log_toplist_error("Output file '{$toplist['outfile']}' is not writeable");
        }
    }
    // Compile and check for errors
    if (($compiled = $compiler->Compile($toplist['template'])) === false) {
        return log_toplist_error("Toplist template for '{$toplist['outfile']}' contains errors");
    }
    // Load and cache overall stats
    if (!isset($g_stats)) {
        $_REQUEST['status'] = STATUS_ACTIVE;
        $g_stats = load_overall_stats(DIR_TRADE_STATS, get_trades(), TRUE);
    }
    // Get time
    if (!isset($g_time)) {
        $g_time = time();
    }
    // Prepare categories and groups
    $toplist['categories'] = empty($toplist['categories']) ? null : explode(',', $toplist['categories']);
    $toplist['groups'] = empty($toplist['groups']) ? null : explode(',', $toplist['groups']);
    // Prepare requirements & sources
    $toplist['trade_sources'] = unserialize($toplist['trade_sources']);
    $toplist['req_field'] = unserialize($toplist['req_field']);
    $toplist['req_operator'] = unserialize($toplist['req_operator']);
    $toplist['req_value'] = unserialize($toplist['req_value']);
    // Sort
    $_REQUEST['sort_by'] = $toplist['sort_by'];
    $_REQUEST['trade_sources'] = $toplist['trade_sources'];
    usort($g_stats, 'cmp_overall_stats');
    $t = new Template();
    $rank = 1;
    $trades = array();
    foreach ($g_stats as $so) {
        // Allowed in toplist?
        if (!$so->trade['flag_toplist']) {
            continue;
        }
        // Check categories
        if (!empty($toplist['categories']) && count(array_intersect($toplist['categories'], explode(',', $so->trade['categories']))) == 0) {
            continue;
        }
        // Check groups
        if (!empty($toplist['groups']) && count(array_intersect($toplist['groups'], explode(',', $so->trade['groups']))) == 0) {
            continue;
        }
        // Only trades with thumbnails
        if ($toplist['flag_thumbs_only'] && $so->trade['thumbnails'] < 1) {
            continue;
        }
        // Check requirement
        if (!empty($toplist['req_field']) && !$so->ignore_requirements) {
            for ($i = 0; $i < count($toplist['req_field']); $i++) {
                $req_failed = FALSE;
                switch ($toplist['req_operator'][$i]) {
                    case '>=':
                        $req_failed = !($so->{$toplist['req_field'][$i]} >= $toplist['req_value'][$i]);
                        break;
                    case '<':
                        $req_failed = !($so->{$toplist['req_field'][$i]} < $toplist['req_value'][$i]);
                        break;
                    case '<=':
                        $req_failed = !($so->{$toplist['req_field'][$i]} <= $toplist['req_value'][$i]);
                        break;
                    case '>':
                        $req_failed = !($so->{$toplist['req_field'][$i]} > $toplist['req_value'][$i]);
                        break;
                    default:
                        $req_failed = FALSE;
                        break;
                }
                if ($req_failed) {
                    break;
                }
            }
            if ($req_failed) {
                continue;
            }
        }
        $trade = get_object_vars($so);
        $trade = array_merge($trade, $trade['trade']);
        unset($trade['trade']);
        $trades[$rank] = $trade;
        $rank++;
    }
    $t->AssignByRef('g_trades', $trades);
    $t->AssignByRef('g_config', $C);
    $t->Assign('g_timestamp', $g_time);
    $t->Assign('g_date', date($C['date_format'], $g_time));
    $t->Assign('g_datetime', date($C['date_format'] . ' ' . $C['time_format'], $g_time));
    $output = $t->Parse($compiled);
    file_write($toplist['outfile'], $output);
}