/
utils.php
103 lines (90 loc) · 2.46 KB
/
utils.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
<?php
/*=======================
* helper functions
=======================*/
function _json_encode($arr){
header("Content-Type: application/json");
echo json_encode($arr);
};
function GetIntParam($val, $arr){
if (( ! array_key_exists($val, $arr))
|| ( ! is_numeric($arr[$val]))
|| (($val = intval($arr[$val])) > 0 && ! is_int($val))
){
return -1;
} else {
return $val;
}
};
function GetStrParam($val, $arr, $min_len = 1, $max_len = 99999, $reg_exp = ""){
if (( ! array_key_exists($val, $arr))
|| (($val = $arr[$val]) == "")
){
return "";
}
$val = strip_tags(strtr($val, array(','=>'',';'=>'','='=>'','*'=>'')));
$len = mb_strlen($val, mb_detect_encoding($val));
if (($len < $min_len)
|| ($len > $max_len)
|| (($reg_exp != "" && preg_match($reg_exp, $val) == 0))
){
return "";
} else {
return $val;
}
};
function dump_exit($var){
var_dump($var);
exit;
}
/*=======================
* page handler
=======================*/
function ShowTemplatePage($page){
global $user,$data; //传递数据对象
$tpl = "tpl\\".$page.".tpl";
include $tpl;
}
function ShowErrorPage($code, $description, $notice){
global $user,$data;
header('HTTP/1.1 '.$code.' '.$description);
header('status: '.$code.' '.$description);
if (isset($notice)){
$data['title'] = $code.' '.$description;
$data['description'] = $notice;
ShowTemplatePage("_error");
}
exit;
}
function Show301(){
ShowErrorPage(301, 'Permanently Moved');
}
function Show302(){
ShowErrorPage(301, 'Temporarily Moved');
}
function Show404(){
ShowErrorPage(404, 'Not Found', "页面当前无法访问");
}
/*=======================
* i18n / translation
=======================*/
function _T($str){
return $str;
}
/*=======================
* Cookie & Session
=======================*/
function GetCookieExpire(){
return time() + (7 * 24 * 60 * 60);
}
/*=======================
* page compress
=======================*/
function compress($buffer) {
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' '), '', $buffer);
$buffer = preg_replace('/ ?([{},:;|=]|==) ?/', '$1', $buffer);
return $buffer;
}
ob_start('compress');
?>