Implement working linkification, now, the real challenge is to get it to play nice...
[htmlpurifier.git] / library / HTMLPurifier / Injector / Linkify.php
blob19be2152aa9a55f60b9bfc805fc3ef7cf2e169c4
1 <?php
3 require_once 'HTMLPurifier/Injector.php';
5 /**
6 * Injector that converts http, https and ftp text URLs to actual links.
7 */
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];
19 } else {
20 $parent = $definition->info_parent_def;
22 if (!isset($parent->child->elements['a'])) {
23 // parent element does not allow link elements, don't bother
24 return;
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
29 // "google.com"
30 return;
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);
36 $token = array();
38 // $i = index
39 // $c = count
40 // $l = is link
41 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
42 if (!$l) {
43 if ($bits[$i] === '') continue;
44 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
45 } else {
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');