init.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. class Af_Readability extends Plugin {
  3. /* @var PluginHost $host */
  4. private $host;
  5. function about() {
  6. return array(1.0,
  7. "Try to inline article content using Readability",
  8. "fox");
  9. }
  10. function flags() {
  11. return array("needs_curl" => true);
  12. }
  13. function save() {
  14. $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
  15. $this->host->set($this, "enable_share_anything", $enable_share_anything);
  16. echo __("Data saved.");
  17. }
  18. function init($host)
  19. {
  20. $this->host = $host;
  21. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  22. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  23. $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
  24. $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
  25. $host->add_filter_action($this, "action_inline", __("Inline content"));
  26. }
  27. function hook_prefs_tab($args) {
  28. if ($args != "prefFeeds") return;
  29. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
  30. print_notice("Enable the plugin for specific feeds in the feed editor.");
  31. print "<form dojoType=\"dijit.form.Form\">";
  32. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  33. evt.preventDefault();
  34. if (this.validate()) {
  35. console.log(dojo.objectToQuery(this.getValues()));
  36. new Ajax.Request('backend.php', {
  37. parameters: dojo.objectToQuery(this.getValues()),
  38. onComplete: function(transport) {
  39. notify_info(transport.responseText);
  40. }
  41. });
  42. //this.reset();
  43. }
  44. </script>";
  45. print_hidden("op", "pluginhandler");
  46. print_hidden("method", "save");
  47. print_hidden("plugin", "af_readability");
  48. $enable_share_anything = $this->host->get($this, "enable_share_anything");
  49. print_checkbox("enable_share_anything", $enable_share_anything);
  50. print "&nbsp;<label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>";
  51. print "<p>"; print_button("submit", __("Save"));
  52. print "</form>";
  53. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  54. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  55. $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
  56. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  57. if (count($enabled_feeds) > 0) {
  58. print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
  59. print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
  60. foreach ($enabled_feeds as $f) {
  61. print "<li>" .
  62. "<img src='images/pub_set.png'
  63. style='vertical-align : middle'> <a href='#'
  64. onclick='editFeed($f)'>".
  65. Feeds::getFeedTitle($f) . "</a></li>";
  66. }
  67. print "</ul>";
  68. }
  69. print "</div>";
  70. }
  71. function hook_prefs_edit_feed($feed_id) {
  72. print "<div class=\"dlgSec\">".__("Readability")."</div>";
  73. print "<div class=\"dlgSecCont\">";
  74. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  75. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  76. $key = array_search($feed_id, $enabled_feeds);
  77. $checked = $key !== FALSE ? "checked" : "";
  78. print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
  79. name=\"af_readability_enabled\"
  80. $checked>&nbsp;<label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
  81. print "</div>";
  82. }
  83. function hook_prefs_save_feed($feed_id) {
  84. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  85. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  86. $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]);
  87. $key = array_search($feed_id, $enabled_feeds);
  88. if ($enable) {
  89. if ($key === FALSE) {
  90. array_push($enabled_feeds, $feed_id);
  91. }
  92. } else {
  93. if ($key !== FALSE) {
  94. unset($enabled_feeds[$key]);
  95. }
  96. }
  97. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  98. }
  99. /**
  100. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  101. */
  102. function hook_article_filter_action($article, $action) {
  103. return $this->process_article($article);
  104. }
  105. public function extract_content($url) {
  106. global $fetch_effective_url;
  107. if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
  108. $tmp = fetch_file_contents(array("url" => $url, "type" => "text/html"));
  109. if ($tmp && mb_strlen($tmp) < 1024 * 500) {
  110. $tmpdoc = new DOMDocument("1.0", "UTF-8");
  111. if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
  112. return false;
  113. if (strtolower($tmpdoc->encoding) != 'utf-8') {
  114. $tmpxpath = new DOMXPath($tmpdoc);
  115. foreach ($tmpxpath->query("//meta") as $elem) {
  116. $elem->parentNode->removeChild($elem);
  117. }
  118. $tmp = $tmpdoc->saveHTML();
  119. }
  120. $r = new Readability($tmp, $fetch_effective_url);
  121. if ($r->init()) {
  122. $tmpxpath = new DOMXPath($r->dom);
  123. $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
  124. foreach ($entries as $entry) {
  125. if ($entry->hasAttribute("href")) {
  126. $entry->setAttribute("href",
  127. rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href")));
  128. }
  129. if ($entry->hasAttribute("src")) {
  130. $entry->setAttribute("src",
  131. rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src")));
  132. }
  133. }
  134. return $r->articleContent->innerHTML;
  135. }
  136. }
  137. return false;
  138. }
  139. function process_article($article) {
  140. $extracted_content = $this->extract_content($article["link"]);
  141. if ($extracted_content) {
  142. $article["content"] = $extracted_content;
  143. }
  144. return $article;
  145. }
  146. function hook_article_filter($article) {
  147. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  148. if (!is_array($enabled_feeds)) return $article;
  149. $key = array_search($article["feed"]["id"], $enabled_feeds);
  150. if ($key === FALSE) return $article;
  151. return $this->process_article($article);
  152. }
  153. function api_version() {
  154. return 2;
  155. }
  156. private function filter_unknown_feeds($enabled_feeds) {
  157. $tmp = array();
  158. foreach ($enabled_feeds as $feed) {
  159. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
  160. $sth->execute([$feed, $_SESSION['uid']]);
  161. if ($row = $sth->fetch()) {
  162. array_push($tmp, $feed);
  163. }
  164. }
  165. return $tmp;
  166. }
  167. }