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\Model; |
27
|
|
|
|
28
|
|
|
use OCA\CMSPico\Files\AbstractNode; |
29
|
|
|
use OCA\CMSPico\Files\FileInterface; |
30
|
|
|
use OCA\CMSPico\Files\FolderInterface; |
31
|
|
|
use OCA\CMSPico\Files\NodeInterface; |
32
|
|
|
use OCP\Constants; |
33
|
|
|
use OCP\Files\GenericFileException; |
34
|
|
|
use OCP\Files\InvalidPathException; |
35
|
|
|
use OCP\Files\NotFoundException; |
36
|
|
|
use OCP\Files\NotPermittedException; |
37
|
|
|
|
38
|
|
|
class TemplateFile extends AbstractNode implements FileInterface |
39
|
|
|
{ |
40
|
|
|
/** @var FileInterface */ |
41
|
|
|
private $file; |
42
|
|
|
|
43
|
|
|
/** @var bool */ |
44
|
|
|
private $isBinary; |
45
|
|
|
|
46
|
|
|
/** @var array<string,string> */ |
47
|
|
|
private $data = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* TemplateFile constructor. |
51
|
|
|
* |
52
|
|
|
* @param FileInterface $file |
53
|
|
|
*/ |
54
|
3 |
|
public function __construct(FileInterface $file) |
55
|
|
|
{ |
56
|
3 |
|
parent::__construct(); |
57
|
|
|
|
58
|
3 |
|
$this->file = $file; |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function rename(string $name): NodeInterface |
65
|
|
|
{ |
66
|
|
|
throw new NotPermittedException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function delete(): void |
73
|
|
|
{ |
74
|
|
|
// nothing to do |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
public function getPath(): string |
81
|
|
|
{ |
82
|
|
|
return $this->file->getPath(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function getLocalPath(): string |
89
|
|
|
{ |
90
|
|
|
throw new NotFoundException(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
3 |
|
public function getName(): string |
97
|
|
|
{ |
98
|
3 |
|
return $this->file->getName(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
*/ |
104
|
3 |
|
public function getParentPath(): string |
105
|
|
|
{ |
106
|
3 |
|
return $this->file->getParentPath(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
|
|
public function getParentFolder(): FolderInterface |
113
|
|
|
{ |
114
|
|
|
throw new InvalidPathException(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
|
|
public function isLocal(): bool |
121
|
|
|
{ |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
|
|
public function getPermissions(): int |
129
|
|
|
{ |
130
|
|
|
return Constants::PERMISSION_READ | Constants::PERMISSION_DELETE; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
|
|
public function getExtension(): string |
137
|
|
|
{ |
138
|
|
|
return $this->file->getExtension(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
3 |
|
public function getContent(): string |
145
|
|
|
{ |
146
|
3 |
|
if ($this->isBinary() || empty($this->data)) { |
147
|
3 |
|
return $this->file->getContent(); |
148
|
|
|
} |
149
|
|
|
|
150
|
3 |
|
return str_replace(array_keys($this->data), $this->data, $this->file->getContent()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritDoc} |
155
|
|
|
*/ |
156
|
|
|
public function putContent(string $data): void |
157
|
|
|
{ |
158
|
|
|
throw new NotPermittedException(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return bool |
163
|
|
|
* @throws NotPermittedException |
164
|
|
|
* @throws GenericFileException |
165
|
|
|
*/ |
166
|
3 |
|
public function isBinary(): bool |
167
|
|
|
{ |
168
|
3 |
|
if ($this->isBinary === null) { |
169
|
3 |
|
$this->isBinary = $this->miscService->isBinaryFile($this->file); |
170
|
|
|
} |
171
|
|
|
|
172
|
3 |
|
return $this->isBinary; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array<string,string> $data |
177
|
|
|
*/ |
178
|
3 |
|
public function setTemplateData(array $data): void |
179
|
|
|
{ |
180
|
3 |
|
$this->data = []; |
181
|
3 |
|
foreach ($data as $key => $value) { |
182
|
3 |
|
$this->data['%%' . $key . '%%'] = $value; |
183
|
|
|
} |
184
|
3 |
|
} |
185
|
|
|
} |
186
|
|
|
|