init.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. class Af_Dilbert extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Embeds Dilbert strips",
  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"], "dilbert.com") !== FALSE) {
  16. if (strpos($article["plugin_data"], "dilbert,$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("/dyn\/str_strip\/.*zoom\.gif$/", $entry->getAttribute("src"), $matches)) {
  26. $entry->setAttribute("src",
  27. rewrite_relative_url("http://dilbert.com/",
  28. $matches[0]));
  29. $basenode = $entry;
  30. break;
  31. }
  32. }
  33. if ($basenode) {
  34. $article["content"] = $doc->saveXML($basenode);
  35. $article["plugin_data"] = "dilbert,$owner_uid:" . $article["plugin_data"];
  36. }
  37. }
  38. } else if (isset($article["stored"]["content"])) {
  39. $article["content"] = $article["stored"]["content"];
  40. }
  41. }
  42. return $article;
  43. }
  44. function api_version() {
  45. return 2;
  46. }
  47. }
  48. ?>