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