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