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