<?php
// 后台管理控制器
class AdminAction extends BaseAction {
	public function defaults() {
		$this -> display('index');
	}
	public function top() {
		$this -> display('top');
	}
	public function center() {
		$this -> display('center');
	}
	public function left() {
		$tree = new Tree(Cache :: read('category'), '0,1', true);
		$this -> assign('sysmenu', array('managecontent' => $tree -> getStreeAdmin())); //菜单名称为sysmenu时,菜单地址设置成数组的下标即可
		$tmp = Cache :: read('menu', '', 0, 'Data');
		$menus = array();
		$userRoleids = array();
		foreach($this -> loginInfo['role'] as $role){
			//后台设置了菜单显示不校验的,显示全部菜单
			if(!empty($role['uncheck'])){
				$menus = $tmp;
				break;
			}
			$userRoleids[] = $role['roleid'];
		}
		if (empty($menus)) {
			foreach($tmp as $k1 => $v1) {
				foreach($v1 as $k2 => $v2) {
					if ($this -> access($userRoleids, $v2['roleids'])) $menus[$k1][$k2] = $v2;
				}
			}
		}
		$this -> assign('menus', $menus);
		$tmp = end($menus[0]);
		$this -> assign('first_end_key', $tmp['id']);
		$this -> display('left');
	}
	public function right() {
		$db = M('Model') -> db();
		$tablepre_len = strlen(C('DB_PREFIX', 1));
		$jxcms_size = 0;
		$all_size = 0;
		$query = $db -> query("SHOW TABLE STATUS");
		while ($rs = $db -> fetchArray($query)) {
			substr($rs['Name'], 0, $tablepre_len) == C('DB_PREFIX', 1) && $jxcms_size += $rs['Data_length'] + $rs['Index_length'];
			$all_size += $rs['Data_length'] + $rs['Index_length'];
		}
		$this -> assign('db_version', $db -> version());
		$this -> assign('max_upload', ini_get('file_uploads')?ini_get('upload_max_filesize'):'Disabled');
		$this -> assign('sys_mail', ini_get('sendmail_path')?'Unix Sendmail(Path:' . ini_get('sendmail_path') . ')':ini_get('SMTP')?'SMTP(Server:' . ini_get('SMTP') . ')':'Disabled');
		$this -> assign('ifcookie', isset($_COOKIE)?'SUCCESS':'FAIL');
		$this -> assign('all_size', number_format($all_size / (1024 * 1024), 2) . 'M');
		$this -> assign('jxcms_size', number_format($jxcms_size / (1024 * 1024), 2) . 'M');
		$this -> display('right');
	}
	public function down() {
		$this -> display('down');
	}
	public function debug() {
		$error = Cache :: read('debug', '', 600);
		if ($error) {
			echo '<pre>';
			print_r($error);
			echo '</pre>';
		} else {
			exit('No result');
		}
	}
	public function login() {
		if ($this -> isSubmit) {
			if (C('captcha_admin_login', 0, 'member')) {
				$captcha = new Captcha();
				$captcha -> check(trim($_POST['captcha'])) or $this -> showMsg(L('CAPTCHA_ERROR'));
			}
			$memberObj = M('member');
			$username = trim($_POST['username']);
			$password = md5(trim($_POST['password']));
			$field = array('userid', 'username', 'password', 'email', 'lastdate', 'groupid');
			$where = array('username' => $username, 'groupid' => 1);
			$r = $memberObj -> field($field) -> where($where) -> find();
			if ($r) {
				if ($r['password'] == $password) {
					// 后台登陆信息
					$loginInfo = array('userid' => $r['userid'], 'username' => $r['username'], 'email' => $r['email'], 'lastdate' => $r['lastdate'], 'groupid' => 1, 'role' => $memberObj -> getRole($r['userid']), 'hash' => Func :: getHash($username . $password));
					// 前台登陆信息
					$userInfo = array('userid' => $r['userid'], 'username' => $r['username'], 'email' => $r['email'], 'lastdate' => $r['lastdate'], 'groupid' => 1, 'password' => $password);
					M('member') -> setLogin($userInfo);
					$_SESSION[C('DB_PREFIX', 1) . 'loginInfo'] = serialize($loginInfo);
					$this -> showMsg(L('LOGIN_SUCCESS'), '?a=defaults');
				} else $this -> showMsg(L('PASSWORD_ERROR'), '?a=login');
			} else $this -> showMsg(L('USERNAME_IS_NOT_EXIST'), '?a=login');
		}
		$this -> display('login');
	}
	public function logout() {
		session_destroy();
		session_unset();
		session_write_close();
		$this -> showMsg(L('LOGOUT_SUCCESS'), '?a=login');
	}
}
