]> git.mxchange.org Git - friendica.git/blob - include/feed.php
DB Version was forgotten ...
[friendica.git] / include / feed.php
1 <?php
2 require_once("include/html2bbcode.php");
3 require_once("include/items.php");
4
5 function feed_import($xml,$importer,&$contact, &$hub) {
6
7         $a = get_app();
8
9         logger("Import Atom/RSS feed", LOGGER_DEBUG);
10
11         if ($xml == "")
12                 return;
13
14         $doc = new DOMDocument();
15         @$doc->loadXML($xml);
16         $xpath = new DomXPath($doc);
17         $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
18         $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
19         $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
20         $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
21         $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
22         $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
23
24         $author = array();
25
26         // Is it RDF?
27         if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
28                 //$author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
29                 $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
30
31                 if ($author["author-name"] == "")
32                         $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
33
34                 $entries = $xpath->query('/rdf:RDF/rss:item');
35         }
36
37         // Is it Atom?
38         if ($xpath->query('/atom:feed/atom:entry')->length > 0) {
39                 //$self = $xpath->query("/atom:feed/atom:link[@rel='self']")->item(0)->attributes;
40                 //if (is_object($self))
41                 //      foreach($self AS $attributes)
42                 //              if ($attributes->name == "href")
43                 //                      $author["author-link"] = $attributes->textContent;
44
45                 //if ($author["author-link"] == "") {
46                 //      $alternate = $xpath->query("/atom:feed/atom:link[@rel='alternate']")->item(0)->attributes;
47                 //      if (is_object($alternate))
48                 //              foreach($alternate AS $attributes)
49                 //                      if ($attributes->name == "href")
50                 //                              $author["author-link"] = $attributes->textContent;
51                 //}
52
53                 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
54
55                 if ($author["author-name"] == "")
56                         $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
57
58                 if ($author["author-name"] == "")
59                         $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
60
61                 //$author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
62
63                 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
64
65                 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
66
67                 $entries = $xpath->query('/atom:feed/atom:entry');
68         }
69
70         // Is it RSS?
71         if ($xpath->query('/rss/channel')->length > 0) {
72                 //$author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
73                 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
74                 //$author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
75
76                 if ($author["author-name"] == "")
77                         $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
78
79                 if ($author["author-name"] == "")
80                         $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
81
82                 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
83
84                 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
85
86                 $entries = $xpath->query('/rss/channel/item');
87         }
88
89         //if ($author["author-link"] == "")
90                 $author["author-link"] = $contact["url"];
91
92         if ($author["author-name"] == "")
93                 $author["author-name"] = $contact["name"];
94
95         //if ($author["author-avatar"] == "")
96                 $author["author-avatar"] = $contact["thumb"];
97
98         $author["owner-link"] = $contact["url"];
99         $author["owner-name"] = $contact["name"];
100         $author["owner-avatar"] = $contact["thumb"];
101
102         $header = array();
103         $header["uid"] = $importer["uid"];
104         $header["network"] = NETWORK_FEED;
105         $header["type"] = "remote";
106         $header["wall"] = 0;
107         $header["origin"] = 0;
108         $header["gravity"] = GRAVITY_PARENT;
109         $header["private"] = 2;
110         $header["verb"] = ACTIVITY_POST;
111         $header["object-type"] = ACTIVITY_OBJ_NOTE;
112
113         $header["contact-id"] = $contact["id"];
114
115         if(!strlen($contact["notify"])) {
116                 // one way feed - no remote comment ability
117                 $header["last-child"] = 0;
118         }
119
120         if (!is_object($entries))
121                 return;
122
123         $entrylist = array();
124
125         foreach ($entries AS $entry)
126                 $entrylist[] = $entry;
127
128         foreach (array_reverse($entrylist) AS $entry) {
129                 $item = array_merge($header, $author);
130
131                 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
132
133                 if ($item["title"] == "")
134                         $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
135
136                 if ($item["title"] == "")
137                         $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
138
139                 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
140                 if (!is_object($alternate))
141                         $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
142
143                 if (is_object($alternate))
144                         foreach($alternate AS $attributes)
145                                 if ($attributes->name == "href")
146                                         $item["plink"] = $attributes->textContent;
147
148                 if ($item["plink"] == "")
149                         $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
150
151                 if ($item["plink"] == "")
152                         $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
153
154                 $item["plink"] = original_url($item["plink"]);
155
156                 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
157
158                 if ($item["uri"] == "")
159                         $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
160
161                 if ($item["uri"] == "")
162                         $item["uri"] = $item["plink"];
163
164                 $item["parent-uri"] = $item["uri"];
165
166                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
167
168                 if ($published == "")
169                         $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
170
171                 if ($published == "")
172                         $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
173
174                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
175
176                 if ($updated == "")
177                         $updated = $published;
178
179                 if ($published != "")
180                         $item["created"] = $published;
181
182                 if ($updated != "")
183                         $item["edited"] = $updated;
184
185                 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
186
187                 if ($creator == "")
188                         $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
189
190                 if ($creator == "")
191                         $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
192
193                 if ($creator != "")
194                         $item["author-name"] = $creator;
195
196                 if ($pubDate != "")
197                         $item["edited"] = $item["created"] = $pubDate;
198
199                 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
200
201                 if ($creator != "")
202                         $item["author-name"] = $creator;
203
204                 //$item["object"] = $xml;
205
206                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
207                         intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
208                 if ($r) {
209                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
210                         continue;
211                 }
212
213                 /// @TODO ?
214                 // <category>Ausland</category>
215                 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
216
217                 $attachments = array();
218
219                 $enclosures = $xpath->query("enclosure", $entry);
220                 foreach ($enclosures AS $enclosure) {
221                         $href = "";
222                         $length = "";
223                         $type = "";
224                         $title = "";
225
226                         foreach($enclosure->attributes AS $attributes) {
227                                 if ($attributes->name == "url")
228                                         $href = $attributes->textContent;
229                                 elseif ($attributes->name == "length")
230                                         $length = $attributes->textContent;
231                                 elseif ($attributes->name == "type")
232                                         $type = $attributes->textContent;
233                         }
234                         if(strlen($item["attach"]))
235                                 $item["attach"] .= ',';
236
237                         $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
238
239                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
240                 }
241
242                 if ($contact["fetch_further_information"]) {
243                         $preview = "";
244
245                         // Handle enclosures and treat them as preview picture
246                         foreach ($attachments AS $attachment)
247                                 if ($attachment["type"] == "image/jpeg")
248                                         $preview = $attachment["link"];
249
250                         $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
251                         $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
252                         $item["title"] = "";
253                         $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
254                         unset($item["attach"]);
255                 } else {
256                         $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
257
258                         if ($body == "")
259                                 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
260
261                         if ($body == "")
262                                 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
263
264                         if ($body == "")
265                                 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
266
267                         // remove the content of the title if it is identically to the body
268                         // This helps with auto generated titles e.g. from tumblr
269                         if (title_is_body($item["title"], $body))
270                                 $item["title"] = "";
271
272                         $item["body"] = html2bbcode($body);
273                 }
274
275                 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
276
277                 $notify = item_is_remote_self($contact, $item);
278                 $id = item_store($item, false, $notify);
279
280                 //print_r($item);
281
282                 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
283         }
284 }
285 ?>