init.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "</div>";
  43. }
  44. function hook_article_filter($article) {
  45. $owner_uid = $article["owner_uid"];
  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. function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) {
  54. if ($auth_login || $auth_pass)
  55. return $feed_data;
  56. if (preg_match('#^https?://feeds.feedburner.com/uclick/([a-z]+)#', $fetch_url, $comic)) {
  57. $site_url = 'http://www.gocomics.com/' . $comic[1];
  58. $article_link = $site_url . date('/Y/m/d');
  59. $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false));
  60. require_once 'lib/MiniTemplator.class.php';
  61. $feed_title = htmlspecialchars($comic[1]);
  62. $site_url = htmlspecialchars($site_url);
  63. $article_link = htmlspecialchars($article_link);
  64. $tpl = new MiniTemplator();
  65. $tpl->readTemplateFromFile('templates/generated_feed.txt');
  66. $tpl->setVariable('FEED_TITLE', $feed_title, true);
  67. $tpl->setVariable('VERSION', VERSION, true);
  68. $tpl->setVariable('FEED_URL', htmlspecialchars($fetch_url), true);
  69. $tpl->setVariable('SELF_URL', $site_url, true);
  70. $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c'), true);
  71. $tpl->setVariable('ARTICLE_UPDATED_RFC822', date(DATE_RFC822), true);
  72. if ($body) {
  73. $doc = new DOMDocument();
  74. if (@$doc->loadHTML($body)) {
  75. $xpath = new DOMXPath($doc);
  76. $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0);
  77. if ($node) {
  78. $tpl->setVariable('ARTICLE_ID', $article_link, true);
  79. $tpl->setVariable('ARTICLE_LINK', $article_link, true);
  80. $tpl->setVariable('ARTICLE_TITLE', date('l, F d, Y'), true);
  81. $tpl->setVariable('ARTICLE_EXCERPT', '', true);
  82. $tpl->setVariable('ARTICLE_CONTENT', $doc->saveXML($node), true);
  83. $tpl->setVariable('ARTICLE_AUTHOR', '', true);
  84. $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true);
  85. $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true);
  86. $tpl->addBlock('entry');
  87. }
  88. }
  89. }
  90. $tpl->addBlock('feed');
  91. $tmp_data = '';
  92. if ($tpl->generateOutputToString($tmp_data))
  93. $feed_data = $tmp_data;
  94. }
  95. return $feed_data;
  96. }
  97. function api_version() {
  98. return 2;
  99. }
  100. }
  101. ?>