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