public function PostController_Imageupload_create() { try { $UploadImage = new Gdn_UploadImage(); $TmpImage = $UploadImage->ValidateUpload('image_file'); // Generate the target image name. $TargetImage = $UploadImage->GenerateTargetName(PATH_UPLOADS . '/imageupload', '', TRUE); $Props = $UploadImage->SaveImageAs($TmpImage, $TargetImage, C('Plugins.UploadImage.MaxHeight', ''), C('Plugins.UploadImage.MaxWidth', 650)); echo json_encode(array('url' => $Props['Url'], 'name' => $UploadImage->GetUploadedFileName())); } catch (Exception $e) { header('HTTP/1.0 400', TRUE, 400); echo $e; } }
/** * Touch icon management screen. * * @since 1.0 * @access public */ public function SettingsController_TouchIcon_Create($Sender) { $Sender->Permission('Garden.Settings.Manage'); $Sender->AddSideMenu('settings/touchicon'); $Sender->Title(T('Touch Icon')); if ($Sender->Form->AuthenticatedPostBack()) { $Upload = new Gdn_UploadImage(); try { // Validate the upload $TmpImage = $Upload->ValidateUpload('TouchIcon', FALSE); if ($TmpImage) { // Save the uploaded image. $TouchIconPath = 'banner/touchicon_' . substr(md5(microtime()), 16) . '.png'; $ImageInfo = $Upload->SaveImageAs($TmpImage, $TouchIconPath, 114, 114, array('OutputType' => 'png', 'ImageQuality' => '8')); SaveToConfig('Garden.TouchIcon', $ImageInfo['SaveName']); } } catch (Exception $ex) { $Sender->Form->AddError($ex->getMessage()); } $Sender->InformMessage(T("Your icon has been saved.")); } $Sender->SetData('Path', $this->getIconUrl()); $Sender->Render($this->GetView('touchicon.php')); }
public function Picture($UserReference = '', $Username = '') { $this->Permission('Garden.SignIn.Allow'); $Session = Gdn::Session(); if (!$Session->IsValid()) { $this->Form->AddError('You must be authenticated in order to use this form.'); } $ImageManipOk = FALSE; 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."))); } $this->GetUserInfo($UserReference, $Username); $this->Form->SetModel($this->UserModel); $this->Form->AddHidden('UserID', $this->User->UserID); if ($this->Form->AuthenticatedPostBack() === TRUE) { $UploadImage = new Gdn_UploadImage(); try { // Validate the upload $TmpImage = $UploadImage->ValidateUpload('Picture'); // Generate the target image name $TargetImage = $UploadImage->GenerateTargetName(PATH_ROOT . DS . 'uploads'); $ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME); // Delete any previously uploaded images @unlink(PATH_ROOT . '/uploads/' . ChangeBasename($this->User->Photo, 'p%s')); // Don't delete this one because it hangs around in activity streams: // @unlink(PATH_ROOT.'/uploads/'.ChangeBasename($this->User->Photo, 't%s')); @unlink(PATH_ROOT . '/uploads/' . ChangeBasename($this->User->Photo, 'n%s')); // Make sure the avatars folder exists. if (!file_exists(PATH_ROOT . '/uploads/userpics')) { mkdir(PATH_ROOT . '/uploads/userpics'); } // Save the uploaded image in profile size $UploadImage->SaveImageAs($TmpImage, PATH_ROOT . '/uploads/userpics/p' . $ImageBaseName, Gdn::Config('Garden.Profile.MaxHeight', 1000), Gdn::Config('Garden.Profile.MaxWidth', 250)); // Save the uploaded image in preview size $UploadImage->SaveImageAs($TmpImage, PATH_ROOT . '/uploads/userpics/t' . $ImageBaseName, Gdn::Config('Garden.Preview.MaxHeight', 100), Gdn::Config('Garden.Preview.MaxWidth', 75)); // Save the uploaded image in thumbnail size $ThumbSize = Gdn::Config('Garden.Thumbnail.Size', 50); $UploadImage->SaveImageAs($TmpImage, PATH_ROOT . '/uploads/userpics/n' . $ImageBaseName, $ThumbSize, $ThumbSize, TRUE); } catch (Exception $ex) { $this->Form->AddError($ex->getMessage()); } // If there were no errors, associate the image with the user if ($this->Form->ErrorCount() == 0) { if (!$this->UserModel->Save(array('UserID' => $this->User->UserID, 'Photo' => 'userpics/' . $ImageBaseName))) { $this->Form->SetValidationResults($this->UserModel->ValidationResults()); } } // If there were no problems, redirect back to the user account if ($this->Form->ErrorCount() == 0) { Redirect('dashboard/profile/' . $UserReference); } } if ($this->Form->ErrorCount() > 0) { $this->DeliveryType(DELIVERY_TYPE_ALL); } $this->Render(); }
/** * Banner management screen. * * @since 2.0.0 * @access public */ public function Banner() { $this->Permission('Garden.Settings.Manage'); $this->AddSideMenu('dashboard/settings/banner'); $this->Title(T('Banner')); $Validation = new Gdn_Validation(); $ConfigurationModel = new Gdn_ConfigurationModel($Validation); $ConfigurationModel->SetField(array('Garden.HomepageTitle' => C('Garden.Title'), 'Garden.Title', 'Garden.Description')); // Set the model on the form. $this->Form->SetModel($ConfigurationModel); // Get the current logo. $Logo = C('Garden.Logo'); if ($Logo) { $Logo = ltrim($Logo, '/'); // Fix the logo path. if (StringBeginsWith($Logo, 'uploads/')) { $Logo = substr($Logo, strlen('uploads/')); } $this->SetData('Logo', $Logo); } // Get the current favicon. $Favicon = C('Garden.FavIcon'); $this->SetData('Favicon', $Favicon); $ShareImage = C('Garden.ShareImage'); $this->SetData('ShareImage', $ShareImage); // If seeing the form for the first time... if (!$this->Form->AuthenticatedPostBack()) { // Apply the config settings to the form. $this->Form->SetData($ConfigurationModel->Data); } else { // Define some validation rules for the fields being saved $ConfigurationModel->Validation->ApplyRule('Garden.Title', 'Required'); $SaveData = array(); if ($this->Form->Save() !== FALSE) { $Upload = new Gdn_Upload(); try { // Validate the upload $TmpImage = $Upload->ValidateUpload('Logo', FALSE); if ($TmpImage) { // Generate the target image name $TargetImage = $Upload->GenerateTargetName(PATH_UPLOADS); $ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME); // Delete any previously uploaded images. if ($Logo) { $Upload->Delete($Logo); } // Save the uploaded image $Parts = $Upload->SaveAs($TmpImage, $ImageBaseName); $ImageBaseName = $Parts['SaveName']; $SaveData['Garden.Logo'] = $ImageBaseName; $this->SetData('Logo', $ImageBaseName); } $ImgUpload = new Gdn_UploadImage(); $TmpFavicon = $ImgUpload->ValidateUpload('Favicon', FALSE); if ($TmpFavicon) { $ICOName = 'favicon_' . substr(md5(microtime()), 16) . '.ico'; if ($Favicon) { $Upload->Delete($Favicon); } // Resize the to a png. $Parts = $ImgUpload->SaveImageAs($TmpFavicon, $ICOName, 16, 16, array('OutputType' => 'ico', 'Crop' => TRUE)); $SaveData['Garden.FavIcon'] = $Parts['SaveName']; $this->SetData('Favicon', $Parts['SaveName']); } $TmpShareImage = $Upload->ValidateUpload('ShareImage', FALSE); if ($TmpShareImage) { $TargetImage = $Upload->GenerateTargetName(PATH_UPLOADS, FALSE); $ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME); if ($ShareImage) { $Upload->Delete($ShareImage); } $Parts = $Upload->SaveAs($TmpShareImage, $ImageBaseName); $SaveData['Garden.ShareImage'] = $Parts['SaveName']; $this->SetData('ShareImage', $Parts['SaveName']); } } catch (Exception $ex) { $this->Form->AddError($ex); } // If there were no errors, save the path to the logo in the config if ($this->Form->ErrorCount() == 0) { SaveToConfig($SaveData); } $this->InformMessage(T("Your settings have been saved.")); } } $this->Render(); }
public function Picture($UserReference = '') { $this->Permission('Garden.SignIn.Allow'); $Session = Gdn::Session(); if (!$Session->IsValid()) { $this->Form->AddError('You must be authenticated in order to use this form.'); } $this->GetUserInfo($UserReference); $this->Form->SetModel($this->UserModel); $this->Form->AddHidden('UserID', $this->User->UserID); if ($this->Form->AuthenticatedPostBack() === TRUE) { $UploadImage = new Gdn_UploadImage(); try { // Validate the upload $TmpImage = $UploadImage->ValidateUpload('Picture'); // Generate the target image name $TargetImage = $UploadImage->GenerateTargetName(PATH_ROOT . DS . 'uploads'); $ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME); // Save the uploaded image in large size $UploadImage->SaveImageAs($TmpImage, PATH_ROOT . DS . 'uploads' . DS . 'o' . $ImageBaseName, Gdn::Config('Garden.Picture.MaxHeight', 1000), Gdn::Config('Garden.Picture.MaxWidth', 1000)); // Save the uploaded image in profile size $UploadImage->SaveImageAs($TmpImage, PATH_ROOT . DS . 'uploads' . DS . 'p' . $ImageBaseName, Gdn::Config('Garden.Profile.MaxHeight', 1000), Gdn::Config('Garden.Profile.MaxWidth', 250)); // Save the uploaded image in preview size $UploadImage->SaveImageAs($TmpImage, PATH_ROOT . DS . 'uploads' . DS . 't' . $ImageBaseName, Gdn::Config('Garden.Preview.MaxHeight', 100), Gdn::Config('Garden.Preview.MaxWidth', 75)); // Save the uploaded image in thumbnail size $ThumbSize = Gdn::Config('Garden.Thumbnail.Size', 50); $UploadImage->SaveImageAs($TmpImage, PATH_ROOT . DS . 'uploads' . DS . 'n' . $ImageBaseName, $ThumbSize, $ThumbSize, TRUE); } catch (Exception $ex) { $this->Form->AddError($ex->getMessage()); } // If there were no errors, associate the image with the user if ($this->Form->ErrorCount() == 0) { $PhotoModel = new Model('Photo'); $PhotoID = $PhotoModel->Insert(array('Name' => $ImageBaseName)); if (!$this->UserModel->Save(array('UserID' => $this->User->UserID, 'PhotoID' => $PhotoID, 'Photo' => $ImageBaseName))) { $this->Form->SetValidationResults($this->UserModel->ValidationResults()); } } // If there were no problems, redirect back to the user account if ($this->Form->ErrorCount() == 0) { Redirect('garden/profile/' . $UserReference); } } $this->Render(); }
/** * Set user's photo (avatar). * * @since 2.0.0 * @access public * @param mixed $UserReference Unique identifier, possible username or ID. * @param string $Username. */ public function Picture($UserReference = '', $Username = '', $UserID = '') { if (!C('Garden.Profile.EditPhotos', TRUE)) { 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 $ImageManipOk = FALSE; 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. $this->GetUserInfo($UserReference, $Username, $UserID, TRUE); $this->Form->SetModel($this->UserModel); $this->Form->AddHidden('UserID', $this->User->UserID); if ($this->Form->AuthenticatedPostBack() === TRUE) { $UploadImage = new Gdn_UploadImage(); try { // Validate the upload $TmpImage = $UploadImage->ValidateUpload('Picture'); // Generate the target image name. $TargetImage = $UploadImage->GenerateTargetName(PATH_UPLOADS, '', TRUE); $Basename = pathinfo($TargetImage, PATHINFO_BASENAME); $Subdir = StringBeginsWith(dirname($TargetImage), PATH_UPLOADS . '/', FALSE, TRUE); // Delete any previously uploaded image. $UploadImage->Delete(ChangeBasename($this->User->Photo, 'p%s')); // Save the uploaded image in profile size. $Props = $UploadImage->SaveImageAs($TmpImage, "userpics/{$Subdir}/p{$Basename}", C('Garden.Profile.MaxHeight', 1000), C('Garden.Profile.MaxWidth', 250), array('SaveGif' => C('Garden.Thumbnail.SaveGif'))); $UserPhoto = sprintf($Props['SaveFormat'], "userpics/{$Subdir}/{$Basename}"); // // Save the uploaded image in preview size // $UploadImage->SaveImageAs( // $TmpImage, // 'userpics/t'.$ImageBaseName, // Gdn::Config('Garden.Preview.MaxHeight', 100), // Gdn::Config('Garden.Preview.MaxWidth', 75) // ); // Save the uploaded image in thumbnail size $ThumbSize = Gdn::Config('Garden.Thumbnail.Size', 40); $UploadImage->SaveImageAs($TmpImage, "userpics/{$Subdir}/n{$Basename}", $ThumbSize, $ThumbSize, array('Crop' => TRUE, 'SaveGif' => C('Garden.Thumbnail.SaveGif'))); } catch (Exception $Ex) { $this->Form->AddError($Ex); } // If there were no errors, associate the image with the user if ($this->Form->ErrorCount() == 0) { if (!$this->UserModel->Save(array('UserID' => $this->User->UserID, 'Photo' => $UserPhoto), array('CheckExisting' => TRUE))) { $this->Form->SetValidationResults($this->UserModel->ValidationResults()); } else { $this->User->Photo = $UserPhoto; } } // If there were no problems, redirect back to the user account if ($this->Form->ErrorCount() == 0) { $this->InformMessage(Sprite('Check', 'InformSprite') . T('Your changes have been saved.'), 'Dismissable AutoDismiss HasSprite'); Redirect($this->DeliveryType() == DELIVERY_TYPE_VIEW ? UserUrl($this->User) : UserUrl($this->User, '', 'picture')); } } if ($this->Form->ErrorCount() > 0) { $this->DeliveryType(DELIVERY_TYPE_ALL); } $this->Title(T('Change Picture')); $this->_SetBreadcrumbs(T('Change My Picture'), UserUrl($this->User, '', 'picture')); $this->Render(); }
/** * Set user's photo (avatar). * * @since 2.0.0 * @access public * @param mixed $UserReference Unique identifier, possible username or ID. * @param string $Username . */ public function picture($UserReference = '', $Username = '', $UserID = '') { if (!Gdn::session()->checkRankedPermission(c('Garden.Profile.EditPhotos', true))) { 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 $ImageManipOk = false; 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); $this->Form->setModel($this->UserModel); if ($this->Form->authenticatedPostBack() === true) { $this->Form->setFormValue('UserID', $this->User->UserID); // Set user's Photo attribute to a URL, provided the current user has proper permission to do so. $photoUrl = $this->Form->getFormValue('Url', false); if ($photoUrl && Gdn::session()->checkPermission('Garden.Settings.Manage')) { if (isUrl($photoUrl) && filter_var($photoUrl, FILTER_VALIDATE_URL)) { $UserPhoto = $photoUrl; } else { $this->Form->addError('Invalid photo URL'); } } else { $UploadImage = new Gdn_UploadImage(); try { // Validate the upload $TmpImage = $UploadImage->ValidateUpload('Picture'); // Generate the target image name. $TargetImage = $UploadImage->GenerateTargetName(PATH_UPLOADS, '', true); $Basename = pathinfo($TargetImage, PATHINFO_BASENAME); $Subdir = stringBeginsWith(dirname($TargetImage), PATH_UPLOADS . '/', false, true); // Delete any previously uploaded image. $UploadImage->delete(changeBasename($this->User->Photo, 'p%s')); // Save the uploaded image in profile size. $Props = $UploadImage->SaveImageAs($TmpImage, "userpics/{$Subdir}/p{$Basename}", c('Garden.Profile.MaxHeight', 1000), c('Garden.Profile.MaxWidth', 250), array('SaveGif' => c('Garden.Thumbnail.SaveGif'))); $UserPhoto = sprintf($Props['SaveFormat'], "userpics/{$Subdir}/{$Basename}"); // // Save the uploaded image in preview size // $UploadImage->SaveImageAs( // $TmpImage, // 'userpics/t'.$ImageBaseName, // Gdn::config('Garden.Preview.MaxHeight', 100), // Gdn::config('Garden.Preview.MaxWidth', 75) // ); // Save the uploaded image in thumbnail size $ThumbSize = Gdn::config('Garden.Thumbnail.Size', 40); $UploadImage->saveImageAs($TmpImage, "userpics/{$Subdir}/n{$Basename}", $ThumbSize, $ThumbSize, array('Crop' => true, 'SaveGif' => c('Garden.Thumbnail.SaveGif'))); } catch (Exception $Ex) { // Throw the exception on API calls. if ($this->deliveryType() === DELIVERY_TYPE_DATA) { throw $Ex; } $this->Form->addError($Ex); } } // If there were no errors, associate the image with the user if ($this->Form->errorCount() == 0) { if (!$this->UserModel->save(array('UserID' => $this->User->UserID, 'Photo' => $UserPhoto), array('CheckExisting' => true))) { $this->Form->setValidationResults($this->UserModel->validationResults()); } else { $this->User->Photo = $UserPhoto; setValue('Photo', $this->Data['Profile'], $UserPhoto); setValue('PhotoUrl', $this->Data['Profile'], Gdn_Upload::url(changeBasename($UserPhoto, 'n%s'))); } } // If there were no problems, redirect back to the user account if ($this->Form->errorCount() == 0 && $this->deliveryType() !== DELIVERY_TYPE_DATA) { $this->informMessage(sprite('Check', 'InformSprite') . t('Your changes have been saved.'), 'Dismissable AutoDismiss HasSprite'); redirect($this->deliveryType() == DELIVERY_TYPE_VIEW ? userUrl($this->User) : userUrl($this->User, '', 'picture')); } } if ($this->Form->errorCount() > 0 && $this->deliveryType() !== DELIVERY_TYPE_DATA) { $this->deliveryType(DELIVERY_TYPE_ALL); } $this->title(t('Change Picture')); $this->_setBreadcrumbs(t('Change My Picture'), userUrl($this->User, '', 'picture')); $this->render(); }
/** * 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 = GetValue('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 = GetValue('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, GetValue('Height', $Options, ''), GetValue('Width', $Options, ''), $Options); Trace($Parsed, 'Saved Image'); $Current = $this->GetFormValue($Field); if ($Current && GetValue('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); } }