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