init.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. $is_video = strpos($filename, ".mp4") !== FALSE;
  53. if (file_exists($filename)) {
  54. header("Content-Disposition: attachment; filename=\"$hash\"");
  55. /* See if we can use X-Sendfile */
  56. $xsendfile = false;
  57. if (function_exists('apache_get_modules') &&
  58. array_search('mod_xsendfile', apache_get_modules()))
  59. $xsendfile = true;
  60. if ($xsendfile) {
  61. header("X-Sendfile: $filename");
  62. header("Content-type: application/octet-stream");
  63. header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
  64. } else {
  65. header("Content-type: " . ($is_video ? "video/mp4" : "image/png"));
  66. $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
  67. header("Last-Modified: $stamp", true);
  68. readfile($filename);
  69. }
  70. } else {
  71. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  72. echo "File not found.";
  73. }
  74. }
  75. }
  76. /**
  77. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  78. */
  79. function hook_house_keeping() {
  80. $files = glob($this->cache_dir . "/*.{png,mp4}", GLOB_BRACE);
  81. $last_article_id = 0;
  82. $article_exists = 1;
  83. foreach ($files as $file) {
  84. list ($article_id, $hash) = explode("-", basename($file));
  85. if ($article_id != $last_article_id) {
  86. $last_article_id = $article_id;
  87. $article_id = db_escape_string($article_id);
  88. $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
  89. $article_exists = db_num_rows($result) > 0;
  90. }
  91. if (!$article_exists) {
  92. unlink($file);
  93. }
  94. }
  95. }
  96. /**
  97. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  98. */
  99. function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
  100. $xpath = new DOMXpath($doc);
  101. if ($article_id) {
  102. $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
  103. foreach ($entries as $entry) {
  104. if ($entry->hasAttribute('src')) {
  105. $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
  106. $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
  107. $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
  108. if (file_exists($local_filename)) {
  109. $entry->setAttribute("src", get_self_url_prefix() .
  110. "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
  111. $article_id . "-" . sha1($src) . $extension);
  112. }
  113. }
  114. }
  115. }
  116. return $doc;
  117. }
  118. function hook_update_task() {
  119. $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
  120. FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
  121. (ttrss_user_entries.feed_id = ttrss_feeds.id)
  122. WHERE ref_id = ttrss_entries.id AND
  123. marked = true AND
  124. (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
  125. site_url != '' AND
  126. plugin_data NOT LIKE '%starred_cache_images%'
  127. ORDER BY ".sql_random_function()." LIMIT 100");
  128. while ($line = db_fetch_assoc($result)) {
  129. if ($line["site_url"]) {
  130. $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
  131. if ($success) {
  132. $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
  133. db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
  134. }
  135. }
  136. }
  137. }
  138. /**
  139. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  140. */
  141. function cache_article_images($content, $site_url, $owner_uid, $article_id) {
  142. libxml_use_internal_errors(true);
  143. $charset_hack = '<head>
  144. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  145. </head>';
  146. $doc = new DOMDocument();
  147. $doc->loadHTML($charset_hack . $content);
  148. $xpath = new DOMXPath($doc);
  149. $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
  150. $success = false;
  151. $has_images = false;
  152. foreach ($entries as $entry) {
  153. if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
  154. $has_images = true;
  155. $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
  156. $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
  157. $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
  158. //_debug("cache_images: downloading: $src to $local_filename");
  159. if (!file_exists($local_filename)) {
  160. $file_content = fetch_file_contents($src);
  161. if ($file_content && strlen($file_content) > 0) {
  162. file_put_contents($local_filename, $file_content);
  163. $success = true;
  164. }
  165. } else {
  166. $success = true;
  167. }
  168. }
  169. }
  170. return $success || !$has_images;
  171. }
  172. function api_version() {
  173. return 2;
  174. }
  175. }