egroupware_official/phpgwapi/inc/class.validator.inc.php

1357 lines
31 KiB
PHP
Raw Normal View History

2001-03-12 14:17:38 +01:00
<?php
2001-10-02 07:10:14 +02:00
/**************************************************************************\
* phpGroupWare API - Session management *
* This file written by Dan Kuykendall <seek3r@phpgroupware.org> *
* and Joseph Engo <jengo@phpgroupware.org> *
* Copyright (C) 2000, 2001 Dan Kuykendall *
* -------------------------------------------------------------------------*
* This library is part of the phpGroupWare API *
* http://www.phpgroupware.org/api *
* ------------------------------------------------------------------------ *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, *
* or any later version. *
* This library 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 Lesser General Public License for more details. *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
\**************************************************************************/
/* $Id$ */
/*
2001-03-12 14:17:38 +01:00
Validator 1.2 1999/03/05 CDI
A class for validating common data from forms
Copyright (c) 1999 CDI, cdi@thewebmasters.net All Rights Reserved
2001-10-02 07:10:14 +02:00
*/
2001-03-12 14:17:38 +01:00
class validator
{
2001-10-02 07:10:14 +02:00
var $ERROR = '';
var $CLEAR = false;
2001-03-12 14:17:38 +01:00
function validator ()
{
return;
}
function clear_error ()
{
2001-10-02 07:10:14 +02:00
$this->ERROR = '';
2001-03-12 14:17:38 +01:00
}
2001-10-02 07:10:14 +02:00
/* Checks a string for whitespace. True or false */
2001-03-12 14:17:38 +01:00
function has_space ($text)
{
if( ereg("[ ]",$text) )
{
return true;
}
2001-10-02 07:10:14 +02:00
return false;
2001-03-12 14:17:38 +01:00
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
function chconvert ($fragment)
{
2001-10-02 07:10:14 +02:00
switch ($fragment)
{
case 7:
$result = 'rwx';
break;
case 6:
$result = 'rw-';
break;
case 5:
$result = 'r-x';
break;
case 4:
$result = 'r--';
break;
case 3:
$result = '-wx';
break;
case 2:
$result = '-w-';
break;
case 1:
$result = '--x';
break;
case 0:
$result = '---';
break;
default:
$result = 'unk';
break;
}
2001-03-12 14:17:38 +01:00
return($result);
2001-10-02 07:10:14 +02:00
}
2001-03-12 14:17:38 +01:00
function get_perms ($fileName )
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$atrib = array();
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$perms = fileperms($fileName);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(!$perms)
{
$this->ERROR = "get_perms: Unable to obtain file perms on [$fileName]";
return false;
}
2001-10-02 07:10:14 +02:00
$octal = sprintf('%lo', ($perms & 07777) );
2001-03-12 14:17:38 +01:00
$one = substr($octal,0,1);
$two = substr($octal,1,1);
$three = substr($octal,2,1);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$user = $this->chconvert($one);
$group = $this->chconvert($two);
$other = $this->chconvert($three);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(is_dir($fileName))
{
$user = "d$user";
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$atrib = array(
2001-10-02 07:10:14 +02:00
'octal' => $octal,
'user' => $user,
'group' => $group,
'other' => $other
);
2001-03-12 14:17:38 +01:00
return $atrib;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
function is_sane ($filename)
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if (!file_exists($filename))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'File does not exist';
2001-03-12 14:17:38 +01:00
return false;
}
if (!is_readable($filename))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'File is not readable';
2001-03-12 14:17:38 +01:00
return false;
}
if(!is_writeable($filename))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'File is not writeable';
2001-03-12 14:17:38 +01:00
return false;
}
if(is_dir($filename))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'File is a directory';
2001-03-12 14:17:38 +01:00
return false;
}
if(is_link($filename))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'File is a symlink';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Strips whitespace (tab or space) from a string
2001-03-12 14:17:38 +01:00
function strip_space ($text)
{
2001-10-02 07:10:14 +02:00
$Return = ereg_replace("([ ]+)",'',$text);
2001-03-12 14:17:38 +01:00
return ($Return);
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Returns true if string contains only numbers
2001-03-12 14:17:38 +01:00
function is_allnumbers ($text)
{
2001-10-02 07:10:14 +02:00
if( (gettype($text)) == 'integer')
{
return true;
}
2001-03-12 14:17:38 +01:00
$Bad = $this->strip_numbers($text);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($Bad))
{
return true;
}
return false;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Strip numbers from a string
2001-03-12 14:17:38 +01:00
function strip_numbers ($text)
{
2001-10-02 07:10:14 +02:00
$Stripped = eregi_replace("([0-9]+)",'',$text);
2001-03-12 14:17:38 +01:00
return ($Stripped);
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Returns true if string contains only letters
2001-03-12 14:17:38 +01:00
function is_allletters ($text)
{
$Bad = $this->strip_letters($text);
if(empty($Bad))
{
return true;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Strips letters from a string
2001-03-12 14:17:38 +01:00
function strip_letters ($text)
{
2001-10-02 07:10:14 +02:00
$Stripped = eregi_replace("([A-Z]+)",'',$text);
2001-03-12 14:17:38 +01:00
return $Stripped;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Checks for HTML entities in submitted text.
// If found returns true, otherwise false. HTML specials are:
//
// " => &quot;
// < => &lt;
// > => &gt;
// & => &amp;
//
// The presence of ",<,>,& will force this method to return true.
//
function has_html ($text='')
2001-03-12 14:17:38 +01:00
{
if(empty($text))
{
return false;
}
$New = htmlspecialchars($text);
if($New == $text)
{
return false;
}
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// strip_html()
//
// Strips all html entities, attributes, elements and tags from
// the submitted string data and returns the results.
//
// Can't use a regex here because there's no way to know
// how the data is laid out. We have to examine every character
// that's been submitted. Consequently, this is not a very
// efficient method. It works, it's very good at removing
// all html from the data, but don't send gobs of data
// at it or your program will slow to a crawl.
// If you're stripping HTML from a file, use PHP's fgetss()
// and NOT this method, as fgetss() does the same thing
// about 100x faster.
function strip_html ($text='')
2001-03-12 14:17:38 +01:00
{
if( (!$text) or (empty($text)) )
{
2001-10-02 07:10:14 +02:00
return '';
2001-03-12 14:17:38 +01:00
}
$outside = true;
2001-10-02 07:10:14 +02:00
$rawText = '';
2001-03-12 14:17:38 +01:00
$length = strlen($text);
$count = 0;
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
for($count=0; $count < $length; $count++)
{
$digit = substr($text,$count,1);
if(!empty($digit))
{
2001-10-02 07:10:14 +02:00
if( ($outside) && ($digit != '<') && ($digit != '>') )
2001-03-12 14:17:38 +01:00
{
$rawText .= $digit;
}
2001-10-02 07:10:14 +02:00
if($digit == '<')
2001-03-12 14:17:38 +01:00
{
$outside = false;
}
2001-10-02 07:10:14 +02:00
if($digit == '>')
2001-03-12 14:17:38 +01:00
{
$outside = true;
}
}
}
return $rawText;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Returns true of the submitted text has meta characters in it
// . \\ + * ? [ ^ ] ( $ )
//
//
function has_metas ($text='')
2001-03-12 14:17:38 +01:00
{
if(empty($text))
{
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$New = quotemeta($text);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if($New == $text)
{
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Strips " . \\ + * ? [ ^ ] ( $ ) " from submitted string
//
// Metas are a virtual MINE FIELD for regular expressions,
// see custom_strip() for how they are removed
2001-03-12 14:17:38 +01:00
function strip_metas ($text = "")
{
if(empty($text))
{
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$Metas = array( '.','+','*','?','[','^',']','(','$',')' );
$text = stripslashes($text);
$New = $this->custom_strip($Metas,$text);
return $New;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// $Chars must be an array of characters to remove.
// This method is meta-character safe.
2001-03-12 14:17:38 +01:00
function custom_strip ($Chars, $text = "")
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($text))
{
return false;
}
2001-10-02 07:10:14 +02:00
if( (gettype($Chars)) != 'array')
2001-03-12 14:17:38 +01:00
{
$this->ERROR = "custom_strip: [$Chars] is not an array";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
while ( list ( $key,$val) = each ($Chars) )
{
if(!empty($val))
{
// str_replace is meta-safe, ereg_replace is not
$text = str_replace($val,"",$text);
}
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return $text;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Array_Echo will walk through an array,
// continuously printing out key value pairs.
//
// Multi dimensional arrays are handled recursively.
function array_echo ($MyArray, $Name='Array')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
if( (gettype($MyArray)) != 'array')
{
return;
}
2001-03-12 14:17:38 +01:00
$count = 0;
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
while ( list ($key,$val) = each ($MyArray) )
{
if($count == 0)
{
echo "\n\n<P><TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 COLS=8\n";
echo "><TR><TD VALIGN=TOP COLSPAN=4><B>$Name Contents:</B></TD\n";
echo "><TD COLSPAN=2><B>KEY</B></TD><TD COLSPAN=2><B>VAL</B></TD></TR\n>";
}
2001-10-02 07:10:14 +02:00
if( (gettype($val)) == 'array')
2001-03-12 14:17:38 +01:00
{
$NewName = "$key [$Name $count]";
$NewArray = $MyArray[$key];
echo "</TD></TR></TABLE\n\n>";
$this->array_echo($NewArray,$NewName);
echo "\n\n<P><TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 COLS=8\n";
echo "><TR><TD VALIGN=TOP COLSPAN=4><B>$Name Continued:</B></TD\n";
echo "><TD COLSPAN=2><B>KEY</B></TD><TD COLSPAN=2><B>VAL</B></TD></TR\n>";
}
else
{
echo "<TR>";
$Col1 = sprintf("[%s][%0d]",$Name,$count);
$Col2 = $key;
if(empty($val)) { $val = '&nbsp;'; }
$Col3 = $val;
echo "<TD COLSPAN=4>$Col1</TD>";
echo "<TD COLSPAN=2>$Col2</TD\n>";
echo "<TD COLSPAN=2>$Col3</TD></TR\n\n>";
}
$count++;
}
echo "<TR><TD COLSPAN=8><B>Array [$Name] complete.</B></TD></TR\n>";
echo "</TD></TR></TABLE\n\n>";
return;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Valid email format? true or false
// This checks the raw address, not RFC 822 addresses.
// Looks for [something]@[valid hostname with DNS record]
function is_email ($Address='')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($Address))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_email: No email address submitted';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
if(!ereg('@',$Address))
2001-03-12 14:17:38 +01:00
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_email: Invalid, no @ symbol in string';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
list($User,$Host) = split('@',$Address);
if ( (empty($User)) || (empty($Address)) )
2001-03-12 14:17:38 +01:00
{
$this->ERROR = "is_email: missing data [$User]@[$Host]";
return false;
}
2001-10-02 07:10:14 +02:00
if( ($this->has_space($User)) || ($this->has_space($Host)) )
2001-03-12 14:17:38 +01:00
{
$this->ERROR = "is_email: Whitespace in [$User]@[$Host]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// Can't look for an MX only record as that precludes
// CNAME only records. Thanks to everyone that slapped
// me upside the head for this glaring oversite. :)
2001-10-02 07:10:14 +02:00
if(!$this->is_host($Host,'ANY'))
{
return false;
}
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Valid URL format? true or false
// Checks format of a URL - does NOT handle query strings or
// urlencoded data.
function is_url ($Url='')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if (empty($Url))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_url: No URL submitted';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// Wow, the magic of parse_url!
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$UrlElements = parse_url($Url);
if( (empty($UrlElements)) or (!$UrlElements) )
{
$this->ERROR = "is_url: Parse error reading [$Url]";
return false;
}
2001-10-02 07:10:14 +02:00
$scheme = $UrlElements['scheme'];
$HostName = $UrlElements['host'];
2001-03-12 14:17:38 +01:00
if(empty($scheme))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_url: Missing protocol declaration';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($HostName))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_url: No hostname in URL';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if (!eregi("^(ht|f)tp",$scheme))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_url: No http:// or ftp://';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
## padraic renaghan change for bookmarker ver 1.4 bug 69
## if hostname is an ip address, check the validity of
## the ip address, otherwise check as if host name
## is specified.
if (ereg("[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+",$HostName)) {
if(!$this->is_ipaddress($HostName)) { return false; }
} else {
if(!$this->is_hostname($HostName)) { return false; }
}
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// URL responds to requests? true or false
// This will obviously fail if you're not connected to
// the internet, or if there are connection problems. (firewall etc)
function url_responds ($Url='')
2001-03-12 14:17:38 +01:00
{
global $php_errormsg;
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($Url))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'url_responds: No URL submitted';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
if(!$this->is_url($Url))
{
return false;
}
$fd = @fopen($Url,'r');
2001-03-12 14:17:38 +01:00
if(!$fd)
{
$this->ERROR = "url_responds: Failed : $php_errormsg";
return false;
}
else
{
@fclose($fd);
return true;
}
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Valid phone number? true or false
// Tries to validate a phone number
// Strips (,),-,+ from number prior to checking
// Less than 7 digits = fail
// More than 13 digits = fail
// Anything other than numbers after the stripping = fail
function is_phone ($Phone='')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($Phone))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_phone: No Phone number submitted';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$Num = $Phone;
$Num = $this->strip_space($Num);
2001-10-02 07:10:14 +02:00
$Num = eregi_replace("(\(|\)|\-|\+)",'',$Num);
2001-03-12 14:17:38 +01:00
if(!$this->is_allnumbers($Num))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_phone: bad data in phone number';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if ( (strlen($Num)) < 7)
{
$this->ERROR = "is_phone: number is too short [$Num][$Phone]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// 000 000 000 0000
2001-10-02 07:10:14 +02:00
// CC AC PRE SUFX = max 13 digits
2001-03-12 14:17:38 +01:00
if( (strlen($Num)) > 13)
{
$this->ERROR = "is_phone: number is too long [$Num][$Phone]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Valid, fully qualified hostname? true or false
// Checks the -syntax- of the hostname, not it's actual
// validity as a reachable internet host
function is_hostname ($hostname='')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$web = false;
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($hostname))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_hostname: No hostname submitted';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// Only a-z, 0-9, and "-" or "." are permitted in a hostname
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// Patch for POSIX regex lib by Sascha Schumann sas@schell.de
$Bad = eregi_replace("[-A-Z0-9\.]","",$hostname);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(!empty($Bad))
{
$this->ERROR = "is_hostname: invalid chars [$Bad]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// See if we're doing www.hostname.tld or hostname.tld
if(eregi("^www\.",$hostname))
{
$web = true;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// double "." is a not permitted
if(ereg("\.\.",$hostname))
{
$this->ERROR = "is_hostname: Double dot in [$hostname]";
return false;
}
if(ereg("^\.",$hostname))
{
$this->ERROR = "is_hostname: leading dot in [$hostname]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$chunks = explode(".",$hostname);
2001-10-02 07:10:14 +02:00
if( (gettype($chunks)) != 'array')
2001-03-12 14:17:38 +01:00
{
$this->ERROR = "is_hostname: Invalid hostname, no dot seperator [$hostname]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$count = ( (count($chunks)) - 1);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if($count < 1)
{
$this->ERROR = "is_hostname: Invalid hostname [$count] [$hostname]\n";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// Bug that can't be killed without doing an is_host,
// something.something will return TRUE, even if it's something
// stupid like NS.SOMETHING (with no tld), because SOMETHING is
// construed to BE the tld. The is_bigfour and is_country
// checks should help eliminate this inconsistancy. To really
// be sure you've got a valid hostname, do an is_host() on it.
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if( ($web) and ($count < 2) )
{
$this->ERROR = "is_hostname: Invalid hostname [$count] [$hostname]\n";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$tld = $chunks[$count];
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($tld))
{
$this->ERROR = "is_hostname: No TLD found in [$hostname]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(!$this->is_bigfour($tld))
{
if(!$this->is_country($tld))
{
$this->ERROR = "is_hostname: Unrecognized TLD [$tld]";
return false;
}
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
function is_bigfour ($tld)
{
if(empty($tld))
{
return false;
}
if(eregi("^\.",$tld))
{
$tld = eregi_replace("^\.","",$tld);
}
2001-10-02 07:10:14 +02:00
$BigFour = array(
'com' => 'com',
'edu' => 'edu',
'net' => 'net',
'org' => 'org',
'gov' => 'gov',
'mil' => 'mil',
'int' => 'int'
);
2001-03-12 14:17:38 +01:00
$tld = strtolower($tld);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(isset($BigFour[$tld]))
{
return true;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Hostname is a reachable internet host? true or false
function is_host ($hostname='', $type='ANY')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($hostname))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_host: No hostname submitted';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
if(!$this->is_hostname($hostname))
{
return false;
}
2001-03-12 14:17:38 +01:00
if(!checkdnsrr($hostname,$type))
{
$this->ERROR = "is_host: no DNS records for [$hostname].";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Dotted quad IPAddress within valid range? true or false
// Checks format, leading zeros, and values > 255
// Does not check for reserved or unroutable IPs.
function is_ipaddress ($IP='')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($IP))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_ipaddress: No IP address submitted';
2001-03-12 14:17:38 +01:00
return false;
}
// 123456789012345
// xxx.xxx.xxx.xxx
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$len = strlen($IP);
if( $len > 15 )
{
$this->ERROR = "is_ipaddress: too long [$IP][$len]";
return false;
}
2001-10-02 07:10:14 +02:00
$Bad = eregi_replace("([0-9\.]+)",'',$IP);
2001-03-12 14:17:38 +01:00
if(!empty($Bad))
{
$this->ERROR = "is_ipaddress: Bad data in IP address [$Bad]";
return false;
}
2001-10-02 07:10:14 +02:00
$chunks = explode('.',$IP);
2001-03-12 14:17:38 +01:00
$count = count($chunks);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if ($count != 4)
{
$this->ERROR = "is_ipaddress: not a dotted quad [$IP]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
while ( list ($key,$val) = each ($chunks) )
{
if(ereg("^0",$val))
{
$this->ERROR = "is_ipaddress: Invalid IP segment [$val]";
return false;
}
$Num = $val;
2001-10-02 07:10:14 +02:00
settype($Num,'integer');
2001-03-12 14:17:38 +01:00
if($Num > 255)
{
$this->ERROR = "is_ipaddress: Segment out of range [$Num]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
} // end is_ipaddress
2001-10-02 07:10:14 +02:00
// ************************************************************
// IP address is valid, and resolves to a hostname? true or false
function ip_resolves ($IP='')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($IP))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'ip_resolves: No IP address submitted';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(!$this->is_ipaddress($IP))
{
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$Hostname = gethostbyaddr($IP);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if($Hostname == $IP)
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'ip_resolves: IP does not resolve.';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if($Hostname)
{
if(!checkdnsrr($Hostname))
{
$this->ERROR = "is_ipaddress: no DNS records for resolved hostname [$Hostname]";
return false;
}
if( (gethostbyname($Hostname)) != $IP )
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_ipaddress: forward:reverse mismatch, possible forgery';
2001-03-12 14:17:38 +01:00
// Non-fatal, but it should be noted.
}
}
else
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'ip_resolves: IP address does not resolve';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
function browser_gen ()
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
$generation = 'UNKNOWN';
$client = getenv('HTTP_USER_AGENT');
2001-03-12 14:17:38 +01:00
if(empty($client))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'browser_gen: No User Agent for Client';
2001-03-12 14:17:38 +01:00
return $generation;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$client = $this->strip_metas($client);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$agents = array(
2001-10-02 07:10:14 +02:00
'Anonymizer' => 'ANONYMIZER',
'Ahoy' => 'SPIDER',
'Altavista' => 'SPIDER',
'Anzwers' => 'SPIDER',
'Arachnoidea' => 'SPIDER',
'Arachnophilia' => 'SPIDER',
'ArchitextSpider' => 'SPIDER',
'Backrub' => 'SPIDER',
'CherryPicker' => 'SPAMMER',
'Crescent' => 'SPAMMER',
'Duppies' => 'SPIDER',
'EmailCollector' => 'SPAMMER',
'EmailSiphon' => 'SPAMMER',
'EmailWolf' => 'SPAMMER',
'Extractor' => 'SPAMMER',
'Fido' => 'SPIDER',
'Fish' => 'SPIDER',
'GAIS' => 'SPIDER',
'Googlebot' => 'SPIDER',
'Gulliver' => 'SPIDER',
'HipCrime' => 'SPAMMER',
'Hamahakki' => 'SPIDER',
'ia_archive' => 'SPIDER',
'IBrowse' => 'THIRD',
'Incy' => 'SPIDER',
'InfoSeek' => 'SPIDER',
'KIT-Fireball' => 'SPIDER',
'Konqueror' => 'THIRD',
'libwww' => 'SECOND',
'LocalEyes' => 'SECOND',
'Lycos' => 'SPIDER',
'Lynx' => 'SECOND',
'Microsoft.URL' => 'SPAMMER',
'MOMspider' => 'SPIDER',
'Mozilla/1' => 'FIRST',
'Mozilla/2' => 'SECOND',
'Mozilla/3' => 'THIRD',
'Mozilla/4' => 'FOURTH',
'Mozilla/5' => 'FIFTH',
'Namecrawler' => 'SPIDER',
'NICErsPRO' => 'SPAMMER',
'Scooter' => 'SPIDER',
'sexsearch' => 'SPIDER',
'Sidewinder' => 'SPIDER',
'Slurp' => 'SPIDER',
'SwissSearch' => 'SPIDER',
'Ultraseek' => 'SPIDER',
'WebBandit' => 'SPAMMER',
'WebCrawler' => 'SPIDER',
'WiseWire' => 'SPIDER',
'Mozilla/3.0 (compatible; Opera/3' => 'THIRD'
);
2001-03-12 14:17:38 +01:00
while ( list ($key,$val) = each ($agents) )
{
$key = $this->strip_metas($key);
if(eregi("^$key",$client))
{
unset($agents);
return $val;
}
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
unset($agents);
return $generation;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// United States valid state code? true or false
2001-03-12 14:17:38 +01:00
function is_state ($State = "")
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($State))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_state: No state submitted';
2001-03-12 14:17:38 +01:00
return false;
}
if( (strlen($State)) != 2)
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_state: Too many digits in state code';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$State = strtoupper($State);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// 50 states, Washington DC, Puerto Rico and the US Virgin Islands
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$SCodes = array (
2001-10-02 07:10:14 +02:00
'AK' => 1,
'AL' => 1,
'AR' => 1,
'AZ' => 1,
'CA' => 1,
'CO' => 1,
'CT' => 1,
'DC' => 1,
'DE' => 1,
'FL' => 1,
'GA' => 1,
'HI' => 1,
'IA' => 1,
'ID' => 1,
'IL' => 1,
'IN' => 1,
'KS' => 1,
'KY' => 1,
'LA' => 1,
'MA' => 1,
'MD' => 1,
'ME' => 1,
'MI' => 1,
'MN' => 1,
'MO' => 1,
'MS' => 1,
'MT' => 1,
'NC' => 1,
'ND' => 1,
'NE' => 1,
'NH' => 1,
'NJ' => 1,
'NM' => 1,
'NV' => 1,
'NY' => 1,
'OH' => 1,
'OK' => 1,
'OR' => 1,
'PA' => 1,
'PR' => 1,
'RI' => 1,
'SC' => 1,
'SD' => 1,
'TN' => 1,
'TX' => 1,
'UT' => 1,
'VA' => 1,
'VI' => 1,
'VT' => 1,
'WA' => 1,
'WI' => 1,
'WV' => 1,
'WY' => 1
);
2001-03-12 14:17:38 +01:00
if(!isset($SCodes[$State]))
{
$this->ERROR = "is_state: Unrecognized state code [$State]";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// Lets not have this big monster camping in memory eh?
unset($SCodes);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Valid postal zip code? true or false
2001-03-12 14:17:38 +01:00
function is_zip ($zipcode = "")
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(empty($zipcode))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_zip: No zipcode submitted';
2001-03-12 14:17:38 +01:00
return false;
}
2001-10-02 07:10:14 +02:00
$Bad = eregi_replace("([-0-9]+)",'',$zipcode);
2001-03-12 14:17:38 +01:00
if(!empty($Bad))
{
$this->ERROR = "is_zip: Bad data in zipcode [$Bad]";
return false;
}
2001-10-02 07:10:14 +02:00
$Num = eregi_replace("\-",'',$zipcode);
2001-03-12 14:17:38 +01:00
$len = strlen($Num);
if ( ($len > 10) or ($len < 5) )
{
$this->ERROR = "is_zipcode: Invalid length [$len] for zipcode";
return false;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
return true;
}
2001-10-02 07:10:14 +02:00
// ************************************************************
// Valid postal country code?
// Returns the name of the country, or null on failure
// Current array recognizes ~232 country codes.
// I don't know if all of these are 100% accurate.
// You don't wanna know how difficult it was just getting
// this listing in here. :)
function is_country ($countrycode='')
2001-03-12 14:17:38 +01:00
{
if($this->CLEAR) { $this->clear_error(); }
2001-10-02 07:10:14 +02:00
$Return = '';
2001-03-12 14:17:38 +01:00
if(empty($countrycode))
{
2001-10-02 07:10:14 +02:00
$this->ERROR = 'is_country: No country code submitted';
2001-03-12 14:17:38 +01:00
return $Return;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
$countrycode = strtolower($countrycode);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if( (strlen($countrycode)) != 2 )
{
$this->ERROR = "is_country: 2 digit codes only [$countrycode]";
return $Return;
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// Now for a really big array
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// Dominican Republic, cc = "do" because it's a reserved
// word in PHP. That parse error took 10 minutes of
// head-scratching to figure out :)
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// A (roughly) 3.1 Kbyte array
2001-10-02 07:10:14 +02:00
$CCodes = array (
'do' => 'Dominican Republic',
'ad' => 'Andorra',
'ae' => 'United Arab Emirates',
'af' => 'Afghanistan',
'ag' => 'Antigua and Barbuda',
'ai' => 'Anguilla',
'al' => 'Albania',
'am' => 'Armenia',
'an' => 'Netherlands Antilles',
'ao' => 'Angola',
'aq' => 'Antarctica',
'ar' => 'Argentina',
'as' => 'American Samoa',
'at' => 'Austria',
'au' => 'Australia',
'aw' => 'Aruba',
'az' => 'Azerbaijan',
'ba' => 'Bosnia Hercegovina',
'bb' => 'Barbados',
'bd' => 'Bangladesh',
'be' => 'Belgium',
'bf' => 'Burkina Faso',
'bg' => 'Bulgaria',
'bh' => 'Bahrain',
'bi' => 'Burundi',
'bj' => 'Benin',
'bm' => 'Bermuda',
'bn' => 'Brunei Darussalam',
'bo' => 'Bolivia',
'br' => 'Brazil',
'bs' => 'Bahamas',
'bt' => 'Bhutan',
'bv' => 'Bouvet Island',
'bw' => 'Botswana',
'by' => 'Belarus (Byelorussia)',
'bz' => 'Belize',
'ca' => 'Canada',
'cc' => 'Cocos Islands',
'cd' => 'Congo, The Democratic Republic of the',
'cf' => 'Central African Republic',
'cg' => 'Congo',
'ch' => 'Switzerland',
'ci' => 'Ivory Coast',
'ck' => 'Cook Islands',
'cl' => 'Chile',
'cm' => 'Cameroon',
'cn' => 'China',
'co' => 'Colombia',
'cr' => 'Costa Rica',
'cs' => 'Czechoslovakia',
'cu' => 'Cuba',
'cv' => 'Cape Verde',
'cx' => 'Christmas Island',
'cy' => 'Cyprus',
'cz' => 'Czech Republic',
'de' => 'Germany',
'dj' => 'Djibouti',
'dk' => 'Denmark',
'dm' => 'Dominica',
'dz' => 'Algeria',
'ec' => 'Ecuador',
'ee' => 'Estonia',
'eg' => 'Egypt',
'eh' => 'Western Sahara',
'er' => 'Eritrea',
'es' => 'Spain',
'et' => 'Ethiopia',
'fi' => 'Finland',
'fj' => 'Fiji',
'fk' => 'Falkland Islands',
'fm' => 'Micronesia',
'fo' => 'Faroe Islands',
'fr' => 'France',
'fx' => 'France, Metropolitan FX',
'ga' => 'Gabon',
'gb' => 'United Kingdom (Great Britain)',
'gd' => 'Grenada',
'ge' => 'Georgia',
'gf' => 'French Guiana',
'gh' => 'Ghana',
'gi' => 'Gibraltar',
'gl' => 'Greenland',
'gm' => 'Gambia',
'gn' => 'Guinea',
'gp' => 'Guadeloupe',
'gq' => 'Equatorial Guinea',
'gr' => 'Greece',
'gs' => 'South Georgia and the South Sandwich Islands',
'gt' => 'Guatemala',
'gu' => 'Guam',
'gw' => 'Guinea-bissau',
'gy' => 'Guyana',
'hk' => 'Hong Kong',
'hm' => 'Heard and McDonald Islands',
'hn' => 'Honduras',
'hr' => 'Croatia',
'ht' => 'Haiti',
'hu' => 'Hungary',
'id' => 'Indonesia',
'ie' => 'Ireland',
'il' => 'Israel',
'in' => 'India',
'io' => 'British Indian Ocean Territory',
'iq' => 'Iraq',
'ir' => 'Iran',
'is' => 'Iceland',
'it' => 'Italy',
'jm' => 'Jamaica',
'jo' => 'Jordan',
'jp' => 'Japan',
'ke' => 'Kenya',
'kg' => 'Kyrgyzstan',
'kh' => 'Cambodia',
'ki' => 'Kiribati',
'km' => 'Comoros',
'kn' => 'Saint Kitts and Nevis',
'kp' => 'North Korea',
'kr' => 'South Korea',
'kw' => 'Kuwait',
'ky' => 'Cayman Islands',
'kz' => 'Kazakhstan',
'la' => 'Laos',
'lb' => 'Lebanon',
'lc' => 'Saint Lucia',
'li' => 'Lichtenstein',
'lk' => 'Sri Lanka',
'lr' => 'Liberia',
'ls' => 'Lesotho',
'lt' => 'Lithuania',
'lu' => 'Luxembourg',
'lv' => 'Latvia',
'ly' => 'Libya',
'ma' => 'Morocco',
'mc' => 'Monaco',
'md' => 'Moldova Republic',
'mg' => 'Madagascar',
'mh' => 'Marshall Islands',
'mk' => 'Macedonia, The Former Yugoslav Republic of',
'ml' => 'Mali',
'mm' => 'Myanmar',
'mn' => 'Mongolia',
'mo' => 'Macau',
'mp' => 'Northern Mariana Islands',
'mq' => 'Martinique',
'mr' => 'Mauritania',
'ms' => 'Montserrat',
'mt' => 'Malta',
'mu' => 'Mauritius',
'mv' => 'Maldives',
'mw' => 'Malawi',
'mx' => 'Mexico',
'my' => 'Malaysia',
'mz' => 'Mozambique',
'na' => 'Namibia',
'nc' => 'New Caledonia',
'ne' => 'Niger',
'nf' => 'Norfolk Island',
'ng' => 'Nigeria',
'ni' => 'Nicaragua',
'nl' => 'Netherlands',
'no' => 'Norway',
'np' => 'Nepal',
'nr' => 'Nauru',
'nt' => 'Neutral Zone',
'nu' => 'Niue',
'nz' => 'New Zealand',
'om' => 'Oman',
'pa' => 'Panama',
'pe' => 'Peru',
'pf' => 'French Polynesia',
'pg' => 'Papua New Guinea',
'ph' => 'Philippines',
'pk' => 'Pakistan',
'pl' => 'Poland',
'pm' => 'St. Pierre and Miquelon',
'pn' => 'Pitcairn',
'pr' => 'Puerto Rico',
'pt' => 'Portugal',
'pw' => 'Palau',
'py' => 'Paraguay',
'qa' => 'Qatar',
're' => 'Reunion',
'ro' => 'Romania',
'ru' => 'Russia',
'rw' => 'Rwanda',
'sa' => 'Saudi Arabia',
'sb' => 'Solomon Islands',
'sc' => 'Seychelles',
'sd' => 'Sudan',
'se' => 'Sweden',
'sg' => 'Singapore',
'sh' => 'St. Helena',
'si' => 'Slovenia',
'sj' => 'Svalbard and Jan Mayen Islands',
'sk' => 'Slovakia (Slovak Republic)',
'sl' => 'Sierra Leone',
'sm' => 'San Marino',
'sn' => 'Senegal',
'so' => 'Somalia',
'sr' => 'Suriname',
'st' => 'Sao Tome and Principe',
'sv' => 'El Salvador',
'sy' => 'Syria',
'sz' => 'Swaziland',
'tc' => 'Turks and Caicos Islands',
'td' => 'Chad',
'tf' => 'French Southern Territories',
'tg' => 'Togo',
'th' => 'Thailand',
'tj' => 'Tajikistan',
'tk' => 'Tokelau',
'tm' => 'Turkmenistan',
'tn' => 'Tunisia',
'to' => 'Tonga',
'tp' => 'East Timor',
'tr' => 'Turkey',
'tt' => 'Trinidad, Tobago',
'tv' => 'Tuvalu',
'tw' => 'Taiwan',
'tz' => 'Tanzania',
'ua' => 'Ukraine',
'ug' => 'Uganda',
'uk' => 'United Kingdom',
'um' => 'United States Minor Islands',
'us' => 'United States of America',
'uy' => 'Uruguay',
'uz' => 'Uzbekistan',
'va' => 'Vatican City',
'vc' => 'Saint Vincent, Grenadines',
've' => 'Venezuela',
'vg' => 'Virgin Islands (British)',
'vi' => 'Virgin Islands (USA)',
'vn' => 'Viet Nam',
'vu' => 'Vanuatu',
'wf' => 'Wallis and Futuna Islands',
'ws' => 'Samoa',
'ye' => 'Yemen',
'yt' => 'Mayotte',
'yu' => 'Yugoslavia',
'za' => 'South Africa',
'zm' => 'Zambia',
'zr' => 'Zaire',
'zw' => 'Zimbabwe'
2001-03-12 14:17:38 +01:00
);
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
if(isset($CCodes[$countrycode]))
{
$Return = $CCodes[$countrycode];
}
else
{
$this->ERROR = "is_country: Unrecognized country code [$countrycode]";
$Return = "";
}
2001-10-02 07:10:14 +02:00
2001-03-12 14:17:38 +01:00
// make sure this monster is removed from memory
2001-10-02 07:10:14 +02:00
unset($CCodes);
2001-03-12 14:17:38 +01:00
2001-10-02 07:10:14 +02:00
return ($Return);
} // end is_country
} // End class
?>