3 This file is part of the Diaspora protocol. It is used for fetching single public posts.
5 require_once("include/crypto.php");
6 require_once("include/diaspora.php");
7 require_once("include/xml.php");
9 function fetch_init($a){
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'));
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);
22 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
27 $reshared = diaspora::is_reshare($item[0]["body"]);
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"]);
41 if ($item[0]["location"] != "")
42 $location["address"] = $item[0]["location"];
44 if ($item[0]["coord"] != "") {
45 $coord = explode(" ", $item[0]["coord"]);
46 $location["lat"] = $coord[0];
47 $location["lng"] = $coord[1];
50 $body = bb2diaspora($item[0]["body"]);
52 if(strlen($item[0]["title"]))
53 $body = "## ".html_entity_decode($item[0]["title"])."\n\n".$body;
55 $nodename = "status_message";
56 $post["raw_message"] = str_replace("&", "&", $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"];
65 $data = array("XML" => array("post" => array($nodename => $post)));
66 $xml = xml::from_array($data, $xmlobj);
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"]));
72 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
78 $b64url_data = base64url_encode($xml);
79 $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
81 $key_id = base64url_encode(diaspora::my_handle($user));
82 $type = "application/xml";
83 $encoding = "base64url";
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);
89 $xmldata = array("me:env" => array("me:data" => $data,
90 "@attributes" => array("type" => $type),
91 "me:encoding" => $encoding,
94 "@attributes2" => array("key_id" => $key_id)));
96 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
98 //header("Content-Type: application/xml; charset=utf-8");
102 $envelope = xml::from_array($xmldata, $xml, false, $namespaces);
103 header("Content-Type: application/magic-envelope+xml; charset=utf-8");