Index: extensions/bbcode_bar/include/events.inc.php
===================================================================
--- extensions/bbcode_bar/bbcode_bar.inc.php	(revision 23278)
+++ extensions/bbcode_bar/include/events.inc.php	(revision 26076)
@@ -1,22 +1,51 @@
 <?php
-if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
-
-// add BBCodeBar to textarea
-function set_bbcode_bar($prefilter='picture', $textarea_id='contentid')
+defined('BBCODE_ID') or die('Hacking attempt!');
+
+function bbcode_bar_admin_menu($menu)
 {
-  global $template, $conf, $pwg_loaded_plugins, $page;
-  
-  load_language('plugin.lang', dirname(__FILE__) . '/');
-  $conf_bbcode_bar = unserialize($conf['bbcode_bar']);
-  
-  
-  // buttons
-  $tpl_codes = array();
-  foreach (unserialize(BBcode_codes) as $key) 
-  {
-    $tpl_codes[$key] = (bool)$conf_bbcode_bar[$key];
-  }
-  $tpl_codes['smilies'] = isset($pwg_loaded_plugins['SmiliesSupport']);
-  
+  $menu[] = array(
+    'NAME' => 'BBCode Bar',
+    'URL' => BBCODE_ADMIN,
+    );
+  return $menu;
+}
+
+function add_bbcode_bar() 
+{
+  global $page, $pwg_loaded_plugins, $template, $conf;
+  
+  if (script_basename() == 'picture') 
+  {
+    $prefilter = 'picture';
+    $textarea_id = 'contentid';
+  }
+  else if (isset($page['section']))
+  {
+    if (
+      script_basename() == 'index' and isset($pwg_loaded_plugins['Comments_on_Albums'])
+      and $page['section'] == 'categories' and isset($page['category'])
+      ) 
+    {
+      $prefilter = 'comments_on_albums';
+      $textarea_id = 'contentid';
+    }
+    else if ($page['section'] == 'guestbook') 
+    {
+      $prefilter = 'guestbook';
+      $textarea_id = 'contentid';
+    }
+    else if ($page['section'] == 'contact') 
+    {
+      $prefilter = 'contactform';
+      $textarea_id = 'cf_content';
+    }
+  }
+  
+  if (!isset($prefilter))
+  {
+    return;
+  }
+  
+  $conf['bbcode_bar']['smilies'] = isset($pwg_loaded_plugins['SmiliesSupport']);
   
   // calculate separators between groups
@@ -37,5 +66,5 @@
     foreach ($groups[$i] as $code)
     {
-      if ($tpl_codes[$code]) $count++;
+      if ($conf['bbcode_bar'][$code]) $count++;
     }
     if ($count>0)
@@ -43,5 +72,5 @@
       foreach ($groups[$i+1] as $code)
       {
-        if ($tpl_codes[$code]) $separator = true;
+        if ($conf['bbcode_bar'][$code]) $separator = true;
       }
     }
@@ -54,77 +83,14 @@
   
   $template->assign(array(
-    'BBC' => $tpl_codes,
-    'SEP' => $tpl_groups,
-    'BBCODE_PATH' => BBcode_PATH,
-    'BBCODE_ID' => $textarea_id,
+    'BBCODE_PATH' => BBCODE_PATH,
+    'BBCODE' => array(
+      'codes' => $conf['bbcode_bar'],
+      'separators' => $tpl_groups,
+      'textarea_id' => $textarea_id,
+      ),
     ));
-  
-  $template->set_prefilter($prefilter, 'set_bbcode_bar_prefilter');    
-
-  // smilies support > 2.3 ## must be parsed after bbcode_bar, because the javascript must be after bbc's one
-  if (isset($pwg_loaded_plugins['SmiliesSupport'])) 
-  {
-    set_smiliessupport($prefilter, $textarea_id);
-  }  
-}
-
-function set_bbcode_bar_prefilter($content, &$smarty)
-{
-  $search = '#(<div id="guestbookAdd">|<div id="commentAdd">|<div class="contact">)#';
-  $replace = file_get_contents(BBcode_PATH.'/template/bbcode_bar.tpl').'$1';
-  return preg_replace($search, $replace, $content);
-}
-
-
-// check tags and eventually close malformed tags, return BBCoded String
-function CheckTags($str)
-{
-  //storage stack
-  $tags = array();
-
-  for ($pos = 0; $pos<strlen($str); $pos++)
-  {
-    if ($str{$pos} == '[')
-    {
-      $end_pos = strpos($str, ']', $pos);
-      $tag = substr($str, ++$pos, $end_pos-$pos);
-      //deals with tags which contains arguments (ie quote)
-      if ( ($equal_pos = strpos($tag, '=', 0)) !== FALSE)
-      $tag = substr($tag, 0, $equal_pos);
-      //check whether we have a defined tag or not.
-      if (in_array(strtolower($tag),unserialize(BBcode_codes)) || in_array(strtolower(substr($tag,1)),unserialize(BBcode_codes)))
-      {
-        //closing tag
-        if ($tag{0} == '/')
-        {
-          //cleaned tag
-          $tag = substr($tag, 1);    
-          $before_tag = substr($str, 0, $pos-1);
-          $after_tag = substr($str, $end_pos+1);  
-          //pop stack
-          while (($temp = array_pop($tags)))
-          {
-            if ($temp != $tag) {
-              $before_tag.='[/'.$temp.']';
-            } else {
-              $before_tag.='[/'.$tag.']';
-              break;
-            }
-          }
-          $end_pos += strlen($before_tag)+strlen($after_tag)-strlen($str);
-          $str = $before_tag.$after_tag;
-        } else { // push stack
-          array_push($tags,$tag);
-        }
-      }
-      $pos = $end_pos;  
-    }
-  }
-  // empty stack and closing tags
-  while ($temp = array_pop($tags))
-  {    
-    $str.='[/'.$temp.']';
-  }
-  return $str;
+
+  $template->set_filename('bbcodebar', realpath(BBCODE_PATH.'/template/bbcode_bar.tpl'));
+  $template->parse('bbcodebar');
 }
 
@@ -134,11 +100,10 @@
   global $conf;
 
-  $conf_bbcode_bar = unserialize($conf['bbcode_bar']);
-  $str = CheckTags(nl2br($str));
+  $str = bbcode_checktags(nl2br($str));
 
   $patterns = array();
   $replacements = array();
 
-  if ($conf_bbcode_bar['p'])
+  if ($conf['bbcode_bar']['p'])
   {
     //Paragraph
@@ -146,5 +111,5 @@
     $replacements[] = '<p>\\1</p>';
   }
-  if ($conf_bbcode_bar['b'])
+  if ($conf['bbcode_bar']['b'])
   {
     // Bold
@@ -152,5 +117,5 @@
     $replacements[] = '<b>\\1</b>';
   }
-  if ($conf_bbcode_bar['i'])
+  if ($conf['bbcode_bar']['i'])
   {
     //Italic
@@ -158,5 +123,5 @@
     $replacements[] = '<i>\\1</i>';
   }
-  if ($conf_bbcode_bar['u'])
+  if ($conf['bbcode_bar']['u'])
   {
     //Underline  
@@ -164,5 +129,5 @@
     $replacements[] = '<u>\\1</u>';
   }
-  if ($conf_bbcode_bar['s'])
+  if ($conf['bbcode_bar']['s'])
   {
     //Strikethrough
@@ -170,17 +135,17 @@
     $replacements[] = '<s>\\1</s>';
   }
-  if ($conf_bbcode_bar['center'])
+  if ($conf['bbcode_bar']['center'])
   {
     //Center
     $patterns[] = '#\[center\](.*?)\[/center\]#is';
-    $replacements[] = '<div align="center"><p>\\1</p></div>';
-  }
-  if ($conf_bbcode_bar['right'])
+    $replacements[] = '<div align="center">\\1</div>';
+  }
+  if ($conf['bbcode_bar']['right'])
   {
     //Right
     $patterns[] = '#\[right\](.*?)\[/right\]#is';
-    $replacements[] = '<div align="right"><p>\\1</p></div>';
-  }
-  if ($conf_bbcode_bar['ol'])
+    $replacements[] = '<div align="right">\\1</div>';
+  }
+  if ($conf['bbcode_bar']['ol'])
   {
     //Olist
@@ -188,5 +153,5 @@
     $replacements[] = '<ol>\\1</ol>';
   }
-  if ($conf_bbcode_bar['ul'])
+  if ($conf['bbcode_bar']['ul'])
   {
     //Ulist
@@ -194,5 +159,5 @@
     $replacements[] = '<ul>\\1</ul>';
   }
-  if ($conf_bbcode_bar['ol'] || $conf_bbcode_bar['ul'])
+  if ($conf['bbcode_bar']['ol'] || $conf['bbcode_bar']['ul'])
   {
     //List
@@ -200,5 +165,5 @@
     $replacements[] = '<li>\\1</li>';
   }
-  if ($conf_bbcode_bar['quote'])
+  if ($conf['bbcode_bar']['quote'])
   {
     // Quotes
@@ -214,5 +179,5 @@
     $replacements[] = '<blockquote><span style="font-size:11px;line-height:normal"><b>\\1 : </b><br/>\\2</span></blockquote>';
   }
-  if ($conf_bbcode_bar['img'])
+  if ($conf['bbcode_bar']['img'])
   {
     //Images
@@ -220,5 +185,5 @@
     $replacements[] = '<img src="\\1" />';
   }
-  if ($conf_bbcode_bar['url'])
+  if ($conf['bbcode_bar']['url'])
   {
     //[url]xxxx://www.zzzz.yyy[/url]
@@ -246,5 +211,5 @@
     $replacements[] = '<a href="\\1" target="_blank">\\2</a>';
   }
-  if ($conf_bbcode_bar['email'])
+  if ($conf['bbcode_bar']['email'])
   {
     //[email]samvure@gmail.com[/email]
@@ -252,5 +217,5 @@
     $replacements[] = '<a href="mailto:\\1">\\1</a>';
   }
-  if ($conf_bbcode_bar['size'])
+  if ($conf['bbcode_bar']['size'])
   {
     //Size
@@ -258,5 +223,5 @@
     $replacements[] = '<span style="font-size: \\1px; line-height: normal">\\2</span>';
   }
-  if ($conf_bbcode_bar['color'])
+  if ($conf['bbcode_bar']['color'])
   {
     //Colours
@@ -267,4 +232,2 @@
   return preg_replace($patterns, $replacements, $str);
 }
-
-?>
