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");
35 q("DELETE FROM `cache` WHERE `updated`<'%s'",
36 dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
42 if(($argc > 1) && ($argv[1] == 'force'))
45 if(($argc > 1) && intval($argv[1])) {
46 $manual_id = intval($argv[1]);
50 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
52 // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
53 $contacts = q("SELECT * FROM `contact`
54 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
55 OR ( `network` IN ( 'stat', 'feed' ) AND `poll` != '' ))
57 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
59 if(! count($contacts)){
63 foreach($contacts as $contact) {
66 $contact['last-update'] = '0000-00-00 00:00:00';
68 if($contact['priority'] || $contact['subhub']) {
73 $t = $contact['last-update'];
75 // We should be getting everything via a hub. But just to be sure, let's check once a day.
76 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
77 // This also lets us update our subscription to the hub, and add or replace hubs in case it
78 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
81 if($contact['subhub']) {
82 $interval = get_config('system','pushpoll_frequency');
83 $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
86 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
92 * Based on $contact['priority'], should we poll this site now? Or later?
95 switch ($contact['priority']) {
97 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
101 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
105 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
109 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
114 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
118 if((! $update) && (! $force))
122 $importer_uid = $contact['uid'];
124 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
125 intval($importer_uid)
132 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
134 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
135 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
136 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
139 if($contact['network'] === 'dfrn') {
141 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
143 if(intval($contact['duplex']) && $contact['dfrn-id'])
144 $idtosend = '0:' . $orig_id;
145 if(intval($contact['duplex']) && $contact['issued-id'])
146 $idtosend = '1:' . $orig_id;
148 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
149 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
150 . '&type=data&last_update=' . $last_update ;
152 $xml = fetch_url($url);
154 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
158 logger("poller: $url appears to be dead - marking for death ");
159 // dead connection - might be a transient event, or this might
160 // mean the software was uninstalled or the domain expired.
161 // Will keep trying for one month.
162 mark_for_death($contact);
164 // set the last-update so we don't keep polling
166 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
167 dbesc(datetime_convert()),
168 intval($contact['id'])
174 if(! strstr($xml,'<?xml')) {
175 logger('poller: response from ' . $url . ' did not contain XML.');
176 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
177 dbesc(datetime_convert()),
178 intval($contact['id'])
184 $res = simplexml_load_string($xml);
186 if(intval($res->status) == 1) {
187 logger("poller: $url replied status 1 - marking for death ");
189 // we may not be friends anymore. Will keep trying for one month.
190 // set the last-update so we don't keep polling
192 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
193 dbesc(datetime_convert()),
194 intval($contact['id'])
197 mark_for_death($contact);
200 if($contact['term-date'] != '0000-00-00 00:00:00') {
201 logger("poller: $url back from the dead - removing mark for death");
202 unmark_for_death($contact);
206 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
211 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
212 $challenge = hex2bin((string) $res->challenge);
216 if(($contact['duplex']) && strlen($contact['prvkey'])) {
217 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
218 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
221 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
222 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
225 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
227 if(strpos($final_dfrn_id,':') == 1)
228 $final_dfrn_id = substr($final_dfrn_id,2);
230 if($final_dfrn_id != $orig_id) {
232 // did not decode properly - cannot trust this site
236 $postvars['dfrn_id'] = $idtosend;
237 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
239 $xml = post_url($contact['poll'],$postvars);
242 // $contact['network'] !== 'dfrn'
244 $xml = fetch_url($contact['poll']);
247 logger('poller: received xml : ' . $xml, LOGGER_DATA);
252 consume_feed($xml,$importer,$contact,$hub,1);
254 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
256 consume_feed($xml,$importer,$contact,$hub,1);
259 if((strlen($hub)) && ($hub_update)
260 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
261 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
262 $hubs = explode(',', $hub);
264 foreach($hubs as $h) {
268 subscribe_to_hub($h,$importer,$contact);
274 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
275 dbesc(datetime_convert()),
276 intval($contact['id'])
279 // loop - next contact
285 if (array_search(__file__,get_included_files())===0){
286 poller_run($argv,$argc);