home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Services / W3C / HTMLValidator.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  10.3 KB  |  361 lines

  1. <?php
  2. /**
  3.  * This file contains the base class for utilizing an instance of the
  4.  * W3 HTML Validator.
  5.  * 
  6.  * PHP versions 5
  7.  * 
  8.  * @category Services
  9.  * @package  Services_W3C_HTMLValidator
  10.  * @author   Brett Bieber <brett.bieber@gmail.com>
  11.  * @license  http://www.opensource.org/licenses/bsd-license.php BSD
  12.  * @version  CVS: $id$
  13.  * @link     http://pear.php.net/package/Services_W3C_HTMLValidator
  14.  * @since    File available since Release 0.2.0
  15.  */
  16.  
  17. /**
  18.  * require the HTTP_Request class for sending requests to the validator.
  19.  */
  20. require_once 'HTTP/Request.php';
  21.  
  22. /**
  23.  * Uses response object.
  24.  */
  25. require_once 'Services/W3C/HTMLValidator/Response.php';
  26.  
  27. require_once 'Services/W3C/HTMLValidator/Error.php';
  28.  
  29. require_once 'Services/W3C/HTMLValidator/Warning.php';
  30.  
  31. /**
  32.  * A simple class for utilizing the W3C HTML Validator service.
  33.  * 
  34.  * @category Services
  35.  * @package  Services_W3C_HTMLValidator
  36.  * @author   Brett Bieber <brett.bieber@gmail.com>
  37.  * @license  http://www.opensource.org/licenses/bsd-license.php BSD
  38.  * @link     http://pear.php.net/package/Services_W3C_HTMLValidator
  39.  */
  40. class Services_W3C_HTMLValidator
  41. {
  42.     /**
  43.      * URI to the w3 validator.
  44.      * 
  45.      * @var string
  46.      */
  47.     public $validator_uri = 'http://validator.w3.org/check';
  48.     
  49.     /**
  50.      * The URL of the document to validate
  51.      * @var string
  52.      */
  53.     public $uri;
  54.     
  55.     /**
  56.      * Internally used filename of a file to upload to the validator
  57.      * POSTed as multipart/form-data
  58.      * @var string
  59.      */
  60.     public $uploaded_file;
  61.     
  62.     /**
  63.      * HTML fragment to validate.
  64.      *  
  65.      * Full documents only. At the moment, will only work if data is sent with the
  66.      * UTF-8 encoding.
  67.      * @var string
  68.      */
  69.     public $fragment;
  70.     
  71.     /**
  72.      * Output format
  73.      * 
  74.      * Triggers the various outputs formats of the validator. If unset, the usual 
  75.      * Web format will be sent. If set to soap12, the SOAP1.2 interface will be 
  76.      * triggered. See below for the SOAP 1.2 response format description.
  77.      */
  78.     public $output = 'soap12';
  79.     
  80.     /**
  81.      * Character encoding
  82.      * 
  83.      * Character encoding override: Specify the character encoding to use when 
  84.      * parsing the document. When used with the auxiliary parameter fbc set to 1, 
  85.      * the given encoding will only be used as a fallback value, in case the charset
  86.      * is absent or unrecognized. Note that this parameter is ignored if validating
  87.      * a fragment with the direct input interface.
  88.      * @var string
  89.      */
  90.     public $charset;
  91.     
  92.     /**
  93.      * Fall Back Character Set
  94.      * 
  95.      * When no character encoding is detected in the document, use the value in 
  96.      * $charset as the fallback character set.
  97.      *
  98.      * @var bool
  99.      */
  100.     public $fbc;
  101.     
  102.     /**
  103.      * Document type
  104.      * 
  105.      * Document Type override: Specify the Document Type (DOCTYPE) to use when 
  106.      * parsing the document. When used with the auxiliary parameter fbd set to 1, 
  107.      * the given document type will only be used as a fallback value, in case the
  108.      * document's DOCTYPE declaration is missing or unrecognized.
  109.      * @var string
  110.      */
  111.     public $doctype;
  112.     
  113.     /**
  114.      * Fall back doctype
  115.      * 
  116.      * When set to 1, use the value stored in $doctype when the document type
  117.      * cannot be automatically determined.
  118.      * 
  119.      * @var bool
  120.      */
  121.     public $fbd;
  122.     
  123.     /**
  124.      * Verbose output
  125.      * 
  126.      * In the web interface, when set to 1, will make error messages, explanations 
  127.      * and other diagnostics more verbose.
  128.      * In SOAP output, does not have any impact.
  129.      * @var bool
  130.      */
  131.     public $verbose = false;
  132.     
  133.     /**
  134.      * Show source
  135.      * 
  136.      * In the web interface, triggers the display of the source after the validation
  137.      * results. In SOAP output, does not have any impact.
  138.      * @var bool
  139.      */
  140.     public $ss = false;
  141.     
  142.     /**
  143.      * outline
  144.      * 
  145.      * In the web interface, when set to 1, triggers the display of the document 
  146.      * outline after the validation results. In SOAP output, does not have any
  147.      * impact.
  148.      * @var bool
  149.      */
  150.     public $outline = false;
  151.     
  152.     /**
  153.      * HTTP_Request object.
  154.      * @var object
  155.      */
  156.     protected $request;
  157.     
  158.     /**
  159.      * Constructor for the class.
  160.      * 
  161.      * @param array $options An array of options
  162.      */
  163.     function __construct($options = array())
  164.     {
  165.         $this->setOptions($options);
  166.     }
  167.     
  168.     /**
  169.      * Sets options for the class.
  170.      * Pass an associative array of options for the class.
  171.      *
  172.      * @param array $options array of options
  173.      * 
  174.      * @return void
  175.      */
  176.     function setOptions($options)
  177.     {
  178.         foreach ($options as $option=>$val) {
  179.             $this->$option = $val;
  180.         }
  181.     }
  182.     
  183.     /**
  184.      * Validates a given URI
  185.      * 
  186.      * Executes the validator using the current parameters and returns a Response 
  187.      * object on success.
  188.      *
  189.      * @param string $uri The address to the page to validate ex: http://example.com/
  190.      * 
  191.      * @return mixed object Services_W3C_HTMLValidator | bool false
  192.      */
  193.     function validate($uri)
  194.     {
  195.         $this->uri = $uri;
  196.         $this->buildRequest('uri');
  197.         if ($this->sendRequest()) {
  198.             return $this->parseSOAP12Response($this->request->getResponseBody());
  199.         } else {
  200.             return false;
  201.         }
  202.     }
  203.     
  204.     /**
  205.      * Validates the local file
  206.      * 
  207.      * Requests validation on the local file, from an instance of the W3C validator.
  208.      * The file is posted to the W3 validator using multipart/form-data.
  209.      * 
  210.      * @param string $file file to be validated.
  211.      * 
  212.      * @return mixed object Services_W3C_HTMLValidator | bool fals
  213.      */
  214.     function validateFile($file)
  215.     {
  216.         if (file_exists($file)) {
  217.             $this->uploaded_file = $file;
  218.             $this->buildRequest('file');
  219.             if ($this->sendRequest()) {
  220.                 return $this->parseSOAP12Response($this->request->getResponseBody());
  221.             } else {
  222.                 return false;
  223.             }
  224.         } else {
  225.             // No such file!
  226.             return false;
  227.         }
  228.     }
  229.     
  230.     /**
  231.      * Validate an html string
  232.      * 
  233.      * @param string $html full html document fragment
  234.      * 
  235.      * @return mixed object Services_W3C_HTMLValidator | bool fals
  236.      */
  237.     function validateFragment($html)
  238.     {
  239.         $this->fragment = $html;
  240.         $this->buildRequest('fragment');
  241.         if ($this->sendRequest()) {
  242.             return $this->parseSOAP12Response($this->request->getResponseBody());
  243.         } else {
  244.             return false;
  245.         }
  246.     }
  247.     
  248.     /**
  249.      * Prepares a request object to send to the validator.
  250.      *
  251.      * @param string $type uri, file, or fragment
  252.      * 
  253.      * @return void
  254.      */
  255.     protected function buildRequest($type = 'uri')
  256.     {
  257.         $this->request = new HTTP_Request();
  258.         $this->request->setURL($this->validator_uri);
  259.         switch ($type) {
  260.         case 'uri':
  261.         default:
  262.             $this->request->setMethod(HTTP_REQUEST_METHOD_GET);
  263.             $this->request->addQueryString('uri', $this->uri);
  264.             $method = 'addQueryString';
  265.             break;
  266.         case 'file':
  267.             $this->request->setMethod(HTTP_REQUEST_METHOD_POST);
  268.             $this->request->addFile('uploaded_file',
  269.                                      $this->uploaded_file,
  270.                                      'text/html');
  271.             $method = 'addPostData';
  272.             break;
  273.         case 'fragment':
  274.             $this->request->setMethod(HTTP_REQUEST_METHOD_POST);
  275.             $this->request->addPostData('fragment', $this->fragment);
  276.             $method = 'addPostData';
  277.             break;
  278.         }
  279.         
  280.         foreach (array( 'charset',
  281.                         'fbc',
  282.                         'doctype',
  283.                         'fbd',
  284.                         'verbose',
  285.                         'ss',
  286.                         'outline',
  287.                         'output') as $option) {
  288.             if (isset($this->$option)) {
  289.                 if (is_bool($this->$option)) {
  290.                     $this->request->$method($option, intval($this->$option));
  291.                 } else {
  292.                     $this->request->$method($option, $this->$option);
  293.                 }
  294.             }
  295.         }
  296.     }
  297.     
  298.     /**
  299.      * Actually sends the request to the HTML Validator service
  300.      *
  301.      * @return bool true | false
  302.      */
  303.     protected function sendRequest()
  304.     {
  305.         if (!PEAR::isError($this->request->sendRequest())) {
  306.             return true;
  307.         } else {
  308.             return false;
  309.         }
  310.     }
  311.     
  312.     /**
  313.      * Parse an XML response from the validator
  314.      * 
  315.      * This function parses a SOAP 1.2 response xml string from the validator.
  316.      *
  317.      * @param string $xml The raw soap12 XML response from the validator.
  318.      * 
  319.      * @return mixed object Services_W3C_HTMLValidator_Response | bool false
  320.      */
  321.     static function parseSOAP12Response($xml)
  322.     {
  323.         $doc = new DOMDocument();
  324.         if ($doc->loadXML($xml)) {
  325.             $response = new Services_W3C_HTMLValidator_Response();
  326.             
  327.             // Get the standard CDATA elements
  328.             foreach (array('uri','checkedby','doctype','charset') as $var) {
  329.                 $element = $doc->getElementsByTagName($var);
  330.                 if ($element->length) {
  331.                     $response->$var = $element->item(0)->nodeValue;
  332.                 }
  333.             }
  334.             // Handle the bool element validity
  335.             $element = $doc->getElementsByTagName('validity');
  336.             if ($element->length &&
  337.                 $element->item(0)->nodeValue == 'true') {
  338.                 $response->validity = true;
  339.             } else {
  340.                 $response->validity = false;
  341.             }
  342.             if (!$response->validity) {
  343.                 $errors = $doc->getElementsByTagName('error');
  344.                 foreach ($errors as $error) {
  345.                     $response->errors[] =
  346.                         new Services_W3C_HTMLValidator_Error($error);
  347.                 }
  348.             }
  349.             $warnings = $doc->getElementsByTagName('warning');
  350.             foreach ($warnings as $warning) {
  351.                 $response->warnings[] =
  352.                     new Services_W3C_HTMLValidator_Warning($warning);
  353.             }
  354.             return $response;
  355.         } else {
  356.             // Could not load the XML.
  357.             return false;
  358.         }
  359.     }
  360. }
  361. ?>