-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
escape_search_data.php
39 lines (30 loc) · 1018 Bytes
/
escape_search_data.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
<?php
/**
* Escapes tags found in search data
*/
if (! isset($argv[1])) {
echo "[FAILED] No search index file provided\n";
exit(1);
}
$searchIndexFile = $argv[1];
if (! file_exists($searchIndexFile)) {
printf("[FAILED] Search index file '%s' does not exist\n", $searchIndexFile);
exit(1);
}
$json = file_get_contents($searchIndexFile);
try {
$index = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $e) {
printf("[FAILED] Could not parse file '%s': %s\n", $searchIndexFile, $e->getMessage());
exit(1);
}
if (! isset($index['docs'])) {
printf("[FAILED] Invalid search index structure in file '%s'\n", $searchIndexFile);
exit(1);
}
$index['docs'] = array_map(function ($item) {
$item['text'] = htmlspecialchars($item['text'], ENT_HTML5 | ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8', false);
return $item;
}, $index['docs']);
$json = json_encode($index, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
file_put_contents($searchIndexFile, $json);