]> git.mxchange.org Git - friendica.git/blob - mod/pubsub.php
add recipient url to pubsub instrumentation
[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 = notags(trim($_GET['hub_mode']));
37                 $hub_topic = notags(trim($_GET['hub_topic']));
38                 $hub_challenge = notags(trim($_GET['hub_challenge']));
39                 $hub_lease = notags(trim($_GET['hub_lease_seconds']));
40                 $hub_verify = notags(trim($_GET['hub_verify_token']));
41
42                 $debugging = get_config('system','debugging');
43                 if($debugging) {
44                         file_put_contents('pubsub.out', 'Pubsubhubbub subscription called from ' . $_SERVER['REMOTE_ADDR'] . ' at ' . datetime_convert() . "\n" . print_r($_GET,true), FILE_APPEND);
45                 }
46
47                 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
48
49                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
50                         dbesc($nick)
51                 );
52                 if(! count($r))
53                         hub_return(false, '');
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 `poll` = '%s' AND `id` = %d AND `uid` = %d AND `blocked` = 0 $sql_extra LIMIT 1",
61                         dbesc($hub_topic),
62                         intval($contact_id),
63                         intval($owner['uid'])
64                 );
65                 if(! count($r))
66                         hub_return(false, '');
67
68                 $contact = $r[0];
69
70                 // We must initiate an unsubscribe request with a verify_token. 
71                 // Don't allow outsiders to unsubscribe us.
72
73                 if(($hub_mode === 'unsubscribe') && (! strlen($hub_verify))) 
74                         hub_return(false, '');
75
76                 $r = q("UPDATE `contact` SET `subhub` = %d WHERE `id` = %d LIMIT 1",
77                         intval($subscribe),
78                         intval($contact['id'])
79                 );
80
81                 hub_return(true, $hub_challenge);
82                 
83         }
84 }
85
86
87 function pubsub_post(&$a) {
88
89         $xml = file_get_contents('php://input');
90
91         $debugging = get_config('system','debugging');
92         $remote_host = 'Pubsub feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' at ' . datetime_convert() . ' for ' .  $a->cmd . "\n\n";
93         if($debugging)
94                 file_put_contents('pubsub.out', $remote_host . $xml, FILE_APPEND);
95
96         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
97         $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0);
98
99         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
100                 dbesc($nick)
101         );
102         if(! count($r))
103                 hub_post_return();
104
105         $importer = $r[0];
106
107         $r = q("SELECT * FROM `contact` WHERE `subhub` = 1 AND `id` = %d AND `uid` = %d AND `blocked` = 0 LIMIT 1",
108                 intval($contact_id),
109                 intval($importer['uid'])
110         );
111         if(! count($r))
112                 hub_post_return();
113
114         $contact = $r[0];
115
116         $feedhub = '';
117
118         require_once('include/items.php');
119
120         consume_feed($xml,$importer,$contact,$feedhub);
121
122         hub_post_return();
123
124 }
125
126
127