init.php 6.4 KB

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