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