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\Files; |
26
|
|
|
|
27
|
|
|
use OCA\CMSPico\Service\MiscService; |
28
|
|
|
use OCP\Constants; |
29
|
|
|
use OCP\Files\InvalidPathException; |
30
|
|
|
|
31
|
|
|
abstract class AbstractNode implements NodeInterface |
32
|
|
|
{ |
33
|
|
|
/** @var MiscService */ |
34
|
|
|
protected $miscService; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* AbstractNode constructor. |
38
|
|
|
*/ |
39
|
7 |
|
public function __construct() |
40
|
|
|
{ |
41
|
7 |
|
$this->miscService = \OC::$server->query(MiscService::class); |
42
|
7 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
4 |
|
public function copy(FolderInterface $targetPath, string $name = null): NodeInterface |
48
|
|
|
{ |
49
|
4 |
|
if ($name !== null) { |
50
|
|
|
$this->assertValidFileName($name); |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
if ($this->isFolder()) { |
54
|
|
|
/** @var FolderInterface $this */ |
55
|
1 |
|
$target = $targetPath->newFolder($name ?: $this->getName()); |
56
|
1 |
|
foreach ($this as $child) { |
57
|
1 |
|
$child->copy($target); |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
/** @var FileInterface $this */ |
61
|
4 |
|
$target = $targetPath->newFile($name ?: $this->getName()); |
62
|
4 |
|
$target->putContent($this->getContent()); |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
return $target; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function move(FolderInterface $targetPath, string $name = null): NodeInterface |
72
|
|
|
{ |
73
|
|
|
if ($name !== null) { |
74
|
|
|
$this->assertValidFileName($name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($this->isFolder()) { |
78
|
|
|
/** @var FolderInterface $this */ |
79
|
|
|
$target = $targetPath->newFolder($name ?: $this->getName()); |
80
|
|
|
foreach ($this as $child) { |
81
|
|
|
$child->move($target); |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
/** @var FileInterface $this */ |
85
|
|
|
$target = $targetPath->newFile($name ?: $this->getName()); |
86
|
|
|
$target->putContent($this->getContent()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->delete(); |
90
|
|
|
return $target; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
|
|
public function truncate() |
97
|
|
|
{ |
98
|
|
|
if ($this->isFolder()) { |
99
|
|
|
/** @var FolderInterface $this */ |
100
|
|
|
foreach ($this as $child) { |
101
|
|
|
$child->delete(); |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
/** @var FileInterface $this */ |
105
|
|
|
$this->putContent(''); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
|
|
public function isFile(): bool |
113
|
|
|
{ |
114
|
|
|
return ($this instanceof FileInterface); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
3 |
|
public function isFolder(): bool |
121
|
|
|
{ |
122
|
3 |
|
return ($this instanceof FolderInterface); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
4 |
|
public function isReadable(): bool |
129
|
|
|
{ |
130
|
4 |
|
return ($this->getPermissions() & Constants::PERMISSION_READ) === Constants::PERMISSION_READ; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
1 |
|
public function isUpdateable(): bool |
137
|
|
|
{ |
138
|
1 |
|
return ($this->getPermissions() & Constants::PERMISSION_UPDATE) === Constants::PERMISSION_UPDATE; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function isDeletable(): bool |
145
|
|
|
{ |
146
|
1 |
|
return ($this->getPermissions() & Constants::PERMISSION_DELETE) === Constants::PERMISSION_DELETE; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function __toString(): string |
153
|
|
|
{ |
154
|
|
|
return $this->getName(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $name |
159
|
|
|
* |
160
|
|
|
* @throws InvalidPathException |
161
|
|
|
*/ |
162
|
1 |
|
protected function assertValidFileName(string $name) |
163
|
|
|
{ |
164
|
1 |
|
if (in_array($name, [ '', '.', '..' ], true)) { |
165
|
|
|
throw new InvalidPathException(); |
166
|
|
|
} |
167
|
1 |
|
if ((strpos($name, '/') !== false) || (strpos($name, '\\') !== false)) { |
168
|
|
|
throw new InvalidPathException(); |
169
|
|
|
} |
170
|
1 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $path |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
* @throws InvalidPathException |
177
|
|
|
*/ |
178
|
7 |
|
protected function normalizePath(string $path): string |
179
|
|
|
{ |
180
|
7 |
|
return '/' . $this->miscService->normalizePath($path); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|