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

Last change on this file since 11217 was 10983, checked in by mistic100, 13 years ago

code cleanup

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