Пример #1
0
 /**
  * Returns the url to the default avatar for a user.
  *
  * @param array $user The user to get the default avatar for.
  * @param string $size The size of avatar to return (only respected for dashboard-uploaded default avatars).
  * @return string The url to the default avatar image.
  */
 public static function getDefaultAvatarUrl($user = [], $size = 'thumbnail')
 {
     if (!empty($user) && function_exists('UserPhotoDefaultUrl')) {
         return userPhotoDefaultUrl($user);
     }
     if ($avatar = c('Garden.DefaultAvatar', false)) {
         if (strpos($avatar, 'defaultavatar/') !== false) {
             if ($size == 'thumbnail') {
                 return Gdn_UploadImage::url(changeBasename($avatar, 'n%s'));
             } elseif ($size == 'profile') {
                 return Gdn_UploadImage::url(changeBasename($avatar, 'p%s'));
             }
         }
         return $avatar;
     }
     return asset('applications/dashboard/design/images/defaulticon.png', true);
 }
 /**
  * Settings page for uploading, deleting and cropping the default avatar.
  *
  * @throws Exception
  */
 public function defaultAvatar()
 {
     $this->permission('Garden.Community.Manage');
     $this->addSideMenu('dashboard/settings/avatars');
     $this->title(t('Default Avatar'));
     $this->addJsFile('avatars.js');
     $validation = new Gdn_Validation();
     $configurationModel = new Gdn_ConfigurationModel($validation);
     $this->Form->setModel($configurationModel);
     if (($avatar = c('Garden.DefaultAvatar')) && $this->isUploadedDefaultAvatar($avatar)) {
         //Get the image source so we can manipulate it in the crop module.
         $upload = new Gdn_UploadImage();
         $thumbnailSize = c('Garden.Thumbnail.Size', 40);
         $basename = changeBasename($avatar, "p%s");
         $source = $upload->copyLocal($basename);
         //Set up cropping.
         $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
         $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($avatar, "n%s")));
         $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($avatar, "p%s")));
         $this->setData('crop', $crop);
     } else {
         $this->setData('avatar', UserModel::getDefaultAvatarUrl());
     }
     if (!$this->Form->authenticatedPostBack()) {
         $this->Form->setData($configurationModel->Data);
     } else {
         if ($this->Form->save() !== false) {
             $upload = new Gdn_UploadImage();
             $newAvatar = false;
             if ($tmpAvatar = $upload->validateUpload('DefaultAvatar', false)) {
                 // New upload
                 $thumbOptions = array('Crop' => true, 'SaveGif' => c('Garden.Thumbnail.SaveGif'));
                 $newAvatar = $this->saveDefaultAvatars($tmpAvatar, $thumbOptions);
             } else {
                 if ($avatar && $crop && $crop->isCropped()) {
                     // New thumbnail
                     $tmpAvatar = $source;
                     $thumbOptions = array('Crop' => true, 'SourceX' => $crop->getCropXValue(), 'SourceY' => $crop->getCropYValue(), 'SourceWidth' => $crop->getCropWidth(), 'SourceHeight' => $crop->getCropHeight());
                     $newAvatar = $this->saveDefaultAvatars($tmpAvatar, $thumbOptions);
                 }
             }
             if ($this->Form->errorCount() == 0) {
                 if ($newAvatar) {
                     $this->deleteDefaultAvatars($avatar);
                     $avatar = c('Garden.DefaultAvatar');
                     $thumbnailSize = c('Garden.Thumbnail.Size', 40);
                     // Update crop properties.
                     $basename = changeBasename($avatar, "p%s");
                     $source = $upload->copyLocal($basename);
                     $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
                     $crop->setSize($thumbnailSize, $thumbnailSize);
                     $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($avatar, "n%s")));
                     $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($avatar, "p%s")));
                     $this->setData('crop', $crop);
                 }
             }
             $this->informMessage(t("Your settings have been saved."));
         }
     }
     $this->render();
 }
Пример #3
0
 /**
  * Form for adding an email image.
  * Exposes the Garden.EmailTemplate.Image setting.
  * Garden.EmailTemplate.Image must be an upload.
  *
  * Saves the image based on 2 config settings:
  * Garden.EmailTemplate.ImageMaxWidth (default 400px) and
  * Garden.EmailTemplate.ImageMaxHeight (default 300px)
  *
  * @throws Gdn_UserException
  */
 public function emailImage()
 {
     if (!Gdn::session()->checkPermission('Garden.Community.Manage')) {
         throw permissionException();
     }
     $this->addJsFile('email.js');
     $this->addSideMenu('dashboard/settings/email');
     $image = c('Garden.EmailTemplate.Image');
     $this->Form = new Gdn_Form();
     $validation = new Gdn_Validation();
     $configurationModel = new Gdn_ConfigurationModel($validation);
     // Set the model on the form.
     $this->Form->setModel($configurationModel);
     if ($this->Form->authenticatedPostBack() !== false) {
         try {
             $upload = new Gdn_UploadImage();
             // Validate the upload
             $tmpImage = $upload->validateUpload('EmailImage', false);
             if ($tmpImage) {
                 // Generate the target image name
                 $targetImage = $upload->generateTargetName(PATH_UPLOADS);
                 $imageBaseName = pathinfo($targetImage, PATHINFO_BASENAME);
                 // Delete any previously uploaded images.
                 if ($image) {
                     $upload->delete($image);
                 }
                 // Save the uploaded image
                 $parts = $upload->saveImageAs($tmpImage, $imageBaseName, c('Garden.EmailTemplate.ImageMaxWidth', 400), c('Garden.EmailTemplate.ImageMaxHeight', 300));
                 $imageBaseName = $parts['SaveName'];
                 saveToConfig('Garden.EmailTemplate.Image', $imageBaseName);
                 $this->setData('EmailImage', Gdn_UploadImage::url($imageBaseName));
             } else {
                 $this->Form->addError(t('There\'s been an error uploading the image. Your email logo can uploaded in one of the following filetypes: gif, jpg, png'));
             }
         } catch (Exception $ex) {
             $this->Form->addError($ex);
         }
     }
     $this->render();
 }
Пример #4
0
 /**
  * Retrieves default values for the email image.
  *
  * @return array An array representing an image.
  */
 public function getDefaultEmailImage()
 {
     $image = array();
     if (c('Garden.EmailTemplate.Image', '')) {
         $image['source'] = Gdn_UploadImage::url(c('Garden.EmailTemplate.Image'));
     }
     $image['link'] = url('/', true);
     $image['alt'] = c('Garden.LogoTitle', c('Garden.Title', ''));
     return $image;
 }
Пример #5
0
 /**
  * Endpoint for retrieving current email image url.
  */
 public function emailImageUrl()
 {
     $this->deliveryMethod(DELIVERY_METHOD_JSON);
     $this->deliveryType(DELIVERY_TYPE_DATA);
     $image = c('Garden.EmailTemplate.Image');
     if ($image) {
         $image = Gdn_UploadImage::url($image);
     }
     $this->setData('EmailImage', $image);
     $this->render();
 }
Пример #6
0
 /**
  * Set user's photo (avatar).
  *
  * @since 2.0.0
  * @access public
  *
  * @param mixed $userReference Unique identifier, possible username or ID.
  * @param string $username The username.
  * @param string $userID The user's ID.
  *
  * @throws Exception
  * @throws Gdn_UserException
  */
 public function picture($userReference = '', $username = '', $userID = '')
 {
     $this->addJsFile('profile.js');
     if (!$this->CanEditPhotos) {
         throw forbiddenException('@Editing user photos has been disabled.');
     }
     // Permission checks
     $this->permission(array('Garden.Profiles.Edit', 'Moderation.Profiles.Edit', 'Garden.ProfilePicture.Edit'), false);
     $session = Gdn::session();
     if (!$session->isValid()) {
         $this->Form->addError('You must be authenticated in order to use this form.');
     }
     // Check ability to manipulate image
     if (function_exists('gd_info')) {
         $gdInfo = gd_info();
         $gdVersion = preg_replace('/[a-z ()]+/i', '', $gdInfo['GD Version']);
         if ($gdVersion < 2) {
             throw new Exception(sprintf(t("This installation of GD is too old (v%s). Vanilla requires at least version 2 or compatible."), $gdVersion));
         }
     } else {
         throw new Exception(sprintf(t("Unable to detect PHP GD installed on this system. Vanilla requires GD version 2 or better.")));
     }
     // Get user data & prep form.
     if ($this->Form->authenticatedPostBack() && $this->Form->getFormValue('UserID')) {
         $userID = $this->Form->getFormValue('UserID');
     }
     $this->getUserInfo($userReference, $username, $userID, true);
     $validation = new Gdn_Validation();
     $configurationModel = new Gdn_ConfigurationModel($validation);
     $this->Form->setModel($configurationModel);
     $avatar = $this->User->Photo;
     if ($avatar === null) {
         $avatar = UserModel::getDefaultAvatarUrl();
     }
     $source = '';
     $crop = null;
     if ($this->isUploadedAvatar($avatar)) {
         // Get the image source so we can manipulate it in the crop module.
         $upload = new Gdn_UploadImage();
         $thumbnailSize = c('Garden.Thumbnail.Size', 40);
         $basename = changeBasename($avatar, "p%s");
         $source = $upload->copyLocal($basename);
         // Set up cropping.
         $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
         $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($avatar, "n%s")));
         $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($avatar, "p%s")));
         $this->setData('crop', $crop);
     } else {
         $this->setData('avatar', $avatar);
     }
     if (!$this->Form->authenticatedPostBack()) {
         $this->Form->setData($configurationModel->Data);
     } else {
         if ($this->Form->save() !== false) {
             $upload = new Gdn_UploadImage();
             $newAvatar = false;
             if ($tmpAvatar = $upload->validateUpload('Avatar', false)) {
                 // New upload
                 $thumbOptions = array('Crop' => true, 'SaveGif' => c('Garden.Thumbnail.SaveGif'));
                 $newAvatar = $this->saveAvatars($tmpAvatar, $thumbOptions, $upload);
             } else {
                 if ($avatar && $crop && $crop->isCropped()) {
                     // New thumbnail
                     $tmpAvatar = $source;
                     $thumbOptions = array('Crop' => true, 'SourceX' => $crop->getCropXValue(), 'SourceY' => $crop->getCropYValue(), 'SourceWidth' => $crop->getCropWidth(), 'SourceHeight' => $crop->getCropHeight());
                     $newAvatar = $this->saveAvatars($tmpAvatar, $thumbOptions);
                 }
             }
             if ($this->Form->errorCount() == 0) {
                 if ($newAvatar !== false) {
                     $thumbnailSize = c('Garden.Thumbnail.Size', 40);
                     // Update crop properties.
                     $basename = changeBasename($newAvatar, "p%s");
                     $source = $upload->copyLocal($basename);
                     $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
                     $crop->setSize($thumbnailSize, $thumbnailSize);
                     $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($newAvatar, "n%s")));
                     $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($newAvatar, "p%s")));
                     $this->setData('crop', $crop);
                 }
             }
             if ($this->deliveryType() === DELIVERY_TYPE_VIEW) {
                 $this->jsonTarget('', '', 'Refresh');
                 $this->RedirectUrl = userUrl($this->User);
             }
             $this->informMessage(t("Your settings have been saved."));
         }
     }
     if (val('SideMenuModule', val('Panel', val('Assets', $this)))) {
         /** @var SideMenuModule $sidemenu */
         $sidemenu = $this->Assets['Panel']['SideMenuModule'];
         $sidemenu->highlightRoute('/profile/picture');
     }
     $this->title(t('Change Picture'));
     $this->_setBreadcrumbs(t('Change My Picture'), userUrl($this->User, '', 'picture'));
     $this->render('picture', 'profile', 'dashboard');
 }