[pLog-svn] r4944 - plog/branches/lifetype-1.2/class/data/validator/rules
jondaley at devel.lifetype.net
jondaley at devel.lifetype.net
Sat Mar 3 08:10:45 EST 2007
Author: jondaley
Date: 2007-03-03 08:10:45 -0500 (Sat, 03 Mar 2007)
New Revision: 4944
Modified:
plog/branches/lifetype-1.2/class/data/validator/rules/urlformatrule.class.php
Log:
updated to version 0.24. Our local changes coming shortly
Modified: plog/branches/lifetype-1.2/class/data/validator/rules/urlformatrule.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/data/validator/rules/urlformatrule.class.php 2007-03-03 13:03:53 UTC (rev 4943)
+++ plog/branches/lifetype-1.2/class/data/validator/rules/urlformatrule.class.php 2007-03-03 13:10:45 UTC (rev 4944)
@@ -9,39 +9,42 @@
*
* Checks that a given URL is valid.
*
- * @copyright 2004Esben Maaløe esm-at-baseclassmodulweb.dk
- * @authorEsben Maaløe esm-at-baseclassmodulweb.dk
+ * @copyright 2004 Esben Maaløe esm-at-baseclassmodulweb.dk
+ * @author Esben Maaløe esm-at-baseclassmodulweb.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.21
+ * 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 );
+ $result = $this->_ValURL( $value, $this->_options );
return( $result['Result'] == EW_OK );
}
- function _ValURL($value, $options = Array())
- {
+ 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(/**/
'AllowedProtocols' => array(), /* array('http', 'https', etc...) always lcase! */
- 'AllowBracks' => true, /* Allow square brackets in the query string ? */
+ 'AllowBracks' => false, /* Allow square brackets in the query string ? */
'Protocols' => array('http', 'https', 'ftp', 'mailto', 'file', 'news', 'gopher', 'telnet', 'nntp'), /**/
'AssumeProtocol' => false, /**/
), $options);
@@ -51,8 +54,8 @@
'Protocol' => false, /**/
'User' => false, /**/
'Password' => false, /**/
- 'Server' => true, /**/
- 'TLD' => true, /**/
+ 'Server' => false, /**/
+ 'TLD' => false, /**/
'Port' => false, /**/
'Resource' => false, /**/
'QueryString' => false, /**/
@@ -64,7 +67,7 @@
'Protocol' => false, /**/
'User' => false, /**/
'Password' => false, /**/
- 'Server' => true, /**/
+ 'Server' => false, /**/
'TLD' => false, /**/
'Port' => false, /**/
'Resource' => false, /**/
@@ -129,7 +132,7 @@
if ($url['Protocol'] == 'mailto' || $url['Protocol'] == 'news')
{
- /* Check for % that are NOT an escape sequence */
+ /* 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;
@@ -248,23 +251,24 @@
/* Check the protocol */
if ($url['Protocol'])
{
- $url['Protocol'] = preg_replace("/[^a-z0-9+-.]/", '', $url['Protocol']);
+ $tmp = preg_replace("/[^a-z0-9+-.]/", '', $url['Protocol']);
+ if ($tmp != $url['Protocol'])
+ {
+ $errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL;
+ }
+
if (count($options['AllowedProtocols']))
if (!in_array($url['Protocol'], $options['AllowedProtocols']))
$errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL;
- if (preg_match('/^[a-z][a-z0-9+-.]*$/', $url['Protocol']) === false)
- {
- $errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL;
- }
}
/* check userinfo */
if ($url['User'])
{
- /* Check for % that are NOT an escape sequence */
- if (preg_match('/%[^a-f0-9]/i', $url['User']) || preg_match("/[^a-z0-9;&=+$,_.!~*'()%-]/i", $url ['User']))
+ /* Check for % that is NOT an escape sequence */
+ if (preg_match('/%[^a-f0-9]/i', $url['User']) || preg_match("/[^a-z0-9;&=+$,_.!~*'()%-]/i", $url['User']))
{
$errArr[EW_ERR_URL_INVALID_USER] = EW_ERR_URL_INVALID_USER;
$url['User'] = urlencode(urldecode($url['User']));
@@ -272,21 +276,21 @@
}
if ($url['Password'])
{
- /* Check for % that are NOT an escape sequence */
- if (preg_match('/%[^a-f0-9]/i', $url['Password']) || preg_match("/[^a-z0-9;&=+$,_.!~*'()%-]/i", $url ['Password']))
+ /* Check for % that is NOT an escape sequence */
+ if (preg_match('/%[^a-f0-9]/i', $url['Password']) || preg_match("/[^a-z0-9;&=+$,_.!~*'()%-]/i", $url['Password']))
{
$errArr[EW_ERR_URL_INVALID_PASSWORD] = EW_ERR_URL_INVALID_PASSWORD;
}
$url['Password'] = urlencode(urldecode($url['Password']));
}
- //userinfo= *( unreserved | escaped |
- //";" | ":" | "&" | "=" | "+" | "$" | "," )
- //unreserved= alphanum | mark
- //mark= "-" | "_" | "." | "!" | "~" | "*" | "'" |
- //"(" | ")"
+ // userinfo = *( unreserved | escaped |
+ // ";" | ":" | "&" | "=" | "+" | "$" | "," )
+ // unreserved = alphanum | mark
+ // mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
+ // "(" | ")"
- //escaped= "%" hex hex
+ // escaped = "%" hex hex
/* Check if the server part is an ip */
if ($url['Server'])
{
@@ -300,7 +304,6 @@
if ($ipPart[0] > 224 || $ipPart[0] == 0)
{
- die("1");
$errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
}
else
@@ -308,17 +311,15 @@
for ($i = 1; $i < 4; $i ++)
{
$ipPart[$i] = (integer) $ipPart[$i];
- if ($ipPart[$i] > 255) {
- die("22");
+ if ($ipPart[$i] > 255)
$errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
- }
}
}
/**
* @todo Implement checking for reserved class D and E, and
* other reserved addresses such as 0.0.0.0 or 255.255.255.255
- * and ip-addresses where either the host or the network part
+ * and ip-addresses where either the host or the network part
* is all binary 0s or all binary 1s
* check:
* http://www.cisco.com/univercd/cc/td/doc/product/atm/l2020/2020r21x/planning/appndxa.htm#xtocid87496
@@ -331,60 +332,47 @@
{
$ServerIsIP = false;
- /* Check for invalid chars */
- $tmp = preg_replace('/[^a-z0-9-.]/', '', $url['Server']);
+ $serverParts = explode('.', $url['Server']);
- if ($tmp != $url['Server'])
+ /* check serverparts */
+ for ($i = 0; $i < count($serverParts); $i ++)
{
- $url['Server'] = $tmp;
- $errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
- die("2");
- }
- else
- {
-
- $serverParts = explode('.', $url['Server']);
-
- if (count($serverParts) > 1)
- $url['TLD'] = array_pop($serverParts);
+ $tmp = preg_replace('/[^a-z0-9-]/', '', $serverParts[$i]);
+
+ /* Check if it is a top-level server */
+ if ($i && $i == count($serverParts) - 1)
+ $tmp = preg_replace('/^[^a-z]/', '', $tmp);
else
- $url['TLD'] = '';
+ $tmp = preg_replace('/^[^a-z0-9]/', '', $serverParts[$i]);
+
+ $tmp = preg_replace('/[^a-z0-9]$/', '', $tmp);
- /* check serverparts */
- for ($i = 0; $i < count($serverParts); $i ++)
+ if ($tmp != $serverParts[$i])
{
- /* check that the first and last chars are alphanum */
- if (strlen($serverParts[$i]) > 1)
- $rgx = '/^[a-z0-9].*[a-z0-9]$/';
+ if ($tmp != '')
+ $serverParts[$i] = $tmp;
else
- $rgx = '/[a-z0-9]/';
+ unset($serverParts[$i]);
- if (!preg_match($rgx, $serverParts[$i]))
- {
- $errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
- die("3");
- break;
- }
+ $errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
+
}
+ }
- /* check TLD */
- if ($url['TLD'])
+ if (count($serverParts) < 2)
+ {
+ if ($Require['TLD'])
{
- if (strlen($url['TLD']) > 1)
- $rgx = '/^[a-z].*[a-z0-9]$/';
- else
- $rgx = '/^[a-z0-9]$/';
-
- if (!preg_match($rgx, $url['TLD'])) {
- $errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
- die("4");
- }
- }
- elseif ($Require['TLD'])
- {
$errArr[EW_ERR_URL_MISSING_TLD] = EW_ERR_URL_MISSING_TLD;
}
}
+ else
+ {
+
+ $url['TLD'] = $serverParts[count($serverParts) - 1];
+ }
+
+ $url['Server'] = join('.', $serverParts);
}
}
@@ -408,12 +396,12 @@
}
/* Check the resource */
- //path= [ abs_path | opaque_part ]
+ //path = [ abs_path | opaque_part ]
//path_segments = segment *( "/" segment )
- //segment= *pchar *( ";" param )
- //param= *pchar
- //pchar= unreserved | escaped |
- //":" | "@" | "&" | "=" | "+" | "$" | ","
+ //segment = *pchar *( ";" param )
+ //param = *pchar
+ //pchar = unreserved | escaped |
+ // ":" | "@" | "&" | "=" | "+" | "$" | ","
if ($url['Resource'])
{
@@ -433,7 +421,7 @@
unset($resourceParts[$key]);
}
- /* Check for % that are NOT an escape sequence || invalid chars*/
+ /* Check for % that is NOT an escape sequence || invalid chars*/
elseif (preg_match('/%[^a-f0-9]/i', $part) || preg_match("/[^@a-z0-9_.!~*'()$+&,%:=;?-]/i", $part))
{
$errArr[EW_ERR_URL_INVALID_RESOURCE] = EW_ERR_URL_INVALID_RESOURCE;
@@ -448,8 +436,13 @@
if ($url['QueryString'])
{
- if (preg_match('/%[^a-f0-9][a-f0-9]?/i', $url['QueryString']) || //
- preg_match("/[^a-z0-9-_.!~*'()%;\/?:@&=+$,]/i", $url['QueryString']))
+
+ /* Check for % NOT part of an escape sequence || invalid chars */
+ $tmp = $options['AllowBracks'] ? /**/
+ "^a-z0-9_.!~*'()%;\/?:@&=+$,\[\]-" : /**/
+ "^a-z0-9_.!~*'()%;\/?:@&=+$,-"; /**/
+
+ if (preg_match('/%[^a-f0-9]/i', $url['QueryString']) || preg_match("/[$tmp]+/i", $url['QueryString']))
{
$errArr[EW_ERR_URL_INVALID_QUERYSTRING] = EW_ERR_URL_INVALID_QUERYSTRING;
$url['QueryString'] = $url['QueryString'];
@@ -461,7 +454,7 @@
if (preg_match('/%[^a-f0-9][a-f0-9]?/i', $url['Anchor']) || //
preg_match("/[^a-z0-9-_.!~*'()%;\/?:@&=+$,]/i", $url['Anchor']))
{
- $errArr[EW_ERR_URL_INVALID_Anchor] = EW_ERR_URL_INVALID_Anchor;
+ $errArr[EW_ERR_URL_INVALID_ANCHOR] = EW_ERR_URL_INVALID_ANCHOR;
$url['Anchor'] = $url['Anchor'];
}
@@ -524,5 +517,6 @@
return $r;
}
+
}
-?>
\ No newline at end of file
+?>
More information about the pLog-svn
mailing list