-
Notifications
You must be signed in to change notification settings - Fork 43
/
Pico.php
362 lines (314 loc) · 8.75 KB
/
Pico.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
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
<?php
/**
* CMS Pico - Create websites using Pico CMS for Nextcloud.
*
* @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>)
* @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace OCA\CMSPico;
use HTMLPurifier;
use HTMLPurifier_Config;
use HTMLPurifier_HTML5Config;
use OCA\CMSPico\Exceptions\WebsiteInvalidFilesystemException;
use OCA\CMSPico\Files\FileInterface;
use OCA\CMSPico\Files\FolderInterface;
use OCA\CMSPico\Files\Glob\GlobIterator;
use OCA\CMSPico\Files\NodeInterface;
use OCA\CMSPico\Model\Website;
use OCA\CMSPico\Model\WebsiteRequest;
use OCA\CMSPico\Service\PicoService;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use Symfony\Component\Yaml\Exception\ParseException;
class Pico extends \Pico
{
/**
* API version 0, used by Pico 0.9 and earlier
*
* @var int
*/
public const API_VERSION_0 = 0;
/**
* API version 1, used by Pico 1.0
*
* @var int
*/
public const API_VERSION_1 = 1;
/**
* API version 2, used by Pico 2.0
*
* @var int
*/
public const API_VERSION_2 = 2;
/**
* API version 3, used by Pico 2.1
*
* @var int
*/
public const API_VERSION_3 = 3;
/** @var PicoService */
private $picoService;
/** @var HTMLPurifier */
private $htmlPurifier;
/** @var WebsiteRequest */
private $websiteRequest;
/** @var Website */
private $website;
/**
* Pico constructor.
*
* {@inheritDoc}
*/
public function __construct($rootDir, $configDir, $pluginsDir, $themesDir, $enableLocalPlugins = true)
{
$this->picoService = \OC::$server->query(PicoService::class);
parent::__construct($rootDir, $configDir, $pluginsDir, $themesDir, $enableLocalPlugins);
}
/**
* {@inheritDoc}
*
* @return string
* @throws WebsiteInvalidFilesystemException
* @throws InvalidPathException
* @throws NotFoundException
* @throws NotPermittedException
*/
public function run()
{
return parent::run();
}
/**
* Set's Nextcloud's website and website request instances.
*
* @param WebsiteRequest $websiteRequest Nextcloud's website request instance
*
* @return void
*/
public function setNextcloudWebsite(WebsiteRequest $websiteRequest)
{
$this->websiteRequest = $websiteRequest;
$this->website = $websiteRequest->getWebsite();
}
/**
* Set's Pico's request URL.
*
* @param string $requestUrl request URL
*
* @return void
*/
public function setRequestUrl($requestUrl)
{
$this->requestUrl = $requestUrl;
}
/**
* Don't let Pico evaluate the request URL.
*
* @see Pico::setRequestUrl()
*
* @return void
*/
protected function evaluateRequestUrl()
{
// do nothing
}
/**
* Checks whether a file is readable in Nextcloud and returns the raw contents of this file
*
* @param string $absolutePath file path
*
* @return string raw contents of the file
* @throws WebsiteInvalidFilesystemException
* @throws InvalidPathException
* @throws NotFoundException
* @throws NotPermittedException
*/
public function loadFileContent($absolutePath)
{
/** @var FolderInterface $folder */
/** @var string $basePath */
/** @var string $relativePath */
[ $folder, $basePath, $relativePath ] = $this->picoService->getRelativePath($this->website, $absolutePath);
$file = $folder->getFile($relativePath);
return $file->getContent();
}
/**
* Returns the parsed and purified file meta from raw file contents.
*
* @param string $rawContent
* @param string[] $headers
*
* @return array
* @throws ParseException
*/
public function parseFileMeta($rawContent, array $headers)
{
$meta = parent::parseFileMeta($rawContent, $headers);
return $this->purifyFileMeta($meta);
}
/**
* Purifies file meta.
*
* @param array $meta
*
* @return array
*/
protected function purifyFileMeta(array $meta): array
{
$newMeta = [];
foreach ($meta as $key => $value) {
if (is_array($value)) {
$newMeta[$key] = $this->purifyFileMeta($value);
} else {
$newMeta[$key] = $this->getHtmlPurifier()->purify($value);
}
}
return $newMeta;
}
/**
* Returns the parsed and purified contents of a page.
*
* @param string $markdown
* @param bool $singleLine
*
* @return string
*/
public function parseFileContent($markdown, $singleLine = false)
{
$content = parent::parseFileContent($markdown, $singleLine);
return $this->purifyFileContent($content);
}
/**
* Purifies file content.
*
* @param string $content
*
* @return string
*/
protected function purifyFileContent(string $content): string
{
return $this->getHtmlPurifier()->purify($content);
}
/**
* Returns the HTMLPurifier instance.
*
* @return HTMLPurifier
*/
public function getHtmlPurifier(): HTMLPurifier
{
if ($this->htmlPurifier === null) {
$this->htmlPurifier = new HTMLPurifier($this->getHtmlPurifierConfig());
$this->triggerEvent('onHtmlPurifier', [ &$this->htmlPurifier ]);
}
return $this->htmlPurifier;
}
/**
* Returns the HTMLPurifier_Config instance.
*
* @return HTMLPurifier_Config
*/
private function getHtmlPurifierConfig(): HTMLPurifier_Config
{
$config = HTMLPurifier_HTML5Config::createDefault();
$config->autoFinalize = false;
$config->set('Attr.EnableID', true);
$allowedSchemes = array_merge($config->get('URI.AllowedSchemes'), [ 'data' => true ]);
$config->set('URI.AllowedSchemes', $allowedSchemes);
$config->set('HTML.Allowed', 'a[href|target]');
$config->set('Attr.AllowedFrameTargets', [ '_blank' ]);
$config->finalize();
return $config;
}
/**
* @param string $absolutePath
* @param string $fileExtension
* @param int $order
*
* @return string[]
* @throws WebsiteInvalidFilesystemException
* @throws InvalidPathException
*/
public function getFiles($absolutePath, $fileExtension = '', $order = \Pico::SORT_ASC)
{
/** @var FolderInterface $folder */
/** @var string $basePath */
/** @var string $relativePath */
[ $folder, $basePath, $relativePath ] = $this->picoService->getRelativePath($this->website, $absolutePath);
if ($folder->isLocal()) {
return parent::getFiles($absolutePath, $fileExtension, $order);
}
$folderFilter = function (NodeInterface $node, int $key, FolderInterface $folder) use ($fileExtension) {
$fileName = $node->getName();
// exclude hidden files/dirs starting with a .
// exclude files ending with a ~ (vim/nano backup) or # (emacs backup)
if (($fileName[0] === '.') || in_array($fileName[-1], [ '~', '#' ], true)) {
return false;
}
if ($node->isFile()) {
/** @var FileInterface $node */
if ($fileExtension && ($fileExtension !== '.' . $node->getExtension())) {
return false;
}
}
return true;
};
try {
$folderIterator = new \RecursiveCallbackFilterIterator($folder->fakeRoot(), $folderFilter);
$result = [];
foreach (new \RecursiveIteratorIterator($folderIterator) as $file) {
$result[] = $basePath . '/' . $relativePath . $file->getPath();
}
return ($order === \Pico::SORT_DESC) ? array_reverse($result) : $result;
} catch (\Exception $e) {
return [];
}
}
/**
* @param string $absolutePathPattern
* @param int $order
*
* @return string[]
* @throws WebsiteInvalidFilesystemException
* @throws InvalidPathException
*/
public function getFilesGlob($absolutePathPattern, $order = \Pico::SORT_ASC)
{
/** @var FolderInterface $folder */
/** @var string $basePath */
/** @var string $pattern */
[ $folder, $basePath, $pattern ] = $this->picoService->getRelativePath($this->website, $absolutePathPattern);
if ($folder->isLocal()) {
return parent::getFilesGlob($absolutePathPattern, $order);
}
try {
$result = [];
foreach (new GlobIterator($folder, $pattern) as $file) {
$fileName = $file->getName();
// exclude files ending with a ~ (vim/nano backup) or # (emacs backup)
if (in_array($fileName[-1], [ '~', '#' ], true)) {
continue;
}
$result[] = $basePath . $file->getPath();
}
return ($order === \Pico::SORT_DESC) ? array_reverse($result) : $result;
} catch (\Exception $e) {
return [];
}
}
}