1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Filesystem
13: {
14: public static $error = '';
15:
16:
17: public static $useftp = FALSE;
18:
19:
20: public static $host = 'localhost';
21:
22:
23: public static $user = 'annonymous';
24:
25:
26: public static $password = 'annonymous@example.com';
27:
28:
29: public static $root = 'public_html';
30:
31:
32: private static $connection = NULL;
33:
34:
35: public static function init($settings)
36: {
37: if (empty($settings) === FALSE) {
38: self::$useftp = TRUE;
39: foreach ($settings as $key => $value) {
40: self::$$key = $value;
41: }
42: }
43: }
44:
45:
46: private static function connect()
47: {
48: if (self::$useftp === FALSE) {
49: return TRUE;
50: }
51:
52: if (empty(self::$connection) === FALSE) {
53: return TRUE;
54: }
55:
56: if (FALSE === self::$connection = ftp_connect(self::$host)) {
57: return FALSE;
58: }
59:
60: if (FALSE === ftp_login(self::$connection, self::$user, self::$password)) {
61: ftp_close(self::$connection);
62: return FALSE;
63: }
64:
65: return TRUE;
66: }
67:
68:
69: public static function disconnect()
70: {
71: if (empty(self::$connection) === TRUE) {
72: return TRUE;
73: }
74:
75: if (FALSE === ftp_close(self::$connection)) {
76: return FALSE;
77: }
78:
79: self::$connection = NULL;
80: return TRUE;
81: }
82:
83:
84:
85: public static function ls($dir, $filter = NULL, $target = 'files', $info = FALSE)
86: {
87: $dir = WWW_DIR . self::prepare_path($dir);
88:
89: if (is_readable($dir) === FALSE) {
90: return FALSE;
91: }
92:
93: $dirs = array();
94: $files = array();
95:
96: if (is_string($filter) === TRUE) {
97: $filter = array($filter);
98: }
99:
100: $content = scandir($dir);
101:
102: foreach ($content as $name) {
103: if ($name === '.'
104: OR $name === '..') {
105:
106: continue;
107: }
108:
109: if (is_dir($dir.'/'.$name) === TRUE) {
110: if ($info === TRUE) {
111: $dirs[$name] = array('name' => $name);
112:
113: $stat = stat($dir.'/'.$name);
114: $dirs[$name]['uid'] = $stat['uid'];
115: $dirs[$name]['gid'] = $stat['gid'];
116: $dirs[$name]['mtime'] = $stat['mtime'];
117: $dirs[$name]['atime'] = $stat['atime'];
118: $dirs[$name]['perms'] = substr(sprintf('%o', fileperms($dir . '/' . $name)), -4);
119:
120: } else {
121: $dirs[] = $name;
122: }
123:
124: } else {
125: $extension = pathinfo($name, PATHINFO_EXTENSION);
126: $extension = strtolower($extension);
127:
128: if ($filter === NULL
129: OR (is_array($filter) === TRUE
130: AND in_array($extension, $filter) === TRUE)) {
131:
132: if ($info === TRUE) {
133: $files[$name] = array(
134: 'name' => $name,
135: 'type' => $extension
136: );
137:
138: $stat = stat($dir.'/'.$name);
139: $files[$name]['uid'] = $stat['uid'];
140: $files[$name]['gid'] = $stat['gid'];
141: $files[$name]['size'] = $stat['size'];
142: $files[$name]['mtime'] = $stat['mtime'];
143: $files[$name]['atime'] = $stat['atime'];
144: $files[$name]['perms'] = substr(sprintf('%o', fileperms($dir . '/' . $name)), -4);
145:
146: } else {
147: $files[] = $name;
148: }
149: }
150: }
151: }
152:
153: $keys = array_keys($files);
154: natsort($keys);
155: $filesTMP = $files;
156: $files = array();
157: foreach ($keys as $key) {
158: $files[$key] = $filesTMP[$key];
159: }
160:
161: $keys = array_keys($dirs);
162: natsort($keys);
163: $dirsTMP = $dirs;
164: $dirs = array();
165: foreach ($keys as $key) {
166: $dirs[$key] = $dirsTMP[$key];
167: }
168:
169: if ($target === 'all') {
170: return array('dirs' => $dirs, 'files' => $files);
171: }
172:
173: if ($target === 'dirs') {
174: return $dirs;
175: }
176:
177: return $files;
178: }
179:
180:
181: public static function cp($source, $target)
182: {
183: if (is_readable($source) === FALSE) {
184: return FALSE;
185: }
186:
187: $target = self::prepare_path($target);
188:
189: if (self::$useftp === FALSE) {
190: if (copy(self::$root . $source, self::$root . $target) === FALSE) {
191: return FALSE;
192: }
193:
194: if (chmod(self::$root . $target, 0666) === FALSE) {
195: return FALSE;
196: }
197:
198: return TRUE;
199:
200: } else {
201: if (self::connect() === FALSE) {
202: return FALSE;
203: }
204:
205: if (is_dir('.' . $source) === FALSE) {
206: $target = rtrim(self::prepare_path($target), '/');
207: if (FALSE === ftp_put(self::$connection, self::$root.$target, $source, FTP_BINARY)) {
208: return FALSE;
209: }
210:
211: if (FALSE === ftp_chmod(self::$connection, 0777, self::$root.$target)) {
212: return FALSE;
213: }
214:
215: return TRUE;
216: }
217: }
218:
219: if (FALSE === self::mkdir($target)) {
220: return FALSE;
221: }
222:
223: $content = self::ls($source, NULL, 'all');
224: $content = $content['dirs'] + $content['files'];
225:
226: foreach ($content as $name => $properties) {
227: if (FALSE === self::cp($source . '/' . $name, $target . '/' . $name)) {
228: return FALSE;
229: }
230: }
231:
232: return TRUE;
233: }
234:
235:
236: public static function mv($source, $target)
237: {
238: if (FALSE === self::cp($source, $target)) {
239: return FALSE;
240: }
241:
242: return self::rm($source, TRUE);
243: }
244:
245:
246: public static function rename($source, $target)
247: {
248: if (is_readable($source) === FALSE) {
249: return FALSE;
250: }
251:
252: $source = trim($source, '.');
253: $source = trim($source, '/');
254:
255: if (self::$useftp === FALSE) {
256: if (FALSE == rename(self::$root . $source, self::$root . $target)) {
257: return FALSE;
258: }
259:
260: } else {
261: if (self::connect() === FALSE) {
262: return FALSE;
263: }
264:
265: if (FALSE === ftp_rename(self::$connection, self::$root . $source, self::$root . $target)) {
266: return FALSE;
267: }
268: }
269:
270: return TRUE;
271: }
272:
273:
274: public static function rm($target, $recursive = FALSE, $type = NULL)
275: {
276: if ($type === NULL) {
277: if (FALSE === $type = self::file_exists($target)) {
278: return TRUE;
279: }
280: }
281:
282: if ($type === 'file') {
283: $target = rtrim(self::prepare_path($target), '/');
284:
285: if (self::$useftp === FALSE) {
286: if (FALSE === unlink(self::$root . $target)) {
287: return FALSE;
288: }
289:
290: } else {
291: if (self::connect() === FALSE) {
292: return FALSE;
293: }
294:
295: if (FALSE === ftp_delete(self::$connection, self::$root . $target)) {
296: return FALSE;
297: }
298: }
299:
300: return TRUE;
301: }
302:
303: $target = self::prepare_path($target);
304:
305: $content = self::ls($target, NULL, 'all');
306:
307: if ($recursive === FALSE
308: AND (empty($content['dirs']) === FALSE
309: OR empty($content['files']) === FALSE)) {
310:
311: return FALSE;
312: }
313:
314: foreach ($content['files'] as $file) {
315: if (FALSE === self::rm($target . $file, $recursive, 'file')) {
316: return FALSE;
317: }
318: }
319:
320: foreach ($content['dirs'] as $dir) {
321: if (FALSE === self::rm($target . $dir, $recursive, 'dir')) {
322: die($target.$dir);
323: return FALSE;
324: }
325: }
326:
327: if (self::$useftp === FALSE) {
328: if (FALSE === rmdir(self::$root . $target)) {
329: return FALSE;
330: }
331:
332: } else {
333: if (self::connect() === FALSE) {
334: return FALSE;
335: }
336:
337: if (FALSE === ftp_rmdir(self::$connection, self::$root . $target)) {
338: return FALSE;
339: }
340: }
341:
342: return TRUE;
343: }
344:
345:
346: public static function mkdir($dir, $mode = 0777)
347: {
348: $dir = self::prepare_path($dir);
349:
350: $sub_dirs = explode('/', trim($dir, '/'));
351:
352: $dir = '';
353: foreach ($sub_dirs as $sub_dir) {
354: $dir .= '/' . $sub_dir;
355:
356: if (file_exists('.' . $dir) === FALSE) {
357: if (self::$useftp == FALSE) {
358: if (FALSE === mkdir(self::$root . $dir)) {
359: return FALSE;
360: }
361:
362: if (FALSE === chmod(self::$root . $dir, $mode)) {
363: return FALSE;
364: }
365:
366: } else {
367: if (self::connect() === FALSE) {
368: return FALSE;
369: }
370:
371: if (FALSE === @ftp_mkdir(self::$connection, self::$root . $dir)) {
372: return FALSE;
373: }
374:
375: if (FALSE === ftp_chmod(self::$connection, $mode, self::$root . $dir)) {
376: return FALSE;
377: }
378: }
379: }
380: }
381:
382: return TRUE;
383: }
384:
385:
386: public static function chmod($target, $mode, $recursive = FALSE)
387: {
388: $dir_mode = $mode;
389: $file_mode = $mode;
390:
391: if (is_array($mode) === TRUE) {
392: $dir_mode = $mode['dir'];
393: $file_mode = $mode['file'];
394: $recursive = TRUE;
395: }
396:
397: if (FALSE === $type = self::file_exists($target)) {
398: return FALSE;
399: }
400:
401: if ($type === 'file') {
402: $target = self::$root.$target;
403: if (self::$useftp === FALSE) {
404: if (chmod(self::$root . $target, $file_mode) == FALSE) {
405: return FALSE;
406: }
407:
408: } else {
409: if (self::connect() === FALSE) {
410: return FALSE;
411: }
412:
413: if (ftp_chmod(self::$connection, $file_mode, self::$root . $target) === FALSE) {
414: return FALSE;
415: }
416: }
417:
418: return TRUE;
419: }
420:
421: $target = trim($target, '.');
422: $target = trim($target, '/') . '/';
423:
424: $content = $this->ls($target, NULL, 'all');
425:
426: foreach ($content['dirs'] as $dir => $properties) {
427: if (FALSE === $this->chmod($target . $dir, $mode, TRUE)) {
428: return FALSE;
429: }
430: }
431:
432: foreach ($content['files'] as $file => $properties) {
433: if (FALSE === ftp_chmod(self::$connection, $file_mode, self::$root . $target . $file)) {
434: return FALSE;
435: }
436: }
437:
438: if (FALSE === ftp_chmod(self::$connection, $dir_mode, self::$root . $target)) {
439: return FALSE;
440: }
441:
442: return TRUE;
443: }
444:
445:
446: public static function file_exists($file)
447: {
448: if (file_exists($file) === FALSE) {
449: return FALSE;
450: }
451:
452: if (is_dir($file) == TRUE) {
453: return 'dir';
454: }
455:
456: return 'file';
457: }
458:
459:
460: public static function prepare_path($path)
461: {
462: $path = trim($path, '.');
463: $path = str_replace(WWW_DIR . '/', '', $path);
464: return '/'.trim($path, '/').'/';
465: }
466: }