]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RawHtml.php
No underscore for private function
[SourceForge/phpwiki.git] / lib / plugin / RawHtml.php
1 <?php
2
3 /**
4  * Copyright 1999,2000,2001,2002,2004 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 // Moved to IniConfig and config-default.ini
24 // Define ENABLE_RAW_HTML to false (in config.ini) to disable the RawHtml
25 // plugin completely
26 /*
27 if (!defined('ENABLE_RAW_HTML'))
28     define('ENABLE_RAW_HTML', true);
29 // must be locked
30 if (!defined('ENABLE_RAW_HTML_LOCKEDONLY'))
31     define('ENABLE_RAW_HTML_LOCKEDONLY', true);
32 // sanitize to safe html code
33 if (!defined('ENABLE_RAW_HTML_SAFE'))
34     define('ENABLE_RAW_HTML_SAFE', true);
35 */
36
37 /** We defined a better policy when to allow RawHtml:
38  *   ENABLE_RAW_HTML_LOCKEDONLY:
39  *  - Allowed if page is locked by ADMIN_USER.
40  *   ENABLE_RAW_HTML_SAFE:
41  *  - Allow some sort of "safe" html tags and attributes.
42  *    Unsafe attributes are automatically stripped. (Experimental!)
43  *    See http://phpwiki.sourceforge.net/phpwiki/allowing%20safe%20HTML
44  */
45
46 /**
47  * Provide for raw HTML within wiki pages.
48  */
49 class WikiPlugin_RawHtml
50     extends WikiPlugin
51 {
52     function getName()
53     {
54         return "RawHtml";
55     }
56
57     function getDescription()
58     {
59         return _("Provide for raw HTML within wiki pages.");
60     }
61
62     function getDefaultArguments()
63     {
64         return array();
65     }
66
67     function managesValidators()
68     {
69         // The plugin output will only change if the plugin
70         // invocation (page text) changes --- so the necessary
71         // validators have already been handled by displayPage.
72         return true;
73     }
74
75     function run($dbi, $argstr, &$request, $basepage)
76     {
77         if (!defined('ENABLE_RAW_HTML') || !ENABLE_RAW_HTML) {
78             return $this->disabled(_("Raw HTML is disabled in this wiki."));
79         }
80         if (!$basepage) {
81             return $this->error("$basepage unset?");
82         }
83
84         $page = $request->getPage($basepage);
85         if (ENABLE_RAW_HTML_LOCKEDONLY) {
86             if (!$page->get('locked')) {
87                 return $this->disabled(fmt("%s is only allowed in locked pages.",
88                     _("Raw HTML")));
89             }
90         }
91         if (ENABLE_RAW_HTML_SAFE) {
92             // check for javascript handlers (on*) and style tags with external urls. no javascript urls.
93             // See also http://simon.incutio.com/archive/2003/02/23/safeHtmlChecker
94             // But we should allow not only code semantic meaning,  presentational markup also.
95
96             // http://chxo.com/scripts/safe_html-test.php looks better
97             $argstr = $this->safe_html($argstr);
98             /*return $this->disabled(HTML(fmt("This %s plugin on %s is disabled because of unsafe HTML code. ",$this->getName(), $basepage),
99                                         fmt("See PhpWiki:allowing%20safe%20HTML")
100                                         ));
101             */
102         }
103
104         return HTML::raw($argstr);
105     }
106
107     // From http://chxo.com/scripts/safe_html-test.php
108     // safe_html by Chris Snyder (csnyder@chxo.com) for http://pcoms.net
109     //   - Huge thanks to James Wetterau for testing and feedback!
110
111     /*
112     Copyright 2003 Chris Snyder. All rights reserved.
113
114     Redistribution and use in source and binary forms, with or without modification,
115     are permitted provided that the following conditions are met:
116
117        1. Redistributions of source code must retain the above copyright
118        notice, this list of conditions and the following disclaimer.
119
120        2. Redistributions in binary form must reproduce the above
121        copyright notice, this list of conditions and the following
122        disclaimer in the documentation and/or other materials provided
123        with the distribution.
124
125     THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
126     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
127     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
128     AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
129     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
130     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE, DATA, OR PROFITS;
131     OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
132     WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
133     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
134     ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135     */
136
137     /*
138      set of functions for sanitizing user input:
139         keeps "friendly" tags but strips javascript events and style attributes
140         closes any open comment tags
141         closes any open HTML tags - results may not be valid HTML, but
142             at least they will keep the rest of the page from breaking
143
144         treats the following as malicious conditions and returns text stripped
145         of all html tags:
146             any instances of ='javascript:
147             event or style attributes remaining after initial replacement
148     */
149
150     function strip_attributes($html, $attrs)
151     {
152         if (!is_array($attrs)) {
153             $array = array("$attrs");
154             unset($attrs);
155             $attrs = $array;
156         }
157
158         foreach ($attrs AS $attribute) {
159             // once for ", once for ', s makes the dot match linebreaks, too.
160             $search[] = "/" . $attribute . '\s*=\s*".+"/Uis';
161             $search[] = "/" . $attribute . "\s*=\s*'.+'/Uis";
162             // and once more for unquoted attributes
163             $search[] = "/" . $attribute . "\s*=\s*\S+/i";
164         }
165         $html = preg_replace($search, "", $html);
166
167         // check for additional matches and strip all tags if found
168         foreach ($search AS $pattern) {
169             if (preg_match($pattern, $html)) {
170                 $html = strip_tags($html);
171                 break;
172             }
173         }
174
175         return $html;
176     }
177
178     function safe_html($html, $allowedtags = "")
179     {
180         $version = "safe_html.php/0.4";
181
182         // anything with ="javascript: is right out -- strip all tags and return if found
183         $pattern = "/=\s*\S+script:\S+/Ui";
184         if (preg_match($pattern, $html)) {
185             $html = strip_tags($html);
186             return $html;
187         }
188
189         // setup -- $allowedtags is an array of $tag=>$closeit pairs, where $tag is an HTML tag to allow and $closeit is 1 if the tag requires a matching, closing tag
190         if ($allowedtags == "") {
191             $allowedtags = array("p" => 1, "br" => 0, "a" => 1, "img" => 0, "li" => 1,
192                 "ol" => 1, "ul" => 1, "b" => 1, "i" => 1, "em" => 1, "strong" => 1, "del" => 1, "ins" => 1,
193                 "sub" => 1, "sup" => 1, "u" => 1, "blockquote" => 1, "pre" => 1, "hr" => 0,
194                 "table" => 1, "thead" => 1, "tfoot" => 1, "tbody" => 1, "tr" => 1, "td" => 1, "th" => 1,
195             );
196         } elseif (!is_array($allowedtags)) {
197             $array = array("$allowedtags");
198             unset($allowedtags);
199             $allowedtags = $array;
200         }
201
202         // there's some debate about this.. is strip_tags() better than rolling your own regex?
203         // note: a bug in PHP 4.3.1 caused improper handling of ! in tag attributes when using strip_tags()
204         $stripallowed = "";
205         foreach ($allowedtags AS $tag => $closeit) {
206             $stripallowed .= "<$tag>";
207         }
208
209         //print "Stripallowed: $stripallowed -- ".print_r($allowedtags,1);
210         $html = strip_tags($html, $stripallowed);
211
212         // also, lets get rid of some pesky attributes that may be set on the remaining tags...
213         $badattrs = array("on\w+", "style");
214         $html = $this->strip_attributes($html, $badattrs);
215
216         // close html tags if necessary -- note that this WON'T be graceful formatting-wise, it just has to fix any maliciousness
217         foreach ($allowedtags AS $tag => $closeit) {
218             if (!$closeit) continue;
219             $patternopen = "/<$tag\b[^>]*>/Ui";
220             $patternclose = "/<\/$tag\b[^>]*>/Ui";
221             $totalopen = preg_match_all($patternopen, $html, $matches);
222             $totalclose = preg_match_all($patternclose, $html, $matches2);
223             if ($totalopen > $totalclose) {
224                 $html .= str_repeat("</$tag>", ($totalopen - $totalclose));
225             }
226         }
227
228         // close any open <!--'s and identify version just in case
229         $html .= "<!-- $version -->";
230         return $html;
231     }
232 }
233
234 // Local Variables:
235 // mode: php
236 // tab-width: 8
237 // c-basic-offset: 4
238 // c-hanging-comment-ender-p: nil
239 // indent-tabs-mode: nil
240 // End: