/** * Save an image from a field and delete any old image that's been uploaded. * * @param string $Field The name of the field. The image will be uploaded with the _New extension while the current image will be just the field name. * @param array $Options */ public function saveImage($Field, $Options = array()) { $Upload = new Gdn_UploadImage(); $FileField = str_replace('.', '_', $Field); if (!getValueR("{$FileField}_New.name", $_FILES)) { trace("{$Field} not uploaded, returning."); return false; } // First make sure the file is valid. try { $TmpName = $Upload->validateUpload($FileField . '_New', true); if (!$TmpName) { return false; // no file uploaded. } } catch (Exception $Ex) { $this->addError($Ex); return false; } // Get the file extension of the file. $Ext = val('OutputType', $Options, trim($Upload->getUploadedFileExtension(), '.')); if ($Ext == 'jpeg') { $Ext = 'jpg'; } Trace($Ext, 'Ext'); // The file is valid so let's come up with its new name. if (isset($Options['Name'])) { $Name = $Options['Name']; } elseif (isset($Options['Prefix'])) { $Name = $Options['Prefix'] . md5(microtime()) . '.' . $Ext; } else { $Name = md5(microtime()) . '.' . $Ext; } // We need to parse out the size. $Size = val('Size', $Options); if ($Size) { if (is_numeric($Size)) { touchValue('Width', $Options, $Size); touchValue('Height', $Options, $Size); } elseif (preg_match('`(\\d+)x(\\d+)`i', $Size, $M)) { touchValue('Width', $Options, $M[1]); touchValue('Height', $Options, $M[2]); } } trace($Options, "Saving image {$Name}."); try { $Parsed = $Upload->saveImageAs($TmpName, $Name, val('Height', $Options, ''), val('Width', $Options, ''), $Options); trace($Parsed, 'Saved Image'); $Current = $this->getFormValue($Field); if ($Current && val('DeleteOriginal', $Options, true)) { // Delete the current image. trace("Deleting original image: {$Current}."); if ($Current) { $Upload->delete($Current); } } // Set the current value. $this->setFormValue($Field, $Parsed['SaveName']); } catch (Exception $Ex) { $this->addError($Ex); } }
/** * 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(); }
/** * Settings page for HTML email styling. * * Exposes config settings: * Garden.EmailTemplate.BackgroundColor * Garden.EmailTemplate.ButtonBackgroundColor * Garden.EmailTemplate.ButtonTextColor * Garden.EmailTemplate.Image * * Saves the image based on 2 config settings: * Garden.EmailTemplate.ImageMaxWidth (default 400px) and * Garden.EmailTemplate.ImageMaxHeight (default 300px) * * @throws Gdn_UserException */ public function emailStyles() { // Set default colors if (!c('Garden.EmailTemplate.TextColor')) { saveToConfig('Garden.EmailTemplate.TextColor', EmailTemplate::DEFAULT_TEXT_COLOR, false); } if (!c('Garden.EmailTemplate.BackgroundColor')) { saveToConfig('Garden.EmailTemplate.BackgroundColor', EmailTemplate::DEFAULT_BACKGROUND_COLOR, false); } if (!c('Garden.EmailTemplate.ContainerBackgroundColor')) { saveToConfig('Garden.EmailTemplate.ContainerBackgroundColor', EmailTemplate::DEFAULT_CONTAINER_BACKGROUND_COLOR, false); } if (!c('Garden.EmailTemplate.ButtonTextColor')) { saveToConfig('Garden.EmailTemplate.ButtonTextColor', EmailTemplate::DEFAULT_BUTTON_TEXT_COLOR, false); } if (!c('Garden.EmailTemplate.ButtonBackgroundColor')) { saveToConfig('Garden.EmailTemplate.ButtonBackgroundColor', EmailTemplate::DEFAULT_BUTTON_BACKGROUND_COLOR, false); } $this->permission('Garden.Settings.Manage'); $this->setHighlightRoute('dashboard/settings/emailstyles'); $this->addJsFile('email.js'); // Get the current logo. $image = c('Garden.EmailTemplate.Image'); if ($image) { $image = ltrim($image, '/'); $this->setData('EmailImage', Gdn_UploadImage::url($image)); } $this->Form = new Gdn_Form(); $validation = new Gdn_Validation(); $configurationModel = new Gdn_ConfigurationModel($validation); $configurationModel->setField(array('Garden.EmailTemplate.TextColor', 'Garden.EmailTemplate.BackgroundColor', 'Garden.EmailTemplate.ContainerBackgroundColor', 'Garden.EmailTemplate.ButtonTextColor', 'Garden.EmailTemplate.ButtonBackgroundColor')); // Set the model on the form. $this->Form->setModel($configurationModel); // If seeing the form for the first time... if ($this->Form->authenticatedPostBack() === false) { // Apply the config settings to the form. $this->Form->setData($configurationModel->Data); } else { $image = c('Garden.EmailTemplate.Image'); $upload = new Gdn_UploadImage(); if ($upload->isUpload('EmailImage')) { try { $tmpImage = $upload->validateUpload('EmailImage'); 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)); } } catch (Exception $ex) { $this->Form->addError($ex); } } if ($this->Form->save() !== false) { $this->informMessage(t("Your settings have been saved.")); } } $this->render(); }
/** * Set the icon for an addon. * * @param int $AddonID Specified addon id. * @throws Exception Addon not found. */ public function icon($AddonID = '') { $Session = Gdn::session(); if (!$Session->isValid()) { $this->Form->addError('You must be authenticated in order to use this form.'); } $Addon = $this->AddonModel->getID($AddonID); if (!$Addon) { throw notFoundException('Addon'); } if ($Session->UserID != $Addon['InsertUserID']) { $this->permission('Addons.Addon.Manage'); } $this->addModule('AddonHelpModule', 'Panel'); $this->Form->setModel($this->AddonModel); $this->Form->addHidden('AddonID', $AddonID); if ($this->Form->authenticatedPostBack()) { $UploadImage = new Gdn_UploadImage(); try { // Validate the upload $imageLocation = $UploadImage->validateUpload('Icon'); $TargetImage = $this->saveIcon($imageLocation); } catch (Exception $ex) { $this->Form->addError($ex); } // If there were no errors, remove the old picture and insert the picture if ($this->Form->errorCount() == 0) { if ($Addon['Icon']) { $UploadImage->delete($Addon['Icon']); } $this->AddonModel->save(array('AddonID' => $AddonID, 'Icon' => $TargetImage)); } // If there were no problems, redirect back to the addon if ($this->Form->errorCount() == 0) { $this->RedirectUrl = Url('/addon/' . AddonModel::slug($Addon)); } } $this->render(); }
/** * Set user's thumbnail (crop & center photo). * * @since 2.0.0 * @access public * @param mixed $UserReference Unique identifier, possible username or ID. * @param string $Username . */ public function thumbnail($UserReference = '', $Username = '') { if (!c('Garden.Profile.EditPhotos', true)) { throw forbiddenException('@Editing user photos has been disabled.'); } // Initial permission checks (valid user) $this->permission('Garden.SignIn.Allow'); $Session = Gdn::session(); if (!$Session->isValid()) { $this->Form->addError('You must be authenticated in order to use this form.'); } // Need some extra JS // jcrop update jan28, 2014 as jQuery upgrade to 1.10.2 no longer // supported browser() $this->addJsFile('jquery.jcrop.min.js'); $this->addJsFile('profile.js'); $this->getUserInfo($UserReference, $Username, '', true); // Permission check (correct user) if ($this->User->UserID != $Session->UserID && !checkPermission('Garden.Users.Edit') && !checkPermission('Moderation.Profiles.Edit')) { throw new Exception(t('You cannot edit the thumbnail of another member.')); } // Form prep $this->Form->setModel($this->UserModel); $this->Form->addHidden('UserID', $this->User->UserID); // Confirm we have a photo to manipulate if (!$this->User->Photo) { $this->Form->addError('You must first upload a picture before you can create a thumbnail.'); } // Define the thumbnail size $this->ThumbSize = Gdn::config('Garden.Thumbnail.Size', 40); // Define the source (profile sized) picture & dimensions. $Basename = changeBasename($this->User->Photo, 'p%s'); $Upload = new Gdn_UploadImage(); $PhotoParsed = Gdn_Upload::Parse($Basename); $Source = $Upload->CopyLocal($Basename); if (!$Source) { $this->Form->addError('You cannot edit the thumbnail of an externally linked profile picture.'); } else { $this->SourceSize = getimagesize($Source); } // We actually need to upload a new file to help with cdb ttls. $NewPhoto = $Upload->generateTargetName('userpics', trim(pathinfo($this->User->Photo, PATHINFO_EXTENSION), '.'), true); // Add some more hidden form fields for jcrop $this->Form->addHidden('x', '0'); $this->Form->addHidden('y', '0'); $this->Form->addHidden('w', $this->ThumbSize); $this->Form->addHidden('h', $this->ThumbSize); $this->Form->addHidden('HeightSource', $this->SourceSize[1]); $this->Form->addHidden('WidthSource', $this->SourceSize[0]); $this->Form->addHidden('ThumbSize', $this->ThumbSize); if ($this->Form->authenticatedPostBack() === true) { try { // Get the dimensions from the form. Gdn_UploadImage::SaveImageAs($Source, changeBasename($NewPhoto, 'n%s'), $this->ThumbSize, $this->ThumbSize, array('Crop' => true, 'SourceX' => $this->Form->getValue('x'), 'SourceY' => $this->Form->getValue('y'), 'SourceWidth' => $this->Form->getValue('w'), 'SourceHeight' => $this->Form->getValue('h'))); // Save new profile picture. $Parsed = $Upload->SaveAs($Source, changeBasename($NewPhoto, 'p%s')); $UserPhoto = sprintf($Parsed['SaveFormat'], $NewPhoto); // Save the new photo info. Gdn::userModel()->setField($this->User->UserID, 'Photo', $UserPhoto); // Remove the old profile picture. @$Upload->delete($Basename); } catch (Exception $Ex) { $this->Form->addError($Ex); } // If there were no problems, redirect back to the user account if ($this->Form->errorCount() == 0) { redirect(userUrl($this->User, '', 'picture')); $this->informMessage(sprite('Check', 'InformSprite') . t('Your changes have been saved.'), 'Dismissable AutoDismiss HasSprite'); } } // Delete the source image if it is externally hosted. if ($PhotoParsed['Type']) { @unlink($Source); } $this->title(t('Edit My Thumbnail')); $this->_setBreadcrumbs(t('Edit My Thumbnail'), '/profile/thumbnail'); $this->render(); }