3 require_once 'HTMLPurifier/Injector.php';
6 * Injector that converts http, https and ftp text URLs to actual links.
8 class HTMLPurifier_Injector_Linkify
extends HTMLPurifier_Injector
11 function handleText(&$token, $config, &$context) {
12 $current_nesting =& $context->get('CurrentNesting');
13 // this snippet could be factored out
14 $definition = $config->getHTMLDefinition();
15 if (!empty($current_nesting)) {
16 $parent_token = array_pop($current_nesting);
17 $current_nesting[] = $parent_token;
18 $parent = $definition->info
[$parent_token->name
];
20 $parent = $definition->info_parent_def
;
22 if (!isset($parent->child
->elements
['a'])) {
23 // parent element does not allow link elements, don't bother
26 if (strpos($token->data
, '://') === false) {
27 // our really quick heuristic failed, abort
28 // this may not work so well if we want to match things like
32 // there is/are URL(s). Let's split the string:
34 $bits = preg_split('#((?:https?|ftp)://[^\s\'"<>()]+)#S', $token->data
, -1, PREG_SPLIT_DELIM_CAPTURE
);
41 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++
, $l = !$l) {
43 if ($bits[$i] === '') continue;
44 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
46 $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i]));
47 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
48 $token[] = new HTMLPurifier_Token_End('a');