-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKBBIModel.php
More file actions
351 lines (295 loc) · 13.1 KB
/
KBBIModel.php
File metadata and controls
351 lines (295 loc) · 13.1 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
namespace App\Models;
use CodeIgniter\Model;
use DOMDocument;
use DOMXPath;
use Exception;
class KBBIModel extends Model
{
private function _user_agent()
{
$userAgents = [
// Chrome (Desktop)
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
// Chrome (Mobile)
"Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1",
// Firefox (Desktop)
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:114.0) Gecko/20100101 Firefox/114.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:114.0) Gecko/20100101 Firefox/114.0",
"Mozilla/5.0 (X11; Linux x86_64; rv:114.0) Gecko/20100101 Firefox/114.0",
// Firefox (Mobile)
"Mozilla/5.0 (Android 10; Mobile; rv:114.0) Gecko/114.0 Firefox/114.0",
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) FxiOS/114.0 Mobile/15E148 Safari/604.1",
// Edge (Desktop)
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.0.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.0.0",
// Safari (Desktop)
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15",
// Safari (Mobile)
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (iPad; CPU OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1",
// Opera (Desktop)
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 OPR/100.0.0.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 OPR/100.0.0.0",
// Opera (Mobile)
"Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36 OPR/74.0.0.0",
// Samsung Internet
"Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36 SamsungBrowser/18.0",
// Internet Explorer
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; rv:11.0) like Gecko",
// UC Browser (Mobile)
"Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.0.0 Mobile Safari/537.36 UCBrowser/13.4.0.1306",
// Brave (Desktop)
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Brave/114.0.0.0",
// New User Agents Added
"Mozilla/5.0 (Linux; Android 13; SM-S911B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Mobile Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"Mozilla/5.0 (iPad; CPU OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/605.1.15",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"Mozilla/5.0 (Android 11; Mobile; rv:117.0) Gecko/117.0 Firefox/117.0",
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1",
];
$userAgent = $userAgents[array_rand($userAgents)];
return $userAgent;
}
private function _fetchHtml($word)
{
$kbbiHost = 'kbbi.kemendikdasmen.go.id';
$kbbiBaseUrl = 'https://' . $kbbiHost;
$userAgent = $this->_user_agent();
$headers = [
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding: gzip, deflate",
"Accept-Language: en-US,en;q=0.5",
"Connection: keep-alive",
"Host: " . $kbbiHost,
"Referer: " . $kbbiBaseUrl . "/",
"Upgrade-Insecure-Requests: 1",
];
// Path ke file cookie dalam folder writable
$cookieDir = WRITEPATH . 'cookies/';
if (!is_dir($cookieDir)) {
mkdir($cookieDir, 0777, true);
}
$cookieFile = $cookieDir . 'kbbi-kemendikdasmen-goid.txt';
$encodedWord = rawurlencode($word);
$url = $kbbiBaseUrl . "/entri/" . $encodedWord;
$ch = curl_init($url);
$responseHeaders = [];
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $headerLine) use (&$responseHeaders) {
$len = strlen($headerLine);
$line = trim($headerLine);
if ($line === '' || !str_contains($line, ':')) {
return $len;
}
[$name, $value] = explode(':', $line, 2);
$name = strtolower(trim($name));
$value = trim($value);
if ($name !== '') {
if (!array_key_exists($name, $responseHeaders)) {
$responseHeaders[$name] = $value;
} else {
$responseHeaders[$name] .= ', ' . $value;
}
}
return $len;
});
// Gunakan file cookie untuk menyimpan sesi
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
$effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
unset($ch);
throw new Exception('cURL Error: ' . $error_msg . ($effectiveUrl ? ' (url: ' . $effectiveUrl . ')' : ''));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
unset($ch);
$isCloudflare =
(isset($responseHeaders['server']) && stripos($responseHeaders['server'], 'cloudflare') !== false) ||
isset($responseHeaders['cf-ray']) ||
isset($responseHeaders['cf-mitigated']) ||
isset($responseHeaders['cf-cache-status']);
if ($httpCode === 200 && $isCloudflare && is_string($response)) {
$challengeMarkers = [
'cf-chl',
'__cf_chl',
'cf-browser-verification',
'Just a moment',
'Checking your browser',
'Attention Required',
];
foreach ($challengeMarkers as $marker) {
if (stripos($response, $marker) !== false) {
$ray = $responseHeaders['cf-ray'] ?? null;
throw new Exception('Cloudflare challenge page detected' . ($ray ? ' (cf-ray: ' . $ray . ')' : '') . ($effectiveUrl ? ' (url: ' . $effectiveUrl . ')' : ''));
}
}
}
if ($httpCode !== 200) {
if ($isCloudflare && in_array($httpCode, [403, 429, 503], true)) {
$ray = $responseHeaders['cf-ray'] ?? null;
throw new Exception('Blocked by Cloudflare (HTTP status code: ' . $httpCode . ')' . ($ray ? ' (cf-ray: ' . $ray . ')' : '') . ($effectiveUrl ? ' (url: ' . $effectiveUrl . ')' : ''));
}
throw new Exception('Failed to fetch HTML, HTTP status code: ' . $httpCode . ($effectiveUrl ? ' (url: ' . $effectiveUrl . ')' : ''));
}
return $response;
}
private function _cleanText($text)
{
return preg_replace('/\s+/', ' ', trim($text));
}
private function _cleanWord($word)
{
// Remove non-alphanumeric characters except spaces
$cleanWord = preg_replace('/[^a-zA-Z0-9\s]/', '', $word);
// Replace multiple spaces with a single space
return preg_replace('/\s+/', ' ', strtolower(trim($cleanWord)));
}
private function _parserV2($htmlData, $word)
{
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($htmlData);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
$dataResponse = [];
$contentDiv = $xpath->query("//div[contains(@class, 'container body-content')]")->item(0);
if (!$contentDiv) {
return false;
}
// Mengambil semua elemen h2 dalam div body-content
$h2Elements = $xpath->query(".//h2[contains(@style, 'margin-bottom:3px')]", $contentDiv);
foreach ($h2Elements as $i => $h2Element) {
// Mengambil lema dari link a di dalam span rootword
$lemaLink = $xpath->query(".//span[contains(@class, 'rootword')]/a", $h2Element)->item(0);
$lema = '';
if ($lemaLink) {
$lema = $this->_cleanText($lemaLink->nodeValue);
}
// Mengambil link Tesaurus
$tesaurusLink = '';
$tesaurusAnchor = $xpath->query(".//p/a[contains(@href, 'tematis/lema')]", $h2Element)->item(0);
if ($tesaurusAnchor) {
$tesaurusLink = $tesaurusAnchor instanceof \DOMElement ? $tesaurusAnchor->getAttribute('href') : '';
} else {
$tesaurusLink = "http://tesaurus.kemendikdasmen.go.id/tematis/lema/" . $word;
}
// Mengambil deskripsi/arti dari ul/li setelah h2
$ulElement = $xpath->query("following-sibling::ul[@class='adjusted-par'][1]", $h2Element)->item(0);
$arti = [];
if ($ulElement) {
$listItems = $xpath->query(".//li", $ulElement);
foreach ($listItems as $j => $listItem) {
$deskripsi = $this->_cleanText($listItem->nodeValue);
$arti[] = ['deskripsi' => $deskripsi];
}
}
// Menyimpan data dalam $dataResponse
if (!empty($lema) && !empty($arti)) {
$dataResponse[] = [
'word' => $word,
'lema' => $lema . " » " . $word,
'arti' => $arti,
'tesaurusLink' => $tesaurusLink,
];
}
}
return count($dataResponse) ? $dataResponse : [];
}
private function _parserV3($htmlData, $word)
{
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($htmlData);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
$dataResponse = [];
// Mengambil semua elemen h2 yang memiliki style 'margin-bottom:3px'
$h2Elements = $xpath->query("//h2[contains(@style, 'margin-bottom:3px')]");
foreach ($h2Elements as $h2Element) {
// Mengambil teks dari elemen h2
$lema = $this->_cleanText($h2Element->textContent);
// Mengambil link Tesaurus dari elemen <p><a>
$tesaurusLink = '';
$tesaurusAnchor = $xpath->query("following-sibling::p[1]/a[contains(@href, 'tematis/lema')]", $h2Element)->item(0);
if ($tesaurusAnchor) {
$tesaurusLink = $tesaurusAnchor instanceof \DOMElement ? $tesaurusAnchor->getAttribute('href') : '';
} else {
$tesaurusLink = "http://tesaurus.kemendikdasmen.go.id/tematis/lema/" . $lema;
}
// Mengambil deskripsi/arti dari ol/li setelah h2
$arti = [];
$olElement = $xpath->query("following-sibling::ol[1]", $h2Element)->item(0);
if ($olElement) {
$listItems = $xpath->query(".//li", $olElement);
foreach ($listItems as $listItem) {
$deskripsi = $this->_cleanText($listItem->nodeValue);
$arti[] = ['deskripsi' => $deskripsi];
}
}
// Mengambil deskripsi/arti dari ul/li setelah h2
$ulElement = $xpath->query("following-sibling::ul[@class='adjusted-par'][1]", $h2Element)->item(0);
if ($ulElement) {
$listItems = $xpath->query(".//li", $ulElement);
foreach ($listItems as $listItem) {
$deskripsi = $this->_cleanText($listItem->nodeValue);
$arti[] = ['deskripsi' => $deskripsi];
}
}
// Menyimpan data dalam $dataResponse
if (!empty($lema) && !empty($arti)) {
$dataResponse[] = [
'word' => $word,
'lema' => $lema,
'arti' => $arti,
'tesaurusLink' => $tesaurusLink,
];
}
}
return count($dataResponse) ? $dataResponse : [];
}
private function _KBBI_official($word)
{
// Clean the word
$cleanWord = $this->_cleanWord($word);
// If not found in the database, fetch from the web
$htmlData = $this->_fetchHtml($word);
$_parserV2 = $this->_parserV2($htmlData, $cleanWord);
if (count($_parserV2)) {
return $_parserV2;
}
$_parserV3 = $this->_parserV3($htmlData, $cleanWord);
if (count($_parserV3)) {
return $_parserV3;
}
return [];
}
public function searchWord($word)
{
try {
$_KBBI_official = $this->_KBBI_official($word);
if (count($_KBBI_official)) {
return $_KBBI_official;
}
} catch (\Exception $e) {
return false;
}
return false;
}
}