Add a validation result (error) to the validation.
public addValidationResult ( string $FieldName, string $ErrorCode = '' ) | ||
$FieldName | string | The name of the form field that has the error. |
$ErrorCode | string | The translation code of the error. Codes that begin with an '@' symbol are treated as literals and not translated. |
/** * Main import page. * * @since 2.0.0 * @access public */ public function index() { $this->permission('Garden.Import'); // This permission doesn't exist, so only users with Admin == '1' will succeed. $Timer = new Gdn_Timer(); // Determine the current step. $this->Form = new Gdn_Form(); $Imp = new ImportModel(); $Imp->loadState(); // Search for the list of acceptable imports. $ImportPaths = array(); $ExistingPaths = SafeGlob(PATH_UPLOADS . '/export*', array('gz', 'txt')); $ExistingPaths2 = SafeGlob(PATH_UPLOADS . '/porter/export*', array('gz')); $ExistingPaths = array_merge($ExistingPaths, $ExistingPaths2); foreach ($ExistingPaths as $Path) { $ImportPaths[$Path] = basename($Path); } // Add the database as a path. $ImportPaths = array_merge(array('db:' => t('This Database')), $ImportPaths); if ($Imp->CurrentStep < 1) { // Check to see if there is a file. $ImportPath = c('Garden.Import.ImportPath'); $Validation = new Gdn_Validation(); if (strcasecmp(Gdn::request()->requestMethod(), 'post') == 0) { $Upload = new Gdn_Upload(); $Validation = new Gdn_Validation(); if (count($ImportPaths) > 0) { $Validation->applyRule('PathSelect', 'Required', t('You must select a file to import.')); } if (count($ImportPaths) == 0 || $this->Form->getFormValue('PathSelect') == 'NEW') { $TmpFile = $Upload->ValidateUpload('ImportFile', false); } else { $TmpFile = ''; } if ($TmpFile) { $Filename = $_FILES['ImportFile']['name']; $Extension = pathinfo($Filename, PATHINFO_EXTENSION); $TargetFolder = PATH_ROOT . DS . 'uploads' . DS . 'import'; if (!file_exists($TargetFolder)) { mkdir($TargetFolder, 0777, true); } $ImportPath = $Upload->GenerateTargetName(PATH_ROOT . DS . 'uploads' . DS . 'import', $Extension); $Upload->SaveAs($TmpFile, $ImportPath); $Imp->ImportPath = $ImportPath; $this->Form->setFormValue('PathSelect', $ImportPath); $UploadedFiles = val('UploadedFiles', $Imp->Data); $UploadedFiles[$ImportPath] = basename($Filename); $Imp->Data['UploadedFiles'] = $UploadedFiles; } elseif ($PathSelect = $this->Form->getFormValue('PathSelect')) { if ($PathSelect == 'NEW') { $Validation->addValidationResult('ImportFile', 'ValidateRequired'); } else { $Imp->ImportPath = $PathSelect; } } elseif (!$Imp->ImportPath && count($ImportPaths) == 0) { // There was no file uploaded this request or before. $Validation->addValidationResult('ImportFile', $Upload->Exception); } // Validate the overwrite. if (true || strcasecmp($this->Form->getFormValue('Overwrite'), 'Overwrite') == 0) { if (!stringBeginsWith($this->Form->getFormValue('PathSelect'), 'Db:', true)) { $Validation->applyRule('Email', 'Required'); } } if ($Validation->validate($this->Form->formValues())) { $this->Form->setFormValue('Overwrite', 'overwrite'); $Imp->fromPost($this->Form->formValues()); $this->View = 'Info'; } else { $this->Form->setValidationResults($Validation->results()); } } else { $this->Form->setFormValue('PathSelect', $Imp->ImportPath); } $Imp->saveState(); } else { $this->setData('Steps', $Imp->steps()); $this->View = 'Info'; } if (!stringBeginsWith($Imp->ImportPath, 'db:') && !file_exists($Imp->ImportPath)) { $Imp->deleteState(); } try { $UploadedFiles = val('UploadedFiles', $Imp->Data, array()); $ImportPaths = array_merge($ImportPaths, $UploadedFiles); $this->setData('ImportPaths', $ImportPaths); $this->setData('Header', $Imp->getImportHeader()); $this->setData('Stats', val('Stats', $Imp->Data, array())); $this->setData('GenerateSQL', val('GenerateSQL', $Imp->Data)); $this->setData('ImportPath', $Imp->ImportPath); $this->setData('OriginalFilename', val('OriginalFilename', $Imp->Data)); $this->setData('CurrentStep', $Imp->CurrentStep); $this->setData('LoadSpeedWarning', $Imp->loadTableType(false) == 'LoadTableWithInsert'); } catch (Gdn_UserException $Ex) { $this->Form->addError($Ex); $Imp->saveState(); $this->View = 'Index'; } $this->render(); }
/** * Add ban data to all Get requests. * * @since 2.0.18 * @access public * * @param mixed User data (array or object). * @param Gdn_Validation $Validation * @param bool $UpdateBlocks * @return bool Whether user is banned. */ public static function checkUser($User, $Validation = null, $UpdateBlocks = false, &$BansFound = null) { $Bans = self::AllBans(); $Fields = array('Name' => 'Name', 'Email' => 'Email', 'IPAddress' => 'LastIPAddress'); $Banned = array(); if (!$BansFound) { $BansFound = array(); } foreach ($Bans as $Ban) { // Convert ban to regex. $Parts = explode('*', str_replace('%', '*', $Ban['BanValue'])); $Parts = array_map('preg_quote', $Parts); $Regex = '`^' . implode('.*', $Parts) . '$`i'; if (preg_match($Regex, val($Fields[$Ban['BanType']], $User))) { $Banned[$Ban['BanType']] = true; $BansFound[] = $Ban; if ($UpdateBlocks) { Gdn::sql()->update('Ban')->set('CountBlockedRegistrations', 'CountBlockedRegistrations + 1', false, false)->where('BanID', $Ban['BanID'])->put(); } } } // Add the validation results. if ($Validation) { foreach ($Banned as $BanType => $Value) { $Validation->addValidationResult(Gdn_Form::LabelCode($BanType), 'ValidateBanned'); } } return count($Banned) == 0; }
/** * Validate data to be used as class properties. * * @param array $Parameters . * @return string|true True on success or string (message) on error. */ public function validate($Parameters = array()) { $validation = new Gdn_Validation(); // Validate integer properties. $validation->applyRule('expiry', 'Integer'); $validation->applyRule('limit', 'Integer'); $validation->applyRule('bodylimit', 'Integer'); $validation->applyRule('titlelimit', 'Integer'); $validation->applyRule('group', 'Integer'); // Validate selection. $validation->applyRule('selection', 'String'); // Validate selector. $validation->applyRule('selector', 'Required'); $selectorWhitelist = array('role', 'rank', 'category', 'score', 'promoted'); if (isset($Parameters['selector']) && !in_array($Parameters['selector'], $selectorWhitelist)) { $validation->addValidationResult('selector', 'Invalid selector.'); } // Validate ContentType. $typeWhitelist = array('all', 'discussions', 'comments'); if (isset($Parameters['contenttype']) && !in_array($Parameters['contenttype'], $typeWhitelist)) { $validation->addValidationResult('contenttype', 'Invalid contenttype.'); } $result = $validation->validate($Parameters); return $result === true ? true : $validation->resultsText(); }
/** * * * @param $pluginName * @param Gdn_Validation $validation * @param bool $setup * @return bool * @throws Exception * @throws Gdn_UserException */ public function enablePlugin($pluginName, $validation, $setup = true) { // Check to see if the plugin is already enabled. if ($this->addonManager->isEnabled($pluginName, Addon::TYPE_ADDON)) { throw new Gdn_UserException(t('The plugin is already enabled.')); } $addon = $this->addonManager->lookupAddon($pluginName); if (!$addon) { throw notFoundException('Plugin'); } if (!$validation instanceof Gdn_Validation) { $validation = new Gdn_Validation(); } try { $this->addonManager->checkRequirements($addon, true); $addon->test(true); } catch (\Exception $ex) { $validation->addValidationResult('addon', '@' . $ex->getMessage()); return false; } // Enable this addon's requirements. $requirements = $this->addonManager->lookupRequirements($addon, AddonManager::REQ_DISABLED); foreach ($requirements as $addonKey => $row) { $requiredAddon = $this->addonManager->lookupAddon($addonKey); $this->enableAddon($requiredAddon, $setup); } // Enable the addon. $this->enableAddon($addon, $setup); // Refresh the locale just in case there are some translations needed this request. Gdn::locale()->refresh(); $this->EventArguments['AddonName'] = $addon->getRawKey(); $this->fireEvent('AddonEnabled'); return true; }