3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Diaspora;
24 use Friendica\BaseModule;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\System;
28 use Friendica\Model\Item;
29 use Friendica\Model\Post;
30 use Friendica\Model\User;
31 use Friendica\Network\HTTPException;
32 use Friendica\Protocol\Diaspora;
33 use Friendica\Util\Strings;
36 * This module is part of the Diaspora protocol.
37 * It is used for fetching single public posts.
39 class Fetch extends BaseModule
41 public static function rawContent(array $parameters = [])
43 if (empty($parameters['guid'])) {
44 throw new HTTPException\NotFoundException();
47 $guid = $parameters['guid'];
51 'uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'received', 'app', 'location', 'coord', 'network',
52 'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'uri-id'
54 $condition = ['origin' => true, 'private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => $guid,
55 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
56 $item = Post::selectFirst($fields, $condition);
58 $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
59 $item = Post::selectFirst(['author-link'], $condition);
60 if (!empty($item["author-link"])) {
61 $parts = parse_url($item["author-link"]);
62 if (empty($parts["scheme"]) || empty($parts["host"])) {
63 throw new HTTPException\InternalServerErrorException();
65 $host = $parts["scheme"] . "://" . $parts["host"];
67 if (Strings::normaliseLink($host) != Strings::normaliseLink(DI::baseUrl()->get())) {
68 $location = $host . "/fetch/" . DI::args()->getArgv()[1] . "/" . urlencode($guid);
69 System::externalRedirect($location, 301);
73 throw new HTTPException\NotFoundException();
76 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
77 $user = User::getOwnerDataById($item["uid"]);
79 throw new HTTPException\NotFoundException();
82 if ($item['gravity'] == GRAVITY_PARENT) {
83 $status = Diaspora::buildStatus($item, $user);
85 $status = Diaspora::constructComment($item, $user);
88 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
91 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
92 echo Diaspora::buildMagicEnvelope($xml, $user);