<?php

require_once( BX_DIRECTORY_PATH_BASE . 'scripts/BxBaseCmtsView.php' );

/**
 * @see BxDolCmts
 */
class BxTemplCmtsView extends BxBaseCmtsView
{
    function BxTemplCmtsView( $sSystem, $iId, $iInit = 1 )
    {
        BxBaseCmtsView::BxBaseCmtsView( $sSystem, $iId, $iInit );
    }

    function _getCommentBodyBox(&$a)
    {
        //HACK linkify comments
        return '
                <td class="' . $this->_sStylePrefix . '-cont-l">&nbsp;</td>
                <td class="' . $this->_sStylePrefix . '-cont-m">' .
                    ($this->_aSystem['is_mood'] ? '<div class="cmt-mood">' . $a['cmt_mood'] . '</div>' : '') .
                    '<div class="cmt-body">' . ($this->iGlobUseTinyMCE || $this->isTagsAllowed() || strncasecmp($a['cmt_text'], '<object', 7) == 0 || strncasecmp($a['cmt_text'], '<iframe', 7) == 0 ? $this->bx_linkify_html($a['cmt_text']) : WordWrapStr($a['cmt_text'])) . '</div>
                </td>
                <td class="' . $this->_sStylePrefix . '-cont-r">&nbsp;</td>';
    }
    function bx_linkify($text, $sAttrs = '', $bHtmlSpecialChars = false)
{
    if ($bHtmlSpecialChars)
        $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');

    $re = "@\b((https?://)|(www\.))(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@";
    preg_match_all($re, $text, $matches, PREG_OFFSET_CAPTURE);

    $matches = $matches[0];

    $i = count($matches);
    while ($i--)
    {
        $url = $matches[$i][0];
        if (!preg_match('@^https?://@', $url))
            $url = 'http://'.$url;

        $text = substr_replace($text, '<a ' . $sAttrs . ' href="'.$url.'">'.$matches[$i][0].'</a>', $matches[$i][1], strlen($matches[$i][0]));
    }

    return $text;
}

/**
 * Wrap in A tag links in HTML string, which aren't wrapped in A tag yet
 * @param $sHtmlOrig - HTML string
 * @param $sAttrs - attributes string to add to the added A tag
 * @return modified HTML string, in case of errror original string is returned
 */
function bx_linkify_html($sHtmlOrig, $sAttrs = '') 
{
    if (!trim($sHtmlOrig))
        return $sHtmlOrig;

    $sId = 'bx-linkify-' . md5(microtime());
    $dom = new DOMDocument();
    @$dom->loadHTML('<?xml encoding="UTF-8"><div id="' . $sId . '">' . $sHtmlOrig . '</div>');
    $xpath = new DOMXpath($dom);

    foreach ($xpath->query('//text()') as $text) {
        $frag = $dom->createDocumentFragment();
        $frag->appendXML($this->bx_linkify($text->nodeValue, $sAttrs, true));
        $text->parentNode->replaceChild($frag, $text);
    }

    if (version_compare(PHP_VERSION, '5.3.6') >= 0)
        $s = $dom->saveHTML($dom->getElementById($sId));
    else
        $s = $dom->saveXML($dom->getElementById($sId), LIBXML_NOEMPTYTAG);

    if (false === $s) // in case of error return original string
        return $sHtmlOrig;

    if (false !== ($iPos = mb_strpos($s, '<html><body>')) && $iPos < mb_strpos($s, $sId))
        $s = mb_substr($s, $iPos + 12, -15); // strip <html><body> tags and everything before them

    return mb_substr($s, 54, -6); // strip added tags
}

//END HACK
}
