-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.php
executable file
·154 lines (135 loc) · 4.21 KB
/
func.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
<?php
// For Debug
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
# error_reporting(E_ALL);
$resumate_version = "0.0.3";
$currentLang = currentLang();
function dieVars($confirmed, $success, $message, $extra=null){
$returnData = array("confirmed" => $confirmed, 'success' => $success, 'confirmString' => $message, 'data' => $extra);
dieJSON($returnData);
}
function dieJSON($array){
$echoJSON = json_encode($array);
if ($echoJSON != NULL){
die($echoJSON);
}else{
die(json_encode(array('confirmed' => false, 'success' => false, 'confirmString' => 'dieJSON()传入参数错误!')));
}
}
function loadfile($dirlist, $filename, &$defFromDir){
foreach ($dirlist as $dir){
$fullname = $dir . $filename;
if (file_exists($fullname) && ($fp = fopen($fullname, "r")) !== false){
$payload = file_get_contents($fullname);
fclose($fp);
$defFromDir = $dir;
return $payload;
}
}
return false;
}
function isAssoc(array $arr){
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
function checkAbsPath($path){
if (substr($path, 0, 2) == "//" || substr($path, 0, 7) == "http://" || substr($path, 0, 8) == "https://"){
return true;
}
return false;
}
function loadSettings($dir='./'){
if (!array_key_exists('resumate_settings', $GLOBALS)){
$data = loadfile([$dir], "settings.json", $dir);
if ($data === false){
return false;
}
$GLOBALS['resumate_settings'] = json_decode($data, true);
}
return $GLOBALS['resumate_settings'];
}
function getSetting($key){
$settings = loadSettings();
return $settings[$key];
}
function getLocalizedSetting($key){
return getLocalizedString(getSetting($key));
}
function getLangFile($lang){
$langFile = getSetting('lang')[$lang]['file'];
$lang = json_decode(file_get_contents('./definitions/lang/' . $langFile), true);
if ($lang['type'] == ''){
die("File $langFile error: Unable to parse JSON, or failed to open file.");
}
if (!array_key_exists('vars', $lang)){
$lang['vars'] = [];
}
return $lang['vars'];
}
function check_var($input){
if (gettype($input) != gettype('')) return false;
if (strlen($input) > 1 && $input[0] == '$' && $input[-1] == '$'){
return substr($input, 1, -1);
}
return false;
}
function getLocalizedString($str){
if (($keyname = check_var($str)) !== false){
$langVars = getLangFile(currentLang());
if (array_key_exists($keyname, $langVars)){
return $langVars[$keyname];
}else{
return $str;
}
}else{
return $str;
}
}
function getLangList(){
return array_keys(getSetting('lang'));
}
function getCookieTimeYears($year){
return time()+60*60*24*365*$year;
}
function getLongCookieTime(){
return getCookieTimeYears(100);
}
function currentLang(){
$defaultLang = getSetting('defaultlang');
$allLang = getLangList();
$cookieLife = getLongCookieTime();
if (!array_key_exists('lang', $_COOKIE)){
$acceptLang = explode(',', str_replace(' ', '', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
foreach ($acceptLang as $entry){
$tempLang = explode(';', $entry)[0];
foreach($allLang as $curLang){
if ($tempLang == $curLang){
setcookie("lang", $tempLang, $cookieLife);
return $tempLang;
}
}
//fuzzy search
//try the main language
$mainLang = explode('-', $tempLang)[0];
foreach($allLang as $curLang){
$allMainLang = explode('-', $curLang)[0];
if ($mainLang == $allMainLang){
setcookie("lang", $mainLang, $cookieLife);
return $mainLang;
}
}
}
setcookie("lang", $defaultLang, $cookieLife);
return $defaultLang;
}else{
$gotcookie = $_COOKIE['lang'];
if (in_array($gotcookie, $allLang)){
return $gotcookie;
}else{
setcookie("lang", $defaultLang, $cookieLife);
return $defaultLang;
}
}
}
?>