]>
Commit | Line | Data |
---|---|---|
1 | <?php\r | |
2 | \r | |
3 | /**\r | |
4 | * Injector that converts http, https and ftp text URLs to actual links.\r | |
5 | */\r | |
6 | class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector\r | |
7 | {\r | |
8 | /**\r | |
9 | * @type string\r | |
10 | */\r | |
11 | public $name = 'Linkify';\r | |
12 | \r | |
13 | /**\r | |
14 | * @type array\r | |
15 | */\r | |
16 | public $needed = array('a' => array('href'));\r | |
17 | \r | |
18 | /**\r | |
19 | * @param HTMLPurifier_Token $token\r | |
20 | */\r | |
21 | public function handleText(&$token)\r | |
22 | {\r | |
23 | if (!$this->allowsElement('a')) {\r | |
24 | return;\r | |
25 | }\r | |
26 | \r | |
27 | if (strpos($token->data, '://') === false) {\r | |
28 | // our really quick heuristic failed, abort\r | |
29 | // this may not work so well if we want to match things like\r | |
30 | // "google.com", but then again, most people don't\r | |
31 | return;\r | |
32 | }\r | |
33 | \r | |
34 | // there is/are URL(s). Let's split the string:\r | |
35 | // Note: this regex is extremely permissive\r | |
36 | $bits = preg_split('#((?:https?|ftp)://[^\s\'",<>()]+)#Su', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);\r | |
37 | \r | |
38 | \r | |
39 | $token = array();\r | |
40 | \r | |
41 | // $i = index\r | |
42 | // $c = count\r | |
43 | // $l = is link\r | |
44 | for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {\r | |
45 | if (!$l) {\r | |
46 | if ($bits[$i] === '') {\r | |
47 | continue;\r | |
48 | }\r | |
49 | $token[] = new HTMLPurifier_Token_Text($bits[$i]);\r | |
50 | } else {\r | |
51 | $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i]));\r | |
52 | $token[] = new HTMLPurifier_Token_Text($bits[$i]);\r | |
53 | $token[] = new HTMLPurifier_Token_End('a');\r | |
54 | }\r | |
55 | }\r | |
56 | }\r | |
57 | }\r | |
58 | \r | |
59 | // vim: et sw=4 sts=4\r |