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 proc_run('php',"include/queue.php");
32 // once daily run expire in background
34 $d1 = get_config('system','last_expire_day');
35 $d2 = intval(datetime_convert('UTC','UTC','now','d'));
37 if($d2 != intval($d1)) {
38 set_config('system','last_expire_day',$d2);
39 proc_run('php','include/expire.php');
44 q("DELETE FROM `cache` WHERE `updated` < '%s'",
45 dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
51 if(($argc > 1) && ($argv[1] == 'force'))
54 if(($argc > 1) && intval($argv[1])) {
55 $manual_id = intval($argv[1]);
59 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
61 $d = datetime_convert();
62 call_hooks('cron', $d);
66 $contacts = q("SELECT `id` FROM `contact`
67 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
69 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()",
74 if(! count($contacts)) {
78 foreach($contacts as $c) {
80 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
87 foreach($res as $contact) {
89 $contact['last-update'] = '0000-00-00 00:00:00';
91 if($contact['priority'] || $contact['subhub']) {
96 $t = $contact['last-update'];
98 // We should be getting everything via a hub. But just to be sure, let's check once a day.
99 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
100 // This also lets us update our subscription to the hub, and add or replace hubs in case it
101 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
104 if($contact['subhub']) {
105 $interval = get_config('system','pushpoll_frequency');
106 $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
109 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
114 * Based on $contact['priority'], should we poll this site now? Or later?
117 switch ($contact['priority']) {
119 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
123 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
127 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
131 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
136 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
140 if((! $update) && (! $force))
144 $importer_uid = $contact['uid'];
146 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
147 intval($importer_uid)
154 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
156 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
157 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
158 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
161 if($contact['network'] === 'dfrn') {
163 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
165 if(intval($contact['duplex']) && $contact['dfrn-id'])
166 $idtosend = '0:' . $orig_id;
167 if(intval($contact['duplex']) && $contact['issued-id'])
168 $idtosend = '1:' . $orig_id;
170 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
171 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
172 . '&type=data&last_update=' . $last_update ;
174 $xml = fetch_url($url);
176 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
180 logger("poller: $url appears to be dead - marking for death ");
181 // dead connection - might be a transient event, or this might
182 // mean the software was uninstalled or the domain expired.
183 // Will keep trying for one month.
184 mark_for_death($contact);
186 // set the last-update so we don't keep polling
188 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
189 dbesc(datetime_convert()),
190 intval($contact['id'])
196 if(! strstr($xml,'<?xml')) {
197 logger('poller: response from ' . $url . ' did not contain XML.');
198 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
199 dbesc(datetime_convert()),
200 intval($contact['id'])
206 $res = simplexml_load_string($xml);
208 if(intval($res->status) == 1) {
209 logger("poller: $url replied status 1 - marking for death ");
211 // we may not be friends anymore. Will keep trying for one month.
212 // set the last-update so we don't keep polling
214 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
215 dbesc(datetime_convert()),
216 intval($contact['id'])
219 mark_for_death($contact);
222 if($contact['term-date'] != '0000-00-00 00:00:00') {
223 logger("poller: $url back from the dead - removing mark for death");
224 unmark_for_death($contact);
228 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
233 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
234 $challenge = hex2bin((string) $res->challenge);
238 if(($contact['duplex']) && strlen($contact['prvkey'])) {
239 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
240 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
243 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
244 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
247 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
249 if(strpos($final_dfrn_id,':') == 1)
250 $final_dfrn_id = substr($final_dfrn_id,2);
252 if($final_dfrn_id != $orig_id) {
254 // did not decode properly - cannot trust this site
258 $postvars['dfrn_id'] = $idtosend;
259 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
261 $xml = post_url($contact['poll'],$postvars);
265 // $contact['network'] !== 'dfrn'
267 $xml = fetch_url($contact['poll']);
270 logger('poller: received xml : ' . $xml, LOGGER_DATA);
272 if(! strstr($xml,'<?xml')) {
273 logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
274 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
275 dbesc(datetime_convert()),
276 intval($contact['id'])
281 consume_feed($xml,$importer,$contact,$hub,1);
283 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
285 consume_feed($xml,$importer,$contact,$hub,1);
288 if((strlen($hub)) && ($hub_update)
289 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
290 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
291 $hubs = explode(',', $hub);
293 foreach($hubs as $h) {
297 subscribe_to_hub($h,$importer,$contact);
303 $updated = datetime_convert();
305 $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
308 intval($contact['id'])
311 // loop - next contact
318 if (array_search(__file__,get_included_files())===0){
319 poller_run($argv,$argc);