Ejemplo n.º 1
0
 /**
  * Returns true or false if the file is accepted or not.
  * 
  * @param MCE_File $file File to grant or deny.
  * @return boolean true or false if the file is accepted or not.
  */
 function accept(&$file)
 {
     $name = $file->getName();
     $absPath = $file->getAbsolutePath();
     $isFile = $file->isFile();
     // Handle exclude folders
     if (is_array($this->_excludeFolders)) {
         foreach ($this->_excludeFolders as $folder) {
             if (strpos($absPath, $folder) != "") {
                 if ($this->_debug) {
                     debug("File denied \"" . $absPath . "\" by \"excludeFolders\".");
                 }
                 return BASIC_FILEFILTER_INVALID_NAME;
             }
         }
     }
     // Handle include folders
     if (is_array($this->_includeFolders)) {
         $state = false;
         foreach ($this->_includeFolders as $folder) {
             if (strpos($absPath, $folder) != "") {
                 $state = true;
                 break;
             }
         }
         if (!$state) {
             if ($this->_debug) {
                 debug("File \"" . $absPath . "\" denied by \"includeFolders\".");
             }
             return BASIC_FILEFILTER_INVALID_NAME;
         }
     }
     // Handle exclude files
     if (is_array($this->_excludeFiles) && $isFile) {
         foreach ($this->_excludeFiles as $fileName) {
             if ($name == $fileName) {
                 if ($this->_debug) {
                     debug("File \"" . $absPath . "\" denied by \"excludeFiles\".");
                 }
                 return BASIC_FILEFILTER_INVALID_NAME;
             }
         }
     }
     // Handle include files
     if (is_array($this->_includeFiles) && $isFile) {
         $state = false;
         foreach ($this->_includeFiles as $fileName) {
             if ($name == $fileName) {
                 $state = true;
                 break;
             }
         }
         if (!$state) {
             if ($this->_debug) {
                 debug("File \"" . $absPath . "\" denied by \"includeFiles\".");
             }
             return BASIC_FILEFILTER_INVALID_NAME;
         }
     }
     // Handle file patterns
     if ($isFile) {
         if ($this->_dirsOnly) {
             if ($this->_debug) {
                 debug("File denied \"" . $absPath . "\" by \"dirsOnly\".");
             }
             return BASIC_FILEFILTER_INVALID_NAME;
         }
         // Handle exclude pattern
         if ($this->_excludeFilePattern && preg_match($this->_excludeFilePattern, $name)) {
             if ($this->_debug) {
                 debug("File \"" . $absPath . "\" denied by \"excludeFilePattern\".");
             }
             return BASIC_FILEFILTER_INVALID_NAME;
         }
         // Handle include pattern
         if ($this->_includeFilePattern && !preg_match($this->_includeFilePattern, $name)) {
             if ($this->_debug) {
                 debug("File \"" . $absPath . "\" denied by \"includeFilePattern\".");
             }
             return BASIC_FILEFILTER_INVALID_NAME;
         }
     } else {
         if ($this->_filesOnly) {
             if ($this->_debug) {
                 debug("Dir denied \"" . $absPath . "\" by \"filesOnly\".");
             }
             return BASIC_FILEFILTER_INVALID_NAME;
         }
         // Handle exclude pattern
         if ($this->_excludeDirectoryPattern && preg_match($this->_excludeDirectoryPattern, $name)) {
             if ($this->_debug) {
                 debug("File \"" . $absPath . "\" denied by \"excludeDirectoryPattern\".");
             }
             return BASIC_FILEFILTER_INVALID_NAME;
         }
         // Handle include pattern
         if ($this->_includeDirectoryPattern && !preg_match($this->_includeDirectoryPattern, $name)) {
             if ($this->_debug) {
                 debug("File \"" . $absPath . "\" denied by \"includeDirectoryPattern\".");
             }
             return BASIC_FILEFILTER_INVALID_NAME;
         }
     }
     // Handle include wildcard pattern
     if ($this->_includeWildcardPattern && !$this->_fnmatch($this->_includeWildcardPattern, $name)) {
         if ($this->_debug) {
             debug("File \"" . $absPath . "\" denied by \"includeWildcardPattern\".");
         }
         return BASIC_FILEFILTER_INVALID_NAME;
     }
     // Handle exclude wildcard pattern
     if ($this->_excludeWildcardPattern && $this->_fnmatch($this->_excludeWildcardPattern, $name)) {
         if ($this->_debug) {
             debug("File \"" . $absPath . "\" denied by \"excludeWildcardPattern\".");
         }
         return BASIC_FILEFILTER_INVALID_NAME;
     }
     // Handle file exntetion pattern
     if (is_array($this->_extensions) && $isFile) {
         $ar = explode('.', $absPath);
         $ext = strtolower(array_pop($ar));
         $valid = false;
         foreach ($this->_extensions as $extension) {
             if ($extension == $ext) {
                 $valid = true;
                 break;
             }
         }
         if (!$valid) {
             return BASIC_FILEFILTER_INVALID_EXTENSION;
         }
     }
     return BASIC_FILEFILTER_ACCEPTED;
 }
Ejemplo n.º 2
0
 /**
  * Handles a file instance while looping an tree of directories.
  * 
  * @param MCE_File $file File object reference
  * @param int $level Current level of tree parse
  * @return int State of what to do next can be CONTINUE, ABORT or ABORTFOLDER.
  */
 function handle($file, $level)
 {
     if ($file->isDirectory() || !is_array($this->_config)) {
         if ($level == 0) {
             return parent::handle($file, $level);
         } else {
             $parentFile = $file->getParentFile();
         }
         $this->_config = $parentFile->getConfig();
     }
     $filter = new Moxiecode_BasicFileFilter();
     $filter->setIncludeFilePattern($this->_config['filesystem.include_file_pattern']);
     $filter->setExcludeFilePattern($this->_config['filesystem.exclude_file_pattern']);
     $filter->setIncludeDirectoryPattern($this->_config['filesystem.include_directory_pattern']);
     $filter->setExcludeDirectoryPattern($this->_config['filesystem.exclude_directory_pattern']);
     $filter->setOnlyDirs($this->_onlyDirs);
     if (!$filter->accept($file)) {
         return $this->ABORT_FOLDER;
     }
     return parent::handle($file, $level);
 }