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