]> git.mxchange.org Git - friendica.git/blob - include/ostatus.php
Some code beautification.
[friendica.git] / include / ostatus.php
1 <?php
2 require_once("mod/share.php");
3 require_once('include/html2bbcode.php');
4 require_once('include/enotify.php');
5 require_once('include/items.php');
6 require_once('include/ostatus_conversation.php');
7
8 function ostatus_fetchauthor($xpath, $context, &$contact) {
9
10         $author = array();
11         $author["author-link"] = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
12         $author["author-name"] = $xpath->evaluate('atom:author/atom:name/text()', $context)->item(0)->nodeValue;
13
14         // Preserve the value
15         $authorlink = $author["author-link"];
16
17         $alternate = $xpath->query("atom:author/atom:link[@rel='alternate']", $context)->item(0)->attributes;
18         if (is_object($alternate))
19                 foreach($alternate AS $attributes)
20                         if ($attributes->name == "href")
21                                 $author["author-link"] = $attributes->textContent;
22
23         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
24                 intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
25                 dbesc(normalise_link($authorlink)), dbesc(NETWORK_STATUSNET));
26         if ($r) {
27                 $contact = $r[0];
28                 $author["contact-id"] = $r[0]["id"];
29         } else
30                 $author["contact-id"] = $contact["id"];
31
32         $avatarlist = array();
33         $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
34         foreach($avatars AS $avatar) {
35                 $href = "";
36                 $width = 0;
37                 foreach($avatar->attributes AS $attributes) {
38                         if ($attributes->name == "href")
39                                 $href = $attributes->textContent;
40                         if ($attributes->name == "width")
41                                 $width = $attributes->textContent;
42                 }
43                 if (($width > 0) AND ($href != ""))
44                         $avatarlist[$width] = $href;
45         }
46         if (count($avatarlist) > 0) {
47                 krsort($avatarlist);
48                 $author["author-avatar"] = current($avatarlist);
49         }
50
51         $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
52         if ($displayname != "")
53                 $author["author-name"] = $displayname;
54
55         $author["owner-name"] = $author["author-name"];
56         $author["owner-link"] = $author["author-link"];
57         $author["owner-avatar"] = $author["author-avatar"];
58
59         return($author);
60 }
61
62 function ostatus_import($xml,$importer,&$contact) {
63
64         $a = get_app();
65
66         logger("Import OStatus message", LOGGER_DEBUG);
67
68         if ($xml == "")
69                 return;
70
71         $doc = new DOMDocument();
72         $doc->loadXML($xml);
73
74         $xpath = new DomXPath($doc);
75         $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
76         $xpath->registerNamespace('thr', "http://purl.org/syndication/thread/1.0");
77         $xpath->registerNamespace('georss', "http://www.georss.org/georss");
78         $xpath->registerNamespace('activity', "http://activitystrea.ms/spec/1.0/");
79         $xpath->registerNamespace('media', "http://purl.org/syndication/atommedia");
80         $xpath->registerNamespace('poco', "http://portablecontacts.net/spec/1.0");
81         $xpath->registerNamespace('ostatus', "http://ostatus.org/schema/1.0");
82         $xpath->registerNamespace('statusnet', "http://status.net/schema/api/1/");
83
84         $header = array();
85         $header["uid"] = $importer["uid"];
86         $header["network"] = NETWORK_OSTATUS;
87         $header["type"] = "remote";
88         $header["wall"] = 0;
89         $header["origin"] = 0;
90         $header["gravity"] = GRAVITY_PARENT;
91
92         // it could either be a received post or a post we fetched by ourselves
93         // depending on that, the first node is different
94         $first_child = $doc->firstChild->tagName;
95
96         if ($first_child == "feed")
97                 $entries = $xpath->query('/atom:feed/atom:entry');
98         else
99                 $entries = $xpath->query('/atom:entry');
100
101         $conversation = "";
102         $conversationlist = array();
103         $item_id = 0;
104
105         // Reverse the order of the entries
106         foreach ($entries AS $entry)
107                 $entrylist[] = $entry;
108
109         foreach (array_reverse($entrylist) AS $entry) {
110
111                 $mention = false;
112
113                 // fetch the author
114                 if ($first_child == "feed")
115                         $author = ostatus_fetchauthor($xpath, $doc->firstChild, $contact);
116                 else
117                         $author = ostatus_fetchauthor($xpath, $entry, $contact);
118
119                 $item = array_merge($header, $author);
120
121                 // Now get the item
122                 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
123
124                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
125                         intval($importer["uid"]), dbesc($item["uri"]));
126                 if ($r) {
127                         logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
128                         continue;
129                 }
130
131                 $item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
132                 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
133                 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
134
135                 if ($item["verb"] == ACTIVITY_FOLLOW) {
136                         // ignore "Follow" messages
137                         continue;
138                 }
139
140                 if ($item["verb"] == ACTIVITY_FAVORITE) {
141                         // ignore "Favorite" messages
142                         continue;
143                 }
144
145                 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
146                 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
147                 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
148
149                 $related = "";
150
151                 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
152                 if (is_object($inreplyto->item(0))) {
153                         foreach($inreplyto->item(0)->attributes AS $attributes) {
154                                 if ($attributes->name == "ref")
155                                         $item["parent-uri"] = $attributes->textContent;
156                                 if ($attributes->name == "href")
157                                         $related = $attributes->textContent;
158                         }
159                 }
160
161                 $georsspoint = $xpath->query('georss:point', $entry);
162                 if ($georsspoint)
163                         $item["coord"] = $georsspoint->item(0)->nodeValue;
164
165                 $categories = $xpath->query('atom:category', $entry);
166                 if ($categories) {
167                         foreach ($categories AS $category) {
168                                 foreach($category->attributes AS $attributes)
169                                         if ($attributes->name == "term") {
170                                                 $term = $attributes->textContent;
171                                                 if(strlen($item["tag"]))
172                                                         $item["tag"] .= ',';
173                                                 $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
174                                         }
175                         }
176                 }
177
178                 $self = "";
179                 $enclosure = "";
180
181                 $links = $xpath->query('atom:link', $entry);
182                 if ($links) {
183                         $rel = "";
184                         $href = "";
185                         $type = "";
186                         $length = "0";
187                         $title = "";
188                         foreach ($links AS $link) {
189                                 foreach($link->attributes AS $attributes) {
190                                         if ($attributes->name == "href")
191                                                 $href = $attributes->textContent;
192                                         if ($attributes->name == "rel")
193                                                 $rel = $attributes->textContent;
194                                         if ($attributes->name == "type")
195                                                 $type = $attributes->textContent;
196                                         if ($attributes->name == "length")
197                                                 $length = $attributes->textContent;
198                                         if ($attributes->name == "title")
199                                                 $title = $attributes->textContent;
200                                 }
201                                 if (($rel != "") AND ($href != ""))
202                                         switch($rel) {
203                                                 case "alternate":
204                                                         $item["plink"] = $href;
205                                                         break;
206                                                 case "ostatus:conversation":
207                                                         $conversation = $href;
208                                                         break;
209                                                 case "enclosure":
210                                                         $enclosure = $href;
211                                                         if(strlen($item["attach"]))
212                                                                 $item["attach"] .= ',';
213
214                                                         $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
215                                                         break;
216                                                 case "related":
217                                                         if (!isset($item["parent-uri"]))
218                                                                 $item["parent-uri"] = $href;
219
220                                                         if ($related == "")
221                                                                 $related = $href;
222                                                         break;
223                                                 case "self":
224                                                         $self = $href;
225                                                         break;
226                                                 case "mentioned":
227                                                         // Notification check
228                                                         if ($importer["nurl"] == normalise_link($href))
229                                                                 $mention = true;
230                                                         break;
231                                         }
232                         }
233                 }
234
235                 $local_id = "";
236                 $repeat_of = "";
237
238                 $notice_info = $xpath->query('statusnet:notice_info', $entry);
239                 if ($notice_info)
240                         foreach($notice_info->item(0)->attributes AS $attributes) {
241                                 if ($attributes->name == "source")
242                                         $item["app"] = strip_tags($attributes->textContent);
243                                 if ($attributes->name == "local_id")
244                                         $local_id = $attributes->textContent;
245                                 if ($attributes->name == "repeat_of")
246                                         $repeat_of = $attributes->textContent;
247                         }
248
249                 // Is it a repeated post?
250                 if ($repeat_of != "") {
251                         $activityobjects = $xpath->query('activity:object', $entry)->item(0);
252
253                         if (is_object($activityobjects)) {
254
255                                 $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
256                                 if ($orig_uris)
257                                         foreach($orig_uris->item(0)->attributes AS $attributes)
258                                                 if ($attributes->name == "href")
259                                                         $orig_uri = $attributes->textContent;
260
261                                 if (!isset($orig_uri))
262                                         $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
263
264                                 if (!isset($orig_uri))
265                                         $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
266
267                                 if (!isset($orig_uri))
268                                         $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
269
270                                 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
271                                 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
272
273                                 $orig_contact = $contact;
274                                 $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $orig_contact);
275
276                                 $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
277                                 $item["body"] = $prefix.html2bbcode($orig_body)."[/share]";
278
279                                 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
280                                 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
281                         }
282                 }
283
284                 if ($enclosure != "")
285                         $item["body"] .= add_page_info($enclosure);
286
287                 if (isset($item["parent-uri"])) {
288                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
289                                 intval($importer["uid"]), dbesc($item["parent-uri"]));
290
291                         if (!$r AND ($related != "")) {
292                                 $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
293
294                                 if ($reply_path != $related) {
295                                         logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
296                                         $reply_xml = fetch_url($reply_path);
297
298                                         $reply_contact = $contact;
299                                         ostatus_import($reply_xml,$importer,$reply_contact);
300
301                                         // After the import try to fetch the parent item again
302                                         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
303                                                 intval($importer["uid"]), dbesc($item["parent-uri"]));
304                                 }
305                         }
306                         if ($r) {
307                                 $item["type"] = 'remote-comment';
308                                 $item["gravity"] = GRAVITY_COMMENT;
309                         }
310                 } else
311                         $item["parent-uri"] = $item["uri"];
312
313                 $item_id = item_store($item);
314                 //echo $xml;
315                 //print_r($item);
316                 //echo $item_id." ".$item["parent-uri"]."\n";
317
318                 if (!$item_id) {
319                         logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
320                         continue;
321                 }
322
323                 logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
324                 $item["id"] = $item_id;
325
326                 if (!isset($item["parent"]) OR ($item["parent"] == 0))
327                         $item["parent"] = $item_id;
328
329                 if ($mention) {
330                         $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
331
332                         notification(array(
333                                 'type'         => NOTIFY_TAGSELF,
334                                 'notify_flags' => $u[0]["notify-flags"],
335                                 'language'     => $u[0]["language"],
336                                 'to_name'      => $u[0]["username"],
337                                 'to_email'     => $u[0]["email"],
338                                 'uid'          => $item["uid"],
339                                 'item'         => $item,
340                                 'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
341                                 'source_name'  => $item["author-name"],
342                                 'source_link'  => $item["author-link"],
343                                 'source_photo' => $item["author-avatar"],
344                                 'verb'         => ACTIVITY_TAG,
345                                 'otype'        => 'item',
346                                 'parent'       => $item["parent"]
347                         ));
348                 }
349
350                 if ($conversation != "") {
351                         // Check for duplicates. We really don't need to check the same conversation twice.
352                         if (!in_array($conversation, $conversationlist)) {
353                                 complete_conversation($item_id, $conversation);
354                                 $conversationlist[] = $conversation;
355                         }
356                 }
357         }
358 }