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