init.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. class Af_GoComics extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Strip unnecessary stuff from gocomics feeds",
  7. "fox");
  8. }
  9. function init($host) {
  10. $this->host = $host;
  11. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  12. }
  13. function hook_article_filter($article) {
  14. $owner_uid = $article["owner_uid"];
  15. if (strpos($article["guid"], "gocomics.com") !== FALSE) {
  16. if (strpos($article["plugin_data"], "gocomics,$owner_uid:") === FALSE) {
  17. $doc = new DOMDocument();
  18. @$doc->loadHTML(fetch_file_contents($article["link"]));
  19. $basenode = false;
  20. if ($doc) {
  21. $xpath = new DOMXPath($doc);
  22. $entries = $xpath->query('(//img[@src])'); // we might also check for img[@class='strip'] I guess...
  23. $matches = array();
  24. foreach ($entries as $entry) {
  25. if (preg_match("/(http:\/\/assets.amuniversal.com\/.*width.*)/i", $entry->getAttribute("src"), $matches)) {
  26. $entry->setAttribute("src", $matches[0]);
  27. $basenode = $entry;
  28. break;
  29. }
  30. }
  31. if (!$basenode) {
  32. // fallback on the smaller version
  33. foreach ($entries as $entry) {
  34. if (preg_match("/(http:\/\/assets.amuniversal.com\/.*)/i", $entry->getAttribute("src"), $matches)) {
  35. $entry->setAttribute("src", $matches[0]);
  36. $basenode = $entry;
  37. break;
  38. }
  39. }
  40. }
  41. if ($basenode) {
  42. $article["content"] = $doc->saveXML($basenode);
  43. $article["plugin_data"] = "gocomics,$owner_uid:" . $article["plugin_data"];
  44. }
  45. }
  46. } else if (isset($article["stored"]["content"])) {
  47. $article["content"] = $article["stored"]["content"];
  48. }
  49. }
  50. return $article;
  51. }
  52. function api_version() {
  53. return 2;
  54. }
  55. }
  56. ?>