]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/cache_starred_images/init.php
plugins/cache_starred_images: use PDO
[tt-rss.git] / plugins / cache_starred_images / init.php
index 6c9e67b171f025c29aeea31120483a2c3a3ba4ad..6c32ff57f2f6b112411334680e7306ef9b64a97f 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 class Cache_Starred_Images extends Plugin implements IHandler {
 
+       /* @var PluginHost $host */
        private $host;
        private $cache_dir;
 
@@ -66,27 +67,11 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                if ($hash) {
 
                        $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') &&
-                                   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);
-                               }
+                               send_local_file($filename);
                        } else {
                                header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
                                echo "File not found.";
@@ -108,11 +93,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) {
@@ -151,7 +136,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
@@ -161,14 +146,16 @@ 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']]);
                                }
                        }
                }
@@ -209,7 +196,7 @@ class Cache_Starred_Images extends Plugin implements IHandler {
                                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;
                                        }