]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Merge branch 'master', remote-tracking branch 'remotes/upstream/master'
[friendica.git] / include / poller.php
1 <?php
2
3 require_once("boot.php");
4
5
6 function poller_run($argv, $argc){
7         global $a, $db;
8
9         if(is_null($a)) {
10                 $a = new App;
11         }
12   
13         if(is_null($db)) {
14             @include(".htconfig.php");
15         require_once("dba.php");
16             $db = new dba($db_host, $db_user, $db_pass, $db_data);
17         unset($db_host, $db_user, $db_pass, $db_data);
18         };
19
20
21         require_once('include/session.php');
22         require_once('include/datetime.php');
23         require_once('library/simplepie/simplepie.inc');
24         require_once('include/items.php');
25         require_once('include/Contact.php');
26         require_once('include/email.php');
27         require_once('include/socgraph.php');
28         require_once('include/pidfile.php');
29
30         load_config('config');
31         load_config('system');
32
33         $lockpath = get_config('system','lockpath');
34         if ($lockpath != '') {
35                 $pidfile = new pidfile($lockpath, 'poller.lck');
36                 if($pidfile->is_already_running()) {
37                         logger("poller: Already running");
38                         exit;
39                 }
40         }
41
42         $a->set_baseurl(get_config('system','url'));
43
44         load_hooks();
45
46         logger('poller: start');
47         
48         // run queue delivery process in the background
49
50         proc_run('php',"include/queue.php");
51         
52         // expire any expired accounts
53
54         q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0 
55                 AND `account_expires_on` != '0000-00-00 00:00:00' 
56                 AND `account_expires_on` < UTC_TIMESTAMP() ");
57   
58         $abandon_days = intval(get_config('system','account_abandon_days'));
59         if($abandon_days < 1)
60                 $abandon_days = 0;
61
62         
63
64         // once daily run birthday_updates and then expire in background
65
66         $d1 = get_config('system','last_expire_day');
67         $d2 = intval(datetime_convert('UTC','UTC','now','d'));
68
69         if($d2 != intval($d1)) {
70
71                 update_contact_birthdays();
72
73                 update_suggestions();
74
75                 set_config('system','last_expire_day',$d2);
76                 proc_run('php','include/expire.php');
77         }
78
79         // clear old cache
80         Cache::clear();
81
82         // clear item cache files if they are older than one day
83         $cache = get_config('system','itemcache');
84         if (($cache != '') and is_dir($cache)) {
85                 if ($dh = opendir($cache)) {
86                         while (($file = readdir($dh)) !== false) {
87                                 $fullpath = $cache."/".$file;
88                                 if ((filetype($fullpath) == "file") and filectime($fullpath) < (time() - 86400))
89                                         unlink($fullpath);
90                         }
91                         closedir($dh);
92                 }
93         }
94
95         $manual_id  = 0;
96         $generation = 0;
97         $hub_update = false;
98         $force      = false;
99         $restart    = false;
100
101         if(($argc > 1) && ($argv[1] == 'force'))
102                 $force = true;
103
104         if(($argc > 1) && ($argv[1] == 'restart')) {
105                 $restart = true;
106                 $generation = intval($argv[2]);
107                 if(! $generation)
108                         killme();               
109         }
110
111         if(($argc > 1) && intval($argv[1])) {
112                 $manual_id = intval($argv[1]);
113                 $force     = true;
114         }
115
116         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
117
118         reload_plugins();
119
120         $d = datetime_convert();
121
122         if(! $restart)
123                 proc_run('php','include/cronhooks.php');
124
125         // Only poll from those with suitable relationships,
126         // and which have a polling address and ignore Diaspora since 
127         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
128
129         $abandon_sql = (($abandon_days) 
130                 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days)) 
131                 : '' 
132         );
133
134         $contacts = q("SELECT `contact`.`id` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
135                 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
136                 AND NOT `network` IN ( '%s', '%s' )
137                 $sql_extra 
138                 AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0 
139                 AND `user`.`account_expired` = 0 $abandon_sql ORDER BY RAND()",
140                 intval(CONTACT_IS_SHARING),
141                 intval(CONTACT_IS_FRIEND),
142                 dbesc(NETWORK_DIASPORA),
143                 dbesc(NETWORK_FACEBOOK)
144         );
145
146         if(! count($contacts)) {
147                 return;
148         }
149
150         foreach($contacts as $c) {
151
152                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
153                         intval($c['id'])
154                 );
155
156                 if((! $res) || (! count($res)))
157                         continue;
158
159                 foreach($res as $contact) {
160
161                         $xml = false;
162
163                         if($manual_id)
164                                 $contact['last-update'] = '0000-00-00 00:00:00';
165
166                         if($contact['network'] === NETWORK_DFRN)
167                                 $contact['priority'] = 2;
168
169                         if(!get_config('system','ostatus_use_priority') and ($contact['network'] === NETWORK_OSTATUS))
170                                 $contact['priority'] = 2;
171
172                         if($contact['priority'] || $contact['subhub']) {
173
174                                 $hub_update = true;
175                                 $update     = false;
176
177                                 $t = $contact['last-update'];
178
179                                 // We should be getting everything via a hub. But just to be sure, let's check once a day.
180                                 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
181                                 // This also lets us update our subscription to the hub, and add or replace hubs in case it
182                                 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'. 
183
184
185                                 if($contact['subhub']) {
186                                         $interval = get_config('system','pushpoll_frequency');
187                                         $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
188                                         $hub_update = false;
189         
190                                         if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
191                                                         $hub_update = true;
192                                 }
193                                 else
194                                         $hub_update = false;
195
196                                 /**
197                                  * Based on $contact['priority'], should we poll this site now? Or later?
198                                  */                     
199
200                                 switch ($contact['priority']) {
201                                         case 5:
202                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
203                                                         $update = true;
204                                                 break;                                  
205                                         case 4:
206                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
207                                                         $update = true;
208                                                 break;
209                                         case 3:
210                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
211                                                         $update = true;
212                                                 break;
213                                         case 2:
214                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
215                                                         $update = true;
216                                                 break;
217                                         case 1:
218                                         default:
219                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
220                                                         $update = true;
221                                                 break;
222                                 }
223                                 if((! $update) && (! $force))
224                                         continue;
225                         }
226
227                         // Check to see if we are running out of memory - if so spawn a new process and kill this one
228
229                         $avail_memory = return_bytes(ini_get('memory_limit'));
230                         $memused = memory_get_peak_usage(true);
231                         if(intval($avail_memory)) {
232                                 if(($memused / $avail_memory) > 0.95) {
233                                         if($generation + 1 > 10) {
234                                                 logger('poller: maximum number of spawns exceeded. Terminating.');
235                                                 killme();
236                                         }
237                                         logger('poller: memory exceeded. ' . $memused . ' bytes used. Spawning new poll.');
238                                         proc_run('php', 'include/poller.php', 'restart', (string) $generation + 1);
239                                         killme();
240                                 }
241                         }
242
243                         $importer_uid = $contact['uid'];
244                 
245                         $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` LEFT JOIN `user` on `contact`.`uid` = `user`.`uid` WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
246                                 intval($importer_uid)
247                         );
248                         if(! count($r))
249                                 continue;
250
251                         $importer = $r[0];
252
253                         logger("poller: poll: ({$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
254
255                         $last_update = (($contact['last-update'] === '0000-00-00 00:00:00') 
256                                 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
257                                 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
258                         );
259
260                         if($contact['network'] === NETWORK_DFRN) {
261
262                                 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
263
264                                 if(intval($contact['duplex']) && $contact['dfrn-id'])
265                                         $idtosend = '0:' . $orig_id;
266                                 if(intval($contact['duplex']) && $contact['issued-id'])
267                                         $idtosend = '1:' . $orig_id;
268
269                                 // they have permission to write to us. We already filtered this in the contact query.
270                                 $perm = 'rw';
271
272                                 $url = $contact['poll'] . '?dfrn_id=' . $idtosend 
273                                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION 
274                                         . '&type=data&last_update=' . $last_update 
275                                         . '&perm=' . $perm ;
276         
277                                 $handshake_xml = fetch_url($url);
278
279                                 logger('poller: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
280
281
282                                 if(! $handshake_xml) {
283                                         logger("poller: $url appears to be dead - marking for death ");
284                                         // dead connection - might be a transient event, or this might
285                                         // mean the software was uninstalled or the domain expired. 
286                                         // Will keep trying for one month.
287                                         mark_for_death($contact);
288
289                                         // set the last-update so we don't keep polling
290
291                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
292                                                 dbesc(datetime_convert()),
293                                                 intval($contact['id'])
294                                         );
295
296                                         continue;
297                                 }
298
299                                 if(! strstr($handshake_xml,'<?xml')) {
300                                         logger('poller: response from ' . $url . ' did not contain XML.');
301                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
302                                                 dbesc(datetime_convert()),
303                                                 intval($contact['id'])
304                                         );
305                                         continue;
306                                 }
307
308
309                                 $res = parse_xml_string($handshake_xml);
310         
311                                 if(intval($res->status) == 1) {
312                                         logger("poller: $url replied status 1 - marking for death ");
313
314                                         // we may not be friends anymore. Will keep trying for one month.
315                                         // set the last-update so we don't keep polling
316
317                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
318                                                 dbesc(datetime_convert()),
319                                                 intval($contact['id'])
320                                         );
321
322                                         mark_for_death($contact);
323                                 }
324                                 else {
325                                         if($contact['term-date'] != '0000-00-00 00:00:00') {
326                                                 logger("poller: $url back from the dead - removing mark for death");
327                                                 unmark_for_death($contact);
328                                         }
329                                 }
330
331                                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
332                                         continue;
333
334                                 if(((float) $res->dfrn_version > 2.21) && ($contact['poco'] == '')) {
335                                         q("update contact set poco = '%s' where id = %d limit 1",
336                                                 dbesc(str_replace('/profile/','/poco/', $contact['url'])),
337                                                 intval($contact['id'])
338                                         );
339                                 }
340
341                                 $postvars = array();
342
343                                 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
344                                 $challenge    = hex2bin((string) $res->challenge);
345
346                                 $final_dfrn_id = '';
347
348                                 if(($contact['duplex']) && strlen($contact['prvkey'])) {
349                                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
350                                         openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
351                                 }
352                                 else {
353                                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
354                                         openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
355                                 }
356
357                                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
358
359                                 if(strpos($final_dfrn_id,':') == 1)
360                                         $final_dfrn_id = substr($final_dfrn_id,2);
361
362                                 if($final_dfrn_id != $orig_id) {
363                                         logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);    
364                                         // did not decode properly - cannot trust this site 
365                                         continue;
366                                 }
367
368                                 $postvars['dfrn_id'] = $idtosend;
369                                 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
370                                 $postvars['perm'] = 'rw';
371
372                                 $xml = post_url($contact['poll'],$postvars);
373                         }
374                         elseif(($contact['network'] === NETWORK_OSTATUS) 
375                                 || ($contact['network'] === NETWORK_DIASPORA)
376                                 || ($contact['network'] === NETWORK_FEED) ) {
377
378                                 // Upgrading DB fields from an older Friendica version
379                                 // Will only do this once per notify-enabled OStatus contact
380                                 // or if relationship changes
381
382                                 $stat_writeable = ((($contact['notify']) && ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['rel'] == CONTACT_IS_FRIEND)) ? 1 : 0);
383
384                                 if($stat_writeable != $contact['writable']) {
385                                         q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
386                                                 intval($stat_writeable),
387                                                 intval($contact['id'])
388                                         );
389                                 }
390
391                                 // Are we allowed to import from this person?
392
393                                 if($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly'])
394                                         continue;
395
396                                 $xml = fetch_url($contact['poll']);
397                         }
398                         elseif($contact['network'] === NETWORK_MAIL || $contact['network'] === NETWORK_MAIL2) {
399
400                                 logger("Mail: Fetching");
401
402                                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
403                                 if($mail_disabled)
404                                         continue;
405
406                                 logger("Mail: Enabled");
407
408                                 $mbox = null;
409                                 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
410                                         intval($importer_uid)
411                                 );
412                                 $mailconf = q("SELECT * FROM `mailacct` WHERE `server` != '' AND `uid` = %d LIMIT 1",
413                                         intval($importer_uid)
414                                 );
415                                 if(count($x) && count($mailconf)) {
416                                     $mailbox = construct_mailbox_name($mailconf[0]);
417                                         $password = '';
418                                         openssl_private_decrypt(hex2bin($mailconf[0]['pass']),$password,$x[0]['prvkey']);
419                                         $mbox = email_connect($mailbox,$mailconf[0]['user'],$password);
420                                         unset($password);
421                                         logger("Mail: Connect");
422                                         if($mbox) {
423                                                 q("UPDATE `mailacct` SET `last_check` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
424                                                         dbesc(datetime_convert()),
425                                                         intval($mailconf[0]['id']),
426                                                         intval($importer_uid)
427                                                 );
428                                         }
429                                 }
430                                 if($mbox) {
431                                         logger("Mail: mbox");
432
433                                         $msgs = email_poll($mbox,$contact['addr']);
434
435                                         if(count($msgs)) {
436                                                 logger("Mail: Parsing ".count($msgs)." mails.");
437
438                                                 foreach($msgs as $msg_uid) {
439                                                         logger("Mail: Parsing mail ".$msg_uid);
440
441                                                         $datarray = array();
442                                                         $meta = email_msg_meta($mbox,$msg_uid);
443                                                         $headers = email_msg_headers($mbox,$msg_uid);
444
445                                                         // look for a 'references' header and try and match with a parent item we have locally.
446
447                                                         $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
448                                                         $datarray['uri'] = msgid2iri(trim($meta->message_id,'<>'));
449
450                                                         if($raw_refs) {
451                                                                 $refs_arr = explode(' ', $raw_refs);
452                                                                 if(count($refs_arr)) {
453                                                                         for($x = 0; $x < count($refs_arr); $x ++)
454                                                                                 $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'";
455                                                                 }
456                                                                 $qstr = implode(',',$refs_arr);
457                                                                 $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1",
458                                                                         intval($importer_uid)
459                                                                 );
460                                                                 if(count($r))
461                                                                         $datarray['parent-uri'] = $r[0]['uri'];
462                                                         }
463
464
465                                                         if(! x($datarray,'parent-uri'))
466                                                                 $datarray['parent-uri'] = $datarray['uri'];
467
468                                                         // Have we seen it before?
469                                                         $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
470                                                                 intval($importer_uid),
471                                                                 dbesc($datarray['uri'])
472                                                         );
473
474                                                         if(count($r)) {
475                                                                 logger("Mail: Seen before ".$msg_uid);
476                                                                 if($meta->deleted && ! $r[0]['deleted']) {
477                                                                         q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1",
478                                                                                 dbesc(datetime_convert()),
479                                                                                 intval($r[0]['id'])
480                                                                         );
481                                                                 }
482                                                                 switch ($mailconf[0]['action']) {
483                                                                         case 0:
484                                                                                 break;
485                                                                         case 1:
486                                                                                 logger("Mail: Deleting ".$msg_uid);
487                                                                                 imap_delete($mbox, $msg_uid, FT_UID);
488                                                                                 break;
489                                                                         case 2:
490                                                                                 logger("Mail: Mark as seen ".$msg_uid);
491                                                                                 imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
492                                                                                 break;
493                                                                         case 3:
494                                                                                 logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']);
495                                                                                 imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
496                                                                                 if ($mailconf[0]['movetofolder'] != "")
497                                                                                         imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
498                                                                                 break;
499                                                                 }
500                                                                 continue;
501                                                         }
502
503                                                         // Decoding the header
504                                                         $subject = imap_mime_header_decode($meta->subject);
505                                                         $datarray['title'] = "";
506                                                         foreach($subject as $subpart)
507                                                                 if ($subpart->charset != "default")
508                                                                         $datarray['title'] .= iconv($subpart->charset, 'UTF-8//IGNORE', $subpart->text);
509                                                                 else
510                                                                         $datarray['title'] .= $subpart->text;
511
512                                                         $datarray['title'] = notags(trim($datarray['title']));
513
514                                                         //$datarray['title'] = notags(trim($meta->subject));
515                                                         $datarray['created'] = datetime_convert('UTC','UTC',$meta->date);
516
517                                                         // Is it  reply?
518                                                         $reply = ((substr(strtolower($datarray['title']), 0, 3) == "re:") or
519                                                                 (substr(strtolower($datarray['title']), 0, 3) == "re-") or
520                                                                 (raw_refs != ""));
521
522                                                         $r = email_get_msg($mbox,$msg_uid, $reply);
523                                                         if(! $r) {
524                                                                 logger("Mail: can't fetch msg ".$msg_uid);
525                                                                 continue;
526                                                         }
527                                                         $datarray['body'] = escape_tags($r['body']);
528
529                                                         logger("Mail: Importing ".$msg_uid);
530
531                                                         // some mailing lists have the original author as 'from' - add this sender info to msg body.
532                                                         // todo: adding a gravatar for the original author would be cool
533
534                                                         if(! stristr($meta->from,$contact['addr'])) {
535                                                                 $from = imap_mime_header_decode($meta->from);
536                                                                 $fromdecoded = "";
537                                                                 foreach($from as $frompart)
538                                                                         if ($frompart->charset != "default")
539                                                                                 $fromdecoded .= iconv($frompart->charset, 'UTF-8//IGNORE', $frompart->text);
540                                                                         else
541                                                                                 $fromdecoded .= $frompart->text;
542
543                                                                 $datarray['body'] = "[b]".t('From: ') . escape_tags($fromdecoded) . "[/b]\n\n" . $datarray['body'];
544                                                         }
545
546                                                         $datarray['uid'] = $importer_uid;
547                                                         $datarray['contact-id'] = $contact['id'];
548                                                         if($datarray['parent-uri'] === $datarray['uri'])
549                                                                 $datarray['private'] = 1;
550                                                         if(($contact['network'] === NETWORK_MAIL) && (! get_pconfig($importer_uid,'system','allow_public_email_replies'))) {
551                                                                 $datarray['private'] = 1;
552                                                                 $datarray['allow_cid'] = '<' . $contact['id'] . '>';
553                                                         }
554                                                         $datarray['author-name'] = $contact['name'];
555                                                         $datarray['author-link'] = 'mailbox';
556                                                         $datarray['author-avatar'] = $contact['photo'];
557
558                                                         $stored_item = item_store($datarray);
559                                                         q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d",
560                                                                 dbesc($datarray['parent-uri']),
561                                                                 intval($importer_uid)
562                                                         );
563                                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
564                                                                 intval($stored_item)
565                                                         );
566                                                         switch ($mailconf[0]['action']) {
567                                                                 case 0:
568                                                                         break;
569                                                                 case 1:
570                                                                         logger("Mail: Deleting ".$msg_uid);
571                                                                         imap_delete($mbox, $msg_uid, FT_UID);
572                                                                         break;
573                                                                 case 2:
574                                                                         logger("Mail: Mark as seen ".$msg_uid);
575                                                                         imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
576                                                                         break;
577                                                                 case 3:
578                                                                         logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']);
579                                                                         imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
580                                                                         if ($mailconf[0]['movetofolder'] != "")
581                                                                                 imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
582                                                                         break;
583                                                         }
584                                                 }
585                                         }
586
587                                         imap_close($mbox);
588                                 }
589                         }
590                         elseif($contact['network'] === NETWORK_FACEBOOK) {
591                                 // This is picked up by the Facebook plugin on a cron hook.
592                                 // Ignored here.
593                         }
594
595                         if($xml) {
596                                 logger('poller: received xml : ' . $xml, LOGGER_DATA);
597
598                                 if(! strstr($xml,'<?xml')) {
599                                         logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
600                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
601                                                 dbesc(datetime_convert()),
602                                                 intval($contact['id'])
603                                         );
604                                         continue;
605                                 }
606
607
608                                 consume_feed($xml,$importer,$contact,$hub,1,1);
609
610                                 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
611         
612                                 consume_feed($xml,$importer,$contact,$hub,1,2);
613
614                                 $hubmode = 'subscribe';
615                                 if($contact['network'] === NETWORK_DFRN || $contact['blocked'] || $contact['readonly'])
616                                         $hubmode = 'unsubscribe';
617
618                                 if((strlen($hub)) && ($hub_update) && ($contact['rel'] != CONTACT_IS_FOLLOWER)) {
619                                         logger('poller: hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
620                                         $hubs = explode(',', $hub);
621                                         if(count($hubs)) {
622                                                 foreach($hubs as $h) {
623                                                         $h = trim($h);
624                                                         if(! strlen($h))
625                                                                 continue;
626                                                         subscribe_to_hub($h,$importer,$contact,$hubmode);
627                                                 }
628                                         }
629                                 }
630                         }
631
632
633                         $updated = datetime_convert();
634
635                         $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
636                                 dbesc($updated),
637                                 dbesc($updated),
638                                 intval($contact['id'])
639                         );
640
641
642                         // load current friends if possible.
643
644                         if($contact['poco']) {  
645                                 $r = q("SELECT count(*) as total from glink 
646                                         where `cid` = %d and updated > UTC_TIMESTAMP() - INTERVAL 1 DAY",
647                                         intval($contact['id'])
648                                 );
649                         }
650                         if(count($r)) {
651                                 if(! $r[0]['total']) {
652                                         poco_load($contact['id'],$importer_uid,$contact['poco']);
653                                 }
654                         }
655
656                         // loop - next contact
657                 }
658         }
659
660                 
661         return;
662 }
663
664 if (array_search(__file__,get_included_files())===0){
665   poller_run($argv,$argc);
666   killme();
667 }