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