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`
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"]);
38 $body = bb2diaspora($item[0]["body"]);
40 if(strlen($item[0]["title"]))
41 $body = "## ".html_entity_decode($item[0]["title"])."\n\n".$body;
43 $nodename = "status_message";
44 $post["raw_message"] = str_replace("&", "&", $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"];
53 $data = array("XML" => array("post" => array($nodename => $post)));
54 $xml = xml::from_array($data, $xmlobj);
56 $r = q("SELECT `guid`, `prvkey` FROM `user` WHERE `uid` = %d", intval($item[0]["uid"]));
58 header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
64 $b64url_data = base64url_encode($xml);
65 $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
67 $key_id = base64url_encode($user["guid"]);
68 $type = "application/xml";
69 $encoding = "base64url";
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);
75 $xmldata = array("me:env" => array("me:data" => $data,
76 "@attributes" => array("type" => $type),
77 "me:encoding" => $encoding,
80 "@attributes2" => array("key_id" => $key_id)));
82 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
84 $envelope = xml::from_array($xmldata, $xml, false, $namespaces);
85 header("Content-Type: application/xml; charset=utf-8");