/** * 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; }
public function copyTo(MOXMAN_Vfs_IFile $dest) { $this->fileSystem->getCache()->remove($dest->getPath()); $fromStream = $this->open("rb"); $toStream = $dest->open("wb"); while (($buff = $fromStream->read(8192)) !== "") { $toStream->write($buff); } $fromStream->close(); $toStream->close(); }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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) { $entries = $this->fileSystem->getEntries($this->path); foreach ($entries as $entry) { $toPath = MOXMAN_Util_PathUtils::combine($dest->getPath(), substr($entry->path, strlen($this->getPath()))); $this->fileSystem->addEntry($toPath, array("isFile" => $entry->isFile, "lastModified" => $entry->lastModified, "data" => $entry->data, "canRead" => $entry->canRead, "canWrite" => $entry->canWrite)); } $this->fileSystem->deleteEntry($this->path); }
private function getIndexPath(MOXMAN_Vfs_IFile $file) { $path = $file->getPath(); $rootName = $file->getFileSystem()->getRootName(); $publicPath = MOXMAN_Util_PathUtils::combine($rootName !== "/" ? "/" . $rootName : $rootName, substr($path, strlen($file->getFileSystem()->getRootPath()))); return $publicPath; }
/** * Copies this file to the specified file instance. * * @param MOXMAN_Vfs_IFile $dest File to copy to. */ public function copyTo(MOXMAN_Vfs_IFile $dest) { if (!$this->exists()) { throw new Exception("Source file doesn't exist: " . $dest->getPublicPath()); } if (MOXMAN_Util_PathUtils::isChildOf($dest->getPath(), $this->getPath())) { throw new Exception("You can't copy the file into it self."); } // File copy or dir copy if ($this->isFile()) { if ($dest instanceof MOXMAN_Vfs_Local_File) { copy($this->internalPath, $this->fromUtf($dest->getPath())); } else { // Copy between file systems $in = $this->open(MOXMAN_Vfs_IFileStream::READ); $out = $dest->open(MOXMAN_Vfs_IFileStream::WRITE); // Stream in file to out file while (($data = $in->read()) !== "") { $out->write($data); } $in->close(); $out->close(); } } else { // Copy dir to dir $this->copyDir($this, $dest); } }
/** * 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 ""; }