/**
  * Constructs a new file object
  *
  * @param sfFilebasePluginFile $file_name         The name of the stream to open
  * @param sfFilebasePlugin $filebase
  * @param $open_mode         The file open mode
  * @param $use_include_path  Whether to search in include paths
  * @param $context           A stream context
  * @throw RuntimeException   If file cannot be opened (e.g. insufficient
  *                           access rights).
  */
 function __construct(sfFilebasePluginFile $file_name, sfFilebasePlugin $filebase, $open_mode = 'r', $use_include_path = false, $context = null)
 {
     is_resource($context) ? parent::__construct($file_name->getPathname(), $open_mode, $use_include_path, $context) : parent::__construct($file_name->getPathname(), $open_mode, $use_include_path);
     $this->filebase = $filebase;
     $this->file = $file_name;
     $this->setFileClass('sfFilebasePluginFileObject');
     $this->setInfoClass('sfFilebasePluginFile');
 }
 function recurse(sfFilebasePluginFile $source, sfFilebaseDirectory $parent_dir, $file_mode)
 {
     try {
         foreach ($source as $file) {
             if ($file instanceof sfFilebasePluginDirectory) {
                 fwrite(STDOUT, sprintf("\n    Creating directory %s in %s\n", $source->getFilename(), $parent_dir->getFilename()));
                 $node = new sfFilebaseDirectory();
                 $hash = md5(uniqid(rand(), true));
                 $node->setHash($hash);
                 $node->setFilename($file->getFilename());
                 $node->save();
                 $node->getNode()->insertAsLastChildOf($parent_dir);
                 recurse($file, $node, $file_mode);
             } else {
                 fwrite(STDOUT, sprintf("\n    Copying %s to %s\n", $source->getPathname(), $parent_dir->getFilename()));
                 $copy = $file->copy($source->getFilebase()->getPathname(), true);
                 $hash = $hash = md5(uniqid(rand(), true)) . '.' . $copy->getExtension();
                 $node = new sfFilebaseFile();
                 $node->setFilename($copy->getFilename());
                 $node->setHash($hash);
                 $node->save();
                 $node->getNode()->insertAsLastChildOf($parent_dir);
                 $move = $copy->move($hash);
                 $move->chmod($file_mode);
             }
         }
     } catch (Exception $e) {
         throw new Exception((string) $e);
     }
 }
 /**
  * Trys to find the mime type of a file
  * and returns it, otherwise a $default value will
  * be returned (application/octet-stream per default)
  *
  * $file must be an instance of sfFilebasePluginFile
  *
  * @param   sfFilebasePluginFile  $absolute_path
  * @param   string                $default
  * @return  string               $mime_type
  */
 public static function getMimeType(sfFilebasePluginFile $file, $default = 'application/octet-stream', $extension = null)
 {
     $mime_type = false;
     $ext = $extension === null ? $file->getExtension() : $extension;
     // Using file_exists() instead of sfFilebasePluginFile::fileExists()
     // to avoid recursion issue. sfFilebasePluginFile::fileExists()
     // calls sfFilebasePlugin::getFilebaseFile() calls
     // sfFilebaseUtil::getMimeType()...
     if (file_exists($file->getPathname())) {
         if (!$file->isReadable()) {
             throw new sfFilebasePluginException(sprintf('File %s is read protected.', $file->getPathname()));
         }
         // 1st step, check magic mimeinfo
         if (class_exists('finfo')) {
             $finfo = new finfo(FILEINFO_MIME);
             $mime_type = $finfo->file($file->getPathname());
         } elseif (function_exists('mime_content_type')) {
             $mime_type = mime_content_type($file->getPathname());
         }
     }
     if ($mime_type) {
         if (!empty($ext)) {
             $ext_mime_type = self::getMimeByExtension($ext, null);
             if ($ext_mime_type != $mime_type && array_key_exists($mime_type, self::$mime_dependencies) && in_array($ext, self::$mime_dependencies[$mime_type])) {
                 return $ext_mime_type;
             }
         }
         return $mime_type;
     }
     // 3. check extension
     return self::getMimeByExtension($ext, $default);
 }
 /**
  * Trys to find the mime type of a file
  * and returns it, otherwise a $default value will
  * be returned (application/octet-stream per default)
  *
  * If $extension isset to <> null, this extension will
  * be assumed as the given file's native extension (used
  * by file upload manager where the tmp-file has no extension)
  *
  * $file must be an instance of sfFilebasePluginFile
  *
  * @param   sfFilebasePluginFile  $absolute_path
  * @param   string                $default
  * @param   string                $default mime
  * @param   string                $default_extension
  * @return  string               $mime_type
  */
 public static function getMimeType(sfFilebasePluginFile $file, $default = 'application/octet-stream', $extension = null)
 {
     $mime_type = false;
     $ext = $extension === null ? $file->getExtension() : $extension;
     // CALL file_exists() TO AVOID RECURSIONS
     if (file_exists($file->getPathname())) {
         if (!$file->isReadable()) {
             throw new sfFilebasePluginException(sprintf('File %s is read protected.', $file->getPathname()));
         }
         // 1st step, check magic mimeinfo
         if (class_exists('finfo')) {
             $finfo = new finfo(FILEINFO_MIME, '/usr/share/file/magic');
             $mime_type = $finfo->file($file->getPathname());
         }
         if (!$mime_type && function_exists('mime_content_type')) {
             $mime_type = mime_content_type($file->getPathname());
         }
     }
     // COMPARE DETECTED MIME TYPE WITH
     // THE FILE'S EXTENSION BY REGARDING
     // MIME DEPENDENCIES
     if ($mime_type) {
         if (!empty($ext)) {
             $ext_mime_type = self::getMimeByExtension($ext, null);
             if ($ext_mime_type != $mime_type && array_key_exists($mime_type, self::$mime_dependencies) && in_array($ext, self::$mime_dependencies[$mime_type])) {
                 return $ext_mime_type;
             }
         }
         return $mime_type;
     }
     // 3. check extension
     return self::getMimeByExtension($ext, $default);
 }