]> git.wh0rd.org - tt-rss.git/blame - classes/feeditem/atom.php
fixing the rel url to abs
[tt-rss.git] / classes / feeditem / atom.php
CommitLineData
cd07592c 1<?php
b4d16900 2class FeedItem_Atom extends FeedItem_Common {
bfc24f37 3
cd07592c
AD
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() {
04d2f9c8 15 $updated = $this->elem->getElementsByTagName("updated")->item(0);
cd07592c 16
04d2f9c8
AD
17 if ($updated) {
18 return strtotime($updated->nodeValue);
19 }
ce5d234d 20
d256f1fb
AD
21 $published = $this->elem->getElementsByTagName("published")->item(0);
22
23 if ($published) {
24 return strtotime($published->nodeValue);
25 }
26
ce5d234d
AD
27 $date = $this->xpath->query("dc:date", $this->elem)->item(0);
28
29 if ($date) {
30 return strtotime($date->nodeValue);
31 }
cd07592c 32 }
6aeb37d0 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)) {}
cd07592c 58
6aeb37d0 59 /* absolute URL is ready! */
60 return $scheme.'://'.$abs;
61 }
62
cd07592c
AD
63 function get_link() {
64 $links = $this->elem->getElementsByTagName("link");
65
66 foreach ($links as $link) {
df2655e0
AD
67 if ($link && $link->hasAttribute("href") &&
68 (!$link->hasAttribute("rel")
69 || $link->getAttribute("rel") == "alternate"
70 || $link->getAttribute("rel") == "standout")) {
b28b2ce9 71 $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)",$link);
6aeb37d0 72 return $this->rel2abs($link->getAttribute("href"), $base);
cd07592c
AD
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) {
27004401
AD
89 if ($content->hasAttribute('type')) {
90 if ($content->getAttribute('type') == 'xhtml') {
bc3c887f
AD
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 }
96ce71f3
AD
98 }
99 }
100
cd07592c
AD
101 return $content->nodeValue;
102 }
103 }
104
105 function get_description() {
27004401
AD
106 $content = $this->elem->getElementsByTagName("summary")->item(0);
107
108 if ($content) {
109 if ($content->hasAttribute('type')) {
110 if ($content->getAttribute('type') == 'xhtml') {
bc3c887f
AD
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 }
27004401
AD
118 }
119 }
cd07592c 120
27004401 121 return $content->nodeValue;
cd07592c 122 }
27004401 123
cd07592c
AD
124 }
125
cd07592c
AD
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
d4992d6b
AD
135 $categories = $this->xpath->query("dc:subject", $this->elem);
136
137 foreach ($categories as $cat) {
138 array_push($cats, $cat->nodeValue);
139 }
cd07592c
AD
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
d4992d6b
AD
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
cd07592c
AD
175 return $encs;
176 }
177
cd07592c
AD
178}
179?>