File: //web/data/www.tbbprovision.com/lib/Controller.class.php
<?php
/**
* 控制器基类
*/
class Controller
{
protected $_controller;
protected $_action;
protected $_view;
protected $_models = array();
protected $_controls = array();
protected $configs = array();
// 构造函数,初始化属性,并实例化对应模型
public function __construct($controller='', $action='', $module='')
{
$this->_controller = $controller;
$this->_action = $action;
$this->_view = new View($controller, $action, $module);
}
/**
* 后台初始化配置
*/
public function manage_init()
{
global $configs;
$this->configs = $configs;
$this->assign('configs',$this->configs);
//判断是否屏蔽国内IP
/*if((int)$configs['close_china_ip'] ) {
if(!(int)f::get_session('manage_user.id') && !in_array(f::get_ip(),$this->configs['trust_ip'])) {
if(substr_count($_SERVER['REQUEST_URI'],'admin')<1) {
$source_result = 0;
$un_login = array("北京", "浙江", "天津", "安徽", "上海", "福建", "重庆", "江西", "山东", "河南", "内蒙古", "湖北", "新疆维吾尔", "湖南", "宁夏回族", "广东", "西藏", "海南", "广西壮族", "四川", "河北", "贵州", "山西", "云南", "辽宁", "陕西", "吉林", "甘肃", "黑龙江", "青海", "江苏");
$ip_area = f::ip(f::get_ip());
$source_result = 0;
foreach ($un_login as $v) {
if (substr_count($ip_area, $v) > 0) {
$source_result = 1;
break;
}
}
if ($source_result) {
header('location:http://www.baidu.com');
exit;
}
}
}
}*/
}
// 分配变量
public function assign($name, $value)
{
$this->_view->assign($name, $value);
}
// 渲染视图
public function render($view_model='',$view_file='',$only_file=0)
{
$this->_view->render($view_model,$view_file,$only_file);
exit;
}
/**
* @param string $model 数据层类名。
* @return mixed 返回指定对象
*/
public function model($model='')
{
//如果没有。则返回全局的
if(!$model){
global $model;
return $model;
}
//如果已经创建过了。就直接返回旧对象
if (!isset($this->_models[$model]))
{
$model_obj = @eval('return new ' . ucfirst($model) . 'Model();');
$this->_models[$model] = $model_obj;
return $model_obj;
}else{
return $this->_models[$model];
}
}
/**
* @param string $model 控制器之间的调用。
* @return mixed 返回指定对象
*/
public function control($control='')
{
//如果没有。则返回全局的
if(!$control){
return false;
}
//如果已经创建过了。就直接返回旧对象
if (!isset($this->_controls[$control]))
{
$control_obj = @eval('return new ' . ucfirst($control) . 'Controller();');
$this->_controls[$control] = $control_obj;
return $control_obj;
}else{
return $this->_controller[$control];
}
}
/**
* 检测是否
*/
public function check_manage_login()
{
if(!(int)f::get_session('manage_user.id')){
js::location('/admin/manage/login/');
exit;
}
}
/**
* 权限验证
*/
public function check_permission()
{
try{
if(f::get_session('manage_user.user_name')=='liaoyaping'){
return true;
}
//权限验证路径
$has_parames = strpos($_SERVER['QUERY_STRING'],'&');
if($has_parames)
$action_url = trim(substr($_SERVER['QUERY_STRING'],0,strpos($_SERVER['QUERY_STRING'],'&')),'/');
else
$action_url = trim($_SERVER['QUERY_STRING'],'/');
//检查路径是否需要验证权限
$permission_row = $this->model('manage')->table('manage_permission')->where(array("url='{$action_url}'"))->fetch_one();
if($permission_row){
$user_id = f::get_session('manage_user.id');
$user_row = $this->model('manage')->table('manage')->where(array("id='{$user_id}'"))->fetch_one();
$group_id = "0,".$user_row['group_id'].'-1';
$where = array("permission_list like '%#{$permission_row['permission_id']}#%'"," and gid in({$group_id})");
$group_row = $this->model('manage')->table('manage_group')->where($where)->fetch_one();
if(!$group_row){
throw new Exception('你无此操作权限');
}
}
}catch (Exception $e){
exit($e->getMessage());
}
}
/**
* 前台页面公共内容
*/
public function webinit()
{
global $model,$configs;
$this->configs = $configs;
//IP黑名单
if(in_array(f::get_ip(),$this->configs['filter_ip'])){
header('location:http://www.baidu.com');
exit;
}
//系统配置缓存 缓存有效时间12小时
if(!is_file(APP_PATH.'config/site_config.php') || (WEB_TIME - filemtime(APP_PATH.'config/site_config.php'))>(86400)){
$global_set_dir = '/config/';
$global_set_name = 'site_config.php';
@chmod(APP_PATH.$global_set_dir.$global_set_name,0755);
$info_cate = $model->table('news_cate')->where('1')->order('my_order desc,cate_id asc')->selectAll();
ob_start();
echo "<?php \r\n";
echo "//网站前端设置\r\n";
foreach((array)$info_cate as $k=>$v) {
echo '$this->configs["info_cate"]['.$k.']= json_decode(\'' . json_encode($v,JSON_UNESCAPED_UNICODE) . '\',true);' . "\r\n";
}
$html = ob_get_contents();
ob_clean();
@file_put_contents(APP_PATH.$global_set_dir.$global_set_name,$html);
unset($html);
}else{
require_once APP_PATH.'config/site_config.php';
}
//给文章列表的标签加样式渐变
$this->configs['tags_style'] = array('bg-danger','bg-info','bg-primary','bg-warning','bg-danger','bg-info','bg-primary','bg-warning');
//星期数字中文影射
$this->configs['week_day_ary'] = array('日','一','二','三','四','五','六');
$this->assign('configs',$this->configs);
}
}