lib.validator.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # DotClear is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. class validator
  23. {
  24. function xhtml($input,$charset='UTF-8')
  25. {
  26. $res = array('valid' => NULL, 'errors' => '');
  27. $content = validator::__getContent($input);
  28. $http = new HttpClient('www.htmlhelp.com',80);
  29. $http->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207');
  30. $http->useGzip(false);
  31. $http->setPersistReferers(false);
  32. $params = array('area' => $content,'charset' => $charset);
  33. if ($http->post('/cgi-bin/validate.cgi',$params) !== false)
  34. {
  35. $result = $http->getContent();
  36. if (strpos($result,'<p class=congratulations><strong>Congratulations, no errors!</strong></p>'))
  37. {
  38. $res['valid'] = true;
  39. }
  40. else
  41. {
  42. $res['valid'] = false;
  43. if ($errors = preg_match('#<h2>Errors</h2>[\s]*(<ul>.*</ul>)#msU',$result,$matches))
  44. {
  45. $res['errors'] = strip_tags($matches[1],'<ul><li><pre><b>');
  46. }
  47. }
  48. return $res;
  49. }
  50. return false;
  51. }
  52. function __getContent($input)
  53. {
  54. return
  55. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '.
  56. '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n".
  57. '<html xmlns="http://www.w3.org/1999/xhtml">'."\n".
  58. '<head>'."\n".
  59. '<title>validation</title>'."\n".
  60. '</head>'."\n".
  61. '<body>'."\n".
  62. $input."\n".
  63. '</body>'."\n".
  64. '</html>';
  65. }
  66. }
  67. ?>