init.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(__DIR__ . "/classes/Readability.php");
  75. $tmp = fetch_file_contents($article["link"]);
  76. if ($tmp) {
  77. $r = new Readability($tmp, $article["link"]);
  78. if ($r->init()) {
  79. $tmpxpath = new DOMXPath($r->dom);
  80. $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
  81. foreach ($entries as $entry) {
  82. if ($entry->hasAttribute("href")) {
  83. $entry->setAttribute("href",
  84. rewrite_relative_url($article["link"], $entry->getAttribute("href")));
  85. }
  86. if ($entry->hasAttribute("src")) {
  87. $entry->setAttribute("src",
  88. rewrite_relative_url($article["link"], $entry->getAttribute("src")));
  89. }
  90. }
  91. $article["content"] = $r->articleContent->innerHTML;
  92. }
  93. }
  94. return $article;
  95. }
  96. function api_version() {
  97. return 2;
  98. }
  99. private function filter_unknown_feeds($enabled_feeds) {
  100. $tmp = array();
  101. foreach ($enabled_feeds as $feed) {
  102. $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
  103. if (db_num_rows($result) != 0) {
  104. array_push($tmp, $feed);
  105. }
  106. }
  107. return $tmp;
  108. }
  109. }
  110. ?>