]> git.mxchange.org Git - friendica.git/blob - mod/pubsub.php
Improve Console/Config display for array values
[friendica.git] / mod / pubsub.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Database\DBM;
5 use Friendica\Protocol\OStatus;
6
7 require_once('include/security.php');
8 require_once('include/items.php');
9
10 function hub_return($valid, $body)
11 {
12         if ($valid) {
13                 header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
14                 echo $body;
15         } else {
16                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
17         }
18         killme();
19 }
20
21 // when receiving an XML feed, always return OK
22
23 function hub_post_return()
24 {
25         header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
26         killme();
27 }
28
29 function pubsub_init(App $a)
30 {
31         $nick       = (($a->argc > 1) ? notags(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      = notags(trim(defaults($_GET, 'hub_mode', '')));
36                 $hub_topic     = notags(trim(defaults($_GET, 'hub_topic', '')));
37                 $hub_challenge = notags(trim(defaults($_GET, 'hub_challenge', '')));
38                 $hub_lease     = notags(trim(defaults($_GET, 'hub_lease_seconds', '')));
39                 $hub_verify    = notags(trim(defaults($_GET, 'hub_verify_token', '')));
40
41                 logger('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
42                 logger('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 (!DBM::is_result($owner)) {
48                         logger('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 (!DBM::is_result($contact)) {
60                         logger('Contact ' . $contact_id . ' not found.');
61                         hub_return(false, '');
62                 }
63
64                 if (!empty($hub_topic) && !link_compare($hub_topic, $contact['poll'])) {
65                         logger('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('Bogus unsubscribe');
74                         hub_return(false, '');
75                 }
76
77                 if (!empty($hub_mode)) {
78                         dba::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
79                         logger($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 = file_get_contents('php://input');
88
89         logger('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' .  $a->cmd . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']);
90         logger('Data: ' . $xml, LOGGER_DATA);
91
92         $nick       = (($a->argc > 1) ? notags(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 (!DBM::is_result($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 (!DBM::is_result($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('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.');
109                 }
110                 if (!DBM::is_result($contact)) {
111                         logger('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_IS_SHARING, CONTACT_IS_FRIEND]) && ($contact['network'] != NETWORK_FEED)) {
117                 logger('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'], [NETWORK_OSTATUS, NETWORK_DFRN, NETWORK_FEED])) {
124                 hub_post_return();
125         }
126
127         logger('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'] === NETWORK_DFRN) {
133                 consume_feed($xml, $importer, $contact, $feedhub);
134         }
135
136         hub_post_return();
137 }