Wednesday, July 30, 2003

Validating HTML/XHTML behind a firewall

Someone asked the question on the evolt mailing list about validating XHTML behind a firewall. The most popular way to validate XHTML is using the w3c HTML validator, however the only options are to enter a url, or to upload a file. That can be cumbersome, so steve clay wrote a handy php script that crawls a url and uploads the file then displays the results. I ported his script to ColdFusion below. It works great in CFMX, but there are issues in CF5 (cfhttp is url encoding the charset). You might also want to wrap a html tags around this code.

<!---
Ported To ColdFusion by Pete Freitag www.cfdev.com
Based on http://mrclay.org/junk/software/private_validator
License: (GPL) http://www.opensource.org/licenses/gpl-license.html 
--->
<cfoutput>
<form action="#CGI.SCRIPT_NAME#" method="post">
	<label for="location">Validate Local URL:</label>
	<input type="text" name="location" value="http://#CGI.SERVER_NAME#/" />
	<input type="submit" value="Validate" />
</form>
</cfoutput>

<cfif IsDefined("form.location")>
	<cfset tempFile = GetTempFile(GetTempDirectory(), "validatorFile")>
	<cfhttp url="#form.location#" method="get" path="#GetTempDirectory()#"
		file="#GetFileFromPath(tempFile)#" />
	<cfhttp url="http://validator.w3.org/check" method="post" resolveurl="yes">
		<cfhttpparam type="formfield" name="charset" value="UTF-8">
		<cfhttpparam type="formfield" name="doctype" value="Inline">
		<cfhttpparam type="formfield" name="ss" value="1"> 
		<cfhttpparam type="formfield" name="verbose" value="1">
		<cfhttpparam type="file" file="#tempFile#" name="uploaded_file">
	</cfhttp>
	<cffile action="delete" file="#tempFile#">
	<cfoutput>#CFHTTP.FileContent#</cfoutput>
</cfif>