]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php
[add] HTML Purifier added to clean code
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / Injector / Linkify.php
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/Linkify.php
new file mode 100644 (file)
index 0000000..5eed3c1
--- /dev/null
@@ -0,0 +1,59 @@
+<?php\r
+\r
+/**\r
+ * Injector that converts http, https and ftp text URLs to actual links.\r
+ */\r
+class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector\r
+{\r
+    /**\r
+     * @type string\r
+     */\r
+    public $name = 'Linkify';\r
+\r
+    /**\r
+     * @type array\r
+     */\r
+    public $needed = array('a' => array('href'));\r
+\r
+    /**\r
+     * @param HTMLPurifier_Token $token\r
+     */\r
+    public function handleText(&$token)\r
+    {\r
+        if (!$this->allowsElement('a')) {\r
+            return;\r
+        }\r
+\r
+        if (strpos($token->data, '://') === false) {\r
+            // our really quick heuristic failed, abort\r
+            // this may not work so well if we want to match things like\r
+            // "google.com", but then again, most people don't\r
+            return;\r
+        }\r
+\r
+        // there is/are URL(s). Let's split the string:\r
+        // Note: this regex is extremely permissive\r
+        $bits = preg_split('#((?:https?|ftp)://[^\s\'",<>()]+)#Su', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);\r
+\r
+\r
+        $token = array();\r
+\r
+        // $i = index\r
+        // $c = count\r
+        // $l = is link\r
+        for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {\r
+            if (!$l) {\r
+                if ($bits[$i] === '') {\r
+                    continue;\r
+                }\r
+                $token[] = new HTMLPurifier_Token_Text($bits[$i]);\r
+            } else {\r
+                $token[] = new HTMLPurifier_Token_Start('a', array('href' => $bits[$i]));\r
+                $token[] = new HTMLPurifier_Token_Text($bits[$i]);\r
+                $token[] = new HTMLPurifier_Token_End('a');\r
+            }\r
+        }\r
+    }\r
+}\r
+\r
+// vim: et sw=4 sts=4\r