mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
规范文件
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\behavior;
|
||||
use think\config;
|
||||
|
||||
/**
|
||||
* 系统行为扩展:模板内容输出替换
|
||||
* @category Think
|
||||
* @package Think
|
||||
* @subpackage Behavior
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class ContentReplace {
|
||||
|
||||
// 行为扩展的执行入口必须是run
|
||||
public function run(&$content){
|
||||
$content = $this->templateContentReplace($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板内容替换
|
||||
* @access protected
|
||||
* @param string $content 模板内容
|
||||
* @return string
|
||||
*/
|
||||
protected function templateContentReplace($content) {
|
||||
if(IS_CGI) {
|
||||
//CGI/FASTCGI模式下
|
||||
$_temp = explode('.php',$_SERVER['PHP_SELF']);
|
||||
$script_name = rtrim(str_replace($_SERVER['HTTP_HOST'],'',$_temp[0].'.php'),'/');
|
||||
}else {
|
||||
$script_name = rtrim($_SERVER['SCRIPT_NAME'],'/');
|
||||
}
|
||||
define('ROOT_URL', rtrim(dirname(str_replace("\\","\/",$script_name)),'/'));
|
||||
define('MODULE_URL', ROOT_URL.(Config::get('require_module')?'/'.(defined('MODULE_ALIAS')?MODULE_ALIAS:MODULE_NAME):''));
|
||||
define('CONTROLLER_URL', MODULE_URL.(Config::get('require_controller')?'/'.CONTROLLER_NAME:''));
|
||||
define('ACTION_URL', CONTROLLER_URL.'/'.ACTION_NAME);
|
||||
|
||||
// 系统默认的特殊变量替换
|
||||
$replace = [
|
||||
'__ROOT__' => ROOT_URL, // 当前网站地址
|
||||
'__APP__' => MODULE_URL, // 当前项目地址
|
||||
'__CONTROLL__' => CONTROLLER_URL, // 当前操作地址
|
||||
'__URL__' => CONTROLLER_URL,
|
||||
'__ACTION__' => ACTION_URL, // 当前操作地址
|
||||
'__SELF__' => $_SERVER['PHP_SELF'], // 当前页面地址
|
||||
'__PUBLIC__' => ROOT_URL.'/Public',// 站点公共目录
|
||||
];
|
||||
// 允许用户自定义模板的字符串替换
|
||||
if(is_array(Config::get('tmpl_parse_string')) )
|
||||
$replace = array_merge($replace,Config::get('tmpl_parse_string'));
|
||||
$content = str_replace(array_keys($replace),array_values($replace),$content);
|
||||
return $content;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\behavior;
|
||||
|
||||
/**
|
||||
* 系统行为扩展:定位模板文件
|
||||
* @category Think
|
||||
* @package Think
|
||||
* @subpackage Behavior
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class LocationTemplate {
|
||||
// 行为扩展的执行入口必须是run
|
||||
public function run(&$templateFile){
|
||||
// 自动定位模板文件
|
||||
if(!is_file($templateFile))
|
||||
$templateFile = $this->parseTemplateFile($templateFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动定位模板文件
|
||||
* @access private
|
||||
* @param string $templateFile 文件名
|
||||
* @return string
|
||||
*/
|
||||
private function parseTemplateFile($template) {
|
||||
$template = str_replace(':','/',$template);
|
||||
if(''==$template) {
|
||||
// 如果模板文件名为空 按照默认规则定位
|
||||
$template = CONTROLLER_NAME.'/'.ACTION_NAME;
|
||||
}elseif(false === strpos($template,'/')){
|
||||
$template = CONTROLLER_NAME.'/'.$template;
|
||||
}elseif(false === strpos($template,'.')) {
|
||||
$template = $template;
|
||||
}
|
||||
$templateFile = MODULE_PATH.'View/'.$template.'.html';
|
||||
return $templateFile;
|
||||
}
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\behavior;
|
||||
|
||||
/**
|
||||
* 系统行为扩展:静态缓存读取
|
||||
* @category Think
|
||||
* @package Think
|
||||
* @subpackage Behavior
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class ReadHtmlCache {
|
||||
protected $options = [
|
||||
'HTML_CACHE_ON' => false,
|
||||
'HTML_CACHE_TIME' => 60,
|
||||
'HTML_CACHE_RULES' => [],
|
||||
'HTML_FILE_SUFFIX' => '.html',
|
||||
];
|
||||
|
||||
// 行为扩展的执行入口必须是run
|
||||
public function run(&$params){
|
||||
// 开启静态缓存
|
||||
if(C('HTML_CACHE_ON')) {
|
||||
$cacheTime = $this->requireHtmlCache();
|
||||
if( false !== $cacheTime && $this->checkHTMLCache(HTML_FILE_NAME,$cacheTime)) { //静态页面有效
|
||||
// 读取静态页面输出
|
||||
readfile(HTML_FILE_NAME);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否需要静态缓存
|
||||
static private function requireHtmlCache() {
|
||||
// 分析当前的静态规则
|
||||
$htmls = C('HTML_CACHE_RULES'); // 读取静态规则
|
||||
if(!empty($htmls)) {
|
||||
$htmls = array_change_key_case($htmls);
|
||||
// 静态规则文件定义格式 actionName=>array('静态规则','缓存时间','附加规则')
|
||||
// 'read'=>array('{id},{name}',60,'md5') 必须保证静态规则的唯一性 和 可判断性
|
||||
// 检测静态规则
|
||||
$moduleName = strtolower(MODULE_NAME);
|
||||
$actionName = strtolower(ACTION_NAME);
|
||||
if(isset($htmls[$moduleName.':'.$actionName])) {
|
||||
$html = $htmls[$moduleName.':'.$actionName]; // 某个模块的操作的静态规则
|
||||
}elseif(isset($htmls[$moduleName.':'])){// 某个模块的静态规则
|
||||
$html = $htmls[$moduleName.':'];
|
||||
}elseif(isset($htmls[$actionName])){
|
||||
$html = $htmls[$actionName]; // 所有操作的静态规则
|
||||
}elseif(isset($htmls['*'])){
|
||||
$html = $htmls['*']; // 全局静态规则
|
||||
}elseif(isset($htmls['empty:index']) && !class_exists(MODULE_NAME.'Action')){
|
||||
$html = $htmls['empty:index']; // 空模块静态规则
|
||||
}elseif(isset($htmls[$moduleName.':_empty']) && $this->isEmptyAction(MODULE_NAME,ACTION_NAME)){
|
||||
$html = $htmls[$moduleName.':_empty']; // 空操作静态规则
|
||||
}
|
||||
if(!empty($html)) {
|
||||
// 解读静态规则
|
||||
$rule = $html[0];
|
||||
// 以$_开头的系统变量
|
||||
$rule = preg_replace('/{\$(_\w+)\.(\w+)\|(\w+)}/e',"\\3(\$\\1['\\2'])",$rule);
|
||||
$rule = preg_replace('/{\$(_\w+)\.(\w+)}/e',"\$\\1['\\2']",$rule);
|
||||
// {ID|FUN} GET变量的简写
|
||||
$rule = preg_replace('/{(\w+)\|(\w+)}/e',"\\2(\$_GET['\\1'])",$rule);
|
||||
$rule = preg_replace('/{(\w+)}/e',"\$_GET['\\1']",$rule);
|
||||
// 特殊系统变量
|
||||
$rule = str_ireplace(
|
||||
['{:app}','{:module}','{:action}','{:group}'],
|
||||
[APP_NAME,MODULE_NAME,ACTION_NAME,defined('GROUP_NAME')?GROUP_NAME:''],
|
||||
$rule);
|
||||
// {|FUN} 单独使用函数
|
||||
$rule = preg_replace('/{|(\w+)}/e',"\\1()",$rule);
|
||||
if(!empty($html[2])) $rule = $html[2]($rule); // 应用附加函数
|
||||
$cacheTime = isset($html[1])?$html[1]:C('HTML_CACHE_TIME'); // 缓存有效期
|
||||
// 当前缓存文件
|
||||
define('HTML_FILE_NAME',HTML_PATH . $rule.C('HTML_FILE_SUFFIX'));
|
||||
return $cacheTime;
|
||||
}
|
||||
}
|
||||
// 无需缓存
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查静态HTML文件是否有效
|
||||
* 如果无效需要重新更新
|
||||
* @access public
|
||||
* @param string $cacheFile 静态文件名
|
||||
* @param integer $cacheTime 缓存有效期
|
||||
* @return boolen
|
||||
*/
|
||||
static public function checkHTMLCache($cacheFile='',$cacheTime='') {
|
||||
if(!is_file($cacheFile)){
|
||||
return false;
|
||||
}elseif (filemtime(C('TEMPLATE_NAME')) > filemtime($cacheFile)) {
|
||||
// 模板文件如果更新静态文件需要更新
|
||||
return false;
|
||||
}elseif(!is_numeric($cacheTime) && function_exists($cacheTime)){
|
||||
return $cacheTime($cacheFile);
|
||||
}elseif ($cacheTime != 0 && NOW_TIME > filemtime($cacheFile)+$cacheTime) {
|
||||
// 文件是否在有效期
|
||||
return false;
|
||||
}
|
||||
//静态文件有效
|
||||
return true;
|
||||
}
|
||||
|
||||
//检测是否是空操作
|
||||
static private function isEmptyAction($module,$action) {
|
||||
$className = $module.'Action';
|
||||
$class = new $className;
|
||||
return !method_exists($class,$action);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\behavior;
|
||||
use think\Config;
|
||||
use think\Log;
|
||||
use think\Debug;
|
||||
|
||||
/**
|
||||
* 系统行为扩展:页面Trace显示输出
|
||||
* @category Think
|
||||
* @package Think
|
||||
* @subpackage Behavior
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class ShowPageTrace {
|
||||
|
||||
// 行为扩展的执行入口必须是run
|
||||
public function run(&$params){
|
||||
if(!IS_AJAX && Config::get('show_page_trace')) {
|
||||
echo $this->showTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示页面Trace信息
|
||||
* @access private
|
||||
*/
|
||||
private function showTrace() {
|
||||
// 系统默认显示信息
|
||||
$files = get_included_files();
|
||||
$info = [];
|
||||
foreach ($files as $key=>$file){
|
||||
$info[] = $file.' ( '.number_format(filesize($file)/1024,2).' KB )';
|
||||
}
|
||||
$trace = [];
|
||||
Debug::remark('START',$GLOBALS['startTime']);
|
||||
$base = [
|
||||
'请求信息' => date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).' '.$_SERVER['SERVER_PROTOCOL'].' '.$_SERVER['REQUEST_METHOD'].' : '.$_SERVER['PHP_SELF'],
|
||||
'运行时间' => Debug::getUseTime('START','END',6).'s',
|
||||
'内存开销' => MEMORY_LIMIT_ON?G('START','END','m').'b':'不支持',
|
||||
'查询信息' => N('db_query').' queries '.N('db_write').' writes ',
|
||||
'文件加载' => count($files),
|
||||
'缓存信息' => N('cache_read').' gets '.N('cache_write').' writes ',
|
||||
'配置加载' => count(Config::get()),
|
||||
];
|
||||
// 读取项目定义的Trace文件
|
||||
$traceFile = MODULE_PATH.'trace.php';
|
||||
if(is_file($traceFile)) {
|
||||
$base = array_merge($base,include $traceFile);
|
||||
}
|
||||
$debug = Log::getLog();
|
||||
$tabs = Config::get('trace_page_tabs');
|
||||
foreach ($tabs as $name=>$title){
|
||||
switch(strtoupper($name)) {
|
||||
case 'BASE':// 基本信息
|
||||
$trace[$title] = $base;
|
||||
break;
|
||||
case 'FILE': // 文件信息
|
||||
$trace[$title] = $info;
|
||||
break;
|
||||
default:// 调试信息
|
||||
$name = strtoupper($name);
|
||||
if(strpos($name,'|')) {// 多组信息
|
||||
$array = explode('|',$name);
|
||||
$result = [];
|
||||
foreach($array as $name){
|
||||
$result += isset($debug[$name])?$debug[$name]:[];
|
||||
}
|
||||
$trace[$title] = $result;
|
||||
}else{
|
||||
$trace[$title] = isset($debug[$name])?$debug[$name]:'';
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($files,$info,$base,$debug);
|
||||
// 调用Trace页面模板
|
||||
ob_start();
|
||||
include Config::has('tmpl_trace_file')?Config::get('tmpl_trace_file'):THINK_PATH.'tpl/page_trace.tpl';
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\behavior;
|
||||
/**
|
||||
* 系统行为扩展:表单令牌生成
|
||||
* @category Think
|
||||
* @package Think
|
||||
* @subpackage Behavior
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class TokenBuild extends Behavior {
|
||||
// 行为参数定义
|
||||
protected $options = [
|
||||
'TOKEN_ON' => false, // 开启令牌验证
|
||||
'TOKEN_NAME' => '__hash__', // 令牌验证的表单隐藏字段名称
|
||||
'TOKEN_TYPE' => 'md5', // 令牌验证哈希规则
|
||||
'TOKEN_RESET' => true, // 令牌错误后是否重置
|
||||
];
|
||||
|
||||
public function run(&$content){
|
||||
if(C('TOKEN_ON')) {
|
||||
if(strpos($content,'{__TOKEN__}')) {
|
||||
// 指定表单令牌隐藏域位置
|
||||
$content = str_replace('{__TOKEN__}',$this->buildToken(),$content);
|
||||
}elseif(preg_match('/<\/form(\s*)>/is',$content,$match)) {
|
||||
// 智能生成表单令牌隐藏域
|
||||
$content = str_replace($match[0],$this->buildToken().$match[0],$content);
|
||||
}
|
||||
}else{
|
||||
$content = str_replace('{__TOKEN__}','',$content);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建表单令牌
|
||||
private function buildToken() {
|
||||
$tokenName = C('TOKEN_NAME');
|
||||
$tokenType = C('TOKEN_TYPE');
|
||||
if(!isset($_SESSION[$tokenName])) {
|
||||
$_SESSION[$tokenName] = [];
|
||||
}
|
||||
// 标识当前页面唯一性
|
||||
$tokenKey = md5($_SERVER['REQUEST_URI']);
|
||||
if(isset($_SESSION[$tokenName][$tokenKey])) {// 相同页面不重复生成session
|
||||
$tokenValue = $_SESSION[$tokenName][$tokenKey];
|
||||
}else{
|
||||
$tokenValue = $tokenType(microtime(true));
|
||||
$_SESSION[$tokenName][$tokenKey] = $tokenValue;
|
||||
}
|
||||
$token = '<input type="hidden" name="'.$tokenName.'" value="'.$tokenKey.'_'.$tokenValue.'" />';
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
4
library/think/cache/driver/secache.php
vendored
4
library/think/cache/driver/secache.php
vendored
@@ -161,7 +161,7 @@ class SecacheClient{
|
||||
if(!file_exists($this->_file)){
|
||||
$this->create();
|
||||
}else{
|
||||
$this->_rs = fopen($this->_file,'rb+') or $this->trigger_error('Can\'t open the cachefile: '.realpath($this->_file),E_USER_ERROR);
|
||||
$this->_rs = fopen($this->_file,'rb+') || $this->trigger_error('Can\'t open the cachefile: '.realpath($this->_file),E_USER_ERROR);
|
||||
$this->_seek($this->header_padding);
|
||||
$info = unpack('V1max_size/a*ver',fread($this->_rs,$this->info_size));
|
||||
if($info['ver']!=$this->ver){
|
||||
@@ -178,7 +178,7 @@ class SecacheClient{
|
||||
}
|
||||
|
||||
function create(){
|
||||
$this->_rs = fopen($this->_file,'wb+') or $this->trigger_error('Can\'t open the cachefile: '.realpath($this->_file),E_USER_ERROR);;
|
||||
$this->_rs = fopen($this->_file,'wb+') || $this->trigger_error('Can\'t open the cachefile: '.realpath($this->_file),E_USER_ERROR);;
|
||||
fseek($this->_rs,0);
|
||||
fputs($this->_rs,'<'.'?php exit()?'.'>');
|
||||
return $this->_format();
|
||||
|
||||
@@ -25,9 +25,11 @@ class Loader {
|
||||
include self::$map[$class];
|
||||
}else{ // 命名空间自动加载
|
||||
$name = strstr($class, '\\', true);
|
||||
if(isset(self::$namespace[$name])){ // 注册的命名空间
|
||||
if(isset(self::$namespace[$name])){
|
||||
// 注册的命名空间
|
||||
$path = dirname(self::$namespace[$name]) . '/';
|
||||
}elseif(is_dir(LIB_PATH.$name)){ // Library目录下面的命名空间自动定位
|
||||
}elseif(in_array($name,['think','org','behavior','com']) || is_dir(LIB_PATH.$name)){
|
||||
// Library目录下面的命名空间自动定位
|
||||
$path = LIB_PATH;
|
||||
}else{ // 项目命名空间
|
||||
$path = APP_PATH;
|
||||
@@ -78,7 +80,7 @@ class Loader {
|
||||
//加载当前项目应用类库
|
||||
$class = substr_replace($class, '', 0, strlen($class_strut[0]) + 1);
|
||||
$baseUrl = MODULE_PATH;
|
||||
}elseif (in_array($class_strut[0], ['think','org', 'com'])) {
|
||||
}elseif (in_array($class_strut[0], ['think','behavior','org', 'com'])) {
|
||||
// org 第三方公共类库 com 企业公共类库
|
||||
$baseUrl = LIB_PATH;
|
||||
}elseif(in_array($class_strut[0], ['vendor', 'traits'])){
|
||||
|
||||
@@ -138,7 +138,7 @@ class Session {
|
||||
* @return void
|
||||
*/
|
||||
static public function delete($name,$prefix='') {
|
||||
$prefix = $prefix?$prefix:$this->prefix;
|
||||
$prefix = $prefix ? $prefix : self::$prefix;
|
||||
if(strpos($name,'.')){
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
if($prefix){
|
||||
@@ -161,7 +161,7 @@ class Session {
|
||||
* @return void
|
||||
*/
|
||||
static public function clear($prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$prefix;
|
||||
$prefix = $prefix ? $prefix : self::$prefix;
|
||||
if($prefix) {
|
||||
unset($_SESSION[$prefix]);
|
||||
}else{
|
||||
|
||||
@@ -168,7 +168,7 @@ class View {
|
||||
list($module,$template) = explode('@',$template);
|
||||
}
|
||||
// 获取当前主题的模版路径
|
||||
defined('THEME_PATH') or define('THEME_PATH', $this->getThemePath($module));
|
||||
defined('THEME_PATH') || define('THEME_PATH', $this->getThemePath($module));
|
||||
|
||||
// 分析模板文件规则
|
||||
if('' == $template) {
|
||||
|
||||
Reference in New Issue
Block a user