Links In Comments...

Hi Guys

 

I'm running Dolphin 7.1 something and wondered if there's any way to allow links in comments on photos, videos, blogs and articles... A bit like facebook so if someone posts as url in a comment it automatically gets parsed and turned into a link? If it could be done it would be a great help.

Cheers

Justin 

Quote · 12 Jan 2016

I am not aware of a way to achieve this in the administration area.
However, this functionality is standard in the current version 7.2 due to a few new functions in the /inc/utils.inc.php, namely bx_linkify and bx_linkify_html. It is possible to copy these two functions and put them in a custom template script file. On my 7.1 dev site I use hypernova template, and in the scripts folder there is a file called BxTemplCmtsView, I took the two functions from the utils.inc.php in 7.2, and the function from BxBaseCmtsView that is responsible for producing the comment text, and pasted them into the BxTemplCmtsView, inside the class declaration brackets. This was enough to linkify urls in the comment box. There is one caveat though, it is only supported by PHP 5.3.6 or higher.
The code is below:

function _getCommentBodyBox(&$a)
    {
        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
}

Let me know if you have any questions or problems.

Quote · 12 Jan 2016

Excellent! Thank you!
Sorry for the delay in replying. Been busy getting various things tweaked and installing the hypernova theme :)

Will give this a try now.

 

Thanks

Justin 

Quote · 20 Jan 2016

Ok, I've just copied the code you kindly shared above and put it into the BxTemplCmtsView file so that now looks like this:

<?php require_once( BX_DIRECTORY_PATH_BASE . 'scripts/BxBaseCmtsView.php' ); /** * @see BxDolCmts */ class BxTemplCmtsView extends BxBaseCmtsView { function _getCommentBodyBox(&$a) { 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 } function BxTemplCmtsView( $sSystem, $iId, $iInit = 1 ) { BxBaseCmtsView::BxBaseCmtsView( $sSystem, $iId, $iInit ); } }

I've just checked and we're running PHP V5.4.36 so that should be ok shouldn't it?

The only problem is it doesn't seem to work :(

 

Have I put it in the right place? I've cleared the cache etc.

Thanks

Justin 

Quote · 20 Jan 2016

Note: To test it, I'm posting a simple comment on a photo with just a url in it such as http://www.google.com and crossing my fingers that it turns it into a working link.

Quote · 20 Jan 2016

Hi NB :)
It's hard to see if the code is right the way it is displayed in this forum.
If you attach your original BxTemplCmtsView, I will paste the extra code where it needs to go, then attach it to a reply, and you can upload the altered file and try it.

--------------------------EDIT:

I just noticed that you have Hypernova installed, so I have attached the file from my install with the working linkify function. Remember to back up any files you are going to change.

--------------------------EDIT(2) - because I dont have sufficient permissions to reply again xD
I also noticed that while the URL is linkified when a comment is added, if the comment is edited, the linkification is removed. This is standard behaviour - stripping the HTML from database input/output for security reasons. Maybe this is something that you need to consider?

BxTemplCmtsView.php · 3.2K · 167 downloads
Quote · 20 Jan 2016

Hi 

Thanks for sending the file. It's still not working though. 

Here's the photo I tested the link on... See the latest comment

http://www.railwaymodellers.com/m/photos/view/Possible-Twig-For-Making-Trees

 

There must be something else that needs altering. I'm running Dolphin 7.1.4. I've not upgraded yet as there's compatibility issues with a couple of modules I use a lot.

 

Cheers

Justin 

Quote · 20 Jan 2016

Shame :/
Did you do the usual clear all cache and logout and back in fingy?
I can have a look if you want to PM some FTP details.

Quote · 20 Jan 2016

I got your PM Justin, and a cursory look shows no difference between your file and mine, however I did notice that your version is 7.1.4 and mine is 7.1.6. I will dig deeper when I get some free time and let you know what I find.

Quote · 20 Jan 2016

Thank you. Much appreciated. I'll have a look and see if I can patch mine up to 7.1.6 without messing up any modules and hacks!

Quote · 20 Jan 2016

All done.
There were two different echo commands, one with wordwrap, one without. As I am not using wrap on my site, the linkify was working, you are using wrap so I just had to linkify that echo also.
One small issue - if you edit a comment, it is reloaded via ajax, and the linkification is lost until the page is reloaded, at which point it comes back. Let me know if you have any more issues.
Paul.

Quote · 23 Jan 2016

You're a legend! Thank you! Not a clue about word wrap! What's that?

 

Can't thank you enough!

Quote · 23 Jan 2016

Wordwrap - open a new text file in notepad, paste a really long link into the file (or type a load of characters with no spacing in it.).
Under the view(?) menu in notepad, turn wordwrap on and off, you will see what it does.

Judging by the code, there is a backend option to wrap the text in the comment box - it seems you have this enabled.
You are welcome by the way, I'm glad it is working :)

Quote · 23 Jan 2016
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.