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