Source for file jFile.class.php

Documentation is available at jFile.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage utils
  5. @author Laurent Jouanneau
  6. @contributor Christophe Thiriot
  7. @contributor Bastien Jaillot
  8. @contributor Loic Mathaud
  9. @contributor Olivier Demah (#733)
  10. @contributor Cedric (fix bug ticket 56)
  11. @contributor Julien Issler
  12. @copyright   2005-2011 Laurent Jouanneau, 2006 Christophe Thiriot, 2006 Loic Mathaud, 2008 Bastien Jaillot, 2008 Olivier Demah, 2009-2010 Julien Issler
  13. @link        http://www.jelix.org
  14. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  15. */
  16.  
  17.  
  18. /**
  19.  * A class helper to read or create files
  20.  * @package    jelix
  21.  * @subpackage utils
  22.  */
  23. class jFile {
  24.     /**
  25.     * Reads the content of a file.
  26.     * @param string $filename the filename we're gonna read
  27.     * @return string the content of the file. false if cannot read the file
  28.     */
  29.     public static function read ($filename){
  30.         return @file_get_contents ($filenamefalse);
  31.     }
  32.  
  33.     /**
  34.     * Write a file to the disk.
  35.     * This function is heavily based on the way smarty process its own files.
  36.     * Is using a temporary file and then rename the file. We guess the file system will be smarter than us, avoiding a writing / reading
  37.     *  while renaming the file.
  38.     * This method comes from CopixFile class of Copix framework
  39.     * @author     GĂ©rald Croes
  40.     * @copyright  2001-2005 CopixTeam
  41.     * @link http://www.copix.org
  42.     */
  43.     public static function write ($file$data){
  44.         $_dirname dirname($file);
  45.  
  46.         //asking to create the directory structure if needed.
  47.         self::createDir ($_dirname);
  48.  
  49.         if(!@is_writable($_dirname)) {
  50.             // cache_dir not writable, see if it exists
  51.             if(!@is_dir($_dirname)) {
  52.                 throw new jException('jelix~errors.file.directory.notexists'array ($_dirname));
  53.             }
  54.             throw new jException('jelix~errors.file.directory.notwritable'array ($file$_dirname));
  55.         }
  56.  
  57.         // write to tmp file, then rename it to avoid
  58.         // file locking race condition
  59.         $_tmp_file tempnam($_dirname'wrt');
  60.  
  61.         if (!($fd @fopen($_tmp_file'wb'))) {
  62.             $_tmp_file $_dirname '/' uniqid('wrt');
  63.             if (!($fd @fopen($_tmp_file'wb'))) {
  64.                 throw new jException('jelix~errors.file.write.error'array ($file$_tmp_file));
  65.             }
  66.         }
  67.  
  68.         fwrite($fd$data);
  69.         fclose($fd);
  70.  
  71.         // Delete the file if it allready exists (this is needed on Win,
  72.         // because it cannot overwrite files with rename()
  73.         if ($GLOBALS['gJConfig']->isWindows && file_exists($file)) {
  74.             unlink($file);
  75.         }
  76.         rename($_tmp_file$file);
  77.         @chmod($file,  0664);
  78.  
  79.         return true;
  80.     }
  81.  
  82.     /**
  83.     * create a directory
  84.     * It creates also all necessary parent directory
  85.     * @param string $dir the path of the directory
  86.     */
  87.     public static function createDir ($dir){
  88.         // recursive feature on mkdir() is broken with PHP 5.0.4 for Windows
  89.         // so should do own recursion
  90.         if (!file_exists($dir)) {
  91.             self::createDir(dirname($dir));
  92.             mkdir($dir0775);
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Recursive function deleting a directory
  98.      *
  99.      * @param string $path The path of the directory to remove recursively
  100.      * @param boolean $deleteParent If the path must be deleted too
  101.      * @param array $except  filenames and suffix of filename, for files to NOT delete
  102.      * @since 1.0b1
  103.      * @author Loic Mathaud
  104.      * @return boolean true if all the content has been removed
  105.      */
  106.     public static function removeDir($path$deleteParent=true$except=array()) {
  107.  
  108.         if($path == '' || $path == '/' || $path == DIRECTORY_SEPARATOR)
  109.             throw new jException('jelix~errors.file.directory.cannot.remove.fs.root')//see ticket #840
  110.  
  111.         if (!file_exists($path))
  112.             return true;
  113.  
  114.         $allIsDeleted true;
  115.  
  116.         $dir new DirectoryIterator($path);
  117.         foreach ($dir as $dirContent{
  118.             if (count($except)) {
  119.                 // test if the basename matches one of patterns
  120.                 $exception false;
  121.                 foreach($except as $pattern{
  122.                     if ($pattern[0== '*'// for pattern like *.foo
  123.                         if ($dirContent->getBasename(!= $dirContent->getBasename(substr($pattern1))) {
  124.                             $allIsDeleted false;
  125.                             $exception true;
  126.                             break;
  127.                         }
  128.                     }
  129.                     else if ($pattern == $dirContent->getBasename()) {
  130.                         $allIsDeleted false;
  131.                         $exception true;
  132.                         break;
  133.                     }
  134.                 }
  135.                 if ($exception)
  136.                     continue;
  137.             }
  138.             // file deletion
  139.             if ($dirContent->isFile(|| $dirContent->isLink()) {
  140.                 unlink($dirContent->getPathName());
  141.             else {
  142.                 // recursive directory deletion
  143.                 if (!$dirContent->isDot(&& $dirContent->isDir()) {
  144.                     $removed self::removeDir($dirContent->getPathName()true$except);
  145.                     if (!$removed)
  146.                         $allIsDeleted false;
  147.                 }
  148.             }
  149.         }
  150.         unset($dir)// see bug #733
  151.         unset($dirContent);
  152.  
  153.         // removes the parent directory
  154.         if ($deleteParent && $allIsDeleted{
  155.             rmdir($path);
  156.         }
  157.         return $allIsDeleted;
  158.     }
  159.  
  160.     /**
  161.      * get the MIME Type of a file
  162.      *
  163.      * @param string $file The full path of the file
  164.      * @return string the MIME type of the file
  165.      * @since 1.1.6
  166.      */
  167.     public static function getMimeType($file){
  168.         if (function_exists('finfo_open')) {
  169.             $finfo finfo_open(FILEINFO_MIME_TYPE);
  170.             $type finfo_file($finfo$file);
  171.             finfo_close($finfo);
  172.             return $type;
  173.         }
  174.         else if (function_exists('mime_content_type')) {
  175.             return mime_content_type($file);
  176.         }
  177.         else {
  178.             // we know that it is not the ideal way to do it
  179.             // but don't want to spent time and resource to guess
  180.             // it from the file content.
  181.             return self::getMimeTypeFromFilename($file);
  182.         }
  183.     }
  184.  
  185.     /**
  186.      * get the MIME Type of a file, only with its name
  187.      *
  188.      * @param string $fileName the file name
  189.      * @return string the MIME type of the file
  190.      * @since 1.1.10
  191.      */
  192.     public static function getMimeTypeFromFilename($fileName){
  193.         $f explode('.'$fileName);
  194.         $ext strtolower(array_pop($f));
  195.         if (array_key_exists($extself::$mimeTypes)) {
  196.             return self::$mimeTypes[$ext];
  197.         }
  198.         else
  199.             return 'application/octet-stream';
  200.     }
  201.  
  202.     protected static $mimeTypes array(
  203.  
  204.         'txt' => 'text/plain',
  205.         'htm' => 'text/html',
  206.         'html' => 'text/html',
  207.         'xhtml' => 'application/xhtml+xml',
  208.         'xht' => 'application/xhtml+xml',
  209.         'php' => 'text/html',
  210.         'css' => 'text/css',
  211.         'js' => 'application/javascript',
  212.         'json' => 'application/json',
  213.         'xml' => 'application/xml',
  214.         'xslt' => 'application/xslt+xml',
  215.         'xsl' => 'application/xml',
  216.         'dtd' => 'application/xml-dtd',
  217.         'atom'=>'application/atom+xml',
  218.         'mathml'=>'application/mathml+xml',
  219.         'rdf'=>'application/rdf+xml',
  220.         'smi'=>'application/smil',
  221.         'smil'=>'application/smil',
  222.         'vxml'=>'application/voicexml+xml',
  223.         'latex'=>'application/x-latex',
  224.         'tcl'=>'application/x-tcl',
  225.         'tex'=>'application/x-tex',
  226.         'texinfo'=>'application/x-texinfo',
  227.         'wrl'=>'model/vrml',
  228.         'wrml'=>'model/vrml',
  229.         'ics'=>'text/calendar',
  230.         'ifb'=>'text/calendar',
  231.         'sgml'=>'text/sgml',
  232.         'htc'=>'text/x-component',
  233.  
  234.         // images
  235.         'png' => 'image/png',
  236.         'jpe' => 'image/jpeg',
  237.         'jpeg' => 'image/jpeg',
  238.         'jpg' => 'image/jpeg',
  239.         'gif' => 'image/gif',
  240.         'bmp' => 'image/bmp',
  241.         'ico' => 'image/x-icon',
  242.         'tiff' => 'image/tiff',
  243.         'tif' => 'image/tiff',
  244.         'svg' => 'image/svg+xml',
  245.         'svgz' => 'image/svg+xml',
  246.         'djvu' => 'image/vnd.djvu',
  247.         'djv'  => 'image/vnd.djvu',
  248.  
  249.         // archives
  250.         'zip' => 'application/zip',
  251.         'rar' => 'application/x-rar-compressed',
  252.         'exe' => 'application/x-msdownload',
  253.         'msi' => 'application/x-msdownload',
  254.         'cab' => 'application/vnd.ms-cab-compressed',
  255.         'tar' => 'application/x-tar',
  256.         'gz'  => 'application/x-gzip',
  257.         'tgz'  => 'application/x-gzip',
  258.  
  259.         // audio/video
  260.         'mp2' => 'audio/mpeg',
  261.         'mp3' => 'audio/mpeg',
  262.         'qt' => 'video/quicktime',
  263.         'mov' => 'video/quicktime',
  264.         'mpeg' => 'video/mpeg',
  265.         'mpg' => 'video/mpeg',
  266.         'mpe' => 'video/mpeg',
  267.         'wav' => 'audio/wav',
  268.         'aiff' => 'audio/aiff',
  269.         'aif' => 'audio/aiff',
  270.         'avi' => 'video/msvideo',
  271.         'wmv' => 'video/x-ms-wmv',
  272.         'ogg' => 'application/ogg',
  273.         'flv' => 'video/x-flv',
  274.         'dvi' => 'application/x-dvi',
  275.         'au'=> 'audio/basic',
  276.         'snd'=> 'audio/basic',
  277.         'mid' => 'audio/midi',
  278.         'midi' => 'audio/midi',
  279.         'm3u' => 'audio/x-mpegurl',
  280.         'm4u' => 'video/vnd.mpegurl',
  281.         'ram' => 'audio/x-pn-realaudio',
  282.         'ra' => 'audio/x-pn-realaudio',
  283.         'rm' => 'application/vnd.rn-realmedia',
  284.  
  285.         // adobe
  286.         'pdf' => 'application/pdf',
  287.         'psd' => 'image/vnd.adobe.photoshop',
  288.         'ai' => 'application/postscript',
  289.         'eps' => 'application/postscript',
  290.         'ps' => 'application/postscript',
  291.         'swf' => 'application/x-shockwave-flash',
  292.  
  293.         // ms office
  294.         'doc' => 'application/msword',
  295.         'docx' => 'application/msword',
  296.         'rtf' => 'application/rtf',
  297.         'xls' => 'application/vnd.ms-excel',
  298.         'xlm' => 'application/vnd.ms-excel',
  299.         'xla' => 'application/vnd.ms-excel',
  300.         'xld' => 'application/vnd.ms-excel',
  301.         'xlt' => 'application/vnd.ms-excel',
  302.         'xlc' => 'application/vnd.ms-excel',
  303.         'xlw' => 'application/vnd.ms-excel',
  304.         'xll' => 'application/vnd.ms-excel',
  305.         'ppt' => 'application/vnd.ms-powerpoint',
  306.         'pps' => 'application/vnd.ms-powerpoint',
  307.  
  308.         // open office
  309.         'odt' => 'application/vnd.oasis.opendocument.text',
  310.         'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  311.     );
  312. }

Documentation generated on Mon, 19 Sep 2011 14:12:34 +0200 by phpDocumentor 1.4.3