]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/cache_starred_images/init.php
tunables:
[tt-rss.git] / plugins / cache_starred_images / init.php
index b851479c0622be9f0955f0ce227fea6e2384d0aa..527e088d584f2f75e5901d8de7bf6c68bbd6ed85 100644 (file)
@@ -6,15 +6,21 @@ class Cache_Starred_Images extends Plugin implements IHandler {
 
        function about() {
                return array(1.0,
-                       "Automatically cache images in Starred articles",
+                       "Automatically cache Starred articles' images and HTML5 video files",
                        "fox",
                        true);
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function csrf_ignore($method) {
                return false;
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function before($method) {
                return true;
        }
@@ -59,9 +65,12 @@ class Cache_Starred_Images extends Plugin implements IHandler {
 
                if ($hash) {
 
-                       $filename = $this->cache_dir . "/" . $hash . '.png';
+                       $filename = $this->cache_dir . "/" . basename($hash);
+                       $is_video = strpos($filename, ".mp4") !== FALSE;
 
                        if (file_exists($filename)) {
+                               header("Content-Disposition: attachment; filename=\"$hash\"");
+
                                /* See if we can use X-Sendfile */
                                $xsendfile = false;
                                if (function_exists('apache_get_modules') &&
@@ -73,7 +82,7 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                                        header("Content-type: application/octet-stream");
                                        header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
                                } else {
-                                       header("Content-type: image/png");
+                                       header("Content-type: " . ($is_video ? "video/mp4" : "image/png"));
                                        $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
                                        header("Last-Modified: $stamp", true);
                                        readfile($filename);
@@ -85,8 +94,11 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                }
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedLocalVariable)
+        */
        function hook_house_keeping() {
-               $files = glob($this->cache_dir . "/*.png");
+               $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
 
                $last_article_id = 0;
                $article_exists = 1;
@@ -109,22 +121,26 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                }
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
                $xpath = new DOMXpath($doc);
 
                if ($article_id) {
-                       $entries = $xpath->query('(//img[@src])');
+                       $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
 
                        foreach ($entries as $entry) {
                                if ($entry->hasAttribute('src')) {
                                        $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
 
-                                       $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png";
+                                       $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
+                                       $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
 
                                        if (file_exists($local_filename)) {
                                                $entry->setAttribute("src", get_self_url_prefix() .
                                                        "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
-                                                       $article_id . "-" . sha1($src));
+                                                       $article_id . "-" . sha1($src) . $extension);
                                        }
 
                                }
@@ -140,12 +156,11 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                                (ttrss_user_entries.feed_id = ttrss_feeds.id)
                        WHERE ref_id = ttrss_entries.id AND
                                marked = true AND
-                               UPPER(content) LIKE '%<IMG%' AND
+                               (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
                                site_url != '' AND
                                plugin_data NOT LIKE '%starred_cache_images%'
                        ORDER BY ".sql_random_function()." LIMIT 100");
 
-
                while ($line = db_fetch_assoc($result)) {
                        if ($line["site_url"]) {
                                $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
@@ -159,6 +174,9 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                }
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function cache_article_images($content, $site_url, $owner_uid, $article_id) {
                libxml_use_internal_errors(true);
 
@@ -170,24 +188,28 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                $doc->loadHTML($charset_hack . $content);
                $xpath = new DOMXPath($doc);
 
-               $entries = $xpath->query('(//img[@src])');
+               $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
 
                $success = false;
                $has_images = false;
 
                foreach ($entries as $entry) {
-                       if ($entry->hasAttribute('src')) {
+
+                       if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
+
                                $has_images = true;
                                $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
 
-                               $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png";
+                               $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
+
+                               $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
 
                                //_debug("cache_images: downloading: $src to $local_filename");
 
                                if (!file_exists($local_filename)) {
                                        $file_content = fetch_file_contents($src);
 
-                                       if ($file_content && strlen($file_content) > 0) {
+                                       if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) {
                                                file_put_contents($local_filename, $file_content);
                                                $success = true;
                                        }
@@ -204,4 +226,3 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                return 2;
        }
 }
-?>