<?php
$uri = $_GET["uri"];
$charset = $_GET["charset"];
$doctype = $_GET["doctype"];
if (trim(strtolower($uri)) == "referer") $uri = $_SERVER["HTTP_REFERER"];
$fp = fsockopen("validator.w3.org", 80, $errno, $errstr, 30);
if ($fp)
{
$send = "GET /check?uri=" . $uri;
if (trim($charset) !== "") $send .= "&charset=" . $charset;
if (trim($doctype) !== "") $send .= "&doctype=" . $doctype;
$send .= " HTTP/1.1\r\n";
$send .= "Host: validator.w3.org\r\n";
$send .= "Accept: text/html;\r\n";
$send .= "Accept-Language: en-us, en;\r\n";
$send .= "Connection: Close\r\n\r\n";
fwrite($fp, $send);
$out = "";
while (!feof($fp))
{
$out .= fgets($fp, 128);
}
fclose($fp);
$im = imagecreatetruecolor(176, 31);
$body = substr($out, strpos($out, "<body"), strpos($out, ">", strpos($out, "</body")) - strpos($out, "<body"));
$h2 = trim(strip_tags(substr($body, strpos($body, ">", strpos($body, "<h2")) + 1, strpos($body, "</h2>", strpos($body, ">", strpos($body, "<h2"))) - strpos($body, ">", strpos($body, "<h2")) - 1)));
$result = str_replace(" validation", "", trim(strip_tags(substr($body, strpos($body, ">", strpos($body, "<td", strpos($body, "Result"))) + 1, strpos($body, "</td>", strpos($body, ">", strpos($body, "<td", strpos($body, "Result")))) - strpos($body, ">", strpos($body, "<td", strpos($body, "Result"))) - 1))));
if (stripos($h2, "valid") != FALSE)
{
$doctype = trim(strip_tags(substr($body, strpos($body, ">", strpos($body, "<td", strpos($body, 'for="doctype"'))) + 1, strpos($body, "<select", strpos($body, ">", strpos($body, "<td", strpos($body, 'for="doctype"')))) - strpos($body, ">", strpos($body, "<td", strpos($body, 'for="doctype"'))) - 1)));
if (stripos($h2, "not") != FALSE)
{
$error = trim(substr($result, strpos($result, ",") + 1));
$warning = "0 Warnings";
$back = imagecolorallocate($im, 210, 61, 36);
}
else if (stripos($h2, "tentatively") != FALSE)
{
$error = "0 Errors";
$warning = str_replace("w", "W", trim(substr($result, strpos($result, ",") + 1)));
$back = imagecolorallocate($im, 85, 176, 90);
}
else
{
$error = "0 Errors";
$warning = "0 Warnings";
$back = imagecolorallocate($im, 85, 176, 90);
}
if (substr($warning, 0, 1) == "1") $warning = str_replace("(s)", "", $warning);
else $warning = str_replace("(s)", "s", $warning);
$string = $doctype . "\n" . $error . ", " . $warning;
$font = "VeraBd.ttf";
}
else
{
$doctype = $h2;
$back = imagecolorallocate($im, 210, 61, 36);
$string = wordwrap($h2, 28);
$font = "VeraMoBd.ttf";
}
imagefill($im, 0, 0, $back);
$fore = imagecolorallocate($im, 255, 255, 255);
imagettftext($im, 7, 0, 4, 13, $fore, $font, $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
}
?>