]> git.mxchange.org Git - friendica.git/blob - src/Module/Diaspora/Fetch.php
Merge pull request #9783 from foss-/patch-1
[friendica.git] / src / Module / Diaspora / Fetch.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Diaspora;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\System;
27 use Friendica\DI;
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;
34
35 /**
36  * This module is part of the Diaspora protocol.
37  * It is used for fetching single public posts.
38  */
39 class Fetch extends BaseModule
40 {
41         public static function rawContent(array $parameters = [])
42         {
43                 $app = DI::app();
44
45                 // @TODO: Replace with parameter from router
46                 if (($app->argc != 3) || (!in_array($app->argv[1], ["post", "status_message", "reshare"]))) {
47                         throw new HTTPException\NotFoundException();
48                 }
49
50                 // @TODO: Replace with parameter from router
51                 $guid = $app->argv[2];
52
53                 // Fetch the item
54                 $fields = [
55                         'uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'received', 'app', 'location', 'coord', 'network',
56                         'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'uri-id'
57                 ];
58                 $condition = ['wall' => true, 'private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
59                 $item = Post::selectFirst($fields, $condition);
60                 if (empty($item)) {
61                         $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
62                         $item = Post::selectFirst(['author-link'], $condition);
63                         if (!empty($item["author-link"])) {
64                                 $parts = parse_url($item["author-link"]);
65                                 if (empty($parts["scheme"]) || empty($parts["host"])) {
66                                         throw new HTTPException\InternalServerErrorException();
67                                 }
68                                 $host = $parts["scheme"] . "://" . $parts["host"];
69
70                                 if (Strings::normaliseLink($host) != Strings::normaliseLink(DI::baseUrl()->get())) {
71                                         $location = $host . "/fetch/" . $app->argv[1] . "/" . urlencode($guid);
72                                         System::externalRedirect($location, 301);
73                                 }
74                         }
75
76                         throw new HTTPException\NotFoundException();
77                 }
78
79                 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
80                 $user = User::getOwnerDataById($item["uid"]);
81                 if (!$user) {
82                         throw new HTTPException\NotFoundException();
83                 }
84
85                 $status = Diaspora::buildStatus($item, $user);
86                 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
87
88                 // Send the envelope
89                 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
90                 echo Diaspora::buildMagicEnvelope($xml, $user);
91
92                 exit();
93         }
94 }