Services_W3C_CSSValidator
[ class tree: Services_W3C_CSSValidator ] [ index: Services_W3C_CSSValidator ] [ all elements ]

Source for file CSSValidator.php

Documentation is available at CSSValidator.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2007, Laurent Laville <pear@laurent-laville.org>
  4.  *
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  *     * Redistributions of source code must retain the above copyright
  12.  *       notice, this list of conditions and the following disclaimer.
  13.  *     * Redistributions in binary form must reproduce the above copyright
  14.  *       notice, this list of conditions and the following disclaimer in the
  15.  *       documentation and/or other materials provided with the distribution.
  16.  *     * Neither the name of the authors nor the names of its contributors
  17.  *       may be used to endorse or promote products derived from this software
  18.  *       without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24.  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * PHP version 5
  33.  *
  34.  * @category Web_Services
  35.  * @package  Services_W3C_CSSValidator
  36.  * @author   Laurent Laville <pear@laurent-laville.org>
  37.  * @license  http://www.opensource.org/licenses/bsd-license.php BSD
  38.  * @version  CVS: $id$
  39.  * @link     http://pear.php.net/package/Services_W3C_CSSValidator
  40.  * @since    File available since Release 0.1.0
  41.  */
  42.  
  43. require_once 'HTTP/Request.php';
  44.  
  45. require_once 'Services/W3C/CSSValidator/Response.php';
  46. require_once 'Services/W3C/CSSValidator/Error.php';
  47. require_once 'Services/W3C/CSSValidator/Warning.php';
  48.  
  49. /**
  50.  * Base class for utilizing the W3C CSS Validator service.
  51.  *
  52.  * @category Web_Services
  53.  * @package  Services_W3C_CSSValidator
  54.  * @author   Laurent Laville <pear@laurent-laville.org>
  55.  * @license  http://www.opensource.org/licenses/bsd-license.php BSD
  56.  * @link     http://pear.php.net/package/Services_W3C_CSSValidator
  57.  * @since    Class available since Release 0.1.0
  58.  */
  59. {
  60.     /**
  61.      * URI to the W3C validator.
  62.      *
  63.      * @var    string 
  64.      */
  65.     const VALIDATOR_URI = 'http://jigsaw.w3.org/css-validator/validator';
  66.  
  67.     /**
  68.      * The URL of the document to validate
  69.      *
  70.      * @var    string 
  71.      */
  72.     protected $uri;
  73.  
  74.     /**
  75.      * Internally used filename of a file to upload to the validator
  76.      * POSTed as multipart/form-data
  77.      *
  78.      * @var    string 
  79.      */
  80.     protected $uploaded_file;
  81.  
  82.     /**
  83.      * CSS fragment to validate.
  84.      *
  85.      * Full documents only. At the moment, will only work if data is sent with the
  86.      * UTF-8 encoding.
  87.      *
  88.      * @var    string 
  89.      */
  90.     protected $fragment;
  91.  
  92.     /**
  93.      * Options list (available with default values) :
  94.      *
  95.      * output - Output format
  96.      *          Triggers the various outputs formats of the validator. If unset,
  97.      *          the usual Web html format will be sent. If set to soap12,
  98.      *          the SOAP1.2 interface will be triggered.
  99.      *
  100.      * warning - Warning level
  101.      *           Default value is '1', and value could one of these :
  102.      *           <ul>
  103.      *             <li>2</li> all warning messages
  104.      *             <li>1</li> normal report
  105.      *             <li>0</li> most important warning messages
  106.      *             <li>no</li> none messages
  107.      *           </ul>
  108.      *
  109.      * profile - Profile
  110.      *           Default value is 'css21', and value could one of these :
  111.      *           <ul>
  112.      *             <li>none</li> none profile
  113.      *             <li>css1</li> CSS level 1
  114.      *             <li>css2</li> CSS level 2
  115.      *             <li>css21</li> CSS level 2.1
  116.      *             <li>css3</li> CSS level 3
  117.      *             <li>svg</li> SVG
  118.      *             <li>svgbasic</li> SVG Basic
  119.      *             <li>svgtiny</li> SVG Tiny
  120.      *             <li>mobile</li> Mobile
  121.      *             <li>atsc-tv</li> ATSC TV
  122.      *             <li>tv</li> TV
  123.      *           </ul>
  124.      *
  125.      * usermedium - User medium
  126.      *              Default value is 'all', and value could one of these :
  127.      *              <ul>
  128.      *                <li>all</li>
  129.      *                <li>aural</li>
  130.      *                <li>braille</li>
  131.      *                <li>embossed</li>
  132.      *                <li>handheld</li>
  133.      *                <li>print</li>
  134.      *                <li>projection</li>
  135.      *                <li>screen</li>
  136.      *                <li>tty</li>
  137.      *                <li>tv</li>
  138.      *                <li>presentation</li>
  139.      *              </ul>
  140.      *
  141.      * lang - Language used for response messages
  142.      *        Default value is 'en', and value could one of these :
  143.      *        en, fr, ja, es, zh-cn, nl, de
  144.      *
  145.      * @var    array 
  146.      */
  147.     protected $options;
  148.  
  149.     /**
  150.      * HTTP_Request object.
  151.      *
  152.      * @var    object 
  153.      */
  154.     protected $request;
  155.  
  156.     /**
  157.      * Constructor for the class.
  158.      *
  159.      * @return void 
  160.      */
  161.     public function __construct()
  162.     {
  163.         $this->options = array('output' => 'soap12''warning' => '1',
  164.             'profile' => 'css21''usermedium' => 'all''lang' => 'en');
  165.     }
  166.  
  167.     /**
  168.      * Sets options for the class.
  169.      *
  170.      * @param string $option Name of option to set
  171.      * @param string $val    Value of option to set
  172.      *
  173.      * @return void 
  174.      */
  175.     public function __set($option$val)
  176.     {
  177.         // properties that can be set directly
  178.         static $setting_allowed = array('uri');
  179.  
  180.         if (isset($this->options[$option])) {
  181.             $this->options[$option$val;
  182.         elseif (property_exists($this$option)) {
  183.             if (in_array($option$setting_allowed)) {
  184.                 $this->$option $val;
  185.             }
  186.         }
  187.     }
  188.  
  189.     /**
  190. /**
  191.      * Gets options for the class.
  192.      *
  193.      * @param string $option Name of option to set
  194.      *
  195.      * @return mixed 
  196.      */
  197.     public function __get($option)
  198.     {
  199.         // properties that can be get directly
  200.         static $getting_allowed = array('uri');
  201.  
  202.         $r = null;
  203.         if (isset($this->options[$option])) {
  204.             $r $this->options[$option];
  205.         elseif (property_exists($this$option)) {
  206.             if (in_array($option$getting_allowed)) {
  207.                 $r $this->$option;
  208.             }
  209.         }
  210.         return $r;
  211.     }
  212.  
  213.     /**
  214.      * Validates a given URI
  215.      *
  216.      * Executes the validator using the current parameters and returns a Response
  217.      * object on success.
  218.      *
  219.      * @param string $uri The address to the page to validate ex: http://example.com/
  220.      *
  221.      * @return mixed object Services_W3C_CSSValidator_Response
  222.      *                       if web service call successfull,
  223.      *                boolean FALSE otherwise
  224.      */
  225.     public function validateUri($uri)
  226.     {
  227.         $this->uri = $uri;
  228.         $this->buildRequest('uri');
  229.         if ($this->sendRequest()) {
  230.             return Services_W3C_CSSValidator::parseSOAP12Response($this
  231.                 ->request->getResponseBody());
  232.         else {
  233.             return false;
  234.         }
  235.     }
  236.  
  237.     /**
  238.      * Validates the local file
  239.      *
  240.      * Requests validation on the local file, from an instance of the W3C validator.
  241.      * The file is posted to the W3C validator using multipart/form-data.
  242.      *
  243.      * @param string $file file to be validated.
  244.      *
  245.      * @return mixed object Services_W3C_CSSValidator_Response
  246.      *                       if web service call successfull,
  247.      *                boolean FALSE otherwise
  248.      */
  249.     public function validateFile($file)
  250.     {
  251.         if (file_exists($file)) {
  252.             $this->uploaded_file = $file;
  253.             $this->buildRequest('file')//return $this->request;
  254.             if ($this->sendRequest()) {
  255.                 return Services_W3C_CSSValidator::parseSOAP12Response($this
  256.                     ->request->getResponseBody());
  257.             else {
  258.                 return false;
  259.             }
  260.         else {
  261.             return false;
  262.         }
  263.     }
  264.  
  265.     /**
  266.      * Validate an html string
  267.      *
  268.      * @param string $css Full css document fragment
  269.      *
  270.      * @return mixed object Services_W3C_CSSValidator_Response
  271.      *                       if web service call successfull,
  272.      *                boolean FALSE otherwise
  273.      */
  274.     public function validateFragment($css)
  275.     {
  276.         $this->fragment = $css;
  277.         $this->buildRequest('fragment');
  278.         if ($this->sendRequest()) {
  279.             return Services_W3C_CSSValidator::parseSOAP12Response($this
  280.                 ->request->getResponseBody());
  281.         else {
  282.             return false;
  283.         }
  284.     }
  285.  
  286.     /**
  287.      * Prepares a request object to send to the validator.
  288.      *
  289.      * @param string $type uri, file, or fragment
  290.      *
  291.      * @return void 
  292.      */
  293.     protected function buildRequest($type 'uri')
  294.     {
  295.         $this->request = new HTTP_Request();
  296.         $this->request->setURL(self::VALIDATOR_URI);
  297.         switch ($type{
  298.         case 'uri':
  299.         default:
  300.             $this->request->setMethod(HTTP_REQUEST_METHOD_GET);
  301.             $this->request->addQueryString('uri'$this->uri);
  302.             $method 'addQueryString';
  303.             break;
  304.         case 'file':
  305.             $this->request->setMethod(HTTP_REQUEST_METHOD_POST);
  306.             $this->request->addFile('file',
  307.                                      $this->uploaded_file,
  308.                                      'text/css');
  309.             $method 'addPostData';
  310.             break;
  311.         case 'fragment':
  312.             $this->request->setMethod(HTTP_REQUEST_METHOD_GET);
  313.             $this->request->addQueryString('text'$this->fragment);
  314.             $method 'addQueryString';
  315.             break;
  316.         }
  317.  
  318.         $options = array('output''warning''profile''usermedium''lang');
  319.         foreach ($options as $option{
  320.             if (isset($this->options[$option])) {
  321.                 if (is_bool($this->options[$option])) {
  322.                     $this->request->$method($option,
  323.                         intval($this->options[$option]));
  324.                 else {
  325.                     $this->request->$method($option$this->options[$option]);
  326.                 }
  327.             }
  328.         }
  329.     }
  330.  
  331.     /**
  332.      * Actually sends the request to the CSS Validator service
  333.      *
  334.      * @return bool TRUE if request was sent successfully, FALSE otherwise
  335.      */
  336.     protected function sendRequest()
  337.     {
  338.         if (PEAR::isError($this->request->sendRequest())) {
  339.             return false;
  340.         else {
  341.             return true;
  342.         }
  343.     }
  344.  
  345.     /**
  346.      * Parse an XML response from the validator
  347.      *
  348.      * This function parses a SOAP 1.2 response xml string from the validator.
  349.      *
  350.      * @param string $xml The raw soap12 XML response from the validator.
  351.      *
  352.      * @return mixed object Services_W3C_CSSValidator_Response
  353.      *                       if parsing soap12 response successfully,
  354.      *                boolean FALSE otherwise
  355.      */
  356.     protected static function parseSOAP12Response($xml)
  357.     {
  358.         $doc = new DOMDocument();
  359.         // try to load soap 1.2 xml response, and suppress warning reports if any
  360.         if (@$doc->loadXML($xml)) {
  361.             $response = new Services_W3C_CSSValidator_Response();
  362.  
  363.             // Get the standard CDATA elements
  364.             $cdata = array('uri''checkedby''csslevel''date');
  365.             foreach ($cdata as $var{
  366.                 $element $doc->getElementsByTagName($var);
  367.                 if ($element->length{
  368.                     $response->$var $element->item(0)->nodeValue;
  369.                 }
  370.             }
  371.             // Handle the bool element validity
  372.             $element $doc->getElementsByTagName('validity');
  373.             if ($element->length &&
  374.                 $element->item(0)->nodeValue == 'true'{
  375.                 $response->validity = true;
  376.             else {
  377.                 $response->validity = false;
  378.             }
  379.             if (!$response->validity{
  380.                 $errors $doc->getElementsByTagName('error');
  381.                 foreach ($errors as $error{
  382.                     $response->addError(new
  383.                         Services_W3C_CSSValidator_Error($error));
  384.                 }
  385.             }
  386.             $warnings $doc->getElementsByTagName('warning');
  387.             foreach ($warnings as $warning{
  388.                     $response->addWarning(new
  389.                         Services_W3C_CSSValidator_Warning($warning));
  390.             }
  391.             return $response;
  392.         else {
  393.             // Could not load the XML document
  394.             return false;
  395.         }
  396.     }
  397. }
  398. ?>

Documentation generated on Fri, 23 Nov 2007 13:00:07 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.