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 // they have permission to write to us. We already filtered this in the contact query.
173 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
174 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
175 . '&type=data&last_update=' . $last_update
178 $xml = fetch_url($url);
180 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
184 logger("poller: $url appears to be dead - marking for death ");
185 // dead connection - might be a transient event, or this might
186 // mean the software was uninstalled or the domain expired.
187 // Will keep trying for one month.
188 mark_for_death($contact);
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'])
200 if(! strstr($xml,'<?xml')) {
201 logger('poller: response from ' . $url . ' did not contain XML.');
202 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
203 dbesc(datetime_convert()),
204 intval($contact['id'])
210 $res = parse_xml_string($xml);
212 if(intval($res->status) == 1) {
213 logger("poller: $url replied status 1 - marking for death ");
215 // we may not be friends anymore. Will keep trying for one month.
216 // set the last-update so we don't keep polling
218 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
219 dbesc(datetime_convert()),
220 intval($contact['id'])
223 mark_for_death($contact);
226 if($contact['term-date'] != '0000-00-00 00:00:00') {
227 logger("poller: $url back from the dead - removing mark for death");
228 unmark_for_death($contact);
232 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
237 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
238 $challenge = hex2bin((string) $res->challenge);
242 if(($contact['duplex']) && strlen($contact['prvkey'])) {
243 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
244 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
247 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
248 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
251 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
253 if(strpos($final_dfrn_id,':') == 1)
254 $final_dfrn_id = substr($final_dfrn_id,2);
256 if($final_dfrn_id != $orig_id) {
257 logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
258 // did not decode properly - cannot trust this site
262 $postvars['dfrn_id'] = $idtosend;
263 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
264 $postvars['perm'] = 'rw';
266 $xml = post_url($contact['poll'],$postvars);
270 // $contact['network'] !== 'dfrn'
272 if(($contact['notify']) && (! $contact['writable'])) {
273 q("UPDATE `contact` SET `writable` = 1 WHERE `id` = %d LIMIT 1",
274 intval($contact['id'])
277 $xml = fetch_url($contact['poll']);
280 logger('poller: received xml : ' . $xml, LOGGER_DATA);
282 if(! strstr($xml,'<?xml')) {
283 logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
284 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
285 dbesc(datetime_convert()),
286 intval($contact['id'])
292 consume_feed($xml,$importer,$contact,$hub,1, true);
294 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
296 consume_feed($xml,$importer,$contact,$hub,1);
299 if((strlen($hub)) && ($hub_update)
300 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
301 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
302 $hubs = explode(',', $hub);
304 foreach($hubs as $h) {
308 subscribe_to_hub($h,$importer,$contact);
314 $updated = datetime_convert();
316 $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
319 intval($contact['id'])
322 // loop - next contact
329 if (array_search(__file__,get_included_files())===0){
330 poller_run($argv,$argc);