3 namespace Friendica\Module\Diaspora;
5 use Friendica\BaseModule;
6 use Friendica\Core\Protocol;
7 use Friendica\Core\System;
8 use Friendica\Model\Item;
9 use Friendica\Model\User;
10 use Friendica\Network\HTTPException;
11 use Friendica\Protocol\Diaspora;
12 use Friendica\Util\Strings;
15 * This module is part of the Diaspora protocol.
16 * It is used for fetching single public posts.
18 class Fetch extends BaseModule
20 public static function rawContent()
22 $app = self::getApp();
24 // @TODO: Replace with parameter from router
25 if (($app->argc != 3) || (!in_array($app->argv[1], ["post", "status_message", "reshare"]))) {
26 throw new HTTPException\NotFoundException();
29 // @TODO: Replace with parameter from router
30 $guid = $app->argv[2];
34 'uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network',
35 'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'attach'
37 $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
38 $item = Item::selectFirst($fields, $condition);
40 $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
41 $item = Item::selectFirst(['author-link'], $condition);
43 $parts = parse_url($item["author-link"]);
44 if (empty($parts["scheme"]) || empty($parts["host"])) {
45 throw new HTTPException\InternalServerErrorException();
47 $host = $parts["scheme"] . "://" . $parts["host"];
49 if (Strings::normaliseLink($host) != Strings::normaliseLink($app->getBaseURL())) {
50 $location = $host . "/fetch/" . $app->argv[1] . "/" . urlencode($guid);
51 System::externalRedirect($location, 301);
55 throw new HTTPException\NotFoundException();
58 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
59 $user = User::getOwnerDataById($item["uid"]);
61 throw new HTTPException\NotFoundException();
64 $status = Diaspora::buildStatus($item, $user);
65 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
68 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
69 echo Diaspora::buildMagicEnvelope($xml, $user);