]> git.mxchange.org Git - friendica.git/blob - mod/p.php
Merge branch 'master' into develop
[friendica.git] / mod / p.php
1 <?php
2 /*
3 This file is part of the Diaspora protocol. It is used for fetching single public posts.
4 */
5 require_once("include/diaspora.php");
6
7 function p_init($a){
8         if ($a->argc != 2) {
9                 header($_SERVER["SERVER_PROTOCOL"].' 510 '.t('Not Extended'));
10                 killme();
11         }
12
13         $guid = $a->argv[1];
14
15         if (strtolower(substr($guid, -4)) != ".xml") {
16                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
17                 killme();
18         }
19
20         $guid = strtolower(substr($guid, 0, -4));
21
22         $item = q("SELECT `body`, `guid`, `contact-id`, `private`, `created`, `app` FROM `item` WHERE `uid` = 0 AND `guid` = '%s' AND `network` IN ('%s', '%s') LIMIT 1",
23                 dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA);
24         if (!$item) {
25                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
26                 killme();
27         }
28
29         $post = array();
30
31         $reshared = diaspora_is_reshare($item[0]["body"]);
32
33         if ($reshared) {
34                 $nodename = "reshare";
35                 $post["root_diaspora_id"] = $reshared["root_handle"];
36                 $post["root_guid"] = $reshared["root_guid"];
37                 $post["guid"] = $item[0]["guid"];
38                 $post["diaspora_handle"] = diaspora_handle_from_contact($item[0]["contact-id"]);
39                 $post["public"] = (!$item[0]["private"] ? 'true':'false');
40                 $post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
41         } else {
42                 $nodename = "status_message";
43                 $post["raw_message"] = str_replace("&", "&amp;", bb2diaspora($item[0]["body"]));
44                 $post["guid"] = $item[0]["guid"];
45                 $post["diaspora_handle"] = diaspora_handle_from_contact($item[0]["contact-id"]);
46                 $post["public"] = (!$item[0]["private"] ? 'true':'false');
47                 $post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
48                 $post["provider_display_name"] = $item[0]["app"];
49         }
50
51         $dom = new DOMDocument("1.0");
52         $root = $dom->createElement("XML");
53         $dom->appendChild($root);
54         $postelement = $dom->createElement("post");
55         $root->appendChild($postelement);
56         $statuselement = $dom->createElement($nodename);
57         $postelement->appendChild($statuselement);
58
59         foreach($post AS $index => $value) {
60                 $postnode = $dom->createElement($index, $value);
61                 $statuselement->appendChild($postnode);
62         }
63
64         header("Content-Type: application/xml; charset=utf-8");
65         $xml = $dom->saveXML();
66
67         // Diaspora doesn't send the XML header, so we remove them as well.
68         // So we avoid possible compatibility problems.
69         if (substr($xml, 0, 21) == '<?xml version="1.0"?>')
70                 $xml = trim(substr($xml, 21));
71
72         echo $xml;
73
74         killme();
75 }