]> git.mxchange.org Git - friendica.git/blob - include/feed.php
The feed function has now a simulation mode
[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, $simulate = false) {
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: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: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                 if ($author["author-link"] == "")
54                         $author["author-link"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
55                 if ($author["author-link"] == "")
56                         $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
57
58                 $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
59
60                 $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
61
62                 if ($author["author-name"] == "")
63                         $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
64
65                 if ($author["author-name"] == "")
66                         $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
67
68                 $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
69
70                 $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
71
72                 $entries = $xpath->query('/atom:feed/atom:entry');
73         }
74
75         // Is it RSS?
76         if ($xpath->query('/rss/channel')->length > 0) {
77                 $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
78
79                 $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
80                 $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
81
82                 if ($author["author-name"] == "")
83                         $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
84
85                 if ($author["author-name"] == "")
86                         $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
87
88                 $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
89
90                 $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
91
92                 $entries = $xpath->query('/rss/channel/item');
93         }
94
95         if (is_array($contact)) {
96                 $author["author-link"] = $contact["url"];
97
98                 if ($author["author-name"] == "")
99                         $author["author-name"] = $contact["name"];
100
101                 $author["author-avatar"] = $contact["thumb"];
102
103                 $author["owner-link"] = $contact["url"];
104                 $author["owner-name"] = $contact["name"];
105                 $author["owner-avatar"] = $contact["thumb"];
106         }
107
108         $header = array();
109         $header["uid"] = $importer["uid"];
110         $header["network"] = NETWORK_FEED;
111         $header["type"] = "remote";
112         $header["wall"] = 0;
113         $header["origin"] = 0;
114         $header["gravity"] = GRAVITY_PARENT;
115         $header["private"] = 2;
116         $header["verb"] = ACTIVITY_POST;
117         $header["object-type"] = ACTIVITY_OBJ_NOTE;
118
119         $header["contact-id"] = $contact["id"];
120
121         if(!strlen($contact["notify"])) {
122                 // one way feed - no remote comment ability
123                 $header["last-child"] = 0;
124         }
125
126         if (!is_object($entries))
127                 return;
128
129         $items = array();
130
131         $entrylist = array();
132
133         foreach ($entries AS $entry)
134                 $entrylist[] = $entry;
135
136         foreach (array_reverse($entrylist) AS $entry) {
137                 $item = array_merge($header, $author);
138
139                 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
140
141                 if ($item["title"] == "")
142                         $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
143
144                 if ($item["title"] == "")
145                         $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
146
147                 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
148                 if (!is_object($alternate))
149                         $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
150
151                 if (is_object($alternate))
152                         foreach($alternate AS $attributes)
153                                 if ($attributes->name == "href")
154                                         $item["plink"] = $attributes->textContent;
155
156                 if ($item["plink"] == "")
157                         $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
158
159                 if ($item["plink"] == "")
160                         $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
161
162                 $item["plink"] = original_url($item["plink"]);
163
164                 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
165
166                 if ($item["uri"] == "")
167                         $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
168
169                 if ($item["uri"] == "")
170                         $item["uri"] = $item["plink"];
171
172                 $item["parent-uri"] = $item["uri"];
173
174                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
175
176                 if ($published == "")
177                         $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
178
179                 if ($published == "")
180                         $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
181
182                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
183
184                 if ($updated == "")
185                         $updated = $published;
186
187                 if ($published != "")
188                         $item["created"] = $published;
189
190                 if ($updated != "")
191                         $item["edited"] = $updated;
192
193                 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
194
195                 if ($creator == "")
196                         $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
197
198                 if ($creator == "")
199                         $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
200
201                 if ($creator != "")
202                         $item["author-name"] = $creator;
203
204                 if ($pubDate != "")
205                         $item["edited"] = $item["created"] = $pubDate;
206
207                 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
208
209                 if ($creator != "")
210                         $item["author-name"] = $creator;
211
212                 if (!$simulate) {
213                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
214                                 intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
215                         if ($r) {
216                                 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
217                                 continue;
218                         }
219                 }
220
221                 /// @TODO ?
222                 // <category>Ausland</category>
223                 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
224
225                 $attachments = array();
226
227                 $enclosures = $xpath->query("enclosure", $entry);
228                 foreach ($enclosures AS $enclosure) {
229                         $href = "";
230                         $length = "";
231                         $type = "";
232                         $title = "";
233
234                         foreach($enclosure->attributes AS $attributes) {
235                                 if ($attributes->name == "url")
236                                         $href = $attributes->textContent;
237                                 elseif ($attributes->name == "length")
238                                         $length = $attributes->textContent;
239                                 elseif ($attributes->name == "type")
240                                         $type = $attributes->textContent;
241                         }
242                         if(strlen($item["attach"]))
243                                 $item["attach"] .= ',';
244
245                         $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
246
247                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
248                 }
249
250                 if ($contact["fetch_further_information"]) {
251                         $preview = "";
252
253                         // Handle enclosures and treat them as preview picture
254                         foreach ($attachments AS $attachment)
255                                 if ($attachment["type"] == "image/jpeg")
256                                         $preview = $attachment["link"];
257
258                         $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
259                         $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
260                         $item["title"] = "";
261                         $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
262                         unset($item["attach"]);
263                 } else {
264                         $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
265
266                         if ($body == "")
267                                 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
268
269                         if ($body == "")
270                                 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
271
272                         if ($body == "")
273                                 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
274
275                         // remove the content of the title if it is identically to the body
276                         // This helps with auto generated titles e.g. from tumblr
277                         if (title_is_body($item["title"], $body))
278                                 $item["title"] = "";
279
280                         $item["body"] = html2bbcode($body);
281                 }
282
283                 if (!$simulate) {
284                         logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
285
286                         $notify = item_is_remote_self($contact, $item);
287                         $id = item_store($item, false, $notify);
288
289                         logger("Feed for contact ".$contact["url"]." stored under id ".$id);
290                 } else
291                         $items[] = $item;
292
293                 if ($simulate)
294                         break;
295         }
296
297         if ($simulate)
298                 return array("header" => $author, "items" => $items);
299 }
300 ?>