-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.php
executable file
·401 lines (331 loc) · 9.03 KB
/
global.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
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
<?
//error_reporting(E_All);
ini_set('date.timezone','Asia/Shanghai');
//date_default_timezone_set("PRC");
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
function app()
{
return Yii::app();
}
function cs()
{
return Yii::app()->getClientScript();
}
function isGuest()
{
return Yii::app()->user->isGuest;
}
function uid()
{
static $_uid;
if(!isset($uid))
{
$_uid = app()->user->id;
}
if($_uid == null) $_uid = 0;
return $_uid;
}
function user() {
static $_user;
if(isset($_user)) {
return $_user;
} else {
$_user = User::model()->findByPk(uid());
return $_user;
}
}
function usetting() {
static $_usetting;
if(isset($_usetting)) {
return $_usetting;
} else {
$_usetting = UserSetting::model()->findByPk(uid());
return $_usetting;
}
}
function user_setting_init() {
Yii::app()->language = usetting()->lang;
}
function dump($target)
{
return CVarDumper::dump($target, 10, true);
}
function timeFormat($time, $format='full')
{
if ($time == '')
return '';
if ($format == 'apart')
{
$unit = 60;
$p = time() - $time;
if ($p / $unit < 1) return ($p / 1) . '秒前';
$unit*=60;
if ($p / $unit < 1) return intval($p / 60) . '分钟前';
$unit*=24;
if ($p / $unit < 1) return intval($p / 60 / 60) . '小时前';
$unit*=30;
if ($p / $unit < 1) return intval($p / 60 / 60 / 24) . '天前';
//return timeFormat($time, 'full');
$format = 'full';
}
if ($format == 'full') return date('Y-m-d H:i:s', $time);
if ($format == 'date') return date('Y-m-d ', $time);
if ($format == 'month') return date('m-d ', $time);
if ($format == 'path') return date('Y/m', $time);
if ($format == 'condensed') return date('YmdHis', $time);
if ($format == 'chinese') return date("Y 年 m 月 d 日", $time);
if ($format == 'year') return date('Y', $time);
if ($format == 'month-only') return date('m', $time);
if ($format == 'hour') return date('H', $time);
if ($format == 'minute') return date('i', $time);
if ($format == 'rfc') return date('r', $time);
}
function gmtTime($dateStr = "") {
if ( empty($dateStr) )
$time = time()-date('Z');
else {
//mktime(hour,minute,second,month,day,year,is_dst)
// input format : "2012-10-23-11-39" to parse
$times = explode("-", $dateStr);
if (count($times) < 5) {
return time()-date('Z');
} else {
//dump($times);die();
$time = mktime( (int)$times[3], (int)$times[4], 0,
(int)$times[1], (int)$times[2], (int)$times[0]);
$time-= date('Z');
}
}
// bug fix
$time += 28800;
return $time;
}
function sql($sql)
{
$connection = app()->db;
$connection->tablePrefix = 'xyz_';
$command = $connection->createCommand($sql);
return $command;
}
function cache()
{
return app()->cache;
}
function finish($str = '')
{
echo $str;
app()->end();
die();
}
function bu($url = null)
{
static $baseUrl;
if($baseUrl === null)
{
$baseUrl = Yii::app()->getRequest()->getBaseUrl();
}
return $url === null ? $baseUrl : $baseUrl . '/' . ltrim($url, '/');
}
function ctl()
{
return app()->controller;
}
function throwException($code, $error)
{
throw new CHttpException($code, $error);
}
function cookie($key, $value = null)
{
if($value === null)
{
$cookies = app()->request->cookies;
return $cookies[$key]->value;
}
app()->request->cookies->add($key, new CHttpCookie($key, $value));
}
function getBrowserInfo()
{
//header('Content-Type: text/html; charset=utf-8');
$info = $_SERVER["HTTP_USER_AGENT"];
$browser = '';
$core = '';
$hot = '';
//检测内核
if(is_int(strpos($info, 'Trident'))) $core = 'Trident';
if(is_int(strpos($info, 'WebKit'))) $core = 'Webkit';
if(is_int(strpos($info, 'Gecko'))) $core = 'Gecko';
if(is_int(strpos($info, 'Presto'))) $core = 'Presto';
//检测浏览器类型
if(is_int(strpos($info, 'MSIE 6'))) $browser = 'IE6';
else if(is_int(strpos($info, 'MSIE 7'))) $browser = 'IE7';
else if(is_int(strpos($info, 'MSIE 8'))) $browser = 'IE8';
else if(is_int(strpos($info, 'MSIE 9'))) $browser = 'IE9';
else if(is_int(strpos($info, 'MSIE 10'))) $browser = 'IE10';
else if(is_int(strpos($info, 'Firefox'))) $browser = 'Firefox';
else if(is_int(strpos($info, 'Chrome'))) $browser = 'Chrome';
else if(is_int(strpos($info, 'Safari'))) $browser = 'Safari';
else if(is_int(strpos($info, 'Opera'))) $browser = 'Opera';
//检测国内主流浏览器
if(is_int(strpos($info, 'SE')) && is_int(strpos($info, 'MetaSr'))) $hot = '搜狗浏览器';
if(is_int(strpos($info, '360SE'))) $hot = '360安全浏览器';
if(is_int(strpos($info, '360EE'))) $hot = '360急速浏览器';
if(is_int(strpos($info, 'QQBrowser'))) $hot = 'QQ浏览器';
if(is_int(strpos($info, 'Maxthon'))) $hot = '遨游浏览器';
if(is_int(strpos($info, 'TheWorld'))) $hot = '世界之窗浏览器';
$array = array();
$array['agent'] = $info;
$array['core'] = $core;
$array['browser'] = $browser;
$array['hot'] = $hot;
return $array;
}
function getTerminal() {
$info = $_SERVER["HTTP_USER_AGENT"];
$type = '';
if (is_int(strpos($info, 'iPad'))) return "ipad";
if (is_int(strpos($info, 'iPhone'))) return "iphone";
if (is_int(strpos($info, 'Windows Phone'))) return "wphone";
if (is_int(strpos($info, 'Android'))) return "aphone";
return "pc";
}
function v() {
switch ( getTerminal() ) {
case 'pc': $ver = ""; break;
case 'ipad': $ver = ""; break;
case 'iphone':
case 'wphone':
case 'aphone': $ver = "/m"; break;
default: $ver = "";
}
return $ver;
}
function f() {
switch ( getTerminal() ) {
case 'pc': $from = 0; break;
case 'ipad': $from = 10; break;
case 'iphone': $from = 20; break;
case 'wphone': $from = 21; break;
case 'aphone': $from = 22; break;
default: $from = 0;
}
return $from;
}
function d($n) {
switch ( $n ) {
//case 0: $des = "来自PC版"; break;
case 10: $des = "来自iPad"; break;
case 20: $des = "来自iPhone"; break;
case 21: $des = "来自Windows Phone"; break;
case 22: $des = "来自Android"; break;
default: $des = "";
}
return $des;
}
function can($group, $cap) {
if (isGuest())
return 0;
if (user()->role == "administrator")
return 1;
if ( empty($group) || empty($cap) )
return 0;
//return in_array($cap, caps());
$groups = array(
"user" => 0,
'panel' => 1,
'post' => 2,
'contribute' => 3,
'setting' => 4,
);
$caps = caps();
$caps = $caps[$groups[$group]]['list'];
//dump($caps);die();
$caps= explode(",", (string)$caps);
return in_array($cap, $caps);
}
function caps() {
static $_caps;
if (isset($_caps)) {
return $_caps;
} else {
$sql = "select * from {{capability}} "
."where `role`='".user()->role."' order by ID";
$_caps = sql($sql)->queryAll();
//if ( empty($caps) || empty($caps[0]['list']) )
// return 0;
//$caps= explode(",", (string)$caps[0]['list']);
return $_caps;
}
}
function isAdmin() {
if ( isGuest() )
return false;
else
return user()->role == "administrator";
}
function get_option($key) {
$row = sql('select `value` from {{setting}} where `key`="'.$key.'"')
->queryRow();
return $row['value'];
}
function set_option($key, $value) {
sql('update {{setting}} set `value`="'.$value.'"'.
' where `key`="'.$key.'"')->query();
return true;
}
function vividFloor($fid) {
$floors = array (
1 => "沙发", 2 => "板凳", 3 => "地毯", 4 => "地板", 5 => "地下室",
6 => "地基", 7 => "地壳", 8 => "地幔", 9 => "地核", 10 => "地心"
);
if ($fid <= 0) return "";
if ($fid > 0 && $fid <= 10) return $floors[$fid];
return $fid."楼";
}
function postFilter($str) {
$str = str_replace("\n", "</p><p>", $str);
$str = "<p>".$str."</p>";
// wp-content/upload => images
$str = str_replace('wp-content/uploads', 'images', $str);
// voice of xyzpedia
$str = preg_replace("/\<voice\>(.+?)\<\/voice\>/is",'<div id="player_wrap"></div>'.
'<script type="text/javascript">var url="\\1";</script>'.
'<script type="text/javascript" src="/js/jquery.jplayer.js"></script>'.
'<script type="text/javascript" src="/js/jquery.voice.single.js"></script>', $str);
// poll
$str = preg_replace("/\<poll\>(.+?)\<\/poll\>/is",'<iframe id="ifm-poll\\1" frameborder="0" width="555px" src="/poll/display/\\1"></iframe>', $str);
return $str;
}
function commentFilter($str) {
$str = stripslashes($str);
$str = str_replace("\n", '<br>', $str);
$str = preg_replace('~\[(.*?)\]~s', '<img src="/images/face/\\1.gif">', $str);
//$str = preg_replace('~@([\x4e00-\x9fa5]+)\(([\d]+)\)~s', '<a href="/author/id/\\2">\\1</a>', $str);
$str = preg_replace('|@([^\s]+)\(([\d]+)\)|U', '<a href="/author/id/$2">@$1</a>', $str);
$str = preg_replace("#((http|ftp)://[\/\?\&\=\%\+\;\-.\w]+)#i","<a href=\\1>\\1</a>", $str);
return $str;
}
function getIP() {
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
$cip = $_SERVER["HTTP_CLIENT_IP"];
else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if(!empty($_SERVER["REMOTE_ADDR"]))
$cip = $_SERVER["REMOTE_ADDR"];
else
$cip = "";
return $cip;
}
// json return error handle utils
function json_success($msg) {
die(json_encode( array(
'error' => 0,
'msg' => $msg
)));
}
function json_error($msg) {
die(json_encode( array(
'error' => 1,
'msg' => $msg
)));
}
?>