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

Last change on this file since 20796 was 20796, checked in by mistic100, 11 years ago

add bbcode bar on contactform

File size: 7.5 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4// add BBCodeBar to textarea
5function set_bbcode_bar($prefilter='picture', $textarea_id='contentid')
6{
7  global $template, $conf, $pwg_loaded_plugins, $page;
8 
9  load_language('plugin.lang', dirname(__FILE__) . '/');
10  $conf_bbcode_bar = unserialize($conf['bbcode_bar']);
11 
12 
13  // buttons
14  $tpl_codes = array();
15  foreach (unserialize(BBcode_codes) as $key) 
16  {
17    $tpl_codes[$key] = (bool)$conf_bbcode_bar[$key];
18  }
19  $tpl_codes['smilies'] = isset($pwg_loaded_plugins['SmiliesSupport']);
20 
21 
22  // calculate separators between groups
23  $groups = array(
24    array('b','i','u','s'),
25    array('p','center','right','quote'),
26    array('ol','ul'),
27    array('img','url','email'),
28    array('size','color','smilies'),
29    );
30   
31  $tpl_groups = array();
32  $count = 0;
33  for ($i=0; $i<count($groups)-1; $i++)
34  {
35    $separator = false;
36    foreach ($groups[$i] as $code)
37    {
38      if ($tpl_codes[$code]) $count++;
39    }
40    if ($count>0)
41    {
42      foreach ($groups[$i+1] as $code)
43      {
44        if ($tpl_codes[$code]) $separator = true;
45      }
46    }
47    if ($separator)
48    {
49      $tpl_groups[$i] = true;
50      $count = 0;
51    }
52  }
53 
54  $template->assign(array(
55    'BBC' => $tpl_codes,
56    'SEP' => $tpl_groups,
57    'BBCODE_PATH' => BBcode_PATH,
58    'BBCODE_ID' => $textarea_id,
59    ));
60 
61  $template->set_prefilter($prefilter, 'set_bbcode_bar_prefilter');   
62
63  // smilies support > 2.3 ## must be parsed after bbcode_bar, because the javascript must be after bbc's one
64  if (isset($pwg_loaded_plugins['SmiliesSupport'])) 
65  {
66    set_smiliessupport($prefilter);
67  } 
68}
69
70function set_bbcode_bar_prefilter($content, &$smarty)
71{
72  $search = '#(<div id="guestbookAdd">|<div id="commentAdd">|<div class="contact">)#';
73  $replace = file_get_contents(BBcode_PATH.'/template/bbcode_bar.tpl').'$1';
74  return preg_replace($search, $replace, $content);
75}
76
77
78// check tags and eventually close malformed tags, return BBCoded String
79function CheckTags($str)
80{
81  //storage stack
82  $tags = array();
83
84  for ($pos = 0; $pos<strlen($str); $pos++)
85  {
86    if ($str{$pos} == '[')
87    {
88      $end_pos = strpos($str, ']', $pos);
89      $tag = substr($str, ++$pos, $end_pos-$pos);
90      //deals with tags which contains arguments (ie quote)
91      if ( ($equal_pos = strpos($tag, '=', 0)) !== FALSE)
92      $tag = substr($tag, 0, $equal_pos);
93      //check whether we have a defined tag or not.
94      if (in_array(strtolower($tag),unserialize(BBcode_codes)) || in_array(strtolower(substr($tag,1)),unserialize(BBcode_codes)))
95      {
96        //closing tag
97        if ($tag{0} == '/')
98        {
99          //cleaned tag
100          $tag = substr($tag, 1);   
101          $before_tag = substr($str, 0, $pos-1);
102          $after_tag = substr($str, $end_pos+1); 
103          //pop stack
104          while (($temp = array_pop($tags)))
105          {
106            if ($temp != $tag) {
107              $before_tag.='[/'.$temp.']';
108            } else {
109              $before_tag.='[/'.$tag.']';
110              break;
111            }
112          }
113          $end_pos += strlen($before_tag)+strlen($after_tag)-strlen($str);
114          $str = $before_tag.$after_tag;
115        } else { // push stack
116          array_push($tags,$tag);
117        }
118      }
119      $pos = $end_pos; 
120    }
121  }
122  // empty stack and closing tags
123  while ($temp = array_pop($tags))
124  {   
125    $str.='[/'.$temp.']';
126  }
127  return $str;
128}
129
130// return string, HTML version of BBCoded $str
131function BBCodeParse($str)
132{
133  global $conf;
134
135  $conf_bbcode_bar = unserialize($conf['bbcode_bar']);
136  $str = CheckTags(nl2br($str));
137
138  $patterns = array();
139  $replacements = array();
140
141  if ($conf_bbcode_bar['p'])
142  {
143    //Paragraph
144    $patterns[] = '#\[p\](.*?)\[/p\]#is';
145    $replacements[] = '<p>\\1</p>';
146  }
147  if ($conf_bbcode_bar['b'])
148  {
149    // Bold
150    $patterns[] = '#\[b\](.*?)\[/b\]#is';
151    $replacements[] = '<b>\\1</b>';
152  }
153  if ($conf_bbcode_bar['i'])
154  {
155    //Italic
156    $patterns[] = '#\[i\](.*?)\[/i\]#is';
157    $replacements[] = '<i>\\1</i>';
158  }
159  if ($conf_bbcode_bar['u'])
160  {
161    //Underline 
162    $patterns[] = '#\[u\](.*?)\[\/u\]#is';
163    $replacements[] = '<u>\\1</u>';
164  }
165  if ($conf_bbcode_bar['s'])
166  {
167    //Strikethrough
168    $patterns[] = '#\[s\](.*?)\[/s\]#is';
169    $replacements[] = '<s>\\1</s>';
170  }
171  if ($conf_bbcode_bar['center'])
172  {
173    //Center
174    $patterns[] = '#\[center\](.*?)\[/center\]#is';
175    $replacements[] = '<div align="center"><p>\\1</p></div>';
176  }
177  if ($conf_bbcode_bar['right'])
178  {
179    //Right
180    $patterns[] = '#\[right\](.*?)\[/right\]#is';
181    $replacements[] = '<div align="right"><p>\\1</p></div>';
182  }
183  if ($conf_bbcode_bar['ol'])
184  {
185    //Olist
186    $patterns[] = '#\[ol\](.*?)\[/ol\]#is';
187    $replacements[] = '<ol>\\1</ol>';
188  }
189  if ($conf_bbcode_bar['ul'])
190  {
191    //Ulist
192    $patterns[] = '#\[ul\](.*?)\[/ul\]#is';
193    $replacements[] = '<ul>\\1</ul>';
194  }
195  if ($conf_bbcode_bar['ol'] || $conf_bbcode_bar['ul'])
196  {
197    //List
198    $patterns[] = '#\[li\](.*?)\[/li\]#is';
199    $replacements[] = '<li>\\1</li>';
200  }
201  if ($conf_bbcode_bar['quote'])
202  {
203    // Quotes
204    $patterns[] = "#\[quote\](.*?)\[/quote\]#is";
205    $replacements[] = '<blockquote><span style="font-size:11px;line-height:normal">\\1</span></blockquote>';
206
207    //Quotes with "user"
208    $patterns[] = "#\[quote=&quot;(.*?)&quot;\](.*?)\[/quote\]#is";
209    $replacements[] = '<blockquote><span style="font-size:11px;line-height:normal"><b>\\1 : </b><br/>\\2</span></blockquote>';
210
211    //Quotes with user
212    $patterns[] = "#\[quote=(.*?)\](.*?)\[/quote\]#is";
213    $replacements[] = '<blockquote><span style="font-size:11px;line-height:normal"><b>\\1 : </b><br/>\\2</span></blockquote>';
214  }
215  if ($conf_bbcode_bar['img'])
216  {
217    //Images
218    $patterns[] = "#\[img\](.*?)\[/img\]#is";
219    $replacements[] = '<img src="\\1" />';
220  }
221  if ($conf_bbcode_bar['url'])
222  {
223    //[url]xxxx://www.zzzz.yyy[/url]
224    $patterns[] = "#\[url\]([\w]+?://[^ \"\n\r\t<]*?)\[/url\]#is"; 
225    $replacements[] = '<a href="\\1" target="_blank">\\1</a>'; 
226
227    //[url]www.zzzzz.yyy[/url]
228    $patterns[] = "#\[url\]((www|ftp)\.[^ \"\n\r\t<]*?)\[/url\]#is"; 
229    $replacements[] = '<a href="http://\\1" target="_blank">\\1</a>'; 
230
231    //[url=xxxx://www.zzzzz.yyy]ZzZzZ[/url]
232    $patterns[] = "#\[url=([\w]+?://[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is"; 
233    $replacements[] = '<a href="\\1" target="_blank">\\2</a>'; 
234
235    //[url=www.zzzzz.yyy]zZzZz[/url]
236    $patterns[] = "#\[url=((www|ftp)\.[^ \"\n\r\t<]*?)\](.*?)\[/url\]#is"; 
237    $replacements[] = '<a href="http://\\1" target="_blank">\\2</a>'; 
238
239    //[url="www.zzzzz.yyy"]zZzZz[/url]
240    $patterns[] = "#\[url=&quot;((www|ftp)\.[^ \n\r\t<]*?)&quot;\](.*?)\[/url\]#is";
241    $replacements[] = '<a href="http://\\1" target="_blank">\\3</a>';
242
243    //[url="http://www.zzzzz.yyy"]zZzZz[/url]
244    $patterns[] = "#\[url=&quot;([\w]+?://[^ \n\r\t<]*?)&quot;\](.*?)\[/url\]#is";
245    $replacements[] = '<a href="\\1" target="_blank">\\2</a>';
246  }
247  if ($conf_bbcode_bar['email'])
248  {
249    //[email]samvure@gmail.com[/email]
250    $patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#is";
251    $replacements[] = '<a href="mailto:\\1">\\1</a>';
252  }
253  if ($conf_bbcode_bar['size'])
254  {
255    //Size
256    $patterns[] = "#\[size=([1-2]?[0-9])\](.*?)\[/size\]#is";
257    $replacements[] = '<span style="font-size: \\1px; line-height: normal">\\2</span>';
258  }
259  if ($conf_bbcode_bar['color'])
260  {
261    //Colours
262    $patterns[] = "#\[color=(\#[0-9A-F]{6}|\#[0-9A-F]{3}|[a-z]+)\](.*?)\[/color\]#is";
263    $replacements[] = '<span style="color: \\1">\\2</span>';
264  }
265 
266  return preg_replace($patterns, $replacements, $str);
267}
268
269?>
Note: See TracBrowser for help on using the repository browser.