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