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