Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. /*
  3. copyright Massimiliano Rossetti www.mrossetti.it ALL RIGHTS RESERVED
  4. Version 0.1 ALPHA
  5. Posted @ http://www.w3tools.info/2011/09/w3c-validator.html
  6. */
  7. class W3cValidator extends CurlObj {
  8.    
  9.     private $link;//link che va validato
  10.     private $xml;//xml ritornato dalla verifica richiesta
  11.        
  12.     public function __construct() {//inizializzo le proprietà di default
  13.         parent::__construct();
  14.     }
  15.    
  16.     private function set_link($url) {
  17.         $this->link = 'http://validator.w3.org/check?url='.$url.'&output=soap12';
  18.     }
  19.    
  20.     private function _validate($url, $mod = NULL) {
  21.         $this->set_link($url);
  22.         if($mod == 'fast') {
  23.             $this->http_header($this->link, '');
  24.         }elseif($mod == 'full') {
  25.             $this->http_get_withheader($this->link, '');
  26.         } else {//validazione predefinita
  27.             $this->http_get($this->link, '');
  28.         }
  29.         if($this->curl_info['http_code'] == 200) {
  30.             return true;
  31.         }
  32.         return false;
  33.     }
  34.    
  35.    
  36.     /*questi metodi ritornano un array con vari parametri a seconda del tipo richiesto*/
  37.    
  38.    
  39.     /* ritorna un array come fast validate ma completo degli errori presenti nella risposta xml */
  40.     public function validate($url) {
  41.         if($this->_validate($url)) {
  42.             return $this->xml_parsing($this->get_curl_output());
  43.         }
  44.         return false;
  45.     }
  46.    
  47.     //ritorna un array con i contenuti dell'header fornito
  48.     public function fast_validate($url) {
  49.         if($this->_validate($url, 'fast')) {
  50.              return $this->header_parsing($this->get_curl_output());
  51.         }
  52.         return false;
  53.     }
  54.    
  55.     /**/
  56.     public function full_validate($url) {
  57.         $this->_validate($url, 'full');
  58.     }
  59.    
  60.    
  61.     private function header_parsing($string) {
  62.         $tmp_arr = explode("\n", trim($string));
  63.         if(count($tmp_arr)) {
  64.             $result_arr = array();
  65.             foreach($tmp_arr as $str_value) {
  66.                 if(strstr($str_value, 'X-W3C-Validator-Status:')) {
  67.                 $result_arr['status'] = strtolower(trim(str_replace('X-W3C-Validator-Status:', '', $str_value)));
  68.                 }
  69.                 if(strstr($str_value, 'X-W3C-Validator-Errors:')) {
  70.                 $result_arr['err_num'] = intval(trim(str_replace('X-W3C-Validator-Errors:', '', $str_value)));
  71.                 }
  72.                 if(strstr($str_value, 'X-W3C-Validator-Warnings:')) {
  73.                 $result_arr['warn_num'] = intval(trim(str_replace('X-W3C-Validator-Warnings:', '', $str_value)));
  74.                 }
  75.            
  76.             }
  77.             unset($tmp_arr);
  78.             return $result_arr;
  79.         }
  80.         return false;
  81.     }
  82.    
  83.    
  84.     private function xml_parsing($string) {
  85.        
  86.         $result_arr = array();
  87.         $xml = new DomDocument();
  88.         @$xml->loadXML($string);
  89.         $xpath = new DOMXpath($xml);
  90.         $xpath->registerNamespace("m", "http://www.w3.org/2005/10/markup-validator");
  91.        
  92.        
  93.         $elements = $xpath->query("//m:validity");
  94.         if($elements->item(0)->nodeValue == 'true') {
  95.             $result_arr['status'] = 'valid';
  96.         }else {
  97.             $result_arr['status'] = 'invalid';
  98.         }
  99.        
  100.         $elements = $xpath->query("//m:errorcount");
  101.         $result_arr['err_num'] = intval($elements->item(0)->nodeValue);
  102.        
  103.  
  104.        
  105.         $result_arr['errors'] = array();
  106.         $result_arr['warnings'] = array();
  107.        
  108.         if($elements->item(0) && $elements->item(0)->nodeValue > 0) {
  109.            
  110.           $node_arr = $xpath->query("//m:errors/m:errorlist/m:error/m:line");
  111.           $i = 0;
  112.           foreach ($node_arr as $node) {
  113.               $result_arr['errors'][$i]['line'] = intval($node->nodeValue);
  114.               $i++;
  115.           }    
  116.            
  117.           $node_arr = $xpath->query("//m:errors/m:errorlist/m:error/m:col");
  118.           $i = 0;
  119.           foreach ($node_arr as $node) {
  120.               $result_arr['errors'][$i]['col'] = intval($node->nodeValue);
  121.               $i++;
  122.           }    
  123.            
  124.           $node_arr = $xpath->query("//m:errors/m:errorlist/m:error/m:message");
  125.           $i = 0;
  126.           foreach ($node_arr as $node) {
  127.               $result_arr['errors'][$i]['message'] = $node->nodeValue;
  128.               $i++;
  129.           }
  130.           $node_arr = $xpath->query("//m:errors/m:errorlist/m:error/m:messageid");
  131.            $i = 0;
  132.           foreach ($node_arr as $node) {
  133.               $result_arr['errors'][$i]['messageid'] = $node->nodeValue;
  134.               $i++;
  135.           }
  136.           $node_arr = $xpath->query("//m:errors/m:errorlist/m:error/m:explanation");
  137.            $i = 0;
  138.           foreach ($node_arr as $node) {
  139.               $result_arr['errors'][$i]['explanation'] = trim($node->nodeValue);
  140.               $i++;
  141.           }
  142.         }
  143.        
  144.         $elements = $xpath->query("//m:warningcount");
  145.         $result_arr['warn_num'] = intval($elements->item(0)->nodeValue);
  146.        
  147.         if($elements->item(0) && $elements->item(0)->nodeValue > 0) {
  148.           $node_arr = $xpath->query("//m:warnings/m:warninglist/m:warning/m:messageid");
  149.           $i = 0;
  150.           foreach ($node_arr as $node) {
  151.               $result_arr['warnings'][$i]['messageid'] = trim($node->nodeValue);
  152.               $i++;
  153.           }
  154.           $node_arr = $xpath->query("//m:warnings/m:warninglist/m:warning/m:message");
  155.           $i = 0;
  156.           foreach ($node_arr as $node) {
  157.               $result_arr['warnings'][$i]['message'] = trim($node->nodeValue);
  158.               $i++;
  159.           }
  160.            
  161.         }
  162.        
  163.        
  164.         return $result_arr;
  165.     }
  166.  
  167.     public function isValid($url) {//ritorna true o false
  168.         $result = $this->fast_validate($url);
  169.         if(is_array($result) && $result['status'] == 'valid') {
  170.             return true;
  171.         }
  172.         return false;
  173.         //http_code
  174.     }
  175.    
  176.     public function return_xml_string($url) {
  177.    
  178.     }
  179.    
  180.     public function get_ErrNum() {//ritorna il numero degli errori presenti nella pagina
  181.     }
  182. }
  183.  
  184. ?>
  185.