2 require_once("boot.php");
4 function poller_run($argv, $argc){
12 @include(".htconfig.php");
13 require_once("dba.php");
14 $db = new dba($db_host, $db_user, $db_pass, $db_data);
15 unset($db_host, $db_user, $db_pass, $db_data);
18 require_once('session.php');
19 require_once('datetime.php');
20 require_once('simplepie/simplepie.inc');
21 require_once('include/items.php');
22 require_once('include/Contact.php');
24 $a->set_baseurl(get_config('system','url'));
26 logger('poller: start');
28 // run queue delivery process in the background
30 proc_run('php',"include/queue.php");
33 q("DELETE FROM `cache` WHERE `updated`<'%s'",
34 dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
40 if(($argc > 1) && ($argv[1] == 'force'))
43 if(($argc > 1) && intval($argv[1])) {
44 $manual_id = intval($argv[1]);
48 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
50 // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
51 $contacts = q("SELECT * FROM `contact`
52 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
53 OR ( `network` IN ( 'stat', 'feed' ) AND `poll` != '' ))
55 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
57 if(! count($contacts)){
61 foreach($contacts as $contact) {
64 $contact['last-update'] = '0000-00-00 00:00:00';
66 if($contact['priority'] || $contact['subhub']) {
71 $t = $contact['last-update'];
73 // We should be getting everything via a hub. But just to be sure, let's check once a day.
74 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
75 // This also lets us update our subscription to the hub, and add or replace hubs in case it
76 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
79 if($contact['subhub']) {
80 $interval = get_config('system','pushpoll_frequency');
81 $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
84 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
90 * Based on $contact['priority'], should we poll this site now? Or later?
93 switch ($contact['priority']) {
95 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
99 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
103 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
107 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
112 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
116 if((! $update) && (! $force))
120 $importer_uid = $contact['uid'];
122 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
123 intval($importer_uid)
130 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
132 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
133 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
134 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
137 if($contact['network'] === 'dfrn') {
139 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
141 if(intval($contact['duplex']) && $contact['dfrn-id'])
142 $idtosend = '0:' . $orig_id;
143 if(intval($contact['duplex']) && $contact['issued-id'])
144 $idtosend = '1:' . $orig_id;
146 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
147 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
148 . '&type=data&last_update=' . $last_update ;
150 $xml = fetch_url($url);
152 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
156 logger("poller: $url appears to be dead - marking for death ");
157 // dead connection - might be a transient event, or this might
158 // mean the software was uninstalled or the domain expired.
159 // Will keep trying for one month.
160 mark_for_death($contact);
162 // set the last-update so we don't keep polling
164 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
165 dbesc(datetime_convert()),
166 intval($contact['id'])
172 if(! strstr($xml,'<?xml')) {
173 logger('poller: response from ' . $url . ' did not contain XML.');
174 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
175 dbesc(datetime_convert()),
176 intval($contact['id'])
182 $res = simplexml_load_string($xml);
184 if(intval($res->status) == 1) {
185 logger("poller: $url replied status 1 - marking for death ");
187 // we may not be friends anymore. Will keep trying for one month.
188 // set the last-update so we don't keep polling
190 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
191 dbesc(datetime_convert()),
192 intval($contact['id'])
195 mark_for_death($contact);
198 if($contact['term-date'] != '0000-00-00 00:00:00') {
199 logger("poller: $url back from the dead - removing mark for death");
200 unmark_for_death($contact);
204 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
209 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
210 $challenge = hex2bin((string) $res->challenge);
214 if(($contact['duplex']) && strlen($contact['prvkey'])) {
215 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
216 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
219 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
220 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
223 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
225 if(strpos($final_dfrn_id,':') == 1)
226 $final_dfrn_id = substr($final_dfrn_id,2);
228 if($final_dfrn_id != $orig_id) {
230 // did not decode properly - cannot trust this site
234 $postvars['dfrn_id'] = $idtosend;
235 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
237 $xml = post_url($contact['poll'],$postvars);
241 // $contact['network'] !== 'dfrn'
243 $xml = fetch_url($contact['poll']);
246 logger('poller: received xml : ' . $xml, LOGGER_DATA);
248 if(! strstr($xml,'<?xml')) {
249 logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
250 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
251 dbesc(datetime_convert()),
252 intval($contact['id'])
257 consume_feed($xml,$importer,$contact,$hub,1);
259 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
261 consume_feed($xml,$importer,$contact,$hub,1);
264 if((strlen($hub)) && ($hub_update)
265 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
266 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
267 $hubs = explode(',', $hub);
269 foreach($hubs as $h) {
273 subscribe_to_hub($h,$importer,$contact);
279 $updated = datetime_convert();
281 $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
284 intval($contact['id'])
287 // loop - next contact
293 if (array_search(__file__,get_included_files())===0){
294 poller_run($argv,$argc);