/
base.php
340 lines (301 loc) · 9.35 KB
/
base.php
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
<?php
defined('SYSPATH') or die('No direct script access.') ;
/**
* 控制器基础类
*
* @package controller
* @author zhong(小钟) <regulusyun@gmail.com>
* @copyright Copyright (c) 2010-9-14
* @license http://www.gnu.org/licenses/gpl.html GPL 3
*/
abstract class Controller_Base extends Controller {
/**
* 用户信息
*/
public $auth = '';
public $auth_field = '';
public $thumb = null;
/**
* 是否输出视图
*
* @var bool
*/
public $auto_render = TRUE;
/**
* 预置模板对象
*
* @var object
*/
public $template = null;
/**
* 默认模板目录
*
* @var string
*/
public $template_dir = 'default';
public $img_size = array(
'_640_640.', '_130_130.', '_120_120.'
);
/**
* 控制器方法执行前的操作
*
*/
public function before()
{
parent::before();
if (!($this->dir = $this->request->directory)) {
$this->dir = 'default';
} else {
$this->dir = $this->request->directory;
$this->template_dir = $this->request->directory;
}
if (!($this->ctrl = $this->request->controller)) {
$this->ctrl = 'index';
}
if (!($this->act = $this->request->action)) {
$this->act = 'index';
}
/* 初始化模板对象 */
$this->template = new View();
$this->template->script = '';
$this->template->css = '';
$this->template->directory = $this->dir;
$this->template->controller = $this->ctrl;
$this->template->action = $this->act;
//缩略图
$this->template->thumb = $this->thumb = Thumb::getInstance();
/* 判断是否已登录 */
$this->template->auth = $this->auth = Session::instance()->get('user', false);
$this->template->auth_field = $this->auth_field = isset($this->auth['field']) ? $this->auth['field'] : array();
$sys_configs = Cache::instance()->get('sys_configs');
if (null == $sys_configs) {
$sys_configs = ORM::factory('user')->setConfig();
}
$this->template->configs = $this->configs = $sys_configs;
/* xss过滤 */
if(!empty($_POST))$_POST = Security::xss_clean($_POST);
if(!empty($_GET))$_GET = Security::xss_clean($_GET);
}
/**
* 控制器方法执行后的操作
*
*/
public function after()
{
if ($this->auto_render) {
$tplfile = strtolower($this->template_dir.'/'.str_replace('_','/',$this->ctrl).'/'.str_replace('_','/',$this->act));
try {
$this->template->set_filename($tplfile);
}
catch(Exception $e) {
die("Template file '$tplfile' is not exist!");
}
$this->request->response = $this->template;
// 生成首页html
if( $this->dir == 'default' and $this->ctrl=='index' and $this->act=='index')
$this->make_html($this->request->response,'index.html', '86400', 'cache');
// 生成公共社区html
if( $this->dir == 'default' and $this->ctrl=='pic' and $this->act=='index')
$this->make_html($this->request->response,'pic.html', '86400', 'cache');
// 生成公共社区html
if( $this->dir == 'book' and $this->ctrl=='index' and $this->act=='index')
$this->make_html($this->request->response,'book.html', '86400', 'cache');
/* 是否开启调试环境 */
if(defined('IN_PRODUCTION') and IN_PRODUCTION === false)
$this->request->response .= '<div id="kohana-profiler">'.View::factory('profiler/stats').'</div>';
}
parent::after();
}
/**
* 生成静态文件
*
* @param string $content
* @param string $file_name
* @param int $life_time 默认24小时
* @param string $path
*/
public function make_html($content,$file_name,$life_time= 86400 ,$path = 'cache/html')
{
if(empty($content) or empty($file_name))return;
$dir = DOCROOT . $path;
$file_name = $dir. DIRECTORY_SEPARATOR.$file_name;
if(file_exists($file_name) and (filemtime($file_name) + $life_time>time()))return;
if(!is_dir($dir))
{
mkdir($dir,0777,true);
}
@file_put_contents($file_name,$content);
}
/**
* 添加js脚本
*
*/
public function _add_script()
{
$args = func_get_args();
foreach($args as $v)
{
//$v .= '?'.time();
$this->template->script .= html::script($v)."\n";
}
}
/**
* 添加css样式表
*
*/
public function _add_css()
{
$args = func_get_args();
foreach($args as $v)
{
$this->template->css .= html::style($v)."\n";
}
}
/**
* 获取 Query String 数据
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getQuery($key = null, $default = null) {
if (null == $key)
return $_GET;
return isset($_GET[$key]) ? $_GET[$key] : $default;
}
/**
* 获取 Http Post 数据
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getPost($key = null, $default = null) {
if (null == $key)
return $_POST;
return isset($_POST[$key]) ? $_POST[$key] : $default;
}
/**
* 获取 Http Request 数据
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getRequest($key = null, $default = null) {
if (null == $key)
return $_REQUEST;
return isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default;
}
/**
* 获取 Files Upload 数据
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getFiles($key = null, $default = null) {
if (null == $key)
return $_FILES;
return isset($_FILES[$key]) ? $_FILES[$key] : $default;
}
/**
* 获取 Http Server 数据
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getServer($key = null, $default = null) {
if (null == $key)
return $_SERVER;
return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
}
/**
* 获取 Http Request 使用的传递方法
*
* @return string
*/
public function getMethod()
{
return $this->getServer('REQUEST_METHOD');
}
/**
* 判断是否 Post 方式
*
* @return boolean
*/
public function isPost()
{
return 'POST' == $this->getMethod();
}
/**
* 公共报错页面
*
* @param string $msg_detail
* @param string $msg_type 默认0,成功1,失败-1
* @param array $links
* @param bool $auto_redirect
*/
public function show_message($msg_detail, $msg_type = 0, $links = array(), $auto_redirect = true, $time = 3000)
{
$str = '';
if (!is_array($msg_detail)) {
$msg_detail = array($msg_detail);
}
$msg_detail = implode(', ', $msg_detail);
if (count($links) == 0)
{
$links[0]['text'] = "上一页";
$links[0]['href'] = 'javascript:history.go(-1)';
}
$links[] = array(
'text' => '返回网站首页',
'href' => '/',
);
$this->template->msg_detail = $msg_detail;
$this->template->msg_type = $msg_type;
$this->template->links = $links;
$this->template->time = $time;
$this->template->default_text = $links[0]['text'];
$this->template->default_url = $links[0]['href'];
$this->template->auto_redirect = $auto_redirect;
$this->template->set_filename($this->template_dir.'/message');
exit($this->template->render());
}
/*
* 验证空间过期,容量
*/
public function checkSpace()
{
$user = ORM::factory('user');
$user->upcache($this->auth['uid']);
if ($user->check_expire($this->auth['uid'])) {
if($this->auth['rank'] == 1) {
$links[] = array('text' => '去完成积分任务','href' => '/job');
} else {
$links[] = array('text' => '去付费中心','href' => '/pay/upgrade');
}
$this->show_message('您的帐号已过期或空间不足,请续费或完成积分任务,再进行操作', 0, $links);
die();
}
if ($user->check_space($this->auth['uid'])) {
$links[] = array('text' => '去付费中心','href' => '/pay/upgrade');
$this->show_message('您的帐号已过期或空间不足,请续费或完成积分任务,再进行操作', 0, $links);
die();
}
}
/**
* 检查登录
*/
public function checklogin()
{
if(!$this->auth){
$links[] = array(
'text' => '去登录',
'href' => '/user/login?forward='.urlencode($_SERVER['REQUEST_URI']),
);
$this->show_message('你尚未登录,请登录后再进行操作。。。', 0, $links);
}
}
}