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

Last change on this file since 6326 was 6297, checked in by patdenice, 14 years ago

Compatible 2.1

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