]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/cache_starred_images/init.php
debug logging system rework:
[tt-rss.git] / plugins / cache_starred_images / init.php
old mode 100644 (file)
new mode 100755 (executable)
index f1341a3..a1916e2
@@ -1,8 +1,10 @@
 <?php
 class Cache_Starred_Images extends Plugin implements IHandler {
 
+       /* @var PluginHost $host */
        private $host;
        private $cache_dir;
+    private $max_cache_attempts = 5; // per-article
 
        function about() {
                return array(1.0,
@@ -11,10 +13,16 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                        true);
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function csrf_ignore($method) {
                return false;
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function before($method) {
                return true;
        }
@@ -59,26 +67,12 @@ class Cache_Starred_Images extends Plugin implements IHandler {
 
                if ($hash) {
 
-                       $filename = $this->cache_dir . "/" . $hash;
-                       $is_video = strpos($filename, ".mp4") !== FALSE;
+                       $filename = $this->cache_dir . "/" . basename($hash);
 
                        if (file_exists($filename)) {
-                               /* See if we can use X-Sendfile */
-                               $xsendfile = false;
-                               if (function_exists('apache_get_modules') &&
-                                   array_search('mod_xsendfile', apache_get_modules()))
-                                       $xsendfile = true;
-
-                               if ($xsendfile) {
-                                       header("X-Sendfile: $filename");
-                                       header("Content-type: application/octet-stream");
-                                       header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
-                               } else {
-                                       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);
-                               }
+                               header("Content-Disposition: attachment; filename=\"$hash\"");
+
+                               send_local_file($filename);
                        } else {
                                header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
                                echo "File not found.";
@@ -86,8 +80,11 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                }
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedLocalVariable)
+        */
        function hook_house_keeping() {
-               $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
+               $files = glob($this->cache_dir . "/*.{png,mp4,status}", GLOB_BRACE);
 
                $last_article_id = 0;
                $article_exists = 1;
@@ -97,11 +94,11 @@ class Cache_Starred_Images extends Plugin implements IHandler {
 
                        if ($article_id != $last_article_id) {
                                $last_article_id = $article_id;
-                               $article_id = db_escape_string($article_id);
 
-                               $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
+                               $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?");
+                               $sth->execute([$article_id]);
 
-                               $article_exists = db_num_rows($result) > 0;
+                               $article_exists = $sth->fetch();
                        }
 
                        if (!$article_exists) {
@@ -110,6 +107,9 @@ 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);
 
@@ -137,7 +137,7 @@ class Cache_Starred_Images extends Plugin implements IHandler {
        }
 
        function hook_update_task() {
-               $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
+               $res = $this->pdo->query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
                        FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
                                (ttrss_user_entries.feed_id = ttrss_feeds.id)
                        WHERE ref_id = ttrss_entries.id AND
@@ -147,22 +147,49 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                                plugin_data NOT LIKE '%starred_cache_images%'
                        ORDER BY ".sql_random_function()." LIMIT 100");
 
-               while ($line = db_fetch_assoc($result)) {
+               $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
+
+               while ($line = $res->fetch()) {
                        if ($line["site_url"]) {
                                $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
 
                                if ($success) {
-                                       $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
+                                       $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
 
-                                       db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
+                                       $usth->execute([$plugin_data, $line['id']]);
                                }
                        }
                }
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function cache_article_images($content, $site_url, $owner_uid, $article_id) {
                libxml_use_internal_errors(true);
 
+               $status_filename = $this->cache_dir . $article_id . "-" . sha1($site_url) . ".status";
+
+               Debug::log("status: $status_filename", Debug::$LOG_EXTENDED);
+
+        if (file_exists($status_filename))
+            $status = json_decode(file_get_contents($status_filename), true);
+        else
+            $status = [];
+
+        $status["attempt"] += 1;
+
+        // only allow several download attempts for article
+        if ($status["attempt"] > $this->max_cache_attempts) {
+            Debug::log("too many attempts for $site_url", Debug::$LOG_VERBOSE);
+            return;
+        }
+
+        if (!file_put_contents($status_filename, json_encode($status))) {
+            user_error("unable to write status file: $status_filename", E_USER_WARNING);
+            return;
+        }
+
                $charset_hack = '<head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                </head>';
@@ -178,7 +205,8 @@ class Cache_Starred_Images extends Plugin implements IHandler {
 
                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'));
 
@@ -186,13 +214,16 @@ class Cache_Starred_Images extends Plugin implements IHandler {
 
                                $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
 
-                               //_debug("cache_images: downloading: $src to $local_filename");
+                               Debug::log("cache_images: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
 
                                if (!file_exists($local_filename)) {
-                                       $file_content = fetch_file_contents($src);
+                                       $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]);
+
+                                       if ($file_content) {
+                        if (strlen($file_content) > MIN_CACHE_FILE_SIZE) {
+                            file_put_contents($local_filename, $file_content);
+                        }
 
-                                       if ($file_content && strlen($file_content) > 0) {
-                                               file_put_contents($local_filename, $file_content);
                                                $success = true;
                                        }
                                } else {
@@ -208,4 +239,3 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                return 2;
        }
 }
-?>