2 require_once("boot.php");
4 function poller_run($argv, $argc){
12 @include(".htconfig.php");
13 require_once("dba.php");
14 $db = new dba($db_host, $db_user, $db_pass, $db_data);
15 unset($db_host, $db_user, $db_pass, $db_data);
18 require_once('session.php');
19 require_once('datetime.php');
20 require_once('simplepie/simplepie.inc');
21 require_once('include/items.php');
22 require_once('include/Contact.php');
24 $a->set_baseurl(get_config('system','url'));
26 logger('poller: start');
28 // run queue delivery process in the background
30 $php_path = ((x($a->config,'php_path') && strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
31 //proc_close(proc_open("\"$php_path\" \"include/queue.php\" &", array(), $foo));
32 proc_run($php_path,"include/queue.php");
38 if(($argc > 1) && ($argv[1] == 'force'))
41 if(($argc > 1) && intval($argv[1])) {
42 $manual_id = intval($argv[1]);
46 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
48 // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
49 $contacts = q("SELECT * FROM `contact`
50 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
51 OR ( `network` IN ( 'stat', 'feed' ) AND `poll` != '' ))
53 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
55 if(! count($contacts)){
59 foreach($contacts as $contact) {
61 if($contact['priority'] || $contact['subhub']) {
66 $t = $contact['last-update'];
68 // We should be getting everything via a hub. But just to be sure, let's check once a day.
69 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
70 // This also lets us update our subscription to the hub, and add or replace hubs in case it
71 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
74 if($contact['subhub']) {
75 $interval = get_config('system','pushpoll_frequency');
76 $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
79 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
85 * Based on $contact['priority'], should we poll this site now? Or later?
88 switch ($contact['priority']) {
90 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
94 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
98 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
102 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
107 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
111 if((! $update) && (! $force))
115 $importer_uid = $contact['uid'];
117 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
118 intval($importer_uid)
125 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
127 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
128 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
129 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
132 if($contact['network'] === 'dfrn') {
134 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
136 if(intval($contact['duplex']) && $contact['dfrn-id'])
137 $idtosend = '0:' . $orig_id;
138 if(intval($contact['duplex']) && $contact['issued-id'])
139 $idtosend = '1:' . $orig_id;
141 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
142 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
143 . '&type=data&last_update=' . $last_update ;
145 $xml = fetch_url($url);
147 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
151 logger("poller: $url appears to be dead - marking for death ");
152 // dead connection - might be a transient event, or this might
153 // mean the software was uninstalled or the domain expired.
154 // Will keep trying for one month.
155 mark_for_death($contact);
157 // set the last-update so we don't keep polling
159 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
160 dbesc(datetime_convert()),
161 intval($contact['id'])
167 if(! strstr($xml,'<?xml')) {
168 logger('poller: response from ' . $url . ' did not contain XML.');
169 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
170 dbesc(datetime_convert()),
171 intval($contact['id'])
177 $res = simplexml_load_string($xml);
179 if(intval($res->status) == 1) {
180 logger("poller: $url replied status 1 - marking for death ");
182 // we may not be friends anymore. Will keep trying for one month.
183 // set the last-update so we don't keep polling
185 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
186 dbesc(datetime_convert()),
187 intval($contact['id'])
190 mark_for_death($contact);
193 if($contact['term-date'] != '0000-00-00 00:00:00') {
194 logger("poller: $url back from the dead - removing mark for death");
195 unmark_for_death($contact);
199 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
204 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
205 $challenge = hex2bin((string) $res->challenge);
209 if(($contact['duplex']) && strlen($contact['prvkey'])) {
210 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
211 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
214 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
215 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
218 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
220 if(strpos($final_dfrn_id,':') == 1)
221 $final_dfrn_id = substr($final_dfrn_id,2);
223 if($final_dfrn_id != $orig_id) {
225 // did not decode properly - cannot trust this site
229 $postvars['dfrn_id'] = $idtosend;
230 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
232 $xml = post_url($contact['poll'],$postvars);
235 // $contact['network'] !== 'dfrn'
237 $xml = fetch_url($contact['poll']);
240 logger('poller: received xml : ' . $xml, LOGGER_DATA);
245 consume_feed($xml,$importer,$contact,$hub,1);
247 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
249 consume_feed($xml,$importer,$contact,$hub,1);
252 if((strlen($hub)) && ($hub_update)
253 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
254 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
255 $hubs = explode(',', $hub);
257 foreach($hubs as $h) {
261 subscribe_to_hub($h,$importer,$contact);
267 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
268 dbesc(datetime_convert()),
269 intval($contact['id'])
272 // loop - next contact
278 if (array_search(__file__,get_included_files())===0){
279 poller_run($argv,$argc);