]> git.mxchange.org Git - friendica.git/blob - mod/fetch.php
It should be now valid
[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`, `coord`
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
39                 $location = array();
40
41                 if ($item[0]["location"] != "")
42                         $location["address"] = $item[0]["location"];
43
44                 if ($item[0]["coord"] != "") {
45                         $coord = explode(" ", $item[0]["coord"]);
46                         $location["lat"] = $coord[0];
47                         $location["lng"] = $coord[1];
48                 }
49
50                 $body = bb2diaspora($item[0]["body"]);
51
52                 if(strlen($item[0]["title"]))
53                         $body = "## ".html_entity_decode($item[0]["title"])."\n\n".$body;
54
55                 $nodename = "status_message";
56                 $post["raw_message"] = str_replace("&", "&amp;", $body);
57                 $post["location"] = $location;
58                 $post["guid"] = $item[0]["guid"];
59                 $post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]);
60                 $post["public"] = (!$item[0]["private"] ? 'true':'false');
61                 $post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
62                 $post["provider_display_name"] = $item[0]["app"];
63         }
64
65         $data = array("XML" => array("post" => array($nodename => $post)));
66         $xml = xml::from_array($data, $xmlobj);
67
68         $r = q("SELECT `user`.`prvkey`, `contact`.`addr`, `user`.`nickname`, `contact`.`nick` FROM `user`
69                 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid`
70                 WHERE `user`.`uid` = %d", intval($item[0]["uid"]));
71         if (!$r) {
72                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
73                 killme();
74         }
75
76         $user = $r[0];
77
78         $b64url_data = base64url_encode($xml);
79         $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
80
81         $key_id = base64url_encode(diaspora::my_handle($user));
82         $type = "application/xml";
83         $encoding = "base64url";
84         $alg = "RSA-SHA256";
85         $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
86         $signature = rsa_sign($signable_data, $user["prvkey"]);
87         $sig = base64url_encode($signature);
88
89         $xmldata = array("me:env" => array("me:data" => $data,
90                                                         "@attributes" => array("type" => $type),
91                                                         "me:encoding" => $encoding,
92                                                         "me:alg" => $alg,
93                                                         "me:sig" => $sig,
94                                                         "@attributes2" => array("key_id" => $key_id)));
95
96         $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
97
98         //header("Content-Type: application/xml; charset=utf-8");
99         //echo $xml;
100         //killme();
101
102         $envelope = xml::from_array($xmldata, $xml, false, $namespaces);
103         header("Content-Type: application/magic-envelope+xml; charset=utf-8");
104         echo $envelope;
105         killme();
106 }