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");
33 q("DELETE FROM `cache` WHERE `updated` < '%s'",
34 dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
40 if(($argc > 1) && ($argv[1] == 'force'))
43 if(($argc > 1) && intval($argv[1])) {
44 $manual_id = intval($argv[1]);
48 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
50 $d = datetime_convert();
51 call_hooks('cron', $d);
55 $contacts = q("SELECT `id` FROM `contact`
56 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
58 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()",
63 if(! count($contacts)) {
67 foreach($contacts as $c) {
69 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
76 foreach($res as $contact) {
78 $contact['last-update'] = '0000-00-00 00:00:00';
80 if($contact['priority'] || $contact['subhub']) {
85 $t = $contact['last-update'];
87 // We should be getting everything via a hub. But just to be sure, let's check once a day.
88 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
89 // This also lets us update our subscription to the hub, and add or replace hubs in case it
90 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
93 if($contact['subhub']) {
94 $interval = get_config('system','pushpoll_frequency');
95 $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
98 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
103 * Based on $contact['priority'], should we poll this site now? Or later?
106 switch ($contact['priority']) {
108 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
112 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
116 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
120 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
125 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
129 if((! $update) && (! $force))
133 $importer_uid = $contact['uid'];
135 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
136 intval($importer_uid)
143 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
145 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
146 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
147 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
150 if($contact['network'] === 'dfrn') {
152 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
154 if(intval($contact['duplex']) && $contact['dfrn-id'])
155 $idtosend = '0:' . $orig_id;
156 if(intval($contact['duplex']) && $contact['issued-id'])
157 $idtosend = '1:' . $orig_id;
159 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
160 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
161 . '&type=data&last_update=' . $last_update ;
163 $xml = fetch_url($url);
165 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
169 logger("poller: $url appears to be dead - marking for death ");
170 // dead connection - might be a transient event, or this might
171 // mean the software was uninstalled or the domain expired.
172 // Will keep trying for one month.
173 mark_for_death($contact);
175 // set the last-update so we don't keep polling
177 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
178 dbesc(datetime_convert()),
179 intval($contact['id'])
185 if(! strstr($xml,'<?xml')) {
186 logger('poller: response from ' . $url . ' did not contain XML.');
187 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
188 dbesc(datetime_convert()),
189 intval($contact['id'])
195 $res = simplexml_load_string($xml);
197 if(intval($res->status) == 1) {
198 logger("poller: $url replied status 1 - marking for death ");
200 // we may not be friends anymore. Will keep trying for one month.
201 // set the last-update so we don't keep polling
203 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
204 dbesc(datetime_convert()),
205 intval($contact['id'])
208 mark_for_death($contact);
211 if($contact['term-date'] != '0000-00-00 00:00:00') {
212 logger("poller: $url back from the dead - removing mark for death");
213 unmark_for_death($contact);
217 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
222 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
223 $challenge = hex2bin((string) $res->challenge);
227 if(($contact['duplex']) && strlen($contact['prvkey'])) {
228 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
229 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
232 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
233 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
236 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
238 if(strpos($final_dfrn_id,':') == 1)
239 $final_dfrn_id = substr($final_dfrn_id,2);
241 if($final_dfrn_id != $orig_id) {
243 // did not decode properly - cannot trust this site
247 $postvars['dfrn_id'] = $idtosend;
248 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
250 $xml = post_url($contact['poll'],$postvars);
254 // $contact['network'] !== 'dfrn'
256 $xml = fetch_url($contact['poll']);
259 logger('poller: received xml : ' . $xml, LOGGER_DATA);
261 if(! strstr($xml,'<?xml')) {
262 logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
263 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
264 dbesc(datetime_convert()),
265 intval($contact['id'])
270 consume_feed($xml,$importer,$contact,$hub,1);
272 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
274 consume_feed($xml,$importer,$contact,$hub,1);
277 if((strlen($hub)) && ($hub_update)
278 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
279 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
280 $hubs = explode(',', $hub);
282 foreach($hubs as $h) {
286 subscribe_to_hub($h,$importer,$contact);
292 $updated = datetime_convert();
294 $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
297 intval($contact['id'])
300 // loop - next contact
307 if (array_search(__file__,get_included_files())===0){
308 poller_run($argv,$argc);