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

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

[extentions] BBcode Bar

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