1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the php-vfs package. |
4
|
|
|
* |
5
|
|
|
* (c) Michael Donat <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace VirtualFileSystem\Wrapper; |
12
|
|
|
|
13
|
|
|
use VirtualFileSystem\Structure\Node; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class to encapsulate permission checks |
17
|
|
|
* |
18
|
|
|
* @author Michael Donat <[email protected]> |
19
|
|
|
* @package php-vfs |
20
|
|
|
* @api |
21
|
|
|
*/ |
22
|
|
|
class PermissionHelper |
23
|
|
|
{ |
24
|
|
|
const MODE_USER_READ = 0400; |
25
|
|
|
const MODE_USER_WRITE = 0200; |
26
|
|
|
|
27
|
|
|
const MODE_GROUP_READ = 0040; |
28
|
|
|
const MODE_GROUP_WRITE = 0020; |
29
|
|
|
|
30
|
|
|
const MODE_WORLD_READ = 0004; |
31
|
|
|
const MODE_WORLD_WRITE = 0002; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Node |
35
|
|
|
*/ |
36
|
|
|
protected $node; |
37
|
|
|
|
38
|
|
|
protected $userid; |
39
|
|
|
protected $groupid; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param integer|null $uid |
43
|
|
|
* @param integer|null $gid |
44
|
|
|
*/ |
45
|
|
|
public function __construct($uid = null, $gid = null) |
46
|
|
|
{ |
47
|
|
|
$this->userid = is_null($uid) ? (function_exists('posix_getuid') ? posix_getuid() : 0) : $uid; |
48
|
|
|
$this->groupid = is_null($gid) ? ((function_exists('posix_getgid') ? posix_getgid() : 0)) : $gid; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks whether user_id on file is the same as executing user |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function userIsOwner() |
57
|
|
|
{ |
58
|
|
|
return (bool) ($this->userid == $this->node->user()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Checks whether file is readable for user |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function userCanRead() |
67
|
|
|
{ |
68
|
|
|
return $this->userIsOwner() && ($this->node->mode() & self::MODE_USER_READ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks whether file is writable for user |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function userCanWrite() |
77
|
|
|
{ |
78
|
|
|
return $this->userIsOwner() && ($this->node->mode() & self::MODE_USER_WRITE); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks whether group_id on file is the same as executing user |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function groupIsOwner() |
87
|
|
|
{ |
88
|
|
|
return (bool) ($this->groupid == $this->node->group()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Checks whether file is readable for group |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function groupCanRead() |
97
|
|
|
{ |
98
|
|
|
return $this->groupIsOwner() && ($this->node->mode() & self::MODE_GROUP_READ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Checks whether file is writable for group |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function groupCanWrite() |
107
|
|
|
{ |
108
|
|
|
return $this->groupIsOwner() && ($this->node->mode() & self::MODE_GROUP_WRITE); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Checks whether file is readable for world |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function worldCanRead() |
117
|
|
|
{ |
118
|
|
|
return (bool) ($this->node->mode() & self::MODE_WORLD_READ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Checks whether file is writable for world |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function worldCanWrite() |
127
|
|
|
{ |
128
|
|
|
return (bool) ($this->node->mode() & self::MODE_WORLD_WRITE); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Checks whether file is readable (by user, group or world) |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function isReadable() |
137
|
|
|
{ |
138
|
|
|
return $this->userCanRead() || $this->groupCanRead() || $this->worldCanRead(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Checks whether file is writable (by user, group or world) |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isWritable() |
147
|
|
|
{ |
148
|
|
|
return $this->userCanWrite() || $this->groupCanWrite() || $this->worldCanWrite(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Checks whether userid is 0 - root. |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function userIsRoot() |
157
|
|
|
{ |
158
|
|
|
return $this->userid == 0; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param \VirtualFileSystem\Structure\Node $node |
163
|
|
|
* |
164
|
|
|
* @return PermissionHelper |
165
|
|
|
*/ |
166
|
|
|
public function setNode($node) |
167
|
|
|
{ |
168
|
|
|
$this->node = $node; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|