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
|
27 |
|
public function __construct() |
40
|
|
|
{ |
41
|
27 |
|
$this->miscService = \OC::$server->query(MiscService::class); |
|
|
|
|
42
|
27 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
11 |
|
public function copy(FolderInterface $targetPath, string $name = null): NodeInterface |
48
|
|
|
{ |
49
|
11 |
|
if ($name !== null) { |
50
|
2 |
|
$this->assertValidFileName($name); |
51
|
|
|
} |
52
|
|
|
|
53
|
11 |
|
if ($this->isFolder()) { |
54
|
|
|
/** @var FolderInterface $this */ |
55
|
8 |
|
$target = $targetPath->newFolder($name ?? $this->getName()); |
56
|
8 |
|
foreach ($this as $child) { |
57
|
8 |
|
$child->copy($target); |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
/** @var FileInterface $this */ |
61
|
11 |
|
$target = $targetPath->newFile($name ?? $this->getName()); |
62
|
11 |
|
$target->putContent($this->getContent()); |
63
|
|
|
} |
64
|
|
|
|
65
|
11 |
|
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(): void |
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
|
11 |
|
public function isReadable(): bool |
129
|
|
|
{ |
130
|
11 |
|
return ($this->getPermissions() & Constants::PERMISSION_READ) === Constants::PERMISSION_READ; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
7 |
|
public function isUpdateable(): bool |
137
|
|
|
{ |
138
|
7 |
|
return ($this->getPermissions() & Constants::PERMISSION_UPDATE) === Constants::PERMISSION_UPDATE; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
4 |
|
public function isDeletable(): bool |
145
|
|
|
{ |
146
|
4 |
|
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
|
2 |
|
protected function assertValidFileName(string $name): void |
163
|
|
|
{ |
164
|
2 |
|
if (in_array($name, [ '', '.', '..' ], true)) { |
165
|
|
|
throw new InvalidPathException(); |
166
|
|
|
} |
167
|
2 |
|
if ((strpos($name, '/') !== false) || (strpos($name, '\\') !== false)) { |
168
|
|
|
throw new InvalidPathException(); |
169
|
|
|
} |
170
|
2 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $path |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
* @throws InvalidPathException |
177
|
|
|
*/ |
178
|
27 |
|
protected function normalizePath(string $path): string |
179
|
|
|
{ |
180
|
27 |
|
return '/' . $this->miscService->normalizePath($path); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.