4 require_once('boot.php');
8 @include('.htconfig.php');
9 require_once('dba.php');
10 $db = new dba($db_host, $db_user, $db_pass, $db_data);
11 unset($db_host, $db_user, $db_pass, $db_data);
13 require_once('session.php');
14 require_once('datetime.php');
15 require_once('simplepie/simplepie.inc');
16 require_once('include/items.php');
17 require_once('include/Contact.php');
19 $a->set_baseurl(get_config('system','url'));
22 // run queue delivery process in the background
24 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
25 proc_close(proc_open("\"$php_path\" \"include/queue.php\" &", array(), $foo));
29 if(($argc > 1) && ($argv[1] == 'force'))
32 // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
33 $contacts = q("SELECT * FROM `contact`
34 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
35 OR ( `network` = 'stat' AND `poll` != '' ) )
36 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
38 if(! count($contacts))
41 foreach($contacts as $contact) {
43 if($contact['priority'] || $contact['subhub']) {
47 // We should be getting everything via a hub. But just to be sure, let's check once a day.
48 // This also lets us update our subscription to the hub, and add or replace hubs in case it
51 if($contact['subhub'])
52 $contact['priority'] = 3;
54 $t = $contact['last-update'];
56 switch ($contact['priority']) {
58 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
62 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
66 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
70 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
75 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
79 if((! $update) && (! $force))
83 $importer_uid = $contact['uid'];
85 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
93 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
95 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
96 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
97 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
100 if($contact['network'] === 'dfrn') {
102 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
104 if(intval($contact['duplex']) && $contact['dfrn-id'])
105 $idtosend = '0:' . $orig_id;
106 if(intval($contact['duplex']) && $contact['issued-id'])
107 $idtosend = '1:' . $orig_id;
109 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
110 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
111 . '&type=data&last_update=' . $last_update ;
113 $xml = fetch_url($url);
115 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
118 logger("poller: $url appears to be dead - marking for death ");
119 // dead connection - might be a transient event, or this might
120 // mean the software was uninstalled or the domain expired.
121 // Will keep trying for one month.
122 mark_for_death($contact);
127 $res = simplexml_load_string($xml);
129 if(intval($res->status) == 1) {
130 logger("poller: $url replied status 1 - marking for death ");
131 // we may not be friends anymore. Will keep trying for one month.
132 mark_for_death($contact);
135 if($contact['term-date'] != '0000-00-00 00:00:00') {
136 logger("poller: $url back from the dead - removing mark for death");
137 unmark_for_death($contact);
141 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
146 $sent_dfrn_id = hex2bin($res->dfrn_id);
147 $challenge = hex2bin($res->challenge);
151 if(($contact['duplex']) && strlen($contact['prvkey'])) {
152 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
153 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
156 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
157 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
160 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
162 if(strpos($final_dfrn_id,':') == 1)
163 $final_dfrn_id = substr($final_dfrn_id,2);
165 if($final_dfrn_id != $orig_id) {
167 // did not decode properly - cannot trust this site
171 $postvars['dfrn_id'] = $idtosend;
172 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
174 $xml = post_url($contact['poll'],$postvars);
177 // $contact['network'] !== 'dfrn'
179 $xml = fetch_url($contact['poll']);
182 logger('poller: received xml : ' . $xml, LOGGER_DATA);
187 consume_feed($xml,$importer,$contact,$hub);
189 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
191 consume_feed($xml,$importer,$contact,$hub);
194 if((strlen($hub)) && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
195 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
196 $hubs = explode(',', $hub);
198 foreach($hubs as $h) {
202 subscribe_to_hub($h,$importer,$contact);
208 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
209 dbesc(datetime_convert()),
210 intval($contact['id'])
213 // loop - next contact