atom.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. class FeedItem_Atom extends FeedItem_Common {
  3. function get_id() {
  4. $id = $this->elem->getElementsByTagName("id")->item(0);
  5. if ($id) {
  6. return $id->nodeValue;
  7. } else {
  8. return $this->get_link();
  9. }
  10. }
  11. function get_date() {
  12. $updated = $this->elem->getElementsByTagName("updated")->item(0);
  13. if ($updated) {
  14. return strtotime($updated->nodeValue);
  15. }
  16. $published = $this->elem->getElementsByTagName("published")->item(0);
  17. if ($published) {
  18. return strtotime($published->nodeValue);
  19. }
  20. $date = $this->xpath->query("dc:date", $this->elem)->item(0);
  21. if ($date) {
  22. return strtotime($date->nodeValue);
  23. }
  24. }
  25. function get_link() {
  26. $links = $this->elem->getElementsByTagName("link");
  27. foreach ($links as $link) {
  28. if ($link && $link->hasAttribute("href") &&
  29. (!$link->hasAttribute("rel")
  30. || $link->getAttribute("rel") == "alternate"
  31. || $link->getAttribute("rel") == "standout")) {
  32. $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $link);
  33. if ($base)
  34. return rewrite_relative_url($base, trim($link->getAttribute("href")));
  35. else
  36. return trim($link->getAttribute("href"));
  37. }
  38. }
  39. }
  40. function get_title() {
  41. $title = $this->elem->getElementsByTagName("title")->item(0);
  42. if ($title) {
  43. return trim($title->nodeValue);
  44. }
  45. }
  46. function get_content() {
  47. $content = $this->elem->getElementsByTagName("content")->item(0);
  48. if ($content) {
  49. if ($content->hasAttribute('type')) {
  50. if ($content->getAttribute('type') == 'xhtml') {
  51. for ($i = 0; $i < $content->childNodes->length; $i++) {
  52. $child = $content->childNodes->item($i);
  53. if ($child->hasChildNodes()) {
  54. return $this->doc->saveXML($child);
  55. }
  56. }
  57. }
  58. }
  59. return $this->subtree_or_text($content);
  60. }
  61. }
  62. function get_description() {
  63. $content = $this->elem->getElementsByTagName("summary")->item(0);
  64. if ($content) {
  65. if ($content->hasAttribute('type')) {
  66. if ($content->getAttribute('type') == 'xhtml') {
  67. for ($i = 0; $i < $content->childNodes->length; $i++) {
  68. $child = $content->childNodes->item($i);
  69. if ($child->hasChildNodes()) {
  70. return $this->doc->saveXML($child);
  71. }
  72. }
  73. }
  74. }
  75. return $this->subtree_or_text($content);
  76. }
  77. }
  78. function get_categories() {
  79. $categories = $this->elem->getElementsByTagName("category");
  80. $cats = array();
  81. foreach ($categories as $cat) {
  82. if ($cat->hasAttribute("term"))
  83. array_push($cats, trim($cat->getAttribute("term")));
  84. }
  85. $categories = $this->xpath->query("dc:subject", $this->elem);
  86. foreach ($categories as $cat) {
  87. array_push($cats, trim($cat->nodeValue));
  88. }
  89. return $cats;
  90. }
  91. function get_enclosures() {
  92. $links = $this->elem->getElementsByTagName("link");
  93. $encs = array();
  94. foreach ($links as $link) {
  95. if ($link && $link->hasAttribute("href") && $link->hasAttribute("rel")) {
  96. if ($link->getAttribute("rel") == "enclosure") {
  97. $enc = new FeedEnclosure();
  98. $enc->type = $link->getAttribute("type");
  99. $enc->link = $link->getAttribute("href");
  100. $enc->length = $link->getAttribute("length");
  101. array_push($encs, $enc);
  102. }
  103. }
  104. }
  105. $enclosures = $this->xpath->query("media:content", $this->elem);
  106. foreach ($enclosures as $enclosure) {
  107. $enc = new FeedEnclosure();
  108. $enc->type = $enclosure->getAttribute("type");
  109. $enc->link = $enclosure->getAttribute("url");
  110. $enc->length = $enclosure->getAttribute("length");
  111. $enc->height = $enclosure->getAttribute("height");
  112. $enc->width = $enclosure->getAttribute("width");
  113. $desc = $this->xpath->query("media:description", $enclosure)->item(0);
  114. if ($desc) $enc->title = strip_tags($desc->nodeValue);
  115. array_push($encs, $enc);
  116. }
  117. $enclosures = $this->xpath->query("media:group", $this->elem);
  118. foreach ($enclosures as $enclosure) {
  119. $enc = new FeedEnclosure();
  120. $content = $this->xpath->query("media:content", $enclosure)->item(0);
  121. if ($content) {
  122. $enc->type = $content->getAttribute("type");
  123. $enc->link = $content->getAttribute("url");
  124. $enc->length = $content->getAttribute("length");
  125. $enc->height = $content->getAttribute("height");
  126. $enc->width = $content->getAttribute("width");
  127. $desc = $this->xpath->query("media:description", $content)->item(0);
  128. if ($desc) {
  129. $enc->title = strip_tags($desc->nodeValue);
  130. } else {
  131. $desc = $this->xpath->query("media:description", $enclosure)->item(0);
  132. if ($desc) $enc->title = strip_tags($desc->nodeValue);
  133. }
  134. array_push($encs, $enc);
  135. }
  136. }
  137. $enclosures = $this->xpath->query("media:thumbnail", $this->elem);
  138. foreach ($enclosures as $enclosure) {
  139. $enc = new FeedEnclosure();
  140. $enc->type = "image/generic";
  141. $enc->link = $enclosure->getAttribute("url");
  142. $enc->height = $enclosure->getAttribute("height");
  143. $enc->width = $enclosure->getAttribute("width");
  144. array_push($encs, $enc);
  145. }
  146. return $encs;
  147. }
  148. }