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