File indexing completed on 2025-03-09 05:25:30
0001 <?php 0002 0003 /** 0004 * Injector that converts http, https and ftp text URLs to actual links. 0005 */ 0006 class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector 0007 { 0008 /** 0009 * @type string 0010 */ 0011 public $name = 'Linkify'; 0012 0013 /** 0014 * @type array 0015 */ 0016 public $needed = array('a' => array('href')); 0017 0018 /** 0019 * @param HTMLPurifier_Token $token 0020 */ 0021 public function handleText(&$token) 0022 { 0023 if (!$this->allowsElement('a')) { 0024 return; 0025 } 0026 0027 if (strpos($token->data, '://') === false) { 0028 // our really quick heuristic failed, abort 0029 // this may not work so well if we want to match things like 0030 // "google.com", but then again, most people don't 0031 return; 0032 } 0033 0034 // there is/are URL(s). Let's split the string. 0035 // We use this regex: 0036 // https://gist.github.com/gruber/249502 0037 // but with @cscott's backtracking fix and also 0038 // the Unicode characters un-Unicodified. 0039 $bits = preg_split( 0040 '/\\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', 0041 $token->data, -1, PREG_SPLIT_DELIM_CAPTURE); 0042 0043 0044 $token = array(); 0045 0046 // $i = index 0047 // $c = count 0048 // $l = is link 0049 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) { 0050 if (!$l) { 0051 if ($bits[$i] === '') { 0052 continue; 0053 } 0054 $token[] = new HTMLPurifier_Token_Text($bits[$i]); 0055 } else { 0056 $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i])); 0057 $token[] = new HTMLPurifier_Token_Text($bits[$i]); 0058 $token[] = new HTMLPurifier_Token_End('a'); 0059 } 0060 } 0061 } 0062 } 0063 0064 // vim: et sw=4 sts=4