Ignore:
Timestamp:
07/05/08 12:58:33 (4 years ago)
Author:
hverton
Message:

added code coloring for [code] bbcode sections, and an option to download the code
fixed a bug with pm read timestamps
registration now sends a pm to the webmaster when new user activation is set to admin-approval
user activation has been extended to work with both manual approvals and with email verification

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/forum_functions_include.php

    r1491 r1524  
    1414+----------------------------------------------------*/ 
    1515if (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; 
    1622 
    1723// add a poll vote to the database 
     
    313319} 
    314320 
    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 = ""; 
     321function _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 
     331function _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&amp;forum_id=".$current_message['forum_id']."&amp;thread_id=".$current_message['thread_id']."&amp;post_id=".$current_message['post_id']."&amp;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 
     381function _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 
     389function 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! 
    322402    $codeblocks = array(); 
    323  
    324     // Split off the [code] blocks to exclude them from BBcode parsing 
    325      
    326     // find the code [code] occurence 
    327     $i = strpos($rawmsg, "[code]"); 
    328  
    329     // loop through the message until all are found and processed 
    330     while ($i !== false) { 
    331         // strip the bit before the [code] BBcode, and add a placeholder 
    332         $message .= substr($rawmsg, 0, $i+6)."{**@@**}"; 
    333         // strip the processed bit 
    334         $rawmsg = substr($rawmsg, $i+6); 
    335         // find the end of the [code] block 
    336         $j = strpos($rawmsg, "[/code]"); 
    337         // if not found, add the remaining bit, a forced [/code], and stop processing 
    338         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 bit 
    345         $rawmsg = substr($rawmsg, $j); 
    346         // check if there are more code segments 
    347         $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 parsing 
    354     $rawmsg = $message; 
    355     $message = ""; 
    356403    $urlblocks = array(); 
    357404 
    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); 
    384410 
    385411    // detect and convert wikitags to wiki bbcodes if needed 
     
    393419            $replace[] = "\\1[wiki]\\2[/wiki]\\3"; 
    394420        } 
    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 
    401436    // 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("*", "&nbsp;", 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]; 
    434442            } 
    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); 
    444445        } 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; 
    452452} 
    453453?> 
Note: See TracChangeset for help on using the changeset viewer.