2006-09-10 02:02:40 +00:00
|
|
|
<?php
|
|
|
|
|
2006-09-11 02:52:47 +00:00
|
|
|
/**
|
|
|
|
* Generates XML and HTML documents describing configuration.
|
|
|
|
*/
|
|
|
|
|
2006-09-10 20:21:19 +00:00
|
|
|
/*
|
|
|
|
TODO:
|
|
|
|
- make XML format richer (see below)
|
|
|
|
- extend XSLT transformation (see the corresponding XSLT file)
|
|
|
|
- allow generation of packaged docs that can be easily moved
|
|
|
|
- multipage documentation
|
|
|
|
- determine how to multilingualize
|
2006-09-13 00:59:20 +00:00
|
|
|
- factor out code into classes
|
2006-09-10 20:21:19 +00:00
|
|
|
*/
|
|
|
|
|
2006-09-11 02:52:47 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Check and configure environment
|
|
|
|
|
|
|
|
if (version_compare('5', PHP_VERSION, '>')) exit('Requires PHP 5 or higher.');
|
2006-09-10 02:02:40 +00:00
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
2006-09-11 02:52:47 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Include HTML Purifier library
|
|
|
|
|
2007-04-22 21:01:48 +00:00
|
|
|
require_once '../library/HTMLPurifier.auto.php';
|
2006-09-10 02:02:40 +00:00
|
|
|
|
|
|
|
|
2006-09-13 00:59:20 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Setup convenience functions
|
|
|
|
|
|
|
|
function appendHTMLDiv($document, $node, $html) {
|
|
|
|
global $purifier;
|
|
|
|
$html = $purifier->purify($html);
|
|
|
|
$dom_html = $document->createDocumentFragment();
|
|
|
|
$dom_html->appendXML($html);
|
|
|
|
|
|
|
|
$dom_div = $document->createElement('div');
|
|
|
|
$dom_div->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
|
|
|
|
$dom_div->appendChild($dom_html);
|
|
|
|
|
|
|
|
$node->appendChild($dom_div);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-11 02:52:47 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Load copies of HTMLPurifier_ConfigDef and HTMLPurifier
|
|
|
|
|
2006-09-24 02:06:12 +00:00
|
|
|
$schema = HTMLPurifier_ConfigSchema::instance();
|
2006-09-10 02:02:40 +00:00
|
|
|
$purifier = new HTMLPurifier();
|
|
|
|
|
2006-09-11 02:52:47 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Generate types.xml, a document describing the constraint "type"
|
|
|
|
|
2006-09-10 22:17:22 +00:00
|
|
|
$types_document = new DOMDocument('1.0', 'UTF-8');
|
|
|
|
$types_root = $types_document->createElement('types');
|
|
|
|
$types_document->appendChild($types_root);
|
|
|
|
$types_document->formatOutput = true;
|
2006-09-24 02:06:12 +00:00
|
|
|
foreach ($schema->types as $name => $expanded_name) {
|
2006-09-10 22:17:22 +00:00
|
|
|
$types_type = $types_document->createElement('type', $expanded_name);
|
|
|
|
$types_type->setAttribute('id', $name);
|
|
|
|
$types_root->appendChild($types_type);
|
|
|
|
}
|
|
|
|
$types_document->save('types.xml');
|
|
|
|
|
2006-09-11 02:52:47 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Generate configdoc.xml, a document documenting configuration directives
|
2006-09-10 22:17:22 +00:00
|
|
|
|
2006-09-10 02:02:40 +00:00
|
|
|
$dom_document = new DOMDocument('1.0', 'UTF-8');
|
|
|
|
$dom_root = $dom_document->createElement('configdoc');
|
|
|
|
$dom_document->appendChild($dom_root);
|
|
|
|
$dom_document->formatOutput = true;
|
|
|
|
|
|
|
|
// add the name of the application
|
|
|
|
$dom_root->appendChild($dom_document->createElement('title', 'HTML Purifier'));
|
|
|
|
|
|
|
|
/*
|
2006-09-10 20:21:19 +00:00
|
|
|
TODO for XML format:
|
|
|
|
- create a definition (DTD or other) once interface stabilizes
|
2006-09-10 02:02:40 +00:00
|
|
|
*/
|
|
|
|
|
2006-09-24 02:06:12 +00:00
|
|
|
foreach($schema->info as $namespace_name => $namespace_info) {
|
2006-09-10 02:02:40 +00:00
|
|
|
|
|
|
|
$dom_namespace = $dom_document->createElement('namespace');
|
|
|
|
$dom_root->appendChild($dom_namespace);
|
|
|
|
|
|
|
|
$dom_namespace->setAttribute('id', $namespace_name);
|
|
|
|
$dom_namespace->appendChild(
|
|
|
|
$dom_document->createElement('name', $namespace_name)
|
|
|
|
);
|
2006-09-13 00:59:20 +00:00
|
|
|
$dom_namespace_description = $dom_document->createElement('description');
|
|
|
|
$dom_namespace->appendChild($dom_namespace_description);
|
|
|
|
appendHTMLDiv($dom_document, $dom_namespace_description,
|
2006-09-24 02:06:12 +00:00
|
|
|
$schema->info_namespace[$namespace_name]->description);
|
2006-09-10 02:02:40 +00:00
|
|
|
|
|
|
|
foreach ($namespace_info as $name => $info) {
|
|
|
|
|
2007-01-20 18:43:58 +00:00
|
|
|
if ($info->class == 'alias') continue;
|
|
|
|
|
2006-09-10 02:02:40 +00:00
|
|
|
$dom_directive = $dom_document->createElement('directive');
|
|
|
|
$dom_namespace->appendChild($dom_directive);
|
|
|
|
|
|
|
|
$dom_directive->setAttribute('id', $namespace_name . '.' . $name);
|
|
|
|
$dom_directive->appendChild(
|
|
|
|
$dom_document->createElement('name', $name)
|
|
|
|
);
|
2006-09-10 22:17:22 +00:00
|
|
|
|
|
|
|
$dom_constraints = $dom_document->createElement('constraints');
|
|
|
|
$dom_directive->appendChild($dom_constraints);
|
2006-09-13 00:59:20 +00:00
|
|
|
|
2006-11-12 02:59:36 +00:00
|
|
|
$dom_type = $dom_document->createElement('type', $info->type);
|
|
|
|
if ($info->allow_null) {
|
|
|
|
$dom_type->setAttribute('allow-null', 'yes');
|
|
|
|
}
|
|
|
|
$dom_constraints->appendChild($dom_type);
|
|
|
|
|
2006-09-13 00:59:20 +00:00
|
|
|
if ($info->allowed !== true) {
|
|
|
|
$dom_allowed = $dom_document->createElement('allowed');
|
|
|
|
$dom_constraints->appendChild($dom_allowed);
|
|
|
|
foreach ($info->allowed as $allowed => $bool) {
|
|
|
|
$dom_allowed->appendChild(
|
|
|
|
$dom_document->createElement('value', $allowed)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2006-09-10 02:02:40 +00:00
|
|
|
|
2006-09-24 02:06:12 +00:00
|
|
|
$raw_default = $schema->defaults[$namespace_name][$name];
|
2006-09-16 01:53:13 +00:00
|
|
|
if (is_bool($raw_default)) {
|
|
|
|
$default = $raw_default ? 'true' : 'false';
|
|
|
|
} elseif (is_string($raw_default)) {
|
|
|
|
$default = "\"$raw_default\"";
|
2006-11-12 02:59:36 +00:00
|
|
|
} elseif (is_null($raw_default)) {
|
|
|
|
$default = 'null';
|
2006-09-16 01:53:13 +00:00
|
|
|
} else {
|
|
|
|
$default = print_r(
|
2006-09-24 02:06:12 +00:00
|
|
|
$schema->defaults[$namespace_name][$name], true
|
2006-09-16 01:53:13 +00:00
|
|
|
);
|
|
|
|
}
|
2006-11-12 00:05:27 +00:00
|
|
|
|
|
|
|
$dom_default = $dom_document->createElement('default', $default);
|
|
|
|
|
|
|
|
// remove this once we get a DTD
|
|
|
|
$dom_default->setAttribute('xml:space', 'preserve');
|
|
|
|
|
|
|
|
$dom_constraints->appendChild($dom_default);
|
2006-09-16 01:53:13 +00:00
|
|
|
|
2006-09-10 02:02:40 +00:00
|
|
|
$dom_descriptions = $dom_document->createElement('descriptions');
|
|
|
|
$dom_directive->appendChild($dom_descriptions);
|
|
|
|
|
|
|
|
foreach ($info->descriptions as $file => $file_descriptions) {
|
|
|
|
foreach ($file_descriptions as $line => $description) {
|
|
|
|
$dom_description = $dom_document->createElement('description');
|
|
|
|
$dom_description->setAttribute('file', $file);
|
|
|
|
$dom_description->setAttribute('line', $line);
|
2006-09-13 00:59:20 +00:00
|
|
|
appendHTMLDiv($dom_document, $dom_description, $description);
|
2006-09-10 02:02:40 +00:00
|
|
|
$dom_descriptions->appendChild($dom_description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// print_r($dom_document->saveXML());
|
|
|
|
|
2006-09-10 20:16:19 +00:00
|
|
|
// save a copy of the raw XML
|
|
|
|
$dom_document->save('configdoc.xml');
|
2006-09-10 02:02:40 +00:00
|
|
|
|
2006-09-11 02:52:47 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Generate final output using XSLT
|
|
|
|
|
2006-09-10 02:02:40 +00:00
|
|
|
// load the stylesheet
|
2006-09-10 19:48:40 +00:00
|
|
|
$xsl_stylesheet_name = 'plain';
|
2006-09-10 02:02:40 +00:00
|
|
|
$xsl_stylesheet = "styles/$xsl_stylesheet_name.xsl";
|
|
|
|
$xsl_dom_stylesheet = new DOMDocument();
|
|
|
|
$xsl_dom_stylesheet->load($xsl_stylesheet);
|
|
|
|
|
|
|
|
// setup the XSLT processor
|
|
|
|
$xsl_processor = new XSLTProcessor();
|
|
|
|
|
|
|
|
// perform the transformation
|
|
|
|
$xsl_processor->importStylesheet($xsl_dom_stylesheet);
|
|
|
|
$html_output = $xsl_processor->transformToXML($dom_document);
|
|
|
|
|
|
|
|
// some slight fudges to preserve backwards compatibility
|
2007-01-30 00:34:23 +00:00
|
|
|
$html_output = str_replace('/>', ' />', $html_output); // <br /> not <br/>
|
2006-09-10 22:17:22 +00:00
|
|
|
$html_output = str_replace(' xmlns=""', '', $html_output); // rm unnecessary xmlns
|
|
|
|
|
|
|
|
if (class_exists('Tidy')) {
|
|
|
|
// cleanup output
|
|
|
|
$config = array(
|
|
|
|
'indent' => true,
|
|
|
|
'output-xhtml' => true,
|
2006-09-10 23:02:36 +00:00
|
|
|
'wrap' => 80
|
2006-09-10 22:17:22 +00:00
|
|
|
);
|
|
|
|
$tidy = new Tidy;
|
|
|
|
$tidy->parseString($html_output, $config, 'utf8');
|
|
|
|
$tidy->cleanRepair();
|
|
|
|
$html_output = (string) $tidy;
|
|
|
|
}
|
2006-09-10 02:02:40 +00:00
|
|
|
|
|
|
|
// write it to a file (todo: parse into seperate pages)
|
2006-09-10 20:16:19 +00:00
|
|
|
file_put_contents("$xsl_stylesheet_name.html", $html_output);
|
2006-09-10 02:02:40 +00:00
|
|
|
|
2006-09-11 02:52:47 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Output for instant feedback
|
|
|
|
|
2006-09-10 22:47:27 +00:00
|
|
|
if (php_sapi_name() != 'cli') {
|
|
|
|
echo $html_output;
|
|
|
|
} else {
|
2006-09-10 22:53:40 +00:00
|
|
|
echo 'Files generated successfully.';
|
2006-09-10 22:47:27 +00:00
|
|
|
}
|
2006-09-10 02:02:40 +00:00
|
|
|
|
|
|
|
?>
|