init.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. class Cache_Starred_Images extends Plugin implements IHandler {
  3. /* @var PluginHost $host */
  4. private $host;
  5. private $cache_dir;
  6. function about() {
  7. return array(1.0,
  8. "Automatically cache Starred articles' images and HTML5 video files",
  9. "fox",
  10. true);
  11. }
  12. /**
  13. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  14. */
  15. function csrf_ignore($method) {
  16. return false;
  17. }
  18. /**
  19. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  20. */
  21. function before($method) {
  22. return true;
  23. }
  24. function after() {
  25. return true;
  26. }
  27. function init($host) {
  28. $this->host = $host;
  29. $this->cache_dir = CACHE_DIR . "/starred-images/";
  30. if (!is_dir($this->cache_dir)) {
  31. mkdir($this->cache_dir);
  32. }
  33. if (is_dir($this->cache_dir)) {
  34. if (!is_writable($this->cache_dir))
  35. chmod($this->cache_dir, 0777);
  36. if (is_writable($this->cache_dir)) {
  37. $host->add_hook($host::HOOK_UPDATE_TASK, $this);
  38. $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
  39. $host->add_hook($host::HOOK_SANITIZE, $this);
  40. $host->add_handler("public", "cache_starred_images_getimage", $this);
  41. } else {
  42. user_error("Starred cache directory is not writable.", E_USER_WARNING);
  43. }
  44. } else {
  45. user_error("Unable to create starred cache directory.", E_USER_WARNING);
  46. }
  47. }
  48. function cache_starred_images_getimage() {
  49. ob_end_clean();
  50. $hash = basename($_REQUEST["hash"]);
  51. if ($hash) {
  52. $filename = $this->cache_dir . "/" . basename($hash);
  53. if (file_exists($filename)) {
  54. header("Content-Disposition: attachment; filename=\"$hash\"");
  55. send_local_file($filename);
  56. } else {
  57. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  58. echo "File not found.";
  59. }
  60. }
  61. }
  62. /**
  63. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  64. */
  65. function hook_house_keeping() {
  66. $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
  67. $last_article_id = 0;
  68. $article_exists = 1;
  69. foreach ($files as $file) {
  70. list ($article_id, $hash) = explode("-", basename($file));
  71. if ($article_id != $last_article_id) {
  72. $last_article_id = $article_id;
  73. $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?");
  74. $sth->execute([$article_id]);
  75. $article_exists = $sth->fetch();
  76. }
  77. if (!$article_exists) {
  78. unlink($file);
  79. }
  80. }
  81. }
  82. /**
  83. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  84. */
  85. function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
  86. $xpath = new DOMXpath($doc);
  87. if ($article_id) {
  88. $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
  89. foreach ($entries as $entry) {
  90. if ($entry->hasAttribute('src')) {
  91. $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
  92. $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
  93. $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
  94. if (file_exists($local_filename)) {
  95. $entry->setAttribute("src", get_self_url_prefix() .
  96. "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
  97. $article_id . "-" . sha1($src) . $extension);
  98. }
  99. }
  100. }
  101. }
  102. return $doc;
  103. }
  104. function hook_update_task() {
  105. $res = $this->pdo->query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
  106. FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
  107. (ttrss_user_entries.feed_id = ttrss_feeds.id)
  108. WHERE ref_id = ttrss_entries.id AND
  109. marked = true AND
  110. (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
  111. site_url != '' AND
  112. plugin_data NOT LIKE '%starred_cache_images%'
  113. ORDER BY ".sql_random_function()." LIMIT 100");
  114. $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
  115. while ($line = $res->fetch()) {
  116. if ($line["site_url"]) {
  117. $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
  118. if ($success) {
  119. $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
  120. $usth->execute([$plugin_data, $line['id']]);
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  127. */
  128. function cache_article_images($content, $site_url, $owner_uid, $article_id) {
  129. libxml_use_internal_errors(true);
  130. $charset_hack = '<head>
  131. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  132. </head>';
  133. $doc = new DOMDocument();
  134. $doc->loadHTML($charset_hack . $content);
  135. $xpath = new DOMXPath($doc);
  136. $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
  137. $success = false;
  138. $has_images = false;
  139. foreach ($entries as $entry) {
  140. if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
  141. $has_images = true;
  142. $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
  143. $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
  144. $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
  145. //_debug("cache_images: downloading: $src to $local_filename");
  146. if (!file_exists($local_filename)) {
  147. $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]);
  148. if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) {
  149. file_put_contents($local_filename, $file_content);
  150. $success = true;
  151. }
  152. } else {
  153. $success = true;
  154. }
  155. }
  156. }
  157. return $success || !$has_images;
  158. }
  159. function api_version() {
  160. return 2;
  161. }
  162. }