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