Add redirect for fetch
[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                                         $app->redirect($location);
48                                 }
49                         }
50
51                         throw new HTTPException\NotFoundException();
52                 }
53
54                 // Fetch some data from the author (We could combine both queries - but I think this is more readable)
55                 $user = User::getOwnerDataById($item["uid"]);
56                 if (!$user) {
57                         throw new HTTPException\NotFoundException();
58                 }
59
60                 $status = Diaspora::buildStatus($item, $user);
61                 $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
62
63                 // Send the envelope
64                 header("Content-Type: application/magic-envelope+xml; charset=utf-8");
65                 echo Diaspora::buildMagicEnvelope($xml, $user);
66
67                 exit();
68         }
69 }