Add URL shortening to demo, and some more options.
[htmlpurifier-web.git] / demo.php
bloba312de87981b9f9d9c742028f6ddb5c5f947e95f
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 static $name = false;
18 if (!$name) {
19 if (file_exists($dir = 'xhtml-compiler/conf/name.txt')) {
20 $name = trim(file_get_contents($dir));
21 } elseif (file_exists('xhtml-compiler/local.txt')) {
22 $name = 'EZYANG';
23 } elseif (file_exists('local.txt')) {
24 $name = 'EZYANG';
27 return $name == 'EZYANG' || $name == 'EZYANG2';
30 if (empty($_REQUEST['experimental'])) {
31 require_once 'live/library/HTMLPurifier.auto.php';
32 } elseif (!isLocal()) {
33 require_once 'dev/library/HTMLPurifier.auto.php';
34 } else {
35 require_once '../htmlpurifier/library/HTMLPurifier.auto.php';
37 require_once 'HTMLPurifier/Printer/ConfigForm.php';
39 $allowed_lite = array(
40 'Core.CollectErrors',
41 'URI.DisableExternalResources',
42 'URI.Munge',
43 'HTML.TidyLevel',
44 'HTML.Doctype',
45 'HTML.Allowed',
46 'HTML.SafeObject',
47 'HTML.FlashCompat',
48 'CSS.AllowedProperties',
49 'AutoFormat',
50 '-AutoFormat.Custom',
51 '-AutoFormat.PurifierLinkify',
54 $config = HTMLPurifier_Config::loadArrayFromForm($_REQUEST, 'filter', $allowed_lite);
55 $purifier = new HTMLPurifier($config);
57 if (!empty($_REQUEST['strict'])) {
58 // backwards-compatibility
59 // (muting deprecated error)
60 @$config->set('HTML', 'Strict', true);
62 if (!empty($_REQUEST['experimental'])) {
63 //require_once 'HTMLPurifier/Lexer/PH5P.php';
64 //$config->set('Core', 'LexerImpl', 'PH5P');
65 //$config->set('HTML.SafeObject', true);
66 //$config->set('HTML.FlashCompat', true);
69 if (file_exists('demo.custom.php') && isLocal()) include 'demo.custom.php';
71 $definition = $config->getHTMLDefinition();
72 $doctype = $definition->doctype;
74 if ($doctype->xml && stripos($_SERVER["HTTP_ACCEPT"], 'application/xhtml+xml') !== false && !isset($_REQUEST['debug'])) {
75 $type = 'application/xhtml+xml';
76 } else {
77 $type = 'text/html';
79 header("Content-type:$type;charset=UTF-8");
81 // prevent PHP versions with shorttags from barfing
82 if ($doctype->xml) {
83 echo '<?xml version="1.0" encoding="UTF-8" ?>' . PHP_EOL;
86 if (!empty($doctype->dtdPublic) && !empty($doctype->dtdSystem)) {
87 echo '<!DOCTYPE html PUBLIC "'.$doctype->dtdPublic.'" "'.$doctype->dtdSystem.'">' . PHP_EOL;
90 if ($doctype->xml) {
91 echo '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">' . PHP_EOL;
92 $END = ' /';
93 } else {
94 echo '<html lang="en">' . PHP_EOL;
95 $END = '';
98 ?><head>
99 <title>HTML Purifier Live Demo</title>
100 <!-- make sure all empty elements that are not generated are
101 appropriately ended -->
102 <meta name="author" content="Edward Z. Yang"<?php echo $END; ?>>
103 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"<?php echo $END; ?>>
104 <link rel="icon" href="favicon.ico" type="image/x-icon"<?php echo $END; ?>>
105 <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"<?php echo $END; ?>>
106 <link rel="stylesheet" href="common.css" type="text/css"<?php echo $END; ?>>
107 <link rel="stylesheet" href="demo.css" type="text/css"<?php echo $END; ?>>
108 <link rel="stylesheet" href="live/library/HTMLPurifier/Printer/ConfigForm.css" type="text/css"<?php echo $END; ?>>
109 <script defer="defer" type="text/javascript" src="live/library/HTMLPurifier/Printer/ConfigForm.js"></script>
110 </head>
111 <body>
112 <div id="logo"></div>
113 <div id="header"><a href=".">HTML Purifier</a></div>
114 <?php
115 if (file_exists('navigation.frag')) {
116 readfile('navigation.frag');
117 } else { ?>
118 <div><strong>Please navigate to <a href="navigation.html">navigation.html</a> to regenerate menu</strong></div>
119 <?php }
121 <div id="main">
122 <h1 id="title">Live Demo</h1>
123 <div id="content">
124 <?php
126 if (!empty($_REQUEST['html'])) { // start result
128 if (strlen($_REQUEST['html']) > 50000) {
130 <p>Request exceeds maximum allowed text size of 50kb.</p>
131 <?php
132 } else { // start main processing
134 $html = get_magic_quotes_gpc() ? stripslashes($_REQUEST['html']) : $_REQUEST['html'];
135 $pure_html = $purifier->purify($html);
138 <p>Here is your purified HTML:</p>
139 <div id="output">
140 <?php if(getFormMethod() == 'get') {
141 // calculate image
142 $img = false;
143 if ($doctype->name == 'HTML 4.01 Transitional' || $doctype->name == 'HTML 4.01 Strict') {
144 $img = 'http://www.w3.org/Icons/valid-html401';
145 } elseif ($doctype->name == 'XHTML 1.0 Transitional' || $doctype->name == 'XHTML 1.0 Strict') {
146 $img = 'http://www.w3.org/Icons/valid-xhtml10';
147 } elseif ($doctype->name == 'XHTML 1.1') {
148 $img = 'http://www.w3.org/Icons/valid-xhtml11';
151 <div id="w3c-validator">
152 <a href="http://validator.w3.org/check?uri=referer" title="Valid <?php echo escapeHTML($doctype->name); ?>">
153 <?php if ($img) { ?>
154 <img
155 src="<?php echo escapeHTML($img); ?>" height="31" width="88"
156 alt="Valid <?php echo escapeHTML($doctype->name); ?>"<?php echo $END; ?>>
157 <?php } else { ?>
158 Valid <?php echo escapeHTML($doctype->name); ?>
159 <?php } ?>
160 </a>
161 </div>
162 <?php } ?>
163 <?php
165 echo $pure_html;
168 <div class="clear"></div>
169 </div>
170 <?php if (@$config->get('Core', 'CollectErrors')) {
171 $e = $purifier->context->get('ErrorCollector');
172 $class = $e->getRaw() ? 'fail' : 'pass';
174 <p>Here are errors that occurred during purification:</p>
175 <div id="errors" class="<?php echo $class ?>">
176 <?php
177 echo $e->getHTMLFormatted($config);
179 </div>
180 <?php } ?>
181 <p>Here is the source code of the purified HTML:</p>
182 <pre id="source"><?php echo escapeHTML($pure_html); ?></pre>
183 <?php
184 if (getFormMethod() == 'post') { // start POST validation notice
186 <p>If you would like to validate the code with
187 <a href="http://validator.w3.org/#validate-by-input">W3C's
188 validator</a>, copy and paste the <em>entire</em> demo page's source.</p>
189 <?php
190 } // end POST validation notice
193 <p>Share this purification using the <a href="javascript:var%20e=document.createElement('script');e.setAttribute('language','javascript');e.setAttribute('src','http://bit.ly/bookmarklet/load.js');document.body.appendChild(e);void(0);">bit.ly URL shortener</a>.</p>
194 <?php
196 } // end main processing
198 // end result
199 } else {
202 <p>Welcome to the live demo. Enter some HTML and see how HTML Purifier
203 will filter it.</p>
204 <?php
209 <form id="filter" action="demo.php<?php
210 echo '?' . getFormMethod();
211 if (isset($_REQUEST['profile']) || isset($_REQUEST['XDEBUG_PROFILE'])) {
212 echo '&amp;XDEBUG_PROFILE=1';
213 } ?>" method="<?php echo getFormMethod(); ?>">
214 <fieldset>
215 <legend>HTML Purifier Input (<?php echo getFormMethod(); ?>)</legend>
217 <?php
218 $form_printer = new HTMLPurifier_Printer_ConfigForm('filter', 'http://htmlpurifier.org/live/configdoc/plain.html#%s', 14);
219 echo $form_printer->render($config, $allowed_lite, false);
222 <div id="textarea">
223 <textarea name="html" cols="60" rows="15"><?php
224 if (isset($html)) echo escapeHTML($html);
225 ?></textarea>
226 </div>
228 <p class="lead">
229 By default, HTML Purifier may remove some of your spacing or
230 indentation. Turn on CollectErrors or experimental features in
231 order to fully preserve whitespace.
232 </p>
233 <?php if (getFormMethod() == 'get') { ?>
234 <p><strong>Warning:</strong> GET request method can only hold
235 8129 characters (probably less depending on your browser).
236 If you need to test anything
237 larger than that, try the <a href="?post">POST form</a>.</p>
238 <?php } ?>
239 <div id="controls">
240 <input type="submit" value="Submit" name="submit" class="button"<?php echo $END; ?>>
241 Use <abbr class="elaborates" title="This runs code from the master Git repository branch.">experimental features</abbr>: <input type="checkbox" value="1" <?php if(!empty($_REQUEST['experimental'])) {echo 'checked="checked" ';} ?>name="experimental"<?php echo $END; ?>>
242 </div>
243 </fieldset>
244 </form>
245 <p class="lead">Try the form in <a href="?get">GET</a> and <a href="?post">POST</a> request
246 flavors (GET is easy to validate with W3C, but POST allows larger inputs).
247 Don't know what to test? Try out these sample filterings:</p>
248 <ul>
249 <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>
250 <li><a href="demo.php?html=%3Cb%3EBold&amp;submit=Submit">Missing end tags fixed</a></li>
251 <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>
252 <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>
253 <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>
254 <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;experimental=1&amp;submit=Submit">Rich formatting preserved</a></li>
255 </ul>
256 </div>
257 </div>
258 </body>
259 </html>