private function addVideoMeta(MOXMAN_Vfs_IFile $file, $metaData)
 {
     $fileName = $file->getName();
     $ext = strtolower(MOXMAN_Util_PathUtils::getExtension($fileName));
     if (preg_match('/^(mp4|ogv|webm)$/', $ext)) {
         $metaData->url_type = MOXMAN_Util_Mime::get($fileName);
         $name = substr($fileName, 0, strlen($fileName) - strlen($ext));
         // Alternative video formats
         $altExt = array("mp4", "ogv", "webm");
         foreach ($altExt as $altExt) {
             if ($ext != $altExt) {
                 $altFile = MOXMAN::getFile($file->getParent(), $name . $altExt);
                 if ($altFile->exists()) {
                     $metaData->alt_url = $altFile->getUrl();
                     break;
                 }
             }
         }
         // Alternative image format
         $altFile = MOXMAN::getFile($file->getParent(), $name . "jpg");
         if ($altFile->exists()) {
             $metaData->alt_img = $altFile->getUrl();
         }
     }
 }
示例#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
 /**
  * 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;
 }
示例#4
0
 /**
  * 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);
         }
     }
 }
示例#6
0
 public function moveThumbnail(MOXMAN_Vfs_IFile $fromFile, MOXMAN_Vfs_IFile $toFile)
 {
     if ($fromFile->isDirectory() || !MOXMAN_Media_ImageAlter::canEdit($fromFile)) {
         return false;
     }
     $config = $fromFile->getConfig();
     // From thumbnail
     $fromThumbnailFolderPath = MOXMAN_Util_PathUtils::combine($fromFile->getParent(), $config->get('thumbnail.folder'));
     $fromThumbnailFile = MOXMAN::getFile($fromThumbnailFolderPath, $config->get('thumbnail.prefix') . $fromFile->getName());
     // To thumbnail
     $toThumbnailFolderPath = MOXMAN_Util_PathUtils::combine($toFile->getParent(), $config->get('thumbnail.folder'));
     $toThumbnailFile = MOXMAN::getFile($toThumbnailFolderPath, $config->get('thumbnail.prefix') . $toFile->getName());
     $thumbnailFolderFile = $toThumbnailFile->getParentFile();
     if (!$thumbnailFolderFile->exists()) {
         $thumbnailFolderFile->mkdir();
         $this->fireThumbnailFileAction(MOXMAN_Vfs_FileActionEventArgs::ADD, $thumbnailFolderFile);
     }
     if ($fromThumbnailFile->exists()) {
         $fromThumbnailFile->moveTo($toThumbnailFile);
         $this->fireThumbnailTargetFileAction(MOXMAN_Vfs_FileActionEventArgs::MOVE, $fromThumbnailFile, $toThumbnailFile);
         return true;
     }
     return false;
 }
示例#7
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;
 }
示例#8
0
 public function getThumbnail(MOXMAN_Vfs_IFile $file)
 {
     $config = $file->getConfig();
     if ($config->get('thumbnail.enabled') !== true) {
         return $file;
     }
     $thumbnailFolderPath = MOXMAN_Util_PathUtils::combine($file->getParent(), $config->get('thumbnail.folder'));
     $thumbnailFile = MOXMAN::getFile($thumbnailFolderPath, $config->get('thumbnail.prefix') . $file->getName());
     return $thumbnailFile;
 }
 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;
 }
示例#10
0
 /**
  * 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;
 }
示例#12
0
 /**
  * Returns true/false if the specified file can be altered or not.
  *
  * @param MOXMAN_Vfs_IFile $file File to check if can be altered.
  * @return Boolean True/false if the image can be edited or not.
  */
 public static function canEdit(MOXMAN_Vfs_IFile $file)
 {
     $ext = MOXMAN_Util_PathUtils::getExtension($file->getName());
     return preg_match('/gif|jpe?g|png|bmp/', $ext) === 1;
 }