init.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. class Af_Readability extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Try to inline article content using Readability",
  7. "fox");
  8. }
  9. function save() {
  10. //
  11. }
  12. function init($host)
  13. {
  14. $this->host = $host;
  15. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  16. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  17. $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
  18. $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
  19. }
  20. function hook_prefs_tab($args) {
  21. if ($args != "prefFeeds") return;
  22. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_readability settings')."\">";
  23. print_notice("Enable the plugin for specific feeds in the feed editor.");
  24. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  25. if (!array($enabled_feeds)) $enabled_feeds = array();
  26. $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
  27. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  28. if (count($enabled_feeds) > 0) {
  29. print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
  30. print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
  31. foreach ($enabled_feeds as $f) {
  32. print "<li>" .
  33. "<img src='images/pub_set.png'
  34. style='vertical-align : middle'> <a href='#'
  35. onclick='editFeed($f)'>".
  36. getFeedTitle($f) . "</a></li>";
  37. }
  38. print "</ul>";
  39. }
  40. print "</div>";
  41. }
  42. function hook_prefs_edit_feed($feed_id) {
  43. print "<div class=\"dlgSec\">".__("Readability")."</div>";
  44. print "<div class=\"dlgSecCont\">";
  45. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  46. if (!array($enabled_feeds)) $enabled_feeds = array();
  47. $key = array_search($feed_id, $enabled_feeds);
  48. $checked = $key !== FALSE ? "checked" : "";
  49. print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
  50. name=\"af_readability_enabled\"
  51. $checked>&nbsp;<label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
  52. print "</div>";
  53. }
  54. function hook_prefs_save_feed($feed_id) {
  55. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  56. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  57. $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]) == 'true';
  58. $key = array_search($feed_id, $enabled_feeds);
  59. if ($enable) {
  60. if ($key === FALSE) {
  61. array_push($enabled_feeds, $feed_id);
  62. }
  63. } else {
  64. if ($key !== FALSE) {
  65. unset($enabled_feeds[$key]);
  66. }
  67. }
  68. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  69. }
  70. function hook_article_filter($article) {
  71. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  72. $key = array_search($article["feed"]["id"], $enabled_feeds);
  73. if ($key === FALSE) return $article;
  74. if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
  75. if (function_exists("curl_init")) {
  76. $ch = curl_init($article["link"]);
  77. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  79. curl_setopt($ch, CURLOPT_HEADER, true);
  80. curl_setopt($ch, CURLOPT_NOBODY, true);
  81. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
  82. !ini_get("safe_mode") && !ini_get("open_basedir"));
  83. curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
  84. @$result = curl_exec($ch);
  85. $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  86. if (strpos($content_type, "text/html") === FALSE)
  87. return $article;
  88. }
  89. $tmp = fetch_file_contents($article["link"]);
  90. if ($tmp) {
  91. $tmpdoc = new DOMDocument("1.0", "UTF-8");
  92. if (!$tmpdoc->loadHTML($tmp))
  93. return $article;
  94. if ($tmpdoc->encoding != 'UTF-8') {
  95. $tmpxpath = new DOMXPath($tmpdoc);
  96. foreach ($tmpxpath->query("//meta") as $elem) {
  97. $elem->parentNode->removeChild($elem);
  98. }
  99. $tmp = $tmpdoc->saveHTML();
  100. }
  101. $r = new Readability($tmp, $article["link"]);
  102. if ($r->init()) {
  103. $tmpxpath = new DOMXPath($r->dom);
  104. $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
  105. foreach ($entries as $entry) {
  106. if ($entry->hasAttribute("href")) {
  107. $entry->setAttribute("href",
  108. rewrite_relative_url($article["link"], $entry->getAttribute("href")));
  109. }
  110. if ($entry->hasAttribute("src")) {
  111. $entry->setAttribute("src",
  112. rewrite_relative_url($article["link"], $entry->getAttribute("src")));
  113. }
  114. }
  115. $article["content"] = $r->articleContent->innerHTML;
  116. }
  117. }
  118. return $article;
  119. }
  120. function api_version() {
  121. return 2;
  122. }
  123. private function filter_unknown_feeds($enabled_feeds) {
  124. $tmp = array();
  125. foreach ($enabled_feeds as $feed) {
  126. $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
  127. if (db_num_rows($result) != 0) {
  128. array_push($tmp, $feed);
  129. }
  130. }
  131. return $tmp;
  132. }
  133. }
  134. ?>