3 This file is part of the Diaspora protocol. It is used for fetching single public posts.
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Protocol\Diaspora;
10 use Friendica\Model\Item;
11 use Friendica\Model\User;
12 use Friendica\Util\XML;
13 use Friendica\Database\DBM;
15 function fetch_init(App $a)
18 if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) {
19 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
26 $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network'];
27 $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
28 $item = Item::selectFirst($fields, $condition);
29 if (!DBM::is_result($item)) {
30 $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
31 $item = Item::selectFirst(['author-link'], $condition);
32 if (DBM::is_result($item)) {
33 $parts = parse_url($item["author-link"]);
34 $host = $parts["scheme"]."://".$parts["host"];
36 if (normalise_link($host) != normalise_link(System::baseUrl())) {
37 $location = $host."/fetch/".$a->argv[1]."/".urlencode($guid);
39 header("HTTP/1.1 301 Moved Permanently");
40 header("Location:".$location);
45 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
49 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
50 $user = User::getOwnerDataById($item["uid"]);
52 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
56 $status = Diaspora::buildStatus($item, $user);
57 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
60 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
61 echo Diaspora::buildMagicEnvelope($xml, $user);