PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Web Files/ASP/includes/class.validator.php

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