init.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. class Cache_Starred_Images extends Plugin {
  3. private $host;
  4. private $cache_dir;
  5. function about() {
  6. return array(1.0,
  7. "Automatically cache images in Starred articles",
  8. "fox",
  9. true);
  10. }
  11. function init($host) {
  12. $this->host = $host;
  13. $this->cache_dir = CACHE_DIR . "/starred-images/";
  14. if (!is_dir($this->cache_dir)) {
  15. mkdir($this->cache_dir);
  16. }
  17. if (is_dir($this->cache_dir)) {
  18. if (!is_writable($this->cache_dir))
  19. chmod($this->cache_dir, 0777);
  20. if (is_writable($this->cache_dir)) {
  21. $host->add_hook($host::HOOK_UPDATE_TASK, $this);
  22. $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
  23. $host->add_hook($host::HOOK_SANITIZE, $this);
  24. } else {
  25. user_error("Starred cache directory is not writable.", E_USER_WARNING);
  26. }
  27. } else {
  28. user_error("Unable to create starred cache directory.", E_USER_WARNING);
  29. }
  30. }
  31. function image() {
  32. $hash = basename($_REQUEST["hash"]);
  33. if ($hash) {
  34. $filename = $this->cache_dir . "/" . $hash . '.png';
  35. if (file_exists($filename)) {
  36. /* See if we can use X-Sendfile */
  37. $xsendfile = false;
  38. if (function_exists('apache_get_modules') &&
  39. array_search('mod_xsendfile', apache_get_modules()))
  40. $xsendfile = true;
  41. if ($xsendfile) {
  42. header("X-Sendfile: $filename");
  43. header("Content-type: application/octet-stream");
  44. header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
  45. } else {
  46. header("Content-type: image/png");
  47. $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
  48. header("Last-Modified: $stamp", true);
  49. ob_clean(); // discard any data in the output buffer (if possible)
  50. flush(); // flush headers (if possible)
  51. readfile($filename);
  52. }
  53. } else {
  54. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  55. echo "File not found.";
  56. }
  57. }
  58. }
  59. function hook_house_keeping() {
  60. $files = glob($this->cache_dir . "/*.png");
  61. $last_article_id = 0;
  62. $article_exists = 1;
  63. foreach ($files as $file) {
  64. list ($article_id, $hash) = explode("-", basename($file));
  65. if ($article_id != $last_article_id) {
  66. $last_article_id = $article_id;
  67. $article_id = db_escape_string($article_id);
  68. $result = db_query("SELECT id FROM ttrss_entries WHERE id = " . $article_id);
  69. $article_exists = db_num_rows($result) > 0;
  70. }
  71. if (!$article_exists) {
  72. unlink($file);
  73. }
  74. }
  75. }
  76. function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
  77. $xpath = new DOMXpath($doc);
  78. if ($article_id) {
  79. $entries = $xpath->query('(//img[@src])');
  80. foreach ($entries as $entry) {
  81. if ($entry->hasAttribute('src')) {
  82. $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
  83. $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png";
  84. if (file_exists($local_filename)) {
  85. $entry->setAttribute("src", get_self_url_prefix() .
  86. "/backend.php?op=pluginhandler&plugin=cache_starred_images&method=image&hash=" .
  87. $article_id . "-" . sha1($src));
  88. }
  89. }
  90. }
  91. }
  92. return $doc;
  93. }
  94. function hook_update_task() {
  95. $result = db_query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
  96. FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
  97. (ttrss_user_entries.feed_id = ttrss_feeds.id)
  98. WHERE ref_id = ttrss_entries.id AND
  99. marked = true AND
  100. UPPER(content) LIKE '%<IMG%' AND
  101. site_url != '' AND
  102. plugin_data NOT LIKE '%starred_cache_images%'
  103. ORDER BY ".sql_random_function()." LIMIT 100");
  104. while ($line = db_fetch_assoc($result)) {
  105. if ($line["site_url"]) {
  106. $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
  107. if ($success) {
  108. $plugin_data = db_escape_string("starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"]);
  109. db_query("UPDATE ttrss_entries SET plugin_data = '$plugin_data' WHERE id = " . $line["id"]);
  110. }
  111. }
  112. }
  113. }
  114. function cache_article_images($content, $site_url, $owner_uid, $article_id) {
  115. libxml_use_internal_errors(true);
  116. $charset_hack = '<head>
  117. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  118. </head>';
  119. $doc = new DOMDocument();
  120. $doc->loadHTML($charset_hack . $content);
  121. $xpath = new DOMXPath($doc);
  122. $entries = $xpath->query('(//img[@src])');
  123. $success = false;
  124. $has_images = false;
  125. foreach ($entries as $entry) {
  126. if ($entry->hasAttribute('src')) {
  127. $has_images = true;
  128. $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
  129. $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . ".png";
  130. //_debug("cache_images: downloading: $src to $local_filename");
  131. if (!file_exists($local_filename)) {
  132. $file_content = fetch_file_contents($src);
  133. if ($file_content && strlen($file_content) > 0) {
  134. file_put_contents($local_filename, $file_content);
  135. $success = true;
  136. }
  137. } else {
  138. $success = true;
  139. }
  140. }
  141. }
  142. return $success || !$has_images;
  143. }
  144. function api_version() {
  145. return 2;
  146. }
  147. }
  148. ?>