<?php
/*
秀影EasyFramework框架
qq:24498936
msn:jdzcn_net@hotmail.com
website:http://www.vodcms.com
网吧管理模型
*/
class NetbarModel extends DBModel {
	protected $_name = VODCMS_NETBAR;
	public function init(){
		
	}
	public function addNetbar($POST){
		if (is_array($POST)){
			if (empty($POST['name'])){
				$this->error[] = _('网吧名称不能为空!');
				return false;
			}
			if (filter_var($POST['sip'], FILTER_VALIDATE_IP)===false){
				$this->error[] = _('无效的开始IP段！');
				return false;
			}else{
				$POST['sip'] = $this->ip2long($POST['sip']);
			}
			if (filter_var($POST['eip'], FILTER_VALIDATE_IP)===false){
				$this->error[] = _('无效的结束IP段！');
				return false;
			}else{
				$POST['eip'] = $this->ip2long($POST['eip']);
			}
			if  (empty($POST['username'])){
				$this->error[] = _('请填写30个字符的联系名称!');
				return false;
			}
			if (empty($POST['pass'])){
				$this->error[] = _('请填写长度在5至30个字符的密码！');
				return false;
			}
			if ( strtotime($POST['lifetime']) == 0){
				$this->error[] = _('服务到期时间格式无效！请填写正确的时间');
				return false;
			}else{
				$POST['lifetime'] = strtotime($POST['lifetime']);
			}
			$POST['addtime'] = time();
			$POST['admin'] = $this->_Response->getSession('admin');
			systemlog::set('添加了一个名为'.$POST['name'].'网吧资料');
			return $this->_DB->insert($this->_name, $POST);
		}else{
			$this->error = _('请传入一个数组');
			return false;
		}
		
	}
	/*
	修改网吧资料
	*/
	public function modify($POST, $where=null){
		if (is_array($POST)){
			if (empty($POST['name'])){
				$this->error[] = _('网吧名称不能为空!');
				return false;
			}
			if (filter_var($POST['sip'], FILTER_VALIDATE_IP)===false){
				$this->error[] = _('无效的开始IP段！');
				return false;
			}else{
				$POST['sip'] = $this->ip2long($POST['sip']);
			}
			if (filter_var($POST['eip'], FILTER_VALIDATE_IP)===false){
				$this->error[] = _('无效的结束IP段！');
				return false;
			}else{
				$POST['eip'] = $this->ip2long($POST['eip']);
			}
			if  (empty($POST['username'])){
				$this->error[] = _('请填写30个字符的联系名称!');
				return false;
			}
			if (empty($POST['pass'])){
				$this->error[] = _('请填写长度在5至30个字符的密码！');
				return false;
			}
			if ( strtotime($POST['lifetime']) == 0){
				$this->error[] = _('服务到期时间格式无效！请填写正确的时间');
				return false;
			}else{
				$POST['lifetime'] = strtotime($POST['lifetime']);
			}
			$POST['addtime'] = time();
			$POST['admin'] = $this->_Response->getSession('admin');
			systemlog::set('修改了编号'.$POST['id'].'网吧资料');
			return $this->_DB->update($this->_name, $POST, $where);
		}else{
			$this->error = _('请传入一个数组');
			return false;
		}
	}
	public function  ip2long($ip){
		$long = ip2long($ip);
		if ($long < 1){
			return sprintf("%u", $long);
		}else {
			return $long;
		}
	}
	public function getAll($where=null){
		$select = $this->_DB->select();
		$sql = ($select->from($this->_name)->where($where)->order('id DESC'));
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total);
		//echo $total;
		$this->printpage = $this->_Page->printpage();
		$sql = $sql.$this->_Page->limit();
		//echo $sql;
		return $this->_DB->fetAll($sql);
	}
	/*
	网吧搜索
	*/
	public function search($type, $keyword){
		$select = $this->_DB->select();
		if ($type == 0){
			$where = "name LIKE '%". $keyword."%'";
		}else {
			$where = "sip LIKE '%". $this->ip2long($keyword)."%' OR eip LIKE '%".$this->ip2long($keyword)."%'";
		}
		$sql = $select->from($this->_name)->where($where)->order('id DESC')->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total);
		$this->printpage = $this->_Page->printpage();
		$sql = $sql.$this->_Page->limit();
		return $this->_DB->fetAll($sql);
	}
	/*
	获取指定记录的资料
	*/
	public function getRow($where=null){
		$select = $this->_DB->select();
		return $this->_DB->fetRow($select->from($this->_name)->where($where)->order('id DESC')->toString());
	}
	/*
	删除网吧
	*/
	public function delete($id){
		systemlog::set('删除了编号为'.$id.'网吧资料');
		return $this->_DB->delete($this->_name, 'id='.$id);
	}
	/*
	反转网吧状态
	*/
	public function locked($id){
		$sql = 'UPDATE '.$this->_name.' SET locked=NOT(locked) WHERE id='.$id;
		return $this->_DB->Exec($sql);
	}
	/*
	根据传入IP判断是否在IP范围列表
	*/
	public function isAllow($ip){
		$ipnum = $this->ip2long($ip);
		$where = 'locked =0 AND sip<='.$ipnum.' AND eip>='.$ipnum;
		$row = $this->getRow($where);
		if ($row['id']){
			return $row;
		}else{
			return NULL;
		}
	}
}