]> git.mxchange.org Git - friendica.git/blob - mod/fetch.php
Remove unused global cache constants
[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\Util\XML;
11
12 function fetch_init(App $a)
13 {
14
15         if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) {
16                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
17                 killme();
18         }
19
20         $guid = $a->argv[2];
21
22         // Fetch the item
23         $item = q(
24                 "SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`, `coord`
25                         FROM `item` WHERE `wall` AND NOT `private` AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
26                 dbesc($guid),
27                 NETWORK_DFRN,
28                 NETWORK_DIASPORA
29         );
30         if (!$item) {
31                 $r = q(
32                         "SELECT `author-link`
33                         FROM `item` WHERE `uid` = 0 AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
34                         dbesc($guid),
35                         NETWORK_DFRN,
36                         NETWORK_DIASPORA
37                 );
38
39                 if ($r) {
40                         $parts = parse_url($r[0]["author-link"]);
41                         $host = $parts["scheme"]."://".$parts["host"];
42
43                         if (normalise_link($host) != normalise_link(System::baseUrl())) {
44                                 $location = $host."/fetch/".$a->argv[1]."/".urlencode($guid);
45
46                                 header("HTTP/1.1 301 Moved Permanently");
47                                 header("Location:".$location);
48                                 killme();
49                         }
50                 }
51
52                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
53                 killme();
54         }
55
56         // Fetch some data from the author (We could combine both queries - but I think this is more readable)
57         $r = q(
58                 "SELECT `user`.`prvkey`, `contact`.`addr`, `user`.`nickname`, `contact`.`nick` FROM `user`
59                 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
60                 WHERE `user`.`uid` = %d",
61                 intval($item[0]["uid"])
62         );
63
64         if (!$r) {
65                 header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
66                 killme();
67         }
68         $user = $r[0];
69
70         $status = Diaspora::buildStatus($item[0], $user);
71         $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
72
73         // Send the envelope
74         header("Content-Type: application/magic-envelope+xml; charset=utf-8");
75         echo Diaspora::buildMagicEnvelope($xml, $user);
76
77         killme();
78 }