ktvalidators.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 566 行 · 第 1/2 页

PHP
566
字号

        return array(
            'errors' => $errors,
            'results' => $results,
        );
    }
}


class KTBooleanValidator extends KTValidator {
    var $sNamespace = 'ktcore.validators.boolean';

    function validate($data) {
        $results = array();
        $errors = array();

        // very simple if we're required and not present, fail
        // otherwise, its ok.
        $val = KTUtil::arrayGet($data, $this->sInputVariable);

        $out = ($val == true);

        if ($this->bProduceOutput) {
            $results[$this->sOutputVariable] = $out;
        }

        return array(
            'errors' => $errors,
            'results' => $results,
        );
    }
}


class KTPasswordValidator extends KTValidator {
    var $sNamespace = 'ktcore.validators.password';

    function validate($data) {
        $results = array();
        $errors = array();

        $bundle = KTUtil::arrayGet($data, '_password_confirm_' . $this->sBasename);
        if ($bundle['base'] != $bundle['confirm']) {
            $errors[$this->sBasename] = _kt('Your passwords do not match.');
        }

        if ($this->bProduceOutput) {
            $results[$this->sOutputVariable] = $val;
        }

        return array(
            'errors' => $errors,
            'results' => $results,
        );
    }
}

class KTMembershipValidator extends KTValidator {
    var $sNamespace = 'ktcore.validators.membership';

    var $bMulti;
    var $aVocab;

    function configure($aOptions) {
        $res = parent::configure($aOptions);
        if (PEAR::isError($res)) {
            return $res;
        }

        $this->bMulti = KTUtil::arrayGet($aOptions, 'multi', false);
        $vocab = (array) KTUtil::arrayGet($aOptions, 'vocab');
        $this->aVocab = array();
        foreach ($vocab as $v) {
            $this->aVocab[$v] = true;
        }
    }

    function validate($data) {
        $results = array();
        $errors = array();

        // very simple if we're required and not present, fail
        // otherwise, its ok.
        $val = KTUtil::arrayGet($data, $this->sInputVariable);
        if (empty($val)) {
            ; // pass
        } else {
            if ($this->bMulti) {
                $val = (array) $val;
                $failed = array();
                foreach ($val as $k) {
                    if (!$this->aVocab[$k]) {
                        $failed[] = $k;
                    }
                }
                if (!empty($failed)) {
                    $errors[$this->sBasename] = KTUtil::arrayGet($this->aOptions,
                        'error_message', sprintf(_kt('"%s" are not valid selections.'),
                            implode(', ', $failed)));
                }
            } else {

                $mandatory=true;

				if (substr($this->sInputVariable, 0, 9) == 'metadata_')
				{
					$fieldid = substr($this->sInputVariable, 9);
					$field = DocumentField::get($fieldid);
					$mandatory = $field->getIsMandatory();
				}

                if (!array_key_exists($val, $this->aVocab) && $mandatory) {
                    $errors[$this->sBasename] = KTUtil::arrayGet($this->aOptions,
                        'error_message', sprintf(_kt('"%s"is not a valid selection.'), $val));
                }
            }
        }

        if ($this->bProduceOutput) {
            $results[$this->sOutputVariable] = $val;
        }

        return array(
            'errors' => $errors,
            'results' => $results,
        );
    }
}


class KTFieldsetValidator extends KTValidator {
    var $sNamespace = 'ktcore.validators.fieldset';

    var $_validators;

    function configure($aOptions) {
        $res = parent::configure($aOptions);
        if (PEAR::isError($res)) {
            return $res;
        }

        $this->_validators = (array) KTUtil::arrayGet($aOptions, 'validators', array());
    }

    function validate($data) {
        $results = array();
        $errors = array();

        // very simple if we're required and not present, fail
        // otherwise, its ok.
        $d = (array) KTUtil::arrayGet($data, $this->sInputVariable);
        //var_dump($this); exit(0);
        foreach ($this->_validators as $v) {
            $res = $v->validate($d);

            // results comes out with a set of names and values.
            // these *shouldn't* overlap, so just merge them
            $extra_results = KTUtil::arrayGet($res, 'results', array());
            $results = kt_array_merge($results, $extra_results);

            // errors *can* overlap
            // the format is:
            //   basename => array(errors)
            // so that a given field can have multiple errors
            // from multiple validators
            //
            // there is also a "global" error notice stored against the var
            // _kt_global
            $extra_errors = KTUtil::arrayGet($res, 'errors', array());
            foreach ($extra_errors as $varname => $aErrors) {
                if (is_string($aErrors)) {
                    $errors[$varname][] = $aErrors;
                } else {
                    $errors[$varname] = kt_array_merge($errors[$varname], $aErrors);
                }
            }
        }
        $final_results = array();
        if ($this->bProduceOutput) {
            $final_results[$this->sOutputVariable] = $results;
        }

        $final_errors = array();
        if (!empty($errors)) {
            $final_errors[$this->sInputVariable] = $errors;
        }

        return array(
            'errors' => $final_errors,
            'results' => $final_results,
        );
    }
}


class KTFileValidator extends KTValidator {
    var $sNamespace = 'ktcore.validators.file';
    // we don't actual need to do *anything*

    function validate($data) {
        $d = (array) KTUtil::arrayGet($data, $this->sInputVariable);
        $results = array();
        if ($this->bProduceOutput) {
            $results[$this->sOutputVariable] = $d;
        }
        return array(
            'errors' => array(),
            'results' => $results,
        );
    }
}

class KTFileIllegalCharValidator extends KTValidator {
    var $sNamespace = 'ktcore.validators.fileillegalchar';
    var $sWarning;

    function configure($aOptions) {
        $res = parent::configure($aOptions);
        if (PEAR::isError($res)) {
            return $res;
        }

        $sChars =  "\/*<>|%+':\"?";
        $sWarning = sprintf(_kt('The name of the document selected is invalid. The following characters are not allowed: %s'), $sChars);
        $this->sWarning = KTUtil::arrayGet($aOptions, 'file_illegal_character_warning', $sWarning);

        $this->bTrim = KTUtil::arrayGet($aOptions, 'trim', true, false);
    }

    function validate($data) {
        $results = array();
        $errors = array();

        $aFile = (array) KTUtil::arrayGet($data, $this->sInputVariable);

        // Get the file name
        $val = $aFile['name'];
        if ($this->bTrim) {
            $val = trim($val);
        }

        // illegal characters: \/ *<>|%+':"?
        $pattern = "[\*|\%|\\\|\/|\<|\>|\+|\:|\?|\||\'|\"]";
        if(preg_match($pattern, $val)){
            $errors[$this->sBasename] = $this->sWarning;
        }

        if ($this->bProduceOutput) {
            $results[$this->sOutputVariable] = $aFile;
        }

        return array(
            'errors' => $errors,
            'results' => $results,
        );
    }
}


class KTArrayValidator extends KTValidator {
    var $sNamespace = 'ktcore.validators.array';

    function validate($data) {
        $results = array();
        $errors = array();

        // very simple if we're required and not present, fail
        // otherwise, its ok.
        $val = KTUtil::arrayGet($data, $this->sInputVariable);
        //var_dump($data); exit(0);
        if ($this->bProduceOutput) {
            $results[$this->sOutputVariable] = $val;
        }

        return array(
            'errors' => $errors,
            'results' => $results,
        );
    }
}

?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?