-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.php
More file actions
119 lines (102 loc) · 3.82 KB
/
Copy pathconfig.php
File metadata and controls
119 lines (102 loc) · 3.82 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
<?php
// 减肥记录管理系统 - 配置文件
define('DB_FILE', __DIR__ . '/database/weight_tracker.db');
define('APP_NAME', '减肥记录管理系统');
define('APP_VERSION', '1.1.0');
define('CACHE_DIR', __DIR__ . '/cache');
define('CACHE_DEFAULT_TTL', 300);
define('CSRF_TOKEN_EXPIRE', 3600);
if (!is_dir(CACHE_DIR)) {
@mkdir(CACHE_DIR, 0755, true);
}
if (isset($_SERVER['REMOTE_ADDR']) && ($_SERVER['REMOTE_ADDR'] === '127.0.0.1' || $_SERVER['REMOTE_ADDR'] === '::1')) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error.log');
} else {
error_reporting(0);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error.log');
}
set_error_handler(function($errno, $errstr, $errfile, $errline) {
if (!(error_reporting() & $errno)) {
return false;
}
$errorTypes = [
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parse',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Strict',
E_RECOVERABLE_ERROR => 'Recoverable Error',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User Deprecated'
];
$type = $errorTypes[$errno] ?? 'Unknown Error';
$logMessage = date('Y-m-d H:i:s') . " [$type] $errstr in $errfile on line $errline";
error_log($logMessage);
return true;
});
set_exception_handler(function($exception) {
$message = $exception->getMessage();
$file = $exception->getFile();
$line = $exception->getLine();
$trace = $exception->getTraceAsString();
$logMessage = date('Y-m-d H:i:s') . " [Exception] $message in $file:$line\nStack trace:\n$trace";
error_log($logMessage);
if (!headers_sent()) {
header('Content-Type: application/json; charset=utf-8');
http_response_code(500);
echo json_encode([
'success' => false,
'message' => '系统内部错误,请稍后重试'
], JSON_UNESCAPED_UNICODE);
}
});
register_shutdown_function(function() {
$error = error_get_last();
if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
$logMessage = date('Y-m-d H:i:s') . " [Fatal] {$error['message']} in {$error['file']} on line {$error['line']}";
error_log($logMessage);
}
});
function isMobileDevice() {
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
$mobileKeywords = [
'mobile', 'android', 'iphone', 'ipad', 'ipod',
'blackberry', 'windows phone', 'opera mini',
'palm', 'smartphone', 'iemobile', 'kindle',
'tablet', 'touch', 'phone'
];
foreach ($mobileKeywords as $keyword) {
if (strpos($userAgent, $keyword) !== false) {
return true;
}
}
return false;
}
function isTabletDevice() {
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
$tabletKeywords = ['ipad', 'tablet', 'android', 'kindle'];
foreach ($tabletKeywords as $keyword) {
if (strpos($userAgent, $keyword) !== false && strpos($userAgent, 'mobile') === false) {
return true;
}
}
return false;
}
function zhWeekday($date) {
$w = date('w', strtotime($date));
$map = ['周日','周一','周二','周三','周四','周五','周六'];
return $map[$w];
}
?>