Location: PHPKode > projects > OpenWolf Guidelines Validator > openWolf 0.9.9/w3Validator.class.php
<?php

/*************************************************

openWolf - an HTML accessibility guidelines validator
Author: Geoff Munn (hide@address.com)
Version: 0.9.9

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

You may contact the author of openWolf by e-mail at: hide@address.com

The latest version of openWolf can be obtained from:
http://openwolf.sourceforge.net/

*************************************************/

class w3{
	//create a site profile with the site name, pages, and rules to check
	
	var $content; 	//Holds the w3 validator page results
	var $w3;	//A snoopy class instance
	var $parse;	//A parse class instance
	var $validStatusValue; //Is this valid html or not?
	var $errorParse;//another parse class instance
	var $is_xml=false;
	
	function validate_page($page){
		$this->w3= new Snoopy;
	
		$xml='';
		if($this->is_xml==true){
			$xml='&output=xml';	
		}
		$url='http://validator.w3.org/check?uri=' . urlencode($page) . '&charset=%28detect+automatically%29&doctype=Inline' . $xml;
		
		$this->w3->fetch($url);
		
		$this->content=$this->w3->results;
		
	}
	
	function validStatus(){
		//We can identify the valid status by looking at the H2 tags and checking for a class called 'valid',
		//or 'invalid.
				
		if($this->is_xml==false){
			$this->parse = new parse;
		
			$this->parse->parse_HTML($this->content, true, false);
			
				$temp=$this->parse->getElementsByTagname('h2');
				
				if(count($temp)>0){
					$status='unknown';
					foreach($temp as $this_element){
						$class=$this->parse->getAttribute($this_element, 'class'); //getAttribute($this_element, 'class');
						switch ($class) {
							case 'valid':
								$this->validStatus='valid';
								$status='valid';
								break;
							case 'tentative_valid':
								$this->validStatus='tentative valid';
								$status='tentative_valid';
								break;
							case 'invalid':
								$this->validStatus='invalid';
								$status='invalid';
								break;
						}
					}
					$this->validStatusValue=$status;
					return $status;
				} else {
					$this->validStatusValue='no status';
					return 'no status';
				}
		} else {
			$bits=explode(':', $this->w3->headers[4]);
			return strtolower(trim($bits[1]));
		}
	}
	
	function reverse_htmlentities($mixed){
	   $htmltable = get_html_translation_table(HTML_ENTITIES);
	   foreach($htmltable as $key => $value){
	       $mixed = ereg_replace(addslashes($value),$key,$mixed);
	   }
	   return $mixed;
	}

	function getErrors(){
			
		if(!$this->is_xml==true){
			//There should be only one <ol>
			$temp=$this->parse->getElementsByTagname('ol');
			
			//Each instance of an <li> element is an individual error/message
			/*
			<li>
				<span class="err_type">ERROR TYPE</span>
				<em>LINE AND COLUMN</em>
				<span class="msg">MESSAGE</span>
				<div>DESCRIPTION</div>
			</li>
			
			*/
			
			$all_instances=$this->parse->all_descendants($temp[0], false);
			$results=Array();
			$count=-1;
			$found_em=true;
			foreach($all_instances as $this_instance){
				
				$tagname=$this->parse->tagname($this_instance);
				switch($tagname){
					case 'span':
						$class=$this->parse->getAttribute($this_instance, 'class');
						if($class=='err_type'){
								$count++;
								//set up the initial values:
								$results['text'][$count]='';
								$results['line_col'][$count]='';
								$results['info'][$count]='';
								
								$results['indices'][$count]=-1;	
																
								$text=strtolower($this->parse->innerText($this_instance));
								if($text=='error'){
									$text='fail';
								}
								$results['type'][$count]=$text;
								$found_em=false;
						}
						if($class=='msg'){
							//this is the instance 
							$results['instances'][$count]=$this->reverse_htmlentities($this->parse->innerHTML($this_instance));
						}
						break;
					case 'pre':
						$results['text'][$count]='<pre>' . $this->parse->innerHTML($this_instance) . '</pre>';
						break;
					case 'em':
						if(!$found_em){
							$results['line_col'][$count]=$this->parse->innerText($this_instance);
							$found_em=true;
						}
						break;
					case 'div':	
						$results['info'][$count]=$this->parse->innerHTML($this_instance);
						break;
				}
			}
			return $results;
			
		} else {
			$temp=$this->parse->getElementsByTagname('msg');
			$count=0;
			$messages=Array();
			
			foreach($temp as $this_element){
				$line=$this->parse->getAttribute($this_element, 'line');
				$col=$this->parse->getAttribute($this_element, 'col');
				$text=$this->parse->innerText($this_element);
				$sourceIndex=$this->parse->getElementByPos($line, $col);
				$messages[$count]['line']=$line;
				$messages[$count]['col']=$col;
				$messages[$count]['text']=$text;
				$count++;
			}
			return $messages;
		}
	}
	
	function getErrorCount(){
		//only set up for xml output so far:
		$bits=explode(':', $this->w3->headers[5]);
		return trim($bits[1]);
	}
	
}

?>
Return current item: OpenWolf Guidelines Validator