]> git.mxchange.org Git - friendica.git/blob - src/Module/OStatus/PubSub.php
Merge pull request #13724 from Raroun/Fix-for-Issue-#13637---Photo-caption-prevents...
[friendica.git] / src / Module / OStatus / PubSub.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\OStatus;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\System;
28 use Friendica\Database\Database;
29 use Friendica\Model\Contact;
30 use Friendica\Model\GServer;
31 use Friendica\Model\Post;
32 use Friendica\Module\Response;
33 use Friendica\Network\HTTPException;
34 use Friendica\Protocol\OStatus;
35 use Friendica\Util\Network;
36 use Friendica\Util\Profiler;
37 use Friendica\Util\Strings;
38 use Psr\Log\LoggerInterface;
39
40 class PubSub extends \Friendica\BaseModule
41 {
42         /** @var Database */
43         private $database;
44         /** @var App\Request */
45         private $request;
46
47         public function __construct(App\Request $request, Database $database, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
48         {
49                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50
51                 $this->database = $database;
52                 $this->request  = $request;
53         }
54
55         protected function post(array $request = [])
56         {
57                 $xml = Network::postdata();
58
59                 $this->logger->info('Feed arrived.', ['from' =>  $this->request->getRemoteAddress(), 'for' => $this->args->getCommand(), 'user-agent' => $this->server['HTTP_USER_AGENT']]);
60                 $this->logger->debug('Data stream.', ['xml' => $xml]);
61                 $this->logger->debug('Got request data.', ['request' => $request]);
62
63                 $nickname   = $this->parameters['nickname'] ?? '';
64                 $contact_id = $this->parameters['cid']      ?? 0;
65
66                 $importer = $this->database->selectFirst('user', [], ['nickname' => $nickname, 'verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false]);
67                 if (!$importer) {
68                         throw new HTTPException\OKException();
69                 }
70
71                 $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
72                 $contact   = $this->database->selectFirst('contact', [], $condition);
73                 if (!$contact) {
74                         $author = OStatus::salmonAuthor($xml, $importer);
75                         if (!empty($author['contact-id'])) {
76                                 $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
77                                 $contact   = $this->database->selectFirst('contact', [], $condition);
78                                 $this->logger->notice('No record found for nickname, using author entry instead.', ['nickname' =>  $nickname, 'contact-id' => $contact_id, 'author-contact-id' => $author['contact-id']]);
79                         }
80
81                         if (!$contact) {
82                                 $this->logger->notice("Contact wasn't found - ignored.", ['author-link' => $author['author-link'], 'contact-id' => $contact_id, 'nickname' => $nickname, 'xml' => $xml]);
83                                 throw new HTTPException\OKException();
84                         }
85                 }
86
87                 if (!empty($contact['gsid'])) {
88                         GServer::setProtocol($contact['gsid'], Post\DeliveryData::OSTATUS);
89                 }
90
91                 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
92                         $this->logger->notice('Contact is not expected to share with us - ignored.', ['contact-id' => $contact['id']]);
93                         throw new HTTPException\OKException();
94                 }
95
96                 // We only import feeds from OStatus here
97                 if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS])) {
98                         $this->logger->warning('Unexpected network', ['contact' => $contact, 'network' => $contact['network']]);
99                         throw new HTTPException\OKException();
100                 }
101
102                 $this->logger->info('Import item from Contact.', ['nickname' => $nickname, 'contact-nickname' => $contact['nick'], 'contact-id' => $contact['id']]);
103                 $feedhub = '';
104                 OStatus::import($xml, $importer, $contact, $feedhub);
105
106                 throw new HTTPException\OKException();
107         }
108
109         protected function rawContent(array $request = [])
110         {
111                 $nickname   = $this->parameters['nickname'] ?? '';
112                 $contact_id = $this->parameters['cid']      ?? 0;
113
114                 $hub_mode      = trim($request['hub_mode']         ?? '');
115                 $hub_topic     = trim($request['hub_topic']        ?? '');
116                 $hub_challenge = trim($request['hub_challenge']    ?? '');
117                 $hub_verify    = trim($request['hub_verify_token'] ?? '');
118
119                 $this->logger->notice('Subscription start.', ['from' => $this->request->getRemoteAddress(), 'mode' => $hub_mode, 'nickname' => $nickname]);
120                 $this->logger->debug('Data: ', ['get' => $request]);
121
122                 $owner = $this->database->selectFirst('user', ['uid'], ['nickname' => $nickname, 'verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false]);
123                 if (!$owner) {
124                         $this->logger->notice('Local account not found.', ['nickname' => $nickname]);
125                         throw new HTTPException\NotFoundException();
126                 }
127
128                 $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
129
130                 if (!empty($hub_verify)) {
131                         $condition['hub-verify'] = $hub_verify;
132                 }
133
134                 $contact = $this->database->selectFirst('contact', ['id', 'poll'], $condition);
135                 if (!$contact) {
136                         $this->logger->notice('Contact not found.', ['contact' => $contact_id]);
137                         throw new HTTPException\NotFoundException();
138                 }
139
140                 if (!empty($hub_topic) && !Strings::compareLink($hub_topic, $contact['poll'])) {
141                         $this->logger->notice("Hub topic isn't valid for Contact.", ['hub_topic' =>  $hub_topic, 'contact_poll' => $contact['poll']]);
142                         throw new HTTPException\NotFoundException();
143                 }
144
145                 // We must initiate an unsubscribe request with a verify_token.
146                 // Don't allow outsiders to unsubscribe us.
147
148                 if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
149                         $this->logger->notice('Bogus unsubscribe');
150                         throw new HTTPException\NotFoundException();
151                 }
152
153                 if (!empty($hub_mode)) {
154                         Contact::update(['subhub' => $hub_mode === 'subscribe'], ['id' => $contact['id']]);
155                         $this->logger->notice('Success for contact.', ['mode' => $hub_mode, 'contact' => $contact_id]);
156                 }
157
158                 $this->httpExit($hub_challenge);
159         }
160 }