init.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. class Af_Comics extends Plugin {
  3. private $host;
  4. private $filters = array();
  5. function about() {
  6. return array(2.0,
  7. "Fixes RSS feeds of assorted comic strips",
  8. "fox");
  9. }
  10. function init($host) {
  11. $this->host = $host;
  12. $host->add_hook($host::HOOK_FETCH_FEED, $this);
  13. $host->add_hook($host::HOOK_SUBSCRIBE_FEED, $this);
  14. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  15. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  16. require_once __DIR__ . "/filter_base.php";
  17. $filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php"));
  18. $names = [];
  19. foreach ($filters as $file) {
  20. $filter_name = preg_replace("/\..*$/", "", basename($file));
  21. if (array_search($filter_name, $names) === FALSE) {
  22. if (!class_exists($filter_name)) {
  23. require_once $file;
  24. }
  25. array_push($names, $filter_name);
  26. $filter = new $filter_name();
  27. if (is_subclass_of($filter, "Af_ComicFilter")) {
  28. array_push($this->filters, $filter);
  29. array_push($names, $filter_name);
  30. }
  31. }
  32. }
  33. }
  34. function hook_prefs_tab($args) {
  35. if ($args != "prefFeeds") return;
  36. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">";
  37. print "<p>" . __("The following comics are currently supported:") . "</p>";
  38. $comics = array("GoComics");
  39. foreach ($this->filters as $f) {
  40. foreach ($f->supported() as $comic) {
  41. array_push($comics, $comic);
  42. }
  43. }
  44. asort($comics);
  45. print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
  46. foreach ($comics as $comic) {
  47. print "<li>$comic</li>";
  48. }
  49. print "</ul>";
  50. print "<p>".__("To subscribe to GoComics use the comic's regular web page as the feed URL (e.g. for the <em>Garfield</em> comic use <code>http://www.gocomics.com/garfield</code>).")."</p>";
  51. print "<p>".__('Drop any updated filters into <code>filters.local</code> in plugin directory.')."</p>";
  52. print "</div>";
  53. }
  54. function hook_article_filter($article) {
  55. foreach ($this->filters as $f) {
  56. if ($f->process($article))
  57. break;
  58. }
  59. return $article;
  60. }
  61. // GoComics dropped feed support so it needs to be handled when fetching the feed.
  62. /**
  63. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  64. */
  65. function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) {
  66. if ($auth_login || $auth_pass)
  67. return $feed_data;
  68. if (preg_match('#^https?://(?:feeds\.feedburner\.com/uclick|www\.gocomics\.com)/([-a-z0-9]+)$#i', $fetch_url, $comic)) {
  69. $site_url = 'https://www.gocomics.com/' . $comic[1];
  70. $article_link = $site_url . date('/Y/m/d');
  71. $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false));
  72. require_once 'lib/MiniTemplator.class.php';
  73. $feed_title = htmlspecialchars($comic[1]);
  74. $site_url = htmlspecialchars($site_url);
  75. $article_link = htmlspecialchars($article_link);
  76. $tpl = new MiniTemplator();
  77. $tpl->readTemplateFromFile('templates/generated_feed.txt');
  78. $tpl->setVariable('FEED_TITLE', $feed_title, true);
  79. $tpl->setVariable('VERSION', VERSION, true);
  80. $tpl->setVariable('FEED_URL', htmlspecialchars($fetch_url), true);
  81. $tpl->setVariable('SELF_URL', $site_url, true);
  82. $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c'), true);
  83. $tpl->setVariable('ARTICLE_UPDATED_RFC822', date(DATE_RFC822), true);
  84. if ($body) {
  85. $doc = new DOMDocument();
  86. if (@$doc->loadHTML($body)) {
  87. $xpath = new DOMXPath($doc);
  88. $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0);
  89. if ($node) {
  90. $node->removeAttribute("width");
  91. if ($node->hasAttribute("srcset") && preg_match("|/transparent\.png$|", $node->getAttribute("srcset"))) {
  92. if ($node->hasAttribute("data-srcset")) {
  93. $node->setAttribute("srcset", $node->getAttribute("data-srcset"));
  94. $node->removeAttribute("data-srcset");
  95. } elseif ($node->hasAttribute("src")) {
  96. $node->removeAttribute("srcset");
  97. }
  98. }
  99. $tpl->setVariable('ARTICLE_ID', $article_link, true);
  100. $tpl->setVariable('ARTICLE_LINK', $article_link, true);
  101. $tpl->setVariable('ARTICLE_TITLE', date('l, F d, Y'), true);
  102. $tpl->setVariable('ARTICLE_EXCERPT', '', true);
  103. $tpl->setVariable('ARTICLE_CONTENT', $doc->saveHTML($node), true);
  104. $tpl->setVariable('ARTICLE_AUTHOR', '', true);
  105. $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true);
  106. $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true);
  107. $tpl->addBlock('entry');
  108. }
  109. }
  110. }
  111. $tpl->addBlock('feed');
  112. $tmp_data = '';
  113. if ($tpl->generateOutputToString($tmp_data))
  114. $feed_data = $tmp_data;
  115. }
  116. return $feed_data;
  117. }
  118. function hook_subscribe_feed($contents, $url, $auth_login, $auth_pass) {
  119. if ($auth_login || $auth_pass)
  120. return $contents;
  121. if (preg_match('#^https?://www\.gocomics\.com/([-a-z0-9]+)$#i', $url))
  122. return '<?xml version="1.0" encoding="utf-8"?>'; // Get is_html() to return false.
  123. return $contents;
  124. }
  125. function api_version() {
  126. return 2;
  127. }
  128. }