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