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

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

[extentions] BBCode Bar

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