]> git.mxchange.org Git - friendica.git/blob - mod/pubsub.php
Move App to src
[friendica.git] / mod / pubsub.php
1 <?php
2
3 use Friendica\App;
4
5 function hub_return($valid,$body) {
6
7         if($valid) {
8                 header($_SERVER["SERVER_PROTOCOL"] . ' 200 ' . 'OK');
9                 echo $body;
10                 killme();
11         }
12         else {
13                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 ' . 'Not Found');
14                 killme();
15         }
16
17         // NOTREACHED
18 }
19
20 // when receiving an XML feed, always return OK
21
22 function hub_post_return() {
23
24         header($_SERVER["SERVER_PROTOCOL"] . ' 200 ' . 'OK');
25         killme();
26
27 }
28
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
38                 $hub_mode      = ((x($_GET,'hub_mode'))          ? notags(trim($_GET['hub_mode']))          : '');
39                 $hub_topic     = ((x($_GET,'hub_topic'))         ? notags(trim($_GET['hub_topic']))         : '');
40                 $hub_challenge = ((x($_GET,'hub_challenge'))     ? notags(trim($_GET['hub_challenge']))     : '');
41                 $hub_lease     = ((x($_GET,'hub_lease_seconds')) ? notags(trim($_GET['hub_lease_seconds'])) : '');
42                 $hub_verify    = ((x($_GET,'hub_verify_token'))  ? notags(trim($_GET['hub_verify_token']))  : '');
43
44                 logger('pubsub: Subscription from ' . $_SERVER['REMOTE_ADDR']);
45                 logger('pubsub: data: ' . print_r($_GET,true), LOGGER_DATA);
46
47                 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
48
49                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
50                         dbesc($nick)
51                 );
52                 if (! dbm::is_result($r)) {
53                         logger('pubsub: local account not found: ' . $nick);
54                         hub_return(false, '');
55                 }
56
57
58                 $owner = $r[0];
59
60                 $sql_extra = ((strlen($hub_verify)) ? sprintf(" AND `hub-verify` = '%s' ", dbesc($hub_verify)) : '');
61
62                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d
63                         AND `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1",
64                         intval($contact_id),
65                         intval($owner['uid'])
66                 );
67                 if (! dbm::is_result($r)) {
68                         logger('pubsub: contact '.$contact_id.' not found.');
69                         hub_return(false, '');
70                 }
71
72                 if ($hub_topic)
73                         if(! link_compare($hub_topic,$r[0]['poll'])) {
74                                 logger('pubsub: hub topic ' . $hub_topic . ' != ' . $r[0]['poll']);
75                                 // should abort but let's humour them.
76                         }
77
78                 $contact = $r[0];
79
80                 // We must initiate an unsubscribe request with a verify_token.
81                 // Don't allow outsiders to unsubscribe us.
82
83                 if($hub_mode === 'unsubscribe') {
84                         if(! strlen($hub_verify)) {
85                                 logger('pubsub: bogus unsubscribe');
86                                 hub_return(false, '');
87                         }
88                         logger('pubsub: unsubscribe success');
89                 }
90
91                 if ($hub_mode)
92                         $r = q("UPDATE `contact` SET `subhub` = %d WHERE `id` = %d",
93                                 intval($subscribe),
94                                 intval($contact['id'])
95                         );
96
97                 hub_return(true, $hub_challenge);
98         }
99 }
100
101 require_once('include/security.php');
102
103 function pubsub_post(App $a) {
104
105         $xml = file_get_contents('php://input');
106
107         logger('pubsub: feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' .  $a->cmd );
108         logger('pubsub: user-agent: ' . $_SERVER['HTTP_USER_AGENT'] );
109         logger('pubsub: data: ' . $xml, LOGGER_DATA);
110
111 //      if(! stristr($xml,'<?xml')) {
112 //              logger('pubsub_post: bad xml');
113 //              hub_post_return();
114 //      }
115
116         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
117         $contact_id = (($a->argc > 2) ? intval($a->argv[2])       : 0 );
118
119         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
120                 dbesc($nick)
121         );
122         if (! dbm::is_result($r)) {
123                 hub_post_return();
124         }
125
126         $importer = $r[0];
127
128         $r = q("SELECT * FROM `contact` WHERE `subhub` AND `id` = %d AND `uid` = %d
129                 AND (`rel` = %d OR `rel` = %d OR network = '%s') AND NOT `blocked` LIMIT 1",
130                 intval($contact_id),
131                 intval($importer['uid']),
132                 intval(CONTACT_IS_SHARING),
133                 intval(CONTACT_IS_FRIEND),
134                 dbesc(NETWORK_FEED)
135         );
136
137         if (! dbm::is_result($r)) {
138                 logger('pubsub: no contact record for "'.$nick.' ('.$contact_id.')" - ignored. '.$xml);
139                 hub_post_return();
140         }
141
142         $contact = $r[0];
143
144         // we have no way to match Diaspora guid's with atom post id's and could get duplicates.
145         // we'll assume that direct delivery is robust (and this is a bad assumption, but the duplicates are messy).
146
147         if($r[0]['network'] === NETWORK_DIASPORA)
148                 hub_post_return();
149
150         $feedhub = '';
151
152         require_once('include/items.php');
153
154         consume_feed($xml,$importer,$contact,$feedhub,1,1);
155
156         // do it a second time so that any children find their parents.
157
158         consume_feed($xml,$importer,$contact,$feedhub,1,2);
159
160         hub_post_return();
161
162 }
163
164
165