1 : <?php
2 : /**
3 : * Base stream contents container.
4 : *
5 : * @package bovigo_vfs
6 : * @version $Id: vfsStreamAbstractContent.php 214 2010-10-07 20:57:57Z google@frankkleine.de $
7 : */
8 : /**
9 : * @ignore
10 : */
11 : require_once dirname(__FILE__) . '/vfsStreamContent.php';
12 : /**
13 : * Base stream contents container.
14 : *
15 : * @package bovigo_vfs
16 : */
17 : abstract class vfsStreamAbstractContent implements vfsStreamContent
18 : {
19 : /**
20 : * name of the container
21 : *
22 : * @var string
23 : */
24 : protected $name;
25 : /**
26 : * type of the container
27 : *
28 : * @var string
29 : */
30 : protected $type;
31 : /**
32 : * timestamp of last modification
33 : *
34 : * @var int
35 : */
36 : protected $lastModified;
37 : /**
38 : * permissions for content
39 : *
40 : * @var int
41 : */
42 : protected $permissions;
43 : /**
44 : * owner of the file
45 : *
46 : * @var int
47 : */
48 : protected $user;
49 : /**
50 : * owner group of the file
51 : *
52 : * @var int
53 : */
54 : protected $group;
55 :
56 : /**
57 : * constructor
58 : *
59 : * @param string $name
60 : * @param int $permissions optional
61 : */
62 : public function __construct($name, $permissions = null)
63 : {
64 155 : $this->name = $name;
65 155 : $this->lastModified = time();
66 155 : if (null === $permissions) {
67 129 : $permissions = $this->getDefaultPermissions() & ~vfsStream::umask();
68 129 : }
69 :
70 155 : $this->permissions = $permissions;
71 155 : $this->user = vfsStream::getCurrentUser();
72 155 : $this->group = vfsStream::getCurrentGroup();
73 155 : }
74 :
75 : /**
76 : * returns default permissions for concrete implementation
77 : *
78 : * @return int
79 : * @since 0.8.0
80 : */
81 : protected abstract function getDefaultPermissions();
82 :
83 : /**
84 : * returns the file name of the content
85 : *
86 : * @return string
87 : */
88 : public function getName()
89 : {
90 106 : return $this->name;
91 : }
92 :
93 : /**
94 : * renames the content
95 : *
96 : * @param string $newName
97 : */
98 : public function rename($newName)
99 : {
100 8 : $this->name = $newName;
101 8 : }
102 :
103 : /**
104 : * checks whether the container can be applied to given name
105 : *
106 : * @param string $name
107 : * @return bool
108 : */
109 : public function appliesTo($name)
110 : {
111 81 : if ($name === $this->name) {
112 10 : return true;
113 : }
114 :
115 80 : return (substr($name, 0, strlen($this->name)) === $this->name && strpos($name, '/') !== false);
116 : }
117 :
118 : /**
119 : * returns the type of the container
120 : *
121 : * @return int
122 : */
123 : public function getType()
124 : {
125 79 : return $this->type;
126 : }
127 :
128 : /**
129 : * alias for lastModified()
130 : *
131 : * @param int $filemtime
132 : * @return vfsStreamContent
133 : * @see lastModified()
134 : */
135 : public function setFilemtime($filemtime)
136 : {
137 81 : return $this->lastModified($filemtime);
138 : }
139 :
140 : /**
141 : * sets the last modification time of the stream content
142 : *
143 : * @param int $filemtime
144 : * @return vfsStreamContent
145 : */
146 : public function lastModified($filemtime)
147 : {
148 81 : $this->lastModified = $filemtime;
149 81 : return $this;
150 : }
151 :
152 : /**
153 : * returns the last modification time of the stream content
154 : *
155 : * @return int
156 : */
157 : public function filemtime()
158 : {
159 42 : return $this->lastModified;
160 : }
161 :
162 : /**
163 : * adds content to given container
164 : *
165 : * @param vfsStreamContainer $container
166 : * @return vfsStreamContent
167 : */
168 : public function at(vfsStreamContainer $container)
169 : {
170 31 : $container->addChild($this);
171 31 : return $this;
172 : }
173 :
174 : /**
175 : * change file mode to given permissions
176 : *
177 : * @param int $permissions
178 : * @return vfsStreamContent
179 : */
180 : public function chmod($permissions)
181 : {
182 9 : $this->permissions = $permissions;
183 9 : clearstatcache();
184 9 : return $this;
185 : }
186 :
187 : /**
188 : * returns permissions
189 : *
190 : * @return int
191 : */
192 : public function getPermissions()
193 : {
194 76 : return $this->permissions;
195 : }
196 :
197 : /**
198 : * checks whether content is readable
199 : *
200 : * @param int $user id of user to check for
201 : * @param int $group id of group to check for
202 : * @return bool
203 : */
204 : public function isReadable($user, $group)
205 : {
206 43 : if ($this->user === $user) {
207 43 : $check = 0400;
208 43 : } elseif ($this->group === $group) {
209 22 : $check = 0040;
210 22 : } else {
211 22 : $check = 0004;
212 : }
213 :
214 43 : return (bool) ($this->permissions & $check);
215 : }
216 :
217 : /**
218 : * checks whether content is writable
219 : *
220 : * @param int $user id of user to check for
221 : * @param int $group id of group to check for
222 : * @return bool
223 : */
224 : public function isWritable($user, $group)
225 : {
226 58 : if ($this->user === $user) {
227 57 : $check = 0200;
228 58 : } elseif ($this->group === $group) {
229 22 : $check = 0020;
230 22 : } else {
231 23 : $check = 0002;
232 : }
233 :
234 58 : return (bool) ($this->permissions & $check);
235 : }
236 :
237 : /**
238 : * checks whether content is executable
239 : *
240 : * @param int $user id of user to check for
241 : * @param int $group id of group to check for
242 : * @return bool
243 : */
244 : public function isExecutable($user, $group)
245 : {
246 22 : if ($this->user === $user) {
247 22 : $check = 0100;
248 22 : } elseif ($this->group === $group) {
249 22 : $check = 0010;
250 22 : } else {
251 22 : $check = 0001;
252 : }
253 :
254 22 : return (bool) ($this->permissions & $check);
255 : }
256 :
257 : /**
258 : * change owner of file to given user
259 : *
260 : * @param int $user
261 : * @return vfsStreamContent
262 : */
263 : public function chown($user)
264 : {
265 4 : $this->user = $user;
266 4 : return $this;
267 : }
268 :
269 : /**
270 : * checks whether file is owned by given user
271 : *
272 : * @param int $user
273 : * @return bool
274 : */
275 : public function isOwnedByUser($user)
276 : {
277 2 : return $this->user === $user;
278 : }
279 :
280 : /**
281 : * returns owner of file
282 : *
283 : * @return int
284 : */
285 : public function getUser()
286 : {
287 44 : return $this->user;
288 : }
289 :
290 : /**
291 : * change owner group of file to given group
292 : *
293 : * @param int $group
294 : * @return vfsStreamContent
295 : */
296 : public function chgrp($group)
297 : {
298 4 : $this->group = $group;
299 4 : return $this;
300 : }
301 :
302 : /**
303 : * checks whether file is owned by group
304 : *
305 : * @param int $group
306 : * @return bool
307 : */
308 : public function isOwnedByGroup($group)
309 : {
310 2 : return $this->group === $group;
311 : }
312 :
313 : /**
314 : * returns owner group of file
315 : *
316 : * @return int
317 : */
318 : public function getGroup()
319 : {
320 44 : return $this->group;
321 : }
322 : }
|