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 $contacts = q("SELECT `id` FROM `contact`
51 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
53 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()",
58 if(! count($contacts)) {
62 foreach($contacts as $c) {
64 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
71 foreach($res as $contact) {
73 $contact['last-update'] = '0000-00-00 00:00:00';
75 if($contact['priority'] || $contact['subhub']) {
80 $t = $contact['last-update'];
82 // We should be getting everything via a hub. But just to be sure, let's check once a day.
83 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
84 // This also lets us update our subscription to the hub, and add or replace hubs in case it
85 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
88 if($contact['subhub']) {
89 $interval = get_config('system','pushpoll_frequency');
90 $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
93 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
98 * Based on $contact['priority'], should we poll this site now? Or later?
101 switch ($contact['priority']) {
103 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
107 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
111 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
115 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
120 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
124 if((! $update) && (! $force))
128 $importer_uid = $contact['uid'];
130 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
131 intval($importer_uid)
138 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
140 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
141 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
142 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
145 if($contact['network'] === 'dfrn') {
147 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
149 if(intval($contact['duplex']) && $contact['dfrn-id'])
150 $idtosend = '0:' . $orig_id;
151 if(intval($contact['duplex']) && $contact['issued-id'])
152 $idtosend = '1:' . $orig_id;
154 $url = $contact['poll'] . '?dfrn_id=' . $idtosend
155 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
156 . '&type=data&last_update=' . $last_update ;
158 $xml = fetch_url($url);
160 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
164 logger("poller: $url appears to be dead - marking for death ");
165 // dead connection - might be a transient event, or this might
166 // mean the software was uninstalled or the domain expired.
167 // Will keep trying for one month.
168 mark_for_death($contact);
170 // set the last-update so we don't keep polling
172 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
173 dbesc(datetime_convert()),
174 intval($contact['id'])
180 if(! strstr($xml,'<?xml')) {
181 logger('poller: response from ' . $url . ' did not contain XML.');
182 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
183 dbesc(datetime_convert()),
184 intval($contact['id'])
190 $res = simplexml_load_string($xml);
192 if(intval($res->status) == 1) {
193 logger("poller: $url replied status 1 - marking for death ");
195 // we may not be friends anymore. Will keep trying for one month.
196 // set the last-update so we don't keep polling
198 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
199 dbesc(datetime_convert()),
200 intval($contact['id'])
203 mark_for_death($contact);
206 if($contact['term-date'] != '0000-00-00 00:00:00') {
207 logger("poller: $url back from the dead - removing mark for death");
208 unmark_for_death($contact);
212 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
217 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
218 $challenge = hex2bin((string) $res->challenge);
222 if(($contact['duplex']) && strlen($contact['prvkey'])) {
223 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
224 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
227 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
228 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
231 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
233 if(strpos($final_dfrn_id,':') == 1)
234 $final_dfrn_id = substr($final_dfrn_id,2);
236 if($final_dfrn_id != $orig_id) {
238 // did not decode properly - cannot trust this site
242 $postvars['dfrn_id'] = $idtosend;
243 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
245 $xml = post_url($contact['poll'],$postvars);
249 // $contact['network'] !== 'dfrn'
251 $xml = fetch_url($contact['poll']);
254 logger('poller: received xml : ' . $xml, LOGGER_DATA);
256 if(! strstr($xml,'<?xml')) {
257 logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
258 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
259 dbesc(datetime_convert()),
260 intval($contact['id'])
265 consume_feed($xml,$importer,$contact,$hub,1);
267 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
269 consume_feed($xml,$importer,$contact,$hub,1);
272 if((strlen($hub)) && ($hub_update)
273 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
274 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
275 $hubs = explode(',', $hub);
277 foreach($hubs as $h) {
281 subscribe_to_hub($h,$importer,$contact);
287 $updated = datetime_convert();
289 $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
292 intval($contact['id'])
295 // loop - next contact
302 if (array_search(__file__,get_included_files())===0){
303 poller_run($argv,$argc);