<?php
final class Tag {
	private static $cache = array();
	private static $getInitial = array();
	/**
	 * 设置对象
	 *
	 * @access private
	 * @param  $model string
	 * @param  $dir string
	 * @param  $init mix
	 * @return obj
	 */
	public function getObj($model, $dir = '', $init = '') {
		isset(self :: $getInitial[$model]) or self :: $getInitial[$model] = M($model, $dir, $init);
		return self :: $getInitial[$model];
	}
	// 生成令牌验证表单
	public function tokenForm($formName = 'info') {
		$name = C('TOKEN_NAME', 1);
		$hash = Func :: getHash($name . TIME . mt_rand(10, 99));
		$index = $name . substr($hash, 0, 4);
		$index = strtoupper($index);
		isset($_SESSION) or session_start();
		$_SESSION[$index] = $hash;
		$formName && $name = $formName . '[' . $name . ']';
		return "<input type=\"hidden\" name=\"$name\" value=\"$hash\" />";
	}
	/**
	 * 返回一个以$start开始,$end为结束的整数数组,用于模板中循环
	 *
	 * @access public
	 * @param  $start
	 * @param  $end
	 * @return array
	 */
	public function intArray($start, $end) {
		$array = array();
		for($i = $start; $i <= $end; $i++) {
			$array[] = $i;
		}
		return $array;
	}
	// 计算 参数:数学表达式 功能: 把表达式的结果输出 例子: {:Tag::compute(1+2)} 输出3
	public function compute($param) {
		return $param;
	}
	/**
	 * 返回是否选中(下拉选择框)
	 *
	 * @access public
	 * @param  $var1
	 * @param  $var2
	 * @return string $var1等于$var2即选中
	 */
	public function selected($var1, $var2) {
		return $var1 == $var2 ? ' selected' : '';
	}
	/**
	 * 返回是否选中(单选、复选框)
	 *
	 * @access public
	 * @param  $var1
	 * @param  $var2
	 * @return string $var1等于$var2即选中
	 */
	public function checked($var1, $var2) {
		if ($var2 === 'notnull') $tmp = $var1 != '' ? ' checked' : '';
		else $tmp = $var1 == $var2 ? ' checked' : '';
		return $tmp;
	}
	/**
	 * 是否显示
	 *
	 * @access public
	 * @param  $var1
	 * @param  $var2
	 * @param  $str 内容,为空的话用于HTML是否显示
	 * @return string $var1等于$var2即为空(不隐藏)
	 */
	public function isShow($var1, $var2, $str = false) {
		if ($str === false) {
			return $var1 == $var2?'':' style="display:none"';
		} else {
			return $var1 == $var2?$str:'';
		}
	}
	/**
	 * 返回下拉框选项
	 *
	 * @access public
	 * @param  $str string or other or array 例如:'1_str1|2_str2|3_str3','1_str1'表示option的value为1,text为str1;'year:50'表示最近50年,'day:时间戳';
	 * @param  $selectid 被选中的ID
	 * @param  $html 待生产选项的HTML格式
	 * @return string
	 */
	public function option($str, $selectid = '', $html = '<option value=\"$id\"$selected>$name</option>') {
		if (is_array($str)) {
			$isArr = true;
			$array = $str;
		} elseif (strstr($str, ':')) {
			$isArr = true;
			$tmp = explode(':', $str);
			$array = array();
			switch ($tmp[0]) {
				case 'year':
					$tmp[1] = intval($tmp[1]);
					$str = date('Y', TIME);
					for($i = 0; $i < $tmp[1]; $i++) {
						$n = $str - $i;
						$array[$n] = $n;
					}
					break;
				case 'day':
					$tmp[1] = intval($tmp[1]);
					$tmp[1] = date('t', $tmp[1]);
					for($i = 1; $i <= $tmp[1]; $i++) {
						$array[$i] = $i;
					}
			}
		} else {
			$isArr = false;
			$array = explode('|', $str);
		}
		$str = '';
		foreach($array as $k => $v) {
			if ($isArr) {
				$id = $k;
				$name = $v;
			} else {
				$tmp = explode('_', $v);
				$id = $tmp[0];
				$name = isset($tmp[1]) ? $tmp[1] : $tmp[0];
			}
			$selected = $id == $selectid ? ' selected' : '';
			eval("\$rs = \"$html\";");
			$str .= $rs;
		}
		return $str;
	}
	/**
	 * 返回下拉框选项
	 *
	 * @access public
	 * @param  $html 待产生选项的HTML格式 例如:"<option value='\$catid'\$selected>\$tab\$name</option>"
	 * @param  $selectid 被选中的ID
	 * @param  $array 数组
	 * @param  $pid 根分类ID
	 * @param  $type 栏目类型 例如:单网页、内部栏目
	 * @param  $checkBigPost 检查大栏目是否允许发布
	 * @return string
	 */
	public function tree($html, $selectId = 0, $array = array(), $pid = 0, $type = 0, $checkBigPost = false) {
		if (empty($array)) $array = Cache :: read('category');
		$treeObj = new Tree($array, $type, $checkBigPost, $selectId);
		return $treeObj -> getTree($pid, $html, $selectId);
	}
	/**
	 * 根据时间戳记返回需要显示的格式
	 *
	 * @access public
	 * @param  $time 时间戳记
	 * @param  $case 显示类型
	 * @return string
	 */
	public function date($time, $case = 1) {
		if (empty($time))return '';
		switch ($case) {
			case 1:
				$time = date("Y-m-d", $time);
				break;
			case 2:
				$time = date("Y-m-d H:i", $time);
				break;
			case 3:
				$time = date("Y-m-d H:i:s", $time);
				break;
			case 4:
				$seconds = TIME - $time;
				if (TIME > $time) {
					$fontBack = L('TIME_FONT');
					$seconds = TIME - $time;
				} else {
					$fontBack = L('TIME_BACK');
					$seconds = $time - TIME;
				}
				$days = $hours = $time = '';
				$days = floor($seconds / 86400);
				if ($days > 0) {
					$seconds -= $days * 86400;
					$time .= $days . L('TIME_DAY') . ' ';
				}
				$hours = floor($seconds / 3600);
				if ($hours > 0) {
					$seconds -= $hours * 3600;
				}
				$time .= $hours . L('TIME_HOUR') . ' ';
				$time .= floor($seconds / 60) . L('TIME_SECOND') . ' ' . $fontBack;
				break;
		}
		return $time;
	}
	/**
	 * 返回编辑器
	 *
	 * @access public
	 * @param  $
	 * @return string
	 */
	public function editor($id, $name, $content, $module, $height = 450, $width = '100%', $param = '') {
		if (!isset(self :: $getInitial['isLoadEditorJs'])) {
			self :: $getInitial['isLoadEditorJs'] = true;
			$str = '<script type="text/javascript" charset="utf-8" src="' . C('APP_DIR', 1, 'root') . 'statics/xheditor/xheditor-zh-cn.min.js"></script>';
		} else $str = '';
		$updo = C('APP_DIR', 1, 'root') . "index.php?c=common&a=upload&immediate=1&module={$module}&type=";
		$jsarr = array('tools' => 'tools: "full"');
		$jsarr['file'] = 'upLinkUrl:"' . $updo . 'file",upLinkExt:"' . str_replace('|', ',', C('uploadfile', 0, 'attachment')) . '"';
		$jsarr['img'] = 'upImgUrl:"' . $updo . 'image",upImgExt:"' . str_replace('|', ',', C('uploadimage', 0, 'attachment')) . '"';
		$jsarr['flash'] = 'upFlashUrl:"' . $updo . 'flash", upFlashExt:"' . str_replace('|', ',', C('uploadflash', 0, 'attachment')) . '"';
		$jsarr['media'] = 'upMediaUrl:"' . $updo . 'media",upMediaExt:"' . str_replace('|', ',', C('uploadmedia', 0, 'attachment')) . '"';
		if (!defined('IN_ADMIN')) {
			$jsarr['tools'] = 'tools: "mini"';
			if (!C('allowuploadfile', 0, 'attachment') or defined('NOT_IN_ROOT')) unset($jsarr['file']);
			if (!C('allowuploadimage', 0, 'attachment') or defined('NOT_IN_ROOT')) unset($jsarr['img']);
			if (!C('allowuploadflash', 0, 'attachment') or defined('NOT_IN_ROOT')) unset($jsarr['flash']);
			if (!C('allowuploadmedia', 0, 'attachment') or defined('NOT_IN_ROOT')) unset($jsarr['media']);
		}
		$jsstr = '{' . implode(',', $jsarr) . '}';
		$str .= "<script type=\"text/javascript\">\$(function(){\$('#{$id}').xheditor({$jsstr});});</script><textarea id=\"{$id}\" name=\"{$name}\" style=\"width:{$width};height:{$height}px;\" {$param}>{$content}</textarea>";
		return $str;
	}
	// 字符串截取 参数: 字符串,截取视觉长度(符合中国习惯，UTF8、GBK中文均相当于两个英文字符),$usedot,$dot
	public function cut($string, $length, $usedot = 1, $dot = '...') {
		$usedot or $dot = '';
		$isutf8 = strtolower(C('APP_CHARSET', 1)) == 'utf-8'?1:0;
		$strlen = $isutf8?Func :: utf8Strlen($string):strlen($string);
		if ($strlen <= $length) return $string;
		$string = str_replace(array(' ', '&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array('∵', ' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
		$strcut = '';
		$length = $length - strlen($dot);
		if ($isutf8) {
			$n = $tn = $noc = 0;
			while ($n < strlen($string)) {
				$t = ord($string[$n]);
				if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
					$tn = 1;
					$n++;
					$noc++;
				} elseif (194 <= $t && $t <= 223) {
					$tn = 2;
					$n += 2;
					$noc += 2;
				} elseif (224 <= $t && $t <= 239) {
					$tn = 3;
					$n += 3;
					$noc += 2;
				} elseif (240 <= $t && $t <= 247) {
					$tn = 4;
					$n += 4;
					$noc += 2;
				} elseif (248 <= $t && $t <= 251) {
					$tn = 5;
					$n += 5;
					$noc += 2;
				} elseif ($t == 252 || $t == 253) {
					$tn = 6;
					$n += 6;
					$noc += 2;
				} else {
					$n++;
				}
				if ($noc >= $length) {
					break;
				}
			}
			if ($noc > $length) {
				$n -= $tn;
			}
			$strcut = substr($string, 0, $n);
			$strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), $strcut);
		} else {
			$current_str = '';
			$search_arr = array('&', ' ', '"', "'", '“', '”', '—', '<', '>', '·', '…', '∵');
			$replace_arr = array('&amp;', '&nbsp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;', ' ');
			$search_flip = array_flip($search_arr);
			for ($i = 0; $i < $length; $i++) {
				$current_str = ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];
				if (in_array($current_str, $search_arr)) {
					$key = $search_flip[$current_str];
					$current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
				}
				$strcut .= $current_str;
			}
		}
		return $strcut . $dot;
	}
	public function description($content, $len = '') {
		$len or $len = C('cutedeslen');
		$content = strip_tags($content);
		return self :: cut($content, $len);
	}
	// 获取位置(面包屑导航)
	public function position($catid = 0, $no = '', $index = '') {
		$catid or $catid = R('catid');
		$name = 'position_' . $catid;
		isset(self :: $cache[$name]) or self :: $cache[$name] = self :: getObj('category') -> position($catid);
		$rs = self :: $cache[$name];
		if (is_numeric($no)) {
			if (isset($rs[$no])) {
				$rs = $rs[$no];
				if ($index) $rs = isset($rs[$index])?$rs[$index]:'';
			} else $rs = '';
		}
		return $rs;
	}
	public function area($sign) {
		$rs = array();
		if ($sign) {
			$pid = is_numeric($sign)?$sign:0;
			$rs = Cache :: read($sign, 'area');
			if (empty($rs)) {
				$areas = Cache :: read('area');
				foreach($areas as $area) {
					if ($pid) {
						if ($area['pid'] == $pid) {
							if ($area['strong']) $area['name'] = '<strong>' . $area['name'] . '</strong>';
							if ($area['color']) $area['name'] = '<span style="color:' . $area['color'] . '">' . $area['name'] . '</span>';
							$rs[] = $area;
						}
					} else {
						if ($area['sign'] == $sign) $pid = $area['areaid'];
					}
				}
				Cache :: write($sign, $rs, 'area');
			}
		}
		return $rs;
	}
	// 调用广告 参数说明：广告英文名
	public function ad($name) {
		isset(self :: $cache['ad']) or self :: $cache['ad'] = Cache :: read('ad');
		$ad = self :: $cache['ad'][$name];
		if (is_array($ad)) {
			if ($ad['usetype']) $str = '<script type="text/javascript" src="' . C('APP_DIR', 1, 'root') . 'api/cps.php?name=' . $name . '"></script>';
			else $str = self :: getObj('ad') -> makeAdCode($ad, false);
		} else $str = '';
		return $str;
	}
	// 调用区块 参数说明：区块名称,去除html,长度
	public function block($filename, $nohtml = 0, $length = 0) {
		$data = self :: getObj('onepage') -> field('content') -> where('filename=' . $filename) -> find();
		$rs = $data['content'];
		if ($nohtml) {
			$rs = str_replace("<br>", "\n", $rs);
			$rs = str_replace("<br />", "\n", $rs);
			$rs = strip_tags($rs);
			$rs = str_replace("\r\n", "<br>", $rs);
			$rs = str_replace("\n", "<br>", $rs);
		}
		if ($length) $rs = self :: cut($rs, $length);
		return $rs;
	}
	// 菜单 参数:$catid 当前分类ID; $pid 分类ID; 模块(ID或英文名称,0表示不限制模型); 在不存在子分类的情况下,是否返回同级分类
	public function menu($catid = 0, $pid = 0, $module = 0, $getSibling = false) {
		$child = self :: getObj('category') -> child($pid, 9, $module);
		if (empty($child) && $getSibling) {
			$pid = get_category($pid, 'pid');
			$child = self :: getObj('category') -> child($pid, 9, $module);
		}
		$rs = array();
		foreach($child as $k => $v) {
			if ($v['showtype'] == 1 or $v['showtype'] == 2) {
				$active = self :: position($catid, $v['level'], 'catid') == $v['catid']?1:0;
				$rs[] = array('catid' => $v['catid'], 'name' => $v['name'], 'url' => Url :: getListUrl($v['catid']), 'active' => $active, 'elite' => $v['elite'], 'subnum' => $v['subnum'], 'type' => $v['type']);
			}
		}
		return $rs;
	}
	// 多列列表
	public function listMore($params) {
		$params = self :: getParam($params);
		if (isset($params['page'])) unset($params['page']);
		$model = isset($params['model'])?$params['model']:0;
		$catid = isset($params['catid'])?intval($params['catid']):0;
		$cacheFile = self :: getCacheName($params);
		$data = Cache :: read($cacheFile['name'], $cacheFile['dir'], C('CACHE_TAGS_TTL', 1), $cacheFile['defaultDir']);
		if ($data === false) {
			$module = get_module();
			$child = self :: getObj('category') -> child($catid, 0, $model);
			foreach($child as $tmp) {
				if ($tmp['showtype'] == 1 or $tmp['showtype'] == 3) {
					$params['catid'] = $tmp['catid'];
					$list = self :: get('listInfo', $params, self :: getObj($module[$tmp['moduleid']]['name']));
					$data[] = array('name' => $tmp['name'], 'catid' => $tmp['catid'], 'url' => Url :: getListUrl($tmp['catid']), 'subnum' => $tmp['subnum'], 'list' => $list);
				}
			}
			if (C('CACHE_TAGS_TTL', 1) && isset($cacheFile)) Cache :: write($cacheFile['name'], $data, $cacheFile['dir'], $cacheFile['defaultDir']);
		}
		return $data;
	}
	// 单列列表
	public function listOne($params) {
		$params = self :: getParam($params);
		if (empty($params['model']) && !empty($params['catid'])) {
			$params['model'] = get_category($params['catid'], 'moduleid');
		}
		is_numeric($params['model']) && $params['model'] = get_module($params['model'], 'name');
		if (isset($params['page'])) $params['page'] = max(intval($params['page']), 1);
		$type = isset($params['type'])?$params['type']:'';
		if ($type != 'like') {
			$cacheFile = self :: getCacheName($params);
			$data = Cache :: read($cacheFile['name'], $cacheFile['dir'], C('CACHE_TAGS_TTL', 1), $cacheFile['defaultDir']);
		} else $data = false;
		if (defined('IN_CREATEHTML') && isset($params['page']) && $params['page'] == 1) $data = false;
		if ($data === false) {
			$obj = self :: getObj($params['model']);
			$data = self :: get('listInfo', $params, $obj);
			$pages = $obj -> pages(Url :: getListUrl($params['catid'], '{page}'), Url :: getListUrl($params['catid']));
			if (isset($params['page'])) $data = array('list' => $data, 'pages' => $pages, 'totle' => $obj -> totle);
			if (C('CACHE_TAGS_TTL', 1) && isset($cacheFile)) Cache :: write($cacheFile['name'], $data, $cacheFile['dir'], $cacheFile['defaultDir']);
		}
		return $data;
	}
	// 上一篇/下一篇
	public function near($id, $catid, $flag = 'next') {
		$id = intval($id);
		$catid = intval($catid);
		$model = get_category($catid, 'moduleid');
		$model = get_module($model, 'name');
		$where = array('passed=1', 'recycle=0');
		if ($flag == 'next') {
			$where[] = 'id>' . $id;
			$order = 'id ASC';
		} else {
			$where[] = 'id<' . $id;
			$order = 'id DESC';
		}
		$flag = $flag == 'next'?'>':'<';
		if ($catid) $where[] = 'catid=' . $catid;
		$data = self :: getObj($model) -> field(array('id', 'title')) -> where($where) -> order($order) -> find();
		if ($data) {
			$data = '<a href="' . Url :: getContentUrl($model, $data['id']) . '">' . $data['title'] . '</a>';
		}
		return $data;
	}
	/**
	 * 获取缓存名称
	 *
	 * @access private
	 * @param  $params array
	 * @return array
	 */
	private function getCacheName($params) {
		$keys = array_keys($params);
		sort($keys);
		$value = array();
		foreach($keys as $k => $v) {
			if ($v == 'schoolid') {
				$defaultDir = 'School_' . Func :: createPrefix($params['schoolid']);
				unset($keys[$k]);
				$dir = $params['schoolid'];
			} else $value[] = $params[$v];
		}
		$value = implode('_', $value);
		if (empty($dir)) {
			$dir = empty($params['catid'])?'base':$params['catid'];
			$defaultDir = 'Cache';
		}
		return array('name' => implode('_', $keys) . '_' . md5($value), 'dir' => $dir, 'defaultDir' => $defaultDir);
	}

	/**
	 * 分析参数
	 *
	 * @access private
	 * @param  $param string
	 * @return array
	 */
	private function getParam($param) {
		$param = str_replace("\'", '', $param); //去除"\'",而不是'\''
		$param = str_replace('\"', '', $param); //去除'\"',而不是"\""
		$head = '{_' . mt_rand() . '_base64_}';
		$len = strlen($head);
		$param = preg_replace('/("|\')(.*?)\\1/e', "'$head' . base64_encode('\\2')", $param);
		$a = explode(',', $param);
		$params = array();
		foreach($a as $v) {
			$t = explode('=', $v, 2);
			$t[0] = trim($t[0]);
			if (substr($t[1], 0, $len) == $head) {
				$p = substr($t[1], $len);
				if ($p) {
					$p = base64_decode($p);
					$p = stripslashes($p);
					$p = str_replace('"', '', $p);
					$params[$t[0]] = $p;
				} else $params[$t[0]] = '';
			} else $params[$t[0]] = $t[1];
		}
		return $params;
	}
	/**
	 * 获取数据
	 *
	 * @access private
	 * @param  $action string
	 * @param  $params array
	 * @return mix
	 */
	private function get($action, $params, $obj = '') {
		if (is_string($params)) $params = self :: getParam($param);
		if (!is_object($obj)) {
			if (empty($params['model'])) show_error(L('MODEL_CALL_UNDEFINE'));
			$obj = self :: getObj($params['model']);
		}
		if (isset($params['model'])) unset($params['model']);
		if (method_exists($obj, $action)) {
			return $obj -> $action($params);
		} else show_error($action . ' ' . L('ERROR_ACTION'));
	}
	public function img($img, $default = 'statics/images/noimage.jpg') {
		$img or $img = $default;
		strstr($img, '://') or $img = C('APP_DIR', 1, 'root') . $img;
		return $img;
	}
	public function bigImg($img) {
		if (strstr($img, '_thumb')) $img = str_replace('_thumb', '', $img);
		return self :: img($img);
	}
	// 根据需要生成缩略图
	public function thumb($img, $width, $height) {
		$img = self :: bigImg($img);
		$root = C('APP_DIR', 1, 'root');
		$len = strlen($root);
		$tmp = substr($img, 0, $len);
		if ($tmp == $root) {
			$tmp = substr($img, $len);
			$thumb = str_replace('.', "_{$width}_{$height}.", $tmp);
			if (is_file($thumb) or self :: getObj('attachment') -> imageResize($tmp, $width, $height, $thumb)) $img = $root . $thumb;
		}
		return $img;
	}
	// 获取附件
	public function attachment($module, $id, $isImage = 0) {
		if (empty($module) or empty($id)) return false;
		$attachments = self :: getObj('attachment', '', $module) -> where(array('isattach' => 1, 'module' => $module, 'contentid' => $id)) -> select();
		if ($isImage) {
			$thumbs = array();
			foreach($attachments as $tmp) {
				if ($tmp['isimage'])$thumbs[] = $tmp['filepath'];
			}
			return $thumbs;
		}
		return $attachments;
	}
	// 头像
	public function avatar($uid, $size = 'm') {
		in_array($size, array('b', 'm', 's')) or $size = 'm';
		$fUid = sprintf("%09d", $uid);
		$avatar = C('uploaddir', 0, 'attachment') . '/avatar/' . substr($fUid, 0, 3) . '/' . substr($fUid, 3, 2) . '/' . substr($fUid, 5, 2) . '/';
		$avatar .= $uid . '_avatar_' . $size . '.jpg';
		file_exists(JXCMS_ROOT . $avatar) or $avatar = 'statics/images/default_avatar_' . $size . '.jpg';
		return C('APP_DIR', 1, 'root') . $avatar;
	}
	// 友情链接 参数:数量,类型 0表示全部,1表示logo链接,2表示文本链接
	public function link($num = 20, $type = 0) {
		isset(self :: $cache['link']) or self :: $cache['link'] = Cache :: read('link');
		if (!is_array(self :: $cache['link'])) return '';
		$rs1 = $rs2 = '';
		foreach(self :: $cache['link'] as $key => $link) {
			if ($link['linktype']) {
				$rs1 .= '<a href="' . $link['url'] . '" title="' . $link['introduce'] . '" target="_blank"><img src="' . $link['logo'] . '" width="88" height="31" border="0"></a> ';
			} else {
				$rs2 .= '<a href="' . $link['url'] . '" title="' . $link['introduce'] . '" target="_blank">' . $link['name'] . '</a> ';
			}
			if ($num - 1 == $key) break;
		}
		switch ($type) {
			case 1:
				$res = $rs1;
				break;
			case 2:
				$res = $rs2;
				break;
			default:
				$res = $rs1 ? $rs1 . '<br />' . $rs2 : $rs2;
				break;
		}
		return $res;
	}
	// 关键词生成标签
	public function tags($keywords, $model) {
		$keywords = explode(',', $keywords);
		$array = array();
		foreach($keywords as $keyword) {
			$array[] = '<a href="' . C('APP_DIR', 1, 'root') . 'index.php?a=search&c=' . $model . '&q=' . urlencode($keyword) . '">' . $keyword . '</a>';
		}
		return implode(' ', $array);
	}
	// 热门关键词 参数:数量
	public function hotKeywords($num, $module = '') {
		$name = $module?'hotKeywords_' . $module:'hotKeywords';
		$data = Cache :: read($name, 'base', C('CACHE_TAGS_TTL', 1));
		if ($data === false) {
			$obj = self :: getObj('keywords');
			if ($module) $obj -> where('module=' . $module);
			$data = $obj -> field(array('word', 'module', 'num')) -> order('num DESC') -> limit($num) -> select();
			if (C('CACHE_TAGS_TTL', 1)) Cache :: write($name, $data, 'base');
		}
		if (count($data) > $num) {
			$tmp = array();
			for($i = 0; $i < $num; $i++) {
				$tmp[] = $data[$i];
			}
			$data = $tmp;
		}
		return $data;
	}
	public function power() {
		if (C('APP_POWER', 1) == 2) $power = 'Memory:' . get_run_memory() . ' Time:' . get_run_time();
		elseif (C('APP_POWER', 1) == 1) $power = 'Time:' . get_run_time();
		else $power = '';
		return $power;
	}
}
