init.php 5.0 KB

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