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