-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitw_proxy.php
146 lines (127 loc) · 3.47 KB
/
itw_proxy.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
<?php
require_once __DIR__.'/mvid_ai.php';
$GLOBALS['itw-dicts'] = [
'da' => 1, // Den Danske Ordbog (DDO)
'no' => 2, // Norsk bokmål
'nb' => 2, // Norsk bokmål
'nn' => 7, // Nynorsk
'sv' => 8, // Natur och Kulturs Stora Svenska Ordbok (NoK)
];
function itw_curl($url, $rq) {
$rq = json_encode_num($rq);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: '.strlen($rq),
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rq);
$ret = curl_exec($ch);
if ($ret === false) {
$err = fopen('php://stderr', 'wb');
fprintf($err, 'CURL Error: %s', curl_error($ch));
return false;
}
$e = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($e < 200 || $e >= 400) {
header('X-ITW-Curl: '.$e);
return false;
}
return $ret;
}
function itw_dict($sessionid, $text) {
$dict = 1;
$locale = mvid_locale();
if (!empty($locale) && !empty($GLOBALS['itw-dicts'][$locale])) {
$dict = $GLOBALS['itw-dicts'][$locale];
}
$rq = [
'type' => 'jsonwsp/request',
'version' => '1.0',
'methodname' => 'search',
'args' => [
'session_id' => $sessionid,
'searchString' => trim($text),
'dictID' => $dict,
],
];
$rv = itw_curl('https://'.$GLOBALS['-config']['MV_IW_DICT_HOST'].'/dictservice/DictionaryService/jsonwsp', $rq);
if ($rv === false) {
header('X-ITW-Dict: CURL error');
return false;
}
$rv = json_decode($rv, true);
if (empty($rv['result']['value'][0]['Key'])) {
if (preg_match('~^(.+?)[\pP\pS\W]+$~u', $text, $m)) {
return itw_dict($sessionid, $m[1]);
}
header('X-ITW-Dict: Empty result');
return false;
}
$rq = [
'type' => 'jsonwsp/request',
'version' => '1.0',
'methodname' => 'getArticle',
'args' => [
'session_id' => $sessionid,
'dictID' => $dict,
'word' => trim($rv['result']['value'][0]['Word']),
'key' => trim($rv['result']['value'][0]['Key']),
'settings' => null,
],
];
return itw_curl('https://'.$GLOBALS['-config']['MV_IW_DICT_HOST'].'/dictservice/DictionaryService/jsonwsp', $rq);
}
function itw_get_dicts($sessionid) {
$rq = [
'type' => 'jsonwsp/request',
'version' => '1.0',
'methodname' => 'getDictionaries',
'args' => [
'session_id' => $sessionid,
],
];
$rv = itw_curl('https://'.$GLOBALS['-config']['MV_IW_DICT_HOST'].'/dictservice/DictionaryService/jsonwsp', $rq);
if ($rv === false) {
header('X-ITW-GetDicts: CURL error');
return false;
}
$rv = json_decode($rv, true);
if (empty($rv['result']['value'])) {
header('X-ITW-GetDicts: Empty result');
return false;
}
return $rv['result']['value'];
}
function itw_search_dict($sessionid, $text, $dict=1) {
$rq = [
'type' => 'jsonwsp/request',
'version' => '1.0',
'methodname' => 'search',
'args' => [
'session_id' => $sessionid,
'searchString' => $text,
'dictID' => $dict,
],
];
return itw_curl('https://'.$GLOBALS['-config']['MV_IW_DICT_HOST'].'/dictservice/DictionaryService/jsonwsp', $rq);
}
function itw_speak($sessionid, $text) {
$rq = [
'type' => 'jsonwsp/request',
'version' => '1.0',
'methodname' => 'speak',
'args' => [
'session_id' => $sessionid,
'text' => trim($text),
'return_indices' => false,
'type' => 'mp3',
],
];
return itw_curl('https://'.$GLOBALS['-config']['MV_IW_ONLINE_HOST'].'/intowords-v3/tts/jsonwsp', $rq);
}