]> git.mxchange.org Git - friendica.git/blob - include/dfrn.php
Improved documentation
[friendica.git] / include / dfrn.php
1 <?php
2 require_once('include/items.php');
3 require_once('include/ostatus.php');
4
5 /**
6  * @brief Generates the atom entries for delivery.php
7  *
8  * This function is used whenever content is transmitted via DFRN.
9  *
10  * @param array $items Item elements
11  * @param array $owner Owner record
12  *
13  * @return string DFRN entries
14  */
15 function dfrn_entries($items,$owner) {
16
17         $doc = new DOMDocument('1.0', 'utf-8');
18         $doc->formatOutput = true;
19
20         $root = dfrn_add_header($doc, $owner, "dfrn:owner", "", false);
21
22         if(! count($items))
23                 return trim($doc->saveXML());
24
25         foreach($items as $item) {
26                 $entry = dfrn_entry($doc, "text", $item, $owner, $item["entry:comment-allow"], $item["entry:cid"]);
27                 $root->appendChild($entry);
28         }
29
30         return(trim($doc->saveXML()));
31 }
32
33 /**
34  * @brief Generate an atom feed for the given user
35  *
36  * This function is called when another server is pulling data from the user feed.
37  *
38  * @param App $a
39  * @param string $dfrn_id
40  * @param string $owner_nick Owner nick name
41  * @param string $last_update Date of the last update
42  * @param int $direction
43  *
44  * @return string DFRN feed entries
45  */
46 function dfrn_feed(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
47
48         $sitefeed    = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
49         $public_feed = (($dfrn_id) ? false : true);
50         $starred     = false;   // not yet implemented, possible security issues
51         $converse    = false;
52
53         if($public_feed && $a->argc > 2) {
54                 for($x = 2; $x < $a->argc; $x++) {
55                         if($a->argv[$x] == 'converse')
56                                 $converse = true;
57                         if($a->argv[$x] == 'starred')
58                                 $starred = true;
59                         if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
60                                 $category = $a->argv[$x+1];
61                 }
62         }
63
64
65
66         // default permissions - anonymous user
67
68         $sql_extra = " AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' ";
69
70         $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
71                 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
72                 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
73                 dbesc($owner_nick)
74         );
75
76         if(! count($r))
77                 killme();
78
79         $owner = $r[0];
80         $owner_id = $owner['user_uid'];
81         $owner_nick = $owner['nickname'];
82
83         $sql_post_table = "";
84         $visibility = "";
85
86         if(! $public_feed) {
87
88                 $sql_extra = '';
89                 switch($direction) {
90                         case (-1):
91                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
92                                 $my_id = $dfrn_id;
93                                 break;
94                         case 0:
95                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
96                                 $my_id = '1:' . $dfrn_id;
97                                 break;
98                         case 1:
99                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
100                                 $my_id = '0:' . $dfrn_id;
101                                 break;
102                         default:
103                                 return false;
104                                 break; // NOTREACHED
105                 }
106
107                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
108                         intval($owner_id)
109                 );
110
111                 if(! count($r))
112                         killme();
113
114                 $contact = $r[0];
115                 require_once('include/security.php');
116                 $groups = init_groups_visitor($contact['id']);
117
118                 if(count($groups)) {
119                         for($x = 0; $x < count($groups); $x ++)
120                                 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
121                         $gs = implode('|', $groups);
122                 }
123                 else
124                         $gs = '<<>>' ; // Impossible to match
125
126                 $sql_extra = sprintf("
127                         AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' )
128                         AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' )
129                         AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
130                         AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s')
131                 ",
132                         intval($contact['id']),
133                         intval($contact['id']),
134                         dbesc($gs),
135                         dbesc($gs)
136                 );
137         }
138
139         if($public_feed)
140                 $sort = 'DESC';
141         else
142                 $sort = 'ASC';
143
144         $date_field = "`changed`";
145         $sql_order = "`item`.`parent` ".$sort.", `item`.`created` ASC";
146
147         if(! strlen($last_update))
148                 $last_update = 'now -30 days';
149
150         if(isset($category)) {
151                 $sql_post_table = sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
152                                 dbesc(protect_sprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($owner_id));
153                 //$sql_extra .= file_tag_file_query('item',$category,'category');
154         }
155
156         if($public_feed) {
157                 if(! $converse)
158                         $sql_extra .= " AND `contact`.`self` = 1 ";
159         }
160
161         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
162
163         //      AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
164         //      dbesc($check_date),
165
166         $r = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id`,
167                 `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
168                 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
169                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
170                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
171                 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
172                 FROM `item` $sql_post_table
173                 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
174                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
175                 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
176                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
177                 AND ((`item`.`wall` = 1) $visibility) AND `item`.$date_field > '%s'
178                 $sql_extra
179                 ORDER BY $sql_order LIMIT 0, 300",
180                 intval($owner_id),
181                 dbesc($check_date),
182                 dbesc($sort)
183         );
184
185         // Will check further below if this actually returned results.
186         // We will provide an empty feed if that is the case.
187
188         $items = $r;
189
190         $doc = new DOMDocument('1.0', 'utf-8');
191         $doc->formatOutput = true;
192
193         $alternatelink = $owner['url'];
194
195         if(isset($category))
196                 $alternatelink .= "/category/".$category;
197
198         if ($public_feed)
199                 $author = "dfrn:owner";
200         else
201                 $author = "author";
202
203         $root = dfrn_add_header($doc, $owner, $author, $alternatelink, true);
204
205         // This hook can't work anymore
206         //      call_hooks('atom_feed', $atom);
207
208         if(! count($items)) {
209                 $atom = trim($doc->saveXML());
210
211                 call_hooks('atom_feed_end', $atom);
212
213                 return $atom;
214         }
215
216         foreach($items as $item) {
217
218                 // prevent private email from leaking.
219                 if($item['network'] === NETWORK_MAIL)
220                         continue;
221
222                 // public feeds get html, our own nodes use bbcode
223
224                 if($public_feed) {
225                         $type = 'html';
226                         // catch any email that's in a public conversation and make sure it doesn't leak
227                         if($item['private'])
228                                 continue;
229                 }
230                 else {
231                         $type = 'text';
232                 }
233
234                 $entry = dfrn_entry($doc, $type, $item, $owner, true);
235                 $root->appendChild($entry);
236
237         }
238
239         $atom = trim($doc->saveXML());
240
241         call_hooks('atom_feed_end', $atom);
242
243         return $atom;
244 }
245
246 /**
247  * @brief Create XML text for DFRN mails
248  *
249  * @param array $item message elements
250  * @param array $owner Owner record
251  *
252  * @return string DFRN mail
253  */
254 function dfrn_mail($item, $owner) {
255         $doc = new DOMDocument('1.0', 'utf-8');
256         $doc->formatOutput = true;
257
258         $root = dfrn_add_header($doc, $owner, "dfrn:owner", "", false);
259
260         $mail = $doc->createElement("dfrn:mail");
261         $sender = $doc->createElement("dfrn:sender");
262
263         xml_add_element($doc, $sender, "dfrn:name", $owner['name']);
264         xml_add_element($doc, $sender, "dfrn:uri", $owner['url']);
265         xml_add_element($doc, $sender, "dfrn:avatar", $owner['thumb']);
266
267         $mail->appendChild($sender);
268
269         xml_add_element($doc, $mail, "dfrn:id", $item['uri']);
270         xml_add_element($doc, $mail, "dfrn:in-reply-to", $item['parent-uri']);
271         xml_add_element($doc, $mail, "dfrn:sentdate", datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME));
272         xml_add_element($doc, $mail, "dfrn:subject", $item['title']);
273         xml_add_element($doc, $mail, "dfrn:content", $item['body']);
274
275         $root->appendChild($mail);
276
277         return(trim($doc->saveXML()));
278 }
279
280 /**
281  * @brief Create XML text for DFRN friend suggestions
282  *
283  * @param array $item suggestion elements
284  * @param array $owner Owner record
285  *
286  * @return string DFRN suggestions
287  */
288 function dfrn_fsuggest($item, $owner) {
289         $doc = new DOMDocument('1.0', 'utf-8');
290         $doc->formatOutput = true;
291
292         $root = dfrn_add_header($doc, $owner, "dfrn:owner", "", false);
293
294         $suggest = $doc->createElement("dfrn:suggest");
295
296         xml_add_element($doc, $suggest, "dfrn:url", $item['url']);
297         xml_add_element($doc, $suggest, "dfrn:name", $item['name']);
298         xml_add_element($doc, $suggest, "dfrn:photo", $item['photo']);
299         xml_add_element($doc, $suggest, "dfrn:request", $item['request']);
300         xml_add_element($doc, $suggest, "dfrn:note", $item['note']);
301
302         $root->appendChild($suggest);
303
304         return(trim($doc->saveXML()));
305 }
306
307 /**
308  * @brief Create XML text for DFRN relocations
309  *
310  * @param array $owner Owner record
311  * @param int $uid User ID
312  *
313  * @return string DFRN relocations
314  */
315 function dfrn_relocate($owner, $uid) {
316
317         $a = get_app();
318
319         /* get site pubkey. this could be a new installation with no site keys*/
320         $pubkey = get_config('system','site_pubkey');
321         if(! $pubkey) {
322                 $res = new_keypair(1024);
323                 set_config('system','site_prvkey', $res['prvkey']);
324                 set_config('system','site_pubkey', $res['pubkey']);
325         }
326
327         $rp = q("SELECT `resource-id` , `scale`, type FROM `photo`
328                         WHERE `profile` = 1 AND `uid` = %d ORDER BY scale;", $uid);
329         $photos = array();
330         $ext = Photo::supportedTypes();
331         foreach($rp as $p){
332                 $photos[$p['scale']] = $a->get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
333         }
334         unset($rp, $ext);
335
336         $doc = new DOMDocument('1.0', 'utf-8');
337         $doc->formatOutput = true;
338
339         $root = dfrn_add_header($doc, $owner, "dfrn:owner", "", false);
340
341         $relocate = $doc->createElement("dfrn:relocate");
342
343         xml_add_element($doc, $relocate, "dfrn:url", $owner['url']);
344         xml_add_element($doc, $relocate, "dfrn:name", $owner['name']);
345         xml_add_element($doc, $relocate, "dfrn:photo", $photos[4]);
346         xml_add_element($doc, $relocate, "dfrn:thumb", $photos[5]);
347         xml_add_element($doc, $relocate, "dfrn:micro", $photos[6]);
348         xml_add_element($doc, $relocate, "dfrn:request", $owner['request']);
349         xml_add_element($doc, $relocate, "dfrn:confirm", $owner['confirm']);
350         xml_add_element($doc, $relocate, "dfrn:notify", $owner['notify']);
351         xml_add_element($doc, $relocate, "dfrn:poll", $owner['poll']);
352         xml_add_element($doc, $relocate, "dfrn:sitepubkey", get_config('system','site_pubkey'));
353
354         $root->appendChild($relocate);
355
356         return(trim($doc->saveXML()));
357 }
358
359 /**
360  * @brief Adds the header elements for the DFRN protocol
361  *
362  * @param object $doc XML document
363  * @param array $owner Owner record
364  * @param string $authorelement Element name for the author
365  * @param string $alternatelink link to profile or category
366  * @param bool $public Is it a header for public posts?
367  *
368  * @return object XML root object
369  */
370 function dfrn_add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) {
371         $a = get_app();
372
373         if ($alternatelink == "")
374                 $alternatelink = $owner['url'];
375
376         $root = $doc->createElementNS(NS_ATOM, 'feed');
377         $doc->appendChild($root);
378
379         $root->setAttribute("xmlns:thr", NS_THR);
380         $root->setAttribute("xmlns:at", "http://purl.org/atompub/tombstones/1.0");
381         $root->setAttribute("xmlns:media", NS_MEDIA);
382         $root->setAttribute("xmlns:dfrn", "http://purl.org/macgirvin/dfrn/1.0");
383         $root->setAttribute("xmlns:activity", NS_ACTIVITY);
384         $root->setAttribute("xmlns:georss", NS_GEORSS);
385         $root->setAttribute("xmlns:poco", NS_POCO);
386         $root->setAttribute("xmlns:ostatus", NS_OSTATUS);
387         $root->setAttribute("xmlns:statusnet", NS_STATUSNET);
388
389         xml_add_element($doc, $root, "id", $a->get_baseurl()."/profile/".$owner["nick"]);
390         xml_add_element($doc, $root, "title", $owner["name"]);
391
392         $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
393         xml_add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
394
395         $attributes = array("rel" => "license", "href" => "http://creativecommons.org/licenses/by/3.0/");
396         xml_add_element($doc, $root, "link", "", $attributes);
397
398         $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $alternatelink);
399         xml_add_element($doc, $root, "link", "", $attributes);
400
401         ostatus_hublinks($doc, $root);
402
403         if ($public) {
404                 $attributes = array("rel" => "salmon", "href" => $a->get_baseurl()."/salmon/".$owner["nick"]);
405                 xml_add_element($doc, $root, "link", "", $attributes);
406
407                 $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-replies", "href" => $a->get_baseurl()."/salmon/".$owner["nick"]);
408                 xml_add_element($doc, $root, "link", "", $attributes);
409
410                 $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-mention", "href" => $a->get_baseurl()."/salmon/".$owner["nick"]);
411                 xml_add_element($doc, $root, "link", "", $attributes);
412         }
413
414         if ($owner['page-flags'] == PAGE_COMMUNITY)
415                 xml_add_element($doc, $root, "dfrn:community", 1);
416
417         xml_add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
418
419         $author = dfrn_add_author($doc, $owner, $authorelement);
420         $root->appendChild($author);
421
422         return $root;
423 }
424
425 /**
426  * @brief Adds the author element in the header for the DFRN protocol
427  *
428  * @param object $doc XML document
429  * @param array $owner Owner record
430  * @param string $authorelement Element name for the author
431  *
432  * @return object XML author object
433  */
434 function dfrn_add_author($doc, $owner, $authorelement) {
435         $a = get_app();
436
437         $author = $doc->createElement($authorelement);
438
439         $namdate = datetime_convert('UTC', 'UTC', $owner['name-date'].'+00:00' , ATOM_TIME);
440         $uridate = datetime_convert('UTC', 'UTC', $owner['uri-date'].'+00:00', ATOM_TIME);
441         $picdate = datetime_convert('UTC', 'UTC', $owner['avatar-date'].'+00:00', ATOM_TIME);
442
443         $attributes = array("dfrn:updated" => $namdate);
444         xml_add_element($doc, $author, "name", $owner["name"], $attributes);
445
446         $attributes = array("dfrn:updated" => $namdate);
447         xml_add_element($doc, $author, "uri", $a->get_baseurl().'/profile/'.$owner["nickname"], $attributes);
448
449         $attributes = array("rel" => "photo", "type" => "image/jpeg", "dfrn:updated" => $picdate,
450                                 "media:width" => 175, "media:height" => 175, "href" => $owner['photo']);
451         xml_add_element($doc, $author, "link", "", $attributes);
452
453         $attributes = array("rel" => "avatar", "type" => "image/jpeg", "dfrn:updated" => $picdate,
454                                 "media:width" => 175, "media:height" => 175, "href" => $owner['photo']);
455         xml_add_element($doc, $author, "link", "", $attributes);
456
457         $birthday = feed_birthday($owner['user_uid'], $owner['timezone']);
458
459         if ($birthday)
460                 xml_add_element($doc, $author, "dfrn:birthday", $birthday);
461
462         return $author;
463 }
464
465 /**
466  * @brief Adds the author elements in the "entry" elements of the DFRN protocol
467  *
468  * @param object $doc XML document
469  * @param string $element Element name for the author
470  * @param string $contact_url Link of the contact
471  * @param array $items Item elements
472  *
473  * @return object XML author object
474  */
475 function dfrn_add_entry_author($doc, $element, $contact_url, $item) {
476         $a = get_app();
477
478         $contact = get_contact_details_by_url($contact_url, $item["uid"]);
479
480         $r = q("SELECT `profile`.`about`, `profile`.`name`, `profile`.`homepage`, `contact`.`nick`, `contact`.`location` FROM `profile`
481                         INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
482                         INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
483                         WHERE `contact`.`self` AND `profile`.`is-default` AND NOT `user`.`hidewall` AND `contact`.`nurl`='%s'",
484                 dbesc(normalise_link($contact_url)));
485         if ($r)
486                 $profile = $r[0];
487
488         $author = $doc->createElement($element);
489         xml_add_element($doc, $author, "name", $contact["name"]);
490         xml_add_element($doc, $author, "uri", $contact["url"]);
491
492         /// @Todo
493         /// - Check real image type and image size
494         /// - Check which of these boths elements we really use
495         $attributes = array(
496                         "rel" => "photo",
497                         "type" => "image/jpeg",
498                         "media:width" => 80,
499                         "media:height" => 80,
500                         "href" => $contact["photo"]);
501         xml_add_element($doc, $author, "link", "", $attributes);
502
503         $attributes = array(
504                         "rel" => "avatar",
505                         "type" => "image/jpeg",
506                         "media:width" => 80,
507                         "media:height" => 80,
508                         "href" => $contact["photo"]);
509         xml_add_element($doc, $author, "link", "", $attributes);
510
511         // Only show contact details when it is a user from our system and we are allowed to
512         if ($profile) {
513                 xml_add_element($doc, $author, "poco:preferredUsername", $profile["nick"]);
514                 xml_add_element($doc, $author, "poco:displayName", $profile["name"]);
515                 xml_add_element($doc, $author, "poco:note", $profile["about"]);
516
517                 if (trim($contact["location"]) != "") {
518                         $element = $doc->createElement("poco:address");
519                         xml_add_element($doc, $element, "poco:formatted", $profile["location"]);
520                         $author->appendChild($element);
521                 }
522
523                 if (trim($profile["homepage"]) != "") {
524                         $urls = $doc->createElement("poco:urls");
525                         xml_add_element($doc, $urls, "poco:type", "homepage");
526                         xml_add_element($doc, $urls, "poco:value", $profile["homepage"]);
527                         xml_add_element($doc, $urls, "poco:primary", "true");
528                         $author->appendChild($urls);
529                 }
530         }
531
532         return $author;
533 }
534
535 /**
536  * @brief Adds the activity elements
537  *
538  * @param object $doc XML document
539  * @param string $element Element name for the activity
540  * @param string $activity activity value
541  *
542  * @return object XML activity object
543  */
544 function dfrn_create_activity($doc, $element, $activity) {
545
546         if($activity) {
547                 $entry = $doc->createElement($element);
548
549                 $r = parse_xml_string($activity, false);
550                 if(!$r)
551                         return false;
552                 if($r->type)
553                         xml_add_element($doc, $entry, "activity:object-type", $r->type);
554                 if($r->id)
555                         xml_add_element($doc, $entry, "id", $r->id);
556                 if($r->title)
557                         xml_add_element($doc, $entry, "title", $r->title);
558                 if($r->link) {
559                         if(substr($r->link,0,1) === '<') {
560                                 if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
561                                         $r->link = str_replace('&','&amp;', $r->link);
562
563                                 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
564
565                                 $data = parse_xml_string($r->link, false);
566                                 foreach ($data->attributes() AS $parameter => $value)
567                                         $attributes[$parameter] = $value;
568                         } else
569                                 $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $r->link);
570
571                         xml_add_element($doc, $entry, "link", "", $attributes);
572                 }
573                 if($r->content)
574                         xml_add_element($doc, $entry, "content", bbcode($r->content), array("type" => "html"));
575
576                 return $entry;
577         }
578
579         return false;
580 }
581
582 /**
583  * @brief Adds the elements for attachments
584  *
585  * @param object $doc XML document
586  * @param object $root XML root
587  * @param array $item Item element
588  *
589  * @return object XML attachment object
590  */
591 function dfrn_get_attachment($doc, $root, $item) {
592         $arr = explode('[/attach],',$item['attach']);
593         if(count($arr)) {
594                 foreach($arr as $r) {
595                         $matches = false;
596                         $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches);
597                         if($cnt) {
598                                 $attributes = array("rel" => "enclosure",
599                                                 "href" => $matches[1],
600                                                 "type" => $matches[3]);
601
602                                 if(intval($matches[2]))
603                                         $attributes["length"] = intval($matches[2]);
604
605                                 if(trim($matches[4]) != "")
606                                         $attributes["title"] = trim($matches[4]);
607
608                                 xml_add_element($doc, $root, "link", "", $attributes);
609                         }
610                 }
611         }
612 }
613
614 /**
615  * @brief Adds the "entry" elements for the DFRN protocol
616  *
617  * @param object $doc XML document
618  * @param string $type "text" or "html"
619  * @param array $item Item element
620  * @param array $owner Owner record
621  * @param bool $comment Trigger the sending of the "comment" element
622  * @param int $cid
623  *
624  * @return object XML entry object
625  */
626 function dfrn_entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
627         $a = get_app();
628
629         $mentioned = array();
630
631         if(!$item['parent'])
632                 return;
633
634         if($item['deleted']) {
635                 $attributes = array("ref" => $item['uri'], "when" => datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME));
636                 return xml_create_element($doc, "at:deleted-entry", "", $attributes);
637         }
638
639         $entry = $doc->createElement("entry");
640
641         if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
642                 $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
643         else
644                 $body = $item['body'];
645
646         if ($type == 'html') {
647                 $htmlbody = $body;
648
649                 if ($item['title'] != "")
650                         $htmlbody = "[b]".$item['title']."[/b]\n\n".$htmlbody;
651
652                 $htmlbody = bbcode($htmlbody, false, false, 7);
653         }
654
655         $author = dfrn_add_entry_author($doc, "author", $item["author-link"], $item);
656         $entry->appendChild($author);
657
658         $dfrnowner = dfrn_add_entry_author($doc, "dfrn:owner", $item["owner-link"], $item);
659         $entry->appendChild($dfrnowner);
660
661         if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
662                 $parent = q("SELECT `guid` FROM `item` WHERE `id` = %d", intval($item["parent"]));
663                 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
664                 $attributes = array("ref" => $parent_item, "type" => "text/html", "href" => $a->get_baseurl().'/display/'.$parent[0]['guid']);
665                 xml_add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
666         }
667
668         xml_add_element($doc, $entry, "id", $item["uri"]);
669         xml_add_element($doc, $entry, "title", $item["title"]);
670
671         xml_add_element($doc, $entry, "published", datetime_convert("UTC","UTC",$item["created"]."+00:00",ATOM_TIME));
672         xml_add_element($doc, $entry, "updated", datetime_convert("UTC","UTC",$item["edited"]."+00:00",ATOM_TIME));
673
674         xml_add_element($doc, $entry, "dfrn:env", base64url_encode($body, true));
675         xml_add_element($doc, $entry, "content", (($type === 'html') ? $htmlbody : $body), array("type" => $type));
676
677         xml_add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html",
678                                                         "href" => $a->get_baseurl()."/display/".$item["guid"]));
679
680         if ($comment)
681                 xml_add_element($doc, $entry, "dfrn:comment-allow", intval($item['last-child']));
682
683         if($item['location'])
684                 xml_add_element($doc, $entry, "dfrn:location", $item['location']);
685
686         if($item['coord'])
687                 xml_add_element($doc, $entry, "georss:point", $item['coord']);
688
689         if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
690                 xml_add_element($doc, $entry, "dfrn:private", (($item['private']) ? $item['private'] : 1));
691
692         if($item['extid'])
693                 xml_add_element($doc, $entry, "dfrn:extid", $item['extid']);
694
695         if($item['bookmark'])
696                 xml_add_element($doc, $entry, "dfrn:bookmark", "true");
697
698         if($item['app'])
699                 xml_add_element($doc, $entry, "statusnet:notice_info", "", array("local_id" => $item['id'], "source" => $item['app']));
700
701         xml_add_element($doc, $entry, "dfrn:diaspora_guid", $item["guid"]);
702
703         if($item['signed_text']) {
704                 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
705                 xml_add_element($doc, $entry, "dfrn:diaspora_signature", $sign);
706         }
707
708         xml_add_element($doc, $entry, "activity:verb", construct_verb($item));
709
710         $actobj = dfrn_create_activity($doc, "activity:object", $item['object']);
711         if ($actobj)
712                 $entry->appendChild($actobj);
713
714         $actarg = dfrn_create_activity($doc, "activity:target", $item['target']);
715         if ($actarg)
716                 $entry->appendChild($actarg);
717
718         $tags = item_getfeedtags($item);
719
720         if(count($tags)) {
721                 foreach($tags as $t)
722                         if (($type != 'html') OR ($t[0] != "@"))
723                                 xml_add_element($doc, $entry, "category", "", array("scheme" => "X-DFRN:".$t[0].":".$t[1], "term" => $t[2]));
724         }
725
726         if(count($tags))
727                 foreach($tags as $t)
728                         if ($t[0] == "@")
729                                 $mentioned[$t[1]] = $t[1];
730
731         foreach ($mentioned AS $mention) {
732                 $r = q("SELECT `forum`, `prv` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
733                         intval($owner["uid"]),
734                         dbesc(normalise_link($mention)));
735                 if ($r[0]["forum"] OR $r[0]["prv"])
736                         xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
737                                                                                 "ostatus:object-type" => ACTIVITY_OBJ_GROUP,
738                                                                                 "href" => $mention));
739                 else
740                         xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
741                                                                                 "ostatus:object-type" => ACTIVITY_OBJ_PERSON,
742                                                                                 "href" => $mention));
743         }
744
745         dfrn_get_attachment($doc, $entry, $item);
746
747         return $entry;
748 }