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