class.Validator.inc
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:29k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. # bookmarker RCS version info:
  3. # $Id: class.Validator.inc,v 1.2 2000/04/11 14:46:24 prenagha Exp $
  4. /*
  5. Validator 1.2  1999/03/05 CDI
  6. A class for validating common data from forms
  7. Copyright (c) 1999 CDI, cdi@thewebmasters.net All Rights Reserved
  8. */
  9. class Validator
  10. {
  11. var $ERROR = "";
  12. var $CLEAR = false;
  13. function Validator ()
  14. {
  15. return;
  16. }
  17. function clear_error ()
  18. {
  19. $this->ERROR = "";
  20. }
  21. // ************************************************************
  22. // Checks a string for whitespace. True or false
  23. function has_space ($text)
  24. {
  25. if( ereg("[  ]",$text) )
  26. {
  27. return true;
  28. }
  29. return false;
  30. }
  31. // ************************************************************
  32. function chconvert ($fragment)
  33. {
  34. if      ($fragment == 7) { $result = "rwx"; }
  35. elseif  ($fragment == 6) { $result = "rw-"; }
  36. elseif  ($fragment == 5) { $result = "r-x"; }
  37. elseif  ($fragment == 4) { $result = "r--"; }
  38. elseif  ($fragment == 3) { $result = "-wx"; }
  39. elseif  ($fragment == 2) { $result = "-w-"; }
  40. elseif  ($fragment == 1) { $result = "--x"; }
  41. elseif  ($fragment == 0) { $result = "---"; }
  42. else { $result = "unk"; }
  43. return($result);
  44.     }
  45. // ************************************************************
  46. function get_perms ($fileName )
  47. {
  48. if($this->CLEAR) { $this->clear_error(); }
  49. $atrib = array();
  50. $perms = fileperms($fileName);
  51. if(!$perms)
  52. {
  53. $this->ERROR = "get_perms: Unable to obtain file perms on [$fileName]";
  54. return false;
  55. }
  56. $octal = sprintf("%lo", ($perms & 07777) );
  57. $one = substr($octal,0,1);
  58. $two = substr($octal,1,1);
  59. $three = substr($octal,2,1);
  60. $user  = $this->chconvert($one);
  61. $group = $this->chconvert($two);
  62. $other = $this->chconvert($three);
  63. if(is_dir($fileName))
  64. {
  65. $user = "d$user";
  66. }
  67. $atrib = array(
  68. "octal" => $octal,
  69. "user" => $user,
  70. "group" => $group,
  71. "other" => $other
  72. );
  73. return $atrib;
  74. }
  75. // ************************************************************
  76. function is_sane ($filename)
  77. {
  78. if($this->CLEAR) { $this->clear_error(); }
  79. if (!file_exists($filename))
  80. {
  81. $this->ERROR = "File does not exist";
  82. return false;
  83. }
  84. if (!is_readable($filename))
  85. {
  86. $this->ERROR = "File is not readable";
  87. return false;
  88. }
  89. if(!is_writeable($filename))
  90. {
  91. $this->ERROR = "File is not writeable";
  92. return false;
  93. }
  94. if(is_dir($filename))
  95. {
  96. $this->ERROR = "File is a directory";
  97. return false;
  98. }
  99. if(is_link($filename))
  100. {
  101. $this->ERROR = "File is a symlink";
  102. return false;
  103. }
  104. return true;
  105. }
  106. // ************************************************************
  107. // Strips whitespace (tab or space) from a string
  108. function strip_space ($text)
  109. {
  110. $Return = ereg_replace("([  ]+)","",$text);
  111. return ($Return);
  112. }
  113. // ************************************************************
  114. // Returns true if string contains only numbers
  115. function is_allnumbers ($text)
  116. {
  117. if( (gettype($text)) == "integer") { return true; }
  118. $Bad = $this->strip_numbers($text);
  119. if(empty($Bad))
  120. {
  121. return true;
  122. }
  123. return false;
  124. }
  125. // ************************************************************
  126. // Strip numbers from a string
  127. function strip_numbers ($text)
  128. {
  129. $Stripped = eregi_replace("([0-9]+)","",$text);
  130. return ($Stripped);
  131. }
  132. // ************************************************************
  133. // Returns true if string contains only letters
  134. function is_allletters ($text)
  135. {
  136. $Bad = $this->strip_letters($text);
  137. if(empty($Bad))
  138. {
  139. return true;
  140. }
  141. return false;
  142. }
  143. // ************************************************************
  144. // Strips letters from a string
  145. function strip_letters ($text)
  146. {
  147. $Stripped = eregi_replace("([A-Z]+)","",$text);
  148. return $Stripped;
  149. }
  150. // ************************************************************
  151. // Checks for HTML entities in submitted text.
  152. // If found returns true, otherwise false. HTML specials are:
  153. //
  154. // " => &quot;
  155. // < => &lt;
  156. // > => &gt;
  157. // & => &amp;
  158. //
  159. // The presence of ",<,>,&  will force this method to return true.
  160. //
  161. function has_html ($text = "")
  162. {
  163. if(empty($text))
  164. {
  165. return false;
  166. }
  167. $New = htmlspecialchars($text);
  168. if($New == $text)
  169. {
  170. return false;
  171. }
  172. return true;
  173. }
  174. // ************************************************************
  175. // strip_html()
  176. //
  177. // Strips all html entities, attributes, elements and tags from
  178. // the submitted string data and returns the results.
  179. //
  180. // Can't use a regex here because there's no way to know
  181. // how the data is laid out. We have to examine every character
  182. // that's been submitted. Consequently, this is not a very
  183. // efficient method. It works, it's very good at removing
  184. // all html from the data, but don't send gobs of data
  185. // at it or your program will slow to a crawl.
  186. // If you're stripping HTML from a file, use PHP's fgetss()
  187. // and NOT this method, as fgetss() does the same thing
  188. // about 100x faster.
  189. function strip_html ($text = "")
  190. {
  191. if( (!$text) or (empty($text)) )
  192. {
  193. return "";
  194. }
  195. $outside = true;
  196. $rawText = "";
  197. $length = strlen($text);
  198. $count = 0;
  199. for($count=0; $count < $length; $count++)
  200. {
  201. $digit = substr($text,$count,1);
  202. if(!empty($digit))
  203. {
  204. if( ($outside) and ($digit != "<") and ($digit != ">") )
  205. {
  206. $rawText .= $digit;
  207. }
  208. if($digit == "<")
  209. {
  210. $outside = false;
  211. }
  212. if($digit == ">")
  213. {
  214. $outside = true;
  215. }
  216. }
  217. }
  218. return $rawText;
  219. }
  220. // ************************************************************
  221. // Returns true of the submitted text has meta characters in it
  222. // . \ + * ? [ ^ ] ( $ )
  223. //
  224. // 
  225. function has_metas ($text = "")
  226. {
  227. if(empty($text))
  228. {
  229. return false;
  230. }
  231. $New = quotemeta($text);
  232. if($New == $text)
  233. {
  234. return false;
  235. }
  236. return true;
  237. }
  238. // ************************************************************
  239. // Strips "  . \ + * ? [ ^ ] ( $ )  " from submitted string
  240. //
  241. // Metas are a virtual MINE FIELD for regular expressions,
  242. //  see custom_strip() for how they are removed
  243.  
  244. function strip_metas ($text = "")
  245. {
  246. if(empty($text))
  247. {
  248. return false;
  249. }
  250. $Metas = array( '.','+','*','?','[','^',']','(','$',')' );
  251. $text = stripslashes($text);
  252. $New = $this->custom_strip($Metas,$text);
  253. return $New;
  254. }
  255. // ************************************************************
  256. // $Chars must be an array of characters to remove.
  257. // This method is meta-character safe.
  258. function custom_strip ($Chars, $text = "")
  259. {
  260. if($this->CLEAR) { $this->clear_error(); }
  261. if(empty($text))
  262. {
  263. return false;
  264. }
  265. if( (gettype($Chars)) != "array")
  266. {
  267. $this->ERROR = "custom_strip: [$Chars] is not an array";
  268. return false;
  269. }
  270. while ( list ( $key,$val) = each ($Chars) )
  271. {
  272. if(!empty($val))
  273. {
  274. // str_replace is meta-safe, ereg_replace is not
  275. $text = str_replace($val,"",$text);
  276. }
  277. }
  278. return $text;
  279. }
  280. // ************************************************************
  281. // Array_Echo will walk through an array,
  282. // continuously printing out key value pairs.
  283. //
  284. // Multi dimensional arrays are handled recursively.
  285. function array_echo ($MyArray, $Name = "Array")
  286. {
  287. if($this->CLEAR) { $this->clear_error(); }
  288. if( (gettype($MyArray)) != "array") { return; }
  289. $count = 0;
  290. while ( list ($key,$val) = each ($MyArray) )
  291. {
  292. if($count == 0)
  293. {
  294. echo "nn<P><TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 COLS=8n";
  295. echo "><TR><TD VALIGN=TOP COLSPAN=4><B>$Name Contents:</B></TDn";
  296. echo "><TD COLSPAN=2><B>KEY</B></TD><TD COLSPAN=2><B>VAL</B></TD></TRn>";
  297. }
  298. if( (gettype($val)) == "array")
  299. {
  300. $NewName = "$key [$Name $count]";
  301. $NewArray = $MyArray[$key];
  302. echo "</TD></TR></TABLEnn>";
  303. $this->array_echo($NewArray,$NewName);
  304. echo "nn<P><TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 COLS=8n";
  305. echo "><TR><TD VALIGN=TOP COLSPAN=4><B>$Name Continued:</B></TDn";
  306. echo "><TD COLSPAN=2><B>KEY</B></TD><TD COLSPAN=2><B>VAL</B></TD></TRn>";
  307. }
  308. else
  309. {
  310. echo "<TR>";
  311. $Col1 = sprintf("[%s][%0d]",$Name,$count);
  312. $Col2 = $key;
  313. if(empty($val)) { $val = '&nbsp;'; }
  314. $Col3 = $val;
  315. echo "<TD COLSPAN=4>$Col1</TD>";
  316. echo "<TD COLSPAN=2>$Col2</TDn>";
  317. echo "<TD COLSPAN=2>$Col3</TD></TRnn>";
  318. }
  319. $count++;
  320. }
  321. echo "<TR><TD COLSPAN=8><B>Array [$Name] complete.</B></TD></TRn>";
  322. echo "</TD></TR></TABLEnn>";
  323. return;
  324. }
  325. // ************************************************************
  326. // Valid email format? true or false
  327. // This checks the raw address, not RFC 822 addresses.
  328. // Looks for [something]@[valid hostname with DNS record]
  329. function is_email ($Address = "")
  330. {
  331. if($this->CLEAR) { $this->clear_error(); }
  332. if(empty($Address))
  333. {
  334. $this->ERROR = "is_email: No email address submitted";
  335. return false;
  336. }
  337. if(!ereg("@",$Address))
  338. {
  339. $this->ERROR = "is_email: Invalid, no @ symbol in string";
  340. return false;
  341. }
  342. list($User,$Host) = split("@",$Address);
  343. if ( (empty($User)) or (empty($Address)) )
  344. {
  345. $this->ERROR = "is_email: missing data [$User]@[$Host]";
  346. return false;
  347. }
  348. if( ($this->has_space($User)) or ($this->has_space($Host)) )
  349. {
  350. $this->ERROR = "is_email: Whitespace in [$User]@[$Host]";
  351. return false;
  352. }
  353. // Can't look for an MX only record as that precludes
  354. // CNAME only records. Thanks to everyone that slapped
  355. // me upside the head for this glaring oversite. :)
  356. if(!$this->is_host($Host,"ANY")) { return false; }
  357. return true;
  358. }
  359. // ************************************************************
  360. // Valid URL format? true or false
  361. // Checks format of a URL - does NOT handle query strings or
  362. // urlencoded data.
  363. function is_url ($Url = "")
  364. {
  365. if($this->CLEAR) { $this->clear_error(); }
  366. if (empty($Url))
  367. {
  368. $this->ERROR = "is_url: No URL submitted";
  369. return false;
  370. }
  371. // Wow, the magic of parse_url!
  372. $UrlElements = parse_url($Url);
  373. if( (empty($UrlElements)) or (!$UrlElements) )
  374. {
  375. $this->ERROR = "is_url: Parse error reading [$Url]";
  376. return false;
  377. }
  378. $scheme = $UrlElements[scheme];
  379. $HostName = $UrlElements[host];
  380. if(empty($scheme))
  381. {
  382. $this->ERROR = "is_url: Missing protocol declaration";
  383. return false;
  384. }
  385. if(empty($HostName))
  386. {
  387. $this->ERROR = "is_url: No hostname in URL";
  388. return false;
  389. }
  390. if (!eregi("^(ht|f)tp",$scheme))
  391. {
  392. $this->ERROR = "is_url: No http:// or ftp://";
  393. return false;
  394. }
  395. ## padraic renaghan change for bookmarker ver 1.4 bug 69
  396. ## if hostname is an ip address, check the validity of
  397. ## the ip address, otherwise check as if host name
  398. ## is specified.
  399.     if (ereg("[0-9]+.[0-9]+.[0-9]+.[0-9]+",$HostName)) {
  400.      if(!$this->is_ipaddress($HostName)) { return false; }
  401.     } else {
  402.  if(!$this->is_hostname($HostName))   { return false; }
  403.   }
  404. return true;
  405. }
  406. // ************************************************************
  407. // URL responds to requests? true or false
  408. // This will obviously fail if you're not connected to
  409. // the internet, or if there are connection problems. (firewall etc)
  410. function url_responds ($Url = "")
  411. {
  412. global $php_errormsg;
  413. if($this->CLEAR) { $this->clear_error(); }
  414. if(empty($Url))
  415. {
  416. $this->ERROR = "url_responds: No URL submitted";
  417. return false;
  418. }
  419. if(!$this->is_url($Url)) { return false; }
  420. $fd = @fopen($Url,"r");
  421. if(!$fd)
  422. {
  423. $this->ERROR = "url_responds: Failed : $php_errormsg";
  424. return false;
  425. }
  426. else
  427. {
  428. @fclose($fd);
  429. return true;
  430. }
  431. }
  432. // ************************************************************
  433. // Valid phone number? true or false
  434. // Tries to validate a phone number
  435. // Strips (,),-,+ from number prior to checking
  436. // Less than 7 digits = fail
  437. // More than 13 digits = fail
  438. // Anything other than numbers after the stripping = fail
  439. function is_phone ($Phone = "")
  440. {
  441. if($this->CLEAR) { $this->clear_error(); }
  442. if(empty($Phone))
  443. {
  444. $this->ERROR = "is_phone: No Phone number submitted";
  445. return false;
  446. }
  447. $Num = $Phone;
  448. $Num = $this->strip_space($Num);
  449. $Num = eregi_replace("((|)|-|+)","",$Num);
  450. if(!$this->is_allnumbers($Num))
  451. {
  452. $this->ERROR = "is_phone: bad data in phone number";
  453. return false;
  454. }
  455. if ( (strlen($Num)) < 7)
  456. {
  457. $this->ERROR = "is_phone: number is too short [$Num][$Phone]";
  458. return false;
  459. }
  460. // 000 000 000 0000
  461.   // CC  AC  PRE SUFX = max 13 digits
  462. if( (strlen($Num)) > 13)
  463. {
  464. $this->ERROR = "is_phone: number is too long [$Num][$Phone]";
  465. return false;
  466. }
  467. return true;
  468. }
  469. // ************************************************************
  470. // Valid, fully qualified hostname? true or false
  471. // Checks the -syntax- of the hostname, not it's actual
  472. // validity as a reachable internet host
  473. function is_hostname ($hostname = "")
  474. {
  475. if($this->CLEAR) { $this->clear_error(); }
  476. $web = false;
  477. if(empty($hostname))
  478. {
  479. $this->ERROR = "is_hostname: No hostname submitted";
  480. return false;
  481. }
  482. // Only a-z, 0-9, and "-" or "." are permitted in a hostname
  483. // Patch for POSIX regex lib by Sascha Schumann sas@schell.de
  484. $Bad = eregi_replace("[-A-Z0-9.]","",$hostname);
  485. if(!empty($Bad))
  486. {
  487. $this->ERROR = "is_hostname: invalid chars [$Bad]";
  488. return false;
  489. }
  490. // See if we're doing www.hostname.tld or hostname.tld
  491. if(eregi("^www.",$hostname))
  492. {
  493. $web = true;
  494. }
  495. // double "." is a not permitted
  496. if(ereg("..",$hostname))
  497. {
  498. $this->ERROR = "is_hostname: Double dot in [$hostname]";
  499. return false;
  500. }
  501. if(ereg("^.",$hostname))
  502. {
  503. $this->ERROR = "is_hostname: leading dot in [$hostname]";
  504. return false;
  505. }
  506. $chunks = explode(".",$hostname);
  507. if( (gettype($chunks)) != "array")
  508. {
  509. $this->ERROR = "is_hostname: Invalid hostname, no dot seperator [$hostname]";
  510. return false;
  511. }
  512. $count = ( (count($chunks)) - 1);
  513. if($count < 1)
  514. {
  515. $this->ERROR = "is_hostname: Invalid hostname [$count] [$hostname]n";
  516. return false;
  517. }
  518. // Bug that can't be killed without doing an is_host,
  519. // something.something will return TRUE, even if it's something
  520. // stupid like NS.SOMETHING (with no tld), because SOMETHING is
  521. // construed to BE the tld.  The is_bigfour and is_country
  522. // checks should help eliminate this inconsistancy. To really
  523. // be sure you've got a valid hostname, do an is_host() on it.
  524. if( ($web) and ($count < 2) )
  525. {
  526. $this->ERROR = "is_hostname: Invalid hostname [$count] [$hostname]n";
  527. return false;
  528. }
  529. $tld = $chunks[$count];
  530. if(empty($tld))
  531. {
  532. $this->ERROR = "is_hostname: No TLD found in [$hostname]";
  533. return false;
  534. }
  535. if(!$this->is_bigfour($tld))
  536. {
  537. if(!$this->is_country($tld))
  538. {
  539. $this->ERROR = "is_hostname: Unrecognized TLD [$tld]";
  540. return false;
  541. }
  542. }
  543. return true;
  544. }
  545. // ************************************************************
  546. function is_bigfour ($tld)
  547. {
  548. if(empty($tld))
  549. {
  550. return false;
  551. }
  552. if(eregi("^.",$tld))
  553. {
  554. $tld = eregi_replace("^.","",$tld);
  555. }
  556. $BigFour = array (com=>com,edu=>edu,net=>net,org=>org,gov=>gov,mil=>mil,int=>int);
  557. $tld = strtolower($tld);
  558. if(isset($BigFour[$tld]))
  559. {
  560. return true;
  561. }
  562. return false;
  563. }
  564. // ************************************************************
  565. // Hostname is a reachable internet host? true or false
  566. function is_host ($hostname = "", $type = "ANY")
  567. {
  568. if($this->CLEAR) { $this->clear_error(); }
  569. if(empty($hostname))
  570. {
  571. $this->ERROR = "is_host: No hostname submitted";
  572. return false;
  573. }
  574. if(!$this->is_hostname($hostname)) { return false; }
  575. if(!checkdnsrr($hostname,$type))
  576. {
  577. $this->ERROR = "is_host: no DNS records for [$hostname].";
  578. return false;
  579. }
  580. return true;
  581. }
  582. // ************************************************************
  583. // Dotted quad IPAddress within valid range? true or false
  584. // Checks format, leading zeros, and values > 255
  585. // Does not check for reserved or unroutable IPs.
  586. function is_ipaddress ($IP = "")
  587. {
  588. if($this->CLEAR) { $this->clear_error(); }
  589. if(empty($IP))
  590. {
  591. $this->ERROR = "is_ipaddress: No IP address submitted";
  592. return false;
  593. }
  594. // 123456789012345
  595. // xxx.xxx.xxx.xxx
  596. $len = strlen($IP);
  597. if( $len > 15 )
  598. {
  599. $this->ERROR = "is_ipaddress: too long [$IP][$len]";
  600. return false;
  601. }
  602. $Bad = eregi_replace("([0-9.]+)","",$IP);
  603. if(!empty($Bad))
  604. {
  605. $this->ERROR = "is_ipaddress: Bad data in IP address [$Bad]";
  606. return false;
  607. }
  608. $chunks = explode(".",$IP);
  609. $count = count($chunks);
  610. if ($count != 4)
  611. {
  612. $this->ERROR = "is_ipaddress: not a dotted quad [$IP]";
  613. return false;
  614. }
  615. while ( list ($key,$val) = each ($chunks) )
  616. {
  617. if(ereg("^0",$val))
  618. {
  619. $this->ERROR = "is_ipaddress: Invalid IP segment [$val]";
  620. return false;
  621. }
  622. $Num = $val;
  623. settype($Num,"integer");
  624. if($Num > 255)
  625. {
  626. $this->ERROR = "is_ipaddress: Segment out of range [$Num]";
  627. return false;
  628. }
  629. }
  630. return true;
  631. } // end is_ipaddress
  632. // ************************************************************
  633. // IP address is valid, and resolves to a hostname? true or false
  634. function ip_resolves ($IP = "")
  635. {
  636. if($this->CLEAR) { $this->clear_error(); }
  637. if(empty($IP))
  638. {
  639. $this->ERROR = "ip_resolves: No IP address submitted";
  640. return false;
  641. }
  642. if(!$this->is_ipaddress($IP))
  643. {
  644. return false;
  645. }
  646. $Hostname = gethostbyaddr($IP);
  647. if($Hostname == $IP)
  648. {
  649. $this->ERROR = "ip_resolves: IP does not resolve.";
  650. return false;
  651. }
  652. if($Hostname)
  653. {
  654. if(!checkdnsrr($Hostname))
  655. {
  656. $this->ERROR = "is_ipaddress: no DNS records for resolved hostname [$Hostname]";
  657. return false;
  658. }
  659. if( (gethostbyname($Hostname)) != $IP )
  660. {
  661. $this->ERROR = "is_ipaddress: forward:reverse mismatch, possible forgery";
  662. // Non-fatal, but it should be noted.
  663. }
  664. }
  665. else
  666. {
  667. $this->ERROR = "ip_resolves: IP address does not resolve";
  668. return false;
  669. }
  670. return true;
  671. }
  672. // ************************************************************
  673. function browser_gen ()
  674. {
  675. if($this->CLEAR) { $this->clear_error(); }
  676. $generation = "UNKNOWN";
  677. $client = getenv("HTTP_USER_AGENT");
  678. if(empty($client))
  679. {
  680. $this->ERROR = "browser_gen: No User Agent for Client";
  681. return $generation;
  682. }
  683. $client = $this->strip_metas($client);
  684. $agents = array(
  685. 'Anonymizer' => "ANONYMIZER",
  686. 'Ahoy' => "SPIDER",
  687. 'Altavista' => "SPIDER",
  688. 'Anzwers' => "SPIDER",
  689. 'Arachnoidea' => "SPIDER",
  690. 'Arachnophilia' => "SPIDER",
  691. 'ArchitextSpider' => "SPIDER",
  692. 'Backrub' => "SPIDER",
  693. 'CherryPicker' => "SPAMMER",
  694. 'Crescent' => "SPAMMER",
  695. 'Duppies' => "SPIDER",
  696. 'EmailCollector' => "SPAMMER",
  697. 'EmailSiphon' => "SPAMMER",
  698. 'EmailWolf' => "SPAMMER",
  699. 'Extractor' => "SPAMMER",
  700. 'Fido' => "SPIDER",
  701. 'Fish' => "SPIDER",
  702. 'GAIS' => "SPIDER",
  703. 'Googlebot' => "SPIDER",
  704. 'Gulliver' => "SPIDER",
  705. 'HipCrime' => "SPAMMER",
  706. 'Hamahakki' => "SPIDER",
  707. 'ia_archive' => "SPIDER",
  708. 'IBrowse' => "THIRD",
  709. 'Incy' => "SPIDER",
  710. 'InfoSeek' => "SPIDER",
  711. 'KIT-Fireball' => "SPIDER",
  712. 'Konqueror' => "THIRD",
  713. 'libwww' => "SECOND",
  714. 'LocalEyes' => "SECOND",
  715. 'Lycos' => "SPIDER",
  716. 'Lynx' => "SECOND",
  717. 'Microsoft.URL' => "SPAMMER",
  718. 'MOMspider' => "SPIDER",
  719. 'Mozilla/1' => "FIRST",
  720. 'Mozilla/2' => "SECOND",
  721. 'Mozilla/3' => "THIRD",
  722. 'Mozilla/4' => "FOURTH",
  723. 'Mozilla/5' => "FIFTH",
  724. 'Namecrawler' => "SPIDER",
  725. 'NICErsPRO' => "SPAMMER",
  726. 'Scooter' => "SPIDER",
  727. 'sexsearch' => "SPIDER",
  728. 'Sidewinder' => "SPIDER",
  729. 'Slurp' => "SPIDER",
  730. 'SwissSearch' => "SPIDER",
  731. 'Ultraseek' => "SPIDER",
  732. 'WebBandit' => "SPAMMER",
  733. 'WebCrawler' => "SPIDER",
  734. 'WiseWire' => "SPIDER",
  735. 'Mozilla/3.0 (compatible; Opera/3' => "THIRD"
  736. );
  737. while ( list ($key,$val) = each ($agents) )
  738. {
  739. $key = $this->strip_metas($key);
  740. if(eregi("^$key",$client))
  741. {
  742. unset($agents);
  743. return $val;
  744. }
  745. }
  746. unset($agents);
  747. return $generation;
  748. }
  749. // ************************************************************
  750. // United States valid state code? true or false
  751. function is_state ($State = "")
  752. {
  753. if($this->CLEAR) { $this->clear_error(); }
  754. if(empty($State))
  755. {
  756. $this->ERROR = "is_state: No state submitted";
  757. return false;
  758. }
  759. if( (strlen($State)) != 2)
  760. {
  761. $this->ERROR = "is_state: Too many digits in state code";
  762. return false;
  763. }
  764. $State = strtoupper($State);
  765. // 50 states, Washington DC, Puerto Rico and the US Virgin Islands
  766. $SCodes = array (
  767. "AK" => 1,
  768. "AL" => 1,
  769. "AR" => 1,
  770. "AZ" => 1,
  771. "CA" => 1,
  772. "CO" => 1,
  773. "CT" => 1,
  774. "DC" => 1,
  775. "DE" => 1,
  776. "FL" => 1,
  777. "GA" => 1,
  778. "HI" => 1,
  779. "IA" => 1,
  780. "ID" => 1,
  781. "IL" => 1,
  782. "IN" => 1,
  783. "KS" => 1,
  784. "KY" => 1,
  785. "LA" => 1,
  786. "MA" => 1,
  787. "MD" => 1,
  788. "ME" => 1,
  789. "MI" => 1,
  790. "MN" => 1,
  791. "MO" => 1,
  792. "MS" => 1,
  793. "MT" => 1,
  794. "NC" => 1,
  795. "ND" => 1,
  796. "NE" => 1,
  797. "NH" => 1,
  798. "NJ" => 1,
  799. "NM" => 1,
  800. "NV" => 1,
  801. "NY" => 1,
  802. "OH" => 1,
  803. "OK" => 1,
  804. "OR" => 1,
  805. "PA" => 1,
  806. "PR" => 1,
  807. "RI" => 1,
  808. "SC" => 1,
  809. "SD" => 1,
  810. "TN" => 1,
  811. "TX" => 1,
  812. "UT" => 1,
  813. "VA" => 1,
  814. "VI" => 1,
  815. "VT" => 1,
  816. "WA" => 1,
  817. "WI" => 1,
  818. "WV" => 1,
  819. "WY" => 1
  820. );
  821. if(!isset($SCodes[$State]))
  822. {
  823. $this->ERROR = "is_state: Unrecognized state code [$State]";
  824. return false;
  825. }
  826. // Lets not have this big monster camping in memory eh?
  827. unset($SCodes);
  828. return true;
  829. }
  830. // ************************************************************
  831. // Valid postal zip code? true or false
  832. function is_zip ($zipcode = "")
  833. {
  834. if($this->CLEAR) { $this->clear_error(); }
  835. if(empty($zipcode))
  836. {
  837. $this->ERROR = "is_zip: No zipcode submitted";
  838. return false;
  839. }
  840. $Bad = eregi_replace("([-0-9]+)","",$zipcode);
  841. if(!empty($Bad))
  842. {
  843. $this->ERROR = "is_zip: Bad data in zipcode [$Bad]";
  844. return false;
  845. }
  846. $Num = eregi_replace("-","",$zipcode);
  847. $len = strlen($Num);
  848. if ( ($len > 10) or ($len < 5) )
  849. {
  850. $this->ERROR = "is_zipcode: Invalid length [$len] for zipcode";
  851. return false;
  852. }
  853. return true;
  854. }
  855. // ************************************************************
  856. // Valid postal country code?
  857. // Returns the name of the country, or null on failure
  858. // Current array recognizes ~232 country codes. 
  859. // I don't know if all of these are 100% accurate.
  860. // You don't wanna know how difficult it was just getting
  861. // this listing in here. :)
  862. function is_country ($countrycode = "")
  863. {
  864. if($this->CLEAR) { $this->clear_error(); }
  865. $Return = "";
  866. if(empty($countrycode))
  867. {
  868. $this->ERROR = "is_country: No country code submitted";
  869. return $Return;
  870. }
  871. $countrycode = strtolower($countrycode);
  872. if( (strlen($countrycode)) != 2 )
  873. {
  874. $this->ERROR = "is_country: 2 digit codes only [$countrycode]";
  875. return $Return;
  876. }
  877. // Now for a really big array
  878. // Dominican Republic, cc = "do" because it's a reserved
  879. // word in PHP. That parse error took 10 minutes of
  880. // head-scratching to figure out :)
  881. // A (roughly) 3.1 Kbyte array
  882. $CCodes = array (
  883. "do" => "Dominican Republic",
  884. ad => "Andorra",
  885. ae => "United Arab Emirates",
  886. af => "Afghanistan",
  887. ag => "Antigua and Barbuda",
  888. ai => "Anguilla",
  889. al => "Albania",
  890. am => "Armenia",
  891. an => "Netherlands Antilles",
  892. ao => "Angola",
  893. aq => "Antarctica",
  894. ar => "Argentina",
  895. "as" => "American Samoa",
  896. at => "Austria",
  897. au => "Australia",
  898. aw => "Aruba",
  899. az => "Azerbaijan",
  900. ba => "Bosnia Hercegovina",
  901. bb => "Barbados",
  902. bd => "Bangladesh",
  903. be => "Belgium",
  904. bf => "Burkina Faso",
  905. bg => "Bulgaria",
  906. bh => "Bahrain",
  907. bi => "Burundi",
  908. bj => "Benin",
  909. bm => "Bermuda",
  910. bn => "Brunei Darussalam",
  911. bo => "Bolivia",
  912. br => "Brazil",
  913. bs => "Bahamas",
  914. bt => "Bhutan",
  915. bv => "Bouvet Island",
  916. bw => "Botswana",
  917. by => "Belarus (Byelorussia)",
  918. bz => "Belize",
  919. ca => "Canada",
  920. cc => "Cocos Islands",
  921. cd => 'Congo, The Democratic Republic of the',
  922. cf => "Central African Republic",
  923. cg => "Congo",
  924. ch => "Switzerland",
  925. ci => "Ivory Coast",
  926. ck => "Cook Islands",
  927. cl => "Chile",
  928. cm => "Cameroon",
  929. cn => "China",
  930. co => "Colombia",
  931. cr => "Costa Rica",
  932. cs => "Czechoslovakia",
  933. cu => "Cuba",
  934. cv => "Cape Verde",
  935. cx => "Christmas Island",
  936. cy => "Cyprus",
  937. cz => 'Czech Republic',
  938. de => "Germany",
  939. dj => "Djibouti",
  940. dk => 'Denmark',
  941. dm => "Dominica",
  942. dz => "Algeria",
  943. ec => "Ecuador",
  944. ee => "Estonia",
  945. eg => "Egypt",
  946. eh => "Western Sahara",
  947. er => 'Eritrea',
  948. es => "Spain",
  949. et => "Ethiopia",
  950. fi => "Finland",
  951. fj => "Fiji",
  952. fk => "Falkland Islands",
  953. fm => "Micronesia",
  954. fo => "Faroe Islands",
  955. fr => "France",
  956. fx => 'France, Metropolitan FX',
  957. ga => "Gabon",
  958. gb => 'United Kingdom (Great Britain)',
  959. gd => "Grenada",
  960. ge => "Georgia",
  961. gf => "French Guiana",
  962. gh => "Ghana",
  963. gi => "Gibraltar",
  964. gl => "Greenland",
  965. gm => "Gambia",
  966. gn => "Guinea",
  967. gp => "Guadeloupe",
  968. gq => "Equatorial Guinea",
  969. gr => "Greece",
  970. gs => 'South Georgia and the South Sandwich Islands',
  971. gt => "Guatemala",
  972. gu => "Guam",
  973. gw => "Guinea-bissau",
  974. gy => "Guyana",
  975. hk => "Hong Kong",
  976. hm => "Heard and McDonald Islands",
  977. hn => "Honduras",
  978. hr => "Croatia",
  979. ht => "Haiti",
  980. hu => "Hungary",
  981. id => "Indonesia",
  982. ie => "Ireland",
  983. il => "Israel",
  984. in => "India",
  985. io => "British Indian Ocean Territory",
  986. iq => "Iraq",
  987. ir => "Iran",
  988. is => "Iceland",
  989. it => "Italy",
  990. jm => "Jamaica",
  991. jo => "Jordan",
  992. jp => "Japan",
  993. ke => "Kenya",
  994. kg => "Kyrgyzstan",
  995. kh => "Cambodia",
  996. ki => "Kiribati",
  997. km => "Comoros",
  998. kn => "Saint Kitts and Nevis",
  999. kp => "North Korea",
  1000. kr => "South Korea",
  1001. kw => "Kuwait",
  1002. ky => "Cayman Islands",
  1003. kz => "Kazakhstan",
  1004. la => "Laos",
  1005. lb => "Lebanon",
  1006. lc => "Saint Lucia",
  1007. li => "Lichtenstein",
  1008. lk => "Sri Lanka",
  1009. lr => "Liberia",
  1010. ls => "Lesotho",
  1011. lt => "Lithuania",
  1012. lu => "Luxembourg",
  1013. lv => "Latvia",
  1014. ly => "Libya",
  1015. ma => "Morocco",
  1016. mc => "Monaco",
  1017. md => "Moldova Republic",
  1018. mg => "Madagascar",
  1019. mh => "Marshall Islands",
  1020. mk => 'Macedonia, The Former Yugoslav Republic of',
  1021. ml => "Mali",
  1022. mm => "Myanmar",
  1023. mn => "Mongolia",
  1024. mo => "Macau",
  1025. mp => "Northern Mariana Islands",
  1026. mq => "Martinique",
  1027. mr => "Mauritania",
  1028. ms => "Montserrat",
  1029. mt => "Malta",
  1030. mu => "Mauritius",
  1031. mv => "Maldives",
  1032. mw => "Malawi",
  1033. mx => "Mexico",
  1034. my => "Malaysia",
  1035. mz => "Mozambique",
  1036. na => "Namibia",
  1037. nc => "New Caledonia",
  1038. ne => "Niger",
  1039. nf => "Norfolk Island",
  1040. ng => "Nigeria",
  1041. ni => "Nicaragua",
  1042. nl => "Netherlands",
  1043. no => "Norway",
  1044. np => "Nepal",
  1045. nr => "Nauru",
  1046. nt => "Neutral Zone",
  1047. nu => "Niue",
  1048. nz => "New Zealand",
  1049. om => "Oman",
  1050. pa => "Panama",
  1051. pe => "Peru",
  1052. pf => "French Polynesia",
  1053. pg => "Papua New Guinea",
  1054. ph => "Philippines",
  1055. pk => "Pakistan",
  1056. pl => "Poland",
  1057. pm => "St. Pierre and Miquelon",
  1058. pn => "Pitcairn",
  1059. pr => "Puerto Rico",
  1060. pt => "Portugal",
  1061. pw => "Palau",
  1062. py => "Paraguay",
  1063. qa => 'Qatar',
  1064. re => "Reunion",
  1065. ro => "Romania",
  1066. ru => "Russia",
  1067. rw => "Rwanda",
  1068. sa => "Saudi Arabia",
  1069. sb => "Solomon Islands",
  1070. sc => "Seychelles",
  1071. sd => "Sudan",
  1072. se => "Sweden",
  1073. sg => "Singapore",
  1074. sh => "St. Helena",
  1075. si => "Slovenia",
  1076. sj => "Svalbard and Jan Mayen Islands",
  1077. sk => 'Slovakia (Slovak Republic)',
  1078. sl => "Sierra Leone",
  1079. sm => "San Marino",
  1080. sn => "Senegal",
  1081. so => "Somalia",
  1082. sr => "Suriname",
  1083. st => "Sao Tome and Principe",
  1084. sv => "El Salvador",
  1085. sy => "Syria",
  1086. sz => "Swaziland",
  1087. tc => "Turks and Caicos Islands",
  1088. td => "Chad",
  1089. tf => "French Southern Territories",
  1090. tg => "Togo",
  1091. th => "Thailand",
  1092. tj => "Tajikistan",
  1093. tk => "Tokelau",
  1094. tm => "Turkmenistan",
  1095. tn => "Tunisia",
  1096. to => "Tonga",
  1097. tp => "East Timor",
  1098. tr => "Turkey",
  1099. tt => "Trinidad, Tobago",
  1100. tv => "Tuvalu",
  1101. tw => "Taiwan",
  1102. tz => "Tanzania",
  1103. ua => "Ukraine",
  1104. ug => "Uganda",
  1105. uk => "United Kingdom",
  1106. um => "United States Minor Islands",
  1107. us => "United States of America",
  1108. uy => "Uruguay",
  1109. uz => "Uzbekistan",
  1110. va => "Vatican City",
  1111. vc => "Saint Vincent, Grenadines",
  1112. ve => "Venezuela",
  1113. vg => "Virgin Islands (British)",
  1114. vi => "Virgin Islands (USA)",
  1115. vn => "Viet Nam",
  1116. vu => "Vanuatu",
  1117. wf => 'Wallis and Futuna Islands',
  1118. ws => "Samoa",
  1119. ye => 'Yemen',
  1120. yt => 'Mayotte',
  1121. yu => "Yugoslavia",
  1122. za => "South Africa",
  1123. zm => "Zambia",
  1124. zr => "Zaire",
  1125. zw => "Zimbabwe"
  1126. );
  1127. if(isset($CCodes[$countrycode]))
  1128. {
  1129. $Return = $CCodes[$countrycode];
  1130. }
  1131. else
  1132. {
  1133. $this->ERROR = "is_country: Unrecognized country code [$countrycode]";
  1134. $Return = "";
  1135. }
  1136. // make sure this monster is removed from memory
  1137. unset($CCodes);
  1138. return ($Return);
  1139. } // end is_country
  1140. } // End class
  1141. ?>