Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationMain
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cApiPathresolveCacheHelper
  • cArray
  • cArticleCollector
  • cDirHandler
  • cFileHandler
  • cHTMLInputSelectElement
  • cIterator
  • cString
  • UI_Config_Table
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the the static file handler class.
  4:  *
  5:  * @package Core
  6:  * @subpackage Util
  7:  * @version SVN Revision $Rev:$
  8:  *
  9:  * @author Mischa Holz
 10:  * @copyright four for business AG <www.4fb.de>
 11:  * @license http://www.contenido.org/license/LIZENZ.txt
 12:  * @link http://www.4fb.de
 13:  * @link http://www.contenido.org
 14:  */
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * Class for file handling.
 19:  * Provides functions for dealing with files.
 20:  *
 21:  * @package Core
 22:  * @subpackage Util
 23:  */
 24: class cFileHandler {
 25: 
 26:     /**
 27:      * Creates a new file
 28:      *
 29:      * @param string $filename the name and path of the new file
 30:      * @param string $content optional content of the new file. Optional.
 31:      * @return bool true on success. Otherwise false.
 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:      * Reads bytes from a file
 44:      *
 45:      * @param string $filename the name and path of the file
 46:      * @param int $length the number of bytes to read. Optional.
 47:      * @param int $offset this will be the first byte which is read. Optional.
 48:      * @param bool $reverse if true, the function will start from the back of
 49:      *        the file. Optional.
 50:      * @throws cInvalidArgumentException if the file with the given filename
 51:      *         does not exist
 52:      * @return string bool success it returns the bytes which have been read.
 53:      *         Otherwise false.
 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:      * Reads a file line by line
 75:      *
 76:      * @param string $filename the name and path of the file
 77:      * @param int $lines the number of lines to be read. Optional.
 78:      * @param int $lineoffset this will be the first line which is read.
 79:      *        Optional.
 80:      * @throws cInvalidArgumentException if the file with the given filename
 81:      *         does not exist
 82:      * @return string array bool one line was read the function will return it.
 83:      *         If more than one line was read the function will return an array
 84:      *         containing the lines. Otherwise false is returned
 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:      * Writes data to a file
127:      *
128:      * @param string $filename the name and path of the file
129:      * @param string $content the data which should be written
130:      * @param bool $append if true the data will be appended to the file.
131:      *        Optional.
132:      * @return bool true on success, false otherwise
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:      * Writes a line to a file (this is similar to
150:      * cFileHandler::write($filename, $data."\n", $apppend)
151:      *
152:      * @see cFileHandler::write($filename, $content, $append)
153:      * @param string $filename the name and path to the file
154:      * @param string $content the data of the line
155:      * @param bool $append if true the data will be appended to file. Optional.
156:      * @return bool true on success, false otherwise
157:      */
158:     public static function writeLine($filename, $content, $append = false) {
159:         return self::write($filename, $content . "\n", $append);
160:     }
161: 
162:     /**
163:      * Checks if a file exists
164:      *
165:      * @param string $filename the name and path of the file
166:      * @return bool true if the file exists
167:      */
168:     public static function exists($filename) {
169:         return file_exists($filename);
170:     }
171: 
172:     /**
173:      * Checks if the file is writable for the PHP user
174:      *
175:      * @param string $filename the name and path of the file
176:      * @return bool true if the file can be written
177:      */
178:     public static function writeable($filename) {
179:         return is_writable($filename);
180:     }
181: 
182:     /**
183:      * Checks if a file is readable for the PHP user
184:      *
185:      * @param string $filename the name and path of the file
186:      * @throws cInvalidArgumentException if the file with the given filename
187:      *         does not exist
188:      * @return bool true if the file is readable
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:      * Checks if a directory is empty
200:      *
201:      * @param string $dir Name of the directory
202:      * @return boolean true if the directory is empty
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:      * Removes a file from the filesystem
223:      *
224:      * @param string $filename the name and path of the file
225:      * @throws cInvalidArgumentException if the file with the given filename
226:      *         does not exist
227:      * @return bool true on success
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:      * Truncates a file so that it is empty
239:      *
240:      * @param string $filename the name and path of the file
241:      * @throws cInvalidArgumentException if the file with the given filename
242:      *         does not exist
243:      * @return bool true on success
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:      * Moves a file
259:      *
260:      * @param string $filename the name of the source file
261:      * @param string $destination the destination. Note that the file can also
262:      *        be renamed in the process of moving it
263:      * @throws cInvalidArgumentException if the file with the given filename
264:      *         does not exist
265:      * @return bool true on success
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:      * Renames a file
281:      *
282:      * @param string $filename the name and path of the file
283:      * @param string $new_filename the new name of the file
284:      * @throws cInvalidArgumentException if the file with the given filename
285:      *         does not exist
286:      * @return bool true on success
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:      * Copies a file
302:      *
303:      * @param string $filename the name and path of the file
304:      * @param string $destination the destination. Note that existing files get
305:      *        overwritten
306:      * @throws cInvalidArgumentException if the file with the given filename
307:      *         does not exist
308:      * @return bool true on success
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:      * Copies a directory and all of its subfolders.
324:      *
325:      * @param string $filename the name and path of the file
326:      * @param string $destination the destination. Note that existing files get
327:      *        overwritten
328:      * @throws cInvalidArgumentException if the file with the given filename
329:      *         does not exist
330:      * @return bool true on success
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:             // workaround for RecursiveDirectoryIterator::SKIP_DOTS, this was not available in PHP 5.2
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:      * Changes the file permissions
374:      *
375:      * @param string $filename the name and path of the file
376:      * @param int $mode the new access mode : php chmod needs octal value
377:      * @throws cInvalidArgumentException if the file with the given filename
378:      *         does not exist
379:      * @return bool true on success
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:         // chmod needs octal value for correct execution.
386:         $mode = intval($mode, 8);
387:         return chmod($filename, $mode);
388:     }
389: 
390:     /**
391:      * Returns an array containing information about the file.
392:      * Currently
393:      * following elements are in it: 'size' - the file size (in byte) 'atime' -
394:      * the time the file was last accessed (unix timestamp) 'ctime' - time the
395:      * file was created (unix timestamp) 'mtime' - time the file was last
396:      * modified (unix timestamp) 'perms' - permissions of the file represented
397:      * in 4 octal digits 'extension' - the file extension or '' if there's no
398:      * extension 'mime' - the mime type of the file
399:      *
400:      * @param string $filename the name and path to the file
401:      * @throws cInvalidArgumentException if the file with the given filename
402:      *         does not exist
403:      * @return array Returns an array containing information about the file
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:             // function is deprecated in PHP 5.3
427:             $ret['mime'] = @mime_content_type($filename);
428:         } else if (function_exists('finfo_open')) {
429:             // extension has to be installed seperately in versions prior to 5.3
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:      * Returns the extension of passed filename
441:      *
442:      * @param string $basename
443:      * @return string
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:      * Sets the default file permissions on the given file.
453:      *
454:      * @param string $filename the name of the file
455:      * @return boolean true on success or false on failure
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:      * Sets the default directory permissions on the given directory.
470:      *
471:      * @param string $pathname the name of the directory
472:      * @return boolean true on success or false on failure
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:      * Validates the given filename.
487:      *
488:      * @param string $filename the filename to validate
489:      * @param boolean $notifyAndExitOnFailure if set, function will show a
490:      *        notification and will exit the script
491:      * @return boolean true if the given filename is valid, false otherwise
492:      */
493:     public static function validateFilename($filename, $notifyAndExitOnFailure = true) {
494:         // check if filename only contains valid characters
495:         if (preg_match('/[^a-z0-9._-]/i', $filename)) {
496:             // validation failure...
497:             if ($notifyAndExitOnFailure) {
498:                 // display notification and exit
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:         // check if filename is empty
510:         if (strlen(trim($filename)) == 0) {
511:             // validation failure...
512:             if ($notifyAndExitOnFailure) {
513:                 // display notification and exit
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:      * Deletes a directory and all of its contents.
527:      *
528:      * @param string $dirname the name of the directory which should be deleted
529:      * @return bool true on success or false on failure
530:      */
531:     public static function recursiveRmdir($dirname) {
532:         if ($dirname == '') {
533:             throw new cInvalidArgumentException("Directory name must not be empty.");
534:         }
535: 
536:         // make sure $dirname ends with a slash
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: 
CMS CONTENIDO 4.9.0 API documentation generated by ApiGen 2.8.0