1<?php
2/**
3 * BBCode plugin: allows BBCode markup familiar from forum software
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Esther Brunner <esther@kaffeehaus.ch>
7 * @author     Christopher Smith <chris@jalakai.co.uk>
8 * @author     Luis Machuca Bezzaza <luis.machuca@gulix.cl>
9 */
10
11if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class syntax_plugin_bbcode_color extends DokuWiki_Syntax_Plugin {
20
21    static $browsercolors = array (
22            'aliceblue' => '#f0f8ff' ,
23            'antiquewhite' => '#faebd7' ,
24            'aqua' => '#00ffff' ,
25            'aquamarine' => '#7fffd4' ,
26            'azure' => '#f0ffff' ,
27            'beige' => '#f5f5dc' ,
28            'bisque' => '#ffe4c4' ,
29            'black' => '#000000' ,
30            'blanchedalmond' => '#ffebcd' ,
31            'blue' => '#0000ff' ,
32            'blueviolet' => '#8a2be2' ,
33            'brown' => '#a52a2a' ,
34            'burlywood' => '#deb887' ,
35            'cadetblue' => '#5f9ea0' ,
36            'chartreuse' => '#7fff00' ,
37            'chocolate' => '#d2691e' ,
38            'coral' => '#ff7f50' ,
39            'cornflowerblue' => '#6495ed' ,
40            'cornsilk' => '#fff8dc' ,
41            'crimson' => '#dc143c' ,
42            'cyan' => '#00ffff' ,
43            'darkblue' => '#00008b' ,
44            'darkcyan' => '#008b8b' ,
45            'darkgoldenrod' => '#b8860b' ,
46            'darkgray' => '#a9a9a9' ,
47            'darkgreen' => '#006400' ,
48            'darkkhaki' => '#bdb76b' ,
49            'darkmagenta' => '#8b008b' ,
50            'darkolivegreen' => '#556b2f' ,
51            'darkorange' => '#ff8c00' ,
52            'darkorchid' => '#9932cc' ,
53            'darkred' => '#8b0000' ,
54            'darksalmon' => '#e9967a' ,
55            'darkseagreen' => '#8fbc8f' ,
56            'darkslateblue' => '#483d8b' ,
57            'darkslategray' => '#2f4f4f' ,
58            'darkturquoise' => '#00ced1' ,
59            'darkviolet' => '#9400d3' ,
60            'deeppink' => '#ff1493' ,
61            'deepskyblue' => '#00bfff' ,
62            'dimgray' => '#696969' ,
63            'dodgerblue' => '#1e90ff' ,
64            'firebrick' => '#b22222' ,
65            'floralwhite' => '#fffaf0' ,
66            'forestgreen' => '#228b22' ,
67            'fuchsia' => '#ff00ff' ,
68            'gainsboro' => '#dcdcdc' ,
69            'ghostwhite' => '#f8f8ff' ,
70            'gold' => '#ffd700' ,
71            'goldenrod' => '#daa520' ,
72            'gray' => '#808080' ,
73            'green' => '#008000' ,
74            'greenyellow' => '#adff2f' ,
75            'honeydew' => '#f0fff0' ,
76            'hotpink' => '#ff69b4' ,
77            'indianred' => '#cd5c5c' ,
78            'indigo' => '#4b0082' ,
79            'ivory' => '#fffff0' ,
80            'khaki' => '#f0e68c' ,
81            'lavender' => '#e6e6fa' ,
82            'lavenderblush' => '#fff0f5' ,
83            'lawngreen' => '#7cfc00' ,
84            'lemonchiffon' => '#fffacd' ,
85            'lightblue' => '#add8e6' ,
86            'lightcoral' => '#f08080' ,
87            'lightcyan' => '#e0ffff' ,
88            'lightgoldenrodyellow' => '#fafad2' ,
89            'lightgrey' => '#d3d3d3' ,
90            'lightgreen' => '#90ee90' ,
91            'lightpink' => '#ffb6c1' ,
92            'lightsalmon' => '#ffa07a' ,
93            'lightseagreen' => '#20b2aa' ,
94            'lightskyblue' => '#87cefa' ,
95            'lightslategray' => '#778899' ,
96            'lightsteelblue' => '#b0c4de' ,
97            'lightyellow' => '#ffffe0' ,
98            'lime' => '#00ff00' ,
99            'limegreen' => '#32cd32' ,
100            'linen' => '#faf0e6' ,
101            'magenta' => '#ff00ff' ,
102            'maroon' => '#800000' ,
103            'mediumaquamarine' => '#66cdaa' ,
104            'mediumblue' => '#0000cd' ,
105            'mediumorchid' => '#ba55d3' ,
106            'mediumpurple' => '#9370d8' ,
107            'mediumseagreen' => '#3cb371' ,
108            'mediumslateblue' => '#7b68ee' ,
109            'mediumspringgreen' => '#00fa9a' ,
110            'mediumturquoise' => '#48d1cc' ,
111            'mediumvioletred' => '#c71585' ,
112            'midnightblue' => '#191970' ,
113            'mintcream' => '#f5fffa' ,
114            'mistyrose' => '#ffe4e1' ,
115            'moccasin' => '#ffe4b5' ,
116            'navajowhite' => '#ffdead' ,
117            'navy' => '#000080' ,
118            'oldlace' => '#fdf5e6' ,
119            'olive' => '#808000' ,
120            'olivedrab' => '#6b8e23' ,
121            'orange' => '#ffa500' ,
122            'orangered' => '#ff4500' ,
123            'orchid' => '#da70d6' ,
124            'palegoldenrod' => '#eee8aa' ,
125            'palegreen' => '#98fb98' ,
126            'paleturquoise' => '#afeeee' ,
127            'palevioletred' => '#d87093' ,
128            'papayawhip' => '#ffefd5' ,
129            'peachpuff' => '#ffdab9' ,
130            'peru' => '#cd853f' ,
131            'pink' => '#ffc0cb' ,
132            'plum' => '#dda0dd' ,
133            'powderblue' => '#b0e0e6' ,
134            'purple' => '#800080' ,
135            'red' => '#ff0000' ,
136            'rosybrown' => '#bc8f8f' ,
137            'royalblue' => '#4169e1' ,
138            'saddlebrown' => '#8b4513' ,
139            'salmon' => '#fa8072' ,
140            'sandybrown' => '#f4a460' ,
141            'seagreen' => '#2e8b57' ,
142            'seashell' => '#fff5ee' ,
143            'sienna' => '#a0522d' ,
144            'silver' => '#c0c0c0' ,
145            'skyblue' => '#87ceeb' ,
146            'slateblue' => '#6a5acd' ,
147            'slategray' => '#708090' ,
148            'snow' => '#fffafa' ,
149            'springgreen' => '#00ff7f' ,
150            'steelblue' => '#4682b4' ,
151            'tan' => '#d2b48c' ,
152            'teal' => '#008080' ,
153            'thistle' => '#d8bfd8' ,
154            'tomato' => '#ff6347' ,
155            'turquoise' => '#40e0d0' ,
156            'violet' => '#ee82ee' ,
157            'wheat' => '#f5deb3' ,
158            'white' => '#ffffff' ,
159            'whitesmoke' => '#f5f5f5' ,
160            'yellow' => '#ffff00' ,
161            'yellowgreen' => '#9acd32' ,
162            );
163
164    function getType() { return 'formatting'; }
165    function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
166    function getSort() { return 105; }
167    function connectTo($mode) { $this->Lexer->addEntryPattern('\[color=.*?\](?=.*?\x5B/color\x5D)',$mode,'plugin_bbcode_color'); }
168    function postConnect() { $this->Lexer->addExitPattern('\[/color\]','plugin_bbcode_color'); }
169
170    /**
171     * Handle the match
172     */
173    function handle($match, $state, $pos, Doku_Handler $handler) {
174        switch ($state) {
175          case DOKU_LEXER_ENTER :
176            $match = substr($match, 7, -1);
177            if (preg_match('/".+?"/',$match)) $match = substr($match, 1, -1); // addition #1: unquote
178            return array($state, $match);
179
180          case DOKU_LEXER_UNMATCHED :
181            return array($state, $match);
182
183          case DOKU_LEXER_EXIT :
184            return array($state, '');
185
186        }
187        return array();
188    }
189
190    /**
191     * Create output
192     */
193    function render($mode, Doku_Renderer $renderer, $data) {
194        if($mode == 'xhtml') {
195            list($state, $match) = $data;
196            switch ($state) {
197              case DOKU_LEXER_ENTER :
198                if ($match = $this->_isValid($match)) {
199                    $renderer->doc .= '<span style="color:'. $match. '">'; // addition #2: SVG browser colors
200                } else {
201                    $renderer->doc .= '<span>';
202                }
203                break;
204
205              case DOKU_LEXER_UNMATCHED :
206                $renderer->doc .= $renderer->_xmlEntities($match);
207                break;
208
209              case DOKU_LEXER_EXIT :
210                $renderer->doc .= '</span>';
211                break;
212
213            }
214            return true;
215        }
216        return false;
217    }
218
219    // validate color value $c
220    // this is cut price validation - only to ensure the basic format is correct and there is nothing harmful
221    // three basic formats  "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
222    function _isValid($c) {
223        $c = trim($c);
224
225        $pattern = "/
226            ([a-zA-z]+)|                                #colorname - not verified
227            (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))|        #colorvalue
228            (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\))     #rgb triplet
229            /x";
230
231        if (preg_match($pattern, $c)) return $c;
232        if (!empty($this->browsercolors[$c])) return $this->browsercolors[$c];
233        return "";
234    }
235}
236// vim:ts=4:sw=4:et:enc=utf-8:
237