init.php 5.3 KB

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