}
}
-- /**
- * Sets an avatar for a given user. The given file will be renamed and is gone after this method call.
- * Validates the 'uploadCoverPhoto' method.
-- *
- * @throws UserInputException If none or more than one user is given.
- * @throws \InvalidArgumentException If the given file is not an image or is incorrectly sized.
- * @since 5.5
- * @throws PermissionDeniedException
- * @throws UserInputException
- * @since 3.1
-- */
- public function setAvatar(): array
- public function validateUploadCoverPhoto()
-- {
- $user = $this->getSingleObject();
-
- $imageData = \getimagesize($this->parameters['fileLocation']);
- WCF::getSession()->checkPermissions(['user.profile.coverPhoto.canUploadCoverPhoto']);
--
- if (!$imageData) {
- throw new \InvalidArgumentException("The given file is not an image.");
- $this->readInteger('userID', true);
- // The `userID` parameter did not exist in 3.1, defaulting to the own user for backwards compatibility.
- if (!$this->parameters['userID']) {
- $this->parameters['userID'] = WCF::getUser()->userID;
-- }
--
- if (
- ($imageData[0] != UserAvatar::AVATAR_SIZE || $imageData[1] != UserAvatar::AVATAR_SIZE)
- && ($imageData[0] != UserAvatar::AVATAR_SIZE_2X || $imageData[1] != UserAvatar::AVATAR_SIZE_2X)
- $this->user = new User($this->parameters['userID']);
- if (!$this->user->userID) {
- throw new UserInputException('userID');
- } elseif ($this->user->userID == WCF::getUser()->userID && WCF::getUser()->disableCoverPhoto) {
- throw new PermissionDeniedException();
- } elseif (
- $this->user->userID != WCF::getUser()->userID
- && (!$this->user->canEdit() || !WCF::getSession()->getPermission('admin.user.canDisableCoverPhoto'))
-- ) {
- throw new \InvalidArgumentException(
- \sprintf(
- "The given file does not have the size of %dx%d",
- UserAvatar::AVATAR_SIZE,
- UserAvatar::AVATAR_SIZE
- )
- );
- throw new PermissionDeniedException();
-- }
--
- $data = [
- 'avatarName' => $this->parameters['filename'] ?? \basename($this->parameters['fileLocation']),
- 'avatarExtension' => ImageUtil::getExtensionByMimeType($imageData['mime']),
- 'width' => $imageData[0],
- 'height' => $imageData[1],
- 'userID' => $user->userID,
- 'fileHash' => \sha1_file($this->parameters['fileLocation']),
- ];
- // validate uploaded file
- if (!isset($this->parameters['__files']) || \count($this->parameters['__files']->getFiles()) != 1) {
- throw new UserInputException('files');
- }
--
- // create avatar
- $avatar = UserAvatarEditor::create($data);
- /** @var UploadHandler $uploadHandler */
- $uploadHandler = $this->parameters['__files'];
--
- try {
- // check avatar directory
- // and create subdirectory if necessary
- $dir = \dirname($avatar->getLocation(null, false));
- if (!\file_exists($dir)) {
- FileUtil::makePath($dir);
- }
- $this->uploadFile = $uploadHandler->getFiles()[0];
--
- \rename($this->parameters['fileLocation'], $avatar->getLocation(null, false));
- $uploadHandler->validateFiles(new UserCoverPhotoUploadFileValidationStrategy());
- }
--
- // Fix the permissions of the file in case the source file was created with restricted
- // permissions (e.g. 0600 instead of 0644). Without this the file might not be readable
- // for the web server if it runs with a different system user.
- FileUtil::makeWritable($avatar->getLocation(null, false));
- /**
- * Uploads a cover photo.
- *
- * @since 3.1
- */
- public function uploadCoverPhoto()
- {
- $saveStrategy = new UserCoverPhotoUploadFileSaveStrategy(
- (!empty($this->parameters['userID']) ? \intval($this->parameters['userID']) : WCF::getUser()->userID)
- );
- /** @noinspection PhpUndefinedMethodInspection */
- $this->parameters['__files']->saveFiles($saveStrategy);
--
- // Create the WebP variant or the JPEG fallback of the avatar.
- $avatarEditor = new UserAvatarEditor($avatar);
- if ($avatarEditor->createAvatarVariant()) {
- $avatar = new UserAvatar($avatar->avatarID);
- }
- if ($this->uploadFile->getValidationErrorType()) {
- return [
- 'filesize' => $this->uploadFile->getFilesize(),
- 'errorMessage' => WCF::getLanguage()->getDynamicVariable(
- 'wcf.user.coverPhoto.upload.error.' . $this->uploadFile->getValidationErrorType(),
- [
- 'file' => $this->uploadFile,
- ]
- ),
- 'errorType' => $this->uploadFile->getValidationErrorType(),
- ];
- } else {
- return [
- 'url' => $saveStrategy->getCoverPhoto()->getURL(),
- ];
- }
- }
--
- // update user
- $userEditor = new UserEditor($user->getDecoratedObject());
- $userEditor->update([
- 'avatarID' => $avatar->avatarID,
- ]);
- } catch (\Exception $e) {
- $editor = new UserAvatarEditor($avatar);
- $editor->delete();
- /**
- * Validates the `deleteCoverPhoto` action.
- *
- * @throws PermissionDeniedException
- * @throws UserInputException
- */
- public function validateDeleteCoverPhoto()
- {
- $this->readInteger('userID', true);
- // The `userID` parameter did not exist in 3.1, defaulting to the own user for backwards compatibility.
- if (!$this->parameters['userID']) {
- $this->parameters['userID'] = WCF::getUser()->userID;
- }
--
- throw $e;
- $this->user = new User($this->parameters['userID']);
- if (!$this->user->userID) {
- throw new UserInputException('userID');
- } elseif (
- $this->user->userID != WCF::getUser()->userID
- && (!$this->user->canEdit() || !WCF::getSession()->getPermission('admin.user.canDisableCoverPhoto'))
- ) {
- throw new PermissionDeniedException();
-- }
- }
--
- // delete old avatar
- if ($user->avatarID) {
- (new UserAvatarAction([$user->avatarID], 'delete'))->executeAction();
- /**
- * Deletes the cover photo of the active user.
- *
- * @return string[] link to the new cover photo
- */
- public function deleteCoverPhoto()
- {
- if ($this->user->coverPhotoHash) {
- UserProfileRuntimeCache::getInstance()->getObject($this->user->userID)->getCoverPhoto()->delete();
-
- (new UserEditor($this->user))->update([
- 'coverPhotoHash' => null,
- 'coverPhotoExtension' => '',
- ]);
-- }
--
- // reset user storage
- UserStorageHandler::getInstance()->reset([$user->userID], 'avatar');
- // force-reload the user profile to use a predictable code-path to fetch the cover photo
- UserProfileRuntimeCache::getInstance()->removeObject($this->user->userID);
--
-- return [
- 'avatar' => $avatar,
- 'url' => UserProfileRuntimeCache::getInstance()->getObject($this->user->userID)->getCoverPhoto()->getURL(),
-- ];
-- }
--
/**
* Returns the user option handler object.
*