-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.php
210 lines (192 loc) · 6.27 KB
/
app.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
<?php
include('lib/log4php/Logger.php');
Logger::configure('config/log4php-config.xml');
include_once dirname(__FILE__) . '/config.php';
/**
* error handle
* database resource.
*/
ini_set('display_errors', 'On');
//ini_set('display_errors', 'Off');
error_reporting(APP_REEOR_REPORTING_LEVEL === 'ERROR' ? E_ERROR : (APP_REEOR_REPORTING_LEVEL === '0' ? 0 : E_ALL));
if (isset($_COOKIE['PHPSESSID']) && !preg_match('/^[a-zA-Z0-9,-]{1,128}$/', $_COOKIE['PHPSESSID'])) die('SESSID错误');
session_start();
/**
*
*/
function isTraceError()
{
return defined('TMS_APP_EXCEPTION_TRACE') && TMS_APP_EXCEPTION_TRACE === 'Y';
}
/**
*
*/
function throwableToStr($excep)
{
$modelLog = TMS_APP::M('log');
$str = $excep->getMessage();
$trace = $excep->getTrace();
foreach ($trace as $t) {
$str .= '<br>';
foreach ($t as $k => $v) {
if (!in_array($k, ['file', 'line'])) {
continue;
}
$str .= $modelLog->toJson($v) . ' ';
}
}
return $str;
}
/***************************
* error and exception handle
***************************/
/**
* file
* line
* function
* args
* type
* class
*/
function show_error($message)
{
require_once 'tms/tms_app.php';
$logger = Logger::getRootLogger();
$method = isset($_SERVER['REQUEST_URI']) ? tms_get_server('REQUEST_URI') : 'unknown request';
$modelLog = TMS_APP::M('log');
$status = 500;
$text = ' Internal Server Error';
if ($message instanceof UrlNotMatchException || $message instanceof SiteUserException) {
/* 已知异常信息 */
$msg = $message->getMessage();
$status = 406; // Not Acceptable
$text = 'Not Acceptable';
} else if ($message instanceof Throwable) {
/* 未知异常 */
$excep = throwableToStr($message);
$logger->error(str_replace('<br>', PHP_EOL, $excep));
$msg = isTraceError() ? $excep : '应用程序内部异常错误';
} else if (is_string($message)) {
/* 文本错误信息 */
$logger->error($message);
$msg = $message;
} else {
/* 其它类型的数据 */
$othermsg = json_encode($message);
$logger->error($othermsg);
$msg = isTraceError() ? $othermsg : '应用程序内部错误';
}
/* 给前端返回信息 */
header("HTTP/1.1 $status $text");
header('Content-Type: text/plain; charset=utf-8');
echo $msg;
/* 数据库中记录日志 */
if (isset($excep)) {
$msg = str_replace('<br>', "\n", $excep);
}
$msg = $modelLog->escape($msg);
/* 错误信息报错的到数据库 */
if ($message instanceof SiteUserException) {
$modelLog->log($message->getUserid(), $method, $msg, '', '', $message->getDebugInfo());
} else {
$modelLog->log('error', $method, $msg);
}
exit;
}
function tms_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
{
switch ($errno) {
case E_ERROR:
show_error(new ErrorException('ERROR:' . $errstr, 0, $errno, $errfile, $errline));
case E_WARNING:
if (strpos($errstr, 'exif_read_data') === false) {
show_error(new ErrorException('E_WARNING:' . $errstr, 0, $errno, $errfile, $errline));
}
break;
case E_PARSE:
show_error(new ErrorException('E_PARSE:' . $errstr, 0, $errno, $errfile, $errline));
case E_NOTICE:
show_error(new ErrorException('E_NOTICE:' . $errstr, 0, $errno, $errfile, $errline));
case E_CORE_ERROR:
show_error(new ErrorException('E_CORE_ERROR:' . $errstr, 0, $errno, $errfile, $errline));
case E_CORE_WARNING:
show_error(new ErrorException('E_CORE_WARNING:' . $errstr, 0, $errno, $errfile, $errline));
case E_COMPILE_ERROR:
show_error(new ErrorException('E_COMPILE_ERROR:' . $errstr, 0, $errno, $errfile, $errline));
case E_COMPILE_WARNING:
show_error(new ErrorException('E_COMPILE_WARNING:' . $errstr, 0, $errno, $errfile, $errline));
case E_USER_ERROR:
show_error(new ErrorException('E_USER_ERROR:' . $errstr, 0, $errno, $errfile, $errline));
case E_USER_WARNING:
show_error(new ErrorException('E_USER_WARNING:' . $errstr, 0, $errno, $errfile, $errline));
case E_USER_NOTICE:
show_error(new ErrorException('E_USER_NOTICE:' . $errstr, 0, $errno, $errfile, $errline));
case E_STRICT:
show_error(new ErrorException('E_STRICT:' . $errstr, 0, $errno, $errfile, $errline));
case E_RECOVERABLE_ERROR:
show_error(new ErrorException('E_RECOVERABLE_ERROR:' . $errstr, 0, $errno, $errfile, $errline));
case E_DEPRECATED:
show_error(new ErrorException('E_DEPRECATED:' . $errstr, 0, $errno, $errfile, $errline));
case E_USER_DEPRECATED:
show_error(new ErrorException('E_USER_DEPRECATED:' . $errstr, 0, $errno, $errfile, $errline));
}
}
set_error_handler('tms_error_handler');
function tms_exception_handler($exception)
{
show_error($exception);
}
set_exception_handler('tms_exception_handler');
/**
* Given a file, i.e. /css/base.css, replaces it with a string containing the
* file's mtime, i.e. /css/base.1221534296.css.
*
* @param $file The file to be loaded. Must be an absolute path (i.e.
* starting with slash).
*/
function auto_version($file)
{
if (strpos($file, DIRECTORY_SEPARATOR) !== 0 || !file_exists(TMS_APP_DIR . $file)) {
return $file;
}
$mtime = filemtime(TMS_APP_DIR . $file);
$file .= '?_=' . $mtime;
return $file;
}
/**
* 获得文件的定制版本
*/
function custom_version($file)
{
if (0 !== strpos($file, DIRECTORY_SEPARATOR)) {
$file = DIRECTORY_SEPARATOR . $file;
}
$full = '/views/' . TMS_APP_VIEW_NAME . $file;
if (!file_exists(TMS_APP_DIR . $full)) {
$full = '/views/' . TMS_APP_VIEW_NAME_DEFAULT . $file;
}
$full = auto_version($full);
$full = TMS_APP_URI . $full;
return $full;
}
/**
* 设置默认数学计算精度
*/
if (defined('APP_TMS_BCSCALE')) {
bcscale(APP_TMS_BCSCALE);
}
$logger = Logger::getRootLogger();
/*************************
* run application.
*************************/
require_once 'tms/tms_app.php';
if (isset($_GET['TMSDEV']) && $_GET['TMSDEV'] === 'yes') {
$devLogger = Logger::getLogger('dev');
$devLogger->debug("进入应用 [REQUEST_URI = " . $_SERVER['REQUEST_URI'] . ']');
}
# 解决同源限制问题
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
header('Access-Control-Allow-Origin:' . $origin);
header('Access-Control-Allow-Credentials:true');
$config = array();
TMS_APP::run($config);