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