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