Source for file check.class.php
Documentation is available at check.class.php
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License 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.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author m2mtech <tech@m2m.at>
* @copyright 2007 m2m server software gmbh
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
* @version $Id: check.class.php 133 2007-08-24 13:59:14Z m2mtech $
* @link http://www.ea-geier.at/
setlocale(LC_CTYPE, 'de_DE'); // enable umlauts
* store error message and throw error
* @param string $message error message
* @param string &$value test value
* @return boolean test result
if (empty($value) && ($value != '0')) return $this->_error('empty');
if (strcmp($value, $escValue) != 0) {
return $this->_error('badformat');
* allowed format [-]nnn[(.|,)[nnn]]
* @param string &$value test value
* @return boolean test result
if ($value == '') return $this->_error('empty');
if (!preg_match('/(-?(\d+([\,\.]\d*)?|([\,\.]\d+)))/', $value, $match)) {
return $this->_error('badformat');
if ($value != $match[0]) {
return $this->_error('badformat');
* according to RFC2822, ignoring IDN
* @param string &$value test value
* @return boolean test result
if (empty($value)) return $this->_error('empty');
if (!preg_match('/^((?:[^@]{1,64}|\".{1,62}\"))@([^@]{1,255})$/', $value, $match)) {
return $this->_error('badformat');
$atext = '[a-z0-9!#$%&\'*+\-\/=?\^_`{|}~]';
$dotatom = $atext . '+(\.' . $atext . '+)*';
$quotedstring = '\"[^\\\")]+\"';
list ($mail, $localpart, $domain) = $match;
if (!preg_match('/^(' . $dotatom . '|' . $quotedstring . ')$/i', $localpart)) {
return $this->_error('badformat');
if (!preg_match('/^[a-z0-9]+((\.|-)[a-z0-9]+)*$/i', $domain)) {
return $this->_error('badformat');
// now we are RFC2822 compliant
// why should somebody use tags in the quoted string of the local part?
if (strcmp($notags, $localpart) != 0) {
return $this->_error('badformat');
// why should someboedy want to add strings which can be used to spam?
$spam = array('content-type:', 'mime-version:', 'multipart/mixed', 'bcc:');
foreach ($spam as $pattern)
return $this->_error('badformat');
$value = strtolower($value); // not compliant, but more user friendly
* validate character string
* @param string &$value test value
* @return boolean test result
if ($value == '') return $this->_error('empty');
$extValue = preg_replace('/[^a-z]/i', '', $value); // remove invalid characters
if (strcmp($value, $extValue) != 0) {
return $this->_error('badformat');
* validate lowercase character string
* @param string &$value test value
* @return boolean test result
return $this->abc($value);
* validate uppercase character string
* @param string &$value test value
* @return boolean test result
return $this->abc($value);
* validate ip address string
* @param string &$value test value
* @param string $mask ip mask (255.255.255.255)
* @return boolean test result
function ip(&$value, $mask = '255.255.255.255') {
$pattern = '/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/';
return $this->_error('badformat');
foreach ($ipArray as $ipNibble) {
if ($ipNibble > 255) return $this->_error('badformat');
if ($mask == '255.255.255.255') return true;
return $this->_error('badformat');
foreach ($maskArray as $key => $maskNibble) {
if ($maskNibble == 255) continue;
if ($maskNibble > 255) return $this->_error('badformat');
$ipArray[$key] = (int) $ipArray[$key] & (int) $maskNibble;
$value = $ipArray[1] . '.' . $ipArray[2] . '.' . $ipArray[3] . '.' . $ipArray[4];
* allowed format: driver://user:password@server/database
* @param string test value
if (empty($value)) return $this->_error('empty');
if (strcmp($value, $escValue) != 0) {
return $this->_error('badformat');
$pattern = '/^[^:]+:\/\/[^:]+:[^@]+@[^\/]+\/.+$/';
|