ktvalidators.php.svn-base

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

SVN-BASE
566
字号
<?php/** * $Id$ * * KnowledgeTree Community Edition * Document Management Made Simple * Copyright (C) 2008 KnowledgeTree Inc. * Portions copyright The Jam Warehouse Software (Pty) Limited *  * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more * details. *  * You should have received a copy of the GNU General Public License * along with this program.  If not, see <http://www.gnu.org/licenses/>. *  * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,  * California 94120-7775, or email info@knowledgetree.com. *  * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. *  * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * KnowledgeTree" logo and retain the original copyright notice. If the display of the  * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices * must display the words "Powered by KnowledgeTree" and retain the original  * copyright notice. * Contributor( s): ______________________________________ */require_once(KT_LIB_DIR . "/validation/basevalidator.inc.php");require_once(KT_LIB_DIR . "/util/ktutil.inc");class KTStringValidator extends KTValidator {    var $sNamespace = 'ktcore.validators.string';    var $iMinLength;    var $iMaxLength;    var $sMinLengthWarning;    var $sMaxLengthWarning;    function configure($aOptions) {        $res = parent::configure($aOptions);        if (PEAR::isError($res)) {            return $res;        }        $this->iMinLength = KTUtil::arrayGet($aOptions, 'min_length', 0);        $this->iMaxLength = KTUtil::arrayGet($aOptions, 'max_length', 254);        // sane default for char fields...        $this->sMinLengthWarning = KTUtil::arrayGet($aOptions, 'min_length_warning',            sprintf(_kt('You must provide a value which is at least %d characters long.'), $this->iMinLength));        $this->sMaxLengthWarning = KTUtil::arrayGet($aOptions, 'max_length_warning',            sprintf(_kt('You must provide a value which is at most %d characters long.'), $this->iMaxLength));        $this->bTrim = KTUtil::arrayGet($aOptions, 'trim', true, false);    }    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 ($this->bTrim) {            $val = trim($val);        }        $l = KTUtil::utf8_strlen($val);        if ($l < $this->iMinLength) {            $errors[$this->sBasename] = $this->sMinLengthWarning;        } else if ($l > $this->iMaxLength) {            $errors[$this->sBasename] = $this->sMaxLengthWarning;        }        if ($this->bProduceOutput) {            $results[$this->sOutputVariable] = $val;        }        return array(            'errors' => $errors,            'results' => $results,        );    }}class KTIllegalCharValidator extends KTValidator {    var $sNamespace = 'ktcore.validators.illegal_char';    var $sWarning;    function configure($aOptions) {        $res = parent::configure($aOptions);        if (PEAR::isError($res)) {            return $res;        }        $sChars =  "\/*<>|%+':\"?";        $sWarning = sprintf(_kt('The value you have entered is invalid. The following characters are not allowed: %s'), $sChars);        $this->sWarning = KTUtil::arrayGet($aOptions, 'illegal_character_warning', $sWarning);        $this->bTrim = KTUtil::arrayGet($aOptions, 'trim', true, false);    }    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 ($this->bTrim) {            $val = trim($val);        }        // illegal characters: \/ *<>|%+':"?        $pattern = "[\*|\%|\\\|\/|\<|\>|\+|\:|\?|\||\'|\"]";        // "'^[^:]+:(?:[0-9a-z\.\?&-_=\+\/]+[\.]{1})*(?:[0-9a-z\.\?&-_=\+\/]+\.)[a-z]{2,3}.*$'i"        if(preg_match($pattern, $val)){            $errors[$this->sBasename] = $this->sWarning;        }        if ($this->bProduceOutput) {            $results[$this->sOutputVariable] = $val;        }        return array(            'errors' => $errors,            'results' => $results,        );    }}class KTEntityValidator extends KTValidator {    var $sNamespace = 'ktcore.validators.entity';    var $sEntityClass;    var $sGetFunction;    function configure($aOptions) {        $res = parent::configure($aOptions);        if (PEAR::isError($res)) {            return $res;        }        $this->sEntityClass = KTUtil::arrayGet($aOptions, 'class');        if (empty($this->sEntityClass)) {            return PEAR::raiseError(_kt("No entity class specified."));        }        $this->sGetFunction = KTUtil::arrayGet($aOptions, 'id_method', 'get');        $this->bMultiple = KTUtil::arrayGet($aOptions, 'multi', false, false);    }    function validate($data) {        $results = array();        $errors = array();        $aFunc = array($this->sEntityClass, $this->sGetFunction);        $val = KTUtil::arrayGet($data, $this->sInputVariable);        $output = null;        if (!empty($val)) {            if ($this->bMultiple) {                // we probably have an array, but make sure                $val = (array) $val;                $failed = array();                foreach ($val as $id) {                    $oEntity =& call_user_func($aFunc, $id);                    if (PEAR::isError($oEntity)) {                        $failed[] = $id;                    } else {                        if ($this->aOptions['ids']) {                            $output[] = $id;                        } else {                            $output[] =& $oEntity;                        }                    }                }                if (!empty($failed)) {                    $errors[$this->sBasename] = sprintf(_kt("No such id's: %s"), implode(', ', $failed));                }            } else {                $oEntity =& call_user_func($aFunc, $val);                if (PEAR::isError($oEntity)) {                    $errors[$this->sBasename] = sprintf(_kt("No such id: %s"), $val);                }                if ($this->aOptions['ids']) {                    $output = $val;                } else {                    $output =& $oEntity;                }            }        }        if ($this->bProduceOutput) {            $results[$this->sOutputVariable] = $output;        }        return array(            'errors' => $errors,            'results' => $results,        );    }}// the required validator checks either single or multiple items// in the data array.class KTRequiredValidator extends KTValidator {    var $sNamespace = 'ktcore.validators.required';    function validate($data) {        $errors = array();        $val = KTUtil::arrayGet($data, $this->sInputVariable);        if (empty($val)) {            $errors[$this->sBasename] = _kt("You must provide a value for this field.");        }        return array(            'errors' => $errors,            'results' => array(),        );    }}// the required validator checks either single or multiple items// in the data array.class KTRequiredFileValidator extends KTValidator {    var $sNamespace = 'ktcore.validators.requiredfile';    function validate($data) {        $errors = array();        $val = KTUtil::arrayGet($_FILES, $this->sInputVariable);        if (empty($val) || empty($val['name'])) {            $errors[$this->sBasename] = _kt("You must select a file to upload.");        }        return array(            'errors' => $errors,            'results' => array(),        );    }}class KTEmailValidator extends KTValidator {    var $sNamespace = 'ktcore.validators.emailaddress';    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);        $sEmailAddress = trim($val);        if (!ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $sEmailAddress )) {            $errors[$this->sBasename] = KTUtil::arrayGet($this->aOptions,                'message',                _kt("This is not a valid email address."));        }        if ($this->bProduceOutput) {            $results[$this->sOutputVariable] = $sEmailAddress;        }

⌨️ 快捷键说明

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