]> git.mxchange.org Git - friendica.git/blob - mod/pubsub.php
5d8ea2ed76640f7a0a3e329123f17aa8ef961808
[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' LIMIT 1",
48                         dbesc($nick)
49                 );
50                 if(! count($r))
51                         hub_return(false, '');
52
53
54                 $owner = $r[0];
55
56                 $sql_extra = ((strlen($hub_verify)) ? sprintf(" AND `hub-verify` = '%s' ", dbesc($hub_verify)) : '');
57
58                 $r = q("SELECT * FROM `contact` WHERE `poll` = '%s' AND `id` = %d AND `uid` = %d 
59                         AND `blocked` = 0 AND `pending` = 0 $sql_extra LIMIT 1",
60                         dbesc($hub_topic),
61                         intval($contact_id),
62                         intval($owner['uid'])
63                 );
64                 if(! count($r))
65                         hub_return(false, '');
66
67                 $contact = $r[0];
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') && (! strlen($hub_verify))) 
73                         hub_return(false, '');
74
75                 $r = q("UPDATE `contact` SET `subhub` = %d WHERE `id` = %d LIMIT 1",
76                         intval($subscribe),
77                         intval($contact['id'])
78                 );
79
80                 hub_return(true, $hub_challenge);               
81         }
82 }
83
84 require_once('include/security.php');
85
86 function pubsub_post(&$a) {
87
88         $xml = file_get_contents('php://input');
89
90         logger('pubsub: feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' .  $a->cmd );
91         logger('pubsub: user-agent: ' . $_SERVER['HTTP_USER_AGENT'] );
92         logger('pubsub: 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         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
98                 dbesc($nick)
99         );
100         if(! count($r))
101                 hub_post_return();
102
103         $importer = $r[0];
104
105         $r = q("SELECT * FROM `contact` WHERE `subhub` = 1 AND `id` = %d AND `uid` = %d 
106                 AND ( `rel` = %d OR `rel` = %d ) AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
107                 intval($contact_id),
108                 intval($importer['uid']),
109                 intval(REL_FAN),
110                 intval(REL_BUD) 
111         );
112
113         if(! count($r)) {
114                 logger('pubsub: no contact record - ignored');
115                 hub_post_return();
116         }
117
118         $contact = $r[0];
119
120         $feedhub = '';
121
122         require_once('include/items.php');
123
124         consume_feed($xml,$importer,$contact,$feedhub,1);
125
126         // do it a second time so that any children find their parents.
127
128         consume_feed($xml,$importer,$contact,$feedhub,1);
129
130         hub_post_return();
131
132 }
133
134
135