]> git.wh0rd.org - tt-rss.git/blob - classes/feeditem/atom.php
532c599cc858033da4ed3c371d1f87103f525b22
[tt-rss.git] / classes / feeditem / atom.php
1 <?php
2 class FeedItem_Atom extends FeedItem_Common {
3
4 function get_id() {
5 $id = $this->elem->getElementsByTagName("id")->item(0);
6
7 if ($id) {
8 return $id->nodeValue;
9 } else {
10 return $this->get_link();
11 }
12 }
13
14 function get_date() {
15 $updated = $this->elem->getElementsByTagName("updated")->item(0);
16
17 if ($updated) {
18 return strtotime($updated->nodeValue);
19 }
20
21 $published = $this->elem->getElementsByTagName("published")->item(0);
22
23 if ($published) {
24 return strtotime($published->nodeValue);
25 }
26
27 $date = $this->xpath->query("dc:date", $this->elem)->item(0);
28
29 if ($date) {
30 return strtotime($date->nodeValue);
31 }
32 }
33
34 function rel2abs($rel, $base)
35 {
36 /* return if already absolute URL */
37 if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
38
39 /* queries and anchors */
40 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
41
42 /* parse base URL and convert to local variables:
43 $scheme, $host, $path */
44 extract(parse_url($base));
45
46 /* remove non-directory element from path */
47 $path = preg_replace('#/[^/]*$#', '', $path);
48
49 /* destroy path if relative url points to root */
50 if ($rel[0] == '/') $path = '';
51
52 /* dirty absolute URL */
53 $abs = "$host$path/$rel";
54
55 /* replace '//' or '/./' or '/foo/../' with '/' */
56 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
57 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
58
59 /* absolute URL is ready! */
60 return $scheme.'://'.$abs;
61 }
62
63 function get_link() {
64 $links = $this->elem->getElementsByTagName("link");
65
66 foreach ($links as $link) {
67 if ($link && $link->hasAttribute("href") &&
68 (!$link->hasAttribute("rel")
69 || $link->getAttribute("rel") == "alternate"
70 || $link->getAttribute("rel") == "standout")) {
71 $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)",$link);
72 return $this->rel2abs($link->getAttribute("href"), $base);
73 }
74 }
75 }
76
77 function get_title() {
78 $title = $this->elem->getElementsByTagName("title")->item(0);
79
80 if ($title) {
81 return $title->nodeValue;
82 }
83 }
84
85 function get_content() {
86 $content = $this->elem->getElementsByTagName("content")->item(0);
87
88 if ($content) {
89 if ($content->hasAttribute('type')) {
90 if ($content->getAttribute('type') == 'xhtml') {
91 for ($i = 0; $i < $content->childNodes->length; $i++) {
92 $child = $content->childNodes->item($i);
93
94 if ($child->hasChildNodes()) {
95 return $this->doc->saveXML($child);
96 }
97 }
98 }
99 }
100
101 return $content->nodeValue;
102 }
103 }
104
105 function get_description() {
106 $content = $this->elem->getElementsByTagName("summary")->item(0);
107
108 if ($content) {
109 if ($content->hasAttribute('type')) {
110 if ($content->getAttribute('type') == 'xhtml') {
111 for ($i = 0; $i < $content->childNodes->length; $i++) {
112 $child = $content->childNodes->item($i);
113
114 if ($child->hasChildNodes()) {
115 return $this->doc->saveXML($child);
116 }
117 }
118 }
119 }
120
121 return $content->nodeValue;
122 }
123
124 }
125
126 function get_categories() {
127 $categories = $this->elem->getElementsByTagName("category");
128 $cats = array();
129
130 foreach ($categories as $cat) {
131 if ($cat->hasAttribute("term"))
132 array_push($cats, $cat->getAttribute("term"));
133 }
134
135 $categories = $this->xpath->query("dc:subject", $this->elem);
136
137 foreach ($categories as $cat) {
138 array_push($cats, $cat->nodeValue);
139 }
140
141 return $cats;
142 }
143
144 function get_enclosures() {
145 $links = $this->elem->getElementsByTagName("link");
146
147 $encs = array();
148
149 foreach ($links as $link) {
150 if ($link && $link->hasAttribute("href") && $link->hasAttribute("rel")) {
151 if ($link->getAttribute("rel") == "enclosure") {
152 $enc = new FeedEnclosure();
153
154 $enc->type = $link->getAttribute("type");
155 $enc->link = $link->getAttribute("href");
156 $enc->length = $link->getAttribute("length");
157
158 array_push($encs, $enc);
159 }
160 }
161 }
162
163 $enclosures = $this->xpath->query("media:content", $this->elem);
164
165 foreach ($enclosures as $enclosure) {
166 $enc = new FeedEnclosure();
167
168 $enc->type = $enclosure->getAttribute("type");
169 $enc->link = $enclosure->getAttribute("url");
170 $enc->length = $enclosure->getAttribute("length");
171
172 array_push($encs, $enc);
173 }
174
175 return $encs;
176 }
177
178 }
179 ?>