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