Merge pull request #9963 from mexon/mat/support-cid-scheme
[friendica.git/.git] / mod / pubsub.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 use Friendica\App;
23 use Friendica\Core\Logger;
24 use Friendica\Core\Protocol;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Protocol\Feed;
29 use Friendica\Protocol\OStatus;
30 use Friendica\Util\Strings;
31 use Friendica\Util\Network;
32 use Friendica\Core\System;
33
34 function hub_return($valid, $body)
35 {
36         if ($valid) {
37                 echo $body;
38         } else {
39                 throw new \Friendica\Network\HTTPException\NotFoundException();
40         }
41         exit();
42 }
43
44 // when receiving an XML feed, always return OK
45
46 function hub_post_return()
47 {
48         throw new \Friendica\Network\HTTPException\OKException();
49 }
50
51 function pubsub_init(App $a)
52 {
53         $nick       = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
54         $contact_id = (($a->argc > 2) ? intval($a->argv[2])       : 0 );
55
56         if ($_SERVER['REQUEST_METHOD'] === 'GET') {
57                 $hub_mode      = Strings::escapeTags(trim($_GET['hub_mode'] ?? ''));
58                 $hub_topic     = Strings::escapeTags(trim($_GET['hub_topic'] ?? ''));
59                 $hub_challenge = Strings::escapeTags(trim($_GET['hub_challenge'] ?? ''));
60                 $hub_verify    = Strings::escapeTags(trim($_GET['hub_verify_token'] ?? ''));
61
62                 Logger::log('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
63                 Logger::log('Data: ' . print_r($_GET,true), Logger::DATA);
64
65                 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
66
67                 $owner = DBA::selectFirst('user', ['uid'], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
68                 if (!DBA::isResult($owner)) {
69                         Logger::log('Local account not found: ' . $nick);
70                         hub_return(false, '');
71                 }
72
73                 $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
74
75                 if (!empty($hub_verify)) {
76                         $condition['hub-verify'] = $hub_verify;
77                 }
78
79                 $contact = DBA::selectFirst('contact', ['id', 'poll'], $condition);
80                 if (!DBA::isResult($contact)) {
81                         Logger::log('Contact ' . $contact_id . ' not found.');
82                         hub_return(false, '');
83                 }
84
85                 if (!empty($hub_topic) && !Strings::compareLink($hub_topic, $contact['poll'])) {
86                         Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
87                         hub_return(false, '');
88                 }
89
90                 // We must initiate an unsubscribe request with a verify_token.
91                 // Don't allow outsiders to unsubscribe us.
92
93                 if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
94                         Logger::log('Bogus unsubscribe');
95                         hub_return(false, '');
96                 }
97
98                 if (!empty($hub_mode)) {
99                         DBA::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
100                         Logger::log($hub_mode . ' success for contact ' . $contact_id . '.');
101                 }
102                 hub_return(true, $hub_challenge);
103         }
104 }
105
106 function pubsub_post(App $a)
107 {
108         $xml = Network::postdata();
109
110         Logger::log('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' .  DI::args()->getCommand() . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']);
111         Logger::log('Data: ' . $xml, Logger::DATA);
112
113         $nick       = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
114         $contact_id = (($a->argc > 2) ? intval($a->argv[2])       : 0 );
115
116         $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
117         if (!DBA::isResult($importer)) {
118                 hub_post_return();
119         }
120
121         $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
122         $contact = DBA::selectFirst('contact', [], $condition);
123
124         if (!DBA::isResult($contact)) {
125                 $author = OStatus::salmonAuthor($xml, $importer);
126                 if (!empty($author['contact-id'])) {
127                         $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
128                         $contact = DBA::selectFirst('contact', [], $condition);
129                         Logger::log('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.');
130                 }
131                 if (!DBA::isResult($contact)) {
132                         Logger::log('Contact ' . $author["author-link"] . ' (' . $contact_id . ') for user ' . $nick . " wasn't found - ignored. XML: " . $xml);
133                         hub_post_return();
134                 }
135         }
136
137         if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
138                 Logger::log('Contact ' . $contact['id'] . ' is not expected to share with us - ignored.');
139                 hub_post_return();
140         }
141
142         // We only import feeds from OStatus here
143         if ($contact['network'] != Protocol::OSTATUS) {
144                 Logger::warning('Unexpected network', ['contact' => $contact]);
145                 hub_post_return();
146         }
147
148         Logger::log('Import item for ' . $nick . ' from ' . $contact['nick'] . ' (' . $contact['id'] . ')');
149         $feedhub = '';
150         OStatus::import($xml, $importer, $contact, $feedhub);
151
152         hub_post_return();
153 }