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');
18 require_once('include/Contact.php');
20 $debugging = get_config('system','debugging');
22 $a->set_baseurl(get_config('system','url'));
24 // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
25 $contacts = q("SELECT * FROM `contact`
26 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
27 OR ( `network` = 'stat' AND `poll` != '' ) )
28 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
30 if(! count($contacts))
33 foreach($contacts as $contact) {
35 if($contact['priority'] || $contact['subhub']) {
39 // We should be getting everything via a hub. But just to be sure, let's check once a day.
40 // This also lets us update our subscription to the hub, and add or replace hubs in case it
43 if($contact['subhub'])
44 $contact['priority'] = 3;
46 $t = $contact['last-update'];
48 switch ($contact['priority']) {
50 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
54 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
58 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
62 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
67 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
75 $importer_uid = $contact['uid'];
77 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
86 echo "IMPORTER: {$importer['name']}\n";
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);
109 echo "URL: " . $url . "\n";
110 echo "XML: " . $xml . "\n";
114 // dead connection - might be a transient event, or this might
115 // mean the software was uninstalled or the domain expired.
116 // Will keep trying for one month.
117 mark_for_death($contact);
122 $res = simplexml_load_string($xml);
124 if(intval($res->status) == 1) {
125 // we may not be friends anymore. Will keep trying for one month.
126 mark_for_death($contact);
129 if($contact['term-date'] != '0000-00-00 00:00:00')
130 unmark_for_death($contact);
133 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
138 $sent_dfrn_id = hex2bin($res->dfrn_id);
139 $challenge = hex2bin($res->challenge);
143 if(($contact['duplex']) && strlen($contact['prvkey'])) {
144 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
145 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
148 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
149 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
152 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
154 if(strpos($final_dfrn_id,':') == 1)
155 $final_dfrn_id = substr($final_dfrn_id,2);
157 if($final_dfrn_id != $orig_id) {
159 // did not decode properly - cannot trust this site
163 $postvars['dfrn_id'] = $idtosend;
164 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
166 $xml = post_url($contact['poll'],$postvars);
169 // $contact['network'] !== 'dfrn'
171 $xml = fetch_url($contact['poll']);
175 echo "XML response:" . $xml . "\n";
176 echo "Length:" . strlen($xml) . "\n";
182 consume_feed($xml,$importer,$contact,$hub);
184 if((strlen($hub)) && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly']))) && ($contact['priority'] == 0)) {
185 $hubs = explode(',', $hub);
187 foreach($hubs as $h) {
191 subscribe_to_hub($h,$importer,$contact);
197 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
198 dbesc(datetime_convert()),
199 intval($contact['id'])
202 // loop - next contact