|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CMS Pico - Create websites using Pico CMS for Nextcloud. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>) |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
declare(strict_types=1); |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\CMSPico\Model; |
|
26
|
|
|
|
|
27
|
|
|
use OCA\CMSPico\Service\MiscService; |
|
28
|
|
|
use OCP\Files\InvalidPathException; |
|
29
|
|
|
use Pico; |
|
30
|
|
|
|
|
31
|
|
|
class PicoPage |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var MiscService */ |
|
34
|
|
|
private $miscService; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Website */ |
|
37
|
|
|
private $website; |
|
38
|
|
|
|
|
39
|
|
|
/** @var Pico */ |
|
40
|
|
|
private $pico; |
|
41
|
|
|
|
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
private $output; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* PicoPage constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Website $website |
|
49
|
|
|
* @param Pico $pico |
|
50
|
|
|
* @param string $output |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function __construct(Website $website, Pico $pico, string $output) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$this->miscService = \OC::$server->query(MiscService::class); |
|
55
|
|
|
|
|
56
|
2 |
|
$this->website = $website; |
|
57
|
2 |
|
$this->pico = $pico; |
|
58
|
2 |
|
$this->output = $output; |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getAbsolutePath(): string |
|
65
|
|
|
{ |
|
66
|
|
|
$requestFile = $this->pico->getRequestFile(); |
|
67
|
|
|
if ($requestFile) { |
|
68
|
|
|
return $requestFile; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$contentDir = $this->pico->getConfig('content_dir'); |
|
72
|
|
|
$contentExt = $this->pico->getConfig('content_ext'); |
|
73
|
|
|
return $contentDir . ($this->website->getPage() ?: 'index') . $contentExt; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function getRelativePath(): string |
|
80
|
|
|
{ |
|
81
|
2 |
|
$requestFile = $this->pico->getRequestFile(); |
|
82
|
2 |
|
if ($requestFile) { |
|
83
|
|
|
try { |
|
84
|
2 |
|
$contentDir = $this->pico->getConfig('content_dir'); |
|
85
|
2 |
|
$relativePath = $this->miscService->getRelativePath($requestFile, $contentDir); |
|
86
|
|
|
|
|
87
|
2 |
|
$contentExt = $this->pico->getConfig('content_ext'); |
|
88
|
2 |
|
return $this->miscService->dropFileExtension($relativePath, $contentExt); |
|
89
|
|
|
} catch (InvalidPathException $e) { |
|
90
|
|
|
// fallback to the website's page |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return ($this->website->getPage() ?: 'index'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getRawContent(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->pico->getRawContent() ?? ''; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function getMeta(): array |
|
109
|
|
|
{ |
|
110
|
2 |
|
return $this->pico->getFileMeta() ?? []; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getContent(): string |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->pico->getFileContent() ?? ''; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function is404Content(): bool |
|
125
|
|
|
{ |
|
126
|
1 |
|
return $this->pico->is404Content(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function render(): string |
|
133
|
|
|
{ |
|
134
|
2 |
|
return $this->output; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|