PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/web_spider/src/lmbUriFilter.class.php

http://github.com/limb-php-framework/limb
PHP | 67 lines | 40 code | 14 blank | 13 comment | 5 complexity | 928c2e404d6eb250787b99fa2942209e MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. /**
  10. * class lmbUriFilter.
  11. *
  12. * @package web_spider
  13. * @version $Id: lmbUriFilter.class.php 7903 2009-04-26 18:36:36Z slevin $
  14. */
  15. class lmbUriFilter
  16. {
  17. protected $allowed_protocols = array();
  18. protected $allowed_hosts = array();
  19. protected $allowed_path_regexes = array();
  20. protected $disallowed_path_regexes = array();
  21. function allowProtocol($protocol)
  22. {
  23. $this->allowed_protocols[] = strtolower($protocol);
  24. }
  25. function allowHost($host)
  26. {
  27. $this->allowed_hosts[] = strtolower($host);
  28. }
  29. function allowPathRegex($regex)
  30. {
  31. $this->allowed_path_regexes[] = $regex;
  32. }
  33. function disallowPathRegex($regex)
  34. {
  35. $this->disallowed_path_regexes[] = $regex;
  36. }
  37. function canPass($uri)
  38. {
  39. if(!in_array($uri->getProtocol(), $this->allowed_protocols))
  40. return false;
  41. if(!in_array($uri->getHost(), $this->allowed_hosts))
  42. return false;
  43. if(!sizeof($this->allowed_path_regexes))
  44. return false;
  45. foreach($this->disallowed_path_regexes as $regex)
  46. if(preg_match($regex, $uri->getPath()))
  47. return false;
  48. foreach($this->allowed_path_regexes as $regex)
  49. if(!preg_match($regex, $uri->getPath()))
  50. return false;
  51. return true;
  52. }
  53. }