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_run($php_path,"include/queue.php");
34 q("DELETE FROM `cache` WHERE `updated`<'%s'",
35 dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
41 if(($argc > 1) && ($argv[1] == 'force'))
44 if(($argc > 1) && intval($argv[1])) {
45 $manual_id = intval($argv[1]);
49 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
51 // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
52 $contacts = q("SELECT * FROM `contact`
53 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
54 OR ( `network` IN ( 'stat', 'feed' ) AND `poll` != '' ))
56 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
58 if(! count($contacts)){
62 foreach($contacts as $contact) {
65 $contact['last-update'] = '0000-00-00 00:00:00';
67 if($contact['priority'] || $contact['subhub']) {
72 $t = $contact['last-update'];
74 // We should be getting everything via a hub. But just to be sure, let's check once a day.
75 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
76 // This also lets us update our subscription to the hub, and add or replace hubs in case it
77 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
80 if($contact['subhub']) {
81 $interval = get_config('system','pushpoll_frequency');
82 $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
85 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
91 * Based on $contact['priority'], should we poll this site now? Or later?
94 switch ($contact['priority']) {
96 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
100 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
104 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
108 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
113 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
117 if((! $update) && (! $force))
121 $importer_uid = $contact['uid'];
123 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
124 intval($importer_uid)
131 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
133 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
134 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
135 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
138 if($contact['network'] === 'dfrn') {
140 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
142 if(intval($contact['duplex']) && $contact['dfrn-id'])
143 $idtosend = '0:' . $orig_id;
144 if(intval($contact['duplex']) && $contact['issued-id'])
145 $idtosend = '1:' . $orig_id;
147 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
148 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
149 . '&type=data&last_update=' . $last_update ;
151 $xml = fetch_url($url);
153 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
157 logger("poller: $url appears to be dead - marking for death ");
158 // dead connection - might be a transient event, or this might
159 // mean the software was uninstalled or the domain expired.
160 // Will keep trying for one month.
161 mark_for_death($contact);
163 // set the last-update so we don't keep polling
165 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
166 dbesc(datetime_convert()),
167 intval($contact['id'])
173 if(! strstr($xml,'<?xml')) {
174 logger('poller: response from ' . $url . ' did not contain XML.');
175 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
176 dbesc(datetime_convert()),
177 intval($contact['id'])
183 $res = simplexml_load_string($xml);
185 if(intval($res->status) == 1) {
186 logger("poller: $url replied status 1 - marking for death ");
188 // we may not be friends anymore. Will keep trying for one month.
189 // set the last-update so we don't keep polling
191 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
192 dbesc(datetime_convert()),
193 intval($contact['id'])
196 mark_for_death($contact);
199 if($contact['term-date'] != '0000-00-00 00:00:00') {
200 logger("poller: $url back from the dead - removing mark for death");
201 unmark_for_death($contact);
205 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
210 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
211 $challenge = hex2bin((string) $res->challenge);
215 if(($contact['duplex']) && strlen($contact['prvkey'])) {
216 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
217 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
220 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
221 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
224 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
226 if(strpos($final_dfrn_id,':') == 1)
227 $final_dfrn_id = substr($final_dfrn_id,2);
229 if($final_dfrn_id != $orig_id) {
231 // did not decode properly - cannot trust this site
235 $postvars['dfrn_id'] = $idtosend;
236 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
238 $xml = post_url($contact['poll'],$postvars);
242 // $contact['network'] !== 'dfrn'
244 $xml = fetch_url($contact['poll']);
247 logger('poller: received xml : ' . $xml, LOGGER_DATA);
249 if(! strstr($xml,'<?xml')) {
250 logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
251 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
252 dbesc(datetime_convert()),
253 intval($contact['id'])
258 consume_feed($xml,$importer,$contact,$hub,1);
260 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
262 consume_feed($xml,$importer,$contact,$hub,1);
265 if((strlen($hub)) && ($hub_update)
266 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
267 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
268 $hubs = explode(',', $hub);
270 foreach($hubs as $h) {
274 subscribe_to_hub($h,$importer,$contact);
280 $updated = datetime_convert();
282 $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
285 intval($contact['id'])
288 // loop - next contact
294 if (array_search(__file__,get_included_files())===0){
295 poller_run($argv,$argc);