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