]> git.mxchange.org Git - friendica.git/blob - mod/pubsub.php
Merge pull request #2314 from rabuzarus/0302_datetime_cleanup
[friendica.git] / mod / pubsub.php
1 <?php
2
3 if(! function_exists('hub_return')) {
4 function hub_return($valid,$body) {
5
6         if($valid) {
7                 header($_SERVER["SERVER_PROTOCOL"] . ' 200 ' . 'OK');
8                 echo $body;
9                 killme();
10         }
11         else {
12                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 ' . 'Not Found');
13                 killme();
14         }
15
16         // NOTREACHED
17 }
18 }
19
20 // when receiving an XML feed, always return OK
21 if(! function_exists('hub_post_return')) {
22 function hub_post_return() {
23         header($_SERVER["SERVER_PROTOCOL"] . ' 200 ' . 'OK');
24         killme();
25 }
26 }
27
28
29 if(! function_exists('pubsub_init')) {
30 function pubsub_init(&$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
37                 $hub_mode      = ((x($_GET,'hub_mode'))          ? notags(trim($_GET['hub_mode']))          : '');
38                 $hub_topic     = ((x($_GET,'hub_topic'))         ? notags(trim($_GET['hub_topic']))         : '');
39                 $hub_challenge = ((x($_GET,'hub_challenge'))     ? notags(trim($_GET['hub_challenge']))     : '');
40                 $hub_lease     = ((x($_GET,'hub_lease_seconds')) ? notags(trim($_GET['hub_lease_seconds'])) : '');
41                 $hub_verify    = ((x($_GET,'hub_verify_token'))  ? notags(trim($_GET['hub_verify_token']))  : '');
42
43                 logger('pubsub: Subscription from ' . $_SERVER['REMOTE_ADDR']);
44                 logger('pubsub: data: ' . print_r($_GET,true), LOGGER_DATA);
45
46                 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
47
48                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
49                         dbesc($nick)
50                 );
51                 if(! count($r)) {
52                         logger('pubsub: local account not found: ' . $nick);
53                         hub_return(false, '');
54                 }
55
56
57                 $owner = $r[0];
58
59                 $sql_extra = ((strlen($hub_verify)) ? sprintf(" AND `hub-verify` = '%s' ", dbesc($hub_verify)) : '');
60
61                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d
62                         AND `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1",
63                         intval($contact_id),
64                         intval($owner['uid'])
65                 );
66                 if(! count($r)) {
67                         logger('pubsub: contact '.$contact_id.' not found.');
68                         hub_return(false, '');
69                 }
70
71                 if ($hub_topic)
72                         if(! link_compare($hub_topic,$r[0]['poll'])) {
73                                 logger('pubsub: hub topic ' . $hub_topic . ' != ' . $r[0]['poll']);
74                                 // should abort but let's humour them.
75                         }
76
77                 $contact = $r[0];
78
79                 // We must initiate an unsubscribe request with a verify_token.
80                 // Don't allow outsiders to unsubscribe us.
81
82                 if($hub_mode === 'unsubscribe') {
83                         if(! strlen($hub_verify)) {
84                                 logger('pubsub: bogus unsubscribe');
85                                 hub_return(false, '');
86                         }
87                         logger('pubsub: unsubscribe success');
88                 }
89
90                 if ($hub_mode)
91                         $r = q("UPDATE `contact` SET `subhub` = %d WHERE `id` = %d",
92                                 intval($subscribe),
93                                 intval($contact['id'])
94                         );
95
96                 hub_return(true, $hub_challenge);
97         }
98 }
99 }
100
101 require_once('include/security.php');
102
103 if(! function_exists('pubsub_post')) {
104 function pubsub_post(&$a) {
105
106         $xml = file_get_contents('php://input');
107
108         logger('pubsub: feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' .  $a->cmd );
109         logger('pubsub: user-agent: ' . $_SERVER['HTTP_USER_AGENT'] );
110         logger('pubsub: data: ' . $xml, LOGGER_DATA);
111
112 //      if(! stristr($xml,'<?xml')) {
113 //              logger('pubsub_post: bad xml');
114 //              hub_post_return();
115 //      }
116
117         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
118         $contact_id = (($a->argc > 2) ? intval($a->argv[2])       : 0 );
119
120         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
121                 dbesc($nick)
122         );
123         if(! count($r))
124                 hub_post_return();
125
126         $importer = $r[0];
127
128         $r = q("SELECT * FROM `contact` WHERE `subhub` = 1 AND `id` = %d AND `uid` = %d
129                 AND ( `rel` = %d OR `rel` = %d OR network = '%s' ) AND `blocked` = 0 AND `readonly` = 0 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(! count($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 }