1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CMS Pico - Create websites using Pico CMS for Nextcloud. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>) |
6
|
|
|
* @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>) |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
declare(strict_types=1); |
25
|
|
|
|
26
|
|
|
namespace OCA\CMSPico; |
27
|
|
|
|
28
|
|
|
use HTMLPurifier; |
29
|
|
|
use HTMLPurifier_Config; |
30
|
|
|
use HTMLPurifier_HTML5Config; |
31
|
|
|
use OCA\CMSPico\Exceptions\WebsiteInvalidFilesystemException; |
32
|
|
|
use OCA\CMSPico\Files\FileInterface; |
33
|
|
|
use OCA\CMSPico\Files\FolderInterface; |
34
|
|
|
use OCA\CMSPico\Files\Glob\GlobIterator; |
35
|
|
|
use OCA\CMSPico\Files\NodeInterface; |
36
|
|
|
use OCA\CMSPico\Model\Website; |
37
|
|
|
use OCA\CMSPico\Model\WebsiteRequest; |
38
|
|
|
use OCA\CMSPico\Service\PicoService; |
39
|
|
|
use OCP\Files\InvalidPathException; |
40
|
|
|
use OCP\Files\NotFoundException; |
41
|
|
|
use OCP\Files\NotPermittedException; |
42
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
43
|
|
|
|
44
|
|
|
class Pico extends \Pico |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* API version 0, used by Pico 0.9 and earlier |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
public const API_VERSION_0 = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* API version 1, used by Pico 1.0 |
55
|
|
|
* |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
public const API_VERSION_1 = 1; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* API version 2, used by Pico 2.0 |
62
|
|
|
* |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
public const API_VERSION_2 = 2; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* API version 3, used by Pico 2.1 |
69
|
|
|
* |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
public const API_VERSION_3 = 3; |
73
|
|
|
|
74
|
|
|
/** @var PicoService */ |
75
|
|
|
private $picoService; |
76
|
|
|
|
77
|
|
|
/** @var HTMLPurifier */ |
78
|
|
|
private $htmlPurifier; |
79
|
|
|
|
80
|
|
|
/** @var WebsiteRequest */ |
81
|
|
|
private $websiteRequest; |
82
|
|
|
|
83
|
|
|
/** @var Website */ |
84
|
|
|
private $website; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Pico constructor. |
88
|
|
|
* |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
5 |
|
public function __construct($rootDir, $configDir, $pluginsDir, $themesDir, $enableLocalPlugins = true) |
92
|
|
|
{ |
93
|
5 |
|
$this->picoService = \OC::$server->query(PicoService::class); |
|
|
|
|
94
|
|
|
|
95
|
5 |
|
parent::__construct($rootDir, $configDir, $pluginsDir, $themesDir, $enableLocalPlugins); |
96
|
5 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
* @throws WebsiteInvalidFilesystemException |
103
|
|
|
* @throws InvalidPathException |
104
|
|
|
* @throws NotFoundException |
105
|
|
|
* @throws NotPermittedException |
106
|
|
|
*/ |
107
|
5 |
|
public function run() |
108
|
|
|
{ |
109
|
5 |
|
return parent::run(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set's Nextcloud's website and website request instances. |
114
|
|
|
* |
115
|
|
|
* @param WebsiteRequest $websiteRequest Nextcloud's website request instance |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
5 |
|
public function setNextcloudWebsite(WebsiteRequest $websiteRequest) |
120
|
|
|
{ |
121
|
5 |
|
$this->websiteRequest = $websiteRequest; |
122
|
5 |
|
$this->website = $websiteRequest->getWebsite(); |
123
|
5 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set's Pico's request URL. |
127
|
|
|
* |
128
|
|
|
* @param string $requestUrl request URL |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
5 |
|
public function setRequestUrl($requestUrl) |
133
|
|
|
{ |
134
|
5 |
|
$this->requestUrl = $requestUrl; |
135
|
5 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Don't let Pico evaluate the request URL. |
139
|
|
|
* |
140
|
|
|
* @see Pico::setRequestUrl() |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
5 |
|
protected function evaluateRequestUrl() |
145
|
|
|
{ |
146
|
|
|
// do nothing |
147
|
5 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Checks whether a file is readable in Nextcloud and returns the raw contents of this file |
151
|
|
|
* |
152
|
|
|
* @param string $absolutePath file path |
153
|
|
|
* |
154
|
|
|
* @return string raw contents of the file |
155
|
|
|
* @throws WebsiteInvalidFilesystemException |
156
|
|
|
* @throws InvalidPathException |
157
|
|
|
* @throws NotFoundException |
158
|
|
|
* @throws NotPermittedException |
159
|
|
|
*/ |
160
|
5 |
|
public function loadFileContent($absolutePath) |
161
|
|
|
{ |
162
|
|
|
/** @var FolderInterface $folder */ |
163
|
|
|
/** @var string $basePath */ |
164
|
|
|
/** @var string $relativePath */ |
165
|
5 |
|
[ $folder, $basePath, $relativePath ] = $this->picoService->getRelativePath($this->website, $absolutePath); |
166
|
|
|
|
167
|
5 |
|
$file = $folder->getFile($relativePath); |
168
|
5 |
|
return $file->getContent(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns the parsed and purified file meta from raw file contents. |
173
|
|
|
* |
174
|
|
|
* @param string $rawContent |
175
|
|
|
* @param string[] $headers |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
* @throws ParseException |
179
|
|
|
*/ |
180
|
5 |
|
public function parseFileMeta($rawContent, array $headers) |
181
|
|
|
{ |
182
|
5 |
|
$meta = parent::parseFileMeta($rawContent, $headers); |
183
|
5 |
|
return $this->purifyFileMeta($meta); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Purifies file meta. |
188
|
|
|
* |
189
|
|
|
* @param array $meta |
190
|
|
|
* |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
5 |
|
protected function purifyFileMeta(array $meta): array |
194
|
|
|
{ |
195
|
5 |
|
$newMeta = []; |
196
|
5 |
|
foreach ($meta as $key => $value) { |
197
|
5 |
|
if (is_array($value)) { |
198
|
3 |
|
$newMeta[$key] = $this->purifyFileMeta($value); |
199
|
|
|
} else { |
200
|
5 |
|
$newMeta[$key] = $this->getHtmlPurifier()->purify($value); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
5 |
|
return $newMeta; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns the parsed and purified contents of a page. |
209
|
|
|
* |
210
|
|
|
* @param string $markdown |
211
|
|
|
* @param bool $singleLine |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
5 |
|
public function parseFileContent($markdown, $singleLine = false) |
216
|
|
|
{ |
217
|
5 |
|
$content = parent::parseFileContent($markdown, $singleLine); |
218
|
5 |
|
return $this->purifyFileContent($content); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Purifies file content. |
223
|
|
|
* |
224
|
|
|
* @param string $content |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
5 |
|
protected function purifyFileContent(string $content): string |
229
|
|
|
{ |
230
|
5 |
|
return $this->getHtmlPurifier()->purify($content); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Returns the HTMLPurifier instance. |
235
|
|
|
* |
236
|
|
|
* @return HTMLPurifier |
237
|
|
|
*/ |
238
|
5 |
|
public function getHtmlPurifier(): HTMLPurifier |
239
|
|
|
{ |
240
|
5 |
|
if ($this->htmlPurifier === null) { |
241
|
5 |
|
$this->htmlPurifier = new HTMLPurifier($this->getHtmlPurifierConfig()); |
242
|
|
|
|
243
|
5 |
|
$this->triggerEvent('onHtmlPurifier', [ &$this->htmlPurifier ]); |
244
|
|
|
} |
245
|
|
|
|
246
|
5 |
|
return $this->htmlPurifier; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Returns the HTMLPurifier_Config instance. |
251
|
|
|
* |
252
|
|
|
* @return HTMLPurifier_Config |
253
|
|
|
*/ |
254
|
5 |
|
private function getHtmlPurifierConfig(): HTMLPurifier_Config |
255
|
|
|
{ |
256
|
5 |
|
$config = HTMLPurifier_HTML5Config::createDefault(); |
257
|
5 |
|
$config->autoFinalize = false; |
258
|
|
|
|
259
|
5 |
|
$config->set('Attr.EnableID', true); |
260
|
|
|
|
261
|
5 |
|
$allowedSchemes = array_merge($config->get('URI.AllowedSchemes'), [ 'data' => true ]); |
262
|
5 |
|
$config->set('URI.AllowedSchemes', $allowedSchemes); |
263
|
|
|
|
264
|
5 |
|
$config->finalize(); |
265
|
|
|
|
266
|
5 |
|
return $config; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param string $absolutePath |
271
|
|
|
* @param string $fileExtension |
272
|
|
|
* @param int $order |
273
|
|
|
* |
274
|
|
|
* @return string[] |
275
|
|
|
* @throws WebsiteInvalidFilesystemException |
276
|
|
|
* @throws InvalidPathException |
277
|
|
|
*/ |
278
|
5 |
|
public function getFiles($absolutePath, $fileExtension = '', $order = \Pico::SORT_ASC) |
279
|
|
|
{ |
280
|
|
|
/** @var FolderInterface $folder */ |
281
|
|
|
/** @var string $basePath */ |
282
|
|
|
/** @var string $relativePath */ |
283
|
5 |
|
[ $folder, $basePath, $relativePath ] = $this->picoService->getRelativePath($this->website, $absolutePath); |
284
|
|
|
|
285
|
5 |
|
if ($folder->isLocal()) { |
286
|
5 |
|
return parent::getFiles($absolutePath, $fileExtension, $order); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$folderFilter = function (NodeInterface $node, int $key, FolderInterface $folder) use ($fileExtension) { |
290
|
|
|
$fileName = $node->getName(); |
291
|
|
|
|
292
|
|
|
// exclude hidden files/dirs starting with a . |
293
|
|
|
// exclude files ending with a ~ (vim/nano backup) or # (emacs backup) |
294
|
|
|
if (($fileName[0] === '.') || in_array($fileName[-1], [ '~', '#' ], true)) { |
295
|
|
|
return false; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if ($node->isFile()) { |
299
|
|
|
/** @var FileInterface $node */ |
300
|
|
|
if ($fileExtension && ($fileExtension !== '.' . $node->getExtension())) { |
301
|
|
|
return false; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return true; |
306
|
|
|
}; |
307
|
|
|
|
308
|
|
|
try { |
309
|
|
|
$folderIterator = new \RecursiveCallbackFilterIterator($folder->fakeRoot(), $folderFilter); |
310
|
|
|
|
311
|
|
|
$result = []; |
312
|
|
|
foreach (new \RecursiveIteratorIterator($folderIterator) as $file) { |
313
|
|
|
$result[] = $basePath . '/' . $relativePath . $file->getPath(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return ($order === \Pico::SORT_DESC) ? array_reverse($result) : $result; |
317
|
|
|
} catch (\Exception $e) { |
318
|
|
|
return []; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param string $absolutePathPattern |
324
|
|
|
* @param int $order |
325
|
|
|
* |
326
|
|
|
* @return string[] |
327
|
|
|
* @throws WebsiteInvalidFilesystemException |
328
|
|
|
* @throws InvalidPathException |
329
|
|
|
*/ |
330
|
5 |
|
public function getFilesGlob($absolutePathPattern, $order = \Pico::SORT_ASC) |
331
|
|
|
{ |
332
|
|
|
/** @var FolderInterface $folder */ |
333
|
|
|
/** @var string $basePath */ |
334
|
|
|
/** @var string $pattern */ |
335
|
5 |
|
[ $folder, $basePath, $pattern ] = $this->picoService->getRelativePath($this->website, $absolutePathPattern); |
336
|
|
|
|
337
|
5 |
|
if ($folder->isLocal()) { |
338
|
5 |
|
return parent::getFilesGlob($absolutePathPattern, $order); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
try { |
342
|
|
|
$result = []; |
343
|
|
|
foreach (new GlobIterator($folder, $pattern) as $file) { |
344
|
|
|
$fileName = $file->getName(); |
345
|
|
|
|
346
|
|
|
// exclude files ending with a ~ (vim/nano backup) or # (emacs backup) |
347
|
|
|
if (in_array($fileName[-1], [ '~', '#' ], true)) { |
348
|
|
|
continue; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
$result[] = $basePath . $file->getPath(); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
return ($order === \Pico::SORT_DESC) ? array_reverse($result) : $result; |
355
|
|
|
} catch (\Exception $e) { |
356
|
|
|
return []; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.