]> git.mxchange.org Git - friendica.git/blob - mod/fetch.php
Diaspora: Support for new fetch functionality (replacement for /p/)
[friendica.git] / mod / fetch.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 fetch_init($a){
8
9         if (($a->argc != 3) OR (!in_array($a->argv[1], array("post", "status_message", "reshare")))) {
10                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
11                 killme();
12         }
13
14         $guid = $a->argv[2];
15
16         $item = q("SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`
17                         FROM `item` WHERE `wall` AND NOT `private`  AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
18                 dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA);
19         if (!$item) {
20                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
21                 killme();
22         }
23         $post = array();
24
25         $reshared = diaspora::is_reshare($item[0]["body"]);
26
27         if ($reshared) {
28                 $nodename = "reshare";
29                 $post["root_diaspora_id"] = $reshared["root_handle"];
30                 $post["root_guid"] = $reshared["root_guid"];
31                 $post["guid"] = $item[0]["guid"];
32                 $post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]);
33                 $post["public"] = (!$item[0]["private"] ? 'true':'false');
34                 $post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
35         } else {
36                 $body = bb2diaspora($item[0]["body"]);
37
38                 if(strlen($item[0]["title"]))
39                         $body = "## ".html_entity_decode($item[0]["title"])."\n\n".$body;
40
41                 $nodename = "status_message";
42                 $post["raw_message"] = str_replace("&", "&amp;", $body);
43                 $post["location"] = $item[0]["location"];
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         $data = array("XML" => array("post" => array($nodename => $post)));
52         $xml = xml::from_array($data, $xmlobj);
53
54         $r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d", intval($item[0]["uid"]));
55         if (!$r) {
56                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
57                 killme();
58         }
59
60         $user = $r[0];
61
62         $key_id = "";
63
64         $b64url_data = base64url_encode($xml);
65         $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
66
67         $type = "application/xml";
68         $encoding = "base64url";
69         $alg = "RSA-SHA256";
70         $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
71         $signature = rsa_sign($signable_data, $user["prvkey"]);
72         $sig = base64url_encode($signature);
73
74         $xmldata = array("me:env" => array("me:data" => $data,
75                                                         "@attributes" => array("type" => $type),
76                                                         "me:encoding" => $encoding,
77                                                         "me:alg" => $alg,
78                                                         "me:sig" => $sig,
79                                                         "@attributes2" => array("key_id" => $key_id)));
80
81         $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
82
83         $envelope = xml::from_array($xmldata, $xml, false, $namespaces);
84         header("Content-Type: application/xml; charset=utf-8");
85         echo $envelope;
86
87         killme();
88 }