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

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

remove SmiliesSupport version test, allow 3 chars hexadecimal colors

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