]> git.mxchange.org Git - friendica.git/blob - src/Module/Diaspora/Fetch.php
Remove deprecated App::getBaseURL() - process methods to DI::baseUrl()->get()
[friendica.git] / src / Module / Diaspora / Fetch.php
1 <?php
2
3 namespace Friendica\Module\Diaspora;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\Protocol;
7 use Friendica\Core\System;
8 use Friendica\DI;
9 use Friendica\Model\Item;
10 use Friendica\Model\User;
11 use Friendica\Network\HTTPException;
12 use Friendica\Protocol\Diaspora;
13 use Friendica\Util\Strings;
14
15 /**
16  * This module is part of the Diaspora protocol.
17  * It is used for fetching single public posts.
18  */
19 class Fetch extends BaseModule
20 {
21         public static function rawContent(array $parameters = [])
22         {
23                 $app = DI::app();
24
25                 // @TODO: Replace with parameter from router
26                 if (($app->argc != 3) || (!in_array($app->argv[1], ["post", "status_message", "reshare"]))) {
27                         throw new HTTPException\NotFoundException();
28                 }
29
30                 // @TODO: Replace with parameter from router
31                 $guid = $app->argv[2];
32
33                 // Fetch the item
34                 $fields = [
35                         'uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'received', 'app', 'location', 'coord', 'network',
36                         'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'attach'
37                 ];
38                 $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
39                 $item = Item::selectFirst($fields, $condition);
40                 if (empty($item)) {
41                         $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
42                         $item = Item::selectFirst(['author-link'], $condition);
43                         if (empty($item)) {
44                                 $parts = parse_url($item["author-link"]);
45                                 if (empty($parts["scheme"]) || empty($parts["host"])) {
46                                         throw new HTTPException\InternalServerErrorException();
47                                 }
48                                 $host = $parts["scheme"] . "://" . $parts["host"];
49
50                                 if (Strings::normaliseLink($host) != Strings::normaliseLink(DI::baseUrl()->get())) {
51                                         $location = $host . "/fetch/" . $app->argv[1] . "/" . urlencode($guid);
52                                         System::externalRedirect($location, 301);
53                                 }
54                         }
55
56                         throw new HTTPException\NotFoundException();
57                 }
58
59                 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
60                 $user = User::getOwnerDataById($item["uid"]);
61                 if (!$user) {
62                         throw new HTTPException\NotFoundException();
63                 }
64
65                 $status = Diaspora::buildStatus($item, $user);
66                 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
67
68                 // Send the envelope
69                 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
70                 echo Diaspora::buildMagicEnvelope($xml, $user);
71
72                 exit();
73         }
74 }