-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
executable file
·128 lines (126 loc) · 4.6 KB
/
index.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
<?php
namespace {
function language(): array {
$languages = [];
foreach (\explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? "") as $v) {
if (false === \strpos($v = \trim($v), ';')) {
$languages[$v] = 1.0;
continue;
}
$any = \explode(';', $v);
$language = \trim(\array_shift($any));
$order = 1.0;
foreach ($any as $vv) {
if (0 === \strpos($vv = \trim($vv), 'q=')) {
$order = (float) \substr($vv, 2);
break;
}
}
$languages[$language] = $order;
}
\arsort($languages);
return $languages;
}
}
namespace x\language {
function of(array $names) {
$files = [];
$lot = (array) \lot('I');
foreach ($names as $k => $v) {
if (!\is_file($v = __DIR__ . \D . 'state' . \D . $v . '.php')) {
unset($names[$k]);
continue;
}
// Queue language item(s) from this extension
$files[] = $v;
// Queue language item(s) from other extension(s) and layout(s)
foreach (\glob(\LOT . \D . '{x,y}' . \D . '*' . \D . 'language' . \D . $v . '.php', \GLOB_BRACE | \GLOB_NOSORT) as $vv) {
if (!\is_file(\dirname($vv, 2) . \D . 'index.php')) {
continue;
}
$files[] = $vv;
}
}
if (!$files) {
return $lot;
}
// Load and merge language item(s) in queue
foreach ($files as $file) {
$lot = \array_replace($lot, \fire(function () {
\extract(\lot());
return (array) require $this->path;
}, [], (object) ['path' => $file]));
// Automatic lower-case mode
foreach ($lot as $k => $v) {
$kk = \strtolower($k);
if ($k === $kk) {
continue;
}
$lot[$kk] = \l($v);
}
}
return [$names, $lot];
}
function route($content, $path) {
if (null !== $content) {
return $content;
}
if ($path !== '/language.json') {
return $content;
}
if ('POST' === $_SERVER['REQUEST_METHOD']) {
// TODO
return $content;
}
\extract(\lot(), \EXTR_SKIP);
$fire = $_GET['fire'] ?? null;
$languages = \array_merge(\explode(',', (string) ($_GET['name'] ?? $state->language ?? 'en')), \array_keys(\language()));
[$names, $lot] = of($languages = \array_unique($languages));
$r = [
'alert' => [],
'count' => 0,
'description' => $lot['.description'] ?? null,
'lot' => [],
'name' => $name = \end($names) ?: null, // The last name
'names' => $names,
'status' => 404,
'title' => $lot['.title'] ?? $name
];
unset($lot['.description'], $lot['.title']);
if ($lot) {
$r['alert']['success'][] = \i('Success.');
$r['count'] = \count($lot);
$r['lot'] = $lot;
$r['status'] = 200;
// Validate function name
if ($fire && !\preg_match('/^[a-z_$][\w$]*(\.[a-z_$][\w$]*)*$/i', $fire)) {
$fire = false;
$r['alert']['error'][] = \i('Invalid function name: %s', '`' . $fire . '`');
$r['lot'] = [];
$r['status'] = 403;
}
} else {
$r['alert']['error'][] = \i('Error.');
}
$age = 60 * 60 * 24; // Cache for a day
$content = \To::JSON($r);
\status($r['status'], $lot ? [
'cache-control' => 'max-age=' . $age . ', private',
'content-type' => 'application/' . ($fire ? 'javascript' : 'json') . '; charset=utf-8',
'expires' => \gmdate('D, d M Y H:i:s', $age + $_SERVER['REQUEST_TIME']) . ' GMT',
'pragma' => 'private'
] : [
'cache-control' => 'max-age=0, must-revalidate, no-cache, no-store',
'content-type' => 'application/' . ($fire ? 'javascript' : 'json') . '; charset=utf-8',
'expires' => '0',
'pragma' => 'no-cache'
]);
return ($fire ? $fire . '(' : "") . $content . ($fire ? ');' : "");
}
function set() {
\extract(\lot());
\lot('I', of(\explode(',', (string) $state->language ?? \P))[1]);
}
\Hook::set('route', __NAMESPACE__ . "\\route", 1);
\Hook::set('set', __NAMESPACE__ . "\\set", 1);
}