friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/Injector/Linkify.php

60 lines
1.6 KiB
PHP
Raw Normal View History

2010-09-08 23:14:17 -04:00
<?php
/**
* Injector that converts http, https and ftp text URLs to actual links.
*/
class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector
{
2016-02-09 05:06:17 -05:00
/**
* @type string
*/
2010-09-08 23:14:17 -04:00
public $name = 'Linkify';
2016-02-09 05:06:17 -05:00
/**
* @type array
*/
2010-09-08 23:14:17 -04:00
public $needed = array('a' => array('href'));
2016-02-09 05:06:17 -05:00
/**
* @param HTMLPurifier_Token $token
*/
public function handleText(&$token)
{
if (!$this->allowsElement('a')) {
return;
}
2010-09-08 23:14:17 -04:00
if (strpos($token->data, '://') === false) {
// our really quick heuristic failed, abort
// this may not work so well if we want to match things like
// "google.com", but then again, most people don't
return;
}
// there is/are URL(s). Let's split the string:
// Note: this regex is extremely permissive
2016-02-09 05:06:17 -05:00
$bits = preg_split('#((?:https?|ftp)://[^\s\'",<>()]+)#Su', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
2010-09-08 23:14:17 -04:00
$token = array();
// $i = index
// $c = count
// $l = is link
for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
if (!$l) {
2016-02-09 05:06:17 -05:00
if ($bits[$i] === '') {
continue;
}
2010-09-08 23:14:17 -04:00
$token[] = new HTMLPurifier_Token_Text($bits[$i]);
} else {
$token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i]));
$token[] = new HTMLPurifier_Token_Text($bits[$i]);
$token[] = new HTMLPurifier_Token_End('a');
}
}
}
}
// vim: et sw=4 sts=4