Adding 3rd-party plugin disclaimer.
[htmlpurifier-web.git] / demo.php
blob0ce0d663fed4314c49d6a789b398b3b8400f4597
1 <?php
3 // major TODO: hook into some sort of templating system that utilizes
4 // XHTML Compiler to process the template (cacheable, of course), before
5 // passing it along to this script
7 // using _REQUEST because we accept GET and POST requests
9 function getFormMethod() {
10 return (isset($_REQUEST['post'])) ? 'post' : 'get';
12 function escapeHTML($html) {
13 return htmlspecialchars(
14 HTMLPurifier_Encoder::cleanUTF8($html), ENT_COMPAT, 'UTF-8');
16 function isLocal() {
17 return file_exists('xhtml-compiler/local.txt');
20 if (!isLocal()) {
21 require_once 'live/library/HTMLPurifier.auto.php';
22 } else {
23 // local dev
24 require_once '../htmlpurifier/library/HTMLPurifier.auto.php';
26 require_once 'HTMLPurifier/Printer/ConfigForm.php';
28 $allowed_lite = array(
29 'Core.CollectErrors',
30 'URI.DisableExternalResources',
31 'URI.Munge',
32 'HTML.TidyLevel',
33 'HTML.Doctype',
34 'HTML.Allowed',
35 'CSS',
36 'AutoFormat',
37 '-AutoFormat.Custom',
38 '-AutoFormat.PurifierLinkify',
41 $config = HTMLPurifier_Config::loadArrayFromForm($_REQUEST, 'filter', $allowed_lite);
43 if (!empty($_REQUEST['strict'])) {
44 // backwards-compatibility
45 $config->set('HTML', 'Strict', true);
48 $definition = $config->getHTMLDefinition();
49 $doctype = $definition->doctype;
51 if ($doctype->xml && stripos($_SERVER["HTTP_ACCEPT"], 'application/xhtml+xml') !== false && !isset($_REQUEST['debug'])) {
52 $type = 'application/xhtml+xml';
53 } else {
54 $type = 'text/html';
56 header("Content-type:$type;charset=UTF-8");
58 // prevent PHP versions with shorttags from barfing
59 if ($doctype->xml) {
60 echo '<?xml version="1.0" encoding="UTF-8" ?>' . PHP_EOL;
63 if (!empty($doctype->dtdPublic) && !empty($doctype->dtdSystem)) {
64 echo '<!DOCTYPE html PUBLIC "'.$doctype->dtdPublic.'" "'.$doctype->dtdSystem.'">' . PHP_EOL;
67 if ($doctype->xml) {
68 echo '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">' . PHP_EOL;
69 $END = ' /';
70 } else {
71 echo '<html lang="en">' . PHP_EOL;
72 $END = '';
75 ?><head>
76 <title>HTML Purifier Live Demo</title>
77 <!-- make sure all empty elements that are not generated are
78 appropriately ended -->
79 <meta name="author" content="Edward Z. Yang"<?php echo $END; ?>>
80 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"<?php echo $END; ?>>
81 <link rel="icon" href="favicon.ico" type="image/x-icon"<?php echo $END; ?>>
82 <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"<?php echo $END; ?>>
83 <link rel="stylesheet" href="common.css" type="text/css"<?php echo $END; ?>>
84 <link rel="stylesheet" href="demo.css" type="text/css"<?php echo $END; ?>>
85 <link rel="stylesheet" href="live/library/HTMLPurifier/Printer/ConfigForm.css" type="text/css"<?php echo $END; ?>>
86 <script defer="defer" type="text/javascript" src="live/library/HTMLPurifier/Printer/ConfigForm.js"></script>
87 </head>
88 <body>
89 <div id="logo"></div>
90 <div id="header"><a href=".">HTML Purifier</a></div>
91 <?php readfile('navigation.frag'); ?>
92 <h1 id="title">Live Demo</h1>
93 <div id="alt-content">
94 <?php
96 if (!empty($_REQUEST['html'])) { // start result
98 if (strlen($_REQUEST['html']) > 50000) {
100 <p>Request exceeds maximum allowed text size of 50kb.</p>
101 <?php
102 } else { // start main processing
104 $html = get_magic_quotes_gpc() ? stripslashes($_REQUEST['html']) : $_REQUEST['html'];
105 $purifier = new HTMLPurifier($config);
106 $pure_html = $purifier->purify($html);
109 <p>Here is your purified HTML:</p>
110 <div id="output">
111 <?php if(getFormMethod() == 'get') {
112 // calculate image
113 $img = false;
114 if ($doctype->name == 'HTML 4.01 Transitional' || $doctype->name == 'HTML 4.01 Strict') {
115 $img = 'http://www.w3.org/Icons/valid-html401';
116 } elseif ($doctype->name == 'XHTML 1.0 Transitional' || $doctype->name == 'XHTML 1.0 Strict') {
117 $img = 'http://www.w3.org/Icons/valid-xhtml10';
118 } elseif ($doctype->name == 'XHTML 1.1') {
119 $img = 'http://www.w3.org/Icons/valid-xhtml11';
122 <div id="w3c-validator">
123 <a href="http://validator.w3.org/check?uri=referer" title="Valid <?php echo escapeHTML($doctype->name); ?>">
124 <?php if ($img) { ?>
125 <img
126 src="<?php echo escapeHTML($img); ?>" height="31" width="88"
127 alt="Valid <?php echo escapeHTML($doctype->name); ?>"<?php echo $END; ?>>
128 <?php } else { ?>
129 Valid <?php echo escapeHTML($doctype->name); ?>
130 <?php } ?>
131 </a>
132 </div>
133 <?php } ?>
134 <?php
136 echo $pure_html;
139 <div class="clear"></div>
140 </div>
141 <?php if ($config->get('Core', 'CollectErrors')) {
142 $e = $purifier->context->get('ErrorCollector');
143 $class = $e->getRaw() ? 'fail' : 'pass';
145 <p>Here are errors that occurred during purification:</p>
146 <div id="errors" class="<?php echo $class ?>">
147 <?php
148 echo $e->getHTMLFormatted($config);
150 </div>
151 <?php } ?>
152 <p>Here is the source code of the purified HTML:</p>
153 <pre id="source"><?php echo escapeHTML($pure_html); ?></pre>
154 <?php
155 if (getFormMethod() == 'post') { // start POST validation notice
157 <p>If you would like to validate the code with
158 <a href="http://validator.w3.org/#validate-by-input">W3C's
159 validator</a>, copy and paste the <em>entire</em> demo page's source.</p>
160 <?php
161 } // end POST validation notice
163 } // end main processing
165 // end result
166 } else {
169 <p>Welcome to the live demo. Enter some HTML and see how HTML Purifier
170 will filter it.</p>
171 <?php
176 <form id="filter" action="demo.php<?php
177 echo '?' . getFormMethod();
178 if (isset($_REQUEST['profile']) || isset($_REQUEST['XDEBUG_PROFILE'])) {
179 echo '&amp;XDEBUG_PROFILE=1';
180 } ?>" method="<?php echo getFormMethod(); ?>">
181 <fieldset>
182 <legend>HTML Purifier Input (<?php echo getFormMethod(); ?>)</legend>
184 <?php
185 $form_printer = new HTMLPurifier_Printer_ConfigForm('filter', 'http://htmlpurifier.org/live/configdoc/plain.html#%s', 14);
186 echo $form_printer->render($config, $allowed_lite, false);
189 <div id="textarea">
190 <textarea name="html" cols="60" rows="15"><?php
191 if (isset($html)) echo escapeHTML($html);
192 ?></textarea>
193 </div>
195 <?php if (getFormMethod() == 'get') { ?>
196 <p><strong>Warning:</strong> GET request method can only hold
197 8129 characters (probably less depending on your browser).
198 If you need to test anything
199 larger than that, try the <a href="?post">POST form</a>.</p>
200 <?php } ?>
201 <div>
202 <input type="submit" value="Submit" name="submit" class="button"<?php echo $END; ?>>
203 </div>
204 </fieldset>
205 </form>
206 <p>Try the form in <a href="?get">GET</a> and <a href="?post">POST</a> request
207 flavors (GET is easy to validate with W3C, but POST allows larger inputs).
208 Don't know what to test? Try out these sample filterings:</p>
209 <ul>
210 <li><a href="demo.php?html=%3Cimg+src%3D%22javascript%3Aevil%28%29%3B%22+onload%3D%22evil%28%29%3B%22+%2F%3E">Malicious code removed</a></li>
211 <li><a href="demo.php?html=%3Cb%3EBold&amp;submit=Submit">Missing end tags fixed</a></li>
212 <li><a href="demo.php?html=%3Cb%3EInline+%3Cdel%3Econtext+%3Cdiv%3ENo+block+allowed%3C%2Fdiv%3E%3C%2Fdel%3E%3C%2Fb%3E&amp;submit=Submit">Illegal nesting fixed</a></li>
213 <li><a href="demo.php?html=%3Ccenter%3ECentered%3C%2Fcenter%3E&amp;filter%5BHTML.Doctype%5D=XHTML+1.0+Strict&amp;submit=Submit">Deprecated tags converted</a></li>
214 <li><a href="demo.php?html=%3Cspan+style%3D%22color%3A%23COW%3Bfloat%3Aaround%3Btext-decoration%3Ablink%3B%22%3EText%3C%2Fspan%3E&amp;submit=Submit"><abbr>CSS</abbr> validated</a></li>
215 <li><a href="demo.php?html=%3Ctable%3E%0D%0A++%3Ccaption%3E%0D%0A++++Cool+table%0D%0A++%3C%2Fcaption%3E%0D%0A++%3Ctfoot%3E%0D%0A++++%3Ctr%3E%0D%0A++++++%3Cth%3EI+can+do+so+much%21%3C%2Fth%3E%0D%0A++++%3C%2Ftr%3E%0D%0A++%3C%2Ftfoot%3E%0D%0A++%3Ctr%3E%0D%0A++++%3Ctd+style%3D%22font-size%3A16pt%3B%0D%0A++++++color%3A%23F00%3Bfont-family%3Asans-serif%3B%0D%0A++++++text-align%3Acenter%3B%22%3EWow%3C%2Ftd%3E%0D%0A++%3C%2Ftr%3E%0D%0A%3C%2Ftable%3E&amp;submit=Submit">Rich formatting preserved</a></li>
216 </ul>
217 </div>
218 </body>
219 </html>