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'));
21 logger('poller: start');
23 // run queue delivery process in the background
25 $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
26 //proc_close(proc_open("\"$php_path\" \"include/queue.php\" &", array(), $foo));
27 proc_run($php_path,"include/queue.php");
33 if(($argc > 1) && ($argv[1] == 'force'))
36 if(($argc > 1) && intval($argv[1])) {
37 $manual_id = intval($argv[1]);
41 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
43 // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
44 $contacts = q("SELECT * FROM `contact`
45 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
46 OR ( `network` IN ( 'stat', 'feed' ) AND `poll` != '' ))
48 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
50 if(! count($contacts)){
54 foreach($contacts as $contact) {
56 if($contact['priority'] || $contact['subhub']) {
61 $t = $contact['last-update'];
63 // We should be getting everything via a hub. But just to be sure, let's check once a day.
64 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
65 // This also lets us update our subscription to the hub, and add or replace hubs in case it
66 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
69 if($contact['subhub']) {
70 $interval = get_config('system','pushpoll_frequency');
71 $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
74 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
80 * Based on $contact['priority'], should we poll this site now? Or later?
83 switch ($contact['priority']) {
85 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
89 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
93 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
97 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
102 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
106 if((! $update) && (! $force))
110 $importer_uid = $contact['uid'];
112 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
113 intval($importer_uid)
120 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
122 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
123 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
124 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
127 if($contact['network'] === 'dfrn') {
129 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
131 if(intval($contact['duplex']) && $contact['dfrn-id'])
132 $idtosend = '0:' . $orig_id;
133 if(intval($contact['duplex']) && $contact['issued-id'])
134 $idtosend = '1:' . $orig_id;
136 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
137 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
138 . '&type=data&last_update=' . $last_update ;
140 $xml = fetch_url($url);
142 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
146 logger("poller: $url appears to be dead - marking for death ");
147 // dead connection - might be a transient event, or this might
148 // mean the software was uninstalled or the domain expired.
149 // Will keep trying for one month.
150 mark_for_death($contact);
152 // set the last-update so we don't keep polling
154 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
155 dbesc(datetime_convert()),
156 intval($contact['id'])
162 if(! strstr($xml,'<?xml')) {
163 logger('poller: response from ' . $url . ' did not contain XML.');
164 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
165 dbesc(datetime_convert()),
166 intval($contact['id'])
172 $res = simplexml_load_string($xml);
174 if(intval($res->status) == 1) {
175 logger("poller: $url replied status 1 - marking for death ");
177 // we may not be friends anymore. Will keep trying for one month.
178 // set the last-update so we don't keep polling
180 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
181 dbesc(datetime_convert()),
182 intval($contact['id'])
185 mark_for_death($contact);
188 if($contact['term-date'] != '0000-00-00 00:00:00') {
189 logger("poller: $url back from the dead - removing mark for death");
190 unmark_for_death($contact);
194 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
199 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
200 $challenge = hex2bin((string) $res->challenge);
204 if(($contact['duplex']) && strlen($contact['prvkey'])) {
205 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
206 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
209 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
210 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
213 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
215 if(strpos($final_dfrn_id,':') == 1)
216 $final_dfrn_id = substr($final_dfrn_id,2);
218 if($final_dfrn_id != $orig_id) {
220 // did not decode properly - cannot trust this site
224 $postvars['dfrn_id'] = $idtosend;
225 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
227 $xml = post_url($contact['poll'],$postvars);
230 // $contact['network'] !== 'dfrn'
232 $xml = fetch_url($contact['poll']);
235 logger('poller: received xml : ' . $xml, LOGGER_DATA);
240 consume_feed($xml,$importer,$contact,$hub,1);
242 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
244 consume_feed($xml,$importer,$contact,$hub,1);
247 if((strlen($hub)) && ($hub_update)
248 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
249 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
250 $hubs = explode(',', $hub);
252 foreach($hubs as $h) {
256 subscribe_to_hub($h,$importer,$contact);
262 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
263 dbesc(datetime_convert()),
264 intval($contact['id'])
267 // loop - next contact