]> git.mxchange.org Git - friendica.git/blob - src/Module/Diaspora/Fetch.php
Switch `static::$parameters` to `$this->parameters`
[friendica.git] / src / Module / Diaspora / Fetch.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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 function rawContent()
42         {
43                 if (empty($this->parameters['guid'])) {
44                         throw new HTTPException\NotFoundException();
45                 }
46
47                 $guid = $this->parameters['guid'];
48
49                 // Fetch the item
50                 $condition = ['origin' => true, 'private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => $guid,
51                         'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
52                 $item = Post::selectFirst([], $condition);
53                 if (empty($item)) {
54                         $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
55                         $item = Post::selectFirst(['author-link'], $condition);
56                         if (!empty($item["author-link"])) {
57                                 $parts = parse_url($item["author-link"]);
58                                 if (empty($parts["scheme"]) || empty($parts["host"])) {
59                                         throw new HTTPException\InternalServerErrorException();
60                                 }
61                                 $host = $parts["scheme"] . "://" . $parts["host"];
62
63                                 if (Strings::normaliseLink($host) != Strings::normaliseLink(DI::baseUrl()->get())) {
64                                         $location = $host . "/fetch/" . DI::args()->getArgv()[1] . "/" . urlencode($guid);
65                                         System::externalRedirect($location, 301);
66                                 }
67                         }
68
69                         throw new HTTPException\NotFoundException();
70                 }
71
72                 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
73                 $user = User::getOwnerDataById($item["uid"]);
74                 if (!$user) {
75                         throw new HTTPException\NotFoundException();
76                 }
77
78                 if ($item['gravity'] == GRAVITY_PARENT) {
79                         $status = Diaspora::buildStatus($item, $user);
80                 } else {
81                         $status = ['type' => 'comment', 'message' => Diaspora::createCommentSignature($item)];
82                 }
83
84                 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
85
86                 // Send the envelope
87                 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
88                 echo Diaspora::buildMagicEnvelope($xml, $user);
89
90                 exit();
91         }
92 }