]> git.mxchange.org Git - friendica.git/blob - mod/fetch.php
Fixes:
[friendica.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\L10n;
8 use Friendica\Core\System;
9 use Friendica\Protocol\Diaspora;
10 use Friendica\Model\Item;
11 use Friendica\Model\User;
12 use Friendica\Util\XML;
13 use Friendica\Database\DBM;
14
15 function fetch_init(App $a)
16 {
17
18         if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) {
19                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
20                 killme();
21         }
22
23         $guid = $a->argv[2];
24
25         // Fetch the item
26         $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network',
27                 'event-id', 'resource-id', 'author-link', 'owner-link', 'attach'];
28         $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
29         $item = Item::selectFirst($fields, $condition);
30         if (!DBM::is_result($item)) {
31                 $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
32                 $item = Item::selectFirst(['author-link'], $condition);
33                 if (DBM::is_result($item)) {
34                         $parts = parse_url($item["author-link"]);
35                         $host = $parts["scheme"]."://".$parts["host"];
36
37                         if (normalise_link($host) != normalise_link(System::baseUrl())) {
38                                 $location = $host."/fetch/".$a->argv[1]."/".urlencode($guid);
39
40                                 header("HTTP/1.1 301 Moved Permanently");
41                                 header("Location:".$location);
42                                 killme();
43                         }
44                 }
45
46                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
47                 killme();
48         }
49
50         // Fetch some data from the author (We could combine both queries - but I think this is more readable)
51         $user = User::getOwnerDataById($item["uid"]);
52         if (!$user) {
53                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
54                 killme();
55         }
56
57         $status = Diaspora::buildStatus($item, $user);
58         $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
59
60         // Send the envelope
61         header("Content-Type: application/magic-envelope+xml; charset=utf-8");
62         echo Diaspora::buildMagicEnvelope($xml, $user);
63
64         killme();
65 }