1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This program is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* This program is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace fkooman\RemoteStorage; |
19
|
|
|
|
20
|
|
|
use fkooman\RemoteStorage\Exception\PathException; |
21
|
|
|
|
22
|
|
|
class Path |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
private $p; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
private $pathParts; |
29
|
|
|
|
30
|
|
|
public function __construct($p) |
31
|
|
|
{ |
32
|
|
|
if (!is_string($p)) { |
33
|
|
|
throw new PathException('invalid path: not a string'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// MUST contain at least one slash and start with it |
37
|
|
|
if (0 !== strpos($p, '/')) { |
38
|
|
|
throw new PathException('invalid path: does not start with /'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// MUST NOT contain encoded "/" |
42
|
|
|
if (false !== stripos($p, '%2f')) { |
43
|
|
|
throw new PathException('invalid path: contains encoded "/"'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// MUST NOT contain encoded "\0" |
47
|
|
|
if (false !== strpos($p, '%00')) { |
48
|
|
|
throw new PathException('invalid path: contains encoded "\0"'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// MUST NOT contain ".." |
52
|
|
|
if (false !== strpos($p, '..')) { |
53
|
|
|
throw new PathException('invalid path: contains ..'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// MUST NOT contain "%2e%2e" |
57
|
|
|
if (false !== stripos($p, '%2e%2e')) { |
58
|
|
|
throw new PathException('invalid path: contains encoded ".."'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// MUST NOT contain "//" |
62
|
|
|
if (false !== strpos($p, '//')) { |
63
|
|
|
throw new PathException('invalid path: contains //'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// MUST contain a user |
67
|
|
|
$pathParts = explode('/', $p); |
68
|
|
|
if (count($pathParts) < 3) { |
69
|
|
|
throw new PathException('invalid path: no user specified'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach ($pathParts as $pathPart) { |
73
|
|
|
$this->pathParts[] = rawurldecode($pathPart); |
74
|
|
|
} |
75
|
|
|
$this->p = implode('/', $this->pathParts); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getPath() |
79
|
|
|
{ |
80
|
|
|
return $this->p; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getIsPublic() |
84
|
|
|
{ |
85
|
|
|
return count($this->pathParts) > 3 && 'public' === $this->pathParts[2]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getUserId() |
89
|
|
|
{ |
90
|
|
|
return $this->pathParts[1]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getIsFolder() |
94
|
|
|
{ |
95
|
|
|
return empty($this->pathParts[count($this->pathParts) - 1]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getIsDocument() |
99
|
|
|
{ |
100
|
|
|
return !$this->getIsFolder(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getModuleName() |
104
|
|
|
{ |
105
|
|
|
$moduleNamePosition = $this->getIsPublic() ? 3 : 2; |
106
|
|
|
if (count($this->pathParts) > $moduleNamePosition + 1) { |
107
|
|
|
return $this->pathParts[$moduleNamePosition]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getFolderPath() |
114
|
|
|
{ |
115
|
|
|
if ($this->getIsFolder()) { |
116
|
|
|
return $this->p; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return substr($this->p, 0, strrpos($this->p, '/') + 1); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getFolderTreeToUserRoot() |
123
|
|
|
{ |
124
|
|
|
$p = $this->getFolderPath(); |
125
|
|
|
do { |
126
|
|
|
$folderTree[] = $p; |
|
|
|
|
127
|
|
|
|
128
|
|
|
// remove from last "/" to previous "/", e.g.: |
129
|
|
|
// "/foo/bar/baz/" -> "/foo/bar/" |
|
|
|
|
130
|
|
|
|
131
|
|
|
// remove the last "/" |
132
|
|
|
$p = substr($p, 0, strlen($p) - 1); |
133
|
|
|
// remove everything after the now last "/" |
134
|
|
|
$p = substr($p, 0, strrpos($p, '/') + 1); |
135
|
|
|
} while (substr_count($p, '/') > 1); |
136
|
|
|
|
137
|
|
|
return $folderTree; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getFolderTreeFromUserRoot() |
141
|
|
|
{ |
142
|
|
|
return array_reverse($this->getFolderTreeToUserRoot()); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.