/**
  * Returns a config based on the specified file.
  *
  * @param MOXMAN_Vfs_IFile $file File to get the config for.
  * @return MOXMAN_Util_Config Config for the specified file.
  */
 public function getConfig(MOXMAN_Vfs_IFile $file)
 {
     $config = clone $this->config;
     $path = $file->isFile() ? $file->getParent() : $file->getPath();
     $root = $this->fileSystem->getRootPath();
     $mcAccessFile = $this->config->get("filesystem.local.access_file_name", "mc_access");
     $user = MOXMAN::getUser();
     $configFiles = array();
     $targetConfigPath = $path . '/' . $mcAccessFile;
     // Collect config files
     while ($path && strlen($path) >= strlen($root)) {
         if (file_exists($path . '/' . $mcAccessFile)) {
             $configFiles[] = $path . '/' . $mcAccessFile;
         }
         $path = MOXMAN_Util_PathUtils::getParent($path);
     }
     // Extend current config with the config files
     for ($i = count($configFiles) - 1; $i >= 0; $i--) {
         // Parse mc_access file
         $iniParser = new MOXMAN_Util_IniParser();
         $iniParser->load($configFiles[$i]);
         // Loop and extend it
         $items = $iniParser->getItems();
         foreach ($items as $key => $value) {
             // Group specific config
             if (is_array($value)) {
                 $targetGroups = explode(',', $key);
                 foreach ($targetGroups as $targetGroup) {
                     if ($user->isMemberOf($targetGroup)) {
                         foreach ($value as $key2 => $value2) {
                             if (strpos($key2, '_') === 0) {
                                 if ($targetConfigPath == $configFiles[$i]) {
                                     $key2 = substr($key2, 1);
                                 } else {
                                     continue;
                                 }
                             }
                             $config->put($key2, $value2);
                         }
                     }
                 }
             } else {
                 if (strpos($key, '_') === 0) {
                     if ($targetConfigPath == $configFiles[$i]) {
                         $key = substr($key, 1);
                     } else {
                         continue;
                     }
                 }
                 $config->put($key, $value);
             }
         }
     }
     return $config;
 }
示例#2
0
 /**
  * Get an unique file
  *
  * @param MOXMAN_Vfs_IFile $file File object to check against
  * @return MOXMAN_Vfs_IFile Unique file object.
  */
 public static function uniqueFile(MOXMAN_Vfs_IFile $file)
 {
     $fileName = $file->getName();
     $ext = MOXMAN_Util_PathUtils::getExtension($fileName);
     for ($i = 2; $file->exists(); $i++) {
         if ($file->isFile() && $ext) {
             $file = MOXMAN::getFile($file->getParent(), basename($fileName, '.' . $ext) . '_' . $i . '.' . $ext);
         } else {
             $file = MOXMAN::getFile($file->getParent(), $fileName . '_' . $i);
         }
     }
     return $file;
 }
示例#3
0
 /**
  * Converts a file instance to a JSON serializable object.
  *
  * @param MOXMAN_Vfs_IFile $file File to convert into JSON format.
  * @param Boolean $meta State if the meta data should be returned or not.
  * @return stdClass JSON serializable object.
  */
 public static function fileToJson($file, $meta = false)
 {
     $config = $file->getConfig();
     $renameFilter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "rename");
     $editFilter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "edit");
     $viewFilter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "view");
     $configuredFilter = new MOXMAN_Vfs_BasicFileFilter();
     $configuredFilter->setIncludeDirectoryPattern($config->get('filesystem.include_directory_pattern'));
     $configuredFilter->setExcludeDirectoryPattern($config->get('filesystem.exclude_directory_pattern'));
     $configuredFilter->setIncludeFilePattern($config->get('filesystem.include_file_pattern'));
     $configuredFilter->setExcludeFilePattern($config->get('filesystem.exclude_file_pattern'));
     $configuredFilter->setIncludeExtensions($config->get('filesystem.extensions'));
     $result = (object) array("path" => $file->getPublicPath(), "size" => $file->getSize(), "lastModified" => $file->getLastModified(), "isFile" => $file->isFile(), "canRead" => $file->canRead(), "canWrite" => $file->canWrite(), "canEdit" => $file->isFile() && $editFilter->accept($file), "canRename" => $renameFilter->accept($file), "canView" => $file->isFile() && $viewFilter->accept($file), "canPreview" => $file->isFile() && MOXMAN_Media_ImageAlter::canEdit($file), "visible" => $configuredFilter->accept($file), "exists" => $file->exists());
     if ($meta) {
         $args = new MOXMAN_Vfs_CustomInfoEventArgs(MOXMAN_Vfs_CustomInfoEventArgs::INSERT_TYPE, $file);
         MOXMAN::getPluginManager()->get("core")->fire("CustomInfo", $args);
         $metaData = (object) array_merge($file->getMetaData()->getAll(), $args->getInfo());
         if (MOXMAN_Media_ImageAlter::canEdit($file)) {
             $thumbnailFolderPath = MOXMAN_Util_PathUtils::combine($file->getParent(), $config->get('thumbnail.folder'));
             $thumbnailFile = MOXMAN::getFile($thumbnailFolderPath, $config->get('thumbnail.prefix') . $file->getName());
             // TODO: Implement stat info cache layer here
             if ($file instanceof MOXMAN_Vfs_Local_File) {
                 $info = MOXMAN_Media_MediaInfo::getInfo($file->getPath());
                 $metaData->width = $info["width"];
                 $metaData->height = $info["height"];
             }
             if ($thumbnailFile->exists()) {
                 $metaData->thumb_url = $thumbnailFile->getUrl();
                 // Get image size server side only on local filesystem
                 if ($file instanceof MOXMAN_Vfs_Local_File) {
                     $info = MOXMAN_Media_MediaInfo::getInfo($thumbnailFile->getPath());
                     $metaData->thumb_width = $info["width"];
                     $metaData->thumb_height = $info["height"];
                 }
             }
         }
         $metaData->url = $file->getUrl();
         $result->meta = $metaData;
     }
     return $result;
 }
示例#4
0
 /**
  * Returns true or false if the file is accepted or not.
  *
  * @param MOXMAN_Vfs_IFile $file File to grant or deny.
  * @param Boolean $isFile Default state if the filter is on an non existing file.
  * @return int Accepted or the reson why it failed.
  */
 public function accept(MOXMAN_Vfs_IFile $file, $isFile = true)
 {
     $name = $file->getName();
     $absPath = $file->getPath();
     $isFile = $file->exists() ? $file->isFile() : $isFile;
     // Handle file patterns
     if ($isFile) {
         if ($this->dirsOnly) {
             if ($this->logFunction) {
                 $this->log("File denied \"" . $absPath . "\" by \"dirsOnly\".");
             }
             return self::INVALID_TYPE;
         }
         // Handle exclude files
         if (is_array($this->excludeFiles) && $isFile) {
             foreach ($this->excludeFiles as $fileName) {
                 if ($name == $fileName) {
                     if ($this->logFunction) {
                         $this->log("File \"" . $absPath . "\" denied by \"excludeFiles\".");
                     }
                     return self::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->logFunction) {
                     $this->log("File \"" . $absPath . "\" denied by \"includeFiles\".");
                 }
                 return self::INVALID_NAME;
             }
         }
         // Handle exclude pattern
         if ($this->excludeFilePattern && preg_match($this->excludeFilePattern, $name)) {
             if ($this->logFunction) {
                 $this->log("File \"" . $absPath . "\" denied by \"excludeFilePattern\".");
             }
             return self::INVALID_NAME;
         }
         // Handle include pattern
         if ($this->includeFilePattern && !preg_match($this->includeFilePattern, $name)) {
             if ($this->logFunction) {
                 $this->log("File \"" . $absPath . "\" denied by \"includeFilePattern\".");
             }
             return self::INVALID_NAME;
         }
         // Handle file extension pattern
         if (is_array($this->extensions)) {
             $ext = MOXMAN_Util_PathUtils::getExtension($absPath);
             $valid = false;
             foreach ($this->extensions as $extension) {
                 if ($extension == $ext) {
                     $valid = true;
                     break;
                 }
             }
             if (!$valid) {
                 if ($this->logFunction) {
                     $this->log("File \"" . $absPath . "\" denied by \"extensions\".");
                 }
                 return self::INVALID_EXTENSION;
             }
         }
     } else {
         if ($this->filesOnly) {
             if ($this->logFunction) {
                 $this->log("Dir denied \"" . $absPath . "\" by \"filesOnly\".");
             }
             return self::INVALID_TYPE;
         }
         // Handle exclude folders
         if (is_array($this->excludeFolders)) {
             foreach ($this->excludeFolders as $folder) {
                 if (strpos($absPath, $folder) !== false) {
                     if ($this->logFunction) {
                         $this->log('File denied "' . $absPath . '" by "excludeFolders".');
                     }
                     return self::INVALID_NAME;
                 }
             }
         }
         // Handle include folders
         if (is_array($this->includeFolders)) {
             $state = false;
             foreach ($this->includeFolders as $folder) {
                 if (strpos($absPath, $folder) !== false) {
                     $state = true;
                     break;
                 }
             }
             if (!$state) {
                 if ($this->logFunction) {
                     $this->log("File \"" . $absPath . "\" denied by \"includeFolders\".");
                 }
                 return self::INVALID_NAME;
             }
         }
         // Handle exclude pattern
         if ($this->excludeDirectoryPattern && preg_match($this->excludeDirectoryPattern, $name)) {
             if ($this->logFunction) {
                 $this->log("File \"" . $absPath . "\" denied by \"excludeDirectoryPattern\".");
             }
             return self::INVALID_NAME;
         }
         // Handle include pattern
         if ($this->includeDirectoryPattern && !preg_match($this->includeDirectoryPattern, $name)) {
             if ($this->logFunction) {
                 $this->log("File \"" . $absPath . "\" denied by \"includeDirectoryPattern\".");
             }
             return self::INVALID_NAME;
         }
     }
     // Handle include wildcard pattern
     if ($this->includeWildcardPattern && !$this->matchWildCard($this->includeWildcardPattern, $name)) {
         if ($this->logFunction) {
             $this->log("File \"" . $absPath . "\" denied by \"includeWildcardPattern\".");
         }
         return self::INVALID_NAME;
     }
     // Handle exclude wildcard pattern
     if ($this->excludeWildcardPattern && $this->matchWildCard($this->excludeWildcardPattern, $name)) {
         if ($this->logFunction) {
             $this->log("File \"" . $absPath . "\" denied by \"excludeWildcardPattern\".");
         }
         return self::INVALID_NAME;
     }
     return self::ACCEPTED;
 }
示例#5
0
 /**
  * Converts a file instance to a JSON serializable object.
  *
  * @param MOXMAN_Vfs_IFile $file File to convert into JSON format.
  * @param Boolean $meta State if the meta data should be returned or not.
  * @return stdClass JSON serializable object.
  */
 public static function fileToJson($file, $meta = false)
 {
     $config = $file->getConfig();
     $renameFilter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "rename");
     $editFilter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "edit");
     $viewFilter = MOXMAN_Vfs_CombinedFileFilter::createFromConfig($config, "view");
     $result = (object) array("path" => $file->getPublicPath(), "size" => $file->getSize(), "lastModified" => $file->getLastModified(), "isFile" => $file->isFile(), "canRead" => $file->canRead(), "canWrite" => $file->canWrite(), "canEdit" => $file->isFile() && $editFilter->accept($file) === MOXMAN_Vfs_IFileFilter::ACCEPTED, "canRename" => $renameFilter->accept($file) === MOXMAN_Vfs_IFileFilter::ACCEPTED, "canView" => $file->isFile() && $viewFilter->accept($file) === MOXMAN_Vfs_IFileFilter::ACCEPTED, "canPreview" => $file->isFile() && MOXMAN_Media_ImageAlter::canEdit($file), "exists" => $file->exists());
     if ($meta) {
         $metaData = $file->getMetaData();
         //$args = $this->fireCustomInfo(MOXMAN_Core_CustomInfoEventArgs::INSERT_TYPE, $file);
         $metaData = (object) $metaData->getAll();
         if ($file instanceof MOXMAN_Vfs_Local_File && MOXMAN_Media_ImageAlter::canEdit($file)) {
             $thumbnailFolderPath = MOXMAN_Util_PathUtils::combine($file->getParent(), $config->get('thumbnail.folder'));
             $thumbnailFile = MOXMAN::getFile($thumbnailFolderPath, $config->get('thumbnail.prefix') . $file->getName());
             // TODO: Implement stat info cache layer here
             $info = MOXMAN_Media_MediaInfo::getInfo($file);
             $metaData->width = $info["width"];
             $metaData->height = $info["height"];
             if ($thumbnailFile->exists()) {
                 $metaData->thumb_url = $thumbnailFile->getUrl();
                 $info = MOXMAN_Media_MediaInfo::getInfo($thumbnailFile);
                 $metaData->thumb_width = $info["width"];
                 $metaData->thumb_height = $info["height"];
             }
         }
         $metaData->url = $file->getUrl();
         $result->meta = $metaData;
     }
     return $result;
 }
 public function putFile(MOXMAN_Vfs_IFile $file)
 {
     $pdo = $this->getPdo();
     if (!$pdo) {
         return null;
     }
     $parentFile = $file->getParentFile();
     if (!$parentFile) {
         $info = array("name" => $file->getFileSystem()->getRootName(), "isDirectory" => true, "canRead" => $file->canRead(), "canWrite" => $file->canWrite(), "size" => 0, "lastModified" => 0);
         return $info;
     }
     $parentPath = $this->getIndexPath($parentFile);
     $attrs = $file->isDirectory() ? "d" : "-";
     $attrs .= $file->canRead() ? "r" : "-";
     $attrs .= $file->canWrite() ? "w" : "-";
     $attrs .= "-";
     $size = $file->getSize();
     $lastModified = $file->getLastModified();
     $numItems = $pdo->i("SELECT COUNT(mc_id) FROM moxman_cache WHERE mc_path = :mc_path AND mc_name = :mc_name", array("mc_path" => $parentPath, "mc_name" => $file->getName()));
     if ($numItems > 0) {
         $pdo->q("UPDATE moxman_cache SET mc_attrs = :mc_attrs, mc_size = :mc_size, " . "mc_last_modified = :mc_last_modified, mc_cached_time = :mc_cached_time WHERE mc_path = :mc_path AND mc_name = :mc_name", array("mc_attrs" => $attrs, "mc_size" => $file->isFile() ? $size : null, "mc_last_modified" => date('Y-m-d H:i:s', $lastModified), "mc_cached_time" => date('Y-m-d H:i:s', time()), "mc_path" => $parentPath, "mc_name" => $file->getName()));
         $this->log("[cache] putFile update");
     } else {
         $pdo->q("INSERT INTO moxman_cache(mc_path, mc_name, mc_extension, mc_attrs, mc_size, mc_last_modified, mc_cached_time) " . "VALUES(:mc_path, :mc_name, :mc_extension, :mc_attrs, :mc_size, :mc_last_modified, :mc_cached_time)", array("mc_path" => $parentPath, "mc_name" => $file->getName(), "mc_extension" => pathinfo($file->getName(), PATHINFO_EXTENSION), "mc_attrs" => $attrs, "mc_size" => $file->isFile() ? $size : null, "mc_last_modified" => date('Y-m-d H:i:s', $lastModified), "mc_cached_time" => date('Y-m-d H:i:s', time())));
         $this->log("[cache] putFile insert");
     }
     $info = array("name" => $file->getName(), "isDirectory" => $attrs[0] == 'd', "canRead" => $attrs[1] == 'r', "canWrite" => $attrs[2] == 'w', "size" => $size, "lastModified" => $lastModified);
     return $info;
 }
 /**
  * Returns true or false if the file is accepted or not.
  *
  * @param MOXMAN_Vfs_IFile $file File to grant or deny.
  * @param Boolean $isFile Default state if the filter is on an non existing file.
  * @return Boolean True/false if the file is accepted or not.
  */
 public function accept(MOXMAN_Vfs_IFile $file, $isFile = true)
 {
     if ($this->isEmpty()) {
         return true;
     }
     $name = $file->getName();
     $isFile = $file->exists() ? $file->isFile() : $isFile;
     // Handle file patterns
     if ($isFile) {
         if ($this->dirsOnly) {
             return false;
         }
         // Handle exclude files
         if ($this->excludeFiles) {
             foreach ($this->excludeFiles as $fileName) {
                 if ($name == $fileName) {
                     return false;
                 }
             }
         }
         // Handle exclude pattern
         if ($this->excludeFilePattern && preg_match($this->excludeFilePattern, $name)) {
             return false;
         }
         // Handle include pattern
         if ($this->includeFilePattern && !preg_match($this->includeFilePattern, $name)) {
             return false;
         }
         // Handle file extension pattern
         if ($this->extensions) {
             $ext = MOXMAN_Util_PathUtils::getExtension($name);
             $valid = false;
             foreach ($this->extensions as $extension) {
                 if ($extension == $ext) {
                     $valid = true;
                     break;
                 }
             }
             if (!$valid) {
                 return false;
             }
         }
     } else {
         if ($this->filesOnly) {
             return false;
         }
         // Handle exclude pattern
         if ($this->excludeDirectoryPattern && preg_match($this->excludeDirectoryPattern, $name)) {
             return false;
         }
         // Handle include pattern
         if ($this->includeDirectoryPattern && !preg_match($this->includeDirectoryPattern, $name)) {
             return false;
         }
     }
     // Handle include wildcard pattern
     if ($this->includeWildcardPatternRegExp && !preg_match($this->includeWildcardPatternRegExp, $name)) {
         return false;
     }
     // Handle exclude wildcard pattern
     if ($this->excludeWildcardPatternRegExp && preg_match($this->excludeWildcardPatternRegExp, $name)) {
         return false;
     }
     return true;
 }
示例#8
0
 /**
  * Returns a new config object based on the specified files path.
  * This will match the file path with the path.overrides in the config.
  *
  * @param MOXMAN_Vfs_IFile $file File to match agains path.overrides.
  * @return MOXMAN_Util_Config New path specific config instance.
  */
 public function getFileConfig(MOXMAN_Vfs_IFile $file)
 {
     $config = new MOXMAN_Util_Config($this->items);
     $pathOverrides = $this->get("filesystem.directories");
     if (is_array($pathOverrides) && !empty($pathOverrides)) {
         if ($file->isFile()) {
             $file = $file->getParentFile();
             // @codeCoverageIgnoreStart
             if (!$file) {
                 return $config;
             }
             // @codeCoverageIgnoreEnd
         }
         $path = $file->getPublicPath();
         foreach (array_keys($pathOverrides) as $pattern) {
             $overrides = $this->getOverrides($pattern);
             if (strpos($pattern, 'regexp:') === 0) {
                 // regexp:/pattern/
                 if (preg_match(substr($pattern, 7), $path)) {
                     $config->extend($overrides);
                 }
             } else {
                 foreach (explode(',', $pattern) as $pattern) {
                     if (strpos($pattern, '/') === false) {
                         // Directory name
                         $pattern = "/\\/" . preg_quote($pattern) . "(\\/|\$)/";
                         if (preg_match($pattern, $path)) {
                             $config->extend($overrides);
                         }
                     } else {
                         if (strrchr($pattern, '/') === '/') {
                             $pattern = substr($pattern, 0, strlen($pattern) - 1);
                         }
                         if (preg_match("/\\/\\.\$/", $pattern)) {
                             // Directory path with /. at the end
                             if ($path === substr($pattern, 0, strlen($pattern) - 2)) {
                                 $config->extend($overrides);
                             }
                         } else {
                             if ($path === $pattern || strpos($path, $pattern . '/') === 0) {
                                 // Directory path
                                 $config->extend($overrides);
                             }
                         }
                     }
                 }
             }
         }
     }
     return $config;
 }