]> git.mxchange.org Git - friendica.git/blob - src/Module/Diaspora/Receive.php
Remove deprecated App::getHostName() - process methods to DI::baseUrl()->getHostName()
[friendica.git] / src / Module / Diaspora / Receive.php
1 <?php
2
3 namespace Friendica\Module\Diaspora;
4
5 use Friendica\App;
6 use Friendica\BaseModule;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Core\L10n\L10n;
9 use Friendica\DI;
10 use Friendica\Model\User;
11 use Friendica\Network\HTTPException;
12 use Friendica\Protocol\Diaspora;
13 use Friendica\Util\Network;
14 use Psr\Log\LoggerInterface;
15
16 /**
17  * This module is part of the Diaspora protocol.
18  * It is used for receiving single posts either for public or for a specific user.
19  */
20 class Receive extends BaseModule
21 {
22         /** @var LoggerInterface */
23         private static $logger;
24
25         public static function init(array $parameters = [])
26         {
27                 self::$logger = DI::logger();
28         }
29
30         public static function post(array $parameters = [])
31         {
32                 $enabled = DI::config()->get('system', 'diaspora_enabled', false);
33                 if (!$enabled) {
34                         self::$logger->info('Diaspora disabled.');
35                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
36                 }
37
38                 $args = DI::args();
39
40                 $type = $args->get(1);
41
42                 switch ($type) {
43                         case 'public':
44                                 self::receivePublic();
45                                 break;
46                         case 'users':
47                                 self::receiveUser($args->get(2));
48                                 break;
49                         default:
50                                 self::$logger->info('Wrong call.');
51                                 throw new HTTPException\BadRequestException('wrong call.');
52                                 break;
53                 }
54         }
55
56         /**
57          * Receive a public Diaspora posting
58          *
59          * @throws HTTPException\InternalServerErrorException
60          * @throws \ImagickException
61          */
62         private static function receivePublic()
63         {
64                 self::$logger->info('Diaspora: Receiving post.');
65
66                 $msg = self::decodePost();
67
68                 self::$logger->info('Diaspora: Dispatching.');
69
70                 Diaspora::dispatchPublic($msg);
71         }
72
73         /**
74          * Receive a Diaspora posting for a user
75          *
76          * @param string $guid The GUID of the importer
77          *
78          * @throws HTTPException\InternalServerErrorException
79          * @throws \ImagickException
80          */
81         private static function receiveUser(string $guid)
82         {
83                 self::$logger->info('Diaspora: Receiving post.');
84
85                 $importer = User::getByGuid($guid);
86
87                 $msg = self::decodePost(false, $importer['prvkey'] ?? '');
88
89                 self::$logger->info('Diaspora: Dispatching.');
90
91                 if (Diaspora::dispatch($importer, $msg)) {
92                         throw new HTTPException\OKException();
93                 } else {
94                         throw new HTTPException\InternalServerErrorException();
95                 }
96         }
97
98         /**
99          * Decodes a Diaspora message based on the posted data
100          *
101          * @param string $privKey The private key of the importer
102          * @param bool   $public  True, if the post is public
103          *
104          * @return array
105          * @throws HTTPException\InternalServerErrorException
106          * @throws \ImagickException
107          */
108         private static function decodePost(bool $public = true, string $privKey = '')
109         {
110                 if (empty($_POST['xml'])) {
111
112                         $postdata = Network::postdata();
113
114                         if (empty($postdata)) {
115                                 throw new HTTPException\InternalServerErrorException('Missing postdata.');
116                         }
117
118                         self::$logger->info('Diaspora: Message is in the new format.');
119
120                         $msg = Diaspora::decodeRaw($postdata, $privKey);
121                 } else {
122
123                         $xml = urldecode($_POST['xml']);
124
125                         self::$logger->info('Diaspora: Decode message in the old format.');
126                         $msg = Diaspora::decode($xml, $privKey);
127
128                         if ($public && !$msg) {
129                                 self::$logger->info('Diaspora: Decode message in the new format.');
130                                 $msg = Diaspora::decodeRaw($xml, $privKey);
131                         }
132                 }
133
134                 self::$logger->info('Diaspora: Post decoded.');
135                 self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
136
137                 if (!is_array($msg)) {
138                         throw new HTTPException\InternalServerErrorException('Message is not an array.');
139                 }
140
141                 return $msg;
142         }
143 }