-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSearchController.php
More file actions
395 lines (309 loc) · 9.81 KB
/
SearchController.php
File metadata and controls
395 lines (309 loc) · 9.81 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
namespace Plugins\bettersearch;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Typemill\Models\Navigation;
use Typemill\Models\StorageWrapper;
use Typemill\Controllers\Controller;
## gets and creates the index
class SearchController extends Controller
{
protected $error = false;
protected $project = false;
protected $projectlist = false;
protected $indexname = 'index';
protected $urlinfo = false;
public function index(Request $request, Response $response, $args)
{
$token = $request->getQueryParams()['token'] ?? false;
$project = $request->getQueryParams()['project'] ?? false;
if(!$this->validateToken($token))
{
$response->getBody()->write('Please reload the page and start again.');
return $response->withStatus(403);
}
$pluginSettings = $this->settings['plugins']['bettersearch'] ?? false;
$storage = new StorageWrapper($this->settings['storage']);
$navigation = new Navigation();
$this->urlinfo = $this->c->get('urlinfo');
if($project)
{
$navigation->setProject($this->settings, '/' . $project . '/');
$project = $navigation->getProject();
if($project)
{
$this->project = $project;
}
}
if(isset($pluginSettings['fullindex']) && $pluginSettings['fullindex'])
{
# search all indexes
$this->projectlist = $navigation->getAllProjects($this->settings);
}
$index = $this->getIndex($storage, $navigation);
if($this->error)
{
$response->getBody()->write($this->error);
$response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
$response->getBody()->write($index);
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
}
private function getIndex($storage, $navigation)
{
if($this->projectlist && is_array($this->projectlist))
{
$mergedIndex = [];
# get the main index
$baseNavigation = new Navigation();
$baseindex = $this->getSingleIndex($storage, $baseNavigation);
# get the project indexes
foreach($this->projectlist as $singleproject)
{
if(isset($singleproject['base']) && $singleproject['base'])
{
# it is the base project, so use the baseindex
$index = $baseindex;
}
else
{
$this->indexname = "index_" . strtolower($singleproject['id']);
$navigation->setProject($this->settings, '/' . $singleproject['id'] . '/');
$index = $this->getSingleIndex($storage, $navigation);
}
if ($index)
{
$decoded = json_decode($index, true);
if (is_array($decoded))
{
$decoded = $this->addProjectFolderToIndex($decoded, $singleproject);
$mergedIndex = array_merge($mergedIndex, $decoded);
}
}
}
# merge index;
return json_encode($mergedIndex, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
else
{
if($this->project)
{
$this->indexname = "index_" . strtolower($this->project);
}
$index = $this->getSingleIndex($storage, $navigation);
return $index;
}
}
private function getSingleIndex($storage, $navigation)
{
$index = $storage->getFile('dataFolder', 'bettersearch', $this->indexname . '.json');
if(!$index or empty(json_decode($index)))
{
$index = $this->createIndex($storage, $navigation);
if(!$index)
{
return false;
}
$index = json_encode($index, JSON_UNESCAPED_SLASHES);
# store the index file here
$store = $storage->writeFile('dataFolder', 'bettersearch', $this->indexname . '.json', $index);
if(!$store)
{
$this->error = $storage->getError();
return false;
}
}
return $index;
}
private function createIndex($storage, $navigation)
{
$urlinfo = $this->c->get('urlinfo');
$langattr = $this->settings['langattr'];
$liveNavigation = $navigation->getLiveNavigation($urlinfo, $langattr);
if(!$liveNavigation)
{
$this->error = "No navigation/content found.";
return false;
}
# get data for search-index
$index = $this->getAllContent($liveNavigation, $storage, []);
if(!$index OR !is_array($index) OR empty($index))
{
$this->error = 'We could not create the search-index.';
return false;
}
return $index;
}
private function getAllContent($navigation, $storage, $index, $firstLevel = false)
{
# get the homepage
if(empty($index))
{
$folder = '';
$urlAbs = $this->urlinfo['baseurl'];
# if it is a current project, load the index file of the project
if($this->project)
{
$folder = '_' . $this->project;
$urlAbs .= '/' . $this->project;
}
# try to add the startpage
$page = $storage->getFile('contentFolder', $folder, 'index.md');
if($page)
{
$pageArray = $this->getPageContentArray($page, $urlAbs, $firstLevel);
$index[$pageArray['url']] = $pageArray;
}
}
foreach($navigation as $item)
{
# Check if the item is a first-level item and set the firstLevelTitle
if (
$item->elementType == "folder" &&
isset($item->keyPathArray) &&
count($item->keyPathArray) == 1
)
{
$firstLevel = [
'name' => $item->name,
'path' => $item->pathWithoutType
];
}
if($item->fileType == 'md')
{
$page = $storage->getFile('contentFolder', '', $item->pathWithoutType . '.md');
$pageArray = $this->getPageContentArray($page, $item->urlAbs, $firstLevel);
$index[$pageArray['url']] = $pageArray;
if($item->elementType == "folder")
{
$index = $this->getAllContent($item->folderContent, $storage, $index, $firstLevel);
}
}
}
return $index;
}
private function getPageContentArray($page, $url, $firstLevel)
{
$parts = explode("\n", $page, 2);
# get the title / headline
$title = trim($parts[0], '# ');
$title = str_replace(["\r\n", "\n", "\r"],' ', $title);
# Get and clean up the content
$content = $parts[1] ?? '';
# Remove empty lines
$content = preg_replace('/^\s*$(?:\r\n?|\n)/m', '', $content);
# get and cleanup the content
$content = str_replace(["\r\n", "\n", "\r"],' ', $content);
$content = $this->strip_markdown($content);
$pageContent = [
'title' => $title,
'content' => $content,
'url' => $url,
'filtername' => false,
'filterpath' => false
];
if(isset($firstLevel) && is_array($firstLevel))
{
$pageContent['filtername'] = $firstLevel['name'] ?? false;
$pageContent['filterpath'] = $firstLevel['path'] ?? false;
}
return $pageContent;
}
# see https://github.com/stiang/remove-markdown/blob/master/index.js
private function strip_markdown($content)
{
# Remove TOC
$content = str_replace('[TOC]', '', $content);
if(!$content){return false;}
# Remove Shortcodes
$content = preg_replace('/\[:.*:\]/m', '', $content);
if(!$content){return false;}
# Remove horizontal rules
$content = preg_replace('/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/m', '', $content);
if(!$content){return false;}
# Lists
$content = preg_replace('/^([\s\t]*)([\*\-\+]|\d+\.)\s+/', '', $content);
if(!$content){return false;}
# fenced codeblock
$content = preg_replace('/~{3}.*\n/', '', $content);
if(!$content){return false;}
# strikethrough
$content = preg_replace('/~~/', '', $content);
if(!$content){return false;}
# attributes
$content = preg_replace('/\{.*?\}/', '', $content);
if(!$content){return false;}
# fenced codeblocks
$content = preg_replace('/`{3}.*\n/', '', $content);
if(!$content){return false;}
# html tags
$content = preg_replace('/<[^>]*>/', '', $content);
if(!$content){return false;}
# setext headers
$content = preg_replace('/^[=\-]{2,}\s*$/', '', $content);
if(!$content){return false;}
# atx headers
$content = preg_replace('/#{1,6}/', '', $content);
$content = preg_replace('/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/', '$1$2$3', $content);
if(!$content){return false;}
# footnotes
$content = preg_replace('/\[\^.+?\](\: .*?$)?/', '', $content);
$content = preg_replace('/\s{0,2}\[.*?\]: .*?$/', '', $content);
if(!$content){return false;}
# images
$content = preg_replace('/\!\[(.*?)\][\[\(].*?[\]\)]/', '', $content);
if(!$content){return false;}
# inline links
# $content = preg_replace('/\[(.*?)\][\[(](.*?)[\])]/', '$1 $2', $content);
# if(!$content){return false;}
# referenced style links
# $content = preg_replace('/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/', '$1', $content);
# blockquotes
$content = preg_replace('/\s{0,3}>\s?/', '', $content);
if(!$content){return false;}
# emphasis
$content = preg_replace('/([\*_]{1,3})(\S.*?\S{0,1})\1/', '$2', $content);
if(!$content){return false;}
# codeblocks
$content = preg_replace('/(`{3,})(.*?)\1/', '$2', $content);
if(!$content){return false;}
# inlinecode
$content = preg_replace('/`(.+?)`/', '$1', $content);
if(!$content){return false;}
return $content;
}
private function addProjectFolderToIndex($decoded, $singleproject)
{
foreach($decoded as $key => &$item)
{
$item['filtername'] = $singleproject['label'];
$item['filterpath'] = '/' . strtolower($singleproject['id']) . '/';
}
return $decoded;
}
private function validateToken($token)
{
if(!$token)
{
return false;
}
$secretKey = hash('sha256', __DIR__);
$decoded = base64_decode($token, true);
if (!$decoded || strpos($decoded, '.') === false)
{
return false;
}
[$payload, $sig] = explode('.', $decoded, 2);
$expected = hash_hmac('sha256', $payload, $secretKey);
if (!hash_equals($expected, $sig))
{
return false;
}
$data = json_decode($payload, true);
if (!$data || time() - $data['t'] > 900) // 15 minutes
{
return false;
}
return true;
}
}