📄 urlformatrule.class.php
字号:
<?phplt_include(PLOG_CLASS_PATH."class/data/validator/rules/rule.class.php");lt_include(PLOG_CLASS_PATH."class/net/dns.class.php");lt_include(PLOG_CLASS_PATH."class/net/http/httpvars.class.php");define( "EW_OK", 1 );define( "EW_ERR_URL_INVALID_PROTOCOL", 10 );define( "EW_ERR_URL_INVALID_USER", 20 );define( "EW_ERR_URL_INVALID_PASSWORD", 30 );define( "EW_ERR_URL_INVALID_SERVER", 40 );define( "EW_ERR_URL_INVALID_TLD", 50 );define( "EW_ERR_URL_INVALID_PORT", 60 );define( "EW_ERR_URL_INVALID_RESOURCE", 70 );define( "EW_ERR_URL_INVALID_QUERYSTRING", 80 );define( "EW_ERR_URL_INVALID_ANCHOR", 100 );define( "EW_ERR_URL_MISSING_PROTOCOL", 110 );define( "EW_ERR_URL_MISSING_USER", 120 );define( "EW_ERR_URL_MISSING_PASSWORD", 130 );define( "EW_ERR_URL_MISSING_SERVER", 140 );define( "EW_ERR_URL_MISSING_TLD", 150 );define( "EW_ERR_URL_MISSING_PORT", 160 );define( "EW_ERR_URL_MISSING_RESOURCE", 170 );define( "EW_ERR_URL_MISSING_QUERYSTRING", 180 );define( "EW_ERR_URL_MISSING_ANCHOR", 190 ); /** * \ingroup Validator_Rules * * Checks that a given URL is valid. * * @copyright 2004 Esben Maal鴈 esm-at-baseclass.modulweb.dk * @author Esben Maal鴈 esm-at-baseclass.modulweb.dk * @license You are free to copy/modify this function to your hearts content * However I ask that you return any improvements you make to me, * and that you credit me in your sourcecode if you use it * @version 0.24 * http://baseclass.modulweb.dk/urlvalidator/ */class UrlFormatRule extends Rule{ var $_options; function UrlFormatRule( $options = Array()) { $this->Rule(); $this->_options = $options; } function validate( $value ) { $result = $this->_ValURL( $value, $this->_options ); return( $result['Result'] == EW_OK ); } function _ValURL($value, $options = array()) { $value = trim($value); if (!$value) return array('Result' => array(EW_ERR_URL_EMPTY_STRING), 'Value' => ''); /* Set up default options */ $options = array_merge(array(/**/ /* array('http', 'https', etc.) always lcase! */ 'AllowedProtocols' => array('http', 'https', 'ftp', 'mailto', 'file', 'news', 'gopher', 'telnet', 'nntp'), 'AllowBracks' => false, /* Allow square brackets in the query string ? */ 'Protocols' => array('http', 'https', 'ftp', 'mailto', 'file', 'news', 'gopher', 'telnet', 'nntp'), /**/ 'AssumeProtocol' => false, /**/ ), $options); /* Setup default values for $options['Require]*/ @ $options['Require'] = array_merge(array(/**/ 'Protocol' => true, /**/ 'User' => false, /**/ 'Password' => false, /**/ 'Server' => true, /**/ 'TLD' => false, /**/ 'Port' => false, /**/ 'Resource' => false, /**/ 'QueryString' => false, /**/ 'Anchor' => false, /**/ ), $options['Require']); /* Setup default values for $options['Forbid]*/ @ $options['Forbid'] = array_merge(array(/**/ 'Protocol' => false, /**/ 'User' => false, /**/ 'Password' => false, /**/ 'Server' => false, /**/ 'TLD' => false, /**/ 'Port' => false, /**/ 'Resource' => false, /**/ 'QueryString' => false, /**/ 'Anchor' => false, /**/ ), $options['Forbid']); /* Create a container for the URL parts*/ $url = array(/**/ 'Protocol' => '', /**/ 'User' => '', /**/ 'Password' => '', /**/ 'Server' => '', /**/ 'Port' => '', /**/ 'Resource' => '', /**/ 'TLD' => '', /**/ 'QueryString' => '', /**/ 'Anchor' => ''); /* Setup errorcodes for invalid elements */ $errCodeInvalid = array(/**/ 'Protocol' => EW_ERR_URL_INVALID_PROTOCOL, /**/ 'User' => EW_ERR_URL_INVALID_USER, /**/ 'Password' => EW_ERR_URL_INVALID_PASSWORD, /**/ 'Server' => EW_ERR_URL_INVALID_SERVER, /**/ 'TLD' => EW_ERR_URL_INVALID_TLD, /**/ 'Port' => EW_ERR_URL_INVALID_PORT, /**/ 'Resource' => EW_ERR_URL_INVALID_RESOURCE, /**/ 'QueryString' => EW_ERR_URL_INVALID_QUERYSTRING, /**/ 'Anchor' => EW_ERR_URL_INVALID_ANCHOR); /* Setup errorcodes for missing elements */ $errCodeMissing = array(/**/ 'Protocol' => EW_ERR_URL_MISSING_PROTOCOL, /**/ 'User' => EW_ERR_URL_MISSING_USER, /**/ 'Password' => EW_ERR_URL_MISSING_PASSWORD, /**/ 'Server' => EW_ERR_URL_MISSING_SERVER, /**/ 'TLD' => EW_ERR_URL_MISSING_TLD, /**/ 'Port' => EW_ERR_URL_MISSING_PORT, /**/ 'Resource' => EW_ERR_URL_MISSING_RESOURCE, /**/ 'QueryString' => EW_ERR_URL_MISSING_QUERYSTRING, /**/ 'Anchor' => EW_ERR_URL_MISSING_ANCHOR); /* set up some needed vars */ extract($options); $errArr = array(); $tmpValue = $value; $lcValue = strtolower($value); /** * Split the url into it's subparts */ foreach ($Protocols as $key => $protocol) { if (strpos($lcValue, "$protocol:") === 0) { $tmp = explode(':', $tmpValue, 2); $url['Protocol'] = $tmp[0]; $tmpValue = $tmp[1]; if ($url['Protocol'] == 'mailto' || $url['Protocol'] == 'news') { /* Check for % that is NOT an escape sequence */ if (preg_match('/%[^a-f0-9]/i', $tmpValue) || preg_match("/^[^a-z0-9;&=+$,_.!*'()%~-]/i", $tmpValue)) { $errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL; } } else { if (!(strpos($tmpValue, '//') === 0)) { $errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL; } else { $tmpValue = substr($tmpValue, 2); } } } } if (!$url['Protocol']) { if (strpos(strtolower($tmpValue), ('mailto:')) === 0 || strpos(strtolower($tmpValue), ('news:')) === 0) $tmp = ':'; else $tmp = '://'; $tmp = explode($tmp, $tmpValue, 2); if (count($tmp) == 2) { $url['Protocol'] = strtolower($tmp[0]); $tmpValue = $tmp[1]; } } $tmp = explode('?', $tmpValue); if (count($tmp) > 1) { $tmpValue = $tmp[0]; $url['QueryString'] = $tmp[1]; $tmp = explode('#', $url['QueryString']); if (count($tmp) > 1) { $url['QueryString'] = $tmp[0]; $url['Anchor'] = $tmp[1]; } } else { $tmp = explode('#', $tmpValue); if (count($tmp) > 1) { $tmpValue = $tmp[0]; $url['Anchor'] = $tmp[1]; } } $tmp = explode('/', $tmpValue, 2); if (count($tmp) > 1) { $url['Server'] = strtolower($tmp[0]); $url['Resource'] = $tmp[1]; } else { $url['Server'] = strtolower($tmpValue); } /* User / password */ $tmp = explode('@', $url['Server']); if (count($tmp) > 1) { $url['User'] = $tmp[0]; $url['Server'] = $tmp[1]; if ($url['User']) { $tmp = explode(':', $url['User']); if (count($tmp) > 1) { $url['User'] = $tmp[0]; $url['Password'] = $tmp[1]; } } } $tmp = explode(':', $url['Server'], 2); if (count($tmp) > 1) { if ($tmp[0]) { $url['Server'] = $tmp[0]; $url['Port'] = $tmp[1]; } } if (!$url['Protocol'] && !$url['Password'] && in_array(strtolower($url['User']), array('mail', 'news'))) { $url['Protocol'] = strtolower($url['User']); $url['User'] = ''; } if ($url['Protocol'] == 'mailto' && $url['Server'] && !$url['User']) { $url['User'] = $url['Server']; $url['Server'] = ''; } /** * Validate the different subparts
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -