query("SELECT `keyname`,`value` FROM hlstats_Options WHERE opttype >= 1");
	while ($rowdata = $db->fetch_row($result))
	{
		$options[$rowdata[0]] = $rowdata[1];
	}
	if ( !count($options) )
	{
		error('Warning: Could not find any options in table hlstats_Options, database ' .
			DB_NAME . '. Check HLstats configuration.');
	}
	$options['MinActivity'] = $options['MinActivity'] * 86400;
	return $options;
}
// Test if flags exists
/**
 * getFlag()
 * 
 * @param string $flag
 * @param string $type
 * @return string Either the flag or default flag if none exists
 */
function getFlag($flag, $type='url')
{
	$image = getImage('/flags/'.strtolower($flag));
	if ($image)
		return $image[$type];
	else
		return IMAGE_PATH.'/flags/0.gif';
}
/**
 * valid_request()
 * 
 * @param string $str
 * @param boolean $numeric
 * @return mixed request
 */
function valid_request($str, $numeric = false)
{
	$search_pattern = array("/[^A-Za-z0-9\[\]*.,=()!\"$%&^`´':;߲ł#+~_\-|<>\/\\\\@{}äöüÄÖÜ ]/");
	$replace_pattern = array('');
	$str = preg_replace($search_pattern, $replace_pattern, $str);
	if ( $numeric == false )
	{
		if ( get_magic_quotes_gpc() )
			return $str = htmlspecialchars(stripslashes($str), ENT_QUOTES);
		else
			return $str = htmlspecialchars($str, ENT_QUOTES);
	}
	else
	{
		if ( is_numeric($str) )
			return intval($str);
		else
			return -1;
	}
}
/**
 * timestamp_to_str()
 * 
 * @param integer $timestamp
 * @return string Formatted Timestamp
 */
function timestamp_to_str($timestamp)
{
	if ($timestamp != '')
	{
		return sprintf('%dd %02d:%02d:%02dh', $timestamp / 86400, $timestamp / 3600 % 24, $timestamp /
			60 % 60, $timestamp % 60);
	}
	return '-';
}
/**
 * error()
 * Formats and outputs the given error message. Optionally terminates script
 * processing.
 * 
 * @param mixed $message
 * @param bool $exit
 * @return void
 */
function error($message, $exit = true)
{
	global $g_options;
?>
 $v)
	{
		$v = valid_request($v, 0);
		if ($k && $k != $key && !in_array($k, $notkeys))
		{
			$querystring .= urlencode($k) . '=' . rawurlencode($v) . '&';
		}
	}
	$querystring .= urlencode($key) . '=' . urlencode($value);
	return $querystring;
}
//
// void pageHeader (array title, array location)
//
// Prints the page heading.
//
/**
 * pageHeader()
 * 
 * @param mixed $title
 * @param mixed $location
 * @return
 */
function pageHeader($title = '', $location = '')
{
	global $db, $g_options;
	if ( defined('PAGE') && PAGE == 'HLSTATS' )
		include (PAGE_PATH . '/header.php');
	elseif ( defined('PAGE') && PAGE == 'INGAME' )
		include (PAGE_PATH . '/ingame/header.php');
}
//
// void pageFooter (void)
//
// Prints the page footer.
//
/**
 * pageFooter()
 * 
 * @return
 */
function pageFooter()
{
	global $g_options;
	if ( defined('PAGE') && PAGE == 'HLSTATS' )
		include (PAGE_PATH . '/footer.php');
	elseif ( defined('PAGE') && PAGE == 'INGAME' )
		include (PAGE_PATH . '/ingame/footer.php');
}
/**
 * getSortArrow()
 * 
 * @param mixed $sort
 * @param mixed $sortorder
 * @param mixed $name
 * @param mixed $longname
 * @param string $var_sort
 * @param string $var_sortorder
 * @param string $sorthash
 * @return string Returns the code for a sort arrow ![]() tag.
 */
function getSortArrow($sort, $sortorder, $name, $longname, $var_sort = 'sort', $var_sortorder =
	'sortorder', $sorthash = '', $ajax = false)
{
	global $g_options;
	if ($sortorder == 'asc')
	{
		$sortimg = 'sort-ascending.gif';
		$othersortorder = 'desc';
	}
	else
	{
		$sortimg = 'sort-descending.gif';
		$othersortorder = 'asc';
	}
	
	$arrowstring = '";
	if ($sort == $name)
	{
		$arrowstring .= '
 tag.
 */
function getSortArrow($sort, $sortorder, $name, $longname, $var_sort = 'sort', $var_sortorder =
	'sortorder', $sorthash = '', $ajax = false)
{
	global $g_options;
	if ($sortorder == 'asc')
	{
		$sortimg = 'sort-ascending.gif';
		$othersortorder = 'desc';
	}
	else
	{
		$sortimg = 'sort-descending.gif';
		$othersortorder = 'asc';
	}
	
	$arrowstring = '";
	if ($sort == $name)
	{
		$arrowstring .= '  ";
	}
	return $arrowstring;
}
/**
 * getSelect()
 * Returns the HTML for a SELECT box, generated using the 'values' array.
 * Each key in the array should be a OPTION VALUE, while each value in the
 * array should be a corresponding descriptive name for the OPTION.
 * 
 * @param mixed $name
 * @param mixed $values
 * @param string $currentvalue
 * @return The 'currentvalue' will be given the SELECTED attribute.
 */
function getSelect($name, $values, $currentvalue = '')
{
	$select = "';
	return $select;
}
/**
 * getLink()
 * 
 * @param mixed $url
 * @param integer $maxlength
 * @param string $type
 * @param string $target
 * @return
 */
 
function getLink($url, $type = 'http://', $target = '_blank')
{
	$urld=parse_url($url);
	if(!isset($urld['scheme']) && (!isset($urld['host']) && isset($urld['path'])))
	{
			$urld['scheme']=str_replace('://', '', $type);
			$urld['host']=$urld['path'];
			unset($urld['path']);
	}
	if($urld['scheme']!='http' && $urld['scheme']!='https')
	{
			return 'Invalid Url :(';
	}
	if(!isset($urld['path']))
	{
			$urld['path']='';
	}
	if(!isset($urld['query']))
	{
			$urld['query']='';
	}
	else
	{
			$urld['query']='?' . urlencode($urld['query']);
	}
	if(!isset($urld['fragment']))
	{
			$urld['fragment']='';
	}
	else
	{
			$urld['fragment']='#' . urlencode($urld['fragment']);
	}
	$uri=sprintf("%s%s%s", $urld['path'], $urld['query'], $urld['fragment']);
	$host_uri=$urld['host'] . $uri;
	return sprintf('%s',$urld['scheme'], $urld['host'], $uri, $target, htmlspecialchars($host_uri, ENT_COMPAT));
}
/**
 * getEmailLink()
 * 
 * @param string $email
 * @param integer $maxlength
 * @return string Formatted email tag
 */
function getEmailLink($email, $maxlength = 40)
{
	if (preg_match('/(.+)@(.+)/', $email, $regs))
	{
		if (strlen($email) > $maxlength)
		{
			$email_title = substr($email, 0, $maxlength - 3) . '...';
		}
		else
		{
			$email_title = $email;
		}
		$email = str_replace('"', urlencode('"'), $email);
		$email = str_replace('<', urlencode('<'), $email);
		$email = str_replace('>', urlencode('>'), $email);
		return "" . htmlspecialchars($email_title, ENT_COMPAT) . '';
	}
	else
	{
		return '';
	}
}
/**
 * getImage()
 * 
 * @param string $filename
 * @return mixed Either the image if exists, or false otherwise
 */
function getImage($filename)
{
	preg_match('/^(.*\/)(.+)$/', $filename, $matches);
	$relpath = $matches[1];
	$realfilename = $matches[2];
	
	$path = IMAGE_PATH . $filename;
	$url = IMAGE_PATH . $relpath . rawurlencode($realfilename);
	// check if image exists
	if (file_exists($path . '.png'))
	{
		$ext = 'png';
	} elseif (file_exists($path . '.gif'))
	{
		$ext = 'gif';
	} elseif (file_exists($path . '.jpg'))
	{
		$ext = 'jpg';
	}
	else
	{
		$ext = '';
	}
	if ($ext)
	{
		$size = getImageSize("$path.$ext");
		return array('url' => "$url.$ext", 'path' => "$path.$ext", 'width' => $size[0], 'height' => $size[1],
			'size' => $size[3]);
	}
	else
	{
		return false;
	}
}
function mystripslashes($text)
{
	return get_magic_quotes_gpc() ? stripslashes($text) : $text;
}
function getRealGame($game)
{
	global $db;
	$result = $db->query("SELECT realgame from hlstats_Games WHERE code='$game'");
	list($realgame) = $db->fetch_row($result);
	return $realgame;
}
function printSectionTitle($title)
{
	echo '
";
	}
	return $arrowstring;
}
/**
 * getSelect()
 * Returns the HTML for a SELECT box, generated using the 'values' array.
 * Each key in the array should be a OPTION VALUE, while each value in the
 * array should be a corresponding descriptive name for the OPTION.
 * 
 * @param mixed $name
 * @param mixed $values
 * @param string $currentvalue
 * @return The 'currentvalue' will be given the SELECTED attribute.
 */
function getSelect($name, $values, $currentvalue = '')
{
	$select = "';
	return $select;
}
/**
 * getLink()
 * 
 * @param mixed $url
 * @param integer $maxlength
 * @param string $type
 * @param string $target
 * @return
 */
 
function getLink($url, $type = 'http://', $target = '_blank')
{
	$urld=parse_url($url);
	if(!isset($urld['scheme']) && (!isset($urld['host']) && isset($urld['path'])))
	{
			$urld['scheme']=str_replace('://', '', $type);
			$urld['host']=$urld['path'];
			unset($urld['path']);
	}
	if($urld['scheme']!='http' && $urld['scheme']!='https')
	{
			return 'Invalid Url :(';
	}
	if(!isset($urld['path']))
	{
			$urld['path']='';
	}
	if(!isset($urld['query']))
	{
			$urld['query']='';
	}
	else
	{
			$urld['query']='?' . urlencode($urld['query']);
	}
	if(!isset($urld['fragment']))
	{
			$urld['fragment']='';
	}
	else
	{
			$urld['fragment']='#' . urlencode($urld['fragment']);
	}
	$uri=sprintf("%s%s%s", $urld['path'], $urld['query'], $urld['fragment']);
	$host_uri=$urld['host'] . $uri;
	return sprintf('%s',$urld['scheme'], $urld['host'], $uri, $target, htmlspecialchars($host_uri, ENT_COMPAT));
}
/**
 * getEmailLink()
 * 
 * @param string $email
 * @param integer $maxlength
 * @return string Formatted email tag
 */
function getEmailLink($email, $maxlength = 40)
{
	if (preg_match('/(.+)@(.+)/', $email, $regs))
	{
		if (strlen($email) > $maxlength)
		{
			$email_title = substr($email, 0, $maxlength - 3) . '...';
		}
		else
		{
			$email_title = $email;
		}
		$email = str_replace('"', urlencode('"'), $email);
		$email = str_replace('<', urlencode('<'), $email);
		$email = str_replace('>', urlencode('>'), $email);
		return "" . htmlspecialchars($email_title, ENT_COMPAT) . '';
	}
	else
	{
		return '';
	}
}
/**
 * getImage()
 * 
 * @param string $filename
 * @return mixed Either the image if exists, or false otherwise
 */
function getImage($filename)
{
	preg_match('/^(.*\/)(.+)$/', $filename, $matches);
	$relpath = $matches[1];
	$realfilename = $matches[2];
	
	$path = IMAGE_PATH . $filename;
	$url = IMAGE_PATH . $relpath . rawurlencode($realfilename);
	// check if image exists
	if (file_exists($path . '.png'))
	{
		$ext = 'png';
	} elseif (file_exists($path . '.gif'))
	{
		$ext = 'gif';
	} elseif (file_exists($path . '.jpg'))
	{
		$ext = 'jpg';
	}
	else
	{
		$ext = '';
	}
	if ($ext)
	{
		$size = getImageSize("$path.$ext");
		return array('url' => "$url.$ext", 'path' => "$path.$ext", 'width' => $size[0], 'height' => $size[1],
			'size' => $size[3]);
	}
	else
	{
		return false;
	}
}
function mystripslashes($text)
{
	return get_magic_quotes_gpc() ? stripslashes($text) : $text;
}
function getRealGame($game)
{
	global $db;
	$result = $db->query("SELECT realgame from hlstats_Games WHERE code='$game'");
	list($realgame) = $db->fetch_row($result);
	return $realgame;
}
function printSectionTitle($title)
{
	echo '  $title
 $title
\n";
}
function getStyleText($style)
{
	return "\t\n";
}
function getJSText($js)
{
	return "\t \n";
}
function get_player_rank($playerdata) {
	global $db, $g_options;
	
	$rank = 0;
	$tempdeaths = $playerdata['deaths'];
	if ($tempdeaths == 0)
		$tempdeaths = 1;
	$query = "
		SELECT
			COUNT(*)
		FROM
			hlstats_Players
		WHERE
			game='".$playerdata['game']."'
			AND hideranking = 0
			AND (
					(".$g_options['rankingtype']." > '".$playerdata[$g_options['rankingtype']]."') OR (
						(".$g_options['rankingtype']." = '".$playerdata[$g_options['rankingtype']]."') AND (kills/IF(deaths=0,1,deaths) > ".($playerdata['kills']/$tempdeaths).")
					)
			)
	";
	$db->query($query);
	list($rank) = $db->fetch_row();
	$rank++;
	return $rank;
}
if (!function_exists('file_get_contents')) {
      function file_get_contents($filename, $incpath = false, $resource_context = null)
      {
          if (false === $fh = fopen($filename, 'rb', $incpath)) {
              trigger_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING);
              return false;
          }
  
          clearstatcache();
          if ($fsize = @filesize($filename)) {
              $data = fread($fh, $fsize);
          } else {
              $data = '';
              while (!feof($fh)) {
                  $data .= fread($fh, 8192);
              }
          }
  
          fclose($fh);
          return $data;
      }
}
/**
 * Convert colors Usage:  color::hex2rgb("FFFFFF")
 * 
 * @author      Tim Johannessen 
 * @version    1.0.1
 */
function hex2rgb($hexVal = '')
{
	$hexVal = preg_replace('[^a-fA-F0-9]', '', $hexVal);
	if (strlen($hexVal) != 6)
	{
		return 'ERR: Incorrect colorcode, expecting 6 chars (a-f, 0-9)';
	}
	$arrTmp = explode(' ', chunk_split($hexVal, 2, ' '));
	$arrTmp = array_map('hexdec', $arrTmp);
	return array('red' => $arrTmp[0], 'green' => $arrTmp[1], 'blue' => $arrTmp[2]);
}
?>