init.php 6.2 KB

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