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\Protocol;
9 use Friendica\Core\System;
10 use Friendica\Protocol\Diaspora;
11 use Friendica\Model\Item;
12 use Friendica\Model\User;
13 use Friendica\Util\Strings;
14 use Friendica\Util\XML;
15 use Friendica\Database\DBA;
17 function fetch_init(App $a)
20 if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) {
21 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
28 $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network',
29 'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'attach'];
30 $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
31 $item = Item::selectFirst($fields, $condition);
32 if (!DBA::isResult($item)) {
33 $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
34 $item = Item::selectFirst(['author-link'], $condition);
35 if (DBA::isResult($item)) {
36 $parts = parse_url($item["author-link"]);
37 $host = $parts["scheme"]."://".$parts["host"];
39 if (Strings::normaliseLink($host) != Strings::normaliseLink(System::baseUrl())) {
40 $location = $host."/fetch/".$a->argv[1]."/".urlencode($guid);
42 header("HTTP/1.1 301 Moved Permanently");
43 header("Location:".$location);
48 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
52 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
53 $user = User::getOwnerDataById($item["uid"]);
55 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
59 $status = Diaspora::buildStatus($item, $user);
60 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
63 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
64 echo Diaspora::buildMagicEnvelope($xml, $user);