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 if(($argc > 1) && ($argv[1] == 'force'))
25 // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
26 $contacts = q("SELECT * FROM `contact`
27 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
28 OR ( `network` = 'stat' AND `poll` != '' ) )
29 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
31 if(! count($contacts))
34 foreach($contacts as $contact) {
36 if($contact['priority'] || $contact['subhub']) {
40 // We should be getting everything via a hub. But just to be sure, let's check once a day.
41 // This also lets us update our subscription to the hub, and add or replace hubs in case it
44 if($contact['subhub'])
45 $contact['priority'] = 3;
47 $t = $contact['last-update'];
49 switch ($contact['priority']) {
51 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
55 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
59 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
63 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
68 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
72 if((! $update) && (! $force))
76 $importer_uid = $contact['uid'];
78 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
86 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
88 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
89 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
90 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
93 if($contact['network'] === 'dfrn') {
95 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
97 if(intval($contact['duplex']) && $contact['dfrn-id'])
98 $idtosend = '0:' . $orig_id;
99 if(intval($contact['duplex']) && $contact['issued-id'])
100 $idtosend = '1:' . $orig_id;
102 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
103 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
104 . '&type=data&last_update=' . $last_update ;
106 $xml = fetch_url($url);
108 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
111 logger("poller: $url appears to be dead - marking for death ");
112 // dead connection - might be a transient event, or this might
113 // mean the software was uninstalled or the domain expired.
114 // Will keep trying for one month.
115 mark_for_death($contact);
120 $res = simplexml_load_string($xml);
122 if(intval($res->status) == 1) {
123 logger("poller: $url replied status 1 - marking for death ");
124 // we may not be friends anymore. Will keep trying for one month.
125 mark_for_death($contact);
128 if($contact['term-date'] != '0000-00-00 00:00:00') {
129 logger("poller: $url back from the dead - removing mark for death");
130 unmark_for_death($contact);
134 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
139 $sent_dfrn_id = hex2bin($res->dfrn_id);
140 $challenge = hex2bin($res->challenge);
144 if(($contact['duplex']) && strlen($contact['prvkey'])) {
145 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
146 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
149 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
150 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
153 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
155 if(strpos($final_dfrn_id,':') == 1)
156 $final_dfrn_id = substr($final_dfrn_id,2);
158 if($final_dfrn_id != $orig_id) {
160 // did not decode properly - cannot trust this site
164 $postvars['dfrn_id'] = $idtosend;
165 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
167 $xml = post_url($contact['poll'],$postvars);
170 // $contact['network'] !== 'dfrn'
172 $xml = fetch_url($contact['poll']);
175 logger('poller: received xml : ' . $xml, LOGGER_DATA);
180 consume_feed($xml,$importer,$contact,$hub);
182 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
184 consume_feed($xml,$importer,$contact,$hub);
187 if((strlen($hub)) && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
188 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
189 $hubs = explode(',', $hub);
191 foreach($hubs as $h) {
195 subscribe_to_hub($h,$importer,$contact);
201 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
202 dbesc(datetime_convert()),
203 intval($contact['id'])
206 // loop - next contact