function coolcode_run(&$message, &$coolcode_matches, $allow_mycode = 'yes') { if ($allow_mycode == 'no' || empty($coolcode_matches)) { return; } global $lang; $langvars = array('c' => 'C Code', 'cpp' => 'C++ Code', 'java' => 'Java Code', 'js' => 'Javascript Code', 'bat' => 'Batch Script', 'sql' => 'SQL Code', 'vb' => 'Visual Basic Code', 'html' => 'HTML Code', 'xml' => 'XML Code', 'php' => $lang->php_code, 'ini' => 'INI', 'code' => $lang->code); require_once MYBB_ROOT . 'inc/highlighter.php'; $codes = array(); foreach ($coolcode_matches as $text) { $code = trim(strtr($text[3], array("" => '', "" => '')), "\n\r"); /* $lines = substr_count($code, "\n"); if(!isset($text[2]{1})) $start = 1; else { $start = intval(substr($text[2],1)); if($start < 1) $start = 1; } $linetext = implode("\n", range($start, $start+$lines)); */ $text[1] = strtolower($text[1]); if ($text[1] != 'code') { $func = $text[1] . '_highlight'; // disallow use of our special characters $code .= "\n"; $code = str_replace(get_placeholder(8), 'class', substr($func($code), 0, -1)); // add & strip extra newline } else { $code = htmlspecialchars_uni($code); } /* $codes[] = "</p>\n<div class=\"code_header\">{$langvars[$text[1]]}\n</div> <div class=\"code_body\" style=\"padding: 0; margin: 0;\"> <table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" style=\"padding: 0;\"> <tr><td style=\"text-align: right; width: 4em;\"><div style=\"font-family: courier new, monospaced; line-height: 1.2em;\"><pre style=\"margin: 0;\">$linetext</pre></div></td> <td><div style=\"font-family: courier new, monospaced; line-height: 1.2em;\"><pre style=\"margin: 0;\">$code</pre></div></td></tr></table></div>\n<p>\n";*/ $codes[] = "</p>\n<div><div class=\"code_header\"><input type=\"button\" class=\"button\" style=\"float: right; margin-top: -2px;\" value=\"Select\" onClick=\"selectObj(this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('pre')[0])\" />{$langvars[$text[1]]}\n</div>\n\n <div class=\"code_body\" style=\"line-height: 1.25em;\">\n\n <pre>{$code}</pre></div></div>\n<p>\n"; //$message = preg_replace("#\x00#", $code, $message, 1); } $message = our_str_replace("", $codes, $message); }
function generic_highlight(&$code, $blocks = array(), $secondaries = array(), $tertiaries = array(), $keywords = array(), $callback = '') { $placeholders = array(); for ($i = 0; $i < 10; $i++) { $placeholders[$i] = get_placeholder($i); } // this will parse out "block" codes, ie comments and quotes $block_matches = array(); $replaced_blocks = array(); //$key_map = array(); foreach ($blocks as $key => $binfo) { //$binfo['key'] = $key; preg_match_all($binfo['pattern'], $code, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); foreach ($matches as $match) { $block_matches[$match[0][1]] = array(0 => $key, 1 => $match); //$key_map[$match[0][1]] = $key; } } ksort($block_matches); // now that we have the sorted array, perform replacements $offset = 0; $codetemp = $code; $code = ''; //$count = 0; do { $redo = false; foreach ($block_matches as $pos => $minfo) { $text =& $minfo[1][0][0]; $binfo =& $blocks[$minfo[0]]; // is this a "passed" block? if ($pos < $offset) { // if the close of the block is also passed, skip the whole thing // -OR there's no more matches if ($pos + strlen($text) < $offset || !preg_match($binfo['pattern'], $codetemp)) { unset($block_matches[$pos]); continue; } // otherwise, we'll have to try and re-match everything :( preg_match_all($binfo['pattern'], $codetemp, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); //$code .= $codetemp; // remove all current blocks for this pattern $block_matches = array_filter($block_matches, create_function('$a', 'return $a[0]!=\'' . $minfo[0] . '\';')); //foreach($block_matches as $pos2 => $minfo2) // if($minfo2[0] == $minfo[0]) // unset($block_matches[$pos2]); // re-assign matches foreach ($matches as $match) { $block_matches[$match[0][1] + $offset] = array(0 => $minfo[0], 1 => $match); } ksort($block_matches); // restart the whole process $redo = true; //$offset = 0; //$codetemp = $code; //$code = ''; break; } if (isset($binfo['replacement'])) { $replaced_blocks[] = preg_replace($binfo['pattern'], $binfo['replacement'], $text); } elseif (isset($binfo['soffset']) || isset($binfo['eoffset'])) { $replaced_blocks[] = offset_join_htmlspecialchars($text, $binfo['prefix'], $binfo['suffix'], $binfo['soffset'], $binfo['eoffset']); } else { $replaced_blocks[] = $binfo['prefix'] . htmlspecialchars($text) . $binfo['suffix']; } $ph = $placeholders[0]; if (isset($binfo['keepprefix'])) { $ph = $minfo[1][$binfo['keepprefix']][0] . $ph; } if (isset($binfo['keepsuffix'])) { $ph .= $minfo[1][$binfo['keepsuffix']][0]; } $code .= substr($codetemp, 0, $pos - $offset) . $ph; $len = strlen($text); $codetemp = substr($codetemp, $pos + $len - $offset); $offset = $pos + $len; //$code .= substr($codetemp, $offset, $pos-$offset).$placeholders[0]; //$offset = $pos + strlen($text); //$len = strlen($text); //$code = substr_replace($code, $placeholders[0], $pos-$offset, $len); //$offset += $len - strlen($placeholders[0]); unset($block_matches[$pos]); // we're done with this block } } while ($redo); //$code .= substr($codetemp, $offset); $code .= $codetemp; //unset($codetemp); unset($block_matches); $i = -1; $secondary_matches = array(); if ($callback) { $callback($code, $secondary_matches); $i = count($secondary_matches) - 1; } foreach ($secondaries as $secondary) { preg_match_all($secondary['pattern'], $code, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); $codetemp = $code; $offset = 0; $code = ''; ++$i; foreach ($matches as $match) { if (isset($secondary['replacement'])) { $secondary_matches[$i][] = preg_replace($secondary['pattern'], $secondary['replacement'], $match[0][0]); } elseif (isset($secondary['soffset']) || isset($secondary['eoffset'])) { $secondary_matches[$i][] = offset_join_htmlspecialchars($match[0][0], $secondary['prefix'], $secondary['suffix'], $secondary['soffset'], $secondary['eoffset']); } else { $secondary_matches[$i][] = $secondary['prefix'] . htmlspecialchars($match[0][0]) . $secondary['suffix']; } $ph = $placeholders[$i + 1]; if (isset($secondary['keepprefix'])) { $ph = $match[$secondary['keepprefix']][0] . $ph; } if (isset($secondary['keepsuffix'])) { $ph .= $match[$secondary['keepsuffix']][0]; } //$code .= substr($codetemp, 0, $match[0][1]-$offset).$ph; //$len = strlen($match[0][0]); //$codetemp = substr($codetemp, $match[0][1]+$len - $offset); //$offset = $match[0][1] + $len; $code .= substr($codetemp, $offset, $match[0][1] - $offset) . $ph; $offset = $match[0][1] + strlen($match[0][0]); //$len = strlen($match[0][0]); //$code = substr_replace($code, $ph, $match[0][1]-$offset, $len); //$offset += $len - strlen($placeholders[$i+1]); } //$code .= $codetemp; $code .= substr($codetemp, $offset); } //$code .= $codetemp; unset($codetemp); $code = htmlspecialchars($code); foreach ($tertiaries as $tertiary) { $code = preg_replace($tertiary['pattern'], $tertiary['replacement'], $code); } foreach ($keywords as $kinfo) { if (isset($kinfo['case']) && $kinfo['case']) { $func = 'strpos'; } else { $func = 'stripos'; } if (isset($kinfo['bsfunc'])) { $bsfunc = $kinfo['bsfunc']; } else { $bsfunc = 'is_variablechar'; } if (isset($kinfo['befunc'])) { $befunc = $kinfo['befunc']; } else { $befunc = 'is_variablechar'; } foreach ($kinfo['keywords'] as $word) { $codetemp = $code; $code = ''; while (($pos = $func($codetemp, $word)) !== false) { $endpos = strlen($word) + $pos; // below line is required to preserve case $myword = substr($codetemp, $pos, strlen($word)); // check boundaries //die(var_dump(array($codetemp{$pos-1}, $codetemp{$endpos}, $pos))); if ((!$pos || !$bsfunc($codetemp[$pos - 1])) && (!isset($codetemp[$endpos]) || !$befunc($codetemp[$endpos]))) { $code .= substr($codetemp, 0, $pos) . $kinfo['prefix'] . $myword . $kinfo['suffix']; } else { $code .= substr($codetemp, 0, $pos) . $myword; } $codetemp = substr($codetemp, $endpos); } $code .= $codetemp; } } ++$i; while ($i--) { if (!empty($secondary_matches[$i])) { $code = our_str_replace($placeholders[$i + 1], $secondary_matches[$i], $code); } } // replace back quotes/comments etc if (!empty($replaced_blocks)) { $code = our_str_replace($placeholders[0], $replaced_blocks, $code); } return $code; }