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 $contacts = q("SELECT * FROM `contact`
25 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
26 OR ( `network` = 'stat' AND `poll` != '' ) )
27 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
29 if(! count($contacts))
32 foreach($contacts as $contact) {
34 if($contact['priority'] || $contact['subhub']) {
38 // We should be getting everything via a hub. But just to be sure, let's check once a day.
39 // This also lets us update our subscription to the hub, and add or replace hubs in case it
42 if($contact['subhub'])
43 $contact['priority'] = 3;
45 $t = $contact['last-update'];
47 switch ($contact['priority']) {
49 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
53 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
57 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
61 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
66 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
74 $importer_uid = $contact['uid'];
76 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
85 echo "IMPORTER: {$importer['name']}\n";
87 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
88 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
89 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
92 if($contact['network'] === 'dfrn') {
94 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
96 if(intval($contact['duplex']) && $contact['dfrn-id'])
97 $idtosend = '0:' . $orig_id;
98 if(intval($contact['duplex']) && $contact['issued-id'])
99 $idtosend = '1:' . $orig_id;
101 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
102 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
103 . '&type=data&last_update=' . $last_update ;
105 $xml = fetch_url($url);
108 echo "URL: " . $url . "\n";
109 echo "XML: " . $xml . "\n";
113 // dead connection - might be a transient event, or this might
114 // mean the software was uninstalled or the domain expired.
115 // Will keep trying for one month.
116 mark_for_death($contact);
121 $res = simplexml_load_string($xml);
123 if(intval($res->status) == 1) {
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 unmark_for_death($contact);
132 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
137 $sent_dfrn_id = hex2bin($res->dfrn_id);
138 $challenge = hex2bin($res->challenge);
142 if(($contact['duplex']) && strlen($contact['prvkey'])) {
143 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
144 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
147 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
148 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
151 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
153 if(strpos($final_dfrn_id,':') == 1)
154 $final_dfrn_id = substr($final_dfrn_id,2);
156 if($final_dfrn_id != $orig_id) {
158 // did not decode properly - cannot trust this site
162 $postvars['dfrn_id'] = $idtosend;
163 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
165 $xml = post_url($contact['poll'],$postvars);
168 // $contact['network'] !== 'dfrn'
170 $xml = fetch_url($contact['poll']);
174 echo "XML response:" . $xml . "\n";
175 echo "Length:" . strlen($xml) . "\n";
181 consume_feed($xml,$importer,$contact,$hub);
183 if((strlen($hub)) && ($contact['rel'] == REL_BUD) && ($contact['priority'] == 0)) {
184 $hubs = explode(',', $hub);
186 foreach($hubs as $h) {
190 subscribe_to_hub($h,$importer,$contact);
196 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
197 dbesc(datetime_convert()),
198 intval($contact['id'])
201 // loop - next contact