Index: trunk/htdocs/common/common.php
===================================================================
--- trunk/htdocs/cacher/common.php	(revision 1038)
+++ trunk/htdocs/common/common.php	(revision 1053)
@@ -1,4 +1,68 @@
 <?php
 
+class Cycle
+{
+	var $values;
+	var $i;
+	var $count;
+
+	function Cycle($v)
+	{
+		$this->values = func_get_args();
+		$this->i = -1;
+		$this->count = count($this->values);
+	}
+
+	function next()
+	{
+		$this->i = ($this->i + 1) % $this->count;
+		return $this->values[$this->i];
+	}
+
+	function cur()
+	{
+		return $this->values[$this->i];
+	}
+
+	function reset()
+	{
+		$this->i = -1;
+	}
+}
+
+function switcher($name, $options)
+{
+	$n = isset($_GET[$name]) ? $_GET[$name] : null;
+	$html = array();
+	foreach ($options as $k => $v) {
+		$html[] = sprintf('<a href="?%s=%s"%s>%s</a>', $name, $k, $k == $n ? ' class="active"' : '', $v);
+	}
+	return implode('', $html);
+}
+
+function moduleswitcher()
+{
+	global $module, $modules;
+	$html = array();
+	foreach ($modules as $k => $v) {
+		$html[] = sprintf('<a href="../%s/"%s>%s</a>', $k, $k == $module ? ' class="active"' : '', $v);
+	}
+	return implode('', $html);
+}
+
+function th($name, $attrs = null)
+{
+	$translated = __($name);
+	if ($translated == $name) {
+		$translated = "$name|$name";
+	}
+	list($text, $title) = explode('|', $translated, 2);
+	return sprintf('%s<th%s id="%s" title="%s"><a href="javascript:" onclick="resort(this); return false">%s</a></th>%s'
+			, "\t"
+			, $attrs ? " $attrs" : ""
+			, $name, htmlspecialchars(trim($title)), trim($text)
+			, "\n");
+}
+
 function xcache_validateFileName($name)
 {
@@ -6,34 +70,19 @@
 }
 
-function get_language_file_ex($name, $l, $s)
-{
-	static $lmap = array(
+function get_language_file_ex($name, $lang)
+{
+	static $langMap = array(
 			'zh'    => 'zh-simplified',
 			'zh-hk' => 'zh-traditional',
 			'zh-tw' => 'zh-traditional',
 			);
-	static $smap = array(
-			'gbk'     => 'gb2312',
-			'gb18030' => 'gb2312',
-			);
-
-	if (isset($lmap[$l])) {
-		$l = $lmap[$l];
-	}
-	$file = "$name-$l-$s.lang.php";
+
+	if (isset($langMap[$lang])) {
+		$lang = $langMap[$lang];
+	}
+	$file = "$name-$lang.lang.php";
 	if (xcache_validateFileName($file) && file_exists($file)) {
 		return $file;
 	}
-	if (isset($smap[$s])) {
-		$s = $smap[$s];
-		$file = "$name-$l-$s.lang.php";
-		if (xcache_validateFileName($file) && file_exists($file)) {
-			return $file;
-		}
-	}
-	$file = "$name-$l.lang.php";
-	if (xcache_validateFileName($file) && file_exists($file)) {
-		return $file;
-	}
 	return null;
 }
@@ -42,26 +91,24 @@
 {
 	global $config;
-	$s = strtolower($config['charset']);
 	if (!empty($config['lang'])) {
-		$l = strtolower($config['lang']);
-		$file = get_language_file_ex($name, $l, $s);
+		$lang = strtolower($config['lang']);
+		$file = get_language_file_ex($name, $lang);
 		if (!isset($file)) {
-			$l = strtok($l, ':-');
-			$file = get_language_file_ex($name, $l, $s);
+			$lang = strtok($lang, ':-');
+			$file = get_language_file_ex($name, $lang);
 		}
 	}
 	else if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
-		foreach (explode(',', str_replace(' ', '', $_SERVER['HTTP_ACCEPT_LANGUAGE'])) as $l) {
-			$l = strtok($l, ':;');
-			$file = get_language_file_ex($name, $l, $s);
+		foreach (explode(',', str_replace(' ', '', $_SERVER['HTTP_ACCEPT_LANGUAGE'])) as $lang) {
+			$lang = strtok($lang, ':;');
+			$file = get_language_file_ex($name, $lang);
 			if (isset($file)) {
-				$config['lang'] = $l;
+				$config['lang'] = $lang;
 				break;
 			}
-			if (strpos($l, '-') !== false) {
-				$ll = strtok($l, ':-');
-				$file = get_language_file_ex($name, $ll, $s);
+			if (strpos($lang, '-') !== false) {
+				$file = get_language_file_ex($name, strtok($lang, ':-'));
 				if (isset($file)) {
-					$config['lang'] = $l;
+					$config['lang'] = $lang;
 					break;
 				}
@@ -91,4 +138,46 @@
 {
 	return $str;
+}
+
+function number_formats($a, $keys)
+{
+	foreach ($keys as $k) {
+		$a[$k] = number_format($a[$k]);
+	}
+	return $a;
+}
+
+function size($size)
+{
+	$size = (int) $size;
+	if ($size < 1024)
+		return number_format($size, 2) . ' b';
+
+	if ($size < 1048576)
+		return number_format($size / 1024, 2) . ' K';
+
+	return number_format($size / 1048576, 2) . ' M';
+}
+
+function age($time)
+{
+	if (!$time) return '';
+	$delta = REQUEST_TIME - $time;
+
+	if ($delta < 0) {
+		$delta = -$delta;
+	}
+	
+  	static $seconds = array(1, 60, 3600, 86400, 604800, 2678400, 31536000);
+	static $name = array('s', 'm', 'h', 'd', 'w', 'M', 'Y');
+
+	for ($i = 6; $i >= 0; $i --) {
+		if ($delta >= $seconds[$i]) {
+			$ret = (int) ($delta / $seconds[$i]);
+			return $ret . ' ' . $name[$i];
+		}
+	}
+
+	return '0 s';
 }
 
@@ -121,5 +210,4 @@
 	return $list_html;
 }
-
 
 error_reporting(E_ALL);
@@ -137,5 +225,11 @@
 
 $config = array();
-include("./config.default.php");
+if (file_exists("./config.default.php")) {
+	include("./config.default.php");
+}
+include("../config.default.php");
+if (file_exists("../config.php")) {
+	include("../config.php");
+}
 if (file_exists("./config.php")) {
 	include("./config.php");
@@ -143,8 +237,12 @@
 
 include(get_language_file("common"));
-if (empty($config['lang'])) {
-	$config['lang'] = 'en-us';
-}
-
+
+$modules = array();
+if (file_exists("../cacher/index.php")) {
+	$modules["cacher"] = _("Cacher");
+}
+if (file_exists("../coverager/index.php")) {
+	$modules["coverager"] = _("Coverager");
+}
 header("Cache-Control: no-cache, must-revalidate");
 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
