📄 denymodel.php
字号:
<?php
/*
限制用户访问模型
支持用户访问到期时间限制
支持IP地址段时间控制
*/
class DenyModel extends DBModel {
protected $_name = VODCMS_DENY;
public function Init(){
}
/*
@param title 限制IP的描述100个字符内
@param sip 被限制IP起始段的地址
@param eip 限制结束IP地址段
@param lifetime 限制时间 单位分钟 默认限制一分钟
return (bool)
*/
public function Deny($title, $sip, $eip , $lifetime=60){
if ( filter_var($sip, FILTER_VALIDATE_IP)===false || filter_var($eip, FILTER_VALIDATE_IP)===false){
//无效的IP地址时
$this->error = _('无效的起始IP或者结束IP');
return false;
}else{ //
if (empty($title)){
$this->error = _('请填写封锁规则描述!');
return false;
}
$array = array();
$array['title'] = $title;
$array['sip'] = $this->ip2long($sip);
$array['eip'] = $this->ip2long($eip);
$array['stime'] = time();
$array['etime'] = time()+ ($lifetime*60);
$array['locked'] = 0;
return $this->_DB->insert($this->_name, $array);
}
}
/*
检测用户是否在被限制的访问范围
*/
public function CheckDeny(){
$userip = $this->_Response->strUserIp();
$sip = $this->ip2long($userip);
$select = $this->_DB->select();
$time = time();
$select->from($this->_name)->where("locked= 0 AND `sip`<= $sip AND `eip`>= $sip AND etime>=$time")->limit(1);
$sql = $select->toString();
$data = $this->_DB->fetRow($sql);
//echo $sql;
if ($data){
$mn = ceil(($data['etime'] - time() ) / 60); //锁定剩余时间
$this->_Response->sendHeader(403);
exit('您的IP:'.$this->_Response->strUserIp().'被系统锁定!请<font color="#FF0000">'.$mn.'</font>分钟后访问此页!');
}
}
public function ip2long($ip){
$long = ip2long($ip);
if ($long < 1){
return sprintf("%u\n", $long);
}else {
return $long;
}
}
/*
创建表结构
*/
public function CreateTable(){
$sql = 'CREATE TABLE `vodcms_deny` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 50 ) NOT NULL ,
`sip` INT UNSIGNED NOT NULL ,
`eip` INT UNSIGNED NOT NULL ,
`stime` INT UNSIGNED NOT NULL ,
`etime` INT UNSIGNED NOT NULL ,
`locked` INT UNSIGNED NOT NULL
) ENGINE = MYISAM';
$this->_DB->Exec($sql);
}
/*
获取全部限制IP
*/
public function getAll(){
$select = $this->_DB->select();
$sql = $select->from($this->_name)->order('id DESC')->toString();
return $this->_DB->fetAll($sql);
}
public function search($keyword){
$select = $this->_DB->select();
$sql = $select->from($this->_name)->where('sip<='.$this->ip2long($keyword).' AND eip>='.$this->ip2long($keyword))->order('id DESC')->toString();
return $this->_DB->fetAll($sql);
}
/*
删除记录
*/
public function delete($where){
return $this->_DB->delete($this->_name, $where);
}
/*
反转记录状态
*/
public function locked($id){
$sql = 'UPDATE '.$this->_name.' SET locked=NOT(locked) WHERE id='.$id;
return $this->_DB->Exec($sql);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -