X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=mod%2Ffetch.php;h=30c38bf543cbe75812c7fea37046cf06972f8e9d;hb=10ede7314136edb1c475e1af546a59ff4c5648cf;hp=1db87f4175a9e250ea787538f161a215d040bede;hpb=2c7fd1cb3c81abc1ef816f022fb894ea93c8c5e7;p=friendica.git diff --git a/mod/fetch.php b/mod/fetch.php index 1db87f4175..30c38bf543 100644 --- a/mod/fetch.php +++ b/mod/fetch.php @@ -2,105 +2,64 @@ /* This file is part of the Diaspora protocol. It is used for fetching single public posts. */ + +use Friendica\App; +use Friendica\Core\System; + require_once("include/crypto.php"); require_once("include/diaspora.php"); require_once("include/xml.php"); -function fetch_init($a){ +function fetch_init(App $a) { - if (($a->argc != 3) OR (!in_array($a->argv[1], array("post", "status_message", "reshare")))) { + if (($a->argc != 3) || (!in_array($a->argv[1], array("post", "status_message", "reshare")))) { header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found')); killme(); } $guid = $a->argv[2]; + // Fetch the item $item = q("SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`, `coord` - FROM `item` WHERE `wall` AND NOT `private` AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1", + FROM `item` WHERE `wall` AND NOT `private` AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1", dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA); if (!$item) { - header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found')); - killme(); - } - $post = array(); - - $reshared = diaspora::is_reshare($item[0]["body"]); - - if ($reshared) { - $nodename = "reshare"; - $post["root_diaspora_id"] = $reshared["root_handle"]; - $post["root_guid"] = $reshared["root_guid"]; - $post["guid"] = $item[0]["guid"]; - $post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]); - $post["public"] = (!$item[0]["private"] ? 'true':'false'); - $post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]); - } else { - - $location = array(); - - if ($item[0]["location"] != "") - $location["address"] = $item[0]["location"]; - - if ($item[0]["coord"] != "") { - $coord = explode(" ", $item[0]["coord"]); - $location["lat"] = $coord[0]; - $location["lng"] = $coord[1]; + $r = q("SELECT `author-link` + FROM `item` WHERE `uid` = 0 AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1", + dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA); + if ($r) { + $parts = parse_url($r[0]["author-link"]); + $host = $parts["scheme"]."://".$parts["host"]; + + if (normalise_link($host) != normalise_link(System::baseUrl())) { + $location = $host."/fetch/".$a->argv[1]."/".urlencode($guid); + + header("HTTP/1.1 301 Moved Permanently"); + header("Location:".$location); + killme(); + } } - $body = bb2diaspora($item[0]["body"]); - - if(strlen($item[0]["title"])) - $body = "## ".html_entity_decode($item[0]["title"])."\n\n".$body; - - $nodename = "status_message"; - $post["raw_message"] = str_replace("&", "&", $body); - $post["location"] = $location; - $post["guid"] = $item[0]["guid"]; - $post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]); - $post["public"] = (!$item[0]["private"] ? 'true':'false'); - $post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]); - $post["provider_display_name"] = $item[0]["app"]; + header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found')); + killme(); } - $data = array("XML" => array("post" => array($nodename => $post))); - $xml = xml::from_array($data, $xmlobj); - + // Fetch some data from the author (We could combine both queries - but I think this is more readable) $r = q("SELECT `user`.`prvkey`, `contact`.`addr`, `user`.`nickname`, `contact`.`nick` FROM `user` - INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` + INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self` WHERE `user`.`uid` = %d", intval($item[0]["uid"])); if (!$r) { header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found')); killme(); } - $user = $r[0]; - $b64url_data = base64url_encode($xml); - $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data); + $status = Diaspora::build_status($item[0], $user); + $xml = Diaspora::build_post_xml($status["type"], $status["message"]); - $key_id = base64url_encode(diaspora::my_handle($user)); - $type = "application/xml"; - $encoding = "base64url"; - $alg = "RSA-SHA256"; - $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg); - $signature = rsa_sign($signable_data, $user["prvkey"]); - $sig = base64url_encode($signature); - - $xmldata = array("me:env" => array("me:data" => $data, - "@attributes" => array("type" => $type), - "me:encoding" => $encoding, - "me:alg" => $alg, - "me:sig" => $sig, - "@attributes2" => array("key_id" => $key_id))); - - $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env"); - - //header("Content-Type: application/xml; charset=utf-8"); - //echo $xml; - //killme(); - - $envelope = xml::from_array($xmldata, $xml, false, $namespaces); + // Send the envelope header("Content-Type: application/magic-envelope+xml; charset=utf-8"); - echo $envelope; + echo Diaspora::build_magic_envelope($xml, $user); + killme(); }