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