/**
* Get a cached test result
*/
function PSS_GetCacheEntry($url)
{
    $id = null;
    $cache_lock = Lock("PSS Cache");
    if (isset($cache_lock)) {
        if (is_file('./tmp/pss.cache')) {
            $cache = json_decode(file_get_contents('./tmp/pss.cache'), true);
            // delete stale cache entries
            $now = time();
            $dirty = false;
            foreach ($cache as $cache_key => &$cache_entry) {
                if ($cache_entry['expires'] < $now) {
                    $dirty = true;
                    unset($cache[$cache_key]);
                }
            }
            if ($dirty) {
                file_put_contents('./tmp/pss.cache', json_encode($cache));
            }
            $key = md5($url);
            if (array_key_exists($key, $cache) && array_key_exists('id', $cache[$key])) {
                $id = $cache[$key]['id'];
            }
        }
        Unlock($cache_lock);
    }
    return $id;
}
    if ($results[0] >= $GLOBALS["spdrefresh"]) {
        if (Lock("SPEED:{$info_hash}")) {
            @runSpeed($info_hash, $results[0]);
            Unlock("SPEED:{$info_hash}");
        }
    }
}
if ($GLOBALS["doavg"] && !$GLOBALS["heavyload"]) {
    // Once every minute or so, we run the speed update checker.
    $query = @mysql_query("SELECT UNIX_TIMESTAMP() - lastAvgCycle FROM summary WHERE info_hash=\"{$info_hash}\"");
    $results = mysql_fetch_row($query);
    if ($results[0] >= $GLOBALS["avgrefresh"]) {
        if (Lock("AVG:{$info_hash}")) {
            @quickQuery("UPDATE summary SET lastAvgCycle = UNIX_TIMESTAMP() WHERE info_hash=\"{$info_hash}\"");
            @runAvg($info_hash);
            Unlock("AVG:{$info_hash}");
        }
    }
}
/* 
 * Under heavy loads, this will lighten the load slightly... very slightly...
 */
if ($GLOBALS["heavyload"]) {
    if (mt_rand(1, 10) == 4) {
        trashCollector($info_hash, $report_interval);
    }
} else {
    trashCollector($info_hash, $report_interval);
}
/*
 * Finally, it's time to do stuff to the summary table.
/**
* For each IP/Installer pair, keep track of the last 4 checks and if they
* were within the last hour fail the request.
* 
* @param mixed $installer
*/
function CheckIp($installer)
{
    $ok = true;
    $ip = $_SERVER["REMOTE_ADDR"];
    if (isset($ip) && strlen($ip)) {
        $lock = Lock("Installers", true, 5);
        if ($lock) {
            $now = time();
            $file = "./tmp/installers.dat";
            if (gz_is_file($file)) {
                $history = json_decode(gz_file_get_contents($file), true);
            }
            if (!isset($history) || !is_array($history)) {
                $history = array();
            }
            if (isset($history[$ip])) {
                if (isset($history[$ip][$installer])) {
                    $history[$ip][$installer][] = $now;
                    if (count($history[$ip][$installer]) > 10) {
                        array_shift($history[$ip][$installer]);
                    }
                    if (isset($history[$ip]["last-{$installer}"]) && $now - $history[$ip]["last-{$installer}"] < 3600) {
                        $count = 0;
                        foreach ($history[$ip][$installer] as $time) {
                            if ($now - $time < 3600) {
                                $count++;
                            }
                        }
                        if ($count > 4) {
                            $ok = false;
                        }
                    }
                } else {
                    $history[$ip][$installer] = array($now);
                }
            } else {
                $history[$ip] = array($installer => array($now));
            }
            $history[$ip]['last'] = $now;
            if ($ok) {
                $history[$ip]["last-{$installer}"] = $now;
            }
            // prune any agents that haven't connected in 7 days
            foreach ($history as $agent => $info) {
                if ($now - $info['last'] > 604800) {
                    unset($history[$agent]);
                }
            }
            gz_file_put_contents($file, json_encode($history));
            Unlock($lock);
        }
    }
    return $ok;
}
Exemple #4
0
}
// Header is most likely already: 200 Ok
//error_log("Send length: $xmitbytes == ".strlen($xmit));
if (isset($_GET["ranges"])) {
    $myxmit = "";
    $ranges = explode(",", $_GET["ranges"]);
    foreach ($ranges as $blocks) {
        $startstop = explode("-", $blocks);
        if (!is_numeric($startstop[0]) || !is_numeric($startstop[1])) {
            reject("400 Bad Request");
        }
        if (isset($startstop[2])) {
            reject("400 Bad Request");
        }
        $start = $startstop[0];
        $stop = $startstop[1];
        if ($start > $stop) {
            reject("400 Bad Request");
        }
        $myxmit .= substr($xmit, $start, $stop - $start + 1);
    }
    header("Content-Length: " . strlen($myxmit));
    mysql_query("UPDATE " . $prefix . "speedlimit SET uploaded=uploaded+" . strlen($myxmit));
    echo $myxmit;
} else {
    mysql_query("UPDATE " . $prefix . "speedlimit SET uploaded=uploaded+{$xmitbytes}");
    header("Content-Length: {$xmitbytes}");
    echo $xmit;
}
Unlock("WebSeed--{$lockno}");
exit;
Exemple #5
0
/**
* See if we are requiring key validation and if so, enforce the restrictions
*
* @param mixed $test
* @param mixed $error
*/
function ValidateKey(&$test, &$error, $key = null)
{
    global $admin;
    // load the secret key (if there is one)
    $secret = '';
    $keys = parse_ini_file('./settings/keys.ini', true);
    if ($keys && isset($keys['server']) && isset($keys['server']['secret'])) {
        $secret = trim($keys['server']['secret']);
    }
    if (strlen($secret)) {
        // ok, we require key validation, see if they have an hmac (user with form)
        // or API key
        if (!isset($key) && isset($test['vh']) && strlen($test['vh'])) {
            // validate the hash
            $hashStr = $secret;
            $hashStr .= $_SERVER['HTTP_USER_AGENT'];
            $hashStr .= $test['owner'];
            $hashStr .= $test['vd'];
            $hmac = sha1($hashStr);
            // check the elapsed time since the hmac was issued
            $now = time();
            $origTime = strtotime($test['vd']);
            $elapsed = abs($now - $origTime);
            if ($hmac != $test['vh'] || $elapsed > 86400) {
                $error = 'Your test request could not be validated (this can happen if you leave the browser window open for over a day before submitting a test).  Please try submitting it again.';
            }
        } elseif (isset($key) || isset($test['key']) && strlen($test['key'])) {
            if (isset($test['key']) && strlen($test['key']) && !isset($key)) {
                $key = $test['key'];
            }
            // see if it was an auto-provisioned key
            if (preg_match('/^(?P<prefix>[0-9A-Za-z]+)\\.(?P<key>[0-9A-Za-z]+)$/', $key, $matches)) {
                $prefix = $matches['prefix'];
                $db = new SQLite3(__DIR__ . "/dat/{$prefix}_api_keys.db");
                $k = $db->escapeString($matches['key']);
                $info = $db->querySingle("SELECT key_limit FROM keys WHERE key='{$k}'", true);
                $db->close();
                if (isset($info) && is_array($info) && isset($info['key_limit'])) {
                    $keys[$key] = array('limit' => $info['key_limit']);
                }
            }
            // validate their API key and enforce any rate limits
            if (array_key_exists($key, $keys)) {
                if (array_key_exists('default location', $keys[$key]) && strlen($keys[$key]['default location']) && !strlen($test['location'])) {
                    $test['location'] = $keys[$key]['default location'];
                }
                if (isset($keys[$key]['priority'])) {
                    $test['priority'] = $keys[$key]['priority'];
                }
                if (isset($keys[$key]['limit'])) {
                    $limit = (int) $keys[$key]['limit'];
                    // update the number of tests they have submitted today
                    if (!is_dir('./dat')) {
                        mkdir('./dat', 0777, true);
                    }
                    $lock = Lock("API Keys");
                    if (isset($lock)) {
                        $keyfile = './dat/keys_' . gmdate('Ymd') . '.dat';
                        $usage = null;
                        if (is_file($keyfile)) {
                            $usage = json_decode(file_get_contents($keyfile), true);
                        }
                        if (!isset($usage)) {
                            $usage = array();
                        }
                        if (isset($usage[$key])) {
                            $used = (int) $usage[$key];
                        } else {
                            $used = 0;
                        }
                        $runcount = max(1, $test['runs']);
                        if (!$test['fvonly']) {
                            $runcount *= 2;
                        }
                        if ($limit > 0) {
                            if ($used + $runcount <= $limit) {
                                $used += $runcount;
                                $usage[$key] = $used;
                            } else {
                                $error = 'The test request will exceed the daily test limit for the given API key';
                            }
                        } else {
                            $used += $runcount;
                            $usage[$key] = $used;
                        }
                        if (!strlen($error)) {
                            file_put_contents($keyfile, json_encode($usage));
                        }
                        Unlock($lock);
                    }
                }
                // check to see if we need to limit queue lengths from this API key
                if ($keys[$key]['queue_limit']) {
                    $test['queue_limit'] = $keys[$key]['queue_limit'];
                }
            } else {
                $error = 'Invalid API Key';
            }
            if (!strlen($error) && $key != $keys['server']['key']) {
                global $usingAPI;
                $usingAPI = true;
            }
            // Make sure API keys don't exceed the max configured priority
            $maxApiPriority = GetSetting('maxApiPriority');
            if ($maxApiPriority) {
                $test['priority'] = max($test['priority'], $maxApiPriority);
            }
        } elseif (!isset($admin) || !$admin) {
            $error = 'An error occurred processing your request (missing API key).';
            if (GetSetting('allow_getkeys')) {
                $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || isset($_SERVER['HTTP_SSL']) && $_SERVER['HTTP_SSL'] == 'On' ? 'https' : 'http';
                $url = "{$protocol}://{$_SERVER['HTTP_HOST']}/getkey.php";
                $error .= "  If you do not have an API key assigned you can request one at {$url}";
            }
        }
    }
}
Exemple #6
0
                unset($tests);
            }
        }
    }
}
// Render the video
if (isset($tests) && count($tests)) {
    $lock = Lock("video-{$videoId}", false, 600);
    if ($lock) {
        RenderVideo($tests);
        if (is_file("{$videoPath}/render.mp4")) {
            rename("{$videoPath}/render.mp4", "{$videoPath}/video.mp4");
        }
        $ini = 'completed=' . gmdate('c') . "\r\n";
        file_put_contents("{$videoPath}/video.ini", $ini);
        Unlock($lock);
    }
}
$elapsed = microtime(true) - $start;
//echo number_format($elapsed, 3) . " seconds";
function RenderVideo(&$tests)
{
    global $width, $height, $maxAspectRatio, $videoExtendTime, $biggestThumbnail, $fps, $labelHeight, $timeHeight, $rowPadding, $speed, $fractionTime;
    // adjust the label sizes if we have a LOT of tests
    $scale = 1;
    $count = count($tests);
    if ($count > 49) {
        $scale = 0;
    } elseif ($count > 36) {
        $scale = 0.5;
    } elseif ($count > 25) {
Exemple #7
0
/**
* Send a quick http request locally if we need to process cron events (to each of the cron entry points)
* 
* This only runs events on 15-minute intervals and tries to keep it close to the clock increments (00, 15, 30, 45)
* 
*/
function CheckCron()
{
    // open and lock the cron job file - abandon quickly if we can't get a lock
    $should_run = false;
    $minutes15 = false;
    $minutes60 = false;
    $cron_lock = Lock("Cron Check", false, 1200);
    if (isset($cron_lock)) {
        $last_run = 0;
        if (is_file('./tmp/wpt_cron.dat')) {
            $last_run = file_get_contents('./tmp/wpt_cron.dat');
        }
        $now = time();
        $elapsed = $now - $last_run;
        if (!$last_run) {
            $should_run = true;
            $minutes15 = true;
            $minutes60 = true;
        } elseif ($elapsed > 120) {
            if ($elapsed > 1200) {
                // if it has been over 20 minutes, run regardless of the wall-clock time
                $should_run = true;
            } else {
                $minute = gmdate('i', $now) % 5;
                if ($minute < 2) {
                    $should_run = true;
                    $minute = gmdate('i', $now) % 15;
                    if ($minute < 2) {
                        $minutes15 = true;
                    }
                    $minute = gmdate('i', $now) % 60;
                    if ($minute < 2) {
                        $minutes60 = true;
                    }
                }
            }
        }
        if ($should_run) {
            file_put_contents('./tmp/wpt_cron.dat', $now);
        }
        Unlock($cron_lock);
    }
    // send the cron requests
    if ($should_run) {
        if (is_file('./settings/benchmarks/benchmarks.txt') && is_file('./benchmarks/cron.php')) {
            SendAsyncRequest('/benchmarks/cron.php');
        }
        SendAsyncRequest('/cron/5min.php');
        if (is_file('./jpeginfo/cleanup.php')) {
            SendAsyncRequest('/jpeginfo/cleanup.php');
        }
        if ($minutes15) {
            SendAsyncRequest('/cron/15min.php');
        }
        if ($minutes60) {
            SendAsyncRequest('/cron/hourly.php');
        }
    }
}
Exemple #8
0
/**
* Do all of the processing for a given benchmark
* 
* @param mixed $benchmark
*/
function ProcessBenchmark($benchmark)
{
    global $logFile;
    $lock = Lock("Benchmark {$benchmark} Cron", false, 86400);
    if (isset($lock)) {
        logMsg("Processing benchmark '{$benchmark}'", "./log/{$logFile}", true);
        $options = array();
        if (include "./settings/benchmarks/{$benchmark}.php") {
            if (!is_dir("./results/benchmarks/{$benchmark}")) {
                mkdir("./results/benchmarks/{$benchmark}", 0777, true);
            }
            if (is_file("./results/benchmarks/{$benchmark}/state.json")) {
                $state = json_decode(file_get_contents("./results/benchmarks/{$benchmark}/state.json"), true);
            } else {
                $state = array('running' => false, 'needs_aggregation' => true, 'runs' => array());
                // build up a list of runs if we have data
                if (is_dir("./results/benchmarks/{$benchmark}/data")) {
                    $files = scandir("./results/benchmarks/{$benchmark}/data");
                    $last_run = 0;
                    foreach ($files as $file) {
                        if (preg_match('/([0-9]+_[0-9]+)\\..*/', $file, $matches)) {
                            $UTC = new DateTimeZone('UTC');
                            $date = DateTime::createFromFormat('Ymd_Hi', $matches[1], $UTC);
                            $time = $date->getTimestamp();
                            $state['runs'][] = $time;
                            if ($time > $last_run) {
                                $last_run = $time;
                            }
                        }
                    }
                    if ($last_run) {
                        $state['last_run'] = $last_run;
                    }
                }
                file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state));
            }
            if (!is_array($state)) {
                $state = array('running' => false);
            }
            if (!array_key_exists('running', $state)) {
                $state['running'] = false;
            }
            if (array_key_exists('running', $state)) {
                CheckBenchmarkStatus($benchmark, $state);
                // update the state between steps
                file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state));
            } else {
                $state['running'] = false;
            }
            if (!$state['running'] && (array_key_exists('runs', $state) && count($state['runs'])) && (!array_key_exists('needs_aggregation', $state) || $state['needs_aggregation'])) {
                AggregateResults($benchmark, $state, $options);
                file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state));
            }
            // see if we need to kick off a new benchmark run
            if (!$state['running'] && (!array_key_exists('tests', $state) || !is_array($state['tests']) || !count($state['tests']))) {
                if (!array_key_exists('last_run', $state)) {
                    $state['last_run'] = 0;
                }
                $now = time();
                if (call_user_func("{$benchmark}ShouldExecute", $state['last_run'], $now)) {
                    if (is_file("./log/{$logFile}")) {
                        unlink("./log/{$logFile}");
                    }
                    logMsg("Running benchmark '{$benchmark}'", "./log/{$logFile}", true);
                    if (SubmitBenchmark($configurations, $state, $benchmark)) {
                        $state['last_run'] = $now;
                        $state['running'] = true;
                    }
                } else {
                    logMsg("Benchmark '{$benchmark}' does not need to be run", "./log/{$logFile}", true);
                }
            }
            file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state));
        }
        logMsg("Done Processing benchmark '{$benchmark}'", "./log/{$logFile}", true);
        Unlock($lock);
    }
}
Exemple #9
0
function EC2_GetRunningInstances()
{
    $now = time();
    $instances = array();
    $key = GetSetting('ec2_key');
    $secret = GetSetting('ec2_secret');
    if ($key && $secret) {
        $locations = EC2_GetAMILocations();
        try {
            $ec2 = \Aws\Ec2\Ec2Client::factory(array('key' => $key, 'secret' => $secret, 'region' => 'us-east-1'));
            $regions = array();
            $response = $ec2->describeRegions();
            if (isset($response['Regions'])) {
                foreach ($response['Regions'] as $region) {
                    $regions[] = $region['RegionName'];
                }
            }
        } catch (\Aws\Ec2\Exception\Ec2Exception $e) {
            $error = $e->getMessage();
            EC2LogError("Listing running EC2 instances: {$error}");
        }
        if (isset($regions) && is_array($regions) && count($regions)) {
            foreach ($regions as $region) {
                try {
                    $ec2 = \Aws\Ec2\Ec2Client::factory(array('key' => $key, 'secret' => $secret, 'region' => $region));
                    $response = $ec2->describeInstances();
                    if (isset($response['Reservations'])) {
                        foreach ($response['Reservations'] as $reservation) {
                            foreach ($reservation['Instances'] as $instance) {
                                $wptLocations = null;
                                // See what locations are associated with the AMI
                                if (isset($instance['ImageId']) && isset($locations[$instance['ImageId']]['locations'])) {
                                    $wptLocations = $locations[$instance['ImageId']]['locations'];
                                } elseif (isset($instance['Tags'])) {
                                    // fall back to using tags to identify locations if they were set
                                    foreach ($instance['Tags'] as $tag) {
                                        if ($tag['Key'] == 'WPTLocations') {
                                            $wptLocations = explode(',', $tag['Value']);
                                            break;
                                        }
                                    }
                                }
                                if (isset($wptLocations)) {
                                    $launchTime = strtotime($instance['LaunchTime']);
                                    $elapsed = $now - $launchTime;
                                    $state = $instance['State']['Code'];
                                    $running = false;
                                    if (is_numeric($state) && $state <= 16) {
                                        $running = true;
                                    }
                                    $instances[] = array('region' => $region, 'id' => $instance['InstanceId'], 'ami' => $instance['ImageId'], 'state' => $state, 'launchTime' => $instance['LaunchTime'], 'launched' => $launchTime, 'runningTime' => $elapsed, 'locations' => $wptLocations, 'running' => $running);
                                }
                            }
                        }
                    }
                } catch (\Aws\Ec2\Exception\Ec2Exception $e) {
                    $error = $e->getMessage();
                    EC2LogError("Listing running EC2 instances: {$error}");
                }
            }
        }
    }
    // update the AMI counts we are tracking locally
    if (count($instances)) {
        $lock = Lock('ec2-instances', true, 120);
        if ($lock) {
            $amis = array();
            foreach ($instances as $instance) {
                if (isset($instance['ami']) && strlen($instance['ami']) && $instance['running']) {
                    if (!isset($amis[$instance['ami']])) {
                        $amis[$instance['ami']] = array('count' => 0);
                    }
                    $amis[$instance['ami']]['count']++;
                }
            }
            file_put_contents('./tmp/ec2-instances.dat', json_encode($amis));
            Unlock($lock);
        }
    }
    return $instances;
}
Exemple #10
0
                    delTree($archiveDir);
                    if (is_file($zipFile)) {
                        $bucket = "{$bucket_base}{$info['year']}_{$info['month']}_{$info['day']}_{$info['group']}{$suffix}";
                        logMessage("Uploading {$zipFile} to {$bucket}");
                        if (ArchiveFile($zipFile, $bucket)) {
                            logMessage("Archiving {$id} Complete");
                            file_put_contents("{$dir}/archive.dat", "{$download_path}{$bucket}/{$id}.zip");
                        } else {
                            logMessage("Archiving {$id} FAILED");
                        }
                        unlink($zipFile);
                    } else {
                        logMessage("Failed to combine files");
                    }
                } else {
                    // Nothing to process, mark the group as done
                    file_put_contents("{$dir}/archive.dat", "");
                    file_put_contents("{$dir}/archive.dat.valid", "");
                    logMessage("{$id} - No tests to Archive");
                }
            } else {
                logMessage("{$id} - Already Archived");
            }
        } else {
            logMessage("{$id} - Tests still running");
        }
        Unlock($dir_lock);
    } else {
        logMessage("{$id} is already being processed");
    }
});
Exemple #11
0
$cron_lock = Lock("cron-60", false, 3600);
if (!isset($cron_lock)) {
    exit(0);
}
header("Content-type: text/plain");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo "Running hourly cron...\n";
require_once './ec2/ec2.inc.php';
if (GetSetting('ec2_key')) {
    EC2_DeleteOrphanedVolumes();
}
GitUpdate();
AgentUpdate();
ApkUpdate();
Unlock($cron_lock);
if (GetSetting('cron_archive')) {
    chdir('./cli');
    include 'archive.php';
}
echo "Done\n";
/**
* Automatically update from the git master (if configured)
* 
*/
function GitUpdate()
{
    if (GetSetting('gitUpdate')) {
        echo "Updating from GitHub...\n";
        echo shell_exec('git pull origin master');
    }
Exemple #12
0
 protected function UnLock()
 {
     if (isset($this->lock)) {
         Unlock($this->lock);
     }
 }
Exemple #13
0
function TSViewCreate($server, $tsview_name, &$metrics)
{
    $needs_update = false;
    if (!is_dir('./dat')) {
        mkdir('./dat', 0777, true);
    }
    $def = './dat/tsview-' . sha1($tsview_name) . '.json';
    $lock = Lock("TSView {$tsview_name}");
    if (isset($lock)) {
        if (is_file($def)) {
            $current = json_decode(file_get_contents($def), true);
        }
        if (!isset($current) || !is_array($current)) {
            $current = array();
        }
        foreach ($metrics as $metric => $x) {
            if (!array_key_exists($metric, $current)) {
                $needs_update = true;
                $current[$metric] = 1;
            }
        }
        if ($needs_update) {
            $data = array('names' => array());
            foreach ($current as $metric => $x) {
                $data['names'][] = str_replace('.', '_', $metric);
            }
            $body = json_encode($data);
            if (http_put_raw("{$server}{$tsview_name}", $body)) {
                file_put_contents($def, json_encode($current));
            }
        }
        Unlock($lock);
    }
}
Exemple #14
0
/**
* See if we are requiring key validation and if so, enforce the restrictions
*
* @param mixed $test
* @param mixed $error
*/
function ValidateKey(&$test, &$error, $key = null)
{
    global $admin;
    // load the secret key (if there is one)
    $secret = '';
    $keys = parse_ini_file('./settings/keys.ini', true);
    if ($keys && isset($keys['server']) && isset($keys['server']['secret'])) {
        $secret = trim($keys['server']['secret']);
    }
    if (strlen($secret)) {
        // ok, we require key validation, see if they have an hmac (user with form)
        // or API key
        if (!isset($key) && isset($test['vh']) && strlen($test['vh'])) {
            // validate the hash
            $hashStr = $secret;
            $hashStr .= $_SERVER['HTTP_USER_AGENT'];
            $hashStr .= $test['owner'];
            $hashStr .= $test['vd'];
            $hmac = sha1($hashStr);
            // check the elapsed time since the hmac was issued
            $now = time();
            $origTime = strtotime($test['vd']);
            $elapsed = abs($now - $origTime);
            if ($hmac != $test['vh'] || $elapsed > 86400) {
                $error = 'Your test request could not be validated (this can happen if you leave the browser window open for over a day before submitting a test).  Please try submitting it again.';
            }
        } elseif (isset($key) || isset($test['key']) && strlen($test['key'])) {
            if (isset($test['key']) && strlen($test['key']) && !isset($key)) {
                $key = $test['key'];
            }
            // validate their API key and enforce any rate limits
            if (array_key_exists($key, $keys)) {
                if (array_key_exists('default location', $keys[$key]) && strlen($keys[$key]['default location']) && !strlen($test['location'])) {
                    $test['location'] = $keys[$key]['default location'];
                }
                if (isset($keys[$key]['priority'])) {
                    $test['priority'] = $keys[$key]['priority'];
                }
                if (isset($keys[$key]['limit'])) {
                    $limit = (int) $keys[$key]['limit'];
                    // update the number of tests they have submitted today
                    if (!is_dir('./dat')) {
                        mkdir('./dat', 0777, true);
                    }
                    $lock = Lock("API Keys");
                    if (isset($lock)) {
                        $keyfile = './dat/keys_' . gmdate('Ymd') . '.dat';
                        $usage = null;
                        if (is_file($keyfile)) {
                            $usage = json_decode(file_get_contents($keyfile), true);
                        }
                        if (!isset($usage)) {
                            $usage = array();
                        }
                        if (isset($usage[$key])) {
                            $used = (int) $usage[$key];
                        } else {
                            $used = 0;
                        }
                        $runcount = max(1, $test['runs']);
                        if (!$test['fvonly']) {
                            $runcount *= 2;
                        }
                        if ($limit > 0) {
                            if ($used + $runcount <= $limit) {
                                $used += $runcount;
                                $usage[$key] = $used;
                            } else {
                                $error = 'The test request will exceed the daily test limit for the given API key';
                            }
                        } else {
                            $used += $runcount;
                            $usage[$key] = $used;
                        }
                        if (!strlen($error)) {
                            file_put_contents($keyfile, json_encode($usage));
                        }
                        Unlock($lock);
                    }
                }
                // check to see if we need to limit queue lengths from this API key
                if ($keys[$key]['queue_limit']) {
                    $test['queue_limit'] = $keys[$key]['queue_limit'];
                }
            } else {
                $error = 'Invalid API Key';
            }
            if (!strlen($error)) {
                global $usingAPI;
                $usingAPI = true;
            }
        } elseif (!isset($admin) || !$admin) {
            $error = 'An error occurred processing your request.  Please reload the testing page and try submitting your test request again. (missing API key)';
        }
    }
}
/**
 * @param TestPaths $localPaths Paths for step or run to save the cached data
 * @param array $requests The requests to save
 * @param array $pageData The page data to save
 * @param int $ver Cache version
 */
function SaveCachedDevToolsRequests($localPaths, &$requests, &$pageData, $ver)
{
    $cacheFile = $localPaths->devtoolsRequestsCacheFile($ver);
    $lock = Lock($cacheFile);
    if (isset($lock)) {
        if (gz_is_file($cacheFile)) {
            $cache = json_decode(gz_file_get_contents($cacheFile), true);
        }
        if (!isset($cache) || !is_array($cache)) {
            $cache = array();
        }
        $cache['requests'] = $requests;
        $cache['pageData'] = $pageData;
        gz_file_put_contents($cacheFile, json_encode($cache));
        Unlock($lock);
    }
}
Exemple #16
0
/**
* Update the feeds
* 
*/
function UpdateFeeds()
{
    if (!is_dir('./tmp')) {
        mkdir('./tmp', 0777);
    }
    $feedData = array();
    $lock = Lock("Update Feeds", false);
    if (isset($lock)) {
        // load the list of feeds
        require_once './settings/feeds.inc';
        require_once './lib/simplepie.inc';
        // loop through and update each one
        foreach ($feeds as $category => &$feedList) {
            $feedData[$category] = array();
            foreach ($feedList as $feedSource => $feedUrl) {
                $feedUrl = trim($feedUrl);
                if (strlen($feedUrl)) {
                    $feed = new SimplePie();
                    if ($feed) {
                        $rawFeed = trim(http_fetch($feedUrl));
                        $feed->set_raw_data($rawFeed);
                        $feed->enable_cache(false);
                        $feed->init();
                        // try sanitizing the data if we have a problem parsing the feed
                        if (strlen($feed->error())) {
                            FixFeed($rawFeed);
                            $feed->set_raw_data($rawFeed);
                            $feed->enable_cache(false);
                            $feed->init();
                        }
                        foreach ($feed->get_items() as $item) {
                            $dateStr = $item->get_date(DATE_RSS);
                            if ($dateStr && strlen($dateStr)) {
                                $date = strtotime($dateStr);
                                if ($date) {
                                    // only include articles from the last 30 days
                                    $now = time();
                                    $elapsed = 0;
                                    if ($now > $date) {
                                        $elapsed = $now - $date;
                                    }
                                    $days = (int) ($elapsed / 86400);
                                    if ($days <= 30) {
                                        // make sure we don't have another article from the exact same time
                                        while (isset($feedData[$category][$date])) {
                                            $date++;
                                        }
                                        $feedData[$category][$date] = array('source' => $feedSource, 'title' => $item->get_title(), 'link' => urldecode($item->get_permalink()), 'date' => $dateStr);
                                    }
                                }
                            }
                            $item->__destruct();
                        }
                        $feed->__destruct();
                        unset($feed);
                    }
                }
            }
            if (count($feedData[$category])) {
                krsort($feedData[$category]);
            }
        }
        // save out the feed data
        file_put_contents('./tmp/feeds.dat', json_encode($feedData));
        Unlock($lock);
    }
}
Exemple #17
0
function EC2_GetRunningInstances()
{
    $now = time();
    $instances = array();
    $key = GetSetting('ec2_key');
    $secret = GetSetting('ec2_secret');
    if ($key && $secret) {
        $ec2 = new AmazonEC2($key, $secret);
        $regions = array();
        $response = $ec2->describe_regions();
        if (isset($response) && $response->isOK()) {
            foreach ($response->body->regionInfo->item as $region) {
                $regions[] = (string) $region->regionName;
            }
        }
        foreach ($regions as $region) {
            $ec2->set_region($region);
            $response = $ec2->describe_instances();
            if (isset($response) && $response->isOK()) {
                foreach ($response->body->reservationSet->item as $item) {
                    foreach ($item->instancesSet->item as $instance) {
                        $wptLocations = null;
                        if (isset($instance->tagSet)) {
                            foreach ($instance->tagSet->item as $tag) {
                                if ($tag->key == 'WPTLocations') {
                                    $wptLocations = explode(',', $tag->value);
                                    break;
                                }
                            }
                        }
                        if (isset($wptLocations)) {
                            $launchTime = strtotime((string) $instance->launchTime);
                            $elapsed = $now - $launchTime;
                            $instances[] = array('region' => $region, 'id' => (string) $instance->instanceId, 'ami' => (string) $instance->imageId, 'state' => (int) $instance->instanceState->code, 'launchTime' => (string) $instance->launchTime, 'launched' => $launchTime, 'runningTime' => $elapsed, 'locations' => $wptLocations);
                        }
                    }
                }
            }
        }
    }
    // update the AMI counts we are tracking locally
    if (count($instances)) {
        $lock = Lock('ec2-instances', true, 120);
        if ($lock) {
            $amis = array();
            foreach ($instances as $instance) {
                if (isset($instance['ami']) && strlen($instance['ami']) && is_numeric($instance['state']) && $instance['state'] <= 16) {
                    if (!isset($amis[$instance['ami']])) {
                        $amis[$instance['ami']] = array('count' => 0);
                    }
                    $amis[$instance['ami']]['count']++;
                }
            }
            file_put_contents('./tmp/ec2-instances.dat', json_encode($amis));
            Unlock($lock);
        }
    }
    return $instances;
}
Exemple #18
0
function isFreeLock($lock)
{
    if (Lock($lock, 0)) {
        Unlock($lock);
        return true;
    }
    return false;
}
Exemple #19
0
                $peers = getRandomPeers($info_hash);
                sendPeerList($peers);
            } else {
                showError("Invalid event= from client.");
            }
        }
    }
}
if ($GLOBALS["countbytes"]) {
    // Once every minute or so, we run the speed update checker.
    $query = @mysql_query("SELECT UNIX_TIMESTAMP() - lastSpeedCycle FROM BTPHP_summary WHERE info_hash=\"{$info_hash}\"");
    $results = mysql_fetch_row($query);
    if ($results[0] >= 60) {
        if (Lock("SPEED:{$info_hash}")) {
            @runSpeed($info_hash, $results[0]);
            Unlock("SPEED:{$info_hash}");
        }
    }
}
/* 
 * Under heavy loads, this will lighten the load slightly... very slightly...
 */
//if (mt_rand(1,10) == 4)
trashCollector($info_hash, $report_interval);
// Finally, it's time to do stuff to the summary table.
if (!empty($summaryupdate)) {
    $stuff = "";
    foreach ($summaryupdate as $column => $value) {
        $stuff .= ', ' . $column . ($value[1] ? "=" : "={$column}+") . $value[0];
    }
    mysql_query("UPDATE BTPHP_summary SET " . substr($stuff, 1) . " WHERE info_hash=\"{$info_hash}\"");
function ImportBenchmarkRun($benchmark, $epoch, &$test_data)
{
    global $error;
    $lock = Lock("Benchmark {$benchmark} Cron", true, 86400);
    if (isset($lock)) {
        if (!is_dir("./results/benchmarks/{$benchmark}")) {
            mkdir("./results/benchmarks/{$benchmark}", 0777, true);
        }
        if (is_file("./results/benchmarks/{$benchmark}/state.json")) {
            $state = json_decode(file_get_contents("./results/benchmarks/{$benchmark}/state.json"), true);
        } else {
            $state = array('running' => false, 'needs_aggregation' => false, 'runs' => array());
        }
        $state['running'] = true;
        $state['last_run'] = $epoch;
        $state['tests'] = array();
        foreach ($test_data as $test) {
            $test['submitted'] = $epoch;
            $state['tests'][] = $test;
        }
        file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state));
        Unlock($lock);
        // kick off the collection and aggregation of the results
        $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || isset($_SERVER['HTTP_SSL']) && $_SERVER['HTTP_SSL'] == 'On' ? 'https' : 'http';
        $host = $_SERVER['HTTP_HOST'];
        $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $cron = "{$protocol}://{$host}{$uri}/benchmarks/cron.php?benchmark=" . urlencode($benchmark);
        file_get_contents($cron);
    }
}
Exemple #21
0
                logMessage("{$id} - Tests still running");
            }
            if (is_file("{$dir}/har.complete")) {
                logMessage("{$id} - HAR already processed");
            }
        }
        Unlock($dir_lock);
    } else {
        logMessage("{$id} is already being processed");
    }
});
// Check the list of crawls that have completed to see if all of the uploads have also completed
$upload_lock = Lock("har-uploads", false, 86400);
if (isset($upload_lock)) {
    CheckUploads();
    Unlock($upload_lock);
}
function CheckUploads()
{
    // Load the completed har uploads
    if (is_file('./results/crawl/har.uploads')) {
        $uploads = json_decode(file_get_contents('./results/crawl/har.uploads'), true);
    }
    if (!isset($uploads) || !is_array($uploads)) {
        $uploads = array();
    }
    $dirty = false;
    // get the list of crawls
    $files = glob('./results/crawl/*.json');
    if ($files !== FALSE && is_array($files)) {
        foreach ($files as $file) {
         }
         $indLock = Lock("Industry Video");
         if (isset($indLock)) {
             // update the page in the industry list
             $ind;
             $data = file_get_contents('./video/dat/industry.dat');
             if ($data) {
                 $ind = json_decode($data, true);
             }
             $update = array();
             $update['id'] = $id;
             $update['last_updated'] = $now;
             $ind[$ini['industry']][$ini['industry_page']] = $update;
             $data = json_encode($ind);
             file_put_contents('./video/dat/industry.dat', $data);
             Unlock($indLock);
         }
     }
 }
 if ($testInfo_dirty) {
     SaveTestInfo($id, $testInfo);
 }
 SecureDir($testPath);
 UnlockTest($testLock);
 /*************************************************************************
  * Do No modify TestInfo after this point
  **************************************************************************/
 // do any post-processing when the full test is complete that doesn't rely on testinfo
 if ($done) {
     logTestMsg($id, "Test Complete");
     // send an async request to the post-processing code so we don't block
function SaveCachedDevToolsRequests($testPath, $run, $cached, &$requests, &$pageData, $ver)
{
    $cacheFile = "{$testPath}/{$run}.{$cached}.devToolsRequests.{$ver}";
    $lock = Lock($cacheFile);
    if (isset($lock)) {
        if (gz_is_file($cacheFile)) {
            $cache = json_decode(gz_file_get_contents($cacheFile), true);
        }
        if (!isset($cache) || !is_array($cache)) {
            $cache = array();
        }
        $cache[$run][$cached]['requests'] = $requests;
        $cache[$run][$cached]['pageData'] = $pageData;
        gz_file_put_contents($cacheFile, json_encode($cache));
        Unlock($lock);
    }
}
Exemple #24
0
/**
* Cache the test ID in the case of multiple submits
*/
function PSS_TestSubmitted(&$test)
{
    if (array_key_exists('id', $test) && array_key_exists('url', $test)) {
        $now = time();
        $cache_time = 10080;
        // 7 days (in minutes)
        if (array_key_exists('cache', $_REQUEST) && $_REQUEST['cache'] > 0) {
            $cache_time = (int) $_REQUEST['cache'];
        }
        $expires = $now + $cache_time * 60;
        $entry = array('id' => $test['id'], 'expires' => $expires);
        $key = md5($test['url']);
        // update the cache
        $cache_lock = Lock("PSS Cache");
        if (isset($cache_lock)) {
            if (is_file('./tmp/pss.cache')) {
                $cache = json_decode(file_get_contents('./tmp/pss.cache'), true);
            } else {
                $cache = array();
            }
            // delete stale cache entries
            foreach ($cache as $cache_key => &$cache_entry) {
                if ($cache_entry['expires'] < $now) {
                    unset($cache[$cache_key]);
                }
            }
            $cache[$key] = $entry;
            file_put_contents('./tmp/pss.cache', json_encode($cache));
            Unlock($cache_lock);
        }
    }
}
function GetEC2Region($ip)
{
    $region = null;
    $json = null;
    if (isset($_REQUEST['ec2zone'])) {
        $region = substr($_REQUEST['ec2zone'], 0, strlen($_REQUEST['ec2zone']) - 1);
    } else {
        $lock = Lock('EC2Regions');
        if (isset($lock)) {
            $region_file = __DIR__ . '/dat/ec2addresses.json';
            $needs_update = false;
            if (is_file($region_file)) {
                $now = time();
                $updated = filemtime($region_file);
                if ($now > $updated && $now - $updated >= 86400) {
                    $needs_update = true;
                }
            }
            if (!is_file($region_file) || $needs_update) {
                $json = file_get_contents('https://ip-ranges.amazonaws.com/ip-ranges.json');
                if (isset($json) && $json !== FALSE && strlen($json)) {
                    file_put_contents($region_file, $json);
                }
            }
            if (!isset($json) && is_file($region_file)) {
                $json = file_get_contents($region_file);
            }
            Unlock($lock);
        }
        if (isset($json)) {
            $regions = json_decode($json, true);
            if (isset($regions['prefixes']) && is_array($regions['prefixes'])) {
                $ip = ip2long($ip);
                foreach ($regions['prefixes'] as $prefix) {
                    if (isset($prefix['ip_prefix']) && isset($prefix['region'])) {
                        list($subnet, $bits) = explode('/', $prefix['ip_prefix']);
                        $subnet = ip2long($subnet);
                        $mask = -1 << 32 - $bits;
                        $subnet &= $mask;
                        if (($ip & $mask) == $subnet) {
                            $region = $prefix['region'];
                            break;
                        }
                    }
                }
            }
        }
    }
    return $region;
}