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

Last change on this file since 15762 was 14525, checked in by mistic100, 12 years ago

update for 2.4

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