Controller.class.php
17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think;
use Com\Formhash;
/**
* ThinkPHP 控制器基类 抽象类
*/
abstract class Controller
{
/**
* 视图实例对象
*
* @var view
* @access protected
*/
protected $view = null;
/**
* 控制器参数
*
* @var config
* @access protected
*/
protected $config = array();
/**
* 架构函数 取得模板对象实例
*
* @access public
*/
public function __construct()
{
Hook::listen('action_begin', $this->config);
// 实例化视图类
$this->view = Think::instance('Think\View');
// 控制器初始化
if (method_exists($this, '_initialize')) {
$this->_initialize();
}
}
/**
* 模板显示 调用内置的模板引擎显示方法,
*
* @access protected
*
* @param string $templateFile 指定要调用的模板文件 默认为空 由系统自动定位模板文件
* @param string $charset 输出编码
* @param string $contentType 输出类型
* @param string $content 输出内容
* @param string $prefix 模板缓存前缀
*
* @return void
*/
protected function display($templateFile = '', $charset = '', $contentType = '', $content = '', $prefix = '')
{
$this->view->display($templateFile, $charset, $contentType, $content, $prefix);
}
/**
* 输出内容文本可以包括Html 并支持内容解析
*
* @access protected
*
* @param string $content 输出内容
* @param string $charset 模板输出字符集
* @param string $contentType 输出类型
* @param string $prefix 模板缓存前缀
*
* @return mixed
*/
protected function show($content, $charset = '', $contentType = '', $prefix = '')
{
$this->view->display('', $charset, $contentType, $content, $prefix);
}
/**
* 获取输出页面内容 调用内置的模板引擎fetch方法,
*
* @access protected
*
* @param string $templateFile 指定要调用的模板文件 默认为空 由系统自动定位模板文件
* @param string $content 模板输出内容
* @param string $prefix 模板缓存前缀*
*
* @return string
*/
protected function fetch($templateFile = '', $content = '', $prefix = '')
{
return $this->view->fetch($templateFile, $content, $prefix);
}
/**
* 创建静态页面
*
* @access protected @htmlfile 生成的静态文件名称 @htmlpath 生成的静态文件路径
*
* @param string $templateFile 指定要调用的模板文件 默认为空 由系统自动定位模板文件
*
* @return string
*/
protected function buildHtml($htmlfile = '', $htmlpath = '', $templateFile = '')
{
$content = $this->fetch($templateFile);
$htmlpath = ! empty($htmlpath) ? $htmlpath : HTML_PATH;
$htmlfile = $htmlpath . $htmlfile . C('HTML_FILE_SUFFIX');
Storage::put($htmlfile, $content, 'html');
return $content;
}
/**
* 模板主题设置
*
* @access protected
*
* @param string $theme 模版主题
*
* @return Action
*/
protected function theme($theme)
{
$this->view->theme($theme);
return $this;
}
/**
* 模板变量赋值
*
* @access protected
*
* @param mixed $name 要显示的模板变量
* @param mixed $value 变量的值
*
* @return Action
*/
protected function assign($name, $value = '')
{
$this->view->assign($name, $value);
return $this;
}
public function __set($name, $value)
{
$this->assign($name, $value);
}
/**
* 取得模板显示变量的值
*
* @access protected
*
* @param string $name 模板显示变量
*
* @return mixed
*/
public function get($name = '')
{
return $this->view->get($name);
}
public function __get($name)
{
return $this->get($name);
}
/**
* 检测模板变量的值
*
* @access public
*
* @param string $name 名称
*
* @return boolean
*/
public function __isset($name)
{
return $this->get($name);
}
/**
* 魔术方法 有不存在的操作的时候执行
*
* @access public
*
* @param string $method 方法名
* @param array $args 参数
*
* @return mixed
*/
public function __call($method, $args)
{
// by zhuxun begin. 创建不存在的方法
if (APP_DEBUG && cfg('CREATE_ACTION_ON') && I('request._create')) {
// 获取类名
$classname = get_class($this);
// 默认控制层名称
$c_layer = ucfirst(C('DEFAULT_C_LAYER'));
$classname = preg_replace("/" . $c_layer . "$/is", '', $classname);
// 切分路径
$paths = explode("/", str_replace("\\", "/", $classname));
// 获取模块名称
$module = array_shift($paths);
// 剔除控制层目录
array_shift($paths);
// 重新拼接控制层
$controller = implode("/", $paths);
// 创建操作方法
Build::buildAction($module, $controller, ACTION_NAME);
// 报生成成功信息
E(__CLASS__ . ':' . $method . L('METHOD_CREATED'));
return;
}
// end.
if (0 === strcasecmp($method, ACTION_NAME . C('ACTION_SUFFIX'))) {
if (method_exists($this, '_empty')) {
// 如果定义了_empty操作 则调用
$this->_empty($method, $args);
} elseif (file_exists_case($this->view->parseTemplate())) {
// 检查是否存在默认模版 如果有直接输出模版
$this->display();
} else {
E(L('_ERROR_ACTION_') . ':' . ACTION_NAME);
}
} else {
E(__CLASS__ . ':' . $method . L('_METHOD_NOT_EXIST_'));
return;
}
}
/**
* 操作错误跳转的快捷方法
*
* @access protected
*
* @param string $message 错误信息
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
*
* @return void
*/
protected function error($message = '', $jumpUrl = '', $ajax = false)
{
$this->dispatchJump($message, 0, $jumpUrl, $ajax);
}
/**
* 操作成功跳转的快捷方法
*
* @access protected
*
* @param string $message 提示信息
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
*
* @return void
*/
protected function success($message = '', $jumpUrl = '', $ajax = false)
{
$this->dispatchJump($message, 1, $jumpUrl, $ajax);
}
/**
* Ajax方式返回数据到客户端
*
* @access protected
*
* @param mixed $data 要返回的数据
* @param String $type AJAX返回数据格式
* @param int $json_option 传递给json_encode的option参数
*
* @return void
*/
protected function ajaxReturn($data, $type = '', $json_option = 0)
{
if (empty($type)) {
$type = C('DEFAULT_AJAX_RETURN');
}
switch (strtoupper($type)) {
case 'JSON':
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data, $json_option));
case 'XML':
// 返回xml格式数据
header('Content-Type:text/xml; charset=utf-8');
exit(xml_encode($data));
case 'JSONP':
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
$handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
exit($handler . '(' . json_encode($data, $json_option) . ');');
case 'EVAL':
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
exit($data);
default:
// 用于扩展其他返回格式数据
Hook::listen('ajax_return', $data);
}
}
/**
* Action跳转(URL重定向) 支持指定模块和延时跳转
*
* @access protected
*
* @param string $url 跳转的URL表达式
* @param array $params 其它URL参数
* @param integer $delay 延时跳转的时间 单位为秒
* @param string $msg 跳转提示信息
*
* @return void
*/
protected function redirect($url, $params = array(), $delay = 0, $msg = '')
{
$url = U($url, $params);
redirect($url, $delay, $msg);
}
/**
* 默认跳转操作 支持错误导向和正确跳转 调用模板显示 默认为public目录下面的success页面 提示页面为可配置 支持模板标签
*
* @param string $message 提示信息
* @param Boolean $status 状态
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
*
* @access private
* @return void
*/
private function dispatchJump($message, $status = 1, $jumpUrl = '', $ajax = false)
{
if (true === $ajax || IS_AJAX) { // AJAX提交
$data = is_array($ajax) ? $ajax : array();
$data['info'] = $message;
$data['status'] = $status;
$data['url'] = $jumpUrl;
$this->ajaxReturn($data);
}
if (is_int($ajax)) {
$this->assign('waitSecond', $ajax);
}
if (! empty($jumpUrl)) {
$this->assign('jumpUrl', $jumpUrl);
}
// 提示标题
$this->assign('msgTitle', $status ? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
// 如果设置了关闭窗口,则提示完毕后自动关闭窗口
if ($this->get('closeWin')) {
$this->assign('jumpUrl', 'javascript:window.close();');
}
$this->assign('status', $status); // 状态
// 保证输出不受静态缓存影响
C('HTML_CACHE_ON', false);
if ($status) { // 发送成功信息
$this->assign('message', $message); // 提示信息
// 成功操作后默认停留1秒
if (! isset($this->waitSecond)) {
$this->assign('waitSecond', '1');
}
// 默认操作成功自动返回操作前页面
if (! isset($this->jumpUrl)) {
$this->assign("jumpUrl", $_SERVER["HTTP_REFERER"]);
}
$this->display(C('TMPL_ACTION_SUCCESS'));
} else {
$this->assign('error', $message); // 提示信息
// 发生错误时候默认停留3秒
if (! isset($this->waitSecond)) {
$this->assign('waitSecond', '3');
}
// 默认发生错误的话自动返回上页
if (! isset($this->jumpUrl)) {
$this->assign('jumpUrl', "javascript:history.back(-1);");
}
$this->display(C('TMPL_ACTION_ERROR'));
// 中止执行 避免出错后继续执行
exit();
}
}
/**
* 析构方法
*
* @access public
*/
public function __destruct()
{
// 执行后续操作
Hook::listen('action_end');
}
// by zhuxun, begin.
/**
* _success_message
* 成功消息提示
*
* @param mixed $message 提示消息
* @param mixed $url 转向URL
* @param mixed $extra 扩展数据
* @param mixed $title 标题
* @param string $tpl 模板文件
*
* @return void
*/
protected function _success_message($message = null, $url = null, $extra = null, $title = null, $tpl = 'Message')
{
$this->_message('Success', $message, $url, $extra, $title, $tpl);
}
/**
* _error_message
* 失败消息提示
*
* @param mixed $message 提示消息
* @param mixed $url 转向URL
* @param mixed $extra 扩展数据
* @param mixed $title 标题
* @param string $tpl 模板文件
*
* @return void
*/
protected function _error_message($message = null, $url = null, $extra = null, $title = null, $tpl = 'Error')
{
$this->_message('Error', $message, $url, $extra, $title, $tpl);
}
/**
* message
* 消息提示
*
* @param string $type 提示类型
* @param mixed $message 提示消息
* @param mixed $url 转向URL
* @param mixed $extra 扩展数据
* @param mixed $title 标题
* @param string $tpl 模板文件
*
* @return void
*/
protected function _message($type = 'Success', $message = null, $url = null, $extra = null, $title = null, $tpl = 'Message')
{
// 不缓存提示页面
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Expires: 0");
// 回调页面 js 相关
$extra_js = '';
// js 函数名
$handlekey = I('get.handlekey');
// 解析消息
$message = L($message);
// 过滤单引号
$jsmessage = str_replace("'", "\\'", $message);
// 如果是成功提示
if ($type == 'Success') {
if (! $title) {
$title = L('_succeed_title');
}
if (! $message) {
$message = L('_succeed_message');
}
$extra_js .= 'if(typeof succeedhandle_' . $handlekey . '==\'function\') {succeedhandle_' . $handlekey . '(\'' . $url . '\', \'' . $jsmessage . '\');}';
} else { // 如果是失败提示
if (! $title) {
$title = L('_failed_title');
}
if (! $message) {
$message = L('_failed_message');
}
$extra_js .= 'if(typeof errorhandle_' . $handlekey . '==\'function\') {errorhandle_' . $handlekey . '(\'' . $url . '\', \'' . $jsmessage . '\');}';
}
// 检查来源链接,不合法的跳转到首页
if (! $url) {
$url = I('server.HTTP_REFERER', '/');
}
// 把 js 和消息一起显示
$message .= $extra_js ? '<script type="text/javascript" reload="true">' . $extra_js . '</script>' : '';
$this->view->assign('title', preg_replace('/\s*\d+\s*\:\s*/i', '', $title));
$this->view->assign('url', $url);
$this->view->assign('message', preg_replace('/\s*\d+\s*\:\s*/i', '', $message));
$this->view->assign('extra', $extra);
$this->view->assign('type', $type);
$this->_output($tpl);
}
/**
* _output
* 输出模板
*
* @param string $tpl 引入的模板
*/
protected function _output($tpl)
{
// 当前时间戳
$this->view->assign('timestamp', NOW_TIME);
$this->view->assign('wbs_javascript_path', cfg('SCRIPTDIR'));
$this->view->assign('wbs_css_path', cfg('CSSDIR'));
// 输出 forumHash
$this->view->assign('formhash', $this->_generate_formhash());
// 如果模板不是以当前模块或者 / 开始时
$cns = explode('/', CONTROLLER_NAME);
$index = 0;
$cname = '';
if (false === ($index = stripos($tpl, '/'))) {
$cname = $tpl;
} else {
$cname = substr($tpl, 0, $index);
}
// 模板检查
if ($cns[0] != $cname && '/' != $tpl{0} && false === stripos($tpl, '@')) {
$tpl = $cns[0] . '/' . $tpl;
}
// 调用模板
$this->view->display($tpl);
exit;
}
/**
* 生成 formhash
*/
protected function _generate_formhash()
{
// 拼凑源字串
$fh_key = I('server.HTTP_HOST') . cfg('FORMHASH_SECRET');
// 生成 hash
$formhash = Formhash::instance();
$hash = '';
$formhash->generate($hash, $fh_key);
return $hash;
}
/**
* 显示异常信息
*
* @param mixed $data 异常信息
* @param string $tpl 模板
*
* @return bool
*/
public function showException($data = null, $tpl = '')
{
list($errcode, $errmsg) = parse_lang_error('_ERR_DEFAULT');
// 如果需要返回的是异常
if ($data instanceof Exception) {
// 如果是显示给用户的错误
if ($data->is_show()) {
$errcode = $data->getCode();
$errmsg = $data->getMessage();
}
} elseif ($data instanceof \Exception) {
$errcode = $data->getCode();
$errmsg = $data->getMessage();
}
// 输出结果
$this->assign('error_msg', $errmsg);
$this->assign('error_code', $errcode);
$tpl = empty($tpl) ? 'Common@Frontend/Error' : $tpl;
$this->_output($tpl);
return true;
}
// end.
}
// 设置控制器别名 便于升级
class_alias('Think\Controller', 'Think\Action');