]> git.mxchange.org Git - friendica.git/blob - mod/pubsub.php
trialing pubsubhubbub - operational tweaks
[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
43
44                 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
45
46                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
47                         dbesc($nick)
48                 );
49                 if(! count($r))
50                         hub_return(false, '');
51
52
53                 $owner = $r[0];
54
55                 $sql_extra = ((strlen($hub_verify)) ? sprintf(" AND `hub-verify` = '%s' ", dbesc($hub_verify)) : '');
56
57                 $r = q("SELECT * FROM `contact` WHERE `poll` = '%s' AND `id` = %d AND `uid` = %d AND `blocked` = 0 $sql_extra LIMIT 1",
58                         dbesc($hub_topic),
59                         intval($contact_id),
60                         intval($owner['uid'])
61                 );
62                 if(! count($r))
63                         hub_return(false, '');
64
65                 $contact = $r[0];
66
67                 // We must initiate an unsubscribe request with a verify_token. 
68                 // Don't allow outsiders to unsubscribe us.
69
70                 if(($hub_mode === 'unsubscribe') && (! strlen($hub_verify))) 
71                         hub_return(false, '');
72
73                 $r = q("UPDATE `contact` SET `subhub` = %d WHERE `id` = %d LIMIT 1",
74                         intval($subscribe),
75                         intval($contact['id'])
76                 );
77
78                 hub_return(true, $hub_challenge);
79                 
80         }
81 }
82
83
84 function pubsub_post(&$a) {
85
86         $xml = file_get_contents('php://input');
87
88         $debugging = get_config('system','debugging');
89         if($debugging)
90                 file_put_contents('pubsub.out',$xml);
91
92         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
93         $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0);
94
95         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
96                 dbesc($nick)
97         );
98         if(! count($r))
99                 hub_post_return();
100
101         $importer = $r[0];
102
103         $r = q("SELECT * FROM `contact` WHERE `subhub` = 1 AND `id` = %d AND `uid` = %d AND `blocked` = 0 LIMIT 1",
104                 intval($contact_id),
105                 intval($importer['uid'])
106         );
107         if(! count($r))
108                 hub_post_return();
109
110         $contact = $r[0];
111
112         $feedhub = '';
113
114         require_once('include/items.php');
115
116         consume_feed($xml,$importer,$contact,$feedhub);
117
118         hub_post_return();
119
120 }
121
122
123