init.php 3.9 KB

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