Update of /cvsroot/php-blog/serendipity/plugins/serendipity_event_htmlvalidator
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12891
Added Files:
serendipity_event_htmlvalidator.php
Log Message:
New event plugin: HTML Validator.
If used, it can validate your entries upon preview of your created entry.
--- NEW FILE: serendipity_event_htmlvalidator.php ---
<?php # $Id: serendipity_event_htmlvalidator.php,v 1.1 2004/02/10 15:49:24 garvinhicking Exp $
switch ($serendipity['lang']) {
case 'de':
@define('PLUGIN_EVENT_HTMLVALIDATOR_NAME', 'HTML Validator');
@define('PLUGIN_EVENT_HTMLVALIDATOR_DESC', 'Überprüft, ob ein Artikel XML-konform ist');
@define('PLUGIN_EVENT_HTMLVALIDATOR_CHARSET', 'Zeichensatz');
@define('PLUGIN_EVENT_HTMLVALIDATOR_CHARSETDESC', 'Der übliche Zeichensatz Ihrer Einträge');
@define('PLUGIN_EVENT_HTMLVALIDATOR_DOCTYPE', 'Dokumenttyp');
@define('PLUGIN_EVENT_HTMLVALIDATOR_DOCTYPEDESC', 'Der übliche Dokumenttyp Ihrer Einträge');
break;
case 'en':
case 'es':
default:
@define('PLUGIN_EVENT_HTMLVALIDATOR_NAME', 'HTML Validator');
@define('PLUGIN_EVENT_HTMLVALIDATOR_DESC', 'Validates entries on their XML-conformity');
@define('PLUGIN_EVENT_HTMLVALIDATOR_CHARSET', 'Charset');
@define('PLUGIN_EVENT_HTMLVALIDATOR_CHARSETDESC', 'The usual charset of your articles');
@define('PLUGIN_EVENT_HTMLVALIDATOR_DOCTYPE', 'Doctype');
@define('PLUGIN_EVENT_HTMLVALIDATOR_DOCTYPEDESC', 'The usual document type of your articles');
break;
}
class serendipity_event_htmlvalidator extends serendipity_event
{
function introspect(&$propbag)
{
global $serendipity;
$propbag->add('name', PLUGIN_EVENT_HTMLVALIDATOR_NAME);
$propbag->add('description', PLUGIN_EVENT_HTMLVALIDATOR_DESC);
$propbag->add('event_hooks', array(
'backend_preview' => true
));
$propbag->add('configuration', array('charset', 'doctype'));
}
function introspect_config_item($name, &$propbag)
{
switch($name) {
case 'charset':
$propbag->add('type', 'select');
$propbag->add('name', PLUGIN_EVENT_HTMLVALIDATOR_CHARSET);
$propbag->add('description', PLUGIN_EVENT_HTMLVALIDATOR_CHARSETDESC);
$propbag->add('select_values', array(
'(detect automatically)' => '(detect automatically)',
'utf-8 (Unicode, worldwide)' => 'utf-8 (Unicode, worldwide)',
'utf-16 (Unicode, worldwide)' => 'utf-16 (Unicode, worldwide)',
'iso-8859-1 (Western Europe)' => 'iso-8859-1 (Western Europe)',
'iso-8859-2 (Central Europe)' => 'iso-8859-2 (Central Europe)',
'iso-8859-3 (Southern Europe)' => 'iso-8859-3 (Southern Europe)',
'iso-8859-4 (Baltic Rim)' => 'iso-8859-4 (Baltic Rim)',
'iso-8859-5 (Cyrillic)' => 'iso-8859-5 (Cyrillic)',
'iso-8859-6-i (Arabic)' => 'iso-8859-6-i (Arabic)',
'iso-8859-7 (Greek)' => 'iso-8859-7 (Greek)',
'iso-8859-8-i (Hebrew)' => 'so-8859-8-i (Hebrew)',
'iso-8859-9 (Turkish)' => 'iso-8859-9 (Turkish)',
'iso-8859-10 (Latin 6)' => 'iso-8859-10 (Latin 6)',
'iso-8859-13 (Latin 7)' => 'iso-8859-13 (Latin 7)',
'iso-8859-14 (Celtic)' => 'iso-8859-14 (Celtic)',
'iso-8859-15 (Latin 9)' => 'iso-8859-15 (Latin 9)',
'us-ascii (basic English)' => 'us-ascii (basic English)',
'euc-jp (Japanese, Unix)' => 'euc-jp (Japanese, Unix)',
'shift_jis (Japanese, Win/Mac)' => 'shift_jis (Japanese, Win/Mac)',
'iso-2022-jp (Japanese, email)' => 'iso-2022-jp (Japanese, email)',
'euc-kr (Korean)' => 'euc-kr (Korean)',
'gb2312 (Chinese, simplified)' => 'gb2312 (Chinese, simplified)',
'gb18030 (Chinese, simplified)' => 'gb18030 (Chinese, simplified)',
'big5 (Chinese, traditional)' => 'big5 (Chinese, traditional)',
'tis-620 (Thai)' => 'tis-620 (Thai)',
'koi8-r (Russian)' => 'koi8-r (Russian)',
'koi8-u (Ukrainian)' => 'koi8-u (Ukrainian)',
'macintosh (MacRoman)' => 'macintosh (MacRoman)',
'windows-1250 (Central Europe)' => 'windows-1250 (Central Europe)',
'windows-1251 (Cyrillic)' => 'windows-1251 (Cyrillic)',
'windows-1252 (Western Europe)' => 'windows-1252 (Western Europe)',
'windows-1253 (Greek)' => 'windows-1253 (Greek)',
'windows-1254 (Turkish)' => 'windows-1254 (Turkish)',
'windows-1255 (Hebrew)' => 'windows-1255 (Hebrew)',
'windows-1256 (Arabic)' => 'windows-1256 (Arabic)',
'windows-1257 (Baltic Rim)' => 'windows-1257 (Baltic Rim)'
));
break;
case 'doctype':
$propbag->add('type', 'select');
$propbag->add('name', PLUGIN_EVENT_HTMLVALIDATOR_DOCTYPE);
$propbag->add('description', PLUGIN_EVENT_HTMLVALIDATOR_DOCTYPEDESC);
$propbag->add('select_values', array(
'Inline' => '(detect automatically)',
'XHTML 1.1' => 'XHTML 1.1',
'XHTML Basic 1.0' => 'XHTML Basic 1.0',
'XHTML 1.0 Strict' => 'XHTML 1.0 Strict',
'XHTML 1.0 Transitional' => 'XHTML 1.0 Transitional',
'XHTML 1.0 Frameset' => 'XHTML 1.0 Frameset',
'HTML 4.01 Strict' => 'HTML 4.01 Strict',
'HTML 4.01 Transitional' => 'HTML 4.01 Transitional',
'HTML 4.01 Frameset' => 'HTML 4.01 Frameset',
'HTML 3.2' => 'HTML 3.2',
'HTML 2.0' => 'HTML 2.0'
));
break;
default:
return false;
}
return true;
}
function generate_content(&$title) {
$title = PLUGIN_EVENT_HTMLVALIDATOR_NAME;
}
function event_hook($event, &$bag, &$eventData) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'backend_preview':
$url = 'validator.w3.org';
$path = '/check';
$fp = fsockopen($url, 80, $errno, $errstr, 30);
$doctype = $this->get_config('doctype');
$charset = $this->get_config('charset');
if (empty($doctype)) {
$doctype = 'XHTML 1.1';
}
if (empty($charset)) {
$charset = 'iso-8859-1 (Western Europe)';
}
$data = '<html><head><title>s9y</title></head><body><div>'
. $eventData
. '</div></body></html>';
$request_data .= '-----------------------------24464570528145
Content-Disposition: form-data; name="uploaded_file"; filename="s9y.htm"
Content-Type: text/html
' . $data . '
-----------------------------24464570528145
Content-Disposition: form-data; name="charset"
' . $charset . '
-----------------------------24464570528145
Content-Disposition: form-data; name="doctype"
' . $doctype . '
-----------------------------24464570528145
Content-Disposition: form-data; name="verbose"
1
-----------------------------24464570528145--';
$request_length = strlen($request_data);
$REQUEST = array();
$REQUEST[] = 'POST ' . $path . ' HTTP/1.0';
$REQUEST[] = 'Host: ' . $url;
$REQUEST[] = 'User-Agent: serendipity/' . $serendipity['version'];
$REQUEST[] = 'Referer: http://validator.w3.org/';
$REQUEST[] = 'Content-Type: multipart/form-data; boundary=---------------------------24464570528145';
$REQUEST[] = 'Content-Length: ' . $request_length;
$REQUEST[] = 'Connection: close' . "\n";
$REQUEST[] = $request_data;
$REQUEST_STRING = implode("\n", $REQUEST);
fputs($fp, $REQUEST_STRING);
$line = fgets($fp, 1024);
if (preg_match('@^HTTP/1\.. (2|3)0(2|0)@', $line)) {
$out = '';
$inheader = 1;
while(!feof($fp) && strlen($out) < 200000) {
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
} elseif (!$inheader) {
$out .= $line;
}
}
}
fclose($fp);
preg_match('@<table class="header">.+</table>.+</div>.+(<h2 .+)</body>@ms', $out, $html);
// Cut the waste
$html[1] = preg_replace(
array(
'@<address>.+</address>@ms',
'@<dl class="tip">.+</dl>@ms',
'@<div id="source".+>.+</div>@msU'
),
array(
'',
'',
''
),
$html[1]
);
echo '<div style="border: 1px solid red; margin: 10px; padding: 5px; "><div>' . $html[1] . '</div>';
return true;
break;
default:
return false;
break;
}
} else {
return false;
}
}
}
/* vim: set sts=4 ts=4 expandtab : */
?>
|