1 <?php
2
3 namespace splitbrain\PHPArchive;
4
5 6 7 8 9 10 11 12 13
14 class FileInfo
15 {
16
17 protected $isdir = false;
18 protected $path = '';
19 protected $size = 0;
20 protected $csize = 0;
21 protected $mtime = 0;
22 protected $mode = 0664;
23 protected $owner = '';
24 protected $group = '';
25 protected $uid = 0;
26 protected $gid = 0;
27 protected $comment = '';
28
29 30 31 32 33
34 public function __construct($path = '')
35 {
36 $this->mtime = time();
37 $this->setPath($path);
38 }
39
40 41 42 43 44 45 46 47
48 public static function fromPath($path, $as = '')
49 {
50 clearstatcache(false, $path);
51
52 if (!file_exists($path)) {
53 throw new FileInfoException("$path does not exist");
54 }
55
56 $stat = stat($path);
57 $file = new FileInfo();
58
59 $file->setPath($path);
60 $file->setIsdir(is_dir($path));
61 $file->setMode(fileperms($path));
62 $file->setOwner(fileowner($path));
63 $file->setGroup(filegroup($path));
64 $file->setSize(filesize($path));
65 $file->setUid($stat['uid']);
66 $file->setGid($stat['gid']);
67 $file->setMtime($stat['mtime']);
68
69 if ($as) {
70 $file->setPath($as);
71 }
72
73 return $file;
74 }
75
76 77 78
79 public function getSize()
80 {
81 if($this->isdir) return 0;
82 return $this->size;
83 }
84
85 86 87
88 public function setSize($size)
89 {
90 $this->size = $size;
91 }
92
93 94 95
96 public function getCompressedSize()
97 {
98 return $this->csize;
99 }
100
101 102 103
104 public function setCompressedSize($csize)
105 {
106 $this->csize = $csize;
107 }
108
109 110 111
112 public function getMtime()
113 {
114 return $this->mtime;
115 }
116
117 118 119
120 public function setMtime($mtime)
121 {
122 $this->mtime = $mtime;
123 }
124
125 126 127
128 public function getGid()
129 {
130 return $this->gid;
131 }
132
133 134 135
136 public function setGid($gid)
137 {
138 $this->gid = $gid;
139 }
140
141 142 143
144 public function getUid()
145 {
146 return $this->uid;
147 }
148
149 150 151
152 public function setUid($uid)
153 {
154 $this->uid = $uid;
155 }
156
157 158 159
160 public function getComment()
161 {
162 return $this->comment;
163 }
164
165 166 167
168 public function setComment($comment)
169 {
170 $this->comment = $comment;
171 }
172
173 174 175
176 public function getGroup()
177 {
178 return $this->group;
179 }
180
181 182 183
184 public function setGroup($group)
185 {
186 $this->group = $group;
187 }
188
189 190 191
192 public function getIsdir()
193 {
194 return $this->isdir;
195 }
196
197 198 199
200 public function setIsdir($isdir)
201 {
202
203 if ($isdir && $this->mode === 0664) {
204 $this->mode = 0775;
205 }
206 $this->isdir = $isdir;
207 }
208
209 210 211
212 public function getMode()
213 {
214 return $this->mode;
215 }
216
217 218 219
220 public function setMode($mode)
221 {
222 $this->mode = $mode;
223 }
224
225 226 227
228 public function getOwner()
229 {
230 return $this->owner;
231 }
232
233 234 235
236 public function setOwner($owner)
237 {
238 $this->owner = $owner;
239 }
240
241 242 243
244 public function getPath()
245 {
246 return $this->path;
247 }
248
249 250 251
252 public function setPath($path)
253 {
254 $this->path = $this->cleanPath($path);
255 }
256
257 258 259 260 261 262
263 protected function cleanPath($path)
264 {
265 $path = str_replace('\\', '/', $path);
266 $path = explode('/', $path);
267 $newpath = array();
268 foreach ($path as $p) {
269 if ($p === '' || $p === '.') {
270 continue;
271 }
272 if ($p === '..') {
273 array_pop($newpath);
274 continue;
275 }
276 array_push($newpath, $p);
277 }
278 return trim(implode('/', $newpath), '/');
279 }
280
281 282 283 284 285 286 287 288 289 290 291
292 public function strip($strip)
293 {
294 $filename = $this->getPath();
295 $striplen = strlen($strip);
296 if (is_int($strip)) {
297
298 $parts = explode('/', $filename);
299 if (!$this->getIsdir()) {
300 $base = array_pop($parts);
301 } else {
302 $base = '';
303 }
304 $filename = join('/', array_slice($parts, $strip));
305 if ($base) {
306 $filename .= "/$base";
307 }
308 } else {
309
310 if (substr($filename, 0, $striplen) == $strip) {
311 $filename = substr($filename, $striplen);
312 }
313 }
314
315 $this->setPath($filename);
316 }
317
318 319 320 321 322 323 324 325 326
327 public function match($include = '', $exclude = '')
328 {
329 $extract = true;
330 if ($include && !preg_match($include, $this->getPath())) {
331 $extract = false;
332 }
333 if ($exclude && preg_match($exclude, $this->getPath())) {
334 $extract = false;
335 }
336
337 return $extract;
338 }
339 }
340
341