Location: PHPKode > scripts > Private Validator > private-validator/PrivateValidator.class.php
<?php

// PrivateValidator class by Stephen Clay
// http://mrclay.org/junk/software/private_validator/
/*

Minimal usage:
	
$myPV = PrivateValidator('http://testing.dev/');
echo $myPV->validate();
exit();

*/

include_once('http.inc.php');

class PrivateValidator 
{
	var $URL;
	var $HTTP_client;
	var $file;
	var $error;
	
/****** public *******/
	
	function PrivateValidator($URL = false) 
	{
		$this->URL         = $URL;		
		$this->HTTP_client = new http(HTTP_V11, false);
	}
	
	function validate() 
	{
		if ($this->URL === false) 
		{
			$this->error = "no URL given.";
			return false;
		}
		
		// make HTTP request
		$lines = file($this->URL);
		if ($lines === false) 
		{
			$this->error = "<code>file('".htmlspecialchars($this->URL)."')</code> returned an error.";
			return false;
		}
		
		$this->file = join('',$lines);
		
		if ($this->is_markup($this->file)) 
		{
			return $this->validate_XHTML();
		}
		else
		{
			return $this->validate_CSS();
		}
	}
	
	function get_error()
	{
		return "<p>PrivateValidator.class.php: <strong style='color:#c00'>".$this->error."</strong></p>";
	}
	function get_pv_bookmarklet() 
	{
		$PATH = PrivateValidator::path_to_pv();
		return <<<EOD
<a href="javascript:(function(){document.location = '{$PATH}?URL=' + escape(document.location);})();">pV</a>
EOD;
	}
	function get_pvnw_bookmarklet() 
	{
		$PATH = PrivateValidator::path_to_pv();
		return <<<EOD
<a href="javascript:(function(){window.open('{$PATH}?URL=' + escape(document.location),'pV');})();">pV(nw)</a>
EOD;
	}

	function set_URL($newURL)
	{
		$this->URL = $newURL;	
	}

/****** private *******/

	// tests for closing HTML tag (I know..should base on content type)
	function is_markup($contents) 
	{
		return (stristr($contents,'</html>') !== false);
	}
	
	function validate_XHTML() 
	{
		$HTTP = &$this->HTTP_client;
	
		// form data
		$pv_fields = array
		(
			'charset' => '(detect automatically)',
			'doctype' => 'Inline',
			'ss'      => '1',  // show source (set to 0 to turn off)
			'verbose' => '1'   // verbose output
		);
		$pv_files = array();
		$pv_files[] = array
		(
			'name' => 'uploaded_file',
			'content-type' => 'text/html',  // this should be copied from internal server's Content-Type
			'filename' => $this->URL,
			// this has your server return a URL request
			'data' => $this->file
		);
		
		$HTTP->host = 'validator.w3.org';
		// HTTP POST
		if ($HTTP->multipart_post( '/check', $pv_fields, $pv_files ) == HTTP_STATUS_OK ) 
		{
			$pv_results = $HTTP->get_response_body();
		
			// add base element so client can find CSS files, etc.
			$pv_results = str_replace("<head>","<head><base href='http://validator.w3.org/' />" ,$pv_results );
			echo $pv_results;
			return true;
		} 
		else
		{
			$this->error = 'Server returned status code '.$HTTP->status;
			return false;
		}
	}

	// not implemented
	function validate_CSS() 
	{
		$this->error = "CSS validation not implemented yet.";
		return false;
	}
	
	function path_to_pv() 
	{
		return ("http://".$_SERVER['SERVER_NAME']
				."/".substr($_SERVER['PHP_SELF'], 1, strrpos($_SERVER['PHP_SELF'],"/"))
				."privateValidate.php");
	}
}

?>
Return current item: Private Validator