4 * Injector that converts http, https and ftp text URLs to actual links.
6 class HTMLPurifier_Injector_Linkify
extends HTMLPurifier_Injector
11 public $name = 'Linkify';
16 public $needed = array('a' => array('href'));
19 * @param HTMLPurifier_Token $token
21 public function handleText(&$token)
23 if (!$this->allowsElement('a')) {
27 if (strpos($token->data
, '://') === false) {
28 // our really quick heuristic failed, abort
29 // this may not work so well if we want to match things like
30 // "google.com", but then again, most people don't
34 // there is/are URL(s). Let's split the string.
36 // https://gist.github.com/gruber/249502
37 // but with @cscott's backtracking fix and also
38 // the Unicode characters un-Unicodified.
40 '/\\b((?:[a-z][\\w\\-]+:(?:\\/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}\\/)(?:[^\\s()<>]|\\((?:[^\\s()<>]|(?:\\([^\\s()<>]+\\)))*\\))+(?:\\((?:[^\\s()<>]|(?:\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'".,<>?\x{00ab}\x{00bb}\x{201c}\x{201d}\x{2018}\x{2019}]))/iu',
41 $token->data
, -1, PREG_SPLIT_DELIM_CAPTURE
);
49 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++
, $l = !$l) {
51 if ($bits[$i] === '') {
54 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
56 $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i]));
57 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
58 $token[] = new HTMLPurifier_Token_End('a');