/** * Renames current working folder * * @param string $newName new folder name * * @return array containing newName and newPath * * @throws \Exception if rename failed */ public function rename($newName) { $disallowUnsafeCharacters = $this->app['config']->get('disallowUnsafeCharacters'); if (!Folder::isValidName($newName, $disallowUnsafeCharacters) || $this->backend->isHiddenFolder($newName)) { throw new InvalidNameException('Invalid folder name'); } $newBackendPath = dirname($this->getPath()) . '/' . $newName; if ($this->backend->has($newBackendPath)) { throw new AlreadyExistsException('File already exists'); } $newClientPath = Path::normalize(dirname($this->getClientCurrentFolder()) . '/' . $newName); $dispatcher = $this->app['dispatcher']; $renameFolderEvent = new RenameFolderEvent($this->app, $this, $newName); $dispatcher->dispatch(CKFinderEvent::RENAME_FOLDER, $renameFolderEvent); $newName = $renameFolderEvent->getNewFolderName(); if (!$renameFolderEvent->isPropagationStopped()) { if (!$this->getBackend()->rename($this->getPath(), $newBackendPath)) { throw new AccessDeniedException(); } /* @var OperationManager $currentRequestOperation */ $currentRequestOperation = $this->app['operation']; if ($currentRequestOperation->isAborted()) { // Don't continue in this case, no need to touch thumbs and cache entries return array('aborted' => true); } // Delete related thumbs path $this->thumbnailRepository->deleteThumbnails($this->resourceType, $this->getClientCurrentFolder()); $this->app['cache']->changePrefix(Path::combine($this->resourceType->getName(), $this->getClientCurrentFolder()), Path::combine($this->resourceType->getName(), $newClientPath)); } return array('newName' => $newName, 'newPath' => $newClientPath); }
/** * Returns a resized image for provided source file * * If an appropriate resized version already exists it's reused. * * @param ResourceType $sourceFileResourceType * @param string $sourceFilePath * @param string $sourceFileName * @param int $requestedWidth * @param int $requestedHeight * * @return ResizedImage * * @throws \Exception */ public function getResizedImage(ResourceType $sourceFileResourceType, $sourceFilePath, $sourceFileName, $requestedWidth, $requestedHeight) { $resizedImage = new ResizedImage($this, $sourceFileResourceType, $sourceFilePath, $sourceFileName, $requestedWidth, $requestedHeight); if (!$this->acl->isAllowed($sourceFileResourceType->getName(), $sourceFilePath, Permission::IMAGE_RESIZE_CUSTOM) && !$this->isSizeAllowedInConfig($requestedWidth, $requestedHeight)) { throw new UnauthorizedException('Provided size is not allowed in images.sizes configuration'); } if (!$resizedImage->exists() && $resizedImage->requestedSizeIsValid()) { $resizedImage->create(); $resizeImageEvent = new ResizeImageEvent($this->app, $resizedImage); $this->dispatcher->dispatch(CKFinderEvent::CREATE_RESIZED_IMAGE, $resizeImageEvent); if (!$resizeImageEvent->isPropagationStopped()) { $resizedImage = $resizeImageEvent->getResizedImage(); $resizedImage->save(); } } return $resizedImage; }
/** * Returns a URL to a file. * * If the useProxyCommand option is set for a backend, the returned * URL will point to the CKFinder connector Proxy command. * * @param ResourceType $resourceType the file resource type * @param string $folderPath the resource-type relative folder path * @param string $fileName the file name * @param string|null $thumbnailFileName the thumbnail file name - if the file is a thumbnail * * @return string|null URL to a file or `null` if the backend does not support it. */ public function getFileUrl(ResourceType $resourceType, $folderPath, $fileName, $thumbnailFileName = null) { if (isset($this->backendConfig['useProxyCommand'])) { $connectorUrl = $this->app->getConnectorUrl(); $queryParameters = array('command' => 'Proxy', 'type' => $resourceType->getName(), 'currentFolder' => $folderPath, 'fileName' => $fileName); if ($thumbnailFileName) { $queryParameters['thumbnail'] = $thumbnailFileName; } $proxyCacheLifetime = (int) $this->ckConfig->get('cache.proxyCommand'); if ($proxyCacheLifetime > 0) { $queryParameters['cache'] = $proxyCacheLifetime; } return $connectorUrl . '?' . http_build_query($queryParameters, '', '&'); } $path = $thumbnailFileName ? Path::combine($resourceType->getDirectory(), $folderPath, ResizedImage::DIR, $fileName, $thumbnailFileName) : Path::combine($resourceType->getDirectory(), $folderPath, $fileName); if (isset($this->backendConfig['baseUrl'])) { return Path::combine($this->backendConfig['baseUrl'], Utils::encodeURLParts($path)); } $baseAdapter = $this->getBaseAdapter(); if (method_exists($baseAdapter, 'getFileUrl')) { return $baseAdapter->getFileUrl($path); } return null; }
/** * Adds an error to the array of errors of the current file. * * @param int $number error number * * @see Error */ public function addError($number) { $this->errors[] = array('number' => $number, 'name' => $this->getFilename(), 'type' => $this->resourceType->getName(), 'folder' => $this->folder); }
/** * Checks if directory contains subdirectories * * @param Backend $backend * @param ResourceType $resourceType * @param string $clientPath * @param Acl $acl * * @return bool */ public function containsDirectories(Backend $backend, ResourceType $resourceType, $clientPath, Acl $acl) { $location = rtrim($this->applyPathPrefix(Path::combine($resourceType->getDirectory(), $clientPath)), '/\\') . '/'; if (!is_dir($location) || false === ($fh = @opendir($location))) { return false; } $hasChildren = false; $resourceTypeName = $resourceType->getName(); $clientPath = rtrim($clientPath, '/\\') . '/'; while (false !== ($filename = readdir($fh))) { if ($filename == '.' || $filename == '..') { continue; } if (is_dir($location . $filename)) { if (!$acl->isAllowed($resourceTypeName, $clientPath . $filename, Permission::FOLDER_VIEW)) { continue; } if ($backend->isHiddenFolder($filename)) { continue; } $hasChildren = true; break; } } closedir($fh); return $hasChildren; }
/** * Deletes all thumbnails under the given path defined by the resource type, * path and file name. * * @param ResourceType $resourceType * @param string $path * @param string $fileName * * @return bool `true` if deleted successfully */ public function deleteThumbnails(ResourceType $resourceType, $path, $fileName = null) { $path = Path::combine($this->getThumbnailsPath(), $resourceType->getName(), $path, $fileName); if ($this->thumbsBackend->has($path)) { return $this->thumbsBackend->deleteDir($path); } return false; }