function smarty_outputfilter_highlight($source, $smarty)
{
    $highlight = $_REQUEST['highlight'];
    $feature_referer_highlight = $GLOBALS['feature_referer_highlight'];
    // @todo remove globals
    if (isset($feature_referer_highlight) && ${$feature_referer_highlight} == 'y') {
        $refererhi = _refererhi();
        if ($refererhi !== null && !empty($refererhi)) {
            if ($highlight !== null && !empty($highlight)) {
                $highlight = $highlight . " " . $refererhi;
            } else {
                $highlight = $refererhi;
            }
        }
    }
    if (!isset($highlight) || empty($highlight)) {
        return $source;
    }
    $source = preg_replace_callback('~(?:<head>.*?</head>                          // head blocks
      |<div[^>]*nohighlight.*?</div>\\{\\*nohighlight  // div with nohightlight
      |<script[^>]+>.*?</script>                     // script blocks
      |onmouseover=(?:"[^"]*"|\'[^\']*\')            // onmouseover (user popup)
      |<[^>]*?>                                      // all html tags
      |(' . _enlightColor($highlight) . '))~xsi', '_enlightColor', $source);
    return $source;
}
function smarty_outputfilter_highlight($source, &$smarty)
{
    global $feature_referer_highlight;
    $highlight = $_REQUEST['highlight'];
    if (isset($feature_referer_highlight) && $feature_referer_highlight == 'y') {
        $refererhi = _refererhi();
        if (isset($refererhi) && !empty($refererhi)) {
            if (isset($highlight) && !empty($highlight)) {
                $highlight = $highlight . " " . $refererhi;
            } else {
                $highlight = $refererhi;
            }
        }
    }
    if (!isset($highlight) || empty($highlight)) {
        return $source;
    }
    $source = preg_replace_callback('~(?:<head>.*?</head>                          # head blocks
      |<div[^>]*nohighlight.*?</div>\\{\\*nohighlight  # div with nohightlight
      |<script[^>]+>.*?</script>                     # script blocks
      |onmouseover=(?:"[^"]*"|\'[^\']*\')            # onmouseover (user popup)
      |<[^>]*?>                                      # all html tags
      |(' . _enlightColor($highlight) . '))~xsi', '_enlightColor', $source);
    return $source;
}
function smarty_outputfilter_highlight($source, &$smarty)
{
    global $prefs;
    $highlight = $_REQUEST['highlight'];
    if (isset($prefs['feature_referer_highlight']) && $prefs['feature_referer_highlight'] == 'y') {
        $refererhi = _refererhi();
        if (isset($refererhi) && !empty($refererhi)) {
            if (isset($highlight) && !empty($highlight)) {
                $highlight = $highlight . " " . $refererhi;
            } else {
                $highlight = $refererhi;
            }
        }
    }
    if (!isset($highlight) || empty($highlight)) {
        return $source;
    }
    $matches = array();
    if (strstr($source, 'id="rightcolumn"')) {
        if (function_exists('mb_eregi')) {
            // UTF8 support enabled
            mb_eregi('^(.*\\s+id="centercolumn"[^>]*>)(.*)(<td[^>]*\\s+id="rightcolumn".*)$', $source, $matches);
        } else {
            // This may not work at all with UTF8 chars
            preg_match('~(.* id="centercolumn"[^>]*>)(.*)(<td[^>]* id="rightcolumn".*)~xsi', $source, $matches);
        }
    } elseif (function_exists('mb_eregi')) {
        if (!mb_eregi('^(.*\\s+id="centercolumn"[^>]*>)(.*)$', $source, $matches)) {
            return $source;
        }
    } elseif (!preg_match('~(.* id="centercolumn"[^>]*>)(.*)~xsi', $source, $matches)) {
        return $source;
    } else {
        $matches[3] = '';
    }
    // Avoid highlight parsing in unknown cases where $matches[2] is empty, which will result in an empty page.
    if ($matches[2] != '') {
        $source = preg_replace_callback('~(?:<head>.*</head>                          # head blocks
	      |<div[^>]*nohighlight.*</div><!--nohighlight--> # div with nohightlight
	      |<script[^>]+>.*</script>                     # script blocks
	      |<a[^>]*onmouseover.*onmouseout[^>]*>            # onmouseover (user popup)
	      |<[^>]*>                                      # all html tags
	      |(' . _enlightColor($highlight) . '))~xsiU', '_enlightColor', $matches[2]);
    }
    return $matches[1] . $source . $matches[3];
}
function smarty_outputfilter_highlight($source, &$smarty)
{
    global $feature_referer_highlight;
    $highlight = $_REQUEST['highlight'];
    if (isset($feature_referer_highlight) && $feature_referer_highlight == 'y') {
        $refererhi = _refererhi();
        if (isset($refererhi) && !empty($refererhi)) {
            if (isset($highlight) && !empty($highlight)) {
                $highlight = $highlight . " " . $refererhi;
            } else {
                $highlight = $refererhi;
            }
        }
    }
    if (!isset($highlight) || empty($highlight)) {
        return $source;
    }
    $beginTag = '<!--HIGHLIGHT BEGIN-->';
    $endTag = '<!--HIGHLIGHT END-->';
    $beginPosition = strpos($source, $beginTag);
    $endPosition = strpos($source, $endTag);
    //return "beginPosition: $beginPosition<br/>endPosition: $endPosition<br/>" . ((!empty($beginPosition) && !empty($endPosition)) ? "ok" : "nao");
    if (!empty($beginPosition) && !empty($endPosition)) {
        $begin = substr($source, 0, $beginPosition);
        $mid = substr($source, $beginPosition + strlen($beginTag), $endPosition - $beginPosition - strlen($beginTag));
        $end = substr($source, $endPosition + strlen($endTag));
    } else {
        // highlight não funciona mais sem as tags $beginTag e $endTag, pra evitar q ele seja chamado em vários níveis.
        return $source;
    }
    $source = '';
    // save memory
    global $enlightPattern;
    $enlightPattern = _enlightColor($highlight);
    // set colors
    $mid = preg_replace_callback('/>([^<>]+)</', '_findMatch', $mid);
    $mid = preg_replace_callback("/tooltip\\('[^']+?'\\)/", '_fixTooltip', $mid);
    return $begin . $mid . $end;
}
function smarty_outputfilter_highlight($source, $smarty)
{
    global $prefs;
    if (empty($_REQUEST['highlight'])) {
        return $source;
    }
    if (!strstr($source, 'id="tiki-center"')) {
        return $source;
    }
    $highlight = $_REQUEST['highlight'];
    if (isset($_REQUEST['boolean']) && ($_REQUEST['boolean'] == 'on' || $_REQUEST['boolean'] == 'y')) {
        $highlight = str_replace(array('(', ')', '*', '-', '"', '~', '<', '>'), ' ', $highlight);
    }
    if (isset($prefs['feature_referer_highlight']) && $prefs['feature_referer_highlight'] == 'y') {
        $refererhi = _refererhi();
        if (isset($refererhi) && !empty($refererhi)) {
            if (isset($highlight) && !empty($highlight)) {
                $highlight = $highlight . " " . $refererhi;
            } else {
                $highlight = $refererhi;
            }
        }
    }
    if (!isset($highlight) || empty($highlight)) {
        return $source;
    }
    $matches = array();
    $end = 0;
    if ($end = strrpos($source, 'id="col2"')) {
        $stop_pattern = '(<div[^>]*\\s+id="col2".*)';
    } elseif ($end = strrpos($source, 'id="col3"')) {
        $stop_pattern = '(<div[^>]*\\s+id="col3".*)';
    } else {
        $stop_pattern = '';
    }
    $result = false;
    if (function_exists('mb_eregi')) {
        // UTF8 support enabled
        $result = mb_eregi('^(.*\\s+id="tiki-center"[^>]*>)(.*)' . $stop_pattern . '$', $source, $matches);
    } else {
        // We do not fallback on the preg_match function, since it is limited by 'pcre.backtrack_limit' which is too low by default (100K)
        //  and this script will not be allowed to change its value on most systems
        //
        if (($start = strpos($source, 'id="tiki-center"')) > 0) {
            $matches = array($source, substr($source, 0, $start), $end > $start ? substr($source, $start, $end - $start) : substr($source, $start), $end > $start ? substr($source, $end) : '');
            $result = true;
        }
    }
    if (!$result) {
        return $source;
    }
    if (strlen($matches[2]) > ini_get('pcre.backtrack_limit')) {
        return $source;
    }
    if (!isset($matches[3])) {
        $matches[3] = '';
    }
    // Avoid highlight parsing in unknown cases where $matches[2] is empty, which will result in an empty page.
    if ($matches[2] != '') {
        $source = preg_replace_callback('~(?:<head>.*</head>                            # head blocks
		|<div[^>]*nohighlight.*</div><!--nohighlight--> # div with nohightlight
		|<script[^>]+>.*</script>                       # script blocks
		|<a[^>]*onmouseover.*onmouseout[^>]*>           # onmouseover (user popup)
		|<[^>]*>                                        # all html tags
		|(' . _enlightColor($highlight) . '))~xsiU', '_enlightColor', $matches[2]);
    }
    return $matches[1] . $source . $matches[3];
}