Пример #1
0
/**
* Automatically update the agent binaries from the public agents (if configured)
* 
*/
function AgentUpdate()
{
    $updateServer = GetSetting('agentUpdate');
    echo "\nChecking for agent update...\n";
    if ($updateServer && strlen($updateServer)) {
        if (!is_dir('./work/update')) {
            mkdir('./work/update', 0777, true);
        }
        $url = $updateServer . 'work/getupdates.php';
        $updates = json_decode(http_fetch($url), true);
        if ($updates && is_array($updates) && count($updates)) {
            foreach ($updates as $update) {
                $needsUpdate = true;
                $ini = "./work/update/{$update['name']}.ini";
                $zip = "./work/update/{$update['name']}.zip";
                if (is_file($ini) && is_file($zip)) {
                    $current = parse_ini_file($ini);
                    if ($current['ver'] == $update['ver']) {
                        $needsUpdate = false;
                    }
                }
                if ($needsUpdate && isset($update['md5'])) {
                    $tmp = "./work/update/{$update['name']}.tmp";
                    if (is_file($tmp)) {
                        unlink($tmp);
                    }
                    $url = $updateServer . str_replace(" ", "%20", "work/update/{$update['name']}.zip?v={$update['ver']}");
                    echo "Fetching {$url}\n";
                    if (http_fetch_file($url, $tmp)) {
                        $md5 = md5_file($tmp);
                        if ($md5 == $update['md5']) {
                            if (is_file("{$ini}.bak")) {
                                unlink("{$ini}.bak");
                            }
                            if (is_file($ini)) {
                                rename($ini, "{$ini}.bak");
                            }
                            if (is_file("{$zip}.bak")) {
                                unlink("{$zip}.bak");
                            }
                            if (is_file($zip)) {
                                rename($zip, "{$zip}.bak");
                            }
                            rename($tmp, $zip);
                            $z = new ZipArchive();
                            if ($z->open($zip) === TRUE) {
                                $z->extractTo('./work/update', "{$update['name']}.ini");
                                $z->close();
                            }
                        }
                    }
                }
            }
        }
    }
}
function get_quotes($stocklist)
{
    if (!$stocklist) {
        return array();
    }
    while (list($symbol, $name) = each($stocklist)) {
        $symbollist[] = $symbol;
        $symbollist_no_coding[] = str_replace('^', '\\^', urldecode($symbol));
        //	$symbol = rawurlencode($symbol);
        $symbolstr .= $symbol;
        if ($i++ < count($stocklist) - 1) {
            $symbolstr .= '+';
        }
    }
    //		$regexp_stocks = '/(' . implode('|',$symbollist) . ')/';
    $regexp_stocks = '/^\\"(' . implode('|', $symbollist_no_coding) . ')/';
    $url = 'http://finance.yahoo.com/d/quotes.csv?f=sl1d1t1c1ohgv&e=.csv&s=' . $symbolstr;
    $lines = http_fetch($url, false, 80, '');
    $quotes = array();
    $i = 0;
    if ($lines) {
        while ($line = each($lines)) {
            $line = $lines[$i];
            if (preg_match($regexp_stocks, $line)) {
                $line = str_replace('"', '', $line);
                list($symbol, $price0, $date, $time, $dchange, $price1, $price2) = split(',', $line);
                if ($price1 > 0 && $dchange != 0) {
                    $pchange = round(10000 * $dchange / $price1) / 100;
                } else {
                    $pchange = 0;
                }
                if ($pchange > 0) {
                    $pchange = '+' . $pchange;
                }
                $name = $stocklist[urlencode($symbol)];
                if (!$name) {
                    $name = $symbol;
                }
                $quotes[] = array('symbol' => $symbol, 'price0' => $price0, 'date' => $date, 'time' => $time, 'dchange' => $dchange, 'price1' => $price1, 'price2' => $price2, 'pchange' => $pchange, 'name' => urldecode($name));
            }
            $i++;
        }
        return $quotes;
    }
}
Пример #3
0
/**
* Get the tester information for a remote location
*/
function GetRemoteTesters($server, $remote_location)
{
    $testers = array();
    global $remote_cache;
    $server_hash = md5($server);
    if (array_key_exists('relay', $_REQUEST) && $_REQUEST['relay']) {
        // see if we need to populate the cache from the remote server
        if (!array_key_exists($server_hash, $remote_cache)) {
            $xml = http_fetch("{$server}/getTesters.php?hidden=1");
            if ($xml) {
                $remote = json_decode(json_encode((array) simplexml_load_string($xml)), true);
                if (is_array($remote) && array_key_exists('data', $remote) && array_key_exists('location', $remote['data'])) {
                    $cache_entry = array();
                    foreach ($remote['data']['location'] as &$location) {
                        if (array_key_exists('testers', $location) && array_key_exists('tester', $location['testers'])) {
                            $parts = explode(':', $location['id']);
                            $id = $parts[0];
                            if (array_key_exists(0, $location['testers']['tester'])) {
                                $cache_entry[$id] = array('elapsed' => $location['elapsed'], 'testers' => $location['testers']['tester']);
                            } else {
                                $cache_entry[$id] = array('elapsed' => $location['elapsed'], 'testers' => array($location['testers']['tester']));
                            }
                        }
                    }
                    $remote_cache[$server_hash] = $cache_entry;
                }
            }
        }
        if (array_key_exists($server_hash, $remote_cache) && array_key_exists($remote_location, $remote_cache[$server_hash])) {
            $testers = $remote_cache[$server_hash][$remote_location];
        }
    }
    return $testers;
}
Пример #4
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);
    }
}
Пример #5
0
/**
* Submit the actual tests
* 
* @param mixed $results
*/
function SubmitTests(&$results, $testCount)
{
    global $video;
    global $private;
    global $runs;
    global $server;
    global $docComplete;
    global $fvonly;
    global $key;
    global $options;
    global $permutations;
    global $priority;
    global $bodies;
    $count = 0;
    foreach ($results as &$result) {
        if (array_key_exists('label', $result) && strlen($result['label']) && array_key_exists($result['label'], $permutations) && array_key_exists('location', $permutations[$result['label']])) {
            if (!array_key_exists('id', $result) || !strlen($result['id']) || array_key_exists('resubmit', $result) && $result['resubmit'] || array_key_exists('result', $result) && strlen($result['result']) && $result['result'] != 0 && $result['result'] != 99999) {
                $count++;
                echo "\rSubmitting test {$count} of {$testCount}...                  ";
                $location = $permutations[$result['label']]['location'];
                $request = $server . "runtest.php?f=json&runs={$runs}&url=" . urlencode($result['url']) . '&location=' . urlencode($location);
                $request .= "&affinity=" . urlencode($result['url']);
                if ($private) {
                    $request .= '&private=1';
                }
                if ($video) {
                    $request .= '&video=1';
                }
                if ($docComplete) {
                    $request .= '&web10=1';
                }
                if ($fvonly) {
                    $request .= '&fvonly=1';
                }
                if ($bodies) {
                    $request .= '&bodies=1';
                }
                if (isset($priority)) {
                    $request .= "&priority={$priority}";
                } else {
                    $request .= "&priority=9";
                }
                if (strlen($key)) {
                    $request .= "&k={$key}";
                }
                if (isset($options) && strlen($options)) {
                    $request .= '&' . $options;
                }
                if (array_key_exists('options', $permutations[$result['label']]) && strlen($permutations[$result['label']]['options'])) {
                    $request .= '&' . $permutations[$result['label']]['options'];
                }
                $response_str = http_fetch($request);
                if (strlen($response_str)) {
                    $response = json_decode($response_str, true);
                    if ($response['statusCode'] == 200) {
                        $entry = array();
                        $entry['url'] = $result['url'];
                        $entry['label'] = $result['label'];
                        $entry['id'] = $response['data']['testId'];
                        $result = $entry;
                    } else {
                        echo "\r                                                     ";
                        echo "\rError '{$response['statusText']}' submitting {$result['url']} for {$result['label']}\r\n";
                    }
                }
            }
        }
    }
    // clear the progress text
    echo "\r                                                     \r";
}
Пример #6
0
/**
* Go through and update the status of all of the tests
* 
* @param mixed $results
*/
function UpdateResults(&$results, $testCount)
{
    global $server;
    global $statsVer;
    global $video;
    $count = 0;
    $changed = false;
    foreach ($results as &$result) {
        if (array_key_exists('id', $result) && strlen($result['id']) && (!array_key_exists('statsVer', $result) || $result['statsVer'] != $statsVer || !array_key_exists('result', $result) || !strlen($result['result']))) {
            $count++;
            echo "\rUpdating the status of test {$count} of {$testCount}...                  ";
            //$url = "{$server}jsonResult.php?test={$result['id']}&medianRun=fastest";
            $url = "{$server}jsonResult.php?test={$result['id']}";
            if ($video) {
                $url .= "&medianMetric=SpeedIndex";
            }
            $response = http_fetch($url);
            if (strlen($response)) {
                $data = json_decode($response, true);
                unset($response);
                if (isset($data) && is_array($data) && array_key_exists('statusCode', $data)) {
                    if (array_key_exists('data', $data) && is_array($data['data']) && $data['statusCode'] == 200) {
                        $changed = true;
                        GetTestResult($data['data'], $result);
                        $result['statsVer'] = $statsVer;
                    } elseif ($data['statusCode'] >= 400) {
                        $changed = true;
                        $result['statsVer'] = $statsVer;
                        $result['result'] = -1;
                    }
                    IncrementStatus($data['statusCode']);
                } else {
                    IncrementStatus(-2);
                }
                unset($data);
            } else {
                IncrementStatus(-1);
            }
        } else {
            IncrementStatus(0);
        }
    }
    // clear the progress text
    echo "\r                                                     \r";
}
Пример #7
0
/**
* Pull the location information from a remote server
*/
function GetRemoteBacklog($server, $remote_location)
{
    $backlog = array();
    global $remote_cache;
    $server_hash = md5($server);
    if (array_key_exists('relay', $_REQUEST) && $_REQUEST['relay']) {
        // see if we need to populate the cache from the remote server
        if (!array_key_exists($server_hash, $remote_cache)) {
            $xml = http_fetch("{$server}/getLocations.php?hidden=1");
            if ($xml) {
                $remote = json_decode(json_encode((array) simplexml_load_string($xml)), true);
                if (is_array($remote) && array_key_exists('data', $remote) && array_key_exists('location', $remote['data'])) {
                    $cache_entry = array();
                    foreach ($remote['data']['location'] as &$location) {
                        $parts = explode(':', $location['id']);
                        $id = $parts[0];
                        $cache_entry[$id] = $location['PendingTests'];
                    }
                    $remote_cache[$server_hash] = $cache_entry;
                }
            }
        }
        if (array_key_exists($server_hash, $remote_cache) && array_key_exists($remote_location, $remote_cache[$server_hash])) {
            $backlog = $remote_cache[$server_hash][$remote_location];
        }
    }
    return $backlog;
}
Пример #8
0
/**
* User submitted the form. Validate the request and email a validation link.
* 
*/
function SumbitRequest()
{
    $error = null;
    $recaptcha_key = GetSetting('recaptcha_key');
    $recaptcha_secret = GetSetting('recaptcha_secret');
    if ($recaptcha_key && $recaptcha_secret) {
        if (isset($_REQUEST['g-recaptcha-response'])) {
            $response = urlencode($_REQUEST['g-recaptcha-response']);
            $url = "https://www.google.com/recaptcha/api/siteverify?secret={$recaptcha_secret}&response={$response}";
            $data = http_fetch($url);
            $check = json_decode($data, true);
            if (isset($check) && $check !== false && is_array($check) && isset($check['success'])) {
                if (!$check['success']) {
                    $error = "Captcha challenge failed";
                }
            } else {
                $error = "Error validating the captcha (could not communicate with server)";
            }
        } else {
            $error = "Please answer the captcha challenge";
        }
    }
    if (isset($error)) {
        echo $error;
    } else {
        if (isset($_REQUEST['agree']) && $_REQUEST['agree']) {
            $email = trim($_REQUEST['email']);
            if (!preg_match('/[^@]+@[^\\.]+\\..+/', $email)) {
                echo 'Please provide a valid email address';
            } elseif (BlockEmail($email)) {
                echo 'Sorry, registration is not permitted.  Please contact us for more information.';
            } elseif ($keyinfo = GetKeyInfo($email)) {
                EmailKeyInfo($keyinfo);
            } elseif ($requestinfo = CreateRequest($email)) {
                EmailValidation($requestinfo);
            } else {
                echo 'Internal generating the API key request';
            }
        } else {
            echo 'Please agree to the terms and conditions';
        }
    }
}