]> git.mxchange.org Git - friendica.git/blob - include/feed.php
Reworked contact relations between Friendica and Diaspora
[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         foreach ($entries AS $entry)
124                 $entrylist[] = $entry;
125
126         foreach (array_reverse($entrylist) AS $entry) {
127                 $item = array_merge($header, $author);
128
129                 $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
130
131                 if ($item["title"] == "")
132                         $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
133
134                 if ($item["title"] == "")
135                         $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
136
137                 $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
138                 if (!is_object($alternate))
139                         $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
140
141                 if (is_object($alternate))
142                         foreach($alternate AS $attributes)
143                                 if ($attributes->name == "href")
144                                         $item["plink"] = $attributes->textContent;
145
146                 if ($item["plink"] == "")
147                         $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
148
149                 if ($item["plink"] == "")
150                         $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
151
152                 $item["plink"] = original_url($item["plink"]);
153
154                 $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
155
156                 if ($item["uri"] == "")
157                         $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
158
159                 if ($item["uri"] == "")
160                         $item["uri"] = $item["plink"];
161
162                 $item["parent-uri"] = $item["uri"];
163
164                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
165
166                 if ($published == "")
167                         $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
168
169                 if ($published == "")
170                         $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
171
172                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
173
174                 if ($updated == "")
175                         $updated = $published;
176
177                 if ($published != "")
178                         $item["created"] = $published;
179
180                 if ($updated != "")
181                         $item["edited"] = $updated;
182
183                 $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
184
185                 if ($creator == "")
186                         $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
187
188                 if ($creator == "")
189                         $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
190
191                 if ($creator != "")
192                         $item["author-name"] = $creator;
193
194                 if ($pubDate != "")
195                         $item["edited"] = $item["created"] = $pubDate;
196
197                 $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
198
199                 if ($creator != "")
200                         $item["author-name"] = $creator;
201
202                 //$item["object"] = $xml;
203
204                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
205                         intval($importer["uid"]), dbesc($item["uri"]));
206                 if ($r) {
207                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
208                         continue;
209                 }
210
211                 // To-Do?
212                 // <category>Ausland</category>
213                 // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
214
215                 $attachments = array();
216
217                 $enclosures = $xpath->query("enclosure", $entry);
218                 foreach ($enclosures AS $enclosure) {
219                         $href = "";
220                         $length = "";
221                         $type = "";
222                         $title = "";
223
224                         foreach($enclosure->attributes AS $attributes) {
225                                 if ($attributes->name == "url")
226                                         $href = $attributes->textContent;
227                                 elseif ($attributes->name == "length")
228                                         $length = $attributes->textContent;
229                                 elseif ($attributes->name == "type")
230                                         $type = $attributes->textContent;
231                         }
232                         if(strlen($item["attach"]))
233                                 $item["attach"] .= ',';
234
235                         $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
236
237                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
238                 }
239
240                 if ($contact["fetch_further_information"]) {
241                         $preview = "";
242
243                         // Handle enclosures and treat them as preview picture
244                         foreach ($attachments AS $attachment)
245                                 if ($attachment["type"] == "image/jpeg")
246                                         $preview = $attachment["link"];
247
248                         $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
249                         $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
250                         $item["title"] = "";
251                         $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
252                         unset($item["attach"]);
253                 } else {
254                         $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
255
256                         if ($body == "")
257                                 $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
258
259                         if ($body == "")
260                                 $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
261
262                         if ($body == "")
263                                 $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
264
265                         // remove the content of the title if it is identically to the body
266                         // This helps with auto generated titles e.g. from tumblr
267                         if (title_is_body($item["title"], $body))
268                                 $item["title"] = "";
269
270                         $item["body"] = html2bbcode($body);
271                 }
272
273                 logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
274
275                 $notify = item_is_remote_self($contact, $item);
276                 $id = item_store($item, false, $notify);
277
278                 //print_r($item);
279
280                 logger("Feed for contact ".$contact["url"]." stored under id ".$id);
281         }
282 }
283 ?>