]> git.mxchange.org Git - friendica.git/blob - mod/fetch.php
Move mod/home to src/Module/Home
[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\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         if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) {
18                 throw new \Friendica\Network\HTTPException\NotFoundException();
19         }
20
21         $guid = $a->argv[2];
22
23         // Fetch the item
24         $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network',
25                 'event-id', 'resource-id', 'author-link', 'author-avatar', 'author-name', 'plink', 'owner-link', 'attach'];
26         $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
27         $item = Item::selectFirst($fields, $condition);
28         if (!DBA::isResult($item)) {
29                 $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
30                 $item = Item::selectFirst(['author-link'], $condition);
31                 if (DBA::isResult($item)) {
32                         $parts = parse_url($item["author-link"]);
33                         $host = $parts["scheme"]."://".$parts["host"];
34
35                         if (Strings::normaliseLink($host) != Strings::normaliseLink(System::baseUrl())) {
36                                 $location = $host."/fetch/".$a->argv[1]."/".urlencode($guid);
37
38                                 header("HTTP/1.1 301 Moved Permanently");
39                                 header("Location:".$location);
40                                 exit();
41                         }
42                 }
43
44                 throw new \Friendica\Network\HTTPException\NotFoundException();
45         }
46
47         // Fetch some data from the author (We could combine both queries - but I think this is more readable)
48         $user = User::getOwnerDataById($item["uid"]);
49         if (!$user) {
50                 throw new \Friendica\Network\HTTPException\NotFoundException();
51         }
52
53         $status = Diaspora::buildStatus($item, $user);
54         $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
55
56         // Send the envelope
57         header("Content-Type: application/magic-envelope+xml; charset=utf-8");
58         echo Diaspora::buildMagicEnvelope($xml, $user);
59
60         exit();
61 }