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