<?php
/* $Id: validation.php,v 1.3 2001/12/05 22:41:02 nkame Exp $
* ----------------------------------------------------------------------
* POST-NUKE Content Management System
* Copyright (C) 2001 by the Post-Nuke Development Team.
* http://www.postnuke.com/
* ----------------------------------------------------------------------
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License (GPL)
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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.
*
* To read the license please visit http://www.gnu.org/copyleft/gpl.html
* ----------------------------------------------------------------------
* Original Author of file: Damien Bonvillain <hide@address.com>
* <hide@address.com>
* Purpose of file:
* Misc validation routines
* ----------------------------------------------------------------------
* Remarks:
* the behaviour of the functions is consistent, they take the parameter
* to validate in entry, and either return -1 in case of an invalid
* entry, or the parameter if it is valid. The parameter MAY have been
* modified if it was first judged invalid and that it was found that a
* modification could be applied in order to make it valid without
* changing its meaning.
* ----------------------------------------------------------------------
* Usage:
* $valide = pn_validation_xxxx($value);
* if($valide == -1) {
* // handle invalid case
* } else {
* $value = $valide; // MUST replace the original by the validated
* }
* ----------------------------------------------------------------------
*/
/**
* Validate an ASCII string. Verify that all the characters are coded on
* 7bits or less.
* @param $string string the string
* @param $strip boolean if true, strip the ASCII-extended char and never
* return false
* @return -1 if invalid, or the validated string
*/
function pn_validation_ascii($string, $strip=0) {
$result = "";
$length = strlen($string);
$idx = 0;
while($length--) {
$c = $string[$idx++];
if(ord($c) > 127)
if($strip)
continue;
else
return -1;
$result .= $c;
}
return $result;
}
/**
* Validate an electronic mail address loosily against RFC822. This validate
* the syntax only.
* @param $address string the mail address
* @param $mandatory boolean true if an address must be present,
* otherwise, "" is a valid email. True by default.
* @return -1 if invalid, or a validated mail address
*/
function pn_validation_mail($address, $mandatory=1) {
$regexp = '/^(?:[^\s\000-\037\177\(\)<>@,;:\\"\[\]]\.?)+@(?:[^\s\000-\037\177\(\)<>@,;:\\\"\[\]]\.?)+\.[a-z]{2,6}$/Ui';
$address = pn_validation_ascii(trim($address), 1);
if(empty($address)) {
return ($mandatory)?-1:$address;
} else {
$result = preg_match($regexp,$address);
return ($result)?$address:-1;
}
}
/**
* Validate the syntax of an url. Based on RFC2396.
* @param $url string the url to validate
* @param $mandatory boolean true if an address must be present,
* otherwise, "" is a valid email. True by default.
* @return -1 if invalid, or a validated url
*/
function pn_validation_url($url, $mandatory=1) {
$result = '';
$regexp = '/^([!\$\046-\073=\077-\132_\141-\172~]|(?:%[a-f0-9]{2}))+$/i';
$url = pn_validation_ascii(trim($url), 1);
if (!empty($url)) {
if(!preg_match($regexp, $url))
return -1;
$url_array = @parse_url($url);
if(empty($url_array)) {
return -1;
} else {
if(empty($url_array[scheme])) {
// second attempt
$url = 'http://' . $url;
$url_array = @parse_url($url);
if(!empty($url_array) && !empty($url_array[scheme])) {
return $url;
} else {
return -1;
}
} else {
return $url;
}
}
} else {
return $mandatory?-1:$url;
}
}
?>