]> git.mxchange.org Git - friendica.git/blob - mod/receive.php
Merge pull request #6977 from annando/fix-reshare
[friendica.git] / mod / receive.php
1 <?php
2 /**
3  * @file mod/receive.php
4  * @brief Diaspora endpoint
5  */
6
7 use Friendica\App;
8 use Friendica\Core\Config;
9 use Friendica\Core\Logger;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\Protocol\Diaspora;
13
14 /**
15  * @param App $a App
16  * @return void
17  * @throws ImagickException
18  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
19  */
20 function receive_post(App $a)
21 {
22         $enabled = intval(Config::get('system', 'diaspora_enabled'));
23         if (!$enabled) {
24                 Logger::log('mod-diaspora: disabled');
25                 System::httpExit(500);
26         }
27
28         if (($a->argc == 2) && ($a->argv[1] === 'public')) {
29                 $public = true;
30                 $importer = [];
31         } else {
32                 $public = false;
33
34                 if ($a->argc != 3 || $a->argv[1] !== 'users') {
35                         System::httpExit(500);
36                 }
37                 $guid = $a->argv[2];
38
39                 $importer = DBA::selectFirst('user', [], ['guid' => $guid, 'account_expired' => false, 'account_removed' => false]);
40                 if (!DBA::isResult($importer)) {
41                         System::httpExit(500);
42                 }
43         }
44
45         // It is an application/x-www-form-urlencoded
46
47         Logger::log('mod-diaspora: receiving post', Logger::DEBUG);
48
49         if (empty($_POST['xml'])) {
50                 $postdata = file_get_contents("php://input");
51                 if ($postdata == '') {
52                         System::httpExit(500);
53                 }
54
55                 Logger::log('mod-diaspora: message is in the new format', Logger::DEBUG);
56                 $msg = Diaspora::decodeRaw($importer, $postdata);
57         } else {
58                 $xml = urldecode($_POST['xml']);
59
60                 Logger::log('mod-diaspora: decode message in the old format', Logger::DEBUG);
61                 $msg = Diaspora::decode($importer, $xml);
62
63                 if ($public && !$msg) {
64                         Logger::log('mod-diaspora: decode message in the new format', Logger::DEBUG);
65                         $msg = Diaspora::decodeRaw($importer, $xml);
66                 }
67         }
68
69         Logger::log('mod-diaspora: decoded', Logger::DEBUG);
70
71         Logger::log('mod-diaspora: decoded msg: ' . print_r($msg, true), Logger::DATA);
72
73         if (!is_array($msg)) {
74                 System::httpExit(500);
75         }
76
77         Logger::log('mod-diaspora: dispatching', Logger::DEBUG);
78
79         $ret = true;
80         if ($public) {
81                 Diaspora::dispatchPublic($msg);
82         } else {
83                 $ret = Diaspora::dispatch($importer, $msg);
84         }
85
86         System::httpExit(($ret) ? 200 : 500);
87         // NOTREACHED
88 }