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