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