Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the URL matcher #59

Merged
merged 1 commit into from Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 115 additions & 0 deletions app/Hook/ClickableHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* @copyright 2006-2014, Miles Johnson - http://milesj.me
* @license https://github.com/milesj/decoda/blob/master/license.md
* @link http://milesj.me/code/php/decoda
*/

namespace App\Hook;

use Decoda\Hook\AbstractHook;

/**
* Converts URLs and emails (not wrapped in tags) into clickable links.
*/
class ClickableHook extends AbstractHook {

/**
* Matches a link or an email, and converts it to an anchor tag.
*
* @param string $content
* @return string
*/
public function beforeParse($content) {
$parser = $this->getParser();

// To make sure we won't parse links inside [url] or [img] tags, we'll first replace all urls/imgs with uniqids
// and keep them in this array, and restore them at the end, after parsing
$ignoredStrings = array();

// The tags we won't touch
// For example, neither [url="http://www.example.com"] nor [img]http://www.example.com[/img] will be replaced.
$ignoredTags = array('url', 'link', 'img', 'image');

$i = 0;
foreach ($ignoredTags as $tag) {
if (preg_match_all(sprintf('/\[%s[\s=\]].*?\[\/%s\]/is', $tag, $tag), $content, $matches, PREG_SET_ORDER)) {
$matches = array_unique(array_map(function($x) { return $x[0]; }, $matches));

foreach ($matches as $val) {
$uniqid = uniqid($i++);

$ignoredStrings[$uniqid] = $val;
$content = str_replace($val, $uniqid, $content);
}
}
}

if ($parser->hasFilter('Url')) {
$protocols = $parser->getFilter('Url')->getConfig('protocols');
$chars = preg_quote('-_=+|\;:&?/[]%,.!@#$*(){}"\'', '/');

$result = "";
foreach (explode(" ", $content) as $v) {
if (substr($v, 0, strlen("http://") === "http://") || substr($v, 0, strlen("https://")) === "https://") {
$result .= "[url]" . $v . "[/url]";
} else {
$result .= $v;
}
$result .= " ";
}

if (strlen($result) > 0) {
$result = substr($result, 0, -1);
}

$content = $result;
}

// Based on W3C HTML5 spec: https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
if ($parser->hasFilter('Email')) {
$pattern = '(:\/\/[\w\.\+]+:)?([a-z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*)';

$content = preg_replace_callback('/' . $pattern . '/i', array($this, '_emailCallback'), $content);
}

// We restore the tags we ommited
foreach ($ignoredStrings as $key => $val) {
$content = str_replace($key, $val, $content);
}

return $content;
}

/**
* Callback for email processing.
*
* @param array $matches
* @return string
*/
protected function _emailCallback($matches) {
// is like http://user:pass@domain.com ? Then we do not touch it.
if ($matches[1]) {
return $matches[0];
}

return $this->getParser()->getFilter('Email')->parse(array(
'tag' => 'email',
'attributes' => array()
), trim($matches[2]));
}

/**
* Callback for URL processing.
*
* @param array $matches
* @return string
*/
protected function _urlCallback($matches) {
return $this->getParser()->getFilter('Url')->parse(array(
'tag' => 'url',
'attributes' => array()
), trim($matches[1]));
}

}
3 changes: 3 additions & 0 deletions app/Shoutbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Illuminate\Database\Eloquent\Model;
use Decoda\Decoda;
use App\Hook\ClickableHook;

class Shoutbox extends Model
{
Expand All @@ -40,6 +41,8 @@ public static function getMessageHtml($message)
{
$code = new Decoda($message);
$code->defaults();
$code->removeHook('Clickable');
$code->addHook(new ClickableHook());
$code->setXhtml(false);
$code->setStrict(false);
$code->setLineBreaks(true);
Expand Down