init
This commit is contained in:
21
app/common/behavior/ActionLog.php
Executable file
21
app/common/behavior/ActionLog.php
Executable file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use think\Config;
|
||||
use think\Cache;
|
||||
use think\Loader;
|
||||
use think\Hook;
|
||||
|
||||
/**
|
||||
* Description of SystemConfig
|
||||
* 系统配置初始化
|
||||
*/
|
||||
class SystemConfig {
|
||||
|
||||
//记录行为
|
||||
public function run() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
35
app/common/behavior/CheckIp.php
Executable file
35
app/common/behavior/CheckIp.php
Executable file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use think\Request;
|
||||
use think\Config;
|
||||
|
||||
load_trait('controller/Jump');
|
||||
|
||||
/**
|
||||
* Description of CheckIp
|
||||
* 检测用户访问的ip
|
||||
*/
|
||||
class CheckIp {
|
||||
|
||||
use \traits\controller\Jump;
|
||||
|
||||
/**
|
||||
* 检测用户IP
|
||||
*/
|
||||
public function run() {
|
||||
$deny_ip = Config::get('admin_deny_ip');
|
||||
$allow_ip = Config::get('admin_allow_ip');
|
||||
$ip = Request::instance()->ip();
|
||||
//$this->error('禁止访问');
|
||||
if (!is_administrator() && $deny_ip) {
|
||||
in_array($ip, explode(',', $deny_ip)) && $this->error('禁止访问'); // 检查IP地址访问
|
||||
}
|
||||
if (!is_administrator() && $allow_ip) {
|
||||
!in_array($ip, explode(',', $allow_ip)) && $this->error('禁止访问'); // 检查IP地址访问
|
||||
}
|
||||
\think\Log::record("[ 访问者IP ]:" . $ip);
|
||||
}
|
||||
|
||||
}
|
||||
53
app/common/behavior/ModuleCacheBehavior.php
Executable file
53
app/common/behavior/ModuleCacheBehavior.php
Executable file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use think\Config;
|
||||
use think\Request;
|
||||
use think\Hook;
|
||||
use think\App;
|
||||
use think\Log;
|
||||
|
||||
class ModuleCacheBehavior {
|
||||
|
||||
protected static $moduleName = '';
|
||||
protected static $cachemoduleName;
|
||||
|
||||
/**
|
||||
* 系统配置读取并缓存
|
||||
*/
|
||||
public function appBegin(&$param) {
|
||||
switch ($param['type']) {
|
||||
case 'redirect': // 重定向跳转
|
||||
break;
|
||||
case 'module': // 模块/控制器/操作
|
||||
self::$moduleName = $param['module'][0];
|
||||
break;
|
||||
case 'controller': // 执行控制器操作
|
||||
break;
|
||||
case 'method': // 回调方法
|
||||
break;
|
||||
case 'function': // 闭包
|
||||
break;
|
||||
case 'response': // Response 实例
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException('dispatch type not support');
|
||||
}
|
||||
self::$cachemoduleName = Config::get('html_cache_module', array());
|
||||
if (!empty(self::$cachemoduleName) && in_array(self::$moduleName, self::$cachemoduleName)) {
|
||||
// 监听 app_dispatch
|
||||
Hook::listen('app_cache_begin', $param);
|
||||
}
|
||||
App::$debug && Log::record("[ 模块缓存 ]:内容命中缓存处理HOOK");
|
||||
}
|
||||
|
||||
public function viewFilter(&$param) {
|
||||
if (!empty(self::$cachemoduleName) && in_array(self::$moduleName, self::$cachemoduleName)) {
|
||||
// 监听 app_cache_end
|
||||
Hook::listen('app_cache_end', $param);
|
||||
}
|
||||
App::$debug && Log::record("[ 模块缓存 ]:视图内容缓存处理HOOK");
|
||||
}
|
||||
|
||||
}
|
||||
171
app/common/behavior/ReadHtmlCacheBehavior.php
Executable file
171
app/common/behavior/ReadHtmlCacheBehavior.php
Executable file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use think\Config;
|
||||
use think\Request;
|
||||
use think\App;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* 系统行为扩展:静态缓存读取
|
||||
*/
|
||||
class ReadHtmlCacheBehavior {
|
||||
|
||||
protected static $actionName;
|
||||
protected static $controllerName;
|
||||
protected static $moduleName;
|
||||
protected $storage;
|
||||
|
||||
// 行为扩展的执行入口必须是run
|
||||
public function run(&$param) {
|
||||
self::$actionName = $param['module'][0];
|
||||
self::$controllerName = empty($param['module'][1]) ? '' : $param['module'][1];
|
||||
self::$moduleName = empty($param['module'][2]) ? '' : $param['module'][2];
|
||||
// 开启静态缓存
|
||||
if (Request::instance()->isGet() && Config::get('html_cache_on', false)) {
|
||||
$cacheTime = $this->requireHtmlCache();
|
||||
if (false !== $cacheTime && $this->checkHTMLCache(HTML_FILE_NAME, $cacheTime)) { //静态页面有效
|
||||
// 读取静态页面输出
|
||||
include HTML_FILE_NAME;
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否需要静态缓存
|
||||
static private function requireHtmlCache() {
|
||||
// 分析当前的静态规则
|
||||
$htmls = Config::get('html_cache_rules', ''); // 读取静态规则
|
||||
if (!empty($htmls)) {
|
||||
$htmls = array_change_key_case($htmls);
|
||||
// 静态规则文件定义格式 actionName=>array('静态规则','缓存时间','附加规则')
|
||||
// 'read'=>array('{id},{name}',60,'md5') 必须保证静态规则的唯一性 和 可判断性
|
||||
// 检测静态规则
|
||||
$moduleName = strtolower(self::$moduleName);
|
||||
$controllerName = strtolower(self::$controllerName);
|
||||
$actionName = strtolower(self::$actionName);
|
||||
$rules = [
|
||||
$moduleName . ':' . $controllerName . ':' . $actionName,
|
||||
$moduleName . ':' . $controllerName,
|
||||
$moduleName . ':',
|
||||
$controllerName . ':' . $actionName,
|
||||
$controllerName . ':',
|
||||
$actionName,
|
||||
'*',
|
||||
];
|
||||
foreach ($rules as $v) {
|
||||
if (!empty($htmls[$v])) {
|
||||
$html = $htmls[$v]; //规则
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!empty($html)) {
|
||||
// thinkphp5
|
||||
$method = Request::instance()->method();
|
||||
switch ($method) {
|
||||
case 'GET':
|
||||
$_GET = Request::instance()->param();
|
||||
break;
|
||||
case 'POST':
|
||||
$_POST = Request::instance()->param();
|
||||
break;
|
||||
case 'REQUEST':
|
||||
$_REQUEST = Request::instance()->request();
|
||||
break;
|
||||
case 'SERVER':
|
||||
$_SERVER = Request::instance()->server();
|
||||
break;
|
||||
case 'SESSION':
|
||||
$_SESSION = Request::instance()->session();
|
||||
break;
|
||||
case 'COOKIE':
|
||||
$_COOKIE = Request::instance()->cookie();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// 解读静态规则
|
||||
$rule = is_array($html) ? $html[0] : $html;
|
||||
// 以$_开头的系统变量
|
||||
$callback = function($match) {
|
||||
switch ($match[1]) {
|
||||
case '_GET': $var = $_GET[$match[2]];
|
||||
break;
|
||||
case '_POST': $var = $_POST[$match[2]];
|
||||
break;
|
||||
case '_REQUEST': $var = $_REQUEST[$match[2]];
|
||||
break;
|
||||
case '_SERVER': $var = $_SERVER[$match[2]];
|
||||
break;
|
||||
case '_SESSION': $var = $_SESSION[$match[2]];
|
||||
break;
|
||||
case '_COOKIE': $var = $_COOKIE[$match[2]];
|
||||
break;
|
||||
}
|
||||
return (count($match) == 4) ? $match[3]($var) : $var;
|
||||
};
|
||||
$rule = preg_replace_callback('/{\$(_\w+)\.(\w+)(?:\|(\w+))?}/', $callback, $rule);
|
||||
// {ID|FUN} GET变量的简写
|
||||
$rule = preg_replace_callback('/{(\w+)\|(\w+)}/', function($match) {
|
||||
return $match[2]($_GET[$match[1]]);
|
||||
}, $rule);
|
||||
$rule = preg_replace_callback('/{(\w+)}/', function($match) {
|
||||
return $_GET[$match[1]];
|
||||
}, $rule);
|
||||
// 特殊系统变量
|
||||
$rule = str_ireplace(
|
||||
array('{:controller}', '{:action}', '{:module}'), array(self::$controllerName, self::$actionName, self::$moduleName), $rule);
|
||||
// {|FUN} 单独使用函数
|
||||
$rule = preg_replace_callback('/{|(\w+)}/', function($match) {
|
||||
return $match[1]();
|
||||
}, $rule);
|
||||
// $cacheTime = config('html_cache_time') ?? '.html';//php7
|
||||
$cacheTime = Config::get('html_cache_time', 60);
|
||||
$cacheTime = empty($cacheTime) ? 60 : $cacheTime;
|
||||
if (is_array($html)) {
|
||||
if (!empty($html[2]))
|
||||
$rule = $html[2]($rule); // 应用附加函数
|
||||
$cacheTime = isset($html[1]) ? $html[1] : $cacheTime; // 缓存有效期
|
||||
}else {
|
||||
$cacheTime = $cacheTime;
|
||||
}
|
||||
// 当前缓存文件
|
||||
// $html_file_suffix = config('html_file_suffix') ?? '.html';//php7
|
||||
$html_file_suffix = Config::get('html_file_suffix');
|
||||
$html_file_suffix = empty($html_file_suffix) ? '.html' : $html_file_suffix;
|
||||
define('HTML_FILE_NAME', RUNTIME_PATH . $rule . $html_file_suffix);
|
||||
return $cacheTime;
|
||||
}
|
||||
}
|
||||
// 无需缓存
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查静态HTML文件是否有效
|
||||
* 如果无效需要重新更新
|
||||
* @access public
|
||||
* @param string $cacheFile 静态文件名
|
||||
* @param integer $cacheTime 缓存有效期
|
||||
* @return boolean
|
||||
*/
|
||||
static public function checkHTMLCache($cacheFile = '', $cacheTime = '') {
|
||||
if (true == Config::get('app_debug')) {
|
||||
return false;
|
||||
} elseif (!is_file($cacheFile)) {
|
||||
return false;
|
||||
// }elseif (filemtime(\think\view()->parseTemplate()) > $this->storage()->read($cacheFile,'mtime')) {
|
||||
// // 模板文件如果更新静态文件需要更新
|
||||
// return false;
|
||||
} elseif (!is_numeric($cacheTime) && function_exists($cacheTime)) {
|
||||
return $cacheTime($cacheFile);
|
||||
} elseif ($cacheTime != 0 && time() > filemtime($cacheFile) + $cacheTime) {
|
||||
// 文件是否在有效期
|
||||
return false;
|
||||
}
|
||||
//静态文件有效
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
36
app/common/behavior/SystemConfig.local.php
Executable file
36
app/common/behavior/SystemConfig.local.php
Executable file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use think\Config;
|
||||
use think\Cache;
|
||||
use think\Loader;
|
||||
use think\App;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* Description of SystemConfig
|
||||
* 系统配置初始化
|
||||
*/
|
||||
class SystemConfig {
|
||||
|
||||
/**
|
||||
* 系统配置读取并缓存
|
||||
*/
|
||||
public function run() {
|
||||
$config = Cache::get('common_config_data');
|
||||
$configuk = Cache::get('common_config_data_uk');
|
||||
if (empty($config)) {
|
||||
$config = Loader::model('Sysconfig')->configLists(['group' => ['in', [1, 2, 3, 4, 6]]]);
|
||||
Cache::tag('sysconfig')->set('common_config_data', $config);
|
||||
App::$debug && Log::record("[ 系统配置 ]:初始化成功");
|
||||
}
|
||||
if (empty($configuk)) {
|
||||
$config = Loader::model('SysconfigEn')->configLists(['group' => ['in', [1, 2, 3, 4, 6]]]);
|
||||
Cache::tag('sysconfiguk')->set('common_config_data_uk', $configuk);
|
||||
App::$debug && Log::record("[ 系统配置 ]:初始化成功");
|
||||
}
|
||||
Config::set($configuk);
|
||||
}
|
||||
|
||||
}
|
||||
30
app/common/behavior/SystemConfig.php
Executable file
30
app/common/behavior/SystemConfig.php
Executable file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use think\Config;
|
||||
use think\Cache;
|
||||
use think\Loader;
|
||||
use think\App;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* Description of SystemConfig
|
||||
* 系统配置初始化
|
||||
*/
|
||||
class SystemConfig {
|
||||
|
||||
/**
|
||||
* 系统配置读取并缓存
|
||||
*/
|
||||
public function run() {
|
||||
$config = Cache::get('common_config_data');
|
||||
if (empty($config)) {
|
||||
$config = Loader::model('Sysconfig')->configLists(['group' => ['in', [1, 2, 3, 4, 6]]]);
|
||||
Cache::tag('sysconfig')->set('common_config_data', $config);
|
||||
App::$debug && Log::record("[ 系统配置 ]:初始化成功");
|
||||
}
|
||||
Config::set($config);
|
||||
}
|
||||
|
||||
}
|
||||
37
app/common/behavior/WriteHtmlCacheBehavior.php
Executable file
37
app/common/behavior/WriteHtmlCacheBehavior.php
Executable file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\behavior;
|
||||
|
||||
use think\Config;
|
||||
use think\Request;
|
||||
use think\App;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* 系统行为扩展:静态缓存写入
|
||||
*/
|
||||
class WriteHtmlCacheBehavior {
|
||||
|
||||
// protected $storage;
|
||||
// public function action_begin(){
|
||||
public function run(&$content) {
|
||||
//2014-11-28 修改 如果有HTTP 4xx 3xx 5xx 头部,禁止存储
|
||||
//2014-12-1 修改 对注入的网址 防止生成,例如 /game/lst/SortType/hot/-e8-90-8c-e5-85-94-e7-88-b1-e6-b6-88-e9-99-a4/-e8-bf-9b-e5-87-bb-e7-9a-84-e9-83-a8-e8-90-bd/-e9-a3-8e-e4-ba-91-e5-a4-a9-e4-b8-8b/index.shtml
|
||||
if (Config::get('html_cache_on') && defined('HTML_FILE_NAME') && !preg_match('/Status.*[345]{1}\d{2}/i', implode(' ', headers_list())) && !preg_match('/(-[a-z0-9]{2}){3,}/i', HTML_FILE_NAME)) {
|
||||
$view_replace_str = Config::get('view_replace_str');
|
||||
$arraySearch = array_keys($view_replace_str);
|
||||
$content = str_replace($arraySearch, $view_replace_str, $content);
|
||||
//静态文件写入
|
||||
$this->initStorage()->write(HTML_FILE_NAME, $content);
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化模板编译存储器
|
||||
private function initStorage() {
|
||||
$type = Config::get('html_cache_compile_type');
|
||||
$type = $type ? $type : 'File';
|
||||
$class = false !== strpos($type, '\\') ? $type : '\\think\\template\\driver\\' . ucwords($type);
|
||||
return new $class();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user