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

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

use sprites, update admin page

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