source: extensions/bbcode_bar/bbcode_bar.inc.php @ 9885

Last change on this file since 9885 was 9787, checked in by mistic100, 13 years ago

[extentions] BBCode Bar

File size: 6.6 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3 
4function set_bbcode_bar()
5{
6        global $template, $conf, $lang, $user, $pwg_loaded_plugins, $page;
7        load_language('plugin.lang', dirname(__FILE__) . '/');
8        $conf_bbcode_bar = explode("," , $conf['bbcode_bar']);
9
10        // buttons
11        for ($i=0; $i<=15; $i++) {
12                if ($conf_bbcode_bar[$i] == 1) $template->assign('BBCode_bar_button_'.sprintf("%02d", $i), true);
13        }
14        $template->assign('repicon', $conf_bbcode_bar[16]);
15       
16        // edit field has different id
17        if (
18                (isset($_GET['action']) AND $_GET['action'] == 'edit_comment') 
19                OR (isset($page['body_id']) AND $page['body_id'] == 'theCommentsPage')
20        ) {
21                $template->assign('form_name', 'editComment');
22        } else {
23                $template->assign('form_name', 'addComment');
24        }
25
26        // smilies support
27        if (isset($pwg_loaded_plugins['SmiliesSupport'])) {
28                $template->assign('BBCode_bar_SmiliesSupport', array('SMILIESSUPPORT_PAGE' => SmiliesTable()));
29        }
30
31        $template->assign('BBCODE_PATH', BBcode_PATH);
32        $template->set_filename('bbcode_bar', dirname(__FILE__).'/template/bbcode_bar.tpl');
33        $lang['Comment'] .= $template->parse('bbcode_bar', true);                       
34}
35
36
37//Check tags and eventually close malformed tags, return BBCoded String
38function CheckTags($str)
39{
40        //array of known tags
41        $known = array('p','b','i','u','s','center','right','ol','ul','li','quote', 'img','url','email','color', 'size');
42        //storage stack
43        $tags = array();
44
45        for ($pos = 0; $pos<strlen($str); $pos++)
46        {
47                if ($str{$pos} == '[')
48                {
49                        $end_pos = strpos($str, ']', $pos);
50                        $tag = substr($str, ++$pos, $end_pos-$pos);
51                        //deals with tags which contains arguments (ie quote)
52                        if ( ($equal_pos = strpos($tag, '=', 0)) !== FALSE)
53                        $tag = substr($tag, 0, $equal_pos);
54                        //check whether we have a defined tag or not.
55                        if (in_array(strtolower($tag),$known) || in_array(strtolower(substr($tag,1)),$known))
56                        {
57                                //closing tag
58                                if ($tag{0} == '/')
59                                {
60                                        //cleaned tag
61                                        $tag = substr($tag, 1);         
62                                        $before_tag = substr($str, 0, $pos-1);
63                                        $after_tag = substr($str, $end_pos+1); 
64                                        //pop stack
65                                        while (($temp = array_pop($tags)))
66                                        {
67                                                if ($temp != $tag) {
68                                                        $before_tag.='[/'.$temp.']';
69                                                } else {
70                                                        $before_tag.='[/'.$tag.']';
71                                                        break;
72                                                }
73                                        }
74                                        $end_pos += strlen($before_tag)+strlen($after_tag)-strlen($str);
75                                        $str = $before_tag.$after_tag;
76                                } else { // push stack
77                                        array_push($tags,$tag);
78                                }
79                        }
80                        $pos = $end_pos;       
81                }
82        }
83        // empty stack and closing tags
84        while ($temp = array_pop($tags))
85        {               
86                $str.='[/'.$temp.']';
87        }
88        return $str;
89}
90
91// return string, HTML version of BBCoded $str
92function BBCodeParse($str)
93{
94        global $conf;
95
96        $conf_bbcode_bar = explode("," , $conf['bbcode_bar']);
97        $str = CheckTags(nl2br($str));
98
99        $patterns = array();
100        $replacements = array();
101
102        if ( $conf_bbcode_bar[0] == 1 )
103        {
104                //Paragraph
105                $patterns[] = '#\[p\](.*?)\[/p\]#is';
106                $replacements[] = '<p>\\1</p>';
107        }
108        if ( $conf_bbcode_bar[1] == 1 )
109        {
110                // Bold
111                $patterns[] = '#\[b\](.*?)\[/b\]#is';
112                $replacements[] = '<strong>\\1</strong>';
113        }
114        if ( $conf_bbcode_bar[2] == 1 )
115        {
116                //Italic
117                $patterns[] = '#\[i\](.*?)\[/i\]#is';
118                $replacements[] = '<em>\\1</em>';
119        }
120        if ( $conf_bbcode_bar[3] == 1 )
121        {
122                //Underline     
123                $patterns[] = '#\[u\](.*?)\[\/u\]#is';
124                $replacements[] = '<u>\\1</u>';
125        }
126        if ( $conf_bbcode_bar[4] == 1 )
127        {
128                //Strikethrough
129                $patterns[] = '#\[s\](.*?)\[/s\]#is';
130                $replacements[] = '<del>\\1</del>';
131        }
132        if ( $conf_bbcode_bar[5] == 1 )
133        {
134                //Center
135                $patterns[] = '#\[center\](.*?)\[/center\]#is';
136                $replacements[] = '<div align="center"><p>\\1</p></div>';
137        }
138        if ( $conf_bbcode_bar[6] == 1 )
139        {
140                //Right
141                $patterns[] = '#\[right\](.*?)\[/right\]#is';
142                $replacements[] = '<div align="right"><p>\\1</p></div>';
143        }
144        if ( $conf_bbcode_bar[7] == 1 )
145        {
146                //Olist
147                $patterns[] = '#\[ol\](.*?)\[/ol\]#is';
148                $replacements[] = '<ol>\\1</ol>';
149        }
150        if ( $conf_bbcode_bar[8] == 1 )
151        {
152                //Ulist
153                $patterns[] = '#\[ul\](.*?)\[/ul\]#is';
154                $replacements[] = '<ul>\\1</ul>';
155        }
156        if (( $conf_bbcode_bar[7] == 1 ) || ( $conf_bbcode_bar[8] == 1 ))
157        {
158                //List
159                $patterns[] = '#\[li\](.*?)\[/li\]#is';
160                $replacements[] = '<li>\\1</li>';
161        }
162        if ( $conf_bbcode_bar[9] == 1 )
163        {
164                // Quotes
165                $patterns[] = "#\[quote\](.*?)\[/quote\]#is";
166                $replacements[] = '<blockquote><span style="font-size: 11px; line-height: normal">\\1</span></blockquote>';
167
168                //Quotes with "user"
169                $patterns[] = "#\[quote=&quot;(.*?)&quot;\](.*?)\[/quote\]#is";
170                $replacements[] = '<blockquote><span style="font-size: 11px; line-height: normal"><b>\\1 : </b><br/>\\2</span></blockquote>';
171
172                //Quotes with user
173                $patterns[] = "#\[quote=(.*?)\](.*?)\[/quote\]#is";
174                $replacements[] = '<blockquote><span style="font-size: 11px; line-height: normal"><b>\\1 : </b><br/>\\2</span></blockquote>';
175        }
176        if ( $conf_bbcode_bar[10] == 1 )
177        {
178                //Images
179                $patterns[] = "#\[img\](.*?)\[/img\]#si";
180                $replacements[] = '<img src="\\1" />';
181        }
182        if ( $conf_bbcode_bar[11] == 1 )
183        {
184                //[url]xxxx://www.zzzz.yyy[/url]
185                $patterns[] = "#\[url\]([\w]+?://[^ \"\n\r\t<]*?)\[/url\]#is"; 
186                $replacements[] = '<a href="\\1" target="_blank">\\1</a>'; 
187
188                //[url]www.zzzzz.yyy[/url]
189                $patterns[] = "#\[url\]((www|ftp)\.[^ \"\n\r\t<]*?)\[/url\]#is"; 
190                $replacements[] = '<a href="http://\\1" target="_blank">\\1</a>'; 
191
192                //[url=xxxx://www.zzzzz.yyy]ZzZzZ[/url]
193                $patterns[] = "#\[url=([\w]+?://[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is"; 
194                $replacements[] = '<a href="\\1" target="_blank">\\2</a>'; 
195
196                // [url=www.zzzzz.yyy]zZzZz[/url]
197                $patterns[] = "#\[url=((www|ftp)\.[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is"; 
198                $replacements[] = '<a href="http://\\1" target="_blank">\\2</a>'; 
199
200                // [url="www.zzzzz.yyy"]zZzZz[/url]
201                $patterns[] = "#\[url=&quot;((www|ftp)\.[^ \n\r\t<]*?)&quot;\](.*?)\[/url\]#is";
202                $replacements[] = '<a href="http://\\1" target="_blank">\\3</a>';
203
204                //[url="http://www.zzzzz.yyy"]zZzZz[/url]
205                $patterns[] = "#\[url=&quot;([\w]+?://[^ \n\r\t<]*?)&quot;\](.*?)\[/url\]#is";
206                $replacements[] = '<a href="\\1" target="_blank">\\2</a>';
207        }
208        if ( $conf_bbcode_bar[12] == 1 )
209        {
210                //[email]samvure@gmail.com[/email]
211                $patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#is";
212                $replacements[] = '<a href="mailto:\\1">\\1</a>';
213        }
214        if ( $conf_bbcode_bar[13] == 1 )
215        {
216                //Size
217                $patterns[] = "#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si";
218                $replacements[] = '<span style="font-size: \\1px; line-height: normal">\\2</span>';
219        }
220        if ( $conf_bbcode_bar[14] == 1 )
221        {
222                //Colours
223                $patterns[] = "#\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*?)\[/color\]#si";
224                $replacements[] = '<span style="color: \\1">\\2</span>';
225        }
226       
227        return preg_replace($patterns, $replacements, $str);
228}
229
230?>
Note: See TracBrowser for help on using the repository browser.