init.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_ARTICLE_FILTER, $this);
  14. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  15. require_once __DIR__ . "/filter_base.php";
  16. $filters = array_merge(glob(__DIR__ . "/filters.local/*.php"), glob(__DIR__ . "/filters/*.php"));
  17. $names = [];
  18. foreach ($filters as $file) {
  19. $filter_name = preg_replace("/\..*$/", "", basename($file));
  20. if (array_search($filter_name, $names) === FALSE) {
  21. if (!class_exists($filter_name)) {
  22. require_once $file;
  23. }
  24. array_push($names, $filter_name);
  25. $filter = new $filter_name();
  26. if (is_subclass_of($filter, "Af_ComicFilter")) {
  27. array_push($this->filters, $filter);
  28. array_push($names, $filter_name);
  29. }
  30. }
  31. }
  32. }
  33. function hook_prefs_tab($args) {
  34. if ($args != "prefFeeds") return;
  35. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">";
  36. print "<p>" . __("The following comics are currently supported:") . "</p>";
  37. $comics = array("GoComics");
  38. foreach ($this->filters as $f) {
  39. foreach ($f->supported() as $comic) {
  40. array_push($comics, $comic);
  41. }
  42. }
  43. asort($comics);
  44. print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
  45. foreach ($comics as $comic) {
  46. print "<li>$comic</li>";
  47. }
  48. print "</ul>";
  49. print "<p>".__('GoComics requires a specific URL to workaround their lack of feed support: <code>http://feeds.feedburner.com/uclick/<em>comic_name</em></code> (e.g. <code>http://www.gocomics.com/garfield</code> uses <code>http://feeds.feedburner.com/uclick/garfield</code>).')."</p>";
  50. print "<p>".__('Drop any updated filters into <code>filters.local</code> in plugin directory.')."</p>";
  51. print "</div>";
  52. }
  53. function hook_article_filter($article) {
  54. foreach ($this->filters as $f) {
  55. if ($f->process($article))
  56. break;
  57. }
  58. return $article;
  59. }
  60. // GoComics dropped feed support so it needs to be handled when fetching the feed.
  61. /**
  62. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  63. */
  64. function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) {
  65. if ($auth_login || $auth_pass)
  66. return $feed_data;
  67. if (preg_match('#^https?://feeds.feedburner.com/uclick/([-a-z]+)#', $fetch_url, $comic)) {
  68. $site_url = 'http://www.gocomics.com/' . $comic[1];
  69. $article_link = $site_url . date('/Y/m/d');
  70. $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false));
  71. require_once 'lib/MiniTemplator.class.php';
  72. $feed_title = htmlspecialchars($comic[1]);
  73. $site_url = htmlspecialchars($site_url);
  74. $article_link = htmlspecialchars($article_link);
  75. $tpl = new MiniTemplator();
  76. $tpl->readTemplateFromFile('templates/generated_feed.txt');
  77. $tpl->setVariable('FEED_TITLE', $feed_title, true);
  78. $tpl->setVariable('VERSION', VERSION, true);
  79. $tpl->setVariable('FEED_URL', htmlspecialchars($fetch_url), true);
  80. $tpl->setVariable('SELF_URL', $site_url, true);
  81. $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c'), true);
  82. $tpl->setVariable('ARTICLE_UPDATED_RFC822', date(DATE_RFC822), true);
  83. if ($body) {
  84. $doc = new DOMDocument();
  85. if (@$doc->loadHTML($body)) {
  86. $xpath = new DOMXPath($doc);
  87. $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0);
  88. if ($node) {
  89. $tpl->setVariable('ARTICLE_ID', $article_link, true);
  90. $tpl->setVariable('ARTICLE_LINK', $article_link, true);
  91. $tpl->setVariable('ARTICLE_TITLE', date('l, F d, Y'), true);
  92. $tpl->setVariable('ARTICLE_EXCERPT', '', true);
  93. $tpl->setVariable('ARTICLE_CONTENT', $doc->saveHTML($node), true);
  94. $tpl->setVariable('ARTICLE_AUTHOR', '', true);
  95. $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true);
  96. $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true);
  97. $tpl->addBlock('entry');
  98. }
  99. }
  100. }
  101. $tpl->addBlock('feed');
  102. $tmp_data = '';
  103. if ($tpl->generateOutputToString($tmp_data))
  104. $feed_data = $tmp_data;
  105. }
  106. return $feed_data;
  107. }
  108. function api_version() {
  109. return 2;
  110. }
  111. }