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

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

[extentions] BBCode Bar

  • uses markItUp!
File size: 6.4 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 = unserialize($conf['bbcode_bar']);
9
10        // buttons
11        foreach(unserialize(BBcode_codes) as $key) {
12                if ($conf_bbcode_bar[$key]) $template->assign('BBC_'.$key, true);
13        }
14       
15        // edit field has different id
16        // if (
17                // (isset($_GET['action']) AND $_GET['action'] == 'edit_comment')
18                // OR (isset($page['body_id']) AND $page['body_id'] == 'theCommentsPage')
19        // ) {
20                // $template->assign('form_name', 'editComment');
21        // } else {
22                // $template->assign('form_name', 'addComment');
23        // }
24        $template->assign('form_name', 'addComment');
25
26        $template->assign('BBCODE_PATH', BBcode_PATH);
27        $template->set_filename('bbcode_bar', dirname(__FILE__).'/template/bbcode_bar.tpl');
28        $template->parse('bbcode_bar', true);           
29
30        // smilies support ## must be parsed avec bbcode_bar, because the javascript must be after bbc's one
31        if (isset($pwg_loaded_plugins['SmiliesSupport']))
32        {
33                set_smiliessupport();
34        }       
35}
36
37
38//Check tags and eventually close malformed tags, return BBCoded String
39function CheckTags($str)
40{
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),unserialize(BBcode_codes)) || in_array(strtolower(substr($tag,1)),unserialize(BBcode_codes)))
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 = unserialize($conf['bbcode_bar']);
96        $str = CheckTags(nl2br($str));
97
98        $patterns = array();
99        $replacements = array();
100
101        if ($conf_bbcode_bar['p'])
102        {
103                //Paragraph
104                $patterns[] = '#\[p\](.*?)\[/p\]#is';
105                $replacements[] = '<p>\\1</p>';
106        }
107        if ($conf_bbcode_bar['b'])
108        {
109                // Bold
110                $patterns[] = '#\[b\](.*?)\[/b\]#is';
111                $replacements[] = '<b>\\1</b>';
112        }
113        if ($conf_bbcode_bar['i'])
114        {
115                //Italic
116                $patterns[] = '#\[i\](.*?)\[/i\]#is';
117                $replacements[] = '<i>\\1</i>';
118        }
119        if ($conf_bbcode_bar['u'])
120        {
121                //Underline     
122                $patterns[] = '#\[u\](.*?)\[\/u\]#is';
123                $replacements[] = '<u>\\1</u>';
124        }
125        if ($conf_bbcode_bar['s'])
126        {
127                //Strikethrough
128                $patterns[] = '#\[s\](.*?)\[/s\]#is';
129                $replacements[] = '<s>\\1</s>';
130        }
131        if ($conf_bbcode_bar['center'])
132        {
133                //Center
134                $patterns[] = '#\[center\](.*?)\[/center\]#is';
135                $replacements[] = '<div align="center"><p>\\1</p></div>';
136        }
137        if ($conf_bbcode_bar['right'])
138        {
139                //Right
140                $patterns[] = '#\[right\](.*?)\[/right\]#is';
141                $replacements[] = '<div align="right"><p>\\1</p></div>';
142        }
143        if ($conf_bbcode_bar['ol'])
144        {
145                //Olist
146                $patterns[] = '#\[ol\](.*?)\[/ol\]#is';
147                $replacements[] = '<ol>\\1</ol>';
148        }
149        if ($conf_bbcode_bar['ul'])
150        {
151                //Ulist
152                $patterns[] = '#\[ul\](.*?)\[/ul\]#is';
153                $replacements[] = '<ul>\\1</ul>';
154        }
155        if ($conf_bbcode_bar['ol'] || $conf_bbcode_bar['ul'])
156        {
157                //List
158                $patterns[] = '#\[li\](.*?)\[/li\]#is';
159                $replacements[] = '<li>\\1</li>';
160        }
161        if ($conf_bbcode_bar['quote'])
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['img'])
176        {
177                //Images
178                $patterns[] = "#\[img\](.*?)\[/img\]#si";
179                $replacements[] = '<img src="\\1" />';
180        }
181        if ($conf_bbcode_bar['url'])
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['email'])
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['size'])
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['color'])
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.