コード例 #1
0
ファイル: class.validation.php プロジェクト: rnovino/Garden
 /**
  * Examines the posted fields, defines $this->_ValidationFields, and
  * enforces the $this->Rules collection on them.
  *
  * @param array $PostedFields An associative array of posted fields to be validated.
  * @param boolean $Insert A boolean value indicating if the posted fields are to be inserted or
  *  updated. If being inserted, the schema's required field rules will be
  *  enforced.
  * @return boolean Whether or not the validation was successful.
  */
 public function Validate($PostedFields, $Insert = FALSE)
 {
     $this->DefineValidationFields($PostedFields, $this->_Schema, $Insert);
     // Create an array to hold validation result messages
     if (!is_array($this->_ValidationResults)) {
         $this->_ValidationResults = array();
     }
     // Check for a honeypot (anti-spam input)
     $HoneypotName = Gdn::Config('Garden.Forms.HoneypotName', '');
     $HoneypotContents = GetPostValue($HoneypotName, '');
     if ($HoneypotContents != '') {
         $this->AddValidationResult($HoneypotName, "You've filled our honeypot! We use honeypots to help prevent spam. If you're  not a spammer or a bot, you should contact the application administrator for help.");
     }
     // Loop through the fields that should be validated
     foreach ($this->_ValidationFields as $FieldName => $FieldValue) {
         // If this field has rules to be enforced...
         if (array_key_exists($FieldName, $this->_FieldRules) && is_array($this->_FieldRules[$FieldName])) {
             // Enforce them...
             $this->_FieldRules[$FieldName] = array_values($this->_FieldRules[$FieldName]);
             $RuleCount = count($this->_FieldRules[$FieldName]);
             for ($i = 0; $i < $RuleCount; ++$i) {
                 $RuleName = $this->_FieldRules[$FieldName][$i];
                 if (array_key_exists($RuleName, $this->_Rules)) {
                     $Rule = $this->_Rules[$RuleName];
                     // echo '<div>FieldName: '.$FieldName.'; Rule: '.$Rule.'</div>';
                     if (substr($Rule, 0, 9) == 'function:') {
                         $Function = substr($Rule, 9);
                         if (!function_exists($Function)) {
                             trigger_error(ErrorMessage('Specified validation function could not be found.', 'Validation', 'Validate', $Function), E_USER_ERROR);
                         }
                         // Call the function. Core-defined validation functions can
                         // be found in ./functions.validation.php
                         $FieldInfo = array('Name' => $FieldName);
                         if (is_array($this->_Schema) && array_key_exists($FieldName, $this->_Schema)) {
                             $FieldInfo = array_merge($FieldInfo, (array) $this->_Schema[$FieldName]);
                         }
                         $FieldInfo = (object) $FieldInfo;
                         $ValidationResult = $Function($FieldValue, $FieldInfo, $PostedFields);
                         if ($ValidationResult !== TRUE) {
                             // If $ValidationResult is not FALSE, assume it is an error message
                             $ErrorCode = $ValidationResult === FALSE ? $Function : $ValidationResult;
                             // If there is a custom error, use it above all else
                             $ErrorCode = ArrayValue($FieldName . '.' . $RuleName, $this->_CustomErrors, $ErrorCode);
                             // Add the result
                             $this->AddValidationResult($FieldName, $ErrorCode);
                             // Only add one error per field
                             $i = $RuleCount;
                         }
                     } else {
                         if (substr($Rule, 0, 6) == 'regex:') {
                             $Regex = substr($Rule, 6);
                             if (ValidateRegex($FieldValue, $Regex) !== TRUE) {
                                 $ErrorCode = 'Regex';
                                 // If there is a custom error, use it above all else
                                 $ErrorCode = ArrayValue($FieldName . '.' . $RuleName, $this->_CustomErrors, $ErrorCode);
                                 // Add the result
                                 $this->AddValidationResult($FieldName, $ErrorCode);
                             }
                         }
                     }
                 }
             }
         }
     }
     return count($this->_ValidationResults) == 0 ? TRUE : FALSE;
 }
コード例 #2
0
ファイル: bootstrap.php プロジェクト: unlight/Candy
 function ValidateUrlPath($Value, $Field = '')
 {
     return ValidateRegex($Value, '/^([\\/\\d\\w\\-]+)?$/');
 }
コード例 #3
0
 function ValidateZipCode($Value, $Field = '')
 {
     if ($Value == '') {
         return true;
     }
     // Do not require by default.
     $Valid = ValidateRegex($Value, '/^([0-9]{5})(-[0-9]{4})?$/');
     return $Valid ? $Valid : T('ValidateZipCode', 'Zip code is invalid.');
 }
コード例 #4
0
 function ValidateUsername($Value, $Field = '')
 {
     return ValidateRegex($Value, '/^([\\d\\w_]{3,20})$/si');
 }
コード例 #5
0
 function ValidateUrlString($Value, $Field = '')
 {
     return ValidateRegex($Value, '/^([\\d\\w_\\-]+)?$/si');
 }
コード例 #6
0
 function ValidateZipCode($Value, $Field = '')
 {
     if (is_null($Value)) {
         return TRUE;
     }
     $Valid = ValidateRegex($Value, '/^([0-9]{5})(-[0-9]{4})?$/');
     return $Valid ? $Valid : T('ValidateZipCode', 'Zip code is invalid.');
 }
コード例 #7
0
 /**
  * Edit Tag form.
  */
 public function SettingsController_EditTag_Create($Sender)
 {
     $Sender->Permission('Garden.Settings.Manage');
     $Sender->Title(T('Edit Tag'));
     $Sender->AddSideMenu('settings/tagging');
     $TagID = GetValue(0, $Sender->RequestArgs);
     $TagModel = new Gdn_Model('Tag');
     $Sender->Tag = $TagModel->GetWhere(array('TagID' => $TagID))->FirstRow();
     // Set the model on the form.
     $Sender->Form->SetModel($TagModel);
     // Make sure the form knows which item we are editing.
     $Sender->Form->AddHidden('TagID', $TagID);
     if (!$Sender->Form->AuthenticatedPostBack()) {
         $Sender->Form->SetData($Sender->Tag);
     } else {
         // Make sure the tag is valid
         $Tag = $Sender->Form->GetFormValue('Name');
         if (!ValidateRegex($Tag, '/^([\\d\\w\\+-_.#]+)$/si')) {
             $Sender->Form->AddError('Tags can only contain the following characters: a-z 0-9 + # _ .');
         }
         // Make sure that the tag name is not already in use.
         if ($TagModel->GetWhere(array('TagID <>' => $TagID, 'Name' => $Tag))->NumRows() > 0) {
             $Sender->Form->AddError('The specified tag name is already in use.');
         }
         if ($Sender->Form->Save()) {
             $Sender->StatusMessage = T('Your changes have been saved successfully.');
         }
     }
     $Sender->Render('plugins/Tagging/views/edittag.php');
 }
コード例 #8
0
 /**
  * Validate the a string is valid for use as a username.
  *
  * @param mixed $value The value to validate.
  * @return bool Returns true if the value validates or false otherwise.
  */
 function validateUsername($value)
 {
     $ValidateUsernameRegex = ValidateUsernameRegex();
     return ValidateRegex($value, "/^({$ValidateUsernameRegex})?\$/siu");
 }
コード例 #9
0
 /**
  * Examines the posted fields, defines $this->_ValidationFields, and enforces the $this->Rules collection on them.
  *
  * @param array $PostedFields An associative array of posted fields to be validated.
  * @param boolean $Insert A boolean value indicating if the posted fields are to be inserted or
  *  updated. If being inserted, the schema's required field rules will be enforced.
  * @return boolean Whether or not the validation was successful.
  */
 public function validate($PostedFields, $Insert = false)
 {
     // Create an array to hold validation result messages
     if (!is_array($this->_ValidationResults) || $this->resetOnValidate()) {
         $this->_ValidationResults = array();
     }
     // Check for a honeypot (anti-spam input)
     $HoneypotName = C('Garden.Forms.HoneypotName', '');
     $HoneypotContents = getPostValue($HoneypotName, '');
     if ($HoneypotContents != '') {
         $this->addValidationResult($HoneypotName, "You've filled our honeypot! We use honeypots to help prevent spam. If you're not a spammer or a bot, you should contact the application administrator for help.");
     }
     $FieldRules = $this->defineValidationRules($PostedFields, $Insert);
     $Fields = $this->defineValidationFields($PostedFields, $Insert);
     // Loop through the fields that should be validated
     foreach ($Fields as $FieldName => $FieldValue) {
         // If this field has rules to be enforced...
         if (array_key_exists($FieldName, $FieldRules) && is_array($FieldRules[$FieldName])) {
             // Enforce them.
             $Rules = $FieldRules[$FieldName];
             // Get the field info for the field.
             $FieldInfo = array('Name' => $FieldName);
             if (is_array($this->_Schema) && array_key_exists($FieldName, $this->_Schema)) {
                 $FieldInfo = array_merge($FieldInfo, (array) $this->_Schema[$FieldName]);
             }
             $FieldInfo = (object) $FieldInfo;
             foreach ($Rules as $RuleName) {
                 if (array_key_exists($RuleName, $this->_Rules)) {
                     $Rule = $this->_Rules[$RuleName];
                     // echo '<div>FieldName: '.$FieldName.'; Rule: '.$Rule.'</div>';
                     if (substr($Rule, 0, 9) == 'function:') {
                         $Function = substr($Rule, 9);
                         if (!function_exists($Function)) {
                             trigger_error(errorMessage('Specified validation function could not be found.', 'Validation', 'Validate', $Function), E_USER_ERROR);
                         }
                         $ValidationResult = $Function($FieldValue, $FieldInfo, $PostedFields);
                         if ($ValidationResult !== true) {
                             // If $ValidationResult is not FALSE, assume it is an error message
                             $ErrorCode = $ValidationResult === false ? $Function : $ValidationResult;
                             // If there is a custom error, use it above all else
                             $ErrorCode = val($FieldName . '.' . $RuleName, $this->_CustomErrors, $ErrorCode);
                             // Add the result
                             $this->addValidationResult($FieldName, $ErrorCode);
                             // Only add one error per field
                         }
                     } elseif (substr($Rule, 0, 6) == 'regex:') {
                         $Regex = substr($Rule, 6);
                         if (ValidateRegex($FieldValue, $Regex) !== true) {
                             $ErrorCode = 'Regex';
                             // If there is a custom error, use it above all else
                             $ErrorCode = val($FieldName . '.' . $RuleName, $this->_CustomErrors, $ErrorCode);
                             // Add the result
                             $this->addValidationResult($FieldName, $ErrorCode);
                         }
                     }
                 }
             }
         }
     }
     $this->_ValidationFields = $Fields;
     return count($this->_ValidationResults) === 0;
 }