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