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