1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23:
24: class cFileHandler {
25:
26: 27: 28: 29: 30: 31: 32:
33: public static function create($filename, $content = '') {
34: $success = file_put_contents($filename, $content) === strlen($content);
35: if ($success) {
36: self::setDefaultFilePerms($filename);
37: }
38:
39: return $success;
40: }
41:
42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54:
55: public static function read($filename, $length = 0, $offset = 0, $reverse = false) {
56: if (!cFileHandler::exists($filename)) {
57: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
58: }
59:
60: if ($reverse) {
61: return file_get_contents($filename, false, null, filesize($filename) - $length - $offset, $length);
62: } else if ($length > 0 && $offset == 0) {
63: return file_get_contents($filename, false, null, 0, $length);
64: } else if ($offset > 0 && $length == 0) {
65: return file_get_contents($filename, false, null, $offset);
66: } else if ($offset > 0 && $length > 0) {
67: return file_get_contents($filename, false, null, $offset, $length);
68: } else {
69: return file_get_contents($filename);
70: }
71: }
72:
73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
86: public static function readLine($filename, $lines = 0, $lineoffset = 0) {
87: if (!cFileHandler::exists($filename)) {
88: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
89: }
90:
91: $f = fopen($filename, 'r');
92:
93: if ($f === false) {
94: fclose($f);
95: return false;
96: }
97: if ($lines == 0) {
98: $lines = 1;
99: }
100:
101: for ($i = 0; $i < $lineoffset; $i++) {
102: $waste = fgets($f);
103: }
104:
105: $ret = null;
106: if ($lines > 1) {
107: $ret = array();
108: for ($i = 0; $i < $lines; $i++) {
109: $temp = fgets($f);
110: if ($temp === false) {
111: fclose($f);
112: return false;
113: }
114: $ret[] = substr($temp, 0, strlen($temp) - 1);
115: }
116: } else {
117: $ret = fgets($f);
118: $ret = substr($ret, 0, strlen($ret) - 1);
119: }
120:
121: fclose($f);
122: return $ret;
123: }
124:
125: 126: 127: 128: 129: 130: 131: 132: 133:
134: public static function write($filename, $content, $append = false) {
135: $flag = 0;
136: if ($append) {
137: $flag = FILE_APPEND;
138: }
139:
140: $success = file_put_contents($filename, $content, $flag);
141: if ((int) $success != 0) {
142: self::setDefaultFilePerms($filename);
143: }
144:
145: return $success;
146: }
147:
148: 149: 150: 151: 152: 153: 154: 155: 156: 157:
158: public static function writeLine($filename, $content, $append = false) {
159: return self::write($filename, $content . "\n", $append);
160: }
161:
162: 163: 164: 165: 166: 167:
168: public static function exists($filename) {
169: return file_exists($filename);
170: }
171:
172: 173: 174: 175: 176: 177:
178: public static function writeable($filename) {
179: return is_writable($filename);
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: public static function readable($filename) {
191: if (!cFileHandler::exists($filename)) {
192: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
193: }
194:
195: return is_readable($filename);
196: }
197:
198: 199: 200: 201: 202: 203:
204: public static function isDirectoryEmpty($dir) {
205: if (!is_readable($dir)) {
206: return false;
207: }
208: if(is_dir($dir)){
209: if ($handle = opendir($dir)) {
210: while (false !== ($entry = readdir($handle))) {
211: if ($entry != "." && $entry != "..") {
212: return false;
213: }
214: }
215: }
216: closedir($handle);
217: }
218: return true;
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228:
229: public static function remove($filename) {
230: if (!cFileHandler::exists($filename)) {
231: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
232: }
233:
234: return unlink($filename);
235: }
236:
237: 238: 239: 240: 241: 242: 243: 244:
245: public static function truncate($filename) {
246: if (!cFileHandler::exists($filename)) {
247: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
248: }
249: $success = file_put_contents($filename, '') === 0;
250: if ($success) {
251: self::setDefaultFilePerms($filename);
252: }
253:
254: return $success;
255: }
256:
257: 258: 259: 260: 261: 262: 263: 264: 265: 266:
267: public static function move($filename, $destination) {
268: if (!cFileHandler::exists($filename)) {
269: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
270: }
271: $success = rename($filename, $destination);
272: if ($success) {
273: self::setDefaultFilePerms($destination);
274: }
275:
276: return $success;
277: }
278:
279: 280: 281: 282: 283: 284: 285: 286: 287:
288: public static function rename($filename, $new_filename) {
289: if (!cFileHandler::exists($filename)) {
290: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
291: }
292: $success = rename($filename, dirname($filename) . '/' . $new_filename);
293: if ($success) {
294: self::setDefaultFilePerms(dirname($filename) . '/' . $new_filename);
295: }
296:
297: return $success;
298: }
299:
300: 301: 302: 303: 304: 305: 306: 307: 308: 309:
310: public static function copy($filename, $destination) {
311: if (!cFileHandler::exists($filename)) {
312: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
313: }
314: $success = copy($filename, $destination);
315: if ($success) {
316: self::setDefaultFilePerms($destination);
317: }
318:
319: return $success;
320: }
321:
322: 323: 324: 325: 326: 327: 328: 329: 330: 331:
332: public static function recursiveCopy($filename, $destination) {
333: if (!cFileHandler::exists($filename)) {
334: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
335: }
336:
337: if (!cFileHandler::exists($destination)) {
338: if (!mkdir($destination)) {
339: return false;
340: }
341: if (!self::chmod($destination, "777")) {
342: return false;
343: }
344: }
345:
346: foreach ($iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($filename), RecursiveIteratorIterator::SELF_FIRST) as $item) {
347:
348: if ($item->getFilename() == '.' || $item->getFilename() == '..') {
349: continue;
350: }
351:
352: if ($item->isDir()) {
353: if (!mkdir($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName())) {
354: return false;
355: }
356: if (!self::chmod($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), "777")) {
357: return false;
358: }
359: } else {
360: if (!copy($item, $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName())) {
361: return false;
362: }
363: if (!self::chmod($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), "777")) {
364: return false;
365: }
366: }
367: }
368:
369: return true;
370: }
371:
372: 373: 374: 375: 376: 377: 378: 379: 380:
381: public static function chmod($filename, $mode) {
382: if (!cFileHandler::exists($filename)) {
383: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
384: }
385:
386: $mode = intval($mode, 8);
387: return chmod($filename, $mode);
388: }
389:
390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404:
405: public static function info($filename) {
406: if (!cFileHandler::exists($filename)) {
407: throw new cInvalidArgumentException('The file ' . $filename . ' could not be accessed because it does not exist.');
408: }
409:
410: $ret = array();
411:
412: $ret['size'] = @filesize($filename);
413: $ret['atime'] = @fileatime($filename);
414: $ret['ctime'] = @filectime($filename);
415: $ret['mtime'] = @filemtime($filename);
416:
417: $temp = @decoct(fileperms($filename));
418: $ret['perms'] = substr($temp, strlen($temp) - 4);
419:
420: $ret['extension'] = substr(basename($filename), (int) strrpos(basename($filename), '.') + 1);
421: if ($ret['extension'] == basename($filename)) {
422: $ret['extension'] = '';
423: }
424:
425: if (version_compare(PHP_VERSION, '5.3', '<')) {
426:
427: $ret['mime'] = @mime_content_type($filename);
428: } else if (function_exists('finfo_open')) {
429:
430: $finfo = @finfo_open(FILEINFO_MIME_TYPE);
431: $ret['mime'] = @finfo_file($finfo, $filename);
432: } else {
433: $ret['mime'] = '';
434: }
435:
436: return $ret;
437: }
438:
439: 440: 441: 442: 443: 444:
445: public static function getExtension($basename) {
446: $aFileName = explode('.', trim($basename, '.'));
447:
448: return (count($aFileName) > 1)? $aFileName[count($aFileName) - 1] : '';
449: }
450:
451: 452: 453: 454: 455: 456:
457: public static function setDefaultFilePerms($filename) {
458: $cfg = cRegistry::getConfig();
459:
460: if (isset($cfg['default_perms']['file']) === false) {
461: return false;
462: }
463:
464: $filePerms = $cfg['default_perms']['file'];
465: return cFileHandler::chmod($filename, $filePerms);
466: }
467:
468: 469: 470: 471: 472: 473:
474: public static function setDefaultDirPerms($pathname) {
475: $cfg = cRegistry::getConfig();
476:
477: if (isset($cfg['default_perms']['directory']) === false) {
478: return false;
479: }
480:
481: $dirPerms = $cfg['default_perms']['directory'];
482: return cFileHandler::chmod($pathname, $dirPerms);
483: }
484:
485: 486: 487: 488: 489: 490: 491: 492:
493: public static function validateFilename($filename, $notifyAndExitOnFailure = true) {
494:
495: if (preg_match('/[^a-z0-9._-]/i', $filename)) {
496:
497: if ($notifyAndExitOnFailure) {
498:
499: cRegistry::addErrorMessage(i18n('Wrong file name.'));
500: $page = new cGuiPage('generic_page');
501: $page->abortRendering();
502: $page->render();
503: exit();
504: }
505:
506: return false;
507: }
508:
509:
510: if (strlen(trim($filename)) == 0) {
511:
512: if ($notifyAndExitOnFailure) {
513:
514: $notification = new cGuiNotification();
515: $notification->displayNotification("error", i18n("Please insert file name."));
516: exit();
517: }
518:
519: return false;
520: }
521:
522: return true;
523: }
524:
525: 526: 527: 528: 529: 530:
531: public static function recursiveRmdir($dirname) {
532: if ($dirname == '') {
533: throw new cInvalidArgumentException("Directory name must not be empty.");
534: }
535:
536:
537: if (substr($dirname, -1) !== '/') {
538: $dirname .= '/';
539: }
540:
541: foreach (new DirectoryIterator($dirname) as $file) {
542:
543: if ($file != "." && $file != "..") {
544:
545: $file = $dirname . $file;
546: if (is_dir($file)) {
547: self::recursiveRmdir($file);
548: } else {
549: unlink($file);
550: }
551: }
552: }
553:
554: return rmdir($dirname);
555: }
556:
557: }
558: