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