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 / CSSValidator.php next >
Encoding:
PHP Script  |  2008-07-02  |  12.9 KB  |  398 lines

  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. class Services_W3C_CSSValidator
  60. {
  61.     /**
  62.      * URI to the W3C validator.
  63.      *
  64.      * @var    string
  65.      */
  66.     const VALIDATOR_URI = 'http://jigsaw.w3.org/css-validator/validator';
  67.  
  68.     /**
  69.      * The URL of the document to validate
  70.      *
  71.      * @var    string
  72.      */
  73.     protected $uri;
  74.  
  75.     /**
  76.      * Internally used filename of a file to upload to the validator
  77.      * POSTed as multipart/form-data
  78.      *
  79.      * @var    string
  80.      */
  81.     protected $uploaded_file;
  82.  
  83.     /**
  84.      * CSS fragment to validate.
  85.      *
  86.      * Full documents only. At the moment, will only work if data is sent with the
  87.      * UTF-8 encoding.
  88.      *
  89.      * @var    string
  90.      */
  91.     protected $fragment;
  92.  
  93.     /**
  94.      * Options list (available with default values) :
  95.      *
  96.      * output - Output format
  97.      *          Triggers the various outputs formats of the validator. If unset,
  98.      *          the usual Web html format will be sent. If set to soap12,
  99.      *          the SOAP1.2 interface will be triggered.
  100.      *
  101.      * warning - Warning level
  102.      *           Default value is '1', and value could one of these :
  103.      *           <ul>
  104.      *             <li>2</li> all warning messages
  105.      *             <li>1</li> normal report
  106.      *             <li>0</li> most important warning messages
  107.      *             <li>no</li> none messages
  108.      *           </ul>
  109.      *
  110.      * profile - Profile
  111.      *           Default value is 'css21', and value could one of these :
  112.      *           <ul>
  113.      *             <li>none</li> none profile
  114.      *             <li>css1</li> CSS level 1
  115.      *             <li>css2</li> CSS level 2
  116.      *             <li>css21</li> CSS level 2.1
  117.      *             <li>css3</li> CSS level 3
  118.      *             <li>svg</li> SVG
  119.      *             <li>svgbasic</li> SVG Basic
  120.      *             <li>svgtiny</li> SVG Tiny
  121.      *             <li>mobile</li> Mobile
  122.      *             <li>atsc-tv</li> ATSC TV
  123.      *             <li>tv</li> TV
  124.      *           </ul>
  125.      *
  126.      * usermedium - User medium
  127.      *              Default value is 'all', and value could one of these :
  128.      *              <ul>
  129.      *                <li>all</li>
  130.      *                <li>aural</li>
  131.      *                <li>braille</li>
  132.      *                <li>embossed</li>
  133.      *                <li>handheld</li>
  134.      *                <li>print</li>
  135.      *                <li>projection</li>
  136.      *                <li>screen</li>
  137.      *                <li>tty</li>
  138.      *                <li>tv</li>
  139.      *                <li>presentation</li>
  140.      *              </ul>
  141.      *
  142.      * lang - Language used for response messages
  143.      *        Default value is 'en', and value could one of these :
  144.      *        en, fr, ja, es, zh-cn, nl, de
  145.      *
  146.      * @var    array
  147.      */
  148.     protected $options;
  149.  
  150.     /**
  151.      * HTTP_Request object.
  152.      *
  153.      * @var    object
  154.      */
  155.     protected $request;
  156.  
  157.     /**
  158.      * Constructor for the class.
  159.      *
  160.      * @return void
  161.      */
  162.     public function __construct()
  163.     {
  164.         $this->options = array('output' => 'soap12', 'warning' => '1',
  165.             'profile' => 'css21', 'usermedium' => 'all', 'lang' => 'en');
  166.     }
  167.  
  168.     /**
  169.      * Sets options for the class.
  170.      *
  171.      * @param string $option Name of option to set
  172.      * @param string $val    Value of option to set
  173.      *
  174.      * @return void
  175.      */
  176.     public function __set($option, $val)
  177.     {
  178.         // properties that can be set directly
  179.         static $setting_allowed = array('uri');
  180.  
  181.         if (isset($this->options[$option])) {
  182.             $this->options[$option] = $val;
  183.         } elseif (property_exists($this, $option)) {
  184.             if (in_array($option, $setting_allowed)) {
  185.                 $this->$option = $val;
  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. ?>