<?php
/*
*/
class AdminModel extends DBModel {
	public function init(){
		if (!defined('SUCCEED_VODCMS')){
			exit();
		}
	}
	public function Login($array = null){
		if (is_array($array)){
			$select = $this->_DB->select();
			$select->from(VODCMS_ADMIN);
			$select->where('locked=0');
			$select->where($array);
			$select->limit(1);
			$sql = $select->toString();
			$data = $this->_DB->fetRow($sql);
			if ($data['id']){
				$super = new SupermanModel();
				$row = $super->getAll(array('name'=>$data['group']));
				$this->_Response->setSession('id', (int)$data['id']);
				$this->_Response->setSession('flag', (int)$row[0]['flag']);
				unset($row);
				$this->_Response->setCookie('admin', (string)$data['username'], 0, '/');
				$this->_Response->setCookie('group', (string)$data['group'], 0, '/');
				$this->_Response->setCookie('hash', md5($data['group'].$data['username'].$data['id']), 0, '/');
				$this->_DB->update(VODCMS_ADMIN, '`lastime` = `logintime`, `logintime` = '.time(), array('username'=>$array['username']));
			}else{
				$this->_DB->update(VODCMS_ADMIN, 'failed = failed+1', array('username'=>$array['username']));
			}
			return $data;
		}else{
			return false;
		}
	}
	/*
	验证管理员是否登录
	简单验证方式
	*/
	public function CheckLogin(){
		$id = (int)$this->_Response->getSession('id');
		$admin = $this->_Response->getCookie('admin');
		$group = $this->_Response->getCookie('group');
		$hash = $this->_Response->getCookie('hash');
		//echo $admin;
		if (empty($id) || empty($admin) || md5($group.$admin.$id) != $hash ){
			return false;
		}else{
			return true;
		}
	}
	/*
	创建表结构
	*/
	public function CreateTable(){
		$sql ='CREATE TABLE `vodcms_admin` (
		`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
		`username` CHAR( 30 ) NOT NULL ,
		`password` CHAR( 32 ) NOT NULL ,
		`lastime` INT UNSIGNED NOT NULL ,
		`lastip` CHAR( 16 ) NOT NULL ,
		`failed` TINYINT UNSIGNED NOT NULL ,
		`locked` TINYINT UNSIGNED NOT NULL
		) ENGINE = MYISAM ';
		return $this->_DB->Exec($sql);
	}
	/*
	统计后台首页各项信息
	*/
	public function information(){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_MEMBERS)->toString();
		$information['总用户数'] = $this->_DB->getcount($sql);	//总用户数
		$select = $this->_DB->select();
		$where = 'TO_DAYS(NOW()) = TO_DAYS(FROM_UNIXTIME(regdate))';
		$sql = $select->from(VODCMS_MEMBERS)->where($where)->toString();	//今日注册数
		$information['今日注册'] = $this->_DB->getCount($sql);
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_MOVIE)->toString();	//影片数
		$information['影片总数'] = $this->_DB->getCount($sql);
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_URL)->toString();	//影片集数
		$information['影片集数'] = $this->_DB->getCount($sql);
		//echo $sql;
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_SERVER)->toString();	//影片服务器数
		$information['服务器数'] = $this->_DB->getCount($sql);
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_ACTIVES)->toString();	//当前点播数
		$information['正在点播数'] = $this->_DB->getCount($sql);
		$select = $this->_DB->select();
		$where = 'locked=1 AND TO_DAYS(NOW()) = TO_DAYS(FROM_UNIXTIME(usedtime))';
		$sql = $select->from(VODCMS_CAR)->where($where)->toString();	//当前点播数
		$information['今日充卡人数'] = $this->_DB->getCount($sql);
		return $information;
	}
}