forked from espterm/espterm-front-end
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.php
230 lines (196 loc) · 5.83 KB
/
base.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* The common stuff required by both index.php and build_html.php
* this must be required_once
*/
if (defined('BASE_INITED')) return;
define('BASE_INITED', true);
if (!empty($argv[1])) {
parse_str($argv[1], $_GET);
}
define('GIT_HASH', trim(shell_exec('git rev-parse --short HEAD')));
define('TIMEZONE', trim(shell_exec('date +%Z'))); // for replacements
$prod = defined('STDIN');
define('DEBUG', !$prod);
// Resolve hostname for ajax etc
$root = 'location.host';
if (!file_exists(__DIR__ . '/_env.php')) {
if (DEBUG) {
die("No _env.php found! Copy _env.php.example</b> to <b>_env.php</b> and check the settings inside!");
}
} else {
if (DEBUG) {
require_once __DIR__ . '/_env.php';
$root = json_encode(ESP_IP);
}
}
define('JS_WEB_ROOT', $root);
define('ESP_PROD', (bool)getenv('ESP_PROD'));
define('ESP_DEMO', (bool)getenv('ESP_DEMO'));
if (ESP_DEMO) {
define('DEMO_APS', <<<APS
{
"result": {
"inProgress": 0,
"APs": [
{"essid": "Cisco", "bssid": "88:f7:c7:52:b3:99", "rssi": 205, "rssi_perc": 100, "enc": 4, "channel": 7},
{"essid": "UPC Wi-Free", "bssid": "8a:f7:c7:52:b3:9b", "rssi": 203, "rssi_perc": 100, "enc": 5, "channel": 1},
{"essid": "UPC Wi-Free", "bssid": "0a:95:2a:0c:84:31", "rssi": 166, "rssi_perc": 32, "enc": 5, "channel": 1},
{"essid": "MujO2Internet_2EEB96", "bssid": "d0:60:8c:2e:eb:96", "rssi": 174, "rssi_perc": 48, "enc": 4, "channel": 4},
{"essid": "Internet", "bssid": "38:72:c0:32:bd:0d", "rssi": 164, "rssi_perc": 28, "enc": 2, "channel": 10},
{"essid": "MyO2Internet_08C850", "bssid": "78:c1:a7:08:c8:50", "rssi": 186, "rssi_perc": 72, "enc": 4, "channel": 11},
{"essid": "UPC Wi-Free", "bssid": "06:7c:34:9a:6f:7c", "rssi": 167, "rssi_perc": 34, "enc": 0, "channel": 11},
{"essid": "Internet_B0", "bssid": "5c:f4:ab:11:3b:b3", "rssi": 175, "rssi_perc": 50, "enc": 3, "channel": 13},
{"essid": "UPC5716805", "bssid": "08:95:2a:0c:84:3f", "rssi": 165, "rssi_perc": 30, "enc": 4, "channel": 1}
]
}
}
APS
);
}
define('LOCALE', isset($_GET['locale']) ? $_GET['locale'] : (getenv('ESP_LANG') ?: 'en'));
$_messages = require(__DIR__ . '/lang/' . LOCALE . '.php');
$_messages_fallback = require(__DIR__ . '/lang/en.php');
$_messages_common = require(__DIR__ . '/lang/common.php');
$_pages = require(__DIR__ . '/_pages.php');
define('APP_NAME', 'ESPTerm');
/** URL (dev or production) */
function url($name, $relative = false)
{
global $_pages;
if ($relative) return $_pages[$name]->path;
if (DEBUG) return "/index.php?page=$name";
if (ESP_DEMO) return "$name.html";
else return $_pages[$name]->path;
}
/** URL label for a button */
function label($name)
{
global $_pages;
return $_pages[$name]->label;
}
function e($s)
{
return htmlspecialchars($s, ENT_HTML5 | ENT_QUOTES);
}
function je($s)
{
return htmlspecialchars(json_encode($s), ENT_HTML5);
}
function tr($key)
{
global $_messages, $_messages_fallback, $_messages_common;
if (isset($_messages[$key])) {
$str = $_messages[$key];
}
else if (isset($_messages_fallback[$key])) {
$str = $_messages_fallback[$key];
}
else if (isset($_messages_common[$key])) {
$str = $_messages_common[$key];
}
else{
ob_end_clean();
die('??' . $key . '??');
}
// allow tildes in translation
$str = preg_replace('/(?<=[^ \\\\])~(?=[^ ])/', ' ', $str);
$str = str_replace('\~', '~', $str);
return $str;
}
/** Like eval, but allows <?php and ?> */
function include_str($code)
{
$tmp = tmpfile();
$tmpf = stream_get_meta_data($tmp);
$tmpf = $tmpf ['uri'];
fwrite($tmp, $code);
$ret = include($tmpf);
fclose($tmp);
return $ret;
}
if (!function_exists('utf8')) {
function utf8($num)
{
if($num<=0x7F) return chr($num);
if($num<=0x7FF) return chr(($num>>6)+192).chr(($num&63)+128);
if($num<=0xFFFF) return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
if($num<=0x1FFFFF) return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128);
return '';
}
}
if (!function_exists('load_esp_charsets')) {
function load_esp_charsets() {
$chsf = __DIR__ . '/../user/character_sets.h';
if (! file_exists($chsf)) {
return [
'!! ERROR: `../user/character_sets.h` not found !!' => [
'start' => 65,
'chars' => ['&'],
],
];
}
$re_table = '/\/\/ %%BEGIN:(.)%%\s*(.*?)\s*\/\/ %%END:\1%%/s';
preg_match_all($re_table, file_get_contents($chsf), $m_tbl);
$re_bounds = '/#define CODEPAGE_(.)_BEGIN\s+(\d+)\n#define CODEPAGE_\1_END\s+(\d+)/';
preg_match_all($re_bounds, file_get_contents($chsf), $m_bounds);
$cps = [];
foreach ($m_tbl[2] as $i => $str) {
$name = $m_tbl[1][$i];
$start = intval($m_bounds[2][$i]);
$table = [];
$str = preg_replace('/,\s*\/\/[^\n]*/', '', $str);
$rows = explode("\n", $str);
$rows = array_map('trim', $rows);
foreach($rows as $j => $v) {
$literal = false;
if (strpos($v, '0x') === 0) {
// hexa codes
$v = substr($v, 2);
$v = hexdec($v);
} else if (strpos($v, 'u\'\\0\'') === 0) {
// zero
$v = 0;
} else if (strpos($v, 'u\'') === 0) {
// utf8 literals
$v = mb_substr($v, 2, 1, 'utf-8');
$literal = true;
} else {
$v = intval($v);
}
$ascii = $start+$j;
$table[] = $literal ? $v : utf8($v==0? $ascii :$v);
}
$obj = [
'start' => $start,
'chars' => $table,
];
$cps[$name] = $obj;
}
return $cps;
}
}
if (!function_exists('tplSubs')) {
function tplSubs($str, $reps)
{
return preg_replace_callback('/%(j:|js:|h:|html:)?([a-z0-9-_.]+)%/i', function ($m) use ($reps) {
$key = $m[2];
if (array_key_exists($key, $reps)) {
$val = $reps[$key];
} else {
$val = '';
}
switch ($m[1]) {
case 'j:':
case 'js:':
$v = json_encode($val);
return substr($v, 1, strlen($v) - 2);
case 'h:':
case 'html:':
return htmlspecialchars($val);
default:
return $val;
}
}, $str);
}
}