phpBB
Statistics
| Revision:

root / branches / phpBB-3_0_0 / phpBB / includes / bbcode.php

History | View | Annotate | Download (16.3 kB)

1 182 psotfx
<?php
2 8146 acydburn
/**
3 5114 acydburn
*
4 5114 acydburn
* @package phpBB3
5 5114 acydburn
* @version $Id$
6 8146 acydburn
* @copyright (c) 2005 phpBB Group
7 8146 acydburn
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 5114 acydburn
*
9 5114 acydburn
*/
10 182 psotfx
11 5114 acydburn
/**
12 8146 acydburn
* @ignore
13 8146 acydburn
*/
14 8146 acydburn
if (!defined('IN_PHPBB'))
15 8146 acydburn
{
16 8146 acydburn
        exit;
17 8146 acydburn
}
18 8146 acydburn
19 8146 acydburn
/**
20 6058 acydburn
* BBCode class
21 5114 acydburn
* @package phpBB3
22 5114 acydburn
*/
23 3812 ludovic_arnaud
class bbcode
24 2304 psotfx
{
25 3812 ludovic_arnaud
        var $bbcode_uid = '';
26 6209 davidmj
        var $bbcode_bitfield = '';
27 3812 ludovic_arnaud
        var $bbcode_cache = array();
28 4170 psotfx
        var $bbcode_template = array();
29 2304 psotfx
30 4978 acydburn
        var $bbcodes = array();
31 4978 acydburn
32 6777 davidmj
        var $template_bitfield;
33 4363 ludovic_arnaud
        var $template_filename = '';
34 4363 ludovic_arnaud
35 6048 acydburn
        /**
36 6048 acydburn
        * Constructor
37 6048 acydburn
        * Init bbcode cache entries if bitfield is specified
38 6048 acydburn
        */
39 6263 davidmj
        function bbcode($bitfield = '')
40 182 psotfx
        {
41 3863 ludovic_arnaud
                if ($bitfield)
42 3863 ludovic_arnaud
                {
43 3863 ludovic_arnaud
                        $this->bbcode_bitfield = $bitfield;
44 3863 ludovic_arnaud
                        $this->bbcode_cache_init();
45 3863 ludovic_arnaud
                }
46 182 psotfx
        }
47 2983 psotfx
48 6048 acydburn
        /**
49 6048 acydburn
        * Second pass bbcodes
50 6048 acydburn
        */
51 4978 acydburn
        function bbcode_second_pass(&$message, $bbcode_uid = '', $bbcode_bitfield = false)
52 1095 natec
        {
53 3812 ludovic_arnaud
                if ($bbcode_uid)
54 1243 natec
                {
55 3812 ludovic_arnaud
                        $this->bbcode_uid = $bbcode_uid;
56 1243 natec
                }
57 3934 ludovic_arnaud
58 4978 acydburn
                if ($bbcode_bitfield !== false)
59 3812 ludovic_arnaud
                {
60 3812 ludovic_arnaud
                        $this->bbcode_bitfield = $bbcode_bitfield;
61 5035 acydburn
62 4903 acydburn
                        // Init those added with a new bbcode_bitfield (already stored codes will not get parsed again)
63 4903 acydburn
                        $this->bbcode_cache_init();
64 3812 ludovic_arnaud
                }
65 4903 acydburn
66 4190 ludovic_arnaud
                if (!$this->bbcode_bitfield)
67 3934 ludovic_arnaud
                {
68 5967 acydburn
                        // Remove the uid from tags that have not been transformed into HTML
69 5967 acydburn
                        if ($this->bbcode_uid)
70 5967 acydburn
                        {
71 5967 acydburn
                                $message = str_replace(':' . $this->bbcode_uid, '', $message);
72 5967 acydburn
                        }
73 5967 acydburn
74 5967 acydburn
                        return;
75 3934 ludovic_arnaud
                }
76 2983 psotfx
77 3812 ludovic_arnaud
                $str = array('search' => array(), 'replace' => array());
78 3812 ludovic_arnaud
                $preg = array('search' => array(), 'replace' => array());
79 182 psotfx
80 6209 davidmj
                $bitfield = new bitfield($this->bbcode_bitfield);
81 6209 davidmj
                $bbcodes_set = $bitfield->get_all_set();
82 6209 davidmj
83 8153 naderman
                $undid_bbcode_specialchars = false;
84 6209 davidmj
                foreach ($bbcodes_set as $bbcode_id)
85 182 psotfx
                {
86 6209 davidmj
                        if (!empty($this->bbcode_cache[$bbcode_id]))
87 182 psotfx
                        {
88 6209 davidmj
                                foreach ($this->bbcode_cache[$bbcode_id] as $type => $array)
89 1818 the_systech
                                {
90 6209 davidmj
                                        foreach ($array as $search => $replace)
91 1243 natec
                                        {
92 6209 davidmj
                                                ${$type}['search'][] = str_replace('$uid', $this->bbcode_uid, $search);
93 6209 davidmj
                                                ${$type}['replace'][] = $replace;
94 6209 davidmj
                                        }
95 5023 acydburn
96 6209 davidmj
                                        if (sizeof($str['search']))
97 6209 davidmj
                                        {
98 6209 davidmj
                                                $message = str_replace($str['search'], $str['replace'], $message);
99 6209 davidmj
                                                $str = array('search' => array(), 'replace' => array());
100 6209 davidmj
                                        }
101 5023 acydburn
102 6209 davidmj
                                        if (sizeof($preg['search']))
103 6209 davidmj
                                        {
104 8153 naderman
                                                // we need to turn the entities back into their original form to allow the
105 8153 naderman
                                                // search patterns to work properly
106 8153 naderman
                                                if (!$undid_bbcode_specialchars)
107 8153 naderman
                                                {
108 8153 naderman
                                                        $message = str_replace(array('&#58;', '&#46;'), array(':', '.'), $message);
109 8153 naderman
                                                        $undid_bbcode_specialchars = true;
110 8153 naderman
                                                }
111 8153 naderman
112 6209 davidmj
                                                $message = preg_replace($preg['search'], $preg['replace'], $message);
113 6209 davidmj
                                                $preg = array('search' => array(), 'replace' => array());
114 1243 natec
                                        }
115 182 psotfx
                                }
116 182 psotfx
                        }
117 3812 ludovic_arnaud
                }
118 792 thefinn
119 4453 ludovic_arnaud
                // Remove the uid from tags that have not been transformed into HTML
120 4453 ludovic_arnaud
                $message = str_replace(':' . $this->bbcode_uid, '', $message);
121 3812 ludovic_arnaud
        }
122 6048 acydburn
123 6048 acydburn
        /**
124 6048 acydburn
        * Init bbcode cache
125 6048 acydburn
        *
126 6048 acydburn
        * requires: $this->bbcode_bitfield
127 6048 acydburn
        * sets: $this->bbcode_cache with bbcode templates needed for bbcode_bitfield
128 6048 acydburn
        */
129 3812 ludovic_arnaud
        function bbcode_cache_init()
130 3812 ludovic_arnaud
        {
131 10150 nickvergessen
                global $phpbb_root_path, $template, $user;
132 4453 ludovic_arnaud
133 4363 ludovic_arnaud
                if (empty($this->template_filename))
134 4363 ludovic_arnaud
                {
135 6777 davidmj
                        $this->template_bitfield = new bitfield($user->theme['bbcode_bitfield']);
136 5372 acydburn
                        $this->template_filename = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/bbcode.html';
137 9461 acydburn
138 5957 acydburn
                        if (!@file_exists($this->template_filename))
139 5957 acydburn
                        {
140 10515 naderman
                                if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
141 8697 Kellanved
                                {
142 8697 Kellanved
                                        $this->template_filename = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template/bbcode.html';
143 8697 Kellanved
                                        if (!@file_exists($this->template_filename))
144 8697 Kellanved
                                        {
145 8697 Kellanved
                                                trigger_error('The file ' . $this->template_filename . ' is missing.', E_USER_ERROR);
146 8697 Kellanved
                                        }
147 8697 Kellanved
                                }
148 8697 Kellanved
                                else
149 8697 Kellanved
                                {
150 8697 Kellanved
                                        trigger_error('The file ' . $this->template_filename . ' is missing.', E_USER_ERROR);
151 8697 Kellanved
                                }
152 5957 acydburn
                        }
153 4363 ludovic_arnaud
                }
154 4363 ludovic_arnaud
155 6271 acydburn
                $bbcode_ids = $rowset = $sql = array();
156 3863 ludovic_arnaud
157 6209 davidmj
                $bitfield = new bitfield($this->bbcode_bitfield);
158 6209 davidmj
                $bbcodes_set = $bitfield->get_all_set();
159 6209 davidmj
160 6209 davidmj
                foreach ($bbcodes_set as $bbcode_id)
161 3812 ludovic_arnaud
                {
162 6209 davidmj
                        if (isset($this->bbcode_cache[$bbcode_id]))
163 3863 ludovic_arnaud
                        {
164 4086 ludovic_arnaud
                                // do not try to re-cache it if it's already in
165 3863 ludovic_arnaud
                                continue;
166 3863 ludovic_arnaud
                        }
167 4670 ludovic_arnaud
                        $bbcode_ids[] = $bbcode_id;
168 3812 ludovic_arnaud
169 4819 acydburn
                        if ($bbcode_id > NUM_CORE_BBCODES)
170 182 psotfx
                        {
171 6271 acydburn
                                $sql[] = $bbcode_id;
172 182 psotfx
                        }
173 3812 ludovic_arnaud
                }
174 4453 ludovic_arnaud
175 6271 acydburn
                if (sizeof($sql))
176 3812 ludovic_arnaud
                {
177 3812 ludovic_arnaud
                        global $db;
178 792 thefinn
179 4453 ludovic_arnaud
                        $sql = 'SELECT *
180 6271 acydburn
                                FROM ' . BBCODES_TABLE . '
181 6271 acydburn
                                WHERE ' . $db->sql_in_set('bbcode_id', $sql);
182 6271 acydburn
                        $result = $db->sql_query($sql, 3600);
183 792 thefinn
184 3812 ludovic_arnaud
                        while ($row = $db->sql_fetchrow($result))
185 3812 ludovic_arnaud
                        {
186 6391 acydburn
                                // To circumvent replacing newlines with <br /> for the generated html,
187 8050 naderman
                                // we use carriage returns here. They are later changed back to newlines
188 8050 naderman
                                $row['bbcode_tpl'] = str_replace("\n", "\r", $row['bbcode_tpl']);
189 8050 naderman
                                $row['second_pass_replace'] = str_replace("\n", "\r", $row['second_pass_replace']);
190 6391 acydburn
191 3812 ludovic_arnaud
                                $rowset[$row['bbcode_id']] = $row;
192 3812 ludovic_arnaud
                        }
193 3863 ludovic_arnaud
                        $db->sql_freeresult($result);
194 3812 ludovic_arnaud
                }
195 4043 ludovic_arnaud
196 3812 ludovic_arnaud
                foreach ($bbcode_ids as $bbcode_id)
197 3812 ludovic_arnaud
                {
198 3812 ludovic_arnaud
                        switch ($bbcode_id)
199 3812 ludovic_arnaud
                        {
200 3812 ludovic_arnaud
                                case 0:
201 3812 ludovic_arnaud
                                        $this->bbcode_cache[$bbcode_id] = array(
202 3812 ludovic_arnaud
                                                'str' => array(
203 6048 acydburn
                                                        '[/quote:$uid]'        => $this->bbcode_tpl('quote_close', $bbcode_id)
204 3812 ludovic_arnaud
                                                ),
205 3812 ludovic_arnaud
                                                'preg' => array(
206 7110 davidmj
                                                        '#\[quote(?:=&quot;(.*?)&quot;)?:$uid\]((?!\[quote(?:=&quot;.*?&quot;)?:$uid\]).)?#ise'        => "\$this->bbcode_second_pass_quote('\$1', '\$2')"
207 3812 ludovic_arnaud
                                                )
208 3812 ludovic_arnaud
                                        );
209 3812 ludovic_arnaud
                                break;
210 6048 acydburn
211 3812 ludovic_arnaud
                                case 1:
212 6048 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
213 6048 acydburn
                                                'str' => array(
214 6048 acydburn
                                                        '[b:$uid]'        => $this->bbcode_tpl('b_open', $bbcode_id),
215 6048 acydburn
                                                        '[/b:$uid]'        => $this->bbcode_tpl('b_close', $bbcode_id),
216 6048 acydburn
                                                )
217 6048 acydburn
                                        );
218 3812 ludovic_arnaud
                                break;
219 6048 acydburn
220 3812 ludovic_arnaud
                                case 2:
221 6048 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
222 6048 acydburn
                                                'str' => array(
223 6048 acydburn
                                                        '[i:$uid]'        => $this->bbcode_tpl('i_open', $bbcode_id),
224 6048 acydburn
                                                        '[/i:$uid]'        => $this->bbcode_tpl('i_close', $bbcode_id),
225 6048 acydburn
                                                )
226 6048 acydburn
                                        );
227 3812 ludovic_arnaud
                                break;
228 6048 acydburn
229 3812 ludovic_arnaud
                                case 3:
230 6048 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
231 6048 acydburn
                                                'preg' => array(
232 6048 acydburn
                                                        '#\[url:$uid\]((.*?))\[/url:$uid\]#s'                        => $this->bbcode_tpl('url', $bbcode_id),
233 6048 acydburn
                                                        '#\[url=([^\[]+?):$uid\](.*?)\[/url:$uid\]#s'        => $this->bbcode_tpl('url', $bbcode_id),
234 6048 acydburn
                                                )
235 6048 acydburn
                                        );
236 3812 ludovic_arnaud
                                break;
237 6048 acydburn
238 3812 ludovic_arnaud
                                case 4:
239 4578 psotfx
                                        if ($user->optionget('viewimg'))
240 4028 psotfx
                                        {
241 6048 acydburn
                                                $this->bbcode_cache[$bbcode_id] = array(
242 6048 acydburn
                                                        'preg' => array(
243 6048 acydburn
                                                                '#\[img:$uid\](.*?)\[/img:$uid\]#s'                => $this->bbcode_tpl('img', $bbcode_id),
244 6048 acydburn
                                                        )
245 6048 acydburn
                                                );
246 4028 psotfx
                                        }
247 4028 psotfx
                                        else
248 4028 psotfx
                                        {
249 6048 acydburn
                                                $this->bbcode_cache[$bbcode_id] = array(
250 6048 acydburn
                                                        'preg' => array(
251 6650 acydburn
                                                                '#\[img:$uid\](.*?)\[/img:$uid\]#s'                => str_replace('$2', '[ img ]', $this->bbcode_tpl('url', $bbcode_id, true)),
252 6048 acydburn
                                                        )
253 6048 acydburn
                                                );
254 4028 psotfx
                                        }
255 4043 ludovic_arnaud
                                break;
256 6048 acydburn
257 3812 ludovic_arnaud
                                case 5:
258 6048 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
259 6048 acydburn
                                                'preg' => array(
260 7147 davidmj
                                                        '#\[size=([\-\+]?\d+):$uid\](.*?)\[/size:$uid\]#s'        => $this->bbcode_tpl('size', $bbcode_id),
261 6048 acydburn
                                                )
262 6048 acydburn
                                        );
263 3812 ludovic_arnaud
                                break;
264 6048 acydburn
265 3812 ludovic_arnaud
                                case 6:
266 6048 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
267 6048 acydburn
                                                'preg' => array(
268 9722 acydburn
                                                        '!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+):$uid\](.*?)\[/color:$uid\]!is'        => $this->bbcode_tpl('color', $bbcode_id),
269 6048 acydburn
                                                )
270 6048 acydburn
                                        );
271 3812 ludovic_arnaud
                                break;
272 6048 acydburn
273 3812 ludovic_arnaud
                                case 7:
274 6048 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
275 6048 acydburn
                                                'str' => array(
276 6048 acydburn
                                                        '[u:$uid]'        => $this->bbcode_tpl('u_open', $bbcode_id),
277 6048 acydburn
                                                        '[/u:$uid]'        => $this->bbcode_tpl('u_close', $bbcode_id),
278 6048 acydburn
                                                )
279 6048 acydburn
                                        );
280 3812 ludovic_arnaud
                                break;
281 6048 acydburn
282 3812 ludovic_arnaud
                                case 8:
283 6048 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
284 6048 acydburn
                                                'preg' => array(
285 6048 acydburn
                                                        '#\[code(?:=([a-z]+))?:$uid\](.*?)\[/code:$uid\]#ise'        => "\$this->bbcode_second_pass_code('\$1', '\$2')",
286 6048 acydburn
                                                )
287 6048 acydburn
                                        );
288 3812 ludovic_arnaud
                                break;
289 6048 acydburn
290 3812 ludovic_arnaud
                                case 9:
291 3812 ludovic_arnaud
                                        $this->bbcode_cache[$bbcode_id] = array(
292 5023 acydburn
                                                'preg' => array(
293 5023 acydburn
                                                        '#(\[\/?(list|\*):[mou]?:?$uid\])[\n]{1}#'        => "\$1",
294 6048 acydburn
                                                        '#(\[list=([^\[]+):$uid\])[\n]{1}#'                        => "\$1",
295 6048 acydburn
                                                        '#\[list=([^\[]+):$uid\]#e'                                        => "\$this->bbcode_list('\$1')",
296 5023 acydburn
                                                ),
297 3812 ludovic_arnaud
                                                'str' => array(
298 6048 acydburn
                                                        '[list:$uid]'                => $this->bbcode_tpl('ulist_open_default', $bbcode_id),
299 6048 acydburn
                                                        '[/list:u:$uid]'        => $this->bbcode_tpl('ulist_close', $bbcode_id),
300 6048 acydburn
                                                        '[/list:o:$uid]'        => $this->bbcode_tpl('olist_close', $bbcode_id),
301 6048 acydburn
                                                        '[*:$uid]'                        => $this->bbcode_tpl('listitem', $bbcode_id),
302 6048 acydburn
                                                        '[/*:$uid]'                        => $this->bbcode_tpl('listitem_close', $bbcode_id),
303 6048 acydburn
                                                        '[/*:m:$uid]'                => $this->bbcode_tpl('listitem_close', $bbcode_id)
304 3812 ludovic_arnaud
                                                ),
305 3812 ludovic_arnaud
                                        );
306 3812 ludovic_arnaud
                                break;
307 6048 acydburn
308 3812 ludovic_arnaud
                                case 10:
309 6048 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
310 6048 acydburn
                                                'preg' => array(
311 6048 acydburn
                                                        '#\[email:$uid\]((.*?))\[/email:$uid\]#is'                        => $this->bbcode_tpl('email', $bbcode_id),
312 6048 acydburn
                                                        '#\[email=([^\[]+):$uid\](.*?)\[/email:$uid\]#is'        => $this->bbcode_tpl('email', $bbcode_id)
313 6048 acydburn
                                                )
314 6048 acydburn
                                        );
315 3812 ludovic_arnaud
                                break;
316 6048 acydburn
317 3863 ludovic_arnaud
                                case 11:
318 4578 psotfx
                                        if ($user->optionget('viewflash'))
319 4028 psotfx
                                        {
320 6048 acydburn
                                                $this->bbcode_cache[$bbcode_id] = array(
321 6048 acydburn
                                                        'preg' => array(
322 6048 acydburn
                                                                '#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#'        => $this->bbcode_tpl('flash', $bbcode_id),
323 6048 acydburn
                                                        )
324 6048 acydburn
                                                );
325 4028 psotfx
                                        }
326 4028 psotfx
                                        else
327 4028 psotfx
                                        {
328 6048 acydburn
                                                $this->bbcode_cache[$bbcode_id] = array(
329 6048 acydburn
                                                        'preg' => array(
330 6650 acydburn
                                                                '#\[flash=([0-9]+),([0-9]+):$uid\](.*?)\[/flash:$uid\]#'        => str_replace('$1', '$3', str_replace('$2', '[ flash ]', $this->bbcode_tpl('url', $bbcode_id, true)))
331 6048 acydburn
                                                        )
332 6048 acydburn
                                                );
333 4028 psotfx
                                        }
334 4043 ludovic_arnaud
                                break;
335 6048 acydburn
336 4819 acydburn
                                case 12:
337 4984 acydburn
                                        $this->bbcode_cache[$bbcode_id] = array(
338 4984 acydburn
                                                'str'        => array(
339 6048 acydburn
                                                        '[/attachment:$uid]'        => $this->bbcode_tpl('inline_attachment_close', $bbcode_id)
340 6048 acydburn
                                                ),
341 4984 acydburn
                                                'preg'        => array(
342 6048 acydburn
                                                        '#\[attachment=([0-9]+):$uid\]#'        => $this->bbcode_tpl('inline_attachment_open', $bbcode_id)
343 6048 acydburn
                                                )
344 4984 acydburn
                                        );
345 6048 acydburn
                                break;
346 6048 acydburn
347 3812 ludovic_arnaud
                                default:
348 3812 ludovic_arnaud
                                        if (isset($rowset[$bbcode_id]))
349 3812 ludovic_arnaud
                                        {
350 6777 davidmj
                                                if ($this->template_bitfield->get($bbcode_id))
351 4453 ludovic_arnaud
                                                {
352 4453 ludovic_arnaud
                                                        // The bbcode requires a custom template to be loaded
353 4453 ludovic_arnaud
                                                        if (!$bbcode_tpl = $this->bbcode_tpl($rowset[$bbcode_id]['bbcode_tag'], $bbcode_id))
354 4453 ludovic_arnaud
                                                        {
355 6048 acydburn
                                                                // For some reason, the required template seems not to be available, use the default template
356 4453 ludovic_arnaud
                                                                $bbcode_tpl = (!empty($rowset[$bbcode_id]['second_pass_replace'])) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
357 4453 ludovic_arnaud
                                                        }
358 4453 ludovic_arnaud
                                                        else
359 4453 ludovic_arnaud
                                                        {
360 4453 ludovic_arnaud
                                                                // In order to use templates with custom bbcodes we need
361 4453 ludovic_arnaud
                                                                // to replace all {VARS} to corresponding backreferences
362 4453 ludovic_arnaud
                                                                // Note that backreferences are numbered from bbcode_match
363 10516 Kellanved
                                                                if (preg_match_all('/\{(URL|LOCAL_URL|EMAIL|TEXT|SIMPLETEXT|INTTEXT|IDENTIFIER|COLOR|NUMBER)[0-9]*\}/', $rowset[$bbcode_id]['bbcode_match'], $m))
364 4453 ludovic_arnaud
                                                                {
365 4453 ludovic_arnaud
                                                                        foreach ($m[0] as $i => $tok)
366 4453 ludovic_arnaud
                                                                        {
367 4453 ludovic_arnaud
                                                                                $bbcode_tpl = str_replace($tok, '$' . ($i + 1), $bbcode_tpl);
368 4453 ludovic_arnaud
                                                                        }
369 4453 ludovic_arnaud
                                                                }
370 4453 ludovic_arnaud
                                                        }
371 4453 ludovic_arnaud
                                                }
372 4453 ludovic_arnaud
                                                else
373 4453 ludovic_arnaud
                                                {
374 4453 ludovic_arnaud
                                                        // Default template
375 4453 ludovic_arnaud
                                                        $bbcode_tpl = (!empty($rowset[$bbcode_id]['second_pass_replace'])) ? $rowset[$bbcode_id]['second_pass_replace'] : $rowset[$bbcode_id]['bbcode_tpl'];
376 4453 ludovic_arnaud
                                                }
377 4453 ludovic_arnaud
378 4453 ludovic_arnaud
                                                // Replace {L_*} lang strings
379 5737 acydburn
                                                $bbcode_tpl = preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $bbcode_tpl);
380 4453 ludovic_arnaud
381 4453 ludovic_arnaud
                                                if (!empty($rowset[$bbcode_id]['second_pass_replace']))
382 4453 ludovic_arnaud
                                                {
383 4453 ludovic_arnaud
                                                        // The custom BBCode requires second-pass pattern replacements
384 4453 ludovic_arnaud
                                                        $this->bbcode_cache[$bbcode_id] = array(
385 4453 ludovic_arnaud
                                                                'preg' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl)
386 4453 ludovic_arnaud
                                                        );
387 4453 ludovic_arnaud
                                                }
388 4453 ludovic_arnaud
                                                else
389 4453 ludovic_arnaud
                                                {
390 4453 ludovic_arnaud
                                                        $this->bbcode_cache[$bbcode_id] = array(
391 4453 ludovic_arnaud
                                                                'str' => array($rowset[$bbcode_id]['second_pass_match'] => $bbcode_tpl)
392 4453 ludovic_arnaud
                                                        );
393 4453 ludovic_arnaud
                                                }
394 182 psotfx
                                        }
395 182 psotfx
                                        else
396 182 psotfx
                                        {
397 5315 acydburn
                                                $this->bbcode_cache[$bbcode_id] = false;
398 182 psotfx
                                        }
399 6048 acydburn
                                break;
400 182 psotfx
                        }
401 182 psotfx
                }
402 3812 ludovic_arnaud
        }
403 3934 ludovic_arnaud
404 6048 acydburn
        /**
405 6048 acydburn
        * Return bbcode template
406 6048 acydburn
        */
407 6650 acydburn
        function bbcode_tpl($tpl_name, $bbcode_id = -1, $skip_bitfield_check = false)
408 3934 ludovic_arnaud
        {
409 6777 davidmj
                static $bbcode_hardtpl = array();
410 4170 psotfx
                if (empty($bbcode_hardtpl))
411 4170 psotfx
                {
412 6073 acydburn
                        global $user;
413 9461 acydburn
414 6073 acydburn
                        $bbcode_hardtpl = array(
415 6048 acydburn
                                'b_open'        => '<span style="font-weight: bold">',
416 6048 acydburn
                                'b_close'        => '</span>',
417 6048 acydburn
                                'i_open'        => '<span style="font-style: italic">',
418 6048 acydburn
                                'i_close'        => '</span>',
419 6048 acydburn
                                'u_open'        => '<span style="text-decoration: underline">',
420 6048 acydburn
                                'u_close'        => '</span>',
421 6073 acydburn
                                'img'                => '<img src="$1" alt="' . $user->lang['IMAGE'] . '" />',
422 7157 dhn2
                                'size'                => '<span style="font-size: $1%; line-height: normal">$2</span>',
423 6048 acydburn
                                'color'                => '<span style="color: $1">$2</span>',
424 6048 acydburn
                                'email'                => '<a href="mailto:$1">$2</a>'
425 4170 psotfx
                        );
426 4170 psotfx
                }
427 4170 psotfx
428 6777 davidmj
                if ($bbcode_id != -1 && !$skip_bitfield_check && !$this->template_bitfield->get($bbcode_id))
429 4043 ludovic_arnaud
                {
430 5315 acydburn
                        return (isset($bbcode_hardtpl[$tpl_name])) ? $bbcode_hardtpl[$tpl_name] : false;
431 4043 ludovic_arnaud
                }
432 4043 ludovic_arnaud
433 4170 psotfx
                if (empty($this->bbcode_template))
434 3934 ludovic_arnaud
                {
435 5422 davidmj
                        if (($tpl = file_get_contents($this->template_filename)) === false)
436 3934 ludovic_arnaud
                        {
437 6048 acydburn
                                trigger_error('Could not load bbcode template', E_USER_ERROR);
438 3934 ludovic_arnaud
                        }
439 3934 ludovic_arnaud
440 3934 ludovic_arnaud
                        // replace \ with \\ and then ' with \'.
441 3934 ludovic_arnaud
                        $tpl = str_replace('\\', '\\\\', $tpl);
442 3934 ludovic_arnaud
                        $tpl = str_replace("'", "\'", $tpl);
443 6048 acydburn
444 4043 ludovic_arnaud
                        // strip newlines and indent
445 4043 ludovic_arnaud
                        $tpl = preg_replace("/\n[\n\r\s\t]*/", '', $tpl);
446 6048 acydburn
447 3934 ludovic_arnaud
                        // Turn template blocks into PHP assignment statements for the values of $bbcode_tpl..
448 5422 davidmj
                        $this->bbcode_template = array();
449 3934 ludovic_arnaud
450 5422 davidmj
                        $matches = preg_match_all('#<!-- BEGIN (.*?) -->(.*?)<!-- END (?:.*?) -->#', $tpl, $match);
451 5422 davidmj
452 5977 acydburn
                        for ($i = 0; $i < $matches; $i++)
453 5422 davidmj
                        {
454 5977 acydburn
                                if (empty($match[1][$i]))
455 5977 acydburn
                                {
456 5977 acydburn
                                        continue;
457 5977 acydburn
                                }
458 5977 acydburn
459 5422 davidmj
                                $this->bbcode_template[$match[1][$i]] = $this->bbcode_tpl_replace($match[1][$i], $match[2][$i]);
460 5422 davidmj
                        }
461 4043 ludovic_arnaud
                }
462 3934 ludovic_arnaud
463 5315 acydburn
                return (isset($this->bbcode_template[$tpl_name])) ? $this->bbcode_template[$tpl_name] : ((isset($bbcode_hardtpl[$tpl_name])) ? $bbcode_hardtpl[$tpl_name] : false);
464 4043 ludovic_arnaud
        }
465 6048 acydburn
466 6048 acydburn
        /**
467 6048 acydburn
        * Return bbcode template replacement
468 6048 acydburn
        */
469 4043 ludovic_arnaud
        function bbcode_tpl_replace($tpl_name, $tpl)
470 4043 ludovic_arnaud
        {
471 4978 acydburn
                global $user;
472 6048 acydburn
473 4043 ludovic_arnaud
                static $replacements = array(
474 6048 acydburn
                        'quote_username_open'        => array('{USERNAME}'        => '$1'),
475 6048 acydburn
                        'color'                                        => array('{COLOR}'                => '$1', '{TEXT}'                        => '$2'),
476 6048 acydburn
                        'size'                                        => array('{SIZE}'                => '$1', '{TEXT}'                        => '$2'),
477 6048 acydburn
                        'img'                                        => array('{URL}'                => '$1'),
478 6048 acydburn
                        'flash'                                        => array('{WIDTH}'                => '$1', '{HEIGHT}'                        => '$2', '{URL}'        => '$3'),
479 6048 acydburn
                        'url'                                        => array('{URL}'                => '$1', '{DESCRIPTION}'        => '$2'),
480 6048 acydburn
                        'email'                                        => array('{EMAIL}'                => '$1', '{DESCRIPTION}'        => '$2')
481 4043 ludovic_arnaud
                );
482 4014 ludovic_arnaud
483 4453 ludovic_arnaud
                $tpl = preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $tpl);
484 4043 ludovic_arnaud
485 4043 ludovic_arnaud
                if (!empty($replacements[$tpl_name]))
486 4043 ludovic_arnaud
                {
487 4043 ludovic_arnaud
                        $tpl = strtr($tpl, $replacements[$tpl_name]);
488 3934 ludovic_arnaud
                }
489 3934 ludovic_arnaud
490 4043 ludovic_arnaud
                return trim($tpl);
491 3934 ludovic_arnaud
        }
492 6048 acydburn
493 6048 acydburn
        /**
494 6048 acydburn
        * Second parse list bbcode
495 6048 acydburn
        */
496 4086 ludovic_arnaud
        function bbcode_list($type)
497 182 psotfx
        {
498 6067 acydburn
                if ($type == '')
499 3812 ludovic_arnaud
                {
500 6067 acydburn
                        $tpl = 'ulist_open_default';
501 6067 acydburn
                        $type = 'default';
502 4086 ludovic_arnaud
                }
503 6067 acydburn
                else if ($type == 'i')
504 6067 acydburn
                {
505 6067 acydburn
                        $tpl = 'olist_open';
506 6067 acydburn
                        $type = 'lower-roman';
507 6067 acydburn
                }
508 6067 acydburn
                else if ($type == 'I')
509 6067 acydburn
                {
510 6067 acydburn
                        $tpl = 'olist_open';
511 6067 acydburn
                        $type = 'upper-roman';
512 6067 acydburn
                }
513 6067 acydburn
                else if (preg_match('#^(disc|circle|square)$#i', $type))
514 6067 acydburn
                {
515 6067 acydburn
                        $tpl = 'ulist_open';
516 6067 acydburn
                        $type = strtolower($type);
517 6067 acydburn
                }
518 6067 acydburn
                else if (preg_match('#^[a-z]$#', $type))
519 6067 acydburn
                {
520 6067 acydburn
                        $tpl = 'olist_open';
521 6067 acydburn
                        $type = 'lower-alpha';
522 6067 acydburn
                }
523 6067 acydburn
                else if (preg_match('#[A-Z]#', $type))
524 6067 acydburn
                {
525 6067 acydburn
                        $tpl = 'olist_open';
526 6067 acydburn
                        $type = 'upper-alpha';
527 6067 acydburn
                }
528 6067 acydburn
                else if (is_numeric($type))
529 6067 acydburn
                {
530 6067 acydburn
                        $tpl = 'olist_open';
531 9461 acydburn
                        $type = 'decimal';
532 6067 acydburn
                }
533 6067 acydburn
                else
534 6067 acydburn
                {
535 6067 acydburn
                        $tpl = 'olist_open';
536 9461 acydburn
                        $type = 'decimal';
537 6067 acydburn
                }
538 4086 ludovic_arnaud
539 4086 ludovic_arnaud
                return str_replace('{LIST_TYPE}', $type, $this->bbcode_tpl($tpl));
540 182 psotfx
        }
541 792 thefinn
542 6048 acydburn
        /**
543 6048 acydburn
        * Second parse quote tag
544 6048 acydburn
        */
545 5027 acydburn
        function bbcode_second_pass_quote($username, $quote)
546 5027 acydburn
        {
547 5027 acydburn
                // when using the /e modifier, preg_replace slashes double-quotes but does not
548 5027 acydburn
                // seem to slash anything else
549 5027 acydburn
                $quote = str_replace('\"', '"', $quote);
550 6042 davidmj
                $username = str_replace('\"', '"', $username);
551 5027 acydburn
552 5027 acydburn
                // remove newline at the beginning
553 7107 davidmj
                if ($quote == "\n")
554 5027 acydburn
                {
555 7107 davidmj
                        $quote = '';
556 5027 acydburn
                }
557 5027 acydburn
558 5027 acydburn
                $quote = (($username) ? str_replace('$1', $username, $this->bbcode_tpl('quote_username_open')) : $this->bbcode_tpl('quote_open')) . $quote;
559 5027 acydburn
560 5027 acydburn
                return $quote;
561 5027 acydburn
        }
562 5027 acydburn
563 6048 acydburn
        /**
564 6048 acydburn
        * Second parse code tag
565 6048 acydburn
        */
566 3863 ludovic_arnaud
        function bbcode_second_pass_code($type, $code)
567 3812 ludovic_arnaud
        {
568 4453 ludovic_arnaud
                // when using the /e modifier, preg_replace slashes double-quotes but does not
569 4453 ludovic_arnaud
                // seem to slash anything else
570 4453 ludovic_arnaud
                $code = str_replace('\"', '"', $code);
571 3863 ludovic_arnaud
572 3863 ludovic_arnaud
                switch ($type)
573 3863 ludovic_arnaud
                {
574 3863 ludovic_arnaud
                        case 'php':
575 4984 acydburn
                                // Not the english way, but valid because of hardcoded syntax highlighting
576 4984 acydburn
                                if (strpos($code, '<span class="syntaxdefault"><br /></span>') === 0)
577 4984 acydburn
                                {
578 4984 acydburn
                                        $code = substr($code, 41);
579 4984 acydburn
                                }
580 4984 acydburn
581 6048 acydburn
                        // no break;
582 6048 acydburn
583 3863 ludovic_arnaud
                        default:
584 8953 acydburn
                                $code = str_replace("\t", '&nbsp; &nbsp;', $code);
585 3863 ludovic_arnaud
                                $code = str_replace('  ', '&nbsp; ', $code);
586 3863 ludovic_arnaud
                                $code = str_replace('  ', ' &nbsp;', $code);
587 11348 git-gate
                                $code = str_replace("\n ", "\n&nbsp;", $code);
588 8953 acydburn
589 11348 git-gate
                                // keep space at the beginning
590 11348 git-gate
                                if (!empty($code) && $code[0] == ' ')
591 11348 git-gate
                                {
592 11348 git-gate
                                        $code = '&nbsp;' . substr($code, 1);
593 11348 git-gate
                                }
594 11348 git-gate
595 4984 acydburn
                                // remove newline at the beginning
596 6459 acydburn
                                if (!empty($code) && $code[0] == "\n")
597 4984 acydburn
                                {
598 4984 acydburn
                                        $code = substr($code, 1);
599 4984 acydburn
                                }
600 6048 acydburn
                        break;
601 3863 ludovic_arnaud
                }
602 3863 ludovic_arnaud
603 3934 ludovic_arnaud
                $code = $this->bbcode_tpl('code_open') . $code . $this->bbcode_tpl('code_close');
604 182 psotfx
605 3812 ludovic_arnaud
                return $code;
606 3812 ludovic_arnaud
        }
607 328 thefinn
}
608 4978 acydburn
609 2669 psotfx
?>