Пример #1
0
/**
 * Parse configuration and call actual host testing.
 * May open a http connection
 *
 * @param string $req Request Data
 */
function testHostConn(&$req)
{
    global $config;
    $mode = ParseINI::parseValue('hosttest_mode', $config);
    if ($mode == 0) {
        return true;
    }
    // Say it works if we didn't test.
    $timeout = ParseINI::parseValue('hosttest_timeout', $config);
    $addr = ParseINI::parseValue('Address', $req);
    preg_match('/TCP:0.0.0.0:([0-9]+),|$/', $addr, $addr_tcp);
    $tcpport = count($addr_tcp) == 2 ? (int) $addr_tcp[1] : 0;
    // Be sure that this is injection safe: this machine might have elevated privilegues on the host test provider based on ip
    preg_match('/UDP:0.0.0.0:([0-9]+),|$/', $addr, $addr_udp);
    $udpport = count($addr_udp) == 2 ? (int) $addr_udp[1] : 0;
    if (!$udpport && !$tcpport) {
        return false;
    }
    // That might be a false false, but it's weird anyway.
    unset($addr, $addr_tcp, $addr_udp);
    if ($mode == 1) {
        // Perform the check from local host
        return performHostTest($_SERVER['REMOTE_ADDR'], $tcpport, $udpport, $timeout);
    } else {
        // Call hostchecker on remote service
        $url = ParseINI::parseValue('hosttest_url', $config);
        $remotetimeout = ParseINI::parseValue('hosttest_remote_timeout', $config);
        if (!preg_match('#^(([a-z0-9.]+\\.[a-z0-9]+)|\\[([0-9a-f:]+)\\]):([0-9]+)(/.*)$#', $url, $url_split)) {
            trigger_error('Unable to parse hosttest_url from config.', E_USER_WARNING);
            return true;
            // As earlier
        }
        $remote_start = microtime(true);
        $fp = fsockopen($url_split[1], $url_split[4], $errno, $errstr, $remotetimeout);
        //fsockopen want's ipv6 with [], http's Host-field however does not!
        if (!$fp) {
            trigger_error('Unable to connect to remote C4HostTest ' . $url_split[1] . ':' . $url_split[4] . " in {$remotetimeout} secons.", E_USER_WARNING);
            return true;
        }
        $req_str = $url_split[5] . '?remotecall&tcp=' . $tcpport . '&udp=' . $udpport . '&ip=' . $_SERVER['REMOTE_ADDR'] . '&time=' . $timeout;
        $remotetimeout += $timeout;
        fwrite($fp, "GET " . $req_str . " HTTP/1.0\r\nHost: " . ($url_split[2] ? $url_split[2] : $urlsplit[3]) . "\r\nUser-Agent: C4Masterserver\r\n\r\n");
        // Injection warning!
        $reply = '';
        do {
            stream_set_timeout($fp, $remotetimeout - microtime(true) + $remote_start);
            $reply .= fread($fp, 8192);
        } while (!feof($fp) && $remotetimeout > microtime(true) - $remote_start);
        if (!preg_match('#^HTTP/1.[01] 200#', $reply)) {
            trigger_error('Unable to process response from C4HostTest. Wrong address in hosttest_url? No redirects allowed!', E_USER_WARNING);
            return true;
        }
        if (!preg_match('/\\r\\n\\r\\n(.*\\n)?Reached=[Yy]es\\r?\\n/s', $reply)) {
            return false;
        }
        return true;
    }
}
Пример #2
0
function GetGamesCountText($time = 86400)
{
    // default: last 24 hours
    if (!gotConnection()) {
        return;
    }
    $count = 0;
    $list = references();
    foreach ($list as $reference) {
        if (ParseINI::parseValue('State', $reference['data']) == 'Running' && $reference['time'] >= time() - $time) {
            $count++;
        }
    }
    $last = $time / 60 / 60;
    if ($count > 0) {
        if ($count > 1) {
            return $count . ' games in the last ' . $last . ' hours.';
        } else {
            return 'One game in the last ' . $last . ' hours.';
        }
    } else {
        return 'No games running in the last ' . $last . ' hours.';
    }
}
Пример #3
0
 public function getCurrentVersion($platform)
 {
     $result = mysql_query('SELECT `new_version` FROM `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'update` WHERE `old_version` = \'\' AND `platform` = \'' . $platform . '\'');
     if ($result) {
         $row = mysql_fetch_assoc($result);
         return $row['new_version'];
     }
     return false;
 }
Пример #4
0
function registerRelease()
{
    global $config, $link, $prefix;
    // check request validity
    if (ParseINI::parseValue('oc_enable_update', $config) != 1) {
        throw new Exception('Update disabled on this server.');
    }
    // mandatory parameters
    if (!isset($_REQUEST['file'])) {
        throw new Exception('Missing mandatory parameter "file"');
    }
    if (!isset($_REQUEST['hash'])) {
        throw new Exception('Missing mandatory parameter "hash"');
    }
    if (!isset($_REQUEST['new_version'])) {
        throw new Exception('Missing mandatory parameter "new_version"');
    }
    if (!isset($_REQUEST['platform'])) {
        throw new Exception('Missing mandatory parameter "platform"');
    }
    if (!isset($_REQUEST['hash'])) {
        throw new Exception('Missing mandatory parameter "hash"');
    }
    // authorization
    $absolutefile = ParseINI::parseValue('oc_update_path', $config) . $_REQUEST['file'];
    if (!file_exists($absolutefile)) {
        throw new Exception('Specified file "' . $absolutefile . '" not found.');
    }
    $filehash = hash_hmac_file('sha256', $absolutefile, ParseINI::parseValue('oc_update_secret', $config));
    if ($filehash != $_REQUEST['hash']) {
        throw new Exception('Authorization failure: Hash incorrect.');
    }
    // checks done, now update DB
    $old_version = array();
    if (isset($_REQUEST['old_version']) && !empty($_REQUEST['old_version'])) {
        $old_version = explode(',', mysql_real_escape_string($_REQUEST['old_version'], $link));
    }
    $delete_old_files = false;
    if (isset($_REQUEST['delete_old_files']) && $_REQUEST['delete_old_files'] == 'yes') {
        $delete_old_files = true;
    }
    $new_version = mysql_real_escape_string($_REQUEST['new_version'], $link);
    $platform = mysql_real_escape_string($_REQUEST['platform'], $link);
    $file = mysql_real_escape_string($_REQUEST['file'], $link);
    if (!empty($old_version)) {
        if ($delete_old_files) {
            $result = mysql_query('SELECT `file` FROM `' . $prefix . 'update` WHERE `new_version` != \'' . $new_version . '\' AND `old_version` != \'\' AND `platform` = \'' . $platform . '\'');
            while (($row = mysql_fetch_assoc($result)) != false) {
                unlink(ParseINI::parseValue('oc_update_path', $config) . $row['file']);
            }
        }
        mysql_query('DELETE FROM `' . $prefix . 'update` WHERE `new_version` != \'' . $new_version . '\' AND `old_version` != \'\' AND `platform` = \'' . $platform . '\'');
        foreach ($old_version as $version) {
            mysql_query('INSERT INTO `' . $prefix . 'update` (`old_version`, `new_version`, `platform`, `file`) VALUES (\'' . $version . '\', \'' . $new_version . '\', \'' . $platform . '\', \'' . $file . '\')');
        }
    } else {
        if ($delete_old_files) {
            $row = mysql_fetch_assoc(mysql_query('SELECT `file` FROM `' . $prefix . 'update` WHERE `old_version` = \'\' AND `platform` = \'' . $platform . '\''));
            unlink(ParseINI::parseValue('oc_update_path', $config) . $row['file']);
        }
        mysql_query('DELETE FROM `' . $prefix . 'update` WHERE `old_version` = \'\' AND `platform` = \'' . $platform . '\'');
        mysql_query('INSERT INTO `' . $prefix . 'update` (`old_version`, `new_version`, `platform`, `file`) VALUES (\'\', \'' . $new_version . '\', \'' . $platform . '\', \'' . $file . '\')');
    }
}
Пример #5
0
 /**
  * Returns all values in a certain category and supports multiple appereances.
  *
  * @param string $key
  * @param string $category
  * @param string $string
  * @return array
  */
 public static function parseValuesByCategory($key, $category, $string)
 {
     if (!$key || !$string || !$category) {
         return array();
     }
     $string = ParseINI::normalize($string);
     if (strpos($string, $key) === false || strpos($string, '[' . $category . ']') === false) {
         return array();
     }
     $values = array();
     $lines = explode("\n", $string);
     $current_category = '';
     foreach ($lines as $line) {
         $line = trim($line);
         if (strpos($line, '[') !== false && strpos($line, ']') !== false && strpos($line, '=') === false) {
             $start = strpos($line, '[') + 1;
             $end = strpos($line, ']') - 1;
             $current_category = substr($line, $start, $end);
         }
         if ($current_category != $category) {
             continue;
         }
         //wrong category
         if (strpos($line, ';') === 0) {
             continue;
         }
         //comment
         if (strpos($line, $key) === false) {
             continue;
         }
         //not needed
         $values[] = ParseINI::parseValue($key, $line);
     }
     return $values;
 }