]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpHighlight.php
don't display the SQL dsn connection password
[SourceForge/phpwiki.git] / lib / plugin / PhpHighlight.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PhpHighlight.php,v 1.7 2003-01-18 22:01:43 carstenklapp Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $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 /**
24  * A plugin that runs the highlight_string() function in PHP on it's
25  * arguments to pretty-print PHP code.
26  *
27  * Usage:
28  * <?plugin PhpHighlight default='#FF0000' comment='#0000CC'
29  * code that should be highlighted
30  * ?>
31  *
32  * You do not have to add '<?php' and '?>' to the code - the plugin
33  * does this automatically if you do not set wrap to 0.
34  *
35  * If you do set wrap to 0, then you'll have to start and stop PHP
36  * mode in the source yourself, or you wont see any highlighting. But
37  * you cannot use '<?php' and '?>' in the source, because this
38  * interferes with PhpWiki, you'll have use '< ?php' and '? >'
39  * instead.
40  *
41  * Author: Martin Geisler <gimpster@gimpster.com>.
42  *
43  * Added compatibility for PHP < 4.2.0, where the highlight_string()
44  * function has no second argument.
45  * Added ability to override colors defined in php.ini --Carsten Klapp
46  */
47
48 class WikiPlugin_PhpHighlight
49 extends WikiPlugin
50 {
51     // Four required functions in a WikiPlugin.
52
53     function getName () {
54         return _("PhpHighlight");
55     }
56
57     function getDescription () {
58         return _("PHP syntax highlighting");
59
60     }
61
62     function getVersion() {
63         return preg_replace("/[Revision: $]/", '',
64                             "\$Revision: 1.7 $");
65     }
66
67     // Establish default values for each of this plugin's arguments.
68     function getDefaultArguments() {
69         // TODO: results of ini_get() should be static for multiple
70         // invocations of plugin on one WikiPage
71         return array('source'  => false,
72                      'wrap'    => true,
73                      'string'  => ini_get("highlight.string"),  //'#00CC00',
74                      'comment' => ini_get("highlight.comment"), //'#FF9900',
75                      'keyword' => ini_get("highlight.keyword"), //'#006600',
76                      'bg'      => ini_get("highlight.bg"),      //'#FFFFFF',
77                      'default' => ini_get("highlight.default"), //'#0000CC',
78                      'html'    => ini_get("highlight.html")     //'#000000'
79                      );
80     }
81
82     function run($dbi, $argstr, $request) {
83
84         extract($this->getArgs($argstr, $request));
85
86         if (!function_exists('version_compare')
87             || version_compare(phpversion(), '4.2.0', 'lt')) {
88             // trigger_error(sprintf(_("%s requires PHP version %s or newer."),
89             //                      $this->getName(), "4.2.0"), E_USER_NOTICE);
90             /* return unhighlighted text as if <verbatim> were used */
91             // return HTML::pre($argstr); // early return
92             $has_old_php = true;
93         }
94
95         $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
96         $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
97
98         if ($wrap) {
99             /* Wrap with "<?php\n" and "\n?>" required by
100              * highlight_string(): */
101             $source = "<?php\n" . $source . "\n?>";
102         } else {
103             $source = str_replace(array('< ?php', '? >'),
104                                   array('<?php', '?>'), $source);
105         }
106
107         if (!empty($has_old_php)) {
108             ob_start();
109             highlight_string($source);
110             $str = ob_get_contents();
111             ob_end_clean();
112         } else {
113             $str = highlight_string($source, true);
114         }
115
116         if ($wrap)
117             /* Remove "<?php\n" and "\n?>" again: */
118             $str = str_replace(array('&lt;?php<br />', '?&gt;'), '', $str);
119
120         /**
121          * We might have made some empty font tags. (The following
122          * str_replace string does not produce results on my system,
123          * maybe a php bug? '<font color="$color"></font>')
124          */
125         foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
126             $search = "<font color=\"$color\"></font>";
127             $str = str_replace($search, '', $str);
128         }
129
130         /* restore default colors in case of multiple invocations of
131            this plugin on one page */
132         $this->restore_colors();
133         return new RawXml($str);
134     }
135
136     function handle_plugin_args_cruft(&$argstr, &$args) {
137         $args['source'] = $argstr;
138     }
139
140     /**
141      * Make sure color argument is valid
142      * See http://www.w3.org/TR/REC-html40/types.html#h-6.5
143      */
144     function sanify_colors($string, $comment, $keyword, $bg, $default, $html) {
145         static $html4colors = array("black", "silver", "gray", "white",
146                                     "maroon", "red", "purple", "fuchsia",
147                                     "green", "lime", "olive", "yellow",
148                                     "navy", "blue", "teal", "aqua");
149         /* max(strlen("fuchsia"), strlen("#00FF00"), ... ) = 7 */
150         static $MAXLEN = 7;
151         foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
152             $length = strlen($color);
153             //trigger_error(sprintf(_("DEBUG: color '%s' is length %d."), $color, $length), E_USER_NOTICE);
154             if (($length == 7 || $length == 4) && substr($color, 0, 1) == "#"
155             && "#" == preg_replace("/[a-fA-F0-9]/", "", $color)
156              ) {
157                 //trigger_error(sprintf(_("DEBUG: color '%s' appears to be hex."), $color), E_USER_NOTICE);
158                 // stop checking, ok to go
159             } elseif (($length < $MAXLEN + 1) && in_array($color, $html4colors)) {
160                 //trigger_error(sprintf(_("DEBUG color '%s' appears to be an HTML 4 color."), $color), E_USER_NOTICE);
161                 // stop checking, ok to go
162             } else {
163                 trigger_error(sprintf(_("Invalid color: %s"),
164                                       $color), E_USER_NOTICE);
165                 // FIXME: also change color to something valid like "black" or ini_get("highlight.xxx")
166             }
167         }
168     }
169
170     function set_colors($string, $comment, $keyword, $bg, $default, $html) {
171         // set highlight colors
172         $this->oldstring = ini_set('highlight.string', $string);
173         $this->oldcomment = ini_set('highlight.comment', $comment);
174         $this->oldkeyword = ini_set('highlight.keyword', $keyword);
175         $this->oldbg = ini_set('highlight.bg', $bg);
176         $this->olddefault = ini_set('highlight.default', $default);
177         $this->oldhtml = ini_set('highlight.html', $html);
178     }
179
180     function restore_colors() {
181         // restore previous default highlight colors
182         ini_set('highlight.string', $this->oldstring);
183         ini_set('highlight.comment', $this->oldcomment);
184         ini_set('highlight.keyword', $this->oldkeyword);
185         ini_set('highlight.bg', $this->oldbg);
186         ini_set('highlight.default', $this->olddefault);
187         ini_set('highlight.html', $this->oldhtml);
188     }
189
190 };
191
192 // $Log: not supported by cvs2svn $
193
194 // For emacs users
195 // Local Variables:
196 // mode: php
197 // tab-width: 8
198 // c-basic-offset: 4
199 // c-hanging-comment-ender-p: nil
200 // indent-tabs-mode: nil
201 // End:
202 ?>