/** * 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; }
/** * Returns an array with media info. * * @param MOXMAN_Vfs_IFile $file File to get the media info for. * @return Array Name/value array with media info. */ public static function getInfo(MOXMAN_Vfs_IFile $file) { if (!$file->exists()) { return null; } $ext = strtolower(MOXMAN_Util_PathUtils::getExtension($file->getName())); switch ($ext) { case "png": return self::getPngInfo($file); default: if ($file instanceof MOXMAN_Vfs_Local_File) { $size = @getimagesize($file->getPath()); if ($size) { return array("width" => $size[0], "height" => $size[1]); } } } }
/** * Removes the local temp file for a specific file instance. * * @param MOXMAN_Vfs_IFile File instance used to create a local temp file. */ public function removeLocalTempFile(MOXMAN_Vfs_IFile $file) { if ($file->exists()) { $tempDir = MOXMAN_Util_PathUtils::combine(MOXMAN_Util_PathUtils::getTempDir($this->config), "moxman_blob_cache"); $tempFile = MOXMAN_Util_PathUtils::combine($tempDir, md5($file->getPath() . $file->getLastModified()) . "." . MOXMAN_Util_PathUtils::getExtension($file->getName())); if (file_exists($tempFile)) { unlink($tempFile); } } }
/** * 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; }
/** * Returns the meta data file instance used to store meta information. * * @param MOXMAN_Vfs_IFile $file File instance to get the meta data file for. * @return MOXMAN_Vfs_IFile Meta data file. */ protected function getMetaFile(MOXMAN_Vfs_IFile $file) { $metaFile = null; if ($file->exists()) { $parent = $file->getParentFile(); // Check if file isn't a root file if ($parent !== null) { $metaFile = $parent->getFileSystem()->getFile(MOXMAN_Util_PathUtils::combine($parent->getPath(), "meta.dat")); } } return $metaFile; }
/** * Removes formats from an image. * * @param MOXMAN_Vfs_IFile $file File to generate images for. */ public function removeFormat(MOXMAN_Vfs_IFile $file) { if (!$file->exists()) { return; } $config = $file->getConfig(); $format = $config->get("autoformat.rules", ""); if ($config->get("autoformat.delete_format_images", true) === false) { return; } // @codeCoverageIgnoreStart if (!$format) { return; } // @codeCoverageIgnoreEnd $chunks = preg_split('/,/', $format, 0, PREG_SPLIT_NO_EMPTY); $imageInfo = MOXMAN_Media_MediaInfo::getInfo($file); $width = $imageInfo["width"]; $height = $imageInfo["height"]; foreach ($chunks as $chunk) { $parts = explode('=', $chunk); $fileName = preg_replace('/\\..+$/', '', $file->getName()); $extension = preg_replace('/^.+\\./', '', $file->getName()); $targetWidth = $newWidth = $width; $targetHeight = $newHeight = $height; $items = explode('|', $parts[0]); foreach ($items as $item) { switch ($item) { case "gif": case "jpg": case "jpeg": case "png": $extension = $item; break; default: $matches = array(); if (preg_match('/\\s?([0-9|\\*]+)\\s?x([0-9|\\*]+)\\s?/', $item, $matches)) { $targetWidth = $matches[1]; $targetHeight = $matches[2]; if ($targetWidth == '*') { // Width is omitted $targetWidth = floor($width / ($height / $targetHeight)); } if ($targetHeight == '*') { // Height is omitted $targetHeight = floor($height / ($width / $targetWidth)); } } } } // Scale it if ($targetWidth != $width || $targetHeight != $height) { $scale = min($targetWidth / $width, $targetHeight / $height); $newWidth = $scale > 1 ? $width : floor($width * $scale); $newHeight = $scale > 1 ? $height : floor($height * $scale); } // Build output path $outPath = $parts[1]; $outPath = str_replace("%f", $fileName, $outPath); $outPath = str_replace("%e", $extension, $outPath); $outPath = str_replace("%ow", "" . $width, $outPath); $outPath = str_replace("%oh", "" . $height, $outPath); $outPath = str_replace("%tw", "" . $targetWidth, $outPath); $outPath = str_replace("%th", "" . $targetHeight, $outPath); $outPath = str_replace("%w", "" . $newWidth, $outPath); $outPath = str_replace("%h", "" . $newHeight, $outPath); $outFile = MOXMAN::getFileSystemManager()->getFile($file->getParent(), $outPath); if ($outFile->exists()) { $outFile->delete(); } } }
/** * Renames/Moves this file to the specified file instance. * * @param MOXMAN_Vfs_IFile $dest File to rename/move to. */ public function moveTo(MOXMAN_Vfs_IFile $dest) { if (!$this->exists()) { throw new Exception("Source file doesn't exist: " . $dest->getPublicPath()); } $isSameFile = strtolower($this->getPath()) != strtolower($dest->getPath()) || $this->getName() == $dest->getName(); if ($dest->exists()) { if ($isSameFile) { throw new Exception("Destination file already exists: " . $dest->getPublicPath()); } } if ($isSameFile && MOXMAN_Util_PathUtils::isChildOf($dest->getPath(), $this->getPath())) { throw new Exception("You can't move the file into it self."); } $status = rename($this->internalPath, $this->fromUtf($dest->getPath())); }
/** * 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; }
/** * Applies formats to an image. * * @param MOXMAN_Vfs_IFile $file File to generate images for. */ public function applyFormat(MOXMAN_Vfs_IFile $file) { if (!$file->exists() || !MOXMAN_Media_ImageAlter::canEdit($file)) { return; } $config = $file->getConfig(); $format = $config->get("autoformat.rules", ""); $quality = $config->get("autoformat.jpeg_quality", 90); // @codeCoverageIgnoreStart if (!$format) { return; } // @codeCoverageIgnoreEnd $chunks = preg_split('/,/', $format, 0, PREG_SPLIT_NO_EMPTY); $imageInfo = MOXMAN_Media_MediaInfo::getInfo($file); $width = $imageInfo["width"]; $height = $imageInfo["height"]; foreach ($chunks as $chunk) { $parts = explode('=', $chunk); $actions = array(); $fileName = preg_replace('/\\..+$/', '', $file->getName()); $extension = preg_replace('/^.+\\./', '', $file->getName()); $targetWidth = $newWidth = $width; $targetHeight = $newHeight = $height; $items = explode('|', $parts[0]); foreach ($items as $item) { switch ($item) { case "gif": case "jpg": case "jpeg": case "png": $extension = $item; break; default: $matches = array(); if (preg_match('/\\s?([0-9|\\*]+)\\s?x([0-9|\\*]+)\\s?/', $item, $matches)) { $actions[] = "resize"; $targetWidth = $matches[1]; $targetHeight = $matches[2]; if ($targetWidth == '*') { // Width is omitted $targetWidth = floor($width / ($height / $targetHeight)); } if ($targetHeight == '*') { // Height is omitted $targetHeight = floor($height / ($width / $targetWidth)); } } } } // Scale it if ($targetWidth != $width || $targetHeight != $height) { $scale = min($targetWidth / $width, $targetHeight / $height); $newWidth = $scale > 1 ? $width : floor($width * $scale); $newHeight = $scale > 1 ? $height : floor($height * $scale); } // Build output path $outPath = $parts[1]; $outPath = str_replace("%f", $fileName, $outPath); $outPath = str_replace("%e", $extension, $outPath); $outPath = str_replace("%ow", "" . $width, $outPath); $outPath = str_replace("%oh", "" . $height, $outPath); $outPath = str_replace("%tw", "" . $targetWidth, $outPath); $outPath = str_replace("%th", "" . $targetHeight, $outPath); $outPath = str_replace("%w", "" . $newWidth, $outPath); $outPath = str_replace("%h", "" . $newHeight, $outPath); $outFile = MOXMAN::getFileSystemManager()->getFile($file->getParent(), $outPath); // Make dirs $parents = array(); $parent = $outFile->getParentFile(); while ($parent) { if ($parent->exists()) { break; } $parents[] = $parent; $parent = $parent->getParentFile(); } for ($i = count($parents) - 1; $i >= 0; $i--) { $parents[$i]->mkdir(); $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $parents[$i]); $args->getData()->format = true; MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args); } if (count($actions) > 0) { foreach ($actions as $action) { switch ($action) { case 'resize': $imageAlter = new MOXMAN_Media_ImageAlter(); $tempFilePath = MOXMAN::getFileSystemManager()->getLocalTempPath($file); $imageAlter->load($file->exportTo($tempFilePath)); $imageAlter->resize($newWidth, $newHeight); $outFileTempPath = MOXMAN::getFileSystemManager()->getLocalTempPath($outFile); $imageAlter->save($outFileTempPath, $quality); $outFile->importFrom($outFileTempPath); $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $outFile); $args->getData()->format = true; MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args); break; } } } else { $imageAlter = new MOXMAN_Media_ImageAlter(); $tempFilePath = MOXMAN::getFileSystemManager()->getLocalTempPath($file); $imageAlter->load($file->exportTo($tempFilePath)); $outFileTempPath = MOXMAN::getFileSystemManager()->getLocalTempPath($outFile); $imageAlter->save($outFileTempPath, $quality); $outFile->importFrom($outFileTempPath); $args = new MOXMAN_Core_FileActionEventArgs(MOXMAN_Core_FileActionEventArgs::ADD, $outFile); $args->getData()->format = true; MOXMAN::getPluginManager()->get("core")->fire("FileAction", $args); } } }