init.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. function csrf_ignore($method) {
  12. return false;
  13. }
  14. function before($method) {
  15. return true;
  16. }
  17. function after() {
  18. return true;
  19. }
  20. function init($host) {
  21. $this->host = $host;
  22. $this->cache_dir = CACHE_DIR . "/starred-images/";
  23. if (!is_dir($this->cache_dir)) {
  24. mkdir($this->cache_dir);
  25. }
  26. if (is_dir($this->cache_dir)) {
  27. if (!is_writable($this->cache_dir))
  28. chmod($this->cache_dir, 0777);
  29. if (is_writable($this->cache_dir)) {
  30. $host->add_hook($host::HOOK_UPDATE_TASK, $this);
  31. $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
  32. $host->add_hook($host::HOOK_SANITIZE, $this);
  33. $host->add_handler("public", "cache_starred_images_getimage", $this);
  34. } else {
  35. user_error("Starred cache directory is not writable.", E_USER_WARNING);
  36. }
  37. } else {
  38. user_error("Unable to create starred cache directory.", E_USER_WARNING);
  39. }
  40. }
  41. function cache_starred_images_getimage() {
  42. ob_end_clean();
  43. $hash = basename($_REQUEST["hash"]);
  44. if ($hash) {
  45. $filename = $this->cache_dir . "/" . basename($hash);
  46. $is_video = strpos($filename, ".mp4") !== FALSE;
  47. if (file_exists($filename)) {
  48. header("Content-Disposition: attachment; filename=\"$hash\"");
  49. /* See if we can use X-Sendfile */
  50. $xsendfile = false;
  51. if (function_exists('apache_get_modules') &&
  52. array_search('mod_xsendfile', apache_get_modules()))
  53. $xsendfile = true;
  54. if ($xsendfile) {
  55. header("X-Sendfile: $filename");
  56. header("Content-type: application/octet-stream");
  57. header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
  58. } else {
  59. header("Content-type: " . ($is_video ? "video/mp4" : "image/png"));
  60. $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
  61. header("Last-Modified: $stamp", true);
  62. readfile($filename);
  63. }
  64. } else {
  65. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  66. echo "File not found.";
  67. }
  68. }
  69. }
  70. function hook_house_keeping() {
  71. $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
  72. $last_article_id = 0;
  73. $article_exists = 1;
  74. foreach ($files as $file) {
  75. list ($article_id, $hash) = explode("-", basename($file));
  76. if ($article_id != $last_article_id) {
  77. $last_article_id = $article_id;
  78. $article_id = db_escape_string($article_id);
  79. $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
  80. $article_exists = db_num_rows($result) > 0;
  81. }
  82. if (!$article_exists) {
  83. unlink($file);
  84. }
  85. }
  86. }
  87. function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
  88. $xpath = new DOMXpath($doc);
  89. if ($article_id) {
  90. $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
  91. foreach ($entries as $entry) {
  92. if ($entry->hasAttribute('src')) {
  93. $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
  94. $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
  95. $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
  96. if (file_exists($local_filename)) {
  97. $entry->setAttribute("src", get_self_url_prefix() .
  98. "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
  99. $article_id . "-" . sha1($src) . $extension);
  100. }
  101. }
  102. }
  103. }
  104. return $doc;
  105. }
  106. function hook_update_task() {
  107. $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
  108. FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
  109. (ttrss_user_entries.feed_id = ttrss_feeds.id)
  110. WHERE ref_id = ttrss_entries.id AND
  111. marked = true AND
  112. (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
  113. site_url != '' AND
  114. plugin_data NOT LIKE '%starred_cache_images%'
  115. ORDER BY ".sql_random_function()." LIMIT 100");
  116. while ($line = db_fetch_assoc($result)) {
  117. if ($line["site_url"]) {
  118. $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
  119. if ($success) {
  120. $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
  121. db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
  122. }
  123. }
  124. }
  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')) {
  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) > 0) {
  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. }
  161. ?>