Changeset 1524 in ExiteCMS for trunk/includes/forum_functions_include.php
- Timestamp:
- 07/05/08 12:58:33 (4 years ago)
- File:
-
- 1 edited
-
trunk/includes/forum_functions_include.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/forum_functions_include.php
r1491 r1524 14 14 +----------------------------------------------------*/ 15 15 if (eregi("forum_functions_include.php", $_SERVER['PHP_SELF']) || !defined('INIT_CMS_OK')) die(); 16 17 $current_message = array(); 18 $codeblocks = array(); 19 $urlblocks = array(); 20 $blockcount = 0; 21 $raw_color_blocks = false; 16 22 17 23 // add a poll vote to the database … … 313 319 } 314 320 315 // parse the [code] sections in a post 316 function parsemessage($rawmsg, $smileys=true) { 317 318 global $settings, $db_prefix; 319 320 // temp message storage 321 $message = ""; 321 function _unhtmlentities($string) { 322 323 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string); 324 $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string); 325 // replace literal entities 326 $trans_tbl = get_html_translation_table(HTML_ENTITIES); 327 $trans_tbl = array_flip($trans_tbl); 328 return strtr($string, $trans_tbl); 329 } 330 331 function _parseubb_codeblock($matches) { 332 global $codeblocks, $blockcount, $current_message, $raw_color_blocks; 333 334 // empty code block? 335 if (trim($matches[2]) == "") { 336 // remove the code block entirely 337 return ""; 338 } 339 // trim the values passed 340 $matches[1] = trim(substr($matches[1],1)); 341 342 if ($raw_color_blocks == false) { 343 require_once PATH_GESHI."/geshi.php"; 344 $geshi =& new GeSHi("", ""); 345 // colorize the code 346 $geshi->set_language($matches[1]); 347 $geshi->set_header_type(GESHI_HEADER_DIV); 348 $geshi->set_tab_width(4); 349 $geshi->set_source(_unhtmlentities($matches[2])); 350 $matches[2] = $geshi->parse_code(); 351 } 352 353 if ($raw_color_blocks) { 354 $codeblocks[] = array($matches[2], $matches[1]); 355 return true; 356 } 357 358 // generate the linenumbers 359 $ln = ""; 360 $cnt = substr_count($matches[2], "\n")+1; 361 for ($i=1;$i<=$cnt;$i++) { 362 $ln .= $i."<br />"; 363 } 364 $id = count($codeblocks); 365 ++$blockcount; 366 $link = "<img src='".THEME."images/panel_on.gif' alt='' title='Toggle full code view' name='b_code_".$id."' onclick=\"javascript:flipOverflow('code_".$id."')\" />"; 367 $link .= "<div class='side' style='display:inline;'> <a href='".BASEDIR."getfile.php?type=fc&forum_id=".$current_message['forum_id']."&thread_id=".$current_message['thread_id']."&post_id=".$current_message['post_id']."&id=".$id."' title='download this ".$matches[1]." code'>download</a> </div>"; 368 $codeblocks[] = array(" 369 <div id='box_code_".$id."' class='codecontainer'> 370 <table class='codeblock' cellpadding='0' cellspacing='0'> 371 <tr style='padding:0px;margin:0px;'> 372 <td class='codenr'>".$ln."</td> 373 <td class='code'>".$matches[2]."</td> 374 </tr> 375 </table> 376 </div>".$link." 377 ", $matches[1]); 378 return "{**@".($id)."@**}"; 379 } 380 381 function _parseubb_urlblock($matches) { 382 global $urlblocks; 383 384 $urlblocks[] = array($matches[1]=="="?$matches[2]:substr($matches[1],1), $matches[2]); 385 return "{@@*".(count($urlblocks)-1)."*@@}"; 386 } 387 388 // message parser, strip [code] and [url] sections, parse for BBcode and smiley's, then insert the sections again 389 function parsemessage($msg_array) { 390 global $settings, $db_prefix, $codeblocks, $urlblocks, $current_message; 391 392 // validate the parameters 393 if (!is_array($msg_array) || !isset($msg_array['post_message']) || !isset($msg_array['post_smileys'])) { 394 return ""; 395 } 396 397 $current_message = $msg_array; 398 $rawmsg = $msg_array['post_message']; 399 $smileys = $msg_array['post_smileys']; 400 401 // make sure these are empty! 322 402 $codeblocks = array(); 323 324 // Split off the [code] blocks to exclude them from BBcode parsing325 326 // find the code [code] occurence327 $i = strpos($rawmsg, "[code]");328 329 // loop through the message until all are found and processed330 while ($i !== false) {331 // strip the bit before the [code] BBcode, and add a placeholder332 $message .= substr($rawmsg, 0, $i+6)."{**@@**}";333 // strip the processed bit334 $rawmsg = substr($rawmsg, $i+6);335 // find the end of the [code] block336 $j = strpos($rawmsg, "[/code]");337 // if not found, add the remaining bit, a forced [/code], and stop processing338 if ($j === false) {339 $message = str_replace("{**@@**}", $rawmsg, $message);340 break;341 }342 // store this code block (convert the & to prevent entity replacement upon display)343 $codeblocks[] = substr($rawmsg, 0, $j);344 // strip the processed bit345 $rawmsg = substr($rawmsg, $j);346 // check if there are more code segments347 $i = strpos($rawmsg, "[code]");348 }349 350 // any text left?351 if (strlen($rawmsg)) $message .= $rawmsg;352 353 // Split off the [url] blocks to exclude them from url parsing354 $rawmsg = $message;355 $message = "";356 403 $urlblocks = array(); 357 404 358 // find the code [url] occurence 359 $i = strpos($rawmsg, "[url"); 360 361 // loop through the message until all are found and processed 362 while ($i !== false) { 363 // strip the bit before the [url] BBcode, and add a placeholder 364 $message .= substr($rawmsg, 0, $i+4)."{@@**@@}"; 365 // strip the processed bit 366 $rawmsg = substr($rawmsg, $i+4); 367 // find the end of the [url] block 368 $j = strpos($rawmsg, "[/url]"); 369 // if not found, add the remaining bit, a forced [/url], and stop processing 370 if ($j === false) { 371 $message = str_replace("{@@**@@}", $rawmsg, $message); 372 break; 373 } 374 // store this url block 375 $urlblocks[] = substr($rawmsg, 0, $j); 376 // strip the processed bit 377 $rawmsg = substr($rawmsg, $j); 378 // check if there are more code segments 379 $i = strpos($rawmsg, "[url"); 380 } 381 382 // any text left? 383 if (strlen($rawmsg)) $message .= $rawmsg; 405 // strip CODE bbcode, optionally perform Geshi color coding 406 $rawmsg = preg_replace_callback('#\[code(=.*?)?\](.*?)([\r\n]*)\[/code\]#si', '_parseubb_codeblock', $rawmsg); 407 408 // strip URL bbcode 409 $rawmsg = preg_replace_callback('#\[url(=.*?)\](.*?)([\r\n]*)\[/url\]#si', '_parseubb_urlblock', $rawmsg); 384 410 385 411 // detect and convert wikitags to wiki bbcodes if needed … … 393 419 $replace[] = "\\1[wiki]\\2[/wiki]\\3"; 394 420 } 395 $message = preg_replace($search, $replace, $message); 396 } 397 398 // find remaining URL's in the text, and convert them to a href as well 399 $pattern = '#(^|[^\"=]{1})(https?://|ftp://|mailto:|news:)([^(,\s<>\[\]\)]+)([,\s\n<>\)]|$)#sme'; 400 $message = preg_replace($pattern,"'$1<a href=\'$2$3\' target=\'_blank\'>'.shortenlink('$2$3',83).'</a>$4'",$message); 421 $rawmsg = preg_replace($search, $replace, $rawmsg); 422 } 423 424 // parse the smileys in the message 425 if ($smileys) $rawmsg = parsesmileys($rawmsg); 426 // parse all ubbcode 427 $rawmsg = parseubb($rawmsg); 428 // convert any newlines to html <br> 429 $rawmsg = nl2br($rawmsg); 430 431 // re-insert the saved code blocks 432 foreach($codeblocks as $key => $codeblock) { 433 $rawmsg = str_replace("{**@".$key."@**}", $codeblock[0], $rawmsg); 434 } 435 401 436 // re-insert the saved url blocks 402 foreach($urlblocks as $urlblock) { 403 // find the first placeholder 404 $i = strpos($message, "{@@**@@}"); 405 $message = substr($message, 0, $i).$urlblock.substr($message, $i+8); 406 } 407 408 // parse the smileys in the message 409 if ($smileys) $message = parsesmileys($message); 410 // page all ubbcode 411 $message = parseubb($message); 412 // convert any newlines to html <br> 413 $message = nl2br($message); 414 415 // re-insert the saved code blocks 416 foreach($codeblocks as $codeblock) { 417 // split the codeblock to add linenumbers 418 $lines = explode("\n", stripinput($codeblock)); 419 // get rid of empty lines at the beginning and the end of the block 420 while (count($lines)>0 && trim($lines[0]) == "") { 421 array_shift($lines); 422 } 423 while (count($lines)>0 && trim($lines[count($lines)-1]) == "") { 424 array_pop($lines); 425 } 426 // only create a code block if there's code to display 427 if (count($lines) > 0) { 428 // check how wide the number column should be 429 $w = strlen(count($lines)); 430 // embedded div with the linenumbers 431 $codeblock = "<div><pre class='codenr'>"; 432 for($i=0;$i<count($lines);$i++) { 433 $codeblock .= str_replace("*", " ", str_pad($i+1, $w, "*", STR_PAD_LEFT))."\n"; 437 foreach($urlblocks as $key => $urlblock) { 438 if (isURL($urlblock[0])) { 439 // check if the URL is prefixed. If not, assume http:// 440 if (!eregi("^((https?|s?ftp|mailto|svn|cvs|callto|mms|skype)\:\/\/){1}", $urlblock[0])) { 441 $urlblock[0] = "http://".$urlblock[0]; 434 442 } 435 $codeblock .= "</pre></div><pre class='code'>"; 436 // add the lines back to the code div 437 foreach($lines as $nr => $line) { 438 $codeblock .= $line; 439 } 440 $codeblock .= "</pre>"; 441 // find the first placeholder 442 $i = strpos($message, "{**@@**}"); 443 $message = substr($message, 0, $i).$codeblock.substr($message, $i+8); 443 // convert it into a link 444 $rawmsg = str_replace("{@@*".$key."*@@}", "<a href='".$urlblock[0]."' alt='' target='_blank'>".$urlblock[1]."</a>", $rawmsg); 444 445 } else { 445 // no code, remove the code block entirely 446 $i = strpos($message, "{**@@**}"); 447 $message = substr($message, 0, $i-6).substr($message, $i+15); 448 } 449 } 450 // return the parsed message body 451 return $message; 446 // make the URL harmless 447 $rawmsg = str_replace("{@@*".$key."*@@}", "[url=".stripinput($urlblock[0])."]".stripinput($urlblock[1])."[/url]", $rawmsg); 448 } 449 } 450 451 return $rawmsg; 452 452 } 453 453 ?>
Note: See TracChangeset
for help on using the changeset viewer.
