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