public function execute(Request $request, WorkingFolder $workingFolder, Config $config, CacheManager $cache) { $fileName = (string) $request->get('fileName'); if (null === $fileName || !File::isValidName($fileName, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException('Invalid file name'); } if (!Image::isSupportedExtension(pathinfo($fileName, PATHINFO_EXTENSION))) { throw new InvalidNameException('Invalid source file name'); } if (!$workingFolder->containsFile($fileName)) { throw new FileNotFoundException(); } $cachePath = Path::combine($workingFolder->getResourceType()->getName(), $workingFolder->getClientCurrentFolder(), $fileName); $imageInfo = array(); $cachedInfo = $cache->get($cachePath); if ($cachedInfo && isset($cachedInfo['width']) && isset($cachedInfo['height'])) { $imageInfo = $cachedInfo; } else { $file = new DownloadedFile($fileName, $this->app); if ($file->isValid()) { $image = Image::create($file->getContents()); $imageInfo = $image->getInfo(); $cache->set($cachePath, $imageInfo); } } return $imageInfo; }
/** * Returns target file name of the copied file * * @return string */ public function getTargetFilename() { if ($this->targetFolder->containsFile($this->getFilename()) && strpos($this->copyOptions, 'overwrite') === false && strpos($this->copyOptions, 'autorename') !== false) { $this->autorename(); } return $this->fileName; }
/** * Copies current file * * @param string $copyOptions defines copy options in case if file already exists * in target directory: * - autorename - renames current file (see File::autorename()) * - overwrite - overwrites existing file * * @return bool true if file was copied successfully * * @throws \Exception */ public function doCopy($copyOptions) { $originalFileStream = $this->getContentsStream(); $originalFileName = $this->getFilename(); // Don't copy file to itself if ($this->targetFolder->getBackend() === $this->resourceType->getBackend() && $this->targetFolder->getPath() === $this->getPath()) { $this->addError(Error::SOURCE_AND_TARGET_PATH_EQUAL); return false; // Check if file already exists in target backend dir } elseif ($this->targetFolder->containsFile($this->getFilename()) && strpos($copyOptions, 'overwrite') === false) { if (strpos($copyOptions, 'autorename') !== false) { $this->autorename(); } else { $this->addError(Error::ALREADY_EXIST); return false; } } if ($this->targetFolder->putStream($this->getFilename(), $originalFileStream)) { $resizedImageRepository = $this->resourceType->getResizedImageRepository(); $resizedImageRepository->copyResizedImages($this->resourceType, $this->folder, $originalFileName, $this->targetFolder->getResourceType(), $this->targetFolder->getClientCurrentFolder(), $this->getFilename()); $this->getCache()->copy(Path::combine($this->resourceType->getName(), $this->folder, $originalFileName), Path::combine($this->targetFolder->getResourceType()->getName(), $this->targetFolder->getClientCurrentFolder(), $this->getFilename())); return true; } else { $this->addError(Error::ACCESS_DENIED); return false; } }
/** * Main command method. * * @param Request $request Current request object * @param WorkingFolder $workingFolder Current working folder object * * @return array * * @throws \Exception */ public function execute(Request $request, WorkingFolder $workingFolder) { $fileName = $request->get('fileName'); $backend = $workingFolder->getBackend(); if (!$workingFolder->containsFile($fileName)) { throw new \Exception('File not found', Error::FILE_NOT_FOUND); } $fileMetadada = $backend->getMetadata(Path::combine($workingFolder->getPath(), $fileName)); return $fileMetadada; }
public function isValid() { if (!$this->saveAsNew && !$this->exists()) { throw new FileNotFoundException(); } if ($this->newFileName) { if (!File::isValidName($this->newFileName, $this->config->get('disallowUnsafeCharacters'))) { throw new InvalidNameException('Invalid file name'); } if ($this->workingFolder->containsFile($this->newFileName)) { throw new AlreadyExistsException('File already exists'); } if ($this->resourceType->getBackend()->isHiddenFile($this->newFileName)) { throw new InvalidRequestException('New provided file name is hidden'); } } if (!$this->hasValidFilename() || !$this->hasValidPath()) { throw new InvalidRequestException('Invalid filename or path'); } if ($this->isHidden() || $this->hasHiddenPath()) { throw new InvalidRequestException('Edited file is hidden'); } return true; }
public function execute(Request $request, WorkingFolder $workingFolder, Config $config, ResizedImageRepository $resizedImageRepository) { $fileName = (string) $request->query->get('fileName'); if (null === $fileName || !File::isValidName($fileName, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException('Invalid file name'); } $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if (!Image::isSupportedExtension($ext)) { throw new InvalidNameException('Invalid source file name'); } if (!$workingFolder->containsFile($fileName)) { throw new FileNotFoundException(); } list($requestedWidth, $requestedHeight) = Image::parseSize((string) $request->query->get('size')); $resizedImage = $resizedImageRepository->getResizedImage($workingFolder->getResourceType(), $workingFolder->getClientCurrentFolder(), $fileName, $requestedWidth, $requestedHeight); return array('url' => $resizedImage->getUrl()); }
/** * Validates the file * * @return bool true if file passed the validation * * @throws AlreadyExistsException * @throws FileNotFoundException * @throws InvalidExtensionException * @throws InvalidNameException * @throws InvalidRequestException * @throws InvalidUploadException */ public function isValid() { if ($this->newFileName) { if (!File::isValidName($this->newFileName, $this->config->get('disallowUnsafeCharacters'))) { throw new InvalidNameException('Invalid file name'); } if ($this->resourceType->getBackend()->isHiddenFile($this->newFileName)) { throw new InvalidRequestException('New provided file name is hidden'); } if (!$this->resourceType->isAllowedExtension($this->getNewExtension())) { throw new InvalidExtensionException(); } if ($this->config->get('checkDoubleExtension') && !$this->areValidDoubleExtensions($this->newFileName)) { throw new InvalidExtensionException(); } if ($this->workingFolder->containsFile($this->newFileName)) { throw new AlreadyExistsException('File already exists'); } } if (!$this->hasValidFilename() || !$this->hasValidPath()) { throw new InvalidRequestException('Invalid filename or path'); } if ($this->isHidden() || $this->hasHiddenPath()) { throw new InvalidRequestException('Edited file is hidden'); } if ($this->config->get('checkDoubleExtension') && !$this->areValidDoubleExtensions()) { throw new InvalidExtensionException(); } if (!$this->resourceType->isAllowedExtension($this->getExtension())) { throw new InvalidExtensionException(); } if (!$this->saveAsNew && !$this->exists()) { throw new FileNotFoundException(); } if ($this->newContents) { if (Utils::containsHtml(substr($this->newContents, 0, 1024)) && !in_array(strtolower($this->newFileName ? $this->getNewExtension() : $this->getExtension()), $this->config->get('htmlExtensions'))) { throw new InvalidUploadException('HTML detected in disallowed file type', Error::UPLOADED_WRONG_HTML_FILE); } $maxFileSize = $this->resourceType->getMaxSize(); if ($maxFileSize && strlen($this->newContents) > $maxFileSize) { throw new InvalidUploadException('Uploaded file is too big', Error::UPLOADED_TOO_BIG); } } return true; }
public function execute(Request $request, WorkingFolder $workingFolder, Config $config, ThumbnailRepository $thumbnailRepository) { if (!$config->get('thumbnails.enabled')) { throw new CKFinderException('Thumbnails feature is disabled', Error::THUMBNAILS_DISABLED); } $fileName = (string) $request->get('fileName'); $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if (!Image::isSupportedExtension($ext, $thumbnailRepository->isBitmapSupportEnabled())) { throw new InvalidNameException('Invalid source file name'); } if (null === $fileName || !File::isValidName($fileName, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException('Invalid file name'); } if (!$workingFolder->containsFile($fileName)) { throw new FileNotFoundException(); } list($requestedWidth, $requestedHeight) = Image::parseSize((string) $request->get('size')); $thumbnail = $thumbnailRepository->getThumbnail($workingFolder->getResourceType(), $workingFolder->getClientCurrentFolder(), $fileName, $requestedWidth, $requestedHeight); Utils::removeSessionCacheHeaders(); $response = new Response(); $response->setPublic(); $response->setEtag(dechex($thumbnail->getTimestamp()) . "-" . dechex($thumbnail->getSize())); $lastModificationDate = new \DateTime(); $lastModificationDate->setTimestamp($thumbnail->getTimestamp()); $response->setLastModified($lastModificationDate); if ($response->isNotModified($request)) { return $response; } $thumbnailsCacheExpires = (int) $config->get('cache.thumbnails'); if ($thumbnailsCacheExpires > 0) { $response->setMaxAge($thumbnailsCacheExpires); $expireTime = new \DateTime(); $expireTime->modify('+' . $thumbnailsCacheExpires . 'seconds'); $response->setExpires($expireTime); } $response->headers->set('Content-Type', $thumbnail->getMimeType() . '; name="' . $thumbnail->getFileName() . '"'); $response->setContent($thumbnail->getImageData()); return $response; }
public function execute(WorkingFolder $workingFolder, Request $request, Config $config) { $fileName = $request->get('fileName'); $thumbnail = $request->get('thumbnail'); $fileNames = (array) $request->get('fileNames'); if (!empty($fileNames)) { $urls = array(); foreach ($fileNames as $fileName) { if (!File::isValidName($fileName, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException(sprintf('Invalid file name: %s', $fileName)); } $urls[$fileName] = $workingFolder->getFileUrl($fileName); } return array('urls' => $urls); } if (!File::isValidName($fileName, $config->get('disallowUnsafeCharacters')) || $thumbnail && !File::isValidName($thumbnail, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException('Invalid file name'); } if (!$workingFolder->containsFile($fileName)) { throw new FileNotFoundException(); } return array('url' => $workingFolder->getFileUrl($thumbnail ? Path::combine(ResizedImage::DIR, $fileName, $thumbnail) : $fileName)); }
/** * Checks if file exists * * @return bool true if file exists */ public function exists() { return $this->workingFolder->containsFile($this->fileName); }
public function execute(Request $request, WorkingFolder $workingFolder, EventDispatcher $dispatcher, Config $config) { $fileName = (string) $request->query->get('fileName'); $thumbnailFileName = (string) $request->query->get('thumbnail'); if (!File::isValidName($fileName, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException(sprintf('Invalid file name: %s', $fileName)); } $cacheLifetime = (int) $request->query->get('cache'); if (!$workingFolder->containsFile($fileName)) { throw new FileNotFoundException(); } if ($thumbnailFileName) { if (!File::isValidName($thumbnailFileName, $config->get('disallowUnsafeCharacters'))) { throw new InvalidRequestException(sprintf('Invalid resized image file name: %s', $fileName)); } if (!$workingFolder->getResourceType()->isAllowedExtension(pathinfo($thumbnailFileName, PATHINFO_EXTENSION))) { throw new InvalidExtensionException(); } $resizedImageRespository = $this->app->getResizedImageRepository(); $file = $resizedImageRespository->getExistingResizedImage($workingFolder->getResourceType(), $workingFolder->getClientCurrentFolder(), $fileName, $thumbnailFileName); $dataStream = $file->readStream(); } else { $file = new DownloadedFile($fileName, $this->app); $file->isValid(); $dataStream = $workingFolder->readStream($file->getFilename()); } $proxyDownload = new ProxyDownloadEvent($this->app, $file); $dispatcher->dispatch(CKFinderEvent::PROXY_DOWNLOAD, $proxyDownload); if ($proxyDownload->isPropagationStopped()) { throw new AccessDeniedException(); } $response = new StreamedResponse(); $response->headers->set('Content-Type', $file->getMimeType()); $response->headers->set('Content-Length', $file->getSize()); $response->headers->set('Content-Disposition', 'inline; filename="' . $fileName . '"'); if ($cacheLifetime > 0) { Utils::removeSessionCacheHeaders(); $response->setPublic(); $response->setEtag(dechex($file->getTimestamp()) . "-" . dechex($file->getSize())); $lastModificationDate = new \DateTime(); $lastModificationDate->setTimestamp($file->getTimestamp()); $response->setLastModified($lastModificationDate); if ($response->isNotModified($request)) { return $response; } $response->setMaxAge($cacheLifetime); $expireTime = new \DateTime(); $expireTime->modify('+' . $cacheLifetime . 'seconds'); $response->setExpires($expireTime); } $chunkSize = 1024 * 100; $response->setCallback(function () use($dataStream, $chunkSize) { if ($dataStream === false) { return false; } while (!feof($dataStream)) { echo fread($dataStream, $chunkSize); flush(); @set_time_limit(8); } return true; }); return $response; }