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