mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-03 14:02:47 +08:00
更新核心类库
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
use Think\Exception;
|
||||
|
||||
/**
|
||||
* App 应用管理
|
||||
@@ -22,52 +23,76 @@ class App {
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
static public function run() {
|
||||
static public function run($config) {
|
||||
// 监听app_init
|
||||
Tag::listen('app_init');
|
||||
if(Config::get('common_module')){
|
||||
define('COMMON_PATH', APP_PATH . Config::get('common_module').'/');
|
||||
// 加载全局初始化文件
|
||||
if(is_file( COMMON_PATH . 'init' . EXT )) {
|
||||
include COMMON_PATH . 'init' . EXT;
|
||||
}else{
|
||||
// 检测全局配置文件
|
||||
if(is_file(COMMON_PATH . 'config' . EXT)) {
|
||||
Config::set(include COMMON_PATH . 'config' . EXT);
|
||||
}
|
||||
// 加载全局别名文件
|
||||
if(is_file(COMMON_PATH . 'alias' . EXT)) {
|
||||
Loader::addMap(include COMMON_PATH . 'alias' . EXT);
|
||||
}
|
||||
// 加载全局公共文件
|
||||
if(is_file( COMMON_PATH . 'common' . EXT)) {
|
||||
include COMMON_PATH . 'common' . EXT;
|
||||
}
|
||||
if(is_file(COMMON_PATH . 'tags' . EXT)) {
|
||||
// 全局行为扩展文件
|
||||
Tag::import(include COMMON_PATH . 'tags' . EXT);
|
||||
}
|
||||
}
|
||||
}
|
||||
Hook::listen('app_init');
|
||||
|
||||
define('COMMON_PATH', APP_PATH . $config['common_module'].'/');
|
||||
// 加载全局初始化文件
|
||||
if(is_file( COMMON_PATH . 'init' . EXT )) {
|
||||
include COMMON_PATH . 'init' . EXT;
|
||||
}else{
|
||||
// 检测全局配置文件
|
||||
if(is_file(COMMON_PATH . 'config' . EXT)) {
|
||||
$config = Config::set(include COMMON_PATH . 'config' . EXT);
|
||||
}
|
||||
// 加载全局别名文件
|
||||
if(is_file(COMMON_PATH . 'alias' . EXT)) {
|
||||
Loader::addMap(include COMMON_PATH . 'alias' . EXT);
|
||||
}
|
||||
// 加载全局公共文件
|
||||
if(is_file( COMMON_PATH . 'common' . EXT)) {
|
||||
include COMMON_PATH . 'common' . EXT;
|
||||
}
|
||||
if(is_file(COMMON_PATH . 'tags' . EXT)) {
|
||||
// 全局行为扩展文件
|
||||
Hook::import(include COMMON_PATH . 'tags' . EXT);
|
||||
}
|
||||
}
|
||||
|
||||
// 应用URL调度
|
||||
self::dispatch($config);
|
||||
|
||||
// 监听app_run
|
||||
Tag::listen('app_run');
|
||||
Hook::listen('app_run');
|
||||
|
||||
// 执行操作
|
||||
$instance = Loader::controller(CONTROLLER_NAME);
|
||||
if(!preg_match('/^[A-Za-z](\/|\w)*$/',CONTROLLER_NAME)){ // 安全检测
|
||||
$instance = false;
|
||||
}elseif($config['action_bind_class']){
|
||||
// 操作绑定到类:模块\Controller\控制器\操作
|
||||
$layer = $config['controller_layer'];
|
||||
if(is_dir(MODULE_PATH.$layer.'/'.CONTROLLER_NAME)){
|
||||
$namespace = MODULE_NAME.'\\'.$layer.'\\'.CONTROLLER_NAME.'\\';
|
||||
}else{
|
||||
// 空控制器
|
||||
$namespace = MODULE_NAME.'\\'.$layer.'\\_empty\\';
|
||||
}
|
||||
$actionName = strtolower(ACTION_NAME);
|
||||
if(class_exists($namespace.$actionName)){
|
||||
$class = $namespace.$actionName;
|
||||
}elseif(class_exists($namespace.'_empty')){
|
||||
// 空操作
|
||||
$class = $namespace.'_empty';
|
||||
}else{
|
||||
throw new Exception('_ERROR_ACTION_:'.ACTION_NAME);
|
||||
}
|
||||
$instance = new $class;
|
||||
// 操作绑定到类后 固定执行run入口
|
||||
$action = 'run';
|
||||
}else{
|
||||
$instance = Loader::controller(CONTROLLER_NAME);
|
||||
// 获取当前操作名
|
||||
$action = ACTION_NAME . $config['action_suffix'];
|
||||
}
|
||||
if(!$instance) {
|
||||
E('[ ' . MODULE_NAME . '\\Controller\\' . parse_name(CONTROLLER_NAME, 1) . 'Controller ] not exists');
|
||||
throw new Exception('[ ' . MODULE_NAME . '\\Controller\\' . parse_name(CONTROLLER_NAME, 1) . 'Controller ] not exists');
|
||||
}
|
||||
|
||||
// 获取当前操作名
|
||||
$action = ACTION_NAME . $config['action_suffix'];
|
||||
try{
|
||||
// 操作方法开始监听
|
||||
$call = [$instance, $action];
|
||||
Tag::listen('action_begin', $call);
|
||||
Hook::listen('action_begin', $call);
|
||||
if(!preg_match('/^[A-Za-z](\w)*$/', $action)){
|
||||
// 非法操作
|
||||
throw new \ReflectionException();
|
||||
@@ -88,9 +113,12 @@ class App {
|
||||
$vars = $_GET;
|
||||
}
|
||||
$params = $method->getParameters();
|
||||
$paramsBindType = $config['url_parmas_bind_type'];
|
||||
foreach ($params as $param){
|
||||
$name = $param->getName();
|
||||
if(isset($vars[$name])) {
|
||||
if( 1 == $paramsBindType && !empty($vars) ){
|
||||
$args[] = array_shift($vars);
|
||||
}if(0 == $paramsBindType && isset($vars[$name])) {
|
||||
$args[] = $vars[$name];
|
||||
}elseif($param->isDefaultValueAvailable()){
|
||||
$args[] = $param->getDefaultValue();
|
||||
@@ -98,12 +126,13 @@ class App {
|
||||
E('_PARAM_ERROR_:' . $name);
|
||||
}
|
||||
}
|
||||
array_walk_recursive($args,'Input::filterExp');
|
||||
$method->invokeArgs($instance, $args);
|
||||
}else{
|
||||
$method->invoke($instance);
|
||||
}
|
||||
// 操作方法执行完成监听
|
||||
Tag::listen('action_end', $call);
|
||||
Hook::listen('action_end', $call);
|
||||
}else{
|
||||
// 操作方法不是Public 抛出异常
|
||||
throw new \ReflectionException();
|
||||
@@ -114,11 +143,11 @@ class App {
|
||||
$method = new \ReflectionMethod($instance, '_empty');
|
||||
$method->invokeArgs($instance, [$action, '']);
|
||||
}else{
|
||||
E('[ ' . (new \ReflectionClass($instance))->getName() . ':' . $action . ' ] not exists ', 404);
|
||||
throw new Exception('[ ' . (new \ReflectionClass($instance))->getName() . ':' . $action . ' ] not exists ', 404);
|
||||
}
|
||||
}
|
||||
// 监听app_end
|
||||
Tag::listen('app_end');
|
||||
Hook::listen('app_end');
|
||||
return ;
|
||||
}
|
||||
|
||||
@@ -128,25 +157,20 @@ class App {
|
||||
* @return void
|
||||
*/
|
||||
static public function dispatch($config) {
|
||||
$var_m = $config['var_module'];
|
||||
$var_g = $config['var_group'];
|
||||
$var_c = $config['var_controller'];
|
||||
$var_a = $config['var_action'];
|
||||
$var_p = $config['var_pathinfo'];
|
||||
if(isset($_GET[$var_p])) { // 判断URL里面是否有兼容模式参数
|
||||
$_SERVER['PATH_INFO'] = $_GET[$var_p];
|
||||
unset($_GET[$var_p]);
|
||||
if(isset($_GET[$config['var_pathinfo']])) { // 判断URL里面是否有兼容模式参数
|
||||
$_SERVER['PATH_INFO'] = $_GET[$config['var_pathinfo']];
|
||||
unset($_GET[$config['var_pathinfo']]);
|
||||
}elseif(IS_CLI){ // CLI模式下 index.php module/controller/action/params/...
|
||||
$_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
|
||||
}
|
||||
|
||||
// 检测域名部署
|
||||
if(!IS_CLI) {
|
||||
Route::checkDomain();
|
||||
if(!IS_CLI && $config['sub_domain_deploy']) {
|
||||
Route::checkDomain($config);
|
||||
}
|
||||
|
||||
// 监听path_info
|
||||
Tag::listen('path_info');
|
||||
Hook::listen('path_info');
|
||||
// 分析PATHINFO信息
|
||||
if(!isset($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) {
|
||||
$types = explode(',', $config['pathinfo_fetch']);
|
||||
@@ -162,43 +186,34 @@ class App {
|
||||
}
|
||||
}
|
||||
|
||||
// 定位模块
|
||||
if(empty($_SERVER['PATH_INFO'])) {
|
||||
$_SERVER['PATH_INFO'] = '';
|
||||
}
|
||||
|
||||
// URL后缀
|
||||
define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'],PATHINFO_EXTENSION)));
|
||||
$_SERVER['PATH_INFO'] = trim(preg_replace('/\.(' . trim($config['url_html_suffix'], '.') . ')$/i', '', $_SERVER['PATH_INFO']), '/');
|
||||
if($_SERVER['PATH_INFO']) {
|
||||
if($config['url_deny_suffix'] && preg_match('/\.('.$config['url_deny_suffix'].')$/i', $_SERVER['PATH_INFO'])){
|
||||
exit;
|
||||
}
|
||||
$paths = explode($config['pathinfo_depr'], $_SERVER['PATH_INFO']);
|
||||
// 获取URL中的模块名
|
||||
if($config['require_module'] && !isset($_GET[$var_m])) {
|
||||
$_GET[$var_m] = array_shift($paths);
|
||||
$_SERVER['PATH_INFO'] = implode('/', $paths);
|
||||
}
|
||||
define('__INFO__','');
|
||||
define('__EXT__','');
|
||||
}else{
|
||||
define('__INFO__',trim($_SERVER['PATH_INFO'],'/'));
|
||||
// URL后缀
|
||||
define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'],PATHINFO_EXTENSION)));
|
||||
$_SERVER['PATH_INFO'] = __INFO__;
|
||||
if(!defined('BIND_MODULE')){
|
||||
if($config['url_deny_suffix'] && preg_match('/\.('.$config['url_deny_suffix'].')$/i', $_SERVER['PATH_INFO'])){
|
||||
exit;
|
||||
}
|
||||
$paths = explode($config['pathinfo_depr'], $_SERVER['PATH_INFO']);
|
||||
// 获取URL中的模块名
|
||||
if($config['require_module'] && !isset($_GET[$config['var_module']])) {
|
||||
$_GET[$config['var_module']] = array_shift($paths);
|
||||
$_SERVER['PATH_INFO'] = implode('/', $paths);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取模块名称
|
||||
$module = strtolower(isset($_GET[$var_m]) ? $_GET[$var_m] : $config['default_module']);
|
||||
if($maps = Config::get('url_module_map')) {
|
||||
if(isset($maps[$module])) {
|
||||
// 记录当前别名
|
||||
define('MODULE_ALIAS',$module);
|
||||
// 获取实际的项目名
|
||||
$module = $maps[MODULE_ALIAS];
|
||||
}elseif(array_search($module,$maps)){
|
||||
// 禁止访问原始项目
|
||||
$module = '';
|
||||
}
|
||||
}
|
||||
define('MODULE_NAME', ucwords($module));
|
||||
define('MODULE_NAME', defined('BIND_MODULE')? BIND_MODULE : self::getModule($config));
|
||||
|
||||
// 模块初始化
|
||||
if(MODULE_NAME && Config::get('common_module') != MODULE_NAME && is_dir( APP_PATH . MODULE_NAME )) {
|
||||
Tag::listen('app_begin');
|
||||
if(MODULE_NAME && $config['common_module'] != MODULE_NAME && is_dir( APP_PATH . MODULE_NAME )) {
|
||||
Hook::listen('app_begin');
|
||||
define('MODULE_PATH', APP_PATH . MODULE_NAME . '/');
|
||||
// 加载模块初始化文件
|
||||
if(is_file( MODULE_PATH . 'init' . EXT )) {
|
||||
@@ -223,33 +238,39 @@ class App {
|
||||
}
|
||||
if(is_file(MODULE_PATH . 'tags' . EXT)) {
|
||||
// 行为扩展文件
|
||||
Tag::import(include MODULE_PATH . 'tags' . EXT);
|
||||
Hook::import(include MODULE_PATH . 'tags' . EXT);
|
||||
}
|
||||
}
|
||||
$var_g = $config['var_group'];
|
||||
$var_c = $config['var_controller'];
|
||||
$var_a = $config['var_action'];
|
||||
}else{
|
||||
E('module not exists :' . MODULE_NAME);
|
||||
throw new Exception('module not exists :' . MODULE_NAME);
|
||||
}
|
||||
// 路由检测和控制器、操作解析
|
||||
Route::check($_SERVER['PATH_INFO']);
|
||||
|
||||
// 获取分组名
|
||||
if(Config::get('require_group')){
|
||||
define('GROUP_NAME', strtolower(isset($_GET[$var_g]) ? $_GET[$var_g] : $config['default_group']));
|
||||
}else{
|
||||
define('GROUP_NAME', '');
|
||||
}
|
||||
Route::check($_SERVER['PATH_INFO'],$config);
|
||||
|
||||
// 获取控制器名
|
||||
define('CONTROLLER_NAME', strtolower(isset($_GET[$var_c]) ? $_GET[$var_c] : $config['default_controller']));
|
||||
define('CONTROLLER_NAME', strip_tags(strtolower(isset($_GET[$config['var_controller']]) ? $_GET[$config['var_controller']] : $config['default_controller'])));
|
||||
|
||||
// 获取操作名
|
||||
define('ACTION_NAME', strtolower(isset($_GET[$var_a]) ? $_GET[$var_a] : $config['default_action']));
|
||||
define('ACTION_NAME', strip_tags(strtolower(isset($_GET[$config['var_action']]) ? $_GET[$config['var_action']] : $config['default_action'])));
|
||||
|
||||
unset($_GET[$var_a], $_GET[$var_c], $_GET[$var_m]);
|
||||
unset($_GET[$config['var_action']], $_GET[$config['var_controller']], $_GET[$config['var_module']]);
|
||||
//保证$_REQUEST正常取值
|
||||
$_REQUEST = array_merge($_POST, $_GET);
|
||||
}
|
||||
|
||||
static private function getModule($config){
|
||||
$module = strtolower(isset($_GET[$config['var_module']]) ? $_GET[$config['var_module']] : $config['default_module']);
|
||||
if($maps = $config['url_module_map']) {
|
||||
if(isset($maps[$module])) {
|
||||
// 记录当前别名
|
||||
define('MODULE_ALIAS',$module);
|
||||
// 获取实际的项目名
|
||||
$module = $maps[MODULE_ALIAS];
|
||||
}elseif(array_search($module,$maps)){
|
||||
// 禁止访问原始项目
|
||||
$module = '';
|
||||
}
|
||||
}
|
||||
return strip_tags(ucwords($module));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class TokenBuildBehavior extends Behavior {
|
||||
if(isset($_SESSION[$tokenName][$tokenKey])) {// 相同页面不重复生成session
|
||||
$tokenValue = $_SESSION[$tokenName][$tokenKey];
|
||||
}else{
|
||||
$tokenValue = $tokenType(microtime(TRUE));
|
||||
$tokenValue = $tokenType(microtime(true));
|
||||
$_SESSION[$tokenName][$tokenKey] = $tokenValue;
|
||||
}
|
||||
$token = '<input type="hidden" name="'.$tokenName.'" value="'.$tokenKey.'_'.$tokenValue.'" />';
|
||||
|
||||
@@ -27,12 +27,12 @@ class Cache {
|
||||
*/
|
||||
static public function connect($options=[]) {
|
||||
$type = !empty($options['type'])?$options['type']:'File';
|
||||
$class = 'Think\\Cache\\Driver\\'.ucwords($type);
|
||||
$class = 'Think\\Cache\\Driver\\'.ucwords($type);
|
||||
self::$handler = new $class($options);
|
||||
return self::$handler;
|
||||
}
|
||||
|
||||
static public function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$handler, $method), $params);
|
||||
}
|
||||
static public function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$handler, $method), $params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,15 +36,16 @@ class Config {
|
||||
|
||||
// 检测配置是否存在
|
||||
static public function has($name,$range=''){
|
||||
$range = $range?$range:self::$_range;
|
||||
$name = strtolower($name);
|
||||
// 优先执行设置获取或赋值
|
||||
$range = $range ? $range : self::$_range;
|
||||
$name = strtolower($name);
|
||||
|
||||
if (!strpos($name, '.')) {
|
||||
return isset(self::$_config[$range][$name]);
|
||||
}else{
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
return isset(self::$_config[$range][$name[0]][$name[1]]);
|
||||
}
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
return isset(self::$_config[$range][$name[0]][$name[1]]);
|
||||
}
|
||||
|
||||
// 获取配置参数 为空则获取所有配置
|
||||
@@ -55,13 +56,13 @@ class Config {
|
||||
return self::$_config[$range];
|
||||
}
|
||||
$name = strtolower($name);
|
||||
// 优先执行设置获取或赋值
|
||||
if (!strpos($name, '.')) {
|
||||
return isset(self::$_config[$range][$name]) ? self::$_config[$range][$name] : null;
|
||||
}else{
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
return isset(self::$_config[$range][$name[0]][$name[1]]) ? self::$_config[$range][$name[0]][$name[1]] : null;
|
||||
}
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
return isset(self::$_config[$range][$name[0]][$name[1]]) ? self::$_config[$range][$name[0]][$name[1]] : null;
|
||||
}
|
||||
|
||||
// 设置配置参数 name为数组则为批量设置
|
||||
@@ -74,18 +75,18 @@ class Config {
|
||||
$name = strtolower($name);
|
||||
if (!strpos($name, '.')) {
|
||||
self::$_config[$range][$name] = $value;
|
||||
return;
|
||||
}else{
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
self::$_config[$range][$name[0]][$name[1]] = $value;
|
||||
}
|
||||
// 二维数组设置和获取支持
|
||||
$name = explode('.', $name);
|
||||
self::$_config[$range][$name[0]][$name[1]] = $value;
|
||||
return;
|
||||
}
|
||||
// 批量设置
|
||||
if (is_array($name)){
|
||||
}elseif (is_array($name)){
|
||||
// 批量设置
|
||||
self::$_config[$range] = array_merge(self::$_config[$range], array_change_key_case($name));
|
||||
return self::$_config[$range];
|
||||
}else{
|
||||
return null; // 避免非法参数
|
||||
}
|
||||
return null; // 避免非法参数
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Think;
|
||||
use Think\View;
|
||||
use Think\Transform;
|
||||
|
||||
class Controller {
|
||||
// 视图类实例
|
||||
@@ -27,7 +28,7 @@ class Controller {
|
||||
'cache_path' => RUNTIME_PATH . 'Cache/',
|
||||
];
|
||||
$this->view = new View();
|
||||
$this->view->engine('think', $config);
|
||||
$this->view->engine(Config::get('template_engine'), $config);
|
||||
|
||||
//控制器初始化
|
||||
if(method_exists($this, '_initialize'))
|
||||
@@ -80,39 +81,41 @@ class Controller {
|
||||
* @return void
|
||||
*/
|
||||
protected function ajaxReturn($data, $type='') {
|
||||
if(empty($type)) $type = C('default_ajax_return');
|
||||
if(empty($type)) $type = Config::get('default_ajax_return');
|
||||
switch (strtoupper($type)){
|
||||
case 'JSON':
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
header('Content-Type:application/json; charset=utf-8');
|
||||
exit(Think\Transform::jsonEncode($data));
|
||||
$data = Transform::jsonEncode($data);
|
||||
break;
|
||||
case 'XML':
|
||||
// 返回xml格式数据
|
||||
header('Content-Type:text/xml; charset=utf-8');
|
||||
exit(Think\Transform::xmlEncode($data));
|
||||
$data = Transform::xmlEncode($data);
|
||||
break;
|
||||
case 'JSONP':
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
header('Content-Type:application/javascript; charset=utf-8');
|
||||
$handler = isset($_GET[C('var_jsonp_handler')]) ? $_GET[C('var_jsonp_handler')] : C('default_jsonp_handler');
|
||||
exit($handler . '(' . Think\Transform::jsonEncode($data) . ');');
|
||||
$data = $handler . '(' . Transform::jsonEncode($data) . ');';
|
||||
break;
|
||||
case 'SCRIPT':
|
||||
// 返回可执行的js脚本
|
||||
header('Content-Type:application/javascript; charset=utf-8');
|
||||
exit($data);
|
||||
break;
|
||||
case 'HTML':
|
||||
// 返回html片段
|
||||
header('Content-Type:text/html; charset=utf-8');
|
||||
echo $data;
|
||||
exit;
|
||||
break;
|
||||
case 'TEXT':
|
||||
// 返回一段纯文本
|
||||
header('Content-Type:text/plain; charset=utf-8');
|
||||
echo $data;
|
||||
exit;
|
||||
break;
|
||||
default:
|
||||
// 用于扩展其他返回格式数据
|
||||
Tag::listen('ajax_return', $data);
|
||||
$data = Hook::listen('ajax_return', $data);
|
||||
}
|
||||
exit($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,27 +161,35 @@ class Controller {
|
||||
$data['url'] = $jumpUrl;
|
||||
$this->ajaxReturn($data);
|
||||
}
|
||||
if(is_int($ajax)) $this->view->assign('waitSecond', $ajax);
|
||||
if(!empty($jumpUrl)) $this->view->assign('jumpUrl', $jumpUrl);
|
||||
// 模板变量
|
||||
$data = [];
|
||||
if(is_int($ajax))
|
||||
$data['waitSecond'] = $ajax;
|
||||
if(!empty($jumpUrl))
|
||||
$data['jumpUrl'] = $jumpUrl;
|
||||
|
||||
// 提示标题
|
||||
$this->view->assign('msgTitle', $status ? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
|
||||
$this->view->assign('status', $status); // 状态
|
||||
$data['msgTitle'] = $status ? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_');
|
||||
$data['status'] = $status; // 状态
|
||||
|
||||
//保证输出不受静态缓存影响
|
||||
C('HTML_CACHE_ON',false);
|
||||
Config::set('html_cache_on',false);
|
||||
if($status) { //发送成功信息
|
||||
$this->view->assign('message', $message);// 提示信息
|
||||
$data['message'] = $message;// 提示信息
|
||||
// 成功操作后默认停留1秒
|
||||
$this->view->assign('waitSecond', '1');
|
||||
$data['waitSecond'] = '1';
|
||||
// 默认操作成功自动返回操作前页面
|
||||
if(!$jumpUrl) $this->view->assign("jumpUrl", $_SERVER["HTTP_REFERER"]);
|
||||
$this->display(C('success_tmpl'));
|
||||
if(!$jumpUrl)
|
||||
$data["jumpUrl"] = $_SERVER["HTTP_REFERER"];
|
||||
$this->display(Config::get('success_tmpl'),$data);
|
||||
}else{
|
||||
$this->view->assign('error', $message);// 提示信息
|
||||
$data['error'] = $message;// 提示信息
|
||||
//发生错误时候默认停留3秒
|
||||
$this->view->assign('waitSecond', '3');
|
||||
$data['waitSecond'] = '3';
|
||||
// 默认发生错误的话自动返回上页
|
||||
if(!$jumpUrl) $this->view->assign('jumpUrl', 'javascript:history.back(-1);');
|
||||
$this->display(C('error_tmpl'));
|
||||
if(!$jumpUrl)
|
||||
$data['jumpUrl'] = 'javascript:history.back(-1);';
|
||||
$this->display(Config::get('error_tmpl'),$data);
|
||||
// 中止执行 避免出错后继续执行
|
||||
exit ;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ class Cookie {
|
||||
'expire' => 0, // cookie 保存时间
|
||||
'path' => '/', // cookie 保存路径
|
||||
'domain' => '', // cookie 有效域名
|
||||
'secure' => false, // cookie 启用安全传输
|
||||
'httponly' => '', // httponly设置
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -27,6 +29,9 @@ class Cookie {
|
||||
*/
|
||||
static public function init($config=[]){
|
||||
self::$config = array_merge(self::$config, array_change_key_case($config));
|
||||
if(!empty(self::$config['httponly'])){
|
||||
ini_set('session.cookie_httponly', 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +71,7 @@ class Cookie {
|
||||
$value = 'think:'.json_encode(array_map('urlencode',$value));
|
||||
}
|
||||
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
|
||||
setcookie($name, $value, $expire, $config['path'], $config['domain']);
|
||||
setcookie($name, $value, $expire, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
|
||||
$_COOKIE[$name] = $value;
|
||||
}
|
||||
|
||||
@@ -77,11 +82,11 @@ class Cookie {
|
||||
* @return mixed
|
||||
*/
|
||||
static public function get($name, $prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$config['prefix'];
|
||||
$prefix = $prefix ? $prefix : self::$config['prefix'];
|
||||
$name = $prefix . $name;
|
||||
if(isset($_COOKIE[$name])){
|
||||
$value = $_COOKIE[$name];
|
||||
if(0===strpos($value,'think:')){
|
||||
if(0 === strpos($value,'think:')){
|
||||
$value = substr($value,6);
|
||||
return array_map('urldecode',json_decode($value,true));
|
||||
}else{
|
||||
@@ -99,9 +104,10 @@ class Cookie {
|
||||
* @return mixed
|
||||
*/
|
||||
static public function delete($name, $prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$config['prefix'];
|
||||
$name = $prefix . $name;
|
||||
setcookie($name, '', time() - 3600, self::$config['path'], self::$config['domain']);
|
||||
$config = self::$config;
|
||||
$prefix = $prefix ? $prefix : $config['prefix'];
|
||||
$name = $prefix . $name;
|
||||
setcookie($name, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
|
||||
unset($_COOKIE[$name]); // 删除指定cookie
|
||||
}
|
||||
|
||||
@@ -115,11 +121,12 @@ class Cookie {
|
||||
if (empty($_COOKIE))
|
||||
return;
|
||||
// 要删除的cookie前缀,不指定则删除config设置的指定前缀
|
||||
$prefix = $prefix ? $prefix: self::$config['prefix'];
|
||||
$config = self::$config;
|
||||
$prefix = $prefix ? $prefix: $config['prefix'];
|
||||
if ($prefix) {// 如果前缀为空字符串将不作处理直接返回
|
||||
foreach ($_COOKIE as $key => $val) {
|
||||
if (0 === strpos($key, $prefix)) {
|
||||
setcookie($key, '', time() - 3600, self::$config['path'], self::$config['domain']);
|
||||
setcookie($key, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
|
||||
unset($_COOKIE[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
class Crypt {
|
||||
/**
|
||||
* 加密字符串
|
||||
* @access public
|
||||
* @param string $str 字符串
|
||||
* @param string $key 加密key
|
||||
* @return string
|
||||
*/
|
||||
static public function encrypt($data,$key,$expire=0){
|
||||
$key = md5($key);
|
||||
$data = base64_encode($data);
|
||||
$x = 0;
|
||||
$len = strlen($data);
|
||||
$l = strlen($key);
|
||||
$char = '';
|
||||
for ($i = 0; $i< $len; $i++) {
|
||||
if ($x == $l) $x = 0;
|
||||
$char .=substr($key, $x, 1);
|
||||
$x++;
|
||||
}
|
||||
$str = sprintf('%010d', $expire ? $expire + time() : 0);
|
||||
for ($i=0; $i< $len; $i++) {
|
||||
$str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1))) % 256);
|
||||
}
|
||||
return str_replace('=', '', base64_encode($str));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密字符串
|
||||
* @access public
|
||||
* @param string $str 字符串
|
||||
* @param string $key 加密key
|
||||
* @return string
|
||||
*/
|
||||
static public function decrypt($data,$key){
|
||||
$key = md5($key);
|
||||
$x = 0;
|
||||
$data = base64_decode($data);
|
||||
$expire = substr($data,0,10);
|
||||
$data = substr($data,10);
|
||||
if($expire > 0 && $expire<time()) {
|
||||
return '';
|
||||
}
|
||||
$len = strlen($data);
|
||||
$l = strlen($key);
|
||||
$char = $str = '';
|
||||
for ($i=0; $i< $len; $i++) {
|
||||
if ($x == $l) $x = 0;
|
||||
$char .= substr($key, $x, 1);
|
||||
$x++;
|
||||
}
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
|
||||
$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
|
||||
}else{
|
||||
$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
|
||||
}
|
||||
}
|
||||
return base64_decode($str);
|
||||
}
|
||||
}
|
||||
@@ -91,7 +91,7 @@ class Db {
|
||||
}
|
||||
|
||||
// 调用驱动类的方法
|
||||
static public function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$_instance, $method), $params);
|
||||
}
|
||||
static public function __callStatic($method, $params){
|
||||
return call_user_func_array(array(self::$_instance, $method), $params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,10 @@ class Debug {
|
||||
*/
|
||||
static public function remark($name,$value='') {
|
||||
// 记录时间和内存使用
|
||||
self::$_info[$name] = is_float($value)?$value:microtime(TRUE);
|
||||
if('time' != $value && function_exists('memory_get_usage')) {
|
||||
self::$_mem['mem'][$name] = is_float($value)?$value:memory_get_usage();
|
||||
self::$_mem['peak'][$name] = function_exists('memory_get_peak_usage')?memory_get_peak_usage(): self::$_mem['mem'][$name];
|
||||
self::$_info[$name] = is_float($value) ? $value : microtime(true);
|
||||
if('time' != $value ) {
|
||||
self::$_mem['mem'][$name] = is_float($value) ? $value : memory_get_usage();
|
||||
self::$_mem['peak'][$name] = function_exists('memory_get_peak_usage') ? memory_get_peak_usage() : self::$_mem['mem'][$name];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,11 +35,12 @@ class Debug {
|
||||
* 统计某个区间的时间(微秒)使用情况
|
||||
* @param string $start 开始标签
|
||||
* @param string $end 结束标签
|
||||
* @param integer|string $dec 小数位或者m
|
||||
* @param integer|string $dec 小数位
|
||||
* @return mixed
|
||||
*/
|
||||
static public function getUseTime($start,$end,$dec=6) {
|
||||
if(!isset(self::$_info[$end])) self::$_info[$end] = microtime(TRUE);
|
||||
if(!isset(self::$_info[$end]))
|
||||
self::$_info[$end] = microtime(true);
|
||||
return number_format((self::$_info[$end]-self::$_info[$start]),$dec);
|
||||
}
|
||||
|
||||
@@ -47,7 +48,7 @@ class Debug {
|
||||
* 记录内存使用情况
|
||||
* @param string $start 开始标签
|
||||
* @param string $end 结束标签
|
||||
* @param integer|string $dec 小数位或者m
|
||||
* @param integer|string $dec 小数位
|
||||
* @return mixed
|
||||
*/
|
||||
static public function getUseMem($start,$end,$dec=2) {
|
||||
@@ -67,11 +68,12 @@ class Debug {
|
||||
* 统计内存峰值情况
|
||||
* @param string $start 开始标签
|
||||
* @param string $end 结束标签
|
||||
* @param integer|string $dec 小数位或者m
|
||||
* @param integer|string $dec 小数位
|
||||
* @return mixed
|
||||
*/
|
||||
static public function getMemPeak($start,$end,$dec=2) {
|
||||
if(!isset(self::$_mem['peak'][$end])) self::$_mem['peak'][$end] = function_exists('memory_get_peak_usage')?memory_get_peak_usage():memory_get_usage();
|
||||
if(!isset(self::$_mem['peak'][$end]))
|
||||
self::$_mem['peak'][$end] = function_exists('memory_get_peak_usage') ? memory_get_peak_usage() : memory_get_usage();
|
||||
$size = self::$_mem['peak'][$end]-self::$_mem['peak'][$start];
|
||||
$a = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$pos = 0;
|
||||
@@ -85,7 +87,7 @@ class Debug {
|
||||
/**
|
||||
* 浏览器友好的变量输出
|
||||
* @param mixed $var 变量
|
||||
* @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
|
||||
* @param boolean $echo 是否输出 默认为true 如果为false 则返回输出字符串
|
||||
* @param string $label 标签 默认为空
|
||||
* @return void|string
|
||||
*/
|
||||
|
||||
@@ -12,5 +12,5 @@
|
||||
namespace Think;
|
||||
|
||||
class Exception extends \Exception {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,222 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
class Filter {
|
||||
//html标签设置
|
||||
static public $htmlTags = [
|
||||
'allow' => 'table|td|th|tr|i|b|u|strong|img|p|br|div|strong|em|ul|ol|li|dl|dd|dt|a',
|
||||
'ban' => 'html|head|meta|link|base|basefont|body|bgsound|title|style|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name|script|style|xml',
|
||||
];
|
||||
|
||||
static public function filter($data,$filter,$option=''){
|
||||
return filter_var($data,is_int($filter)?$filter:filter_id($filter),$option);
|
||||
}
|
||||
|
||||
static private function filter_input($type,$name,$filter,$options=''){
|
||||
return filter_input($type,$name,is_int($filter)?$filter:filter_id($filter),$option);
|
||||
}
|
||||
|
||||
static public function get($name,$filter,$option=''){
|
||||
return self::filter_input(INPUT_GET,$name,$filter,$option);
|
||||
}
|
||||
|
||||
static public function post($name,$filter,$option=''){
|
||||
return self::filter_input(INPUT_POST,$name,$filter,$option);
|
||||
}
|
||||
|
||||
static public function cookie($name,$filter,$option=''){
|
||||
return self::filter_input(INPUT_COOKIE,$name,$filter,$option);
|
||||
}
|
||||
|
||||
static public function server($name,$filter,$option=''){
|
||||
return self::filter_input(INPUT_SERVER,$name,$filter,$option);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字符串,以便可以正常进行搜索
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function forSearch($string) {
|
||||
return str_replace( ['%','_'], ['\%','\_'], $string );
|
||||
}
|
||||
|
||||
/**
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function forShow($string) {
|
||||
return self::nl2Br( self::hsc($string) );
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理纯文本数据,以便在textarea标签中显示
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function forTarea($string) {
|
||||
return str_ireplace(['<textarea>','</textarea>'], ['<textarea>','</textarea>'], $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据中的单引号和双引号进行转义
|
||||
* @access public
|
||||
* @param string $text 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function forTag($string) {
|
||||
return str_replace(['"',"'"], ['"','''], $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把换行转换为<br />标签
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function nl2Br($string) {
|
||||
return nl2Br($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果 magic_quotes_gpc 为关闭状态,这个函数可以转义字符串
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function addSlashes($string) {
|
||||
return addslashes($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于在textbox表单中显示html代码
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static function hsc($string) {
|
||||
return preg_replace(["/&/i", "/ /i"], ['&', '&nbsp;'], htmlspecialchars($string, ENT_QUOTES));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是hsc()方法的逆操作
|
||||
* @access public
|
||||
* @param string $text 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static function undoHsc($text) {
|
||||
return preg_replace(["/>/i", "/</i", "/"/i", "/'/i", '/&nbsp;/i'], [">", "<", "\"", "'", " "], $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出安全的html,用于过滤危险代码
|
||||
* @access public
|
||||
* @param string $text 要处理的字符串
|
||||
* @param mixed $allowTags 允许的标签列表,如 table|td|th|td
|
||||
* @return string
|
||||
*/
|
||||
static public function safeHtml($text, $allowTags = null) {
|
||||
$text = trim($text);
|
||||
//完全过滤注释
|
||||
$text = preg_replace('/<!--?.*-->/','',$text);
|
||||
//完全过滤动态代码
|
||||
$text = preg_replace('/<\?|\?'.'>/','',$text);
|
||||
//完全过滤js
|
||||
$text = preg_replace('/<script?.*\/script>/','',$text);
|
||||
|
||||
$text = str_replace('[','[',$text);
|
||||
$text = str_replace(']',']',$text);
|
||||
$text = str_replace('|','|',$text);
|
||||
//过滤换行符
|
||||
$text = preg_replace('/\r?\n/','',$text);
|
||||
//br
|
||||
$text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);
|
||||
$text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
|
||||
//过滤危险的属性,如:过滤on事件lang js
|
||||
while(preg_match('/(<[^><]+)(lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],$mat[1],$text);
|
||||
}
|
||||
while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],$mat[1].$mat[3],$text);
|
||||
}
|
||||
if( empty($allowTags) ) { $allowTags = self::$htmlTags['allow']; }
|
||||
//允许的HTML标签
|
||||
$text = preg_replace('/<('.$allowTags.')( [^><\[\]]*)>/i','[\1\2]',$text);
|
||||
//过滤多余html
|
||||
if ( empty($banTag) ) { $banTag = self::$htmlTags['ban']; }
|
||||
$text = preg_replace('/<\/?('.$banTag.')[^><]*>/i','',$text);
|
||||
//过滤合法的html标签
|
||||
while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
|
||||
}
|
||||
//转换引号
|
||||
while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
|
||||
}
|
||||
//空属性转换
|
||||
$text = str_replace('\'\'','||',$text);
|
||||
$text = str_replace('""','||',$text);
|
||||
//过滤错误的单个引号
|
||||
while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
|
||||
}
|
||||
//转换其它所有不合法的 < >
|
||||
$text = str_replace('<','<',$text);
|
||||
$text = str_replace('>','>',$text);
|
||||
$text = str_replace('"','"',$text);
|
||||
//反转换
|
||||
$text = str_replace('[','<',$text);
|
||||
$text = str_replace(']','>',$text);
|
||||
$text = str_replace('|','"',$text);
|
||||
//过滤多余空格
|
||||
$text = str_replace(' ',' ',$text);
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除html标签,得到纯文本。可以处理嵌套的标签
|
||||
* @access public
|
||||
* @param string $string 要处理的html
|
||||
* @return string
|
||||
*/
|
||||
static public function deleteHtmlTags($string) {
|
||||
while(strstr($string, '>')) {
|
||||
$currentBeg = strpos($string, '<');
|
||||
$currentEnd = strpos($string, '>');
|
||||
$tmpStringBeg = @substr($string, 0, $currentBeg);
|
||||
$tmpStringEnd = @substr($string, $currentEnd + 1, strlen($string));
|
||||
$string = $tmpStringBeg.$tmpStringEnd;
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文本中的换行
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @param mixed $br 对换行的处理,
|
||||
* false:去除换行;true:保留原样;string:替换成string
|
||||
* @return string
|
||||
*/
|
||||
static public function nl2($string, $br = '<br />') {
|
||||
if ($br == false) {
|
||||
$string = preg_replace("/(\015\012)|(\015)|(\012)/", '', $string);
|
||||
} elseif ($br != true){
|
||||
$string = preg_replace("/(\015\012)|(\015)|(\012)/", $br, $string);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
86
Library/Think/Hook.php
Normal file
86
Library/Think/Hook.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
class Hook {
|
||||
|
||||
static private $tags = [];
|
||||
|
||||
/**
|
||||
* 动态添加行为扩展到某个标签
|
||||
* @param string $tag 标签名称
|
||||
* @param mixed $behavior 行为名称
|
||||
* @return void
|
||||
*/
|
||||
static public function add($tag,$behavior) {
|
||||
if(is_array($behavior)) {
|
||||
self::$tags[$tag] = array_merge(self::$tags[$tag],$behavior);
|
||||
}else{
|
||||
self::$tags[$tag][] = $behavior;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量导入行为
|
||||
* @param array $tags 标签行为
|
||||
* @return void
|
||||
*/
|
||||
static public function import($tags) {
|
||||
self::$tags = array_merge(self::$tags,$tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听标签的行为
|
||||
* @param string $tag 标签名称
|
||||
* @param mixed $params 传入参数
|
||||
* @return void
|
||||
*/
|
||||
static public function listen($tag, &$params=null) {
|
||||
if(isset(self::$tags[$tag])) {
|
||||
foreach (self::$tags[$tag] as $name) {
|
||||
|
||||
Config::get('app_debug') && Debug::remark('behavior_start','time');
|
||||
|
||||
$result = self::exec($name, $tag,$params);
|
||||
|
||||
if(Config::get('app_debug')){
|
||||
Debug::remark('behavior_end','time');
|
||||
Log::record('Run '.$name.' [ RunTime:'.Debug::getUseTime('behavior_start','behavior_end').'s ]','INFO');
|
||||
}
|
||||
if(false === $result) {
|
||||
// 如果返回false 则中断行为执行
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行某个行为
|
||||
* @param string $name 行为名称
|
||||
* @param string $tag 方法名(标签名)
|
||||
* @param Mixed $params 传人的参数
|
||||
* @return void
|
||||
*/
|
||||
static public function exec($name, $tag,&$params=null) {
|
||||
if($name instanceof \Closure) {
|
||||
return $name($params);
|
||||
}
|
||||
if('Behavior' == substr($name,-8) ){
|
||||
// 行为扩展必须用run入口方法
|
||||
$tag = 'run';
|
||||
}
|
||||
$addon = new $name();
|
||||
return $addon->$tag($params);
|
||||
}
|
||||
}
|
||||
@@ -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: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
/* 缩略图相关常量定义 */
|
||||
define('THINKIMAGE_THUMB_SCALING', 1); //常量,标识缩略图等比例缩放类型
|
||||
define('THINKIMAGE_THUMB_FILLED', 2); //常量,标识缩略图缩放后填充类型
|
||||
define('THINKIMAGE_THUMB_CENTER', 3); //常量,标识缩略图居中裁剪类型
|
||||
define('THINKIMAGE_THUMB_NORTHWEST', 4); //常量,标识缩略图左上角裁剪类型
|
||||
define('THINKIMAGE_THUMB_SOUTHEAST', 5); //常量,标识缩略图右下角裁剪类型
|
||||
define('THINKIMAGE_THUMB_FIXED', 6); //常量,标识缩略图固定尺寸缩放类型
|
||||
|
||||
/* 水印相关常量定义 */
|
||||
define('THINKIMAGE_WATER_NORTHWEST', 1); //常量,标识左上角水印
|
||||
define('THINKIMAGE_WATER_NORTH', 2); //常量,标识上居中水印
|
||||
define('THINKIMAGE_WATER_NORTHEAST', 3); //常量,标识右上角水印
|
||||
define('THINKIMAGE_WATER_WEST', 4); //常量,标识左居中水印
|
||||
define('THINKIMAGE_WATER_CENTER', 5); //常量,标识居中水印
|
||||
define('THINKIMAGE_WATER_EAST', 6); //常量,标识右居中水印
|
||||
define('THINKIMAGE_WATER_SOUTHWEST', 7); //常量,标识左下角水印
|
||||
define('THINKIMAGE_WATER_SOUTH', 8); //常量,标识下居中水印
|
||||
define('THINKIMAGE_WATER_SOUTHEAST', 9); //常量,标识右下角水印
|
||||
|
||||
/**
|
||||
* 图片处理驱动类,可配置图片处理库
|
||||
* 目前支持GD库和imagick
|
||||
* @author 麦当苗儿 <zuojiazi.cn@gmail.com>
|
||||
*/
|
||||
class Image {
|
||||
/**
|
||||
* 图片资源
|
||||
* @var resource
|
||||
*/
|
||||
private static $im;
|
||||
|
||||
/**
|
||||
* 初始化方法,用于实例化一个图片处理对象
|
||||
* @param string $type 要使用的类库,默认使用GD库
|
||||
*/
|
||||
static public function init($type = 'Gd', $imgname = null){
|
||||
/* 引入处理库,实例化图片处理对象 */
|
||||
$class = '\\Think\\Image\\Driver\\'.ucwords($type);
|
||||
self::$im = new $class($imgname);
|
||||
return self::$im;
|
||||
}
|
||||
|
||||
// 调用驱动类的方法
|
||||
static public function __callStatic($method, $params){
|
||||
self::$im || self::init();
|
||||
return call_user_func_array([self::$im, $method], $params);
|
||||
}
|
||||
}
|
||||
@@ -1,549 +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: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Image\Driver;
|
||||
|
||||
class Gd{
|
||||
/**
|
||||
* 图像资源对象
|
||||
* @var resource
|
||||
*/
|
||||
private $im;
|
||||
private $gif;
|
||||
/**
|
||||
* 图像信息,包括width,height,type,mime,size
|
||||
* @var array
|
||||
*/
|
||||
private $info;
|
||||
|
||||
/**
|
||||
* 构造方法,可用于打开一张图像
|
||||
* @param string $imgname 图像路径
|
||||
*/
|
||||
public function __construct($imgname = null) {
|
||||
$imgname && $this->open($imgname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开一张图像
|
||||
* @param string $imgname 图像路径
|
||||
*/
|
||||
public function open($imgname){
|
||||
//检测图像文件
|
||||
if(!is_file($imgname)) throw new \Exception('不存在的图像文件');
|
||||
|
||||
//获取图像信息
|
||||
$info = getimagesize($imgname);
|
||||
|
||||
//检测图像合法性
|
||||
if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){
|
||||
throw new \Exception('非法图像文件');
|
||||
}
|
||||
|
||||
//设置图像信息
|
||||
$this->info = [
|
||||
'width' => $info[0],
|
||||
'height' => $info[1],
|
||||
'type' => image_type_to_extension($info[2], false),
|
||||
'mime' => $info['mime'],
|
||||
];
|
||||
|
||||
//销毁已存在的图像
|
||||
empty($this->im) || imagedestroy($this->im);
|
||||
|
||||
//打开图像
|
||||
if('gif' == $this->info['type']){
|
||||
$class = '\\Think\\Image\\Driver\\Gif';
|
||||
$this->gif = new $class($imgname);
|
||||
$this->im = imagecreatefromstring($this->gif->image());
|
||||
} else {
|
||||
$fun = "imagecreatefrom{$this->info['type']}";
|
||||
$this->im = $fun($imgname);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存图像
|
||||
* @param string $imgname 图像保存名称
|
||||
* @param string $type 图像类型
|
||||
* @param boolean $interlace 是否对JPEG类型图像设置隔行扫描
|
||||
*/
|
||||
public function save($imgname, $type = null, $interlace = true){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被保存的图像资源');
|
||||
|
||||
//自动获取图像类型
|
||||
if(is_null($type)){
|
||||
$type = $this->info['type'];
|
||||
} else {
|
||||
$type = strtolower($type);
|
||||
}
|
||||
|
||||
//JPEG图像设置隔行扫描
|
||||
if('jpeg' == $type || 'jpg' == $type){
|
||||
$type = 'jpeg';
|
||||
imageinterlace($this->im, $interlace);
|
||||
}
|
||||
|
||||
//保存图像
|
||||
if('gif' == $type && !empty($this->gif)){
|
||||
$this->gif->save($imgname);
|
||||
} else {
|
||||
$fun = "image{$type}";
|
||||
$fun($this->im, $imgname);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像宽度
|
||||
* @return integer 图像宽度
|
||||
*/
|
||||
public function width(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['width'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像高度
|
||||
* @return integer 图像高度
|
||||
*/
|
||||
public function height(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['height'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像类型
|
||||
* @return string 图像类型
|
||||
*/
|
||||
public function type(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像MIME类型
|
||||
* @return string 图像MIME类型
|
||||
*/
|
||||
public function mime(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['mime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
|
||||
* @return array 图像尺寸
|
||||
*/
|
||||
public function size(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return [$this->info['width'], $this->info['height']];
|
||||
}
|
||||
|
||||
/**
|
||||
* 裁剪图像
|
||||
* @param integer $w 裁剪区域宽度
|
||||
* @param integer $h 裁剪区域高度
|
||||
* @param integer $x 裁剪区域x坐标
|
||||
* @param integer $y 裁剪区域y坐标
|
||||
* @param integer $width 图像保存宽度
|
||||
* @param integer $height 图像保存高度
|
||||
*/
|
||||
public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被裁剪的图像资源');
|
||||
|
||||
//设置保存尺寸
|
||||
empty($width) && $width = $w;
|
||||
empty($height) && $height = $h;
|
||||
|
||||
do {
|
||||
//创建新图像
|
||||
$img = imagecreatetruecolor($width, $height);
|
||||
// 调整默认颜色
|
||||
$color = imagecolorallocate($img, 255, 255, 255);
|
||||
imagefill($img, 0, 0, $color);
|
||||
|
||||
//裁剪
|
||||
imagecopyresampled($img, $this->im, 0, 0, $x, $y, $width, $height, $w, $h);
|
||||
imagedestroy($this->im); //销毁原图
|
||||
|
||||
//设置新图像
|
||||
$this->im = $img;
|
||||
} while(!empty($this->gif) && $this->gifNext());
|
||||
|
||||
$this->info['width'] = $width;
|
||||
$this->info['height'] = $height;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成缩略图
|
||||
* @param integer $width 缩略图最大宽度
|
||||
* @param integer $height 缩略图最大高度
|
||||
* @param integer $type 缩略图裁剪类型
|
||||
*/
|
||||
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALE){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被缩略的图像资源');
|
||||
|
||||
//原图宽度和高度
|
||||
$w = $this->info['width'];
|
||||
$h = $this->info['height'];
|
||||
|
||||
/* 计算缩略图生成的必要参数 */
|
||||
switch ($type) {
|
||||
/* 等比例缩放 */
|
||||
case THINKIMAGE_THUMB_SCALING:
|
||||
//原图尺寸小于缩略图尺寸则不进行缩略
|
||||
if($w < $width && $h < $height) return;
|
||||
|
||||
//计算缩放比例
|
||||
$scale = min($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$x = $y = 0;
|
||||
$width = $w * $scale;
|
||||
$height = $h * $scale;
|
||||
break;
|
||||
|
||||
/* 居中裁剪 */
|
||||
case THINKIMAGE_THUMB_CENTER:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
$x = ($this->info['width'] - $w)/2;
|
||||
$y = ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 左上角裁剪 */
|
||||
case THINKIMAGE_THUMB_NORTHWEST:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$x = $y = 0;
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
break;
|
||||
|
||||
/* 右下角裁剪 */
|
||||
case THINKIMAGE_THUMB_SOUTHEAST:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
$x = $this->info['width'] - $w;
|
||||
$y = $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 填充 */
|
||||
case THINKIMAGE_THUMB_FILLED:
|
||||
//计算缩放比例
|
||||
if($w < $width && $h < $height){
|
||||
$scale = 1;
|
||||
} else {
|
||||
$scale = min($width/$w, $height/$h);
|
||||
}
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$neww = $w * $scale;
|
||||
$newh = $h * $scale;
|
||||
$posx = ($width - $w * $scale)/2;
|
||||
$posy = ($height - $h * $scale)/2;
|
||||
|
||||
do{
|
||||
//创建新图像
|
||||
$img = imagecreatetruecolor($width, $height);
|
||||
// 调整默认颜色
|
||||
$color = imagecolorallocate($img, 255, 255, 255);
|
||||
imagefill($img, 0, 0, $color);
|
||||
|
||||
//裁剪
|
||||
imagecopyresampled($img, $this->im, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
|
||||
imagedestroy($this->im); //销毁原图
|
||||
$this->im = $img;
|
||||
} while(!empty($this->gif) && $this->gifNext());
|
||||
|
||||
$this->info['width'] = $width;
|
||||
$this->info['height'] = $height;
|
||||
return $this;
|
||||
|
||||
/* 固定 */
|
||||
case THINKIMAGE_THUMB_FIXED:
|
||||
$x = $y = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('不支持的缩略图裁剪类型');
|
||||
}
|
||||
|
||||
/* 裁剪图像 */
|
||||
$this->crop($w, $h, $x, $y, $width, $height);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加水印
|
||||
* @param string $source 水印图片路径
|
||||
* @param integer $locate 水印位置
|
||||
* @param integer $alpha 水印透明度
|
||||
*/
|
||||
public function water($source, $locate = THINKIMAGE_WATER_SOUTHEAST){
|
||||
//资源检测
|
||||
if(empty($this->im)) throw new \Exception('没有可以被添加水印的图像资源');
|
||||
if(!is_file($source)) throw new \Exception('水印图像不存在');
|
||||
|
||||
//获取水印图像信息
|
||||
$info = getimagesize($source);
|
||||
if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){
|
||||
throw new \Exception('非法水印文件');
|
||||
}
|
||||
|
||||
//创建水印图像资源
|
||||
$fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
|
||||
$water = $fun($source);
|
||||
|
||||
//设定水印图像的混色模式
|
||||
imagealphablending($water, true);
|
||||
|
||||
/* 设定水印位置 */
|
||||
switch ($locate) {
|
||||
/* 右下角水印 */
|
||||
case THINKIMAGE_WATER_SOUTHEAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 左下角水印 */
|
||||
case THINKIMAGE_WATER_SOUTHWEST:
|
||||
$x = 0;
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 左上角水印 */
|
||||
case THINKIMAGE_WATER_NORTHWEST:
|
||||
$x = $y = 0;
|
||||
break;
|
||||
|
||||
/* 右上角水印 */
|
||||
case THINKIMAGE_WATER_NORTHEAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = 0;
|
||||
break;
|
||||
|
||||
/* 居中水印 */
|
||||
case THINKIMAGE_WATER_CENTER:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
/* 下居中水印 */
|
||||
case THINKIMAGE_WATER_SOUTH:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 右居中水印 */
|
||||
case THINKIMAGE_WATER_EAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
/* 上居中水印 */
|
||||
case THINKIMAGE_WATER_NORTH:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = 0;
|
||||
break;
|
||||
|
||||
/* 左居中水印 */
|
||||
case THINKIMAGE_WATER_WEST:
|
||||
$x = 0;
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 自定义水印坐标 */
|
||||
if(is_array($locate)){
|
||||
list($x, $y) = $locate;
|
||||
} else {
|
||||
throw new \Exception('不支持的水印位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
do{
|
||||
//添加水印
|
||||
$src = imagecreatetruecolor($info[0], $info[1]);
|
||||
// 调整默认颜色
|
||||
$color = imagecolorallocate($src, 255, 255, 255);
|
||||
imagefill($src, 0, 0, $color);
|
||||
|
||||
imagecopy($src, $this->im, 0, 0, $x, $y, $info[0], $info[1]);
|
||||
imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
|
||||
imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], 100);
|
||||
|
||||
//销毁零时图片资源
|
||||
imagedestroy($src);
|
||||
} while(!empty($this->gif) && $this->gifNext());
|
||||
|
||||
//销毁水印资源
|
||||
imagedestroy($water);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图像添加文字
|
||||
* @param string $text 添加的文字
|
||||
* @param string $font 字体路径
|
||||
* @param integer $size 字号
|
||||
* @param string $color 文字颜色
|
||||
* @param integer $locate 文字写入位置
|
||||
* @param integer $offset 文字相对当前位置的偏移量
|
||||
* @param integer $angle 文字倾斜角度
|
||||
*/
|
||||
public function text($text, $font, $size, $color = '#00000000',
|
||||
$locate = THINKIMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0){
|
||||
//资源检测
|
||||
if(empty($this->im)) throw new \Exception('没有可以被写入文字的图像资源');
|
||||
if(!is_file($font)) throw new \Exception("不存在的字体文件:{$font}");
|
||||
|
||||
//获取文字信息
|
||||
$info = imagettfbbox($size, $angle, $font, $text);
|
||||
$minx = min($info[0], $info[2], $info[4], $info[6]);
|
||||
$maxx = max($info[0], $info[2], $info[4], $info[6]);
|
||||
$miny = min($info[1], $info[3], $info[5], $info[7]);
|
||||
$maxy = max($info[1], $info[3], $info[5], $info[7]);
|
||||
|
||||
/* 计算文字初始坐标和尺寸 */
|
||||
$x = $minx;
|
||||
$y = abs($miny);
|
||||
$w = $maxx - $minx;
|
||||
$h = $maxy - $miny;
|
||||
|
||||
/* 设定文字位置 */
|
||||
switch ($locate) {
|
||||
/* 右下角文字 */
|
||||
case THINKIMAGE_WATER_SOUTHEAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 左下角文字 */
|
||||
case THINKIMAGE_WATER_SOUTHWEST:
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 左上角文字 */
|
||||
case THINKIMAGE_WATER_NORTHWEST:
|
||||
// 起始坐标即为左上角坐标,无需调整
|
||||
break;
|
||||
|
||||
/* 右上角文字 */
|
||||
case THINKIMAGE_WATER_NORTHEAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
break;
|
||||
|
||||
/* 居中文字 */
|
||||
case THINKIMAGE_WATER_CENTER:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 下居中文字 */
|
||||
case THINKIMAGE_WATER_SOUTH:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 右居中文字 */
|
||||
case THINKIMAGE_WATER_EAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 上居中文字 */
|
||||
case THINKIMAGE_WATER_NORTH:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
break;
|
||||
|
||||
/* 左居中文字 */
|
||||
case THINKIMAGE_WATER_WEST:
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 自定义文字坐标 */
|
||||
if(is_array($locate)){
|
||||
list($posx, $posy) = $locate;
|
||||
$x += $posx;
|
||||
$y += $posy;
|
||||
} else {
|
||||
throw new \Exception('不支持的文字位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
/* 设置偏移量 */
|
||||
if(is_array($offset)){
|
||||
$offset = array_map('intval', $offset);
|
||||
list($ox, $oy) = $offset;
|
||||
} else{
|
||||
$offset = intval($offset);
|
||||
$ox = $oy = $offset;
|
||||
}
|
||||
|
||||
/* 设置颜色 */
|
||||
if(is_string($color) && 0 === strpos($color, '#')){
|
||||
$color = str_split(substr($color, 1), 2);
|
||||
$color = array_map('hexdec', $color);
|
||||
if(empty($color[3]) || $color[3] > 127){
|
||||
$color[3] = 0;
|
||||
}
|
||||
} elseif (!is_array($color)) {
|
||||
throw new \Exception('错误的颜色值');
|
||||
}
|
||||
|
||||
do{
|
||||
/* 写入文字 */
|
||||
$col = imagecolorallocatealpha($this->im, $color[0], $color[1], $color[2], $color[3]);
|
||||
imagettftext($this->im, $size, $angle, $x + $ox, $y + $oy, $col, $font, $text);
|
||||
} while(!empty($this->gif) && $this->gifNext());
|
||||
return $this;
|
||||
}
|
||||
|
||||
/* 切换到GIF的下一帧并保存当前帧,内部使用 */
|
||||
private function gifNext(){
|
||||
ob_start();
|
||||
ob_implicit_flush(0);
|
||||
imagegif($this->im);
|
||||
$img = ob_get_clean();
|
||||
|
||||
$this->gif->image($img);
|
||||
$next = $this->gif->nextImage();
|
||||
|
||||
if($next){
|
||||
$this->im = imagecreatefromstring($next);
|
||||
return $next;
|
||||
} else {
|
||||
$this->im = imagecreatefromstring($this->gif->image());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 析构方法,用于销毁图像资源
|
||||
*/
|
||||
public function __destruct() {
|
||||
empty($this->im) || imagedestroy($this->im);
|
||||
}
|
||||
}
|
||||
@@ -1,570 +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: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Image\Driver;
|
||||
|
||||
class Gif{
|
||||
/**
|
||||
* GIF帧列表
|
||||
* @var array
|
||||
*/
|
||||
private $frames = [];
|
||||
|
||||
/**
|
||||
* 每帧等待时间列表
|
||||
* @var array
|
||||
*/
|
||||
private $delays = [];
|
||||
|
||||
/**
|
||||
* 构造方法,用于解码GIF图片
|
||||
* @param string $src GIF图片数据
|
||||
* @param string $mod 图片数据类型
|
||||
*/
|
||||
public function __construct($src = null, $mod = 'url') {
|
||||
if(!is_null($src)){
|
||||
if('url' == $mod && is_file($src)){
|
||||
$src = file_get_contents($src);
|
||||
}
|
||||
|
||||
/* 解码GIF图片 */
|
||||
try{
|
||||
$de = new GIFDecoder($src);
|
||||
$this->frames = $de->GIFGetFrames();
|
||||
$this->delays = $de->GIFGetDelays();
|
||||
} catch(Exception $e){
|
||||
throw new \Exception("解码GIF图片出错");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置或获取当前帧的数据
|
||||
* @param string $stream 二进制数据流
|
||||
* @return boolean 获取到的数据
|
||||
*/
|
||||
public function image($stream = null){
|
||||
if(is_null($stream)){
|
||||
$current = current($this->frames);
|
||||
return false === $current ? reset($this->frames) : $current;
|
||||
} else {
|
||||
$this->frames[key($this->frames)] = $stream;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前帧移动到下一帧
|
||||
* @return string 当前帧数据
|
||||
*/
|
||||
public function nextImage(){
|
||||
return next($this->frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码并保存当前GIF图片
|
||||
* @param string $gifname 图片名称
|
||||
*/
|
||||
public function save($gifname){
|
||||
$gif = new GIFEncoder($this->frames, $this->delays, 0, 2, 0, 0, 0, 'bin');
|
||||
file_put_contents($gifname, $gif->GetAnimation());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFEncoder Version 2.0 by László Zsidi, http://gifs.hu
|
||||
::
|
||||
:: This class is a rewritten 'GifMerge.class.php' version.
|
||||
::
|
||||
:: Modification:
|
||||
:: - Simplified and easy code,
|
||||
:: - Ultra fast encoding,
|
||||
:: - Built-in errors,
|
||||
:: - Stable working
|
||||
::
|
||||
::
|
||||
:: Updated at 2007. 02. 13. '00.05.AM'
|
||||
::
|
||||
::
|
||||
::
|
||||
:: Try on-line GIFBuilder Form demo based on GIFEncoder.
|
||||
::
|
||||
:: http://gifs.hu/phpclasses/demos/GifBuilder/
|
||||
::
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
*/
|
||||
|
||||
Class GIFEncoder {
|
||||
var $GIF = "GIF89a"; /* GIF header 6 bytes */
|
||||
var $VER = "GIFEncoder V2.05"; /* Encoder version */
|
||||
|
||||
var $BUF = array ( );
|
||||
var $LOP = 0;
|
||||
var $DIS = 2;
|
||||
var $COL = -1;
|
||||
var $IMG = -1;
|
||||
|
||||
var $ERR = array (
|
||||
'ERR00'=>"Does not supported function for only one image!",
|
||||
'ERR01'=>"Source is not a GIF image!",
|
||||
'ERR02'=>"Unintelligible flag ",
|
||||
'ERR03'=>"Does not make animation from animated GIF source",
|
||||
);
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFEncoder...
|
||||
::
|
||||
*/
|
||||
function GIFEncoder (
|
||||
$GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,
|
||||
$GIF_red, $GIF_grn, $GIF_blu, $GIF_mod
|
||||
) {
|
||||
if ( ! is_array ( $GIF_src ) && ! is_array ( $GIF_tim ) ) {
|
||||
printf ( "%s: %s", $this->VER, $this->ERR [ 'ERR00' ] );
|
||||
exit ( 0 );
|
||||
}
|
||||
$this->LOP = ( $GIF_lop > -1 ) ? $GIF_lop : 0;
|
||||
$this->DIS = ( $GIF_dis > -1 ) ? ( ( $GIF_dis < 3 ) ? $GIF_dis : 3 ) : 2;
|
||||
$this->COL = ( $GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1 ) ?
|
||||
( $GIF_red | ( $GIF_grn << 8 ) | ( $GIF_blu << 16 ) ) : -1;
|
||||
|
||||
for ( $i = 0; $i < count ( $GIF_src ); $i++ ) {
|
||||
if ( strToLower ( $GIF_mod ) == "url" ) {
|
||||
$this->BUF [ ] = fread ( fopen ( $GIF_src [ $i ], "rb" ), filesize ( $GIF_src [ $i ] ) );
|
||||
}
|
||||
else if ( strToLower ( $GIF_mod ) == "bin" ) {
|
||||
$this->BUF [ ] = $GIF_src [ $i ];
|
||||
}
|
||||
else {
|
||||
printf ( "%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod );
|
||||
exit ( 0 );
|
||||
}
|
||||
if ( substr ( $this->BUF [ $i ], 0, 6 ) != "GIF87a" && substr ( $this->BUF [ $i ], 0, 6 ) != "GIF89a" ) {
|
||||
printf ( "%s: %d %s", $this->VER, $i, $this->ERR [ 'ERR01' ] );
|
||||
exit ( 0 );
|
||||
}
|
||||
for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = TRUE; $k; $j++ ) {
|
||||
switch ( $this->BUF [ $i ] { $j } ) {
|
||||
case "!":
|
||||
if ( ( substr ( $this->BUF [ $i ], ( $j + 3 ), 8 ) ) == "NETSCAPE" ) {
|
||||
printf ( "%s: %s ( %s source )!", $this->VER, $this->ERR [ 'ERR03' ], ( $i + 1 ) );
|
||||
exit ( 0 );
|
||||
}
|
||||
break;
|
||||
case ";":
|
||||
$k = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
GIFEncoder::GIFAddHeader ( );
|
||||
for ( $i = 0; $i < count ( $this->BUF ); $i++ ) {
|
||||
GIFEncoder::GIFAddFrames ( $i, $GIF_dly [ $i ] );
|
||||
}
|
||||
GIFEncoder::GIFAddFooter ( );
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFAddHeader...
|
||||
::
|
||||
*/
|
||||
function GIFAddHeader ( ) {
|
||||
$cmap = 0;
|
||||
|
||||
if ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x80 ) {
|
||||
$cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) );
|
||||
|
||||
$this->GIF .= substr ( $this->BUF [ 0 ], 6, 7 );
|
||||
$this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap );
|
||||
$this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord ( $this->LOP ) . "\0";
|
||||
}
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFAddFrames...
|
||||
::
|
||||
*/
|
||||
function GIFAddFrames ( $i, $d ) {
|
||||
|
||||
$Locals_str = 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) );
|
||||
|
||||
$Locals_end = strlen ( $this->BUF [ $i ] ) - $Locals_str - 1;
|
||||
$Locals_tmp = substr ( $this->BUF [ $i ], $Locals_str, $Locals_end );
|
||||
|
||||
$Global_len = 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
|
||||
$Locals_len = 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
|
||||
|
||||
$Global_rgb = substr ( $this->BUF [ 0 ], 13,
|
||||
3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) ) );
|
||||
$Locals_rgb = substr ( $this->BUF [ $i ], 13,
|
||||
3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) );
|
||||
|
||||
$Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
|
||||
chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . "\x0\x0";
|
||||
|
||||
if ( $this->COL > -1 && ord ( $this->BUF [ $i ] { 10 } ) & 0x80 ) {
|
||||
for ( $j = 0; $j < ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ); $j++ ) {
|
||||
if (
|
||||
ord ( $Locals_rgb { 3 * $j + 0 } ) == ( ( $this->COL >> 16 ) & 0xFF ) &&
|
||||
ord ( $Locals_rgb { 3 * $j + 1 } ) == ( ( $this->COL >> 8 ) & 0xFF ) &&
|
||||
ord ( $Locals_rgb { 3 * $j + 2 } ) == ( ( $this->COL >> 0 ) & 0xFF )
|
||||
) {
|
||||
$Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 1 ) .
|
||||
chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . chr ( $j ) . "\x0";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
switch ( $Locals_tmp { 0 } ) {
|
||||
case "!":
|
||||
$Locals_img = substr ( $Locals_tmp, 8, 10 );
|
||||
$Locals_tmp = substr ( $Locals_tmp, 18, strlen ( $Locals_tmp ) - 18 );
|
||||
break;
|
||||
case ",":
|
||||
$Locals_img = substr ( $Locals_tmp, 0, 10 );
|
||||
$Locals_tmp = substr ( $Locals_tmp, 10, strlen ( $Locals_tmp ) - 10 );
|
||||
break;
|
||||
}
|
||||
if ( ord ( $this->BUF [ $i ] { 10 } ) & 0x80 && $this->IMG > -1 ) {
|
||||
if ( $Global_len == $Locals_len ) {
|
||||
if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len ) ) {
|
||||
$this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
|
||||
}
|
||||
else {
|
||||
$byte = ord ( $Locals_img { 9 } );
|
||||
$byte |= 0x80;
|
||||
$byte &= 0xF8;
|
||||
$byte |= ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
|
||||
$Locals_img { 9 } = chr ( $byte );
|
||||
$this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
|
||||
}
|
||||
}
|
||||
else {
|
||||
$byte = ord ( $Locals_img { 9 } );
|
||||
$byte |= 0x80;
|
||||
$byte &= 0xF8;
|
||||
$byte |= ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
|
||||
$Locals_img { 9 } = chr ( $byte );
|
||||
$this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
|
||||
}
|
||||
}
|
||||
else {
|
||||
$this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
|
||||
}
|
||||
$this->IMG = 1;
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFAddFooter...
|
||||
::
|
||||
*/
|
||||
function GIFAddFooter ( ) {
|
||||
$this->GIF .= ";";
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFBlockCompare...
|
||||
::
|
||||
*/
|
||||
function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len ) {
|
||||
|
||||
for ( $i = 0; $i < $Len; $i++ ) {
|
||||
if (
|
||||
$GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } ||
|
||||
$GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } ||
|
||||
$GlobalBlock { 3 * $i + 2 } != $LocalBlock { 3 * $i + 2 }
|
||||
) {
|
||||
return ( 0 );
|
||||
}
|
||||
}
|
||||
|
||||
return ( 1 );
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFWord...
|
||||
::
|
||||
*/
|
||||
function GIFWord ( $int ) {
|
||||
|
||||
return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) );
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GetAnimation...
|
||||
::
|
||||
*/
|
||||
function GetAnimation ( ) {
|
||||
return ( $this->GIF );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFDecoder Version 2.0 by László Zsidi, http://gifs.hu
|
||||
::
|
||||
:: Created at 2007. 02. 01. '07.47.AM'
|
||||
::
|
||||
::
|
||||
::
|
||||
::
|
||||
:: Try on-line GIFBuilder Form demo based on GIFDecoder.
|
||||
::
|
||||
:: http://gifs.hu/phpclasses/demos/GifBuilder/
|
||||
::
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
*/
|
||||
|
||||
Class GIFDecoder {
|
||||
var $GIF_buffer = array ( );
|
||||
var $GIF_arrays = array ( );
|
||||
var $GIF_delays = array ( );
|
||||
var $GIF_stream = "";
|
||||
var $GIF_string = "";
|
||||
var $GIF_bfseek = 0;
|
||||
|
||||
var $GIF_screen = array ( );
|
||||
var $GIF_global = array ( );
|
||||
var $GIF_sorted;
|
||||
var $GIF_colorS;
|
||||
var $GIF_colorC;
|
||||
var $GIF_colorF;
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFDecoder ( $GIF_pointer )
|
||||
::
|
||||
*/
|
||||
function GIFDecoder ( $GIF_pointer ) {
|
||||
$this->GIF_stream = $GIF_pointer;
|
||||
|
||||
GIFDecoder::GIFGetByte ( 6 ); // GIF89a
|
||||
GIFDecoder::GIFGetByte ( 7 ); // Logical Screen Descriptor
|
||||
|
||||
$this->GIF_screen = $this->GIF_buffer;
|
||||
$this->GIF_colorF = $this->GIF_buffer [ 4 ] & 0x80 ? 1 : 0;
|
||||
$this->GIF_sorted = $this->GIF_buffer [ 4 ] & 0x08 ? 1 : 0;
|
||||
$this->GIF_colorC = $this->GIF_buffer [ 4 ] & 0x07;
|
||||
$this->GIF_colorS = 2 << $this->GIF_colorC;
|
||||
|
||||
if ( $this->GIF_colorF == 1 ) {
|
||||
GIFDecoder::GIFGetByte ( 3 * $this->GIF_colorS );
|
||||
$this->GIF_global = $this->GIF_buffer;
|
||||
}
|
||||
/*
|
||||
*
|
||||
* 05.06.2007.
|
||||
* Made a little modification
|
||||
*
|
||||
*
|
||||
- for ( $cycle = 1; $cycle; ) {
|
||||
+ if ( GIFDecoder::GIFGetByte ( 1 ) ) {
|
||||
- switch ( $this->GIF_buffer [ 0 ] ) {
|
||||
- case 0x21:
|
||||
- GIFDecoder::GIFReadExtensions ( );
|
||||
- break;
|
||||
- case 0x2C:
|
||||
- GIFDecoder::GIFReadDescriptor ( );
|
||||
- break;
|
||||
- case 0x3B:
|
||||
- $cycle = 0;
|
||||
- break;
|
||||
- }
|
||||
- }
|
||||
+ else {
|
||||
+ $cycle = 0;
|
||||
+ }
|
||||
- }
|
||||
*/
|
||||
for ( $cycle = 1; $cycle; ) {
|
||||
if ( GIFDecoder::GIFGetByte ( 1 ) ) {
|
||||
switch ( $this->GIF_buffer [ 0 ] ) {
|
||||
case 0x21:
|
||||
GIFDecoder::GIFReadExtensions ( );
|
||||
break;
|
||||
case 0x2C:
|
||||
GIFDecoder::GIFReadDescriptor ( );
|
||||
break;
|
||||
case 0x3B:
|
||||
$cycle = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$cycle = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFReadExtension ( )
|
||||
::
|
||||
*/
|
||||
function GIFReadExtensions ( ) {
|
||||
GIFDecoder::GIFGetByte ( 1 );
|
||||
for ( ; ; ) {
|
||||
GIFDecoder::GIFGetByte ( 1 );
|
||||
if ( ( $u = $this->GIF_buffer [ 0 ] ) == 0x00 ) {
|
||||
break;
|
||||
}
|
||||
GIFDecoder::GIFGetByte ( $u );
|
||||
/*
|
||||
* 07.05.2007.
|
||||
* Implemented a new line for a new function
|
||||
* to determine the originaly delays between
|
||||
* frames.
|
||||
*
|
||||
*/
|
||||
if ( $u == 4 ) {
|
||||
$this->GIF_delays [ ] = ( $this->GIF_buffer [ 1 ] | $this->GIF_buffer [ 2 ] << 8 );
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFReadExtension ( )
|
||||
::
|
||||
*/
|
||||
function GIFReadDescriptor ( ) {
|
||||
$GIF_screen = array ( );
|
||||
|
||||
GIFDecoder::GIFGetByte ( 9 );
|
||||
$GIF_screen = $this->GIF_buffer;
|
||||
$GIF_colorF = $this->GIF_buffer [ 8 ] & 0x80 ? 1 : 0;
|
||||
if ( $GIF_colorF ) {
|
||||
$GIF_code = $this->GIF_buffer [ 8 ] & 0x07;
|
||||
$GIF_sort = $this->GIF_buffer [ 8 ] & 0x20 ? 1 : 0;
|
||||
}
|
||||
else {
|
||||
$GIF_code = $this->GIF_colorC;
|
||||
$GIF_sort = $this->GIF_sorted;
|
||||
}
|
||||
$GIF_size = 2 << $GIF_code;
|
||||
$this->GIF_screen [ 4 ] &= 0x70;
|
||||
$this->GIF_screen [ 4 ] |= 0x80;
|
||||
$this->GIF_screen [ 4 ] |= $GIF_code;
|
||||
if ( $GIF_sort ) {
|
||||
$this->GIF_screen [ 4 ] |= 0x08;
|
||||
}
|
||||
$this->GIF_string = "GIF87a";
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_screen );
|
||||
if ( $GIF_colorF == 1 ) {
|
||||
GIFDecoder::GIFGetByte ( 3 * $GIF_size );
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_buffer );
|
||||
}
|
||||
else {
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_global );
|
||||
}
|
||||
$this->GIF_string .= chr ( 0x2C );
|
||||
$GIF_screen [ 8 ] &= 0x40;
|
||||
GIFDecoder::GIFPutByte ( $GIF_screen );
|
||||
GIFDecoder::GIFGetByte ( 1 );
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_buffer );
|
||||
for ( ; ; ) {
|
||||
GIFDecoder::GIFGetByte ( 1 );
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_buffer );
|
||||
if ( ( $u = $this->GIF_buffer [ 0 ] ) == 0x00 ) {
|
||||
break;
|
||||
}
|
||||
GIFDecoder::GIFGetByte ( $u );
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_buffer );
|
||||
}
|
||||
$this->GIF_string .= chr ( 0x3B );
|
||||
/*
|
||||
Add frames into $GIF_stream array...
|
||||
*/
|
||||
$this->GIF_arrays [ ] = $this->GIF_string;
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFGetByte ( $len )
|
||||
::
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* 05.06.2007.
|
||||
* Made a little modification
|
||||
*
|
||||
*
|
||||
- function GIFGetByte ( $len ) {
|
||||
- $this->GIF_buffer = array ( );
|
||||
-
|
||||
- for ( $i = 0; $i < $len; $i++ ) {
|
||||
+ if ( $this->GIF_bfseek > strlen ( $this->GIF_stream ) ) {
|
||||
+ return 0;
|
||||
+ }
|
||||
- $this->GIF_buffer [ ] = ord ( $this->GIF_stream { $this->GIF_bfseek++ } );
|
||||
- }
|
||||
+ return 1;
|
||||
- }
|
||||
*/
|
||||
function GIFGetByte ( $len ) {
|
||||
$this->GIF_buffer = array ( );
|
||||
|
||||
for ( $i = 0; $i < $len; $i++ ) {
|
||||
if ( $this->GIF_bfseek > strlen ( $this->GIF_stream ) ) {
|
||||
return 0;
|
||||
}
|
||||
$this->GIF_buffer [ ] = ord ( $this->GIF_stream { $this->GIF_bfseek++ } );
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFPutByte ( $bytes )
|
||||
::
|
||||
*/
|
||||
function GIFPutByte ( $bytes ) {
|
||||
for ( $i = 0; $i < count ( $bytes ); $i++ ) {
|
||||
$this->GIF_string .= chr ( $bytes [ $i ] );
|
||||
}
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: PUBLIC FUNCTIONS
|
||||
::
|
||||
::
|
||||
:: GIFGetFrames ( )
|
||||
::
|
||||
*/
|
||||
function GIFGetFrames ( ) {
|
||||
return ( $this->GIF_arrays );
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFGetDelays ( )
|
||||
::
|
||||
*/
|
||||
function GIFGetDelays ( ) {
|
||||
return ( $this->GIF_delays );
|
||||
}
|
||||
}
|
||||
@@ -1,591 +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: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Image\Driver;
|
||||
|
||||
class Imagick{
|
||||
/**
|
||||
* 图像资源对象
|
||||
* @var resource
|
||||
*/
|
||||
private $im;
|
||||
|
||||
/**
|
||||
* 图像信息,包括width,height,type,mime,size
|
||||
* @var array
|
||||
*/
|
||||
private $info;
|
||||
|
||||
/**
|
||||
* 构造方法,可用于打开一张图像
|
||||
* @param string $imgname 图像路径
|
||||
*/
|
||||
public function __construct($imgname = null) {
|
||||
if ( !extension_loaded('Imagick') ) {
|
||||
E(L('_NOT_SUPPERT_').':Imagick');
|
||||
}
|
||||
$imgname && $this->open($imgname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开一张图像
|
||||
* @param string $imgname 图像路径
|
||||
*/
|
||||
public function open($imgname){
|
||||
//检测图像文件
|
||||
if(!is_file($imgname)) throw new \Exception('不存在的图像文件');
|
||||
|
||||
//销毁已存在的图像
|
||||
empty($this->im) || $this->im->destroy();
|
||||
|
||||
//载入图像
|
||||
$this->im = new \Imagick(realpath($imgname));
|
||||
|
||||
//设置图像信息
|
||||
$this->info = [
|
||||
'width' => $this->im->getImageWidth(),
|
||||
'height' => $this->im->getImageHeight(),
|
||||
'type' => strtolower($this->im->getImageFormat()),
|
||||
'mime' => $this->im->getImageMimeType(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存图像
|
||||
* @param string $imgname 图像保存名称
|
||||
* @param string $type 图像类型
|
||||
* @param boolean $interlace 是否对JPEG类型图像设置隔行扫描
|
||||
*/
|
||||
public function save($imgname, $type = null, $interlace = true){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被保存的图像资源');
|
||||
|
||||
//设置图片类型
|
||||
if(is_null($type)){
|
||||
$type = $this->info['type'];
|
||||
} else {
|
||||
$type = strtolower($type);
|
||||
$this->im->setImageFormat($type);
|
||||
}
|
||||
|
||||
//JPEG图像设置隔行扫描
|
||||
if('jpeg' == $type || 'jpg' == $type){
|
||||
$this->im->setImageInterlaceScheme(1);
|
||||
}
|
||||
|
||||
//去除图像配置信息
|
||||
$this->im->stripImage();
|
||||
|
||||
//保存图像
|
||||
$imgname = realpath(dirname($imgname)) . '/' . basename($imgname); //强制绝对路径
|
||||
if ('gif' == $type) {
|
||||
$this->im->writeImages($imgname, true);
|
||||
} else {
|
||||
$this->im->writeImage($imgname);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像宽度
|
||||
* @return integer 图像宽度
|
||||
*/
|
||||
public function width(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['width'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像高度
|
||||
* @return integer 图像高度
|
||||
*/
|
||||
public function height(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['height'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像类型
|
||||
* @return string 图像类型
|
||||
*/
|
||||
public function type(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像MIME类型
|
||||
* @return string 图像MIME类型
|
||||
*/
|
||||
public function mime(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['mime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
|
||||
* @return array 图像尺寸
|
||||
*/
|
||||
public function size(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return [$this->info['width'], $this->info['height']];
|
||||
}
|
||||
|
||||
/**
|
||||
* 裁剪图像
|
||||
* @param integer $w 裁剪区域宽度
|
||||
* @param integer $h 裁剪区域高度
|
||||
* @param integer $x 裁剪区域x坐标
|
||||
* @param integer $y 裁剪区域y坐标
|
||||
* @param integer $width 图像保存宽度
|
||||
* @param integer $height 图像保存高度
|
||||
*/
|
||||
public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被裁剪的图像资源');
|
||||
|
||||
//设置保存尺寸
|
||||
empty($width) && $width = $w;
|
||||
empty($height) && $height = $h;
|
||||
|
||||
//裁剪图片
|
||||
if('gif' == $this->info['type']){
|
||||
$img = $this->im->coalesceImages();
|
||||
$this->im->destroy(); //销毁原图
|
||||
|
||||
//循环裁剪每一帧
|
||||
do {
|
||||
$this->_crop($w, $h, $x, $y, $width, $height, $img);
|
||||
} while ($img->nextImage());
|
||||
|
||||
//压缩图片
|
||||
$this->im = $img->deconstructImages();
|
||||
$img->destroy(); //销毁零时图片
|
||||
} else {
|
||||
$this->_crop($w, $h, $x, $y, $width, $height);
|
||||
}
|
||||
}
|
||||
|
||||
/* 裁剪图片,内部调用 */
|
||||
private function _crop($w, $h, $x, $y, $width, $height, $img = null){
|
||||
is_null($img) && $img = $this->im;
|
||||
|
||||
//裁剪
|
||||
$info = $this->info;
|
||||
if($x != 0 || $y != 0 || $w != $info['width'] || $h != $info['height']){
|
||||
$img->cropImage($w, $h, $x, $y);
|
||||
$img->setImagePage($w, $h, 0, 0); //调整画布和图片一致
|
||||
}
|
||||
|
||||
//调整大小
|
||||
if($w != $width || $h != $height){
|
||||
$img->scaleImage($width, $height);
|
||||
}
|
||||
|
||||
//设置缓存尺寸
|
||||
$this->info['width'] = $w;
|
||||
$this->info['height'] = $h;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成缩略图
|
||||
* @param integer $width 缩略图最大宽度
|
||||
* @param integer $height 缩略图最大高度
|
||||
* @param integer $type 缩略图裁剪类型
|
||||
*/
|
||||
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALE){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被缩略的图像资源');
|
||||
|
||||
//原图宽度和高度
|
||||
$w = $this->info['width'];
|
||||
$h = $this->info['height'];
|
||||
|
||||
/* 计算缩略图生成的必要参数 */
|
||||
switch ($type) {
|
||||
/* 等比例缩放 */
|
||||
case THINKIMAGE_THUMB_SCALING:
|
||||
//原图尺寸小于缩略图尺寸则不进行缩略
|
||||
if($w < $width && $h < $height) return;
|
||||
|
||||
//计算缩放比例
|
||||
$scale = min($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$x = $y = 0;
|
||||
$width = $w * $scale;
|
||||
$height = $h * $scale;
|
||||
break;
|
||||
|
||||
/* 居中裁剪 */
|
||||
case THINKIMAGE_THUMB_CENTER:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
$x = ($this->info['width'] - $w)/2;
|
||||
$y = ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 左上角裁剪 */
|
||||
case THINKIMAGE_THUMB_NORTHWEST:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$x = $y = 0;
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
break;
|
||||
|
||||
/* 右下角裁剪 */
|
||||
case THINKIMAGE_THUMB_SOUTHEAST:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
$x = $this->info['width'] - $w;
|
||||
$y = $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 填充 */
|
||||
case THINKIMAGE_THUMB_FILLED:
|
||||
//计算缩放比例
|
||||
if($w < $width && $h < $height){
|
||||
$scale = 1;
|
||||
} else {
|
||||
$scale = min($width/$w, $height/$h);
|
||||
}
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$neww = $w * $scale;
|
||||
$newh = $h * $scale;
|
||||
$posx = ($width - $w * $scale)/2;
|
||||
$posy = ($height - $h * $scale)/2;
|
||||
|
||||
//创建一张新图像
|
||||
$newimg = new Imagick();
|
||||
$newimg->newImage($width, $height, 'white', $this->info['type']);
|
||||
|
||||
|
||||
if('gif' == $this->info['type']){
|
||||
$imgs = $this->im->coalesceImages();
|
||||
$img = new Imagick();
|
||||
$this->im->destroy(); //销毁原图
|
||||
|
||||
//循环填充每一帧
|
||||
do {
|
||||
//填充图像
|
||||
$image = $this->_fill($newimg, $posx, $posy, $neww, $newh, $imgs);
|
||||
|
||||
$img->addImage($image);
|
||||
$img->setImageDelay($imgs->getImageDelay());
|
||||
$img->setImagePage($width, $height, 0, 0);
|
||||
|
||||
$image->destroy(); //销毁零时图片
|
||||
|
||||
} while ($imgs->nextImage());
|
||||
|
||||
//压缩图片
|
||||
$this->im->destroy();
|
||||
$this->im = $img->deconstructImages();
|
||||
$imgs->destroy(); //销毁零时图片
|
||||
$img->destroy(); //销毁零时图片
|
||||
|
||||
} else {
|
||||
//填充图像
|
||||
$img = $this->_fill($newimg, $posx, $posy, $neww, $newh);
|
||||
//销毁原图
|
||||
$this->im->destroy();
|
||||
$this->im = $img;
|
||||
}
|
||||
|
||||
//设置新图像属性
|
||||
$this->info['width'] = $width;
|
||||
$this->info['height'] = $height;
|
||||
return;
|
||||
|
||||
/* 固定 */
|
||||
case THINKIMAGE_THUMB_FIXED:
|
||||
$x = $y = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('不支持的缩略图裁剪类型');
|
||||
}
|
||||
|
||||
/* 裁剪图像 */
|
||||
$this->crop($w, $h, $x, $y, $width, $height);
|
||||
}
|
||||
|
||||
/* 填充指定图像,内部使用 */
|
||||
private function _fill($newimg, $posx, $posy, $neww, $newh, $img = null){
|
||||
is_null($img) && $img = $this->im;
|
||||
|
||||
/* 将指定图片绘入空白图片 */
|
||||
$draw = new ImagickDraw();
|
||||
$draw->composite($img->getImageCompose(), $posx, $posy, $neww, $newh, $img);
|
||||
$image = $newimg->clone();
|
||||
$image->drawImage($draw);
|
||||
$draw->destroy();
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加水印
|
||||
* @param string $source 水印图片路径
|
||||
* @param integer $locate 水印位置
|
||||
* @param integer $alpha 水印透明度
|
||||
*/
|
||||
public function water($source, $locate = THINKIMAGE_WATER_SOUTHEAST){
|
||||
//资源检测
|
||||
if(empty($this->im)) throw new \Exception('没有可以被添加水印的图像资源');
|
||||
if(!is_file($source)) throw new \Exception('水印图像不存在');
|
||||
|
||||
//创建水印图像资源
|
||||
$water = new Imagick(realpath($source));
|
||||
$info = [$water->getImageWidth(), $water->getImageHeight()];
|
||||
|
||||
/* 设定水印位置 */
|
||||
switch ($locate) {
|
||||
/* 右下角水印 */
|
||||
case THINKIMAGE_WATER_SOUTHEAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 左下角水印 */
|
||||
case THINKIMAGE_WATER_SOUTHWEST:
|
||||
$x = 0;
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 左上角水印 */
|
||||
case THINKIMAGE_WATER_NORTHWEST:
|
||||
$x = $y = 0;
|
||||
break;
|
||||
|
||||
/* 右上角水印 */
|
||||
case THINKIMAGE_WATER_NORTHEAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = 0;
|
||||
break;
|
||||
|
||||
/* 居中水印 */
|
||||
case THINKIMAGE_WATER_CENTER:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
/* 下居中水印 */
|
||||
case THINKIMAGE_WATER_SOUTH:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 右居中水印 */
|
||||
case THINKIMAGE_WATER_EAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
/* 上居中水印 */
|
||||
case THINKIMAGE_WATER_NORTH:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = 0;
|
||||
break;
|
||||
|
||||
/* 左居中水印 */
|
||||
case THINKIMAGE_WATER_WEST:
|
||||
$x = 0;
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 自定义水印坐标 */
|
||||
if(is_array($locate)){
|
||||
list($x, $y) = $locate;
|
||||
} else {
|
||||
throw new \Exception('不支持的水印位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
//创建绘图资源
|
||||
$draw = new ImagickDraw();
|
||||
$draw->composite($water->getImageCompose(), $x, $y, $info[0], $info[1], $water);
|
||||
|
||||
if('gif' == $this->info['type']){
|
||||
$img = $this->im->coalesceImages();
|
||||
$this->im->destroy(); //销毁原图
|
||||
|
||||
do{
|
||||
//添加水印
|
||||
$img->drawImage($draw);
|
||||
} while ($img->nextImage());
|
||||
|
||||
//压缩图片
|
||||
$this->im = $img->deconstructImages();
|
||||
$img->destroy(); //销毁零时图片
|
||||
|
||||
} else {
|
||||
//添加水印
|
||||
$this->im->drawImage($draw);
|
||||
}
|
||||
|
||||
//销毁水印资源
|
||||
$draw->destroy();
|
||||
$water->destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 图像添加文字
|
||||
* @param string $text 添加的文字
|
||||
* @param string $font 字体路径
|
||||
* @param integer $size 字号
|
||||
* @param string $color 文字颜色
|
||||
* @param integer $locate 文字写入位置
|
||||
* @param integer $offset 文字相对当前位置的偏移量
|
||||
* @param integer $angle 文字倾斜角度
|
||||
*/
|
||||
public function text($text, $font, $size, $color = '#00000000',
|
||||
$locate = THINKIMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0){
|
||||
//资源检测
|
||||
if(empty($this->im)) throw new \Exception('没有可以被写入文字的图像资源');
|
||||
if(!is_file($font)) throw new \Exception("不存在的字体文件:{$font}");
|
||||
|
||||
//获取颜色和透明度
|
||||
if(is_array($color)){
|
||||
$color = array_map('dechex', $color);
|
||||
foreach ($color as &$value) {
|
||||
$value = str_pad($value, 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
$color = '#' . implode('', $color);
|
||||
} elseif(!is_string($color) || 0 !== strpos($color, '#')) {
|
||||
throw new \Exception('错误的颜色值');
|
||||
}
|
||||
$col = substr($color, 0, 7);
|
||||
$alp = strlen($color) == 9 ? substr($color, -2) : 0;
|
||||
|
||||
|
||||
//获取文字信息
|
||||
$draw = new ImagickDraw();
|
||||
$draw->setFont(realpath($font));
|
||||
$draw->setFontSize($size);
|
||||
$draw->setFillColor($col);
|
||||
$draw->setFillAlpha(1-hexdec($alp)/127);
|
||||
$draw->setTextAntialias(true);
|
||||
$draw->setStrokeAntialias(true);
|
||||
|
||||
$metrics = $this->im->queryFontMetrics($draw, $text);
|
||||
|
||||
/* 计算文字初始坐标和尺寸 */
|
||||
$x = 0;
|
||||
$y = $metrics['ascender'];
|
||||
$w = $metrics['textWidth'];
|
||||
$h = $metrics['textHeight'];
|
||||
|
||||
/* 设定文字位置 */
|
||||
switch ($locate) {
|
||||
/* 右下角文字 */
|
||||
case THINKIMAGE_WATER_SOUTHEAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 左下角文字 */
|
||||
case THINKIMAGE_WATER_SOUTHWEST:
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 左上角文字 */
|
||||
case THINKIMAGE_WATER_NORTHWEST:
|
||||
// 起始坐标即为左上角坐标,无需调整
|
||||
break;
|
||||
|
||||
/* 右上角文字 */
|
||||
case THINKIMAGE_WATER_NORTHEAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
break;
|
||||
|
||||
/* 居中文字 */
|
||||
case THINKIMAGE_WATER_CENTER:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 下居中文字 */
|
||||
case THINKIMAGE_WATER_SOUTH:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 右居中文字 */
|
||||
case THINKIMAGE_WATER_EAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 上居中文字 */
|
||||
case THINKIMAGE_WATER_NORTH:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
break;
|
||||
|
||||
/* 左居中文字 */
|
||||
case THINKIMAGE_WATER_WEST:
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 自定义文字坐标 */
|
||||
if(is_array($locate)){
|
||||
list($posx, $posy) = $locate;
|
||||
$x += $posx;
|
||||
$y += $posy;
|
||||
} else {
|
||||
throw new \Exception('不支持的文字位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
/* 设置偏移量 */
|
||||
if(is_array($offset)){
|
||||
$offset = array_map('intval', $offset);
|
||||
list($ox, $oy) = $offset;
|
||||
} else{
|
||||
$offset = intval($offset);
|
||||
$ox = $oy = $offset;
|
||||
}
|
||||
|
||||
/* 写入文字 */
|
||||
if('gif' == $this->info['type']){
|
||||
$img = $this->im->coalesceImages();
|
||||
$this->im->destroy(); //销毁原图
|
||||
do{
|
||||
$img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text);
|
||||
} while ($img->nextImage());
|
||||
|
||||
//压缩图片
|
||||
$this->im = $img->deconstructImages();
|
||||
$img->destroy(); //销毁零时图片
|
||||
|
||||
} else {
|
||||
$this->im->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text);
|
||||
}
|
||||
$draw->destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 析构方法,用于销毁图像资源
|
||||
*/
|
||||
public function __destruct() {
|
||||
empty($this->im) || $this->im->destroy();
|
||||
}
|
||||
}
|
||||
@@ -13,78 +13,160 @@ namespace Think;
|
||||
|
||||
class Input {
|
||||
// 全局过滤规则
|
||||
static $filter = NULL;
|
||||
static $filter = null;
|
||||
|
||||
/**
|
||||
* 获取系统变量 支持过滤和默认值
|
||||
* @access public
|
||||
* @param string $type 输入数据类型
|
||||
* @param string $method 输入数据类型
|
||||
* @param array $args 参数 array(key,filter,default)
|
||||
* @return mixed
|
||||
*/
|
||||
static public function __callStatic($type,$args=[]) {
|
||||
switch(strtolower($type)) {
|
||||
case 'get': $input =& $_GET;break;
|
||||
case 'post': $input =& $_POST;break;
|
||||
case 'put' : parse_str(file_get_contents('php://input'), $input);break;
|
||||
case 'param' :
|
||||
static public function __callStatic($method,$args=[]) {
|
||||
static $_PUT = null;
|
||||
$name = $args[0];
|
||||
$default = isset($args[2]) ? $args[2] : null;
|
||||
if(strpos($name,'/')){ // 指定修饰符
|
||||
list($name,$type) = explode('/',$name,2);
|
||||
}else{ // 默认强制转换为字符串
|
||||
$type = 's';
|
||||
}
|
||||
switch(strtolower($method)) {
|
||||
case 'get' :
|
||||
$input =& $_GET;
|
||||
break;
|
||||
case 'post' :
|
||||
$input =& $_POST;
|
||||
break;
|
||||
case 'put' :
|
||||
if(is_null($_PUT)){
|
||||
parse_str(file_get_contents('php://input'), $_PUT);
|
||||
}
|
||||
$input = $_PUT;
|
||||
break;
|
||||
case 'param' :
|
||||
switch($_SERVER['REQUEST_METHOD']) {
|
||||
case 'POST':
|
||||
$input = $_POST;
|
||||
break;
|
||||
case 'PUT':
|
||||
parse_str(file_get_contents('php://input'), $input);
|
||||
if(is_null($_PUT)){
|
||||
parse_str(file_get_contents('php://input'), $_PUT);
|
||||
}
|
||||
$input = $_PUT;
|
||||
break;
|
||||
default:
|
||||
$input = $_GET;
|
||||
}
|
||||
break;
|
||||
case 'request': $input =& $_REQUEST;break;
|
||||
case 'server': $input =& $_SERVER;break;
|
||||
case 'cookie': $input =& $_COOKIE;break;
|
||||
case 'session': $input =& $_SESSION;break;
|
||||
case 'globals': $input =& $GLOBALS;break;
|
||||
default:return NULL;
|
||||
case 'path' :
|
||||
$input = [];
|
||||
if(!empty($_SERVER['PATH_INFO'])){
|
||||
$depr = Config::get('url_pathinfo_depr');
|
||||
$input = explode($depr,trim($_SERVER['PATH_INFO'],$depr));
|
||||
}
|
||||
break;
|
||||
case 'request' :
|
||||
$input =& $_REQUEST;
|
||||
break;
|
||||
case 'session' :
|
||||
$input =& $_SESSION;
|
||||
break;
|
||||
case 'cookie' :
|
||||
$input =& $_COOKIE;
|
||||
break;
|
||||
case 'server' :
|
||||
$input =& $_SERVER;
|
||||
break;
|
||||
case 'globals' :
|
||||
$input =& $GLOBALS;
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
// 变量全局过滤
|
||||
array_walk_recursive($input,'self::filter_exp');
|
||||
if(self::$filter) {
|
||||
$_filters = explode(',',self::$filter);
|
||||
foreach($_filters as $_filter){
|
||||
// 全局参数过滤
|
||||
array_walk_recursive($input,$_filter);
|
||||
|
||||
if(''==$name) { // 获取全部变量
|
||||
$data = $input;
|
||||
if(isset(self::$filter)) {
|
||||
$filter = self::$filter;
|
||||
if(is_string($filters)){
|
||||
$filters = explode(',',$filters);
|
||||
}
|
||||
foreach($filters as $filter){
|
||||
$data = self::filter($filter,$data); // 参数过滤
|
||||
}
|
||||
}
|
||||
}
|
||||
if(''== $args[0]) {
|
||||
// 返回全部数据
|
||||
return $input;
|
||||
}elseif(isset($input[$args[0]])) {
|
||||
$data = $input[$args[0]];
|
||||
}elseif(isset($input[$name])) { // 取值操作
|
||||
$data = $input[$name];
|
||||
if(!empty($args[1])) {
|
||||
$filters = explode(',',$args[1]);
|
||||
foreach($filters as $filter){
|
||||
if(is_callable($filter)) {
|
||||
$data = is_array($data)?array_map($filter,$data):$filter($data); // 参数过滤
|
||||
if(is_string($filters)){
|
||||
if(0 === strpos($filters,'/') && 1 !== preg_match($filters,(string)$data)){
|
||||
// 支持正则验证
|
||||
return $default;
|
||||
}else{
|
||||
$data = filter_var($data,is_int($filter)?$filter:filter_id($filter));
|
||||
if(false === $data) {
|
||||
return isset($args[2])?$args[2]:NULL;
|
||||
$filters = explode(',',$filters);
|
||||
}
|
||||
}elseif(is_int($filters)){
|
||||
$filters = array($filters);
|
||||
}
|
||||
|
||||
if(is_array($filters)){
|
||||
foreach($filters as $filter){
|
||||
if(function_exists($filter)) {
|
||||
$data = is_array($data) ? self::filter($filter,$data) : $filter($data); // 参数过滤
|
||||
}else{
|
||||
$data = filter_var($data,is_int($filter) ? $filter : filter_id($filter));
|
||||
if(false === $data) {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!empty($type)){
|
||||
switch(strtolower($type)){
|
||||
case 'a': // 数组
|
||||
$data = (array)$data;
|
||||
break;
|
||||
case 'd': // 数字
|
||||
$data = (int)$data;
|
||||
break;
|
||||
case 'f': // 浮点
|
||||
$data = (float)$data;
|
||||
break;
|
||||
case 'b': // 布尔
|
||||
$data = (boolean)$data;
|
||||
break;
|
||||
case 's': // 字符串
|
||||
default:
|
||||
$data = (string)$data;
|
||||
}
|
||||
}
|
||||
|
||||
}else{
|
||||
// 不存在指定输入
|
||||
$data = isset($args[2])?$args[2]:NULL;
|
||||
}else{ // 变量默认值
|
||||
$data = $default;
|
||||
}
|
||||
is_array($data) && array_walk_recursive($data,'self::filterExp');
|
||||
return $data;
|
||||
}
|
||||
|
||||
// 过滤表单中的表达式
|
||||
static private function filter_exp(&$value){
|
||||
if (in_array(strtolower($value),['exp','or'])){
|
||||
static public function filterExp(&$value){
|
||||
// TODO 其他安全过滤
|
||||
|
||||
// 过滤查询特殊字符
|
||||
if(preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i',$value)){
|
||||
$value .= ' ';
|
||||
}
|
||||
}
|
||||
|
||||
static public function filter($filter, $data) {
|
||||
$result = array();
|
||||
foreach ($data as $key => $val) {
|
||||
$result[$key] = is_array($val)
|
||||
? self::filter($filter, $val)
|
||||
: call_user_func($filter, $val);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,9 +130,13 @@ class Loader {
|
||||
* @return Object
|
||||
*/
|
||||
static public function model($name = '', $layer = 'Model') {
|
||||
if(empty($name)) return new Model;
|
||||
if(empty($name)) {
|
||||
return new Model;
|
||||
}
|
||||
static $_model = [];
|
||||
if(isset($_model[$name . $layer])) return $_model[$name . $layer];
|
||||
if(isset($_model[$name . $layer])) {
|
||||
return $_model[$name . $layer];
|
||||
}
|
||||
if(strpos($name, '/')) {
|
||||
list($module, $name) = explode('/', $name);
|
||||
}else{
|
||||
@@ -157,7 +161,9 @@ class Loader {
|
||||
*/
|
||||
static public function controller($name, $layer = 'Controller') {
|
||||
static $_instance = [];
|
||||
if(isset($_instance[$name.$layer])) return $_instance[$name . $layer];
|
||||
if(isset($_instance[$name.$layer])) {
|
||||
return $_instance[$name . $layer];
|
||||
}
|
||||
if(strpos($name, '/')) {
|
||||
list($module,$name) = explode('/', $name);
|
||||
}else{
|
||||
@@ -226,7 +232,7 @@ class Loader {
|
||||
$_instance[$identify] = $o;
|
||||
}
|
||||
else
|
||||
E('_CLASS_NOT_EXIST_:' . $class);
|
||||
throw new Exception('_CLASS_NOT_EXIST_:' . $class);
|
||||
}
|
||||
return $_instance[$identify];
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ class Log {
|
||||
* @return array
|
||||
*/
|
||||
static public function getLog($level=''){
|
||||
return $level?self::$log[$level]:self::$log;
|
||||
return $level ? self::$log[$level] : self::$log;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,7 +55,7 @@ class Log {
|
||||
* @return void
|
||||
*/
|
||||
static public function save($destination='',$level='') {
|
||||
$log = $level?self::$log[$level]:self::$log;
|
||||
$log = self::getLog($level);
|
||||
if(empty($log)) return ;
|
||||
$message = '';
|
||||
if($level) {
|
||||
|
||||
@@ -83,11 +83,6 @@ class Model {
|
||||
$this->dbName = $config['db_name'];
|
||||
}
|
||||
|
||||
// 设置表前缀
|
||||
if(empty($this->tablePrefix)) {
|
||||
$this->tablePrefix = is_null($this->tablePrefix)?'':C('database.prefix');
|
||||
}
|
||||
|
||||
// 数据库初始化操作
|
||||
// 获取数据库操作对象
|
||||
// 当前模型有独立的数据库连接信息
|
||||
@@ -566,7 +561,7 @@ class Model {
|
||||
$config = C($config);
|
||||
}
|
||||
$_db[$linkNum] = Db::instance($config);
|
||||
}elseif(NULL === $config){
|
||||
}elseif(null === $config){
|
||||
$_db[$linkNum]->close(); // 关闭数据库连接
|
||||
unset($_db[$linkNum]);
|
||||
return ;
|
||||
@@ -825,7 +820,7 @@ class Model {
|
||||
* @param array $args 参数
|
||||
* @return Model
|
||||
*/
|
||||
public function scope($scope='',$args=NULL){
|
||||
public function scope($scope='',$args=null){
|
||||
if('' === $scope) {
|
||||
if(isset($this->scope['default'])) {
|
||||
// 默认的命名范围
|
||||
|
||||
@@ -69,7 +69,7 @@ class Baidu extends Driver{
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
$data = $this->call('passport/users/getLoggedInUser');
|
||||
return !empty($data['uid'])?$data['uid']:NULL;
|
||||
return !empty($data['uid'])?$data['uid']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class Diandian extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -67,7 +67,7 @@ class Douban extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class Github extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('user');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -75,7 +75,7 @@ class Google extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('userinfo');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -70,7 +70,7 @@ class Kaixin extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('users/me');
|
||||
return !empty($data['uid'])?$data['uid']:NULL;
|
||||
return !empty($data['uid'])?$data['uid']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -76,7 +76,7 @@ class Msn extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('me');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -83,7 +83,7 @@ class Qq extends Driver{
|
||||
if(isset($data['openid']))
|
||||
return $data['openid'];
|
||||
}
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getOauthInfo(){
|
||||
|
||||
@@ -93,7 +93,7 @@ class Renren extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class Sina extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class Sohu extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -71,7 +71,7 @@ class T163 extends Driver{
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('users/show');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -71,7 +71,7 @@ class Taobao extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,7 +74,7 @@ class Tencent extends Driver{
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return NULL;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,7 +69,7 @@ class X360 extends Driver{
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
$data = $this->call('user/me');
|
||||
return !empty($data['id'])?$data['id']:NULL;
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,7 +26,7 @@ class Parser {
|
||||
}
|
||||
|
||||
// 调用驱动类的方法
|
||||
static public function __callStatic($method, $params){
|
||||
static public function __callStatic($method, $params){
|
||||
return self::parse($params[0],$method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ class Route {
|
||||
'DELETE' => [],
|
||||
'*' => [],
|
||||
];
|
||||
|
||||
// URL映射规则
|
||||
static private $map = [];
|
||||
// 子域名部署规则
|
||||
@@ -86,7 +87,7 @@ class Route {
|
||||
}
|
||||
|
||||
// 检测子域名部署
|
||||
static public function checkDomain(){
|
||||
static public function checkDomain($config=[]){
|
||||
// 开启子域名部署 支持二级和三级域名
|
||||
if(!empty(self::$domain)) {
|
||||
$rules = self::$domain;
|
||||
@@ -123,7 +124,7 @@ class Route {
|
||||
exit;
|
||||
}
|
||||
if(is_array($rule)) {
|
||||
$_GET[Config::get('var_module')] = $rule[0];
|
||||
$_GET[$config['var_module']] = $rule[0];
|
||||
if(isset($rule[1])) { // 传入参数
|
||||
parse_str($rule[1], $parms);
|
||||
if(isset($panDomain)) {
|
||||
@@ -136,18 +137,20 @@ class Route {
|
||||
$_GET = array_merge($_GET,$parms);
|
||||
}
|
||||
}else{
|
||||
$_GET[Config::get('var_module')] = $rule;
|
||||
$_GET[$config['var_module']] = $rule;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检测URL路由
|
||||
static public function check($regx) {
|
||||
static public function check($regx,$config) {
|
||||
// 优先检测是否存在PATH_INFO
|
||||
if(empty($regx)) $regx = '/' ;
|
||||
// 分隔符替换 确保路由定义使用统一的分隔符
|
||||
$regx = str_replace(Config::get('pathinfo_depr'), '/', $regx);
|
||||
if('/' != $config['pathinfo_depr']){
|
||||
$regx = str_replace($config['pathinfo_depr'], '/', $regx);
|
||||
}
|
||||
if(isset(self::$map[$regx])) { // URL映射
|
||||
return self::parseUrl(self::$map[$regx]);
|
||||
}
|
||||
@@ -187,7 +190,7 @@ class Route {
|
||||
self::invokeRegx($route, $matches);
|
||||
exit;
|
||||
}
|
||||
return self::parseRegex($matches, $route, $regx);
|
||||
return self::parseRegex($matches, $route, $regx,$config);
|
||||
}else{ // 规则路由
|
||||
$len1 = substr_count($regx, '/');
|
||||
$len2 = substr_count($rule, '/');
|
||||
@@ -210,13 +213,13 @@ class Route {
|
||||
self::invokeRule($route, $var);
|
||||
exit;
|
||||
}
|
||||
return self::parseRule($rule, $route, $regx);
|
||||
return self::parseRule($rule, $route, $regx,$config);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return self::parseUrl($regx);
|
||||
return self::parseUrl($regx,$config);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,33 +268,34 @@ class Route {
|
||||
$reflect->invokeArgs($args);
|
||||
}
|
||||
|
||||
// 解析模块的URL地址
|
||||
static private function parseUrl($url) {
|
||||
// 解析模块的URL地址 [模块/]控制器/操作
|
||||
static private function parseUrl($url,$config=[]) {
|
||||
if('/' == $url) {
|
||||
return ;
|
||||
}
|
||||
$paths = explode('/', $url);
|
||||
$var_g = Config::get('var_group');
|
||||
$var_c = Config::get('var_controller');
|
||||
$var_a = Config::get('var_action');
|
||||
if(Config::get('require_group') && !isset($_GET[$var_g])) {
|
||||
$_GET[$var_g] = array_shift($paths);
|
||||
}
|
||||
if(Config::get('require_controller') && !isset($_GET[$var_c])) {
|
||||
$_GET[$var_c] = array_shift($paths);
|
||||
|
||||
$_GET[$config['var_action']] = array_pop($paths);
|
||||
|
||||
if(!defined('BIND_CONTROLLER') && !isset($_GET[$config['var_controller']])) {
|
||||
$_GET[$config['var_controller']] = array_pop($paths);
|
||||
}
|
||||
if(!isset($_GET[$var_a])) {
|
||||
$_GET[$var_a] = array_shift($paths);
|
||||
|
||||
if(!defined('BIND_MODULE') && !isset($_GET[$config['var_module']])) {
|
||||
$_GET[$config['var_module']] = array_pop($paths);
|
||||
}
|
||||
|
||||
// 解析剩余的URL参数
|
||||
$var = [];
|
||||
preg_replace('@(\w+)\/([^\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode('/', $paths));
|
||||
$_GET = array_merge($var, $_GET);
|
||||
if(!empty($paths)) {
|
||||
preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){ $var[strtolower($match[1])]=strip_tags($match[2]);}, implode('/',$paths));
|
||||
}
|
||||
$_GET = array_merge($var, $_GET);
|
||||
}
|
||||
|
||||
// 解析规范的路由地址
|
||||
// 地址格式 [控制器/操作?]参数1=值1&参数2=值2...
|
||||
static private function parseRoute($url) {
|
||||
static private function parseRoute($url,$config=[]) {
|
||||
$var = [];
|
||||
if(false !== strpos($url, '?')) { // [控制器/操作?]参数1=值1&参数2=值2...
|
||||
$info = parse_url($url);
|
||||
@@ -304,9 +308,12 @@ class Route {
|
||||
}
|
||||
if(isset($path)) {
|
||||
$action = array_pop($path);
|
||||
$_GET[Config::get('var_action')] = '[rest]'==$action? REQUEST_METHOD : $action;
|
||||
$_GET[$config['var_action']] = '[rest]'==$action ? REQUEST_METHOD : $action;
|
||||
if(!empty($path)) {
|
||||
$_GET[Config::get('var_controller')] = array_pop($path);
|
||||
$_GET[$config['var_controller']] = array_pop($path);
|
||||
}
|
||||
if(!empty($path)) {
|
||||
$_GET[$config['var_module']] = array_pop($path);
|
||||
}
|
||||
}
|
||||
return $var;
|
||||
@@ -352,7 +359,7 @@ class Route {
|
||||
// 外部地址中可以用动态变量 采用 :1 :2 的方式
|
||||
// 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
|
||||
// 'new/:id'=>array('/new.php?id=:1',301), 重定向
|
||||
static private function parseRule($rule, $route, $regx) {
|
||||
static private function parseRule($rule, $route, $regx,$config) {
|
||||
// 获取路由地址规则
|
||||
$url = is_array($route) ? $route[0] : $route;
|
||||
// 获取URL地址中的参数
|
||||
@@ -383,7 +390,7 @@ class Route {
|
||||
exit;
|
||||
}else{
|
||||
// 解析路由地址
|
||||
$var = self::parseRoute($url);
|
||||
$var = self::parseRoute($url,$config);
|
||||
// 解析路由地址里面的动态参数
|
||||
$values = array_values($matches);
|
||||
foreach ($var as $key => $val){
|
||||
@@ -393,8 +400,10 @@ class Route {
|
||||
}
|
||||
$var = array_merge($matches, $var);
|
||||
// 解析剩余的URL参数
|
||||
if($paths) {
|
||||
preg_replace('@(\w+)\/([^\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/', $paths));
|
||||
if(!empty($paths)) {
|
||||
preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){
|
||||
$var[strtolower($match[1])] = strip_tags($match[2]);
|
||||
}, implode('/',$paths));
|
||||
}
|
||||
// 解析路由自动传人参数
|
||||
if(is_array($route) && isset($route[1])) {
|
||||
@@ -413,7 +422,7 @@ class Route {
|
||||
// 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式
|
||||
// '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'),
|
||||
// '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向
|
||||
static private function parseRegex($matches, $route, $regx) {
|
||||
static private function parseRegex($matches, $route, $regx,$config) {
|
||||
// 获取路由地址规则
|
||||
$url = is_array($route) ? $route[0] : $route;
|
||||
$url = preg_replace('/:(\d+)/e', '$matches[\\1]', $url);
|
||||
@@ -422,11 +431,13 @@ class Route {
|
||||
exit;
|
||||
}else{
|
||||
// 解析路由地址
|
||||
$var = self::parseRoute($url);
|
||||
$var = self::parseRoute($url,$config);
|
||||
// 解析剩余的URL参数
|
||||
$regx = substr_replace($regx, '', 0, strlen($matches[0]));
|
||||
if($regx) {
|
||||
preg_replace('@(\w+)\/([^,\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', $regx);
|
||||
preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){
|
||||
$var[strtolower($match[1])] = strip_tags($match[2]);
|
||||
}, $regx);
|
||||
}
|
||||
// 解析路由自动传人参数
|
||||
if(is_array($route) && isset($route[1])) {
|
||||
|
||||
@@ -33,21 +33,32 @@ class Session {
|
||||
* @return void
|
||||
*/
|
||||
static public function init($config=[]) {
|
||||
if(isset($config['prefix'])) self::$prefix = $config['prefix'];
|
||||
if(isset($config['prefix']))
|
||||
self::$prefix = $config['prefix'];
|
||||
if(isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])){
|
||||
session_id($_REQUEST[$config['var_session_id']]);
|
||||
}elseif(isset($config['id'])) {
|
||||
session_id($config['id']);
|
||||
}
|
||||
ini_set('session.auto_start', 0);
|
||||
if(isset($config['name'])) session_name($config['name']);
|
||||
if(isset($config['path'])) session_save_path($config['path']);
|
||||
if(isset($config['domain'])) ini_set('session.cookie_domain', $config['domain']);
|
||||
if(isset($config['expire'])) ini_set('session.gc_maxlifetime', $config['expire']);
|
||||
if(isset($config['use_trans_sid'])) ini_set('session.use_trans_sid', $config['use_trans_sid']?1:0);
|
||||
if(isset($config['use_cookies'])) ini_set('session.use_cookies', $config['use_cookies']?1:0);
|
||||
if(isset($config['cache_limiter'])) session_cache_limiter($config['cache_limiter']);
|
||||
if(isset($config['cache_expire'])) session_cache_expire($config['cache_expire']);
|
||||
if(isset($config['name']))
|
||||
session_name($config['name']);
|
||||
if(isset($config['path']))
|
||||
session_save_path($config['path']);
|
||||
if(isset($config['domain']))
|
||||
ini_set('session.cookie_domain', $config['domain']);
|
||||
if(isset($name['expire'])) {
|
||||
ini_set('session.gc_maxlifetime', $name['expire']);
|
||||
ini_set('session.cookie_lifetime', $name['expire']);
|
||||
}
|
||||
if(isset($config['use_trans_sid']))
|
||||
ini_set('session.use_trans_sid', $config['use_trans_sid']?1:0);
|
||||
if(isset($config['use_cookies']))
|
||||
ini_set('session.use_cookies', $config['use_cookies']?1:0);
|
||||
if(isset($config['cache_limiter']))
|
||||
session_cache_limiter($config['cache_limiter']);
|
||||
if(isset($config['cache_expire']))
|
||||
session_cache_expire($config['cache_expire']);
|
||||
if(!empty($config['type'])) { // 读取session驱动
|
||||
$class = 'Think\\Session\\Driver\\'. ucwords(strtolower($config['type']));
|
||||
// 检查驱动类
|
||||
@@ -65,11 +76,16 @@ class Session {
|
||||
* @return void
|
||||
*/
|
||||
static public function set($name,$value='',$prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$prefix;
|
||||
if($prefix){
|
||||
if (!is_array($_SESSION[$prefix])) {
|
||||
$_SESSION[$prefix] = [];
|
||||
$prefix = $prefix ? $prefix : self::$prefix;
|
||||
if(strpos($name,'.')){
|
||||
// 二维数组赋值
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
if($prefix){
|
||||
$_SESSION[$prefix][$name1][$name2] = $value;
|
||||
}else{
|
||||
$_SESSION[$name1][$name2] = $value;
|
||||
}
|
||||
}elseif($prefix){
|
||||
$_SESSION[$prefix][$name] = $value;
|
||||
}else{
|
||||
$_SESSION[$name] = $value;
|
||||
@@ -82,13 +98,27 @@ class Session {
|
||||
* @param string $prefix 作用域(前缀)
|
||||
* @return mixed
|
||||
*/
|
||||
static public function get($name,$prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$prefix;
|
||||
if($prefix){ // 获取session
|
||||
return isset($_SESSION[$prefix][$name])?$_SESSION[$prefix][$name]:null;
|
||||
static public function get($name='',$prefix='') {
|
||||
$prefix = $prefix ? $prefix : self::$prefix;
|
||||
if(''==$name){
|
||||
// 获取全部的session
|
||||
$value = $prefix ? $_SESSION[$prefix] : $_SESSION;
|
||||
}elseif($prefix){ // 获取session
|
||||
if(strpos($name,'.')){
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
$value = isset($_SESSION[$prefix][$name1][$name2])?$_SESSION[$prefix][$name1][$name2]:null;
|
||||
}else{
|
||||
$value = isset($_SESSION[$prefix][$name]) ? $_SESSION[$prefix][$name] : null;
|
||||
}
|
||||
}else{
|
||||
return isset($_SESSION[$name])?$_SESSION[$name]:null;
|
||||
if(strpos($name,'.')){
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
$value = isset($_SESSION[$name1][$name2]) ? $_SESSION[$name1][$name2] : null;
|
||||
}else{
|
||||
$value = isset($_SESSION[$name]) ? $_SESSION[$name] : null;
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,10 +129,19 @@ class Session {
|
||||
*/
|
||||
static public function delete($name,$prefix='') {
|
||||
$prefix = $prefix?$prefix:$this->prefix;
|
||||
if($prefix){
|
||||
unset($_SESSION[$prefix][$name]);
|
||||
if(strpos($name,'.')){
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
if($prefix){
|
||||
unset($_SESSION[$prefix][$name1][$name2]);
|
||||
}else{
|
||||
unset($_SESSION[$name1][$name2]);
|
||||
}
|
||||
}else{
|
||||
unset($_SESSION[$name]);
|
||||
if($prefix){
|
||||
unset($_SESSION[$prefix][$name]);
|
||||
}else{
|
||||
unset($_SESSION[$name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,11 +166,12 @@ class Session {
|
||||
* @return boolean
|
||||
*/
|
||||
static public function has($name,$prefix='') {
|
||||
$prefix = $prefix?$prefix:self::$prefix;
|
||||
if($prefix){
|
||||
return isset($_SESSION[$prefix][$name]);
|
||||
$prefix = $prefix ? $prefix : self::$prefix;
|
||||
if(strpos($name,'.')){ // 支持数组
|
||||
list($name1,$name2) = explode('.',$name);
|
||||
return $prefix ? isset($_SESSION[$prefix][$name1][$name2]) : isset($_SESSION[$name1][$name2]);
|
||||
}else{
|
||||
return isset($_SESSION[$name]);
|
||||
return $prefix ? isset($_SESSION[$prefix][$name]) : isset($_SESSION[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +180,7 @@ class Session {
|
||||
* @param string $name session操作名称
|
||||
* @return void
|
||||
*/
|
||||
static public function operate($name) {
|
||||
static private function operate($name) {
|
||||
if('pause'==$name){ // 暂停session
|
||||
session_write_close();
|
||||
}elseif('start'==$name){ // 启动session
|
||||
|
||||
40
Library/Think/Storage.php
Normal file
40
Library/Think/Storage.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Think;
|
||||
// 分布式文件存储类
|
||||
class Storage {
|
||||
|
||||
/**
|
||||
* 操作句柄
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
static protected $handler;
|
||||
|
||||
/**
|
||||
* 连接分布式文件系统
|
||||
* @access public
|
||||
* @param string $type 文件类型
|
||||
* @param array $options 配置数组
|
||||
* @return void
|
||||
*/
|
||||
static public function connect($type='File',$options=array()) {
|
||||
$class = 'Think\\Storage\\Driver\\'.ucwords($type);
|
||||
self::$handler = new $class($options);
|
||||
}
|
||||
|
||||
static public function __callstatic($method,$args){
|
||||
//调用缓存驱动的方法
|
||||
if(method_exists(self::$handler, $method)){
|
||||
return call_user_func_array(array(self::$handler,$method), $args);
|
||||
}
|
||||
}
|
||||
}
|
||||
125
Library/Think/Storage/Driver/File.php
Normal file
125
Library/Think/Storage/Driver/File.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Think\Storage\Driver;
|
||||
use Think\Storage;
|
||||
// 本地文件写入存储类
|
||||
class File extends Storage{
|
||||
|
||||
private $contents=array();
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
*/
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件内容读取
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return string
|
||||
*/
|
||||
public function read($filename,$type=''){
|
||||
return $this->get($filename,'content',$type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件写入
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param string $content 文件内容
|
||||
* @return boolean
|
||||
*/
|
||||
public function put($filename,$content,$type=''){
|
||||
$dir = dirname($filename);
|
||||
if(!is_dir($dir)){
|
||||
mkdir($dir,0777,true);
|
||||
}
|
||||
if(false === file_put_contents($filename,$content)){
|
||||
E(L('_STORAGE_WRITE_ERROR_').':'.$filename);
|
||||
}else{
|
||||
$this->contents[$filename]=$content;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件追加写入
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param string $content 追加的文件内容
|
||||
* @return boolean
|
||||
*/
|
||||
public function append($filename,$content,$type=''){
|
||||
if(is_file($filename)){
|
||||
$content = $this->read($filename,$type).$content;
|
||||
}
|
||||
return $this->put($filename,$content,$type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载文件
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param array $vars 传入变量
|
||||
* @return void
|
||||
*/
|
||||
public function load($_filename,$vars=null){
|
||||
if(!is_null($vars)){
|
||||
extract($vars, EXTR_OVERWRITE);
|
||||
}
|
||||
include $_filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件是否存在
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($filename,$type=''){
|
||||
return is_file($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return boolean
|
||||
*/
|
||||
public function unlink($filename,$type=''){
|
||||
unset($this->contents[$filename]);
|
||||
return is_file($filename) ? unlink($filename) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件信息
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param string $name 信息名 mtime或者content
|
||||
* @return boolean
|
||||
*/
|
||||
public function get($filename,$name,$type=''){
|
||||
if(!isset($this->contents[$filename])){
|
||||
if(!is_file($filename)) {
|
||||
return false;
|
||||
}
|
||||
$this->contents[$filename] = file_get_contents($filename);
|
||||
}
|
||||
$content=$this->contents[$filename];
|
||||
$info = array(
|
||||
'mtime' => filemtime($filename),
|
||||
'content' => $content
|
||||
);
|
||||
return $info[$name];
|
||||
}
|
||||
}
|
||||
194
Library/Think/Storage/Driver/Sae.php
Normal file
194
Library/Think/Storage/Driver/Sae.php
Normal file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: luofei614 <weibo.com/luofei614>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Think\Storage\Driver;
|
||||
use Think\Storage;
|
||||
// SAE环境文件写入存储类
|
||||
class Sae extends Storage{
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
*/
|
||||
private $mc;
|
||||
private $kvs = array();
|
||||
private $htmls = array();
|
||||
private $contents = array();
|
||||
public function __construct() {
|
||||
if(!function_exists('memcache_init')){
|
||||
header('Content-Type:text/html;charset=utf-8');
|
||||
exit('请在SAE平台上运行代码。');
|
||||
}
|
||||
$this->mc = @memcache_init();
|
||||
if(!$this->mc){
|
||||
header('Content-Type:text/html;charset=utf-8');
|
||||
exit('您未开通Memcache服务,请在SAE管理平台初始化Memcache服务');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得SaeKv对象
|
||||
*/
|
||||
private function getKv(){
|
||||
static $kv;
|
||||
if(!$kv){
|
||||
$kv = new \SaeKV();
|
||||
if(!$kv->init())
|
||||
E('您没有初始化KVDB,请在SAE管理平台初始化KVDB服务');
|
||||
}
|
||||
return $kv;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 文件内容读取
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return string
|
||||
*/
|
||||
public function read($filename,$type=''){
|
||||
switch(strtolower($type)){
|
||||
case 'f':
|
||||
$kv = $this->getKv();
|
||||
if(!isset($this->kvs[$filename])){
|
||||
$this->kvs[$filename]=$kv->get($filename);
|
||||
}
|
||||
return $this->kvs[$filename];
|
||||
default:
|
||||
return $this->get($filename,'content',$type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件写入
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param string $content 文件内容
|
||||
* @return boolean
|
||||
*/
|
||||
public function put($filename,$content,$type=''){
|
||||
switch(strtolower($type)){
|
||||
case 'f':
|
||||
$kv = $this->getKv();
|
||||
$this->kvs[$filename] = $content;
|
||||
return $kv->set($filename,$content);
|
||||
case 'html':
|
||||
$kv = $this->getKv();
|
||||
$content = time().$content;
|
||||
$this->htmls[$filename] = $content;
|
||||
return $kv->set($filename,$content);
|
||||
default:
|
||||
$content = time().$content;
|
||||
if(!$this->mc->set($filename,$content,MEMCACHE_COMPRESSED,0)){
|
||||
E(L('_STORAGE_WRITE_ERROR_').':'.$filename);
|
||||
}else{
|
||||
$this->contents[$filename] = $content;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件追加写入
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param string $content 追加的文件内容
|
||||
* @return boolean
|
||||
*/
|
||||
public function append($filename,$content,$type=''){
|
||||
if($old_content = $this->read($filename,$type)){
|
||||
$content = $old_content.$content;
|
||||
}
|
||||
return $this->put($filename,$content,$type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载文件
|
||||
* @access public
|
||||
* @param string $_filename 文件名
|
||||
* @param array $vars 传入变量
|
||||
* @return void
|
||||
*/
|
||||
public function load($_filename,$vars=null){
|
||||
if(!is_null($vars)){
|
||||
extract($vars, EXTR_OVERWRITE);
|
||||
}
|
||||
eval('?>'.$this->read($_filename));
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件是否存在
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($filename,$type=''){
|
||||
if($this->read($filename,$type)){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return boolean
|
||||
*/
|
||||
public function unlink($filename,$type=''){
|
||||
switch(strtolower($type)){
|
||||
case 'f':
|
||||
$kv = $this->getKv();
|
||||
unset($this->kvs[$filename]);
|
||||
return $kv->delete($filename);
|
||||
case 'html':
|
||||
$kv = $this->getKv();
|
||||
unset($this->htmls[$filename]);
|
||||
return $kv->delete($filename);
|
||||
default:
|
||||
unset($this->contents[$filename]);
|
||||
return $this->mc->delete($filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件信息
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param string $name 信息名 mtime或者content
|
||||
* @return boolean
|
||||
*/
|
||||
public function get($filename,$name,$type=''){
|
||||
switch(strtolower($type)){
|
||||
case 'html':
|
||||
if(!isset($this->htmls[$filename])){
|
||||
$kv = $this->getKv();
|
||||
$this->htmls[$filename] = $kv->get($filename);
|
||||
}
|
||||
$content = $this->htmls[$filename];
|
||||
break;
|
||||
default:
|
||||
if(!isset($this->contents[$filename])){
|
||||
$this->contents[$filename] = $this->mc->get($filename);
|
||||
}
|
||||
$content = $this->contents[$filename];
|
||||
}
|
||||
if(false===$content){
|
||||
return false;
|
||||
}
|
||||
$info = array(
|
||||
'mtime' => substr($content,0,10),
|
||||
'content' => substr($content,10)
|
||||
);
|
||||
return $info[$name];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,9 +17,9 @@ namespace Think;
|
||||
* 编译型模板引擎 支持动态缓存
|
||||
*/
|
||||
class Template {
|
||||
protected $tVar = []; // 模板变量
|
||||
protected $data = []; // 模板变量
|
||||
protected $config = [ // 引擎配置
|
||||
'tpl_path' => '',
|
||||
'tpl_path' => '', // 模板路径
|
||||
'tpl_suffix' => '.html', // 默认模板文件后缀
|
||||
'cache_suffix' => '.php', // 默认模板缓存后缀
|
||||
'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
|
||||
@@ -28,8 +28,8 @@ class Template {
|
||||
'tpl_end' => '}', // 模板引擎普通标签结束标记
|
||||
'strip_space' => false, // 是否去除模板文件里面的html空格与换行
|
||||
'tpl_cache' => true, // 是否开启模板编译缓存,设为false则每次都会重新编译
|
||||
'compile_type' => 'file',
|
||||
'cache_path' => '',
|
||||
'compile_type' => 'file', // 模板编译类型
|
||||
'cache_path' => '', // 模板缓存目录
|
||||
'cache_prefix' => '', // 模板缓存前缀标识,可以动态改变
|
||||
'cache_time' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒)
|
||||
'layout_item' => '{__CONTENT__}', // 布局模板的内容替换标识
|
||||
@@ -38,7 +38,7 @@ class Template {
|
||||
'taglib_load' => true, // 是否使用内置标签库之外的其它标签库,默认自动检测
|
||||
'taglib_build_in' => 'cx', // 内置标签库名称(标签使用不必指定标签库名称),以逗号分隔 注意解析顺序
|
||||
'taglib_pre_load' => '', // 需要额外加载的标签库(须指定标签库名称),多个以逗号分隔
|
||||
'display_cache' => false,
|
||||
'display_cache' => false, // 模板渲染缓存
|
||||
];
|
||||
|
||||
private $literal = [];
|
||||
@@ -84,9 +84,9 @@ class Template {
|
||||
*/
|
||||
public function assign($name,$value=''){
|
||||
if(is_array($name)) {
|
||||
$this->tVar = array_merge($this->tVar,$name);
|
||||
$this->data = array_merge($this->data,$name);
|
||||
}else {
|
||||
$this->tVar[$name] = $value;
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ class Template {
|
||||
}
|
||||
|
||||
public function get($name){
|
||||
return $this->tVar[$name];
|
||||
return $this->data[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,7 +118,7 @@ class Template {
|
||||
*/
|
||||
public function display($template,$vars=[],$config=[]) {
|
||||
if($vars){
|
||||
$this->tVar = $vars;
|
||||
$this->data = $vars;
|
||||
}
|
||||
if($config){
|
||||
$this->config($config);
|
||||
@@ -133,7 +133,7 @@ class Template {
|
||||
ob_start();
|
||||
ob_implicit_flush(0);
|
||||
// 读取编译存储
|
||||
$this->storage->read($cacheFile,$this->tVar);
|
||||
$this->storage->read($cacheFile,$this->data);
|
||||
// 获取并清空缓存
|
||||
$content = ob_get_clean();
|
||||
if($this->config['cache_id'] && $this->config['display_cache']) {
|
||||
@@ -152,7 +152,7 @@ class Template {
|
||||
*/
|
||||
public function fetch($content,$vars=[]) {
|
||||
if($vars){
|
||||
$this->tVar = $vars;
|
||||
$this->data = $vars;
|
||||
}
|
||||
$cacheFile = $this->config['cache_path'].$this->config['cache_prefix'].md5($content).$this->config['cache_suffix'];
|
||||
if(!$this->checkCache($content,$cacheFile)) { // 缓存无效
|
||||
@@ -160,7 +160,7 @@ class Template {
|
||||
$this->compiler($content,$cacheFile);
|
||||
}
|
||||
// 读取编译存储
|
||||
$this->storage->read($cacheFile,$this->tVar);
|
||||
$this->storage->read($cacheFile,$this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -591,22 +591,22 @@ class Template {
|
||||
//模板函数过滤
|
||||
$fun = strtolower(trim($args[0]));
|
||||
switch($fun) {
|
||||
case 'default': // 特殊模板函数
|
||||
$name = '('.$name.')?('.$name.'):'.$args[1];
|
||||
break;
|
||||
default: // 通用模板函数
|
||||
if(!in_array($fun,$template_deny_funs)){
|
||||
if(isset($args[1])){
|
||||
if(strstr($args[1],'###')){
|
||||
$args[1] = str_replace('###',$name,$args[1]);
|
||||
$name = "$fun($args[1])";
|
||||
}else{
|
||||
$name = "$fun($name,$args[1])";
|
||||
case 'default': // 特殊模板函数
|
||||
$name = '('.$name.')?('.$name.'):'.$args[1];
|
||||
break;
|
||||
default: // 通用模板函数
|
||||
if(!in_array($fun,$template_deny_funs)){
|
||||
if(isset($args[1])){
|
||||
if(strstr($args[1],'###')){
|
||||
$args[1] = str_replace('###',$name,$args[1]);
|
||||
$name = "$fun($args[1])";
|
||||
}else{
|
||||
$name = "$fun($name,$args[1])";
|
||||
}
|
||||
}else if(!empty($args[0])){
|
||||
$name = "$fun($name)";
|
||||
}
|
||||
}else if(!empty($args[0])){
|
||||
$name = "$fun($name)";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $name;
|
||||
@@ -627,11 +627,14 @@ class Template {
|
||||
$vars[2] = trim($vars[2]);
|
||||
switch($vars[1]){
|
||||
case 'SERVER':
|
||||
$parseStr = '$_SERVER[\''.strtoupper($vars[2]).'\']';break;
|
||||
$parseStr = '$_SERVER[\''.strtoupper($vars[2]).'\']';
|
||||
break;
|
||||
case 'GET':
|
||||
$parseStr = '$_GET[\''.$vars[2].'\']';break;
|
||||
$parseStr = '$_GET[\''.$vars[2].'\']';
|
||||
break;
|
||||
case 'POST':
|
||||
$parseStr = '$_POST[\''.$vars[2].'\']';break;
|
||||
$parseStr = '$_POST[\''.$vars[2].'\']';
|
||||
break;
|
||||
case 'COOKIE':
|
||||
if(isset($vars[3])) {
|
||||
$parseStr = '$_COOKIE[\''.$vars[2].'\'][\''.$vars[3].'\']';
|
||||
@@ -647,19 +650,25 @@ class Template {
|
||||
}
|
||||
break;
|
||||
case 'ENV':
|
||||
$parseStr = '$_ENV[\''.strtoupper($vars[2]).'\']';break;
|
||||
$parseStr = '$_ENV[\''.strtoupper($vars[2]).'\']';
|
||||
break;
|
||||
case 'REQUEST':
|
||||
$parseStr = '$_REQUEST[\''.$vars[2].'\']';break;
|
||||
$parseStr = '$_REQUEST[\''.$vars[2].'\']';
|
||||
break;
|
||||
case 'CONST':
|
||||
$parseStr = strtoupper($vars[2]);break;
|
||||
$parseStr = strtoupper($vars[2]);
|
||||
break;
|
||||
case 'LANG':
|
||||
$parseStr = 'L("'.$vars[2].'")';break;
|
||||
$parseStr = 'L("'.$vars[2].'")';
|
||||
break;
|
||||
case 'CONFIG':
|
||||
if(isset($vars[3])) {
|
||||
$vars[2] .= '.'.$vars[3];
|
||||
}
|
||||
$parseStr = 'C("'.$vars[2].'")';break;
|
||||
default:break;
|
||||
$parseStr = 'C("'.$vars[2].'")';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}else if(count($vars)==2){
|
||||
switch($vars[1]){
|
||||
@@ -676,8 +685,9 @@ class Template {
|
||||
$parseStr = $this->config['tpl_end'];
|
||||
break;
|
||||
default:
|
||||
if(defined($vars[1]))
|
||||
if(defined($vars[1])){
|
||||
$parseStr = $vars[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $parseStr;
|
||||
|
||||
@@ -10,17 +10,20 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Template\Driver;
|
||||
use Think\Exception;
|
||||
|
||||
class File {
|
||||
// 写入编译缓存
|
||||
public function write($cacheFile,$content){
|
||||
// 检测模板目录
|
||||
$dir = dirname($cacheFile);
|
||||
if(!is_dir($dir))
|
||||
mkdir($dir,0755,true);
|
||||
if(!is_dir($dir)){
|
||||
mkdir($dir,0777,true);
|
||||
}
|
||||
// 生成模板缓存文件
|
||||
if( false === file_put_contents($cacheFile,$content))
|
||||
E('_CACHE_WRITE_ERROR_:'.$cacheFile);
|
||||
if( false === file_put_contents($cacheFile,$content)){
|
||||
throw new Exception('_CACHE_WRITE_ERROR_:'.$cacheFile);
|
||||
}
|
||||
}
|
||||
|
||||
// 读取编译编译
|
||||
|
||||
@@ -10,24 +10,27 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
use Think\Exception;
|
||||
|
||||
class View {
|
||||
protected $engine = null; // 模板引擎实例
|
||||
protected $theme = ''; // 模板主题名称
|
||||
protected $data = []; // 模板变量
|
||||
protected $config = [ // 视图参数
|
||||
'http_output_content' => true,
|
||||
'http_content_type' => 'text/html',
|
||||
'http_charset' => 'utf-8',
|
||||
'http_cache_control' => 'private',
|
||||
'http_render_content' => false,
|
||||
'theme_on' => false,
|
||||
'auto_detect_theme' => false,
|
||||
'var_theme' => 't',
|
||||
'default_theme' => 'default',
|
||||
'http_cache_id' => null,
|
||||
'view_path' => '',
|
||||
'view_suffix' => '.html',
|
||||
'http_output_content' => true,
|
||||
'http_content_type' => 'text/html',
|
||||
'http_charset' => 'utf-8',
|
||||
'http_cache_control' => 'private',
|
||||
'http_render_content' => false,
|
||||
'theme_on' => false,
|
||||
'auto_detect_theme' => false,
|
||||
'var_theme' => 't',
|
||||
'default_theme' => 'default',
|
||||
'http_cache_id' => null,
|
||||
'view_path' => '',
|
||||
'view_suffix' => '.html',
|
||||
'view_depr' => '/',
|
||||
'view_layer' => 'View',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -100,11 +103,11 @@ class View {
|
||||
* @return mixed
|
||||
*/
|
||||
public function display($template = '', $vars = [], $cache_id = '') {
|
||||
Tag::listen('view_begin', $template);
|
||||
Hook::listen('view_begin', $template);
|
||||
// 解析并获取模板内容
|
||||
$content = $this->fetch($template, $vars, $cache_id);
|
||||
// 输出内容过滤
|
||||
Tag::listen('view_filter', $content);
|
||||
Hook::listen('view_filter', $content);
|
||||
// 输出模板内容
|
||||
if($this->config['http_output_content']) {
|
||||
$this->render($content);
|
||||
@@ -123,10 +126,12 @@ class View {
|
||||
*/
|
||||
protected function fetch($template, $vars = [], $cache_id='') {
|
||||
if(!$this->config['http_render_content']) {
|
||||
// 获取模板文件名
|
||||
$template = $this->parseTemplate($template);
|
||||
// 模板不存在 抛出异常
|
||||
if(!is_file($template))
|
||||
E('template file not exists:' . $template);
|
||||
if(!is_file($template)) {
|
||||
throw new Exception('template file not exists:' . $template);
|
||||
}
|
||||
}
|
||||
$vars = $vars ? $vars : $this->data;
|
||||
// 页面缓存
|
||||
@@ -152,17 +157,25 @@ class View {
|
||||
if(is_file($template)) {
|
||||
return $template;
|
||||
}
|
||||
$template = str_replace(':', '/', $template);
|
||||
// 获取当前主题名称
|
||||
$theme = $this->getTemplateTheme();
|
||||
$depr = $this->config['view_depr'];
|
||||
$template = str_replace(':', $depr, $template);
|
||||
|
||||
// 获取当前模块
|
||||
$module = MODULE_NAME;
|
||||
if(strpos($template,'@')){ // 跨模块调用模版文件
|
||||
list($module,$template) = explode('@',$template);
|
||||
}
|
||||
// 获取当前主题的模版路径
|
||||
defined('THEME_PATH') or define('THEME_PATH', $this->getThemePath($module));
|
||||
|
||||
// 分析模板文件规则
|
||||
if('' == $template) {
|
||||
// 如果模板文件名为空 按照默认规则定位
|
||||
$template = CONTROLLER_NAME . '/' . ACTION_NAME;
|
||||
}elseif(false === strpos($template, '/')){
|
||||
$template = CONTROLLER_NAME . '/' . $template;
|
||||
$template = CONTROLLER_NAME . $depr . ACTION_NAME;
|
||||
}elseif(false === strpos($template, $depr)){
|
||||
$template = CONTROLLER_NAME . $depr . $template;
|
||||
}
|
||||
return ($this->config['view_path'] ? $this->config['view_path'] : MODULE_PATH . 'View/').$theme.$template.$this->config['view_suffix'];
|
||||
return THEME_PATH.$template.$this->config['view_suffix'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,7 +183,7 @@ class View {
|
||||
* @access private
|
||||
* @return string
|
||||
*/
|
||||
private function getTemplateTheme() {
|
||||
private function getTemplateTheme($module) {
|
||||
if($this->config['theme_on']) {
|
||||
if($this->theme) { // 指定模板主题
|
||||
$theme = $this->theme;
|
||||
@@ -182,7 +195,7 @@ class View {
|
||||
}elseif(Cookie::get('think_theme')){
|
||||
$theme = Cookie::get('think_theme');
|
||||
}
|
||||
if(!is_dir(MODULE_PATH . 'View/' . $theme)) {
|
||||
if(!is_dir(APP_PATH.$module . '/'. $this->config['view_layer'].'/' . $theme)) {
|
||||
$theme = $this->config['default_theme'];
|
||||
}
|
||||
Cookie::set('think_theme', $theme, 864000);
|
||||
@@ -194,6 +207,24 @@ class View {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的模板路径
|
||||
* @access protected
|
||||
* @param string $module 模块名
|
||||
* @return string
|
||||
*/
|
||||
protected function getThemePath($module=MODULE_NAME){
|
||||
// 获取当前主题名称
|
||||
$theme = $this->getTemplateTheme($module);
|
||||
// 获取当前主题的模版路径
|
||||
$tmplPath = $this->config['view_path']; // 模块设置独立的视图目录
|
||||
if(!$tmplPath){
|
||||
// 定义TMPL_PATH 则改变全局的视图目录到模块之外
|
||||
$tmplPath = defined('TMPL_PATH')? TMPL_PATH.$module.'/' : APP_PATH.$module.'/'.$this->config['view_layer'].'/';
|
||||
}
|
||||
return $tmplPath.$theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图输出参数设置
|
||||
* @access public
|
||||
|
||||
Reference in New Issue
Block a user