Fix wrong condition in Diaspora\Fetch
[friendica.git/.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\User;
30 use Friendica\Network\HTTPException;
31 use Friendica\Protocol\Diaspora;
32 use Friendica\Util\Strings;
33
34 /**
35  * This module is part of the Diaspora protocol.
36  * It is used for fetching single public posts.
37  */
38 class Fetch extends BaseModule
39 {
40         public static function rawContent(array $parameters = [])
41         {
42                 $app = DI::app();
43
44                 // @TODO: Replace with parameter from router
45                 if (($app->argc != 3) || (!in_array($app->argv[1], ["post", "status_message", "reshare"]))) {
46                         throw new HTTPException\NotFoundException();
47                 }
48
49                 // @TODO: Replace with parameter from router
50                 $guid = $app->argv[2];
51
52                 // Fetch the item
53                 $fields = [
54                         'uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'received', 'app', 'location', 'coord', 'network',
55                         'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'attach'
56                 ];
57                 $condition = ['wall' => true, 'private' => [Item::PUBLIC, Item::UNLISTED], 'guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
58                 $item = Item::selectFirst($fields, $condition);
59                 if (empty($item)) {
60                         $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
61                         $item = Item::selectFirst(['author-link'], $condition);
62                         if (!empty($item["author-link"])) {
63                                 $parts = parse_url($item["author-link"]);
64                                 if (empty($parts["scheme"]) || empty($parts["host"])) {
65                                         throw new HTTPException\InternalServerErrorException();
66                                 }
67                                 $host = $parts["scheme"] . "://" . $parts["host"];
68
69                                 if (Strings::normaliseLink($host) != Strings::normaliseLink(DI::baseUrl()->get())) {
70                                         $location = $host . "/fetch/" . $app->argv[1] . "/" . urlencode($guid);
71                                         System::externalRedirect($location, 301);
72                                 }
73                         }
74
75                         throw new HTTPException\NotFoundException();
76                 }
77
78                 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
79                 $user = User::getOwnerDataById($item["uid"]);
80                 if (!$user) {
81                         throw new HTTPException\NotFoundException();
82                 }
83
84                 $status = Diaspora::buildStatus($item, $user);
85                 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
86
87                 // Send the envelope
88                 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
89                 echo Diaspora::buildMagicEnvelope($xml, $user);
90
91                 exit();
92         }
93 }