-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.php
More file actions
258 lines (224 loc) · 7.28 KB
/
callback.php
File metadata and controls
258 lines (224 loc) · 7.28 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
header('HTTP/1.1 400 Bad Request');
$a = !empty($_REQUEST['a']) ? $_REQUEST['a'] : '';
$origin = '*';
if (!empty($_SERVER['HTTP_ORIGIN'])) {
$origin = trim($_SERVER['HTTP_ORIGIN']);
}
header('Access-Control-Allow-Origin: '.$origin);
header('Access-Control-Allow-Credentials: true');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('HTTP/1.1 200 Options');
header('Access-Control-Allow-Headers: HMAC, *');
die();
}
require_once __DIR__.'/mvid_ai.php';
$access = mvid_check_access($GLOBALS['mv-session-id']);
$start = microtime(true);
$acl = null;
$rv = [
'e' => [],
'a' => $a,
];
while ($a === 'keepalive') {
if (!$GLOBALS['hmac-fresh']) {
$rv['keepalive'] = mvid_keepalive($GLOBALS['mv-session-id']);
if (!$rv['keepalive']) {
$rv['e'][] = 'Could not keepalive session';
break;
}
}
$rv['hmac'] = $GLOBALS['access-hmac'];
$rv['sessionid'] = $GLOBALS['mv-session-id'];
break;
}
while ($a === 'logout') {
setcookie_73('mv-session-id', '', ['expires' => time() + 86400, 'path' => '/', 'secure' => true, 'samesite' => 'None']);
setcookie_73('access-hmac', '', ['expires' => time() + 86400, 'path' => '/', 'secure' => true, 'samesite' => 'None']);
$rv['logout'] = true;
break;
}
while ($a === 'itw-dict') {
if (empty($GLOBALS['mv-session-id'])) {
$rv['e'][] = 'Invalid or empty session ID!';
break;
}
if (empty($_REQUEST['t'])) {
$rv['e'][] = 'Invalid or empty text!';
break;
}
require_once __DIR__.'/itw_proxy.php';
$ret = itw_dict($GLOBALS['mv-session-id'], $_REQUEST['t']);
if ($ret === false) {
$rv['e'][] = 'Invalid or empty result from IntoWords Dictionary!';
break;
}
header('HTTP/1.1 200 Ok');
header('Content-Type: application/json; charset=UTF-8');
echo $ret;
die(); // Not break
}
while ($a === 'itw-speak') {
if (empty($GLOBALS['mv-session-id'])) {
$rv['e'][] = 'Invalid or empty session ID!';
break;
}
if (empty($_REQUEST['t'])) {
$rv['e'][] = 'Invalid or empty text!';
break;
}
require_once __DIR__.'/itw_proxy.php';
$ret = itw_speak($GLOBALS['mv-session-id'], $_REQUEST['t']);
if ($ret === false) {
$rv['e'][] = 'Invalid or empty result from IntoWords TTS!';
break;
}
header('HTTP/1.1 200 Ok');
header('Content-Type: application/json; charset=UTF-8');
echo $ret;
die(); // Not break
}
while ($a === 'grammar' || $a === 'danproof') {
if (empty($_SERVER['HTTP_HMAC'])) {
$rv['e'][] = 'Invalid or empty HMAC header!';
break;
}
if (!$access) {
header('HTTP/1.1 403 Forbidden - check your MV-ID access');
die();
}
if (preg_match_all('~<(STYLE:\w+:\w+)>~u', $_REQUEST['t'], $ms, PREG_SET_ORDER)) {
foreach ($ms as $m) {
// Shuffle whitespace from inside the style to outside the style
$_REQUEST['t'] = preg_replace('~\Q'.$m[0].'\E(\s+)~us', '\1'.$m[0], $_REQUEST['t']);
$_REQUEST['t'] = preg_replace('~(\s+)</\Q'.$m[1].'\E>~us', '</'.$m[1].'>\1', $_REQUEST['t']);
// Remove now-empty styles
$_REQUEST['t'] = preg_replace('~\Q'.$m[0].'\E</\Q'.$m[1].'\E>~us', '', $_REQUEST['t']);
// Remove styles that are fully inside words
$_REQUEST['t'] = preg_replace('~([\pL\pN\pM])\Q'.$m[0].'\E(.+)</\Q'.$m[1].'\E>([\pL\pN\pM])~us', '\1\2\3', $_REQUEST['t']);
}
}
preg_match('~\.(da|nb|sv)\.~', $GLOBALS['mv-ais'], $m);
$svc = 'GRAMMAR_'.strtoupper($m[1]);
$nonce = mt_rand();
$nonced = '';
if (strpos($GLOBALS['mv-ais'], '.da.') !== false) {
$nonced = preg_replace('~<(/?s\d+)>~', '<$1-'.$nonce.'>', $_REQUEST['t']);
}
else {
// Remove s-tags for non-Danish, for now
preg_match('~<s(\d+)>~', $_REQUEST['t'], $m);
$nonce = $m[1];
$nonced = preg_replace('~<(/?s\d+)>~', '', $_REQUEST['t']);
}
for ($try=0 ; $try < 3 ; ++$try) {
$port = $GLOBALS['-config'][$svc.'_PORT'];
header('X-Grammar-Port: '.$port, false);
$s = fsockopen($GLOBALS['-config'][$svc.'_HOST'], $port, $errno, $errstr, 1);
if ($s === false) {
$e = json_encode_num([__LINE__, $errno, $GLOBALS['-config'][$svc.'_HOST'], $port]);
header('X-Grammar-Error: '.$e, false);
continue;
}
header('X-10-Connect: '.(microtime(true) - $start), false);
if (fwrite($s, $nonced."\n<END-OF-INPUT>\n") === false) {
$e = json_encode_num([__LINE__, $GLOBALS['-config'][$svc.'_HOST'], $port]);
header('X-Grammar-Error: '.$e, false);
continue;
}
header('X-20-Write: '.(microtime(true) - $start), false);
$output = stream_get_contents($s);
header('X-30-Read: '.(microtime(true) - $start), false);
$output = trim($output);
if (strpos($GLOBALS['mv-ais'], '.da.') !== false) {
if (!preg_match('~<s\d+-'.$nonce.'>\n~', $output)) {
$output = '';
}
}
if (!empty($output)) {
$rv['c'] = $output;
break;
}
}
if (strpos($GLOBALS['mv-ais'], '.da.') !== false) {
$rv['c'] = preg_replace('~<(/?s\d+)-\d+>~', '<$1>', $rv['c']);
}
else {
// Hack to show alternatives
$rv['c'] = preg_replace('~ <R:([^>]+)>~', ' <R:$1> <AFR:$1>', $rv['c']);
// Add s-tags again
$rv['c'] = "<s$nonce>\n".$rv['c']."\n</s$nonce>";
}
break;
}
while ($a === 'comma') {
if (empty($_SERVER['HTTP_HMAC'])) {
$rv['e'][] = 'Invalid or empty HMAC header!';
break;
}
if (!$access) {
header('HTTP/1.1 403 Forbidden - check your MV-ID access');
die();
}
if (preg_match_all('~<(STYLE:\w+:\w+)>~u', $_REQUEST['t'], $ms, PREG_SET_ORDER)) {
foreach ($ms as $m) {
// Shuffle whitespace from inside the style to outside the style
$_REQUEST['t'] = preg_replace('~\Q'.$m[0].'\E(\s+)~us', '\1'.$m[0], $_REQUEST['t']);
$_REQUEST['t'] = preg_replace('~(\s+)</\Q'.$m[1].'\E>~us', '</'.$m[1].'>\1', $_REQUEST['t']);
// Remove now-empty styles
$_REQUEST['t'] = preg_replace('~\Q'.$m[0].'\E</\Q'.$m[1].'\E>~us', '', $_REQUEST['t']);
// Remove styles that are fully inside words
$_REQUEST['t'] = preg_replace('~([\pL\pN\pM])\Q'.$m[0].'\E(.+)</\Q'.$m[1].'\E>([\pL\pN\pM])~us', '\1\2\3', $_REQUEST['t']);
}
}
$nonce = mt_rand();
$nonced = preg_replace('~<(/?s\d+)>~', '<$1-'.$nonce.'>', $_REQUEST['t']);
for ($try=0 ; $try < 3 ; ++$try) {
$port = $GLOBALS['-config']['COMMA_PORT'];
//header('X-Komma-Port: '.$port, false);
$s = fsockopen($GLOBALS['-config']['COMMA_HOST'], $port, $errno, $errstr, 1);
if ($s === false) {
$e = json_encode_num([__LINE__, $errno, $GLOBALS['-config']['COMMA_HOST'], $port]);
header('X-Komma-Error: '.$e, false);
continue;
}
//header('X-10-Connect: '.(microtime(true) - $start), false);
if (fwrite($s, $nonced."\n<END-OF-INPUT>\n") === false) {
$e = json_encode_num([__LINE__, $GLOBALS['-config']['COMMA_HOST'], $port]);
header('X-Komma-Error: '.$e, false);
continue;
}
//header('X-20-Write: '.(microtime(true) - $start), false);
$output = stream_get_contents($s);
//header('X-30-Read: '.(microtime(true) - $start), false);
$output = trim($output);
if (!preg_match('~<s\d+-'.$nonce.'>\n~', $output)) {
$output = '';
}
if (!empty($output)) {
$rv['c'] = $output;
break;
}
}
$rv['c'] = preg_replace('~<(/?s\d+)-\d+>~', '<$1>', $rv['c']);
break;
}
if (empty($rv['e'])) {
header('HTTP/1.1 200 Ok');
unset($rv['e']);
}
if (!empty($_GET['callback'])) {
header('Content-Type: application/javascript; charset=UTF-8');
echo $_GET['callback'].'('.json_encode_num($rv).');';
}
else {
header('Content-Type: application/json; charset=UTF-8');
echo json_encode_num($rv);
}
flush();
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
if (!empty($rv['e'])) {
exit(0);
}