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