1) Refactor App->config[] into Core\Config
[friendica.git/.git] / mod / fetch.php
1 <?php
2 /*
3 This file is part of the Diaspora protocol. It is used for fetching single public posts.
4 */
5
6 use Friendica\App;
7 use Friendica\Core\Protocol;
8 use Friendica\Core\System;
9 use Friendica\Protocol\Diaspora;
10 use Friendica\Model\Item;
11 use Friendica\Model\User;
12 use Friendica\Util\Strings;
13 use Friendica\Database\DBA;
14
15 function fetch_init(App $a)
16 {
17
18         if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) {
19                 System::httpExit(404);
20         }
21
22         $guid = $a->argv[2];
23
24         // Fetch the item
25         $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network',
26                 'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'attach'];
27         $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
28         $item = Item::selectFirst($fields, $condition);
29         if (!DBA::isResult($item)) {
30                 $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
31                 $item = Item::selectFirst(['author-link'], $condition);
32                 if (DBA::isResult($item)) {
33                         $parts = parse_url($item["author-link"]);
34                         $host = $parts["scheme"]."://".$parts["host"];
35
36                         if (Strings::normaliseLink($host) != Strings::normaliseLink(System::baseUrl())) {
37                                 $location = $host."/fetch/".$a->argv[1]."/".urlencode($guid);
38
39                                 header("HTTP/1.1 301 Moved Permanently");
40                                 header("Location:".$location);
41                                 exit();
42                         }
43                 }
44
45                 System::httpExit(404);
46         }
47
48         // Fetch some data from the author (We could combine both queries - but I think this is more readable)
49         $user = User::getOwnerDataById($item["uid"]);
50         if (!$user) {
51                 System::httpExit(404);
52         }
53
54         $status = Diaspora::buildStatus($item, $user);
55         $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
56
57         // Send the envelope
58         header("Content-Type: application/magic-envelope+xml; charset=utf-8");
59         echo Diaspora::buildMagicEnvelope($xml, $user);
60
61         exit();
62 }