Ejemplo n.º 1
0
 /**
  * Fixes filenames
  *
  * @param MOXMAN_Vfs_IFile $file File to fix name on.
  */
 public function renameFile(MOXMAN_Vfs_IFile $file)
 {
     $config = $file->getConfig();
     $autorename = $config->get("autorename.enabled", "");
     $spacechar = $config->get("autorename.space", "_");
     $custom = $config->get("autorename.pattern", "/[^0-9a-z\\-_]/i");
     $overwrite = $config->get("upload.overwrite", false);
     $lowercase = $config->get("autorename.lowercase", false);
     $prefix = $lowercase = $config->get("autorename.prefix", '');
     // @codeCoverageIgnoreStart
     if (!$autorename) {
         return $file;
     }
     // @codeCoverageIgnoreEnd
     $path = $file->getPath();
     $name = $file->getName();
     $orgname = $name;
     $ext = MOXMAN_Util_PathUtils::getExtension($path);
     $name = preg_replace("/\\." . $ext . "\$/i", "", $name);
     $name = str_replace(array('\'', '"'), '', $name);
     $name = htmlentities($name, ENT_QUOTES, 'UTF-8');
     $name = preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', $name);
     $name = preg_replace($custom, $spacechar, $name);
     $name = str_replace(" ", $spacechar, $name);
     $name = trim($name);
     if ($lowercase) {
         $ext = strtolower($ext);
         $name = strtolower($name);
     }
     if ($ext) {
         $name = $name . "." . $ext;
     }
     //add prefix
     if ($prefix != '') {
         $aa = explode("-", $name);
         if (count($aa) == 1) {
             $name = $prefix . $name;
         }
     }
     // If no change to name after all this, return original file.
     if ($name === $orgname) {
         return $file;
     }
     // Return new file
     $toFile = MOXMAN::getFile($file->getParent() . "/" . $name);
     return $toFile;
 }
Ejemplo n.º 2
0
 /**
  * Returns an URL for the specified file object.
  *
  * @param MOXMAN_Vfs_IFile $file File to get the absolute URL for.
  * @return String Absolute URL for the specified file.
  */
 public function getUrl(MOXMAN_Vfs_IFile $file)
 {
     $config = $file->getConfig();
     // Get config items
     $wwwroot = $config->get("filesystem.local.wwwroot");
     $prefix = $config->get("filesystem.local.urlprefix");
     $suffix = $config->get("filesystem.local.urlsuffix");
     $paths = MOXMAN_Util_PathUtils::getSitePaths();
     // No wwwroot specified try to figure out a wwwroot
     if (!$wwwroot) {
         $wwwroot = $paths["wwwroot"];
     } else {
         // Force the www root to an absolute file system path
         $wwwroot = MOXMAN_Util_PathUtils::toAbsolute(MOXMAN_ROOT, $wwwroot);
     }
     // Add prefix to URL
     if ($prefix == "") {
         $prefix = MOXMAN_Util_PathUtils::combine("{proto}://{host}", $paths["prefix"]);
     }
     // Replace protocol
     if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {
         $prefix = str_replace("{proto}", "https", $prefix);
     } else {
         $prefix = str_replace("{proto}", "http", $prefix);
     }
     // Replace host/port
     $prefix = str_replace("{host}", $_SERVER['HTTP_HOST'], $prefix);
     $prefix = str_replace("{port}", $_SERVER['SERVER_PORT'], $prefix);
     // Insert path into URL
     $url = substr($file->getPath(), strlen($wwwroot));
     $url = MOXMAN_Util_PathUtils::combine($prefix, $url);
     // Add suffix to URL
     if ($suffix) {
         $url .= $suffix;
     }
     return $url;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 4
0
 /**
  * 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();
         }
     }
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 6
0
 /**
  * Returns public config options to the client.
  *
  * @param MOXMAN_Vfs_IFile $file File to get public config options for.
  * @return stdClass JSON serializable object of config options.
  */
 protected function getPublicConfig($file = null)
 {
     $exposed = array("general.hidden_tools", "general.disabled_tools", "filesystem.extensions", "filesystem.force_directory_template", "upload.maxsize", "upload.chunk_size", "upload.extensions", "createdoc.templates", "createdoc.fields", "createdir.templates");
     $result = array();
     $config = $file ? $file->getConfig() : MOXMAN::getConfig();
     foreach ($exposed as $name) {
         $result[$name] = $config->get($name);
     }
     return (object) $result;
 }
Ejemplo n.º 7
0
 /**
  * Returns an URL for the specified file object.
  *
  * @param MOXMAN_Vfs_IFile $file File to get the absolute URL for.
  * @return String Absolute URL for the specified file.
  */
 public function getUrl(MOXMAN_Vfs_IFile $file)
 {
     $config = $file->getConfig();
     // Get config items
     $wwwroot = $config->get("filesystem.local.wwwroot");
     $prefix = $config->get("filesystem.local.urlprefix");
     $suffix = $config->get("filesystem.local.urlsuffix");
     // Map to wwwroot array
     if (is_array($wwwroot)) {
         foreach ($wwwroot as $rootPath => $rootConfig) {
             $rootPath = MOXMAN_Util_PathUtils::toAbsolute(MOXMAN_ROOT, $rootPath);
             if (strpos($file->getPath(), $rootPath) === 0) {
                 $wwwroot = $rootPath;
                 if (isset($rootConfig["wwwroot"])) {
                     $wwwroot = $rootConfig["wwwroot"];
                 }
                 if (isset($rootConfig["urlprefix"])) {
                     $prefix = $rootConfig["urlprefix"];
                 }
                 if (isset($rootConfig["urlsuffix"])) {
                     $suffix = $rootConfig["urlsuffix"];
                 }
                 break;
             }
         }
         if (is_array($wwwroot)) {
             $wwwroot = "";
         }
     }
     $paths = MOXMAN_Util_PathUtils::getSitePaths();
     // No wwwroot specified try to figure out a wwwroot
     if (!$wwwroot) {
         $wwwroot = $paths["wwwroot"];
     } else {
         // Force the www root to an absolute file system path
         $wwwroot = MOXMAN_Util_PathUtils::toAbsolute(MOXMAN_ROOT, $wwwroot);
     }
     // Add prefix to URL
     if ($prefix == "") {
         $prefix = MOXMAN_Util_PathUtils::combine("{proto}://{host}", $paths["prefix"]);
     }
     // Replace protocol
     if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {
         $prefix = str_replace("{proto}", "https", $prefix);
     } else {
         $prefix = str_replace("{proto}", "http", $prefix);
     }
     // Replace host/port
     $prefix = str_replace("{host}", $_SERVER['HTTP_HOST'], $prefix);
     $prefix = str_replace("{port}", $_SERVER['SERVER_PORT'], $prefix);
     // Insert path into URL
     if (stripos($file->getPath(), $wwwroot) === 0) {
         $url = substr($file->getPath(), strlen($wwwroot));
         $url = MOXMAN_Util_PathUtils::combine($prefix, MOXMAN_Http_Uri::escapeUriString($url));
         // Add suffix to URL
         if ($suffix) {
             $url .= $suffix;
         }
         return $url;
     }
     return "";
 }