+ if(count($r))
+ $datarray['parent-uri'] = $r[0]['parent-uri']; // Set the parent as the top-level item
+ // $datarray['parent-uri'] = $r[0]['uri'];
+ }
+
+ // Decoding the header
+ $subject = imap_mime_header_decode($meta->subject);
+ $datarray['title'] = "";
+ foreach($subject as $subpart)
+ if ($subpart->charset != "default")
+ $datarray['title'] .= iconv($subpart->charset, 'UTF-8//IGNORE', $subpart->text);
+ else
+ $datarray['title'] .= $subpart->text;
+
+ $datarray['title'] = notags(trim($datarray['title']));
+
+ //$datarray['title'] = notags(trim($meta->subject));
+ $datarray['created'] = datetime_convert('UTC','UTC',$meta->date);
+
+ // Is it a reply?
+ $reply = ((substr(strtolower($datarray['title']), 0, 3) == "re:") or
+ (substr(strtolower($datarray['title']), 0, 3) == "re-") or
+ ($raw_refs != ""));
+
+ // Remove Reply-signs in the subject
+ $datarray['title'] = RemoveReply($datarray['title']);
+
+ // If it seems to be a reply but a header couldn't be found take the last message with matching subject
+ if(!x($datarray,'parent-uri') and $reply) {
+ $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `title` = \"%s\" AND `uid` = %d ORDER BY `created` DESC LIMIT 1",
+ dbesc(protect_sprintf($datarray['title'])),
+ intval($importer_uid));
+ if(count($r))
+ $datarray['parent-uri'] = $r[0]['parent-uri'];
+ }
+
+ if(! x($datarray,'parent-uri'))
+ $datarray['parent-uri'] = $datarray['uri'];
+
+
+ $r = email_get_msg($mbox,$msg_uid, $reply);
+ if(! $r) {
+ logger("Mail: can't fetch msg ".$msg_uid." for ".$mailconf[0]['user']);
+ continue;
+ }
+ $datarray['body'] = escape_tags($r['body']);
+ $datarray['body'] = limit_body_size($datarray['body']);
+
+ logger("Mail: Importing ".$msg_uid." for ".$mailconf[0]['user']);
+
+ // some mailing lists have the original author as 'from' - add this sender info to msg body.
+ /// @TODO Adding a gravatar for the original author would be cool
+
+ if(! stristr($meta->from,$contact['addr'])) {
+ $from = imap_mime_header_decode($meta->from);
+ $fromdecoded = "";
+ foreach($from as $frompart)
+ if ($frompart->charset != "default")
+ $fromdecoded .= iconv($frompart->charset, 'UTF-8//IGNORE', $frompart->text);
+ else
+ $fromdecoded .= $frompart->text;
+
+ $fromarr = imap_rfc822_parse_adrlist($fromdecoded, $a->get_hostname());
+
+ $frommail = $fromarr[0]->mailbox."@".$fromarr[0]->host;
+
+ if (isset($fromarr[0]->personal))
+ $fromname = $fromarr[0]->personal;
+ else
+ $fromname = $frommail;
+
+ //$datarray['body'] = "[b]".t('From: ') . escape_tags($fromdecoded) . "[/b]\n\n" . $datarray['body'];
+
+ $datarray['author-name'] = $fromname;
+ $datarray['author-link'] = "mailto:".$frommail;
+ $datarray['author-avatar'] = $contact['photo'];
+
+ $datarray['owner-name'] = $contact['name'];
+ $datarray['owner-link'] = "mailto:".$contact['addr'];
+ $datarray['owner-avatar'] = $contact['photo'];
+
+ } else {
+ $datarray['author-name'] = $contact['name'];
+ $datarray['author-link'] = 'mailbox';
+ $datarray['author-avatar'] = $contact['photo'];
+ }
+
+ $datarray['uid'] = $importer_uid;
+ $datarray['contact-id'] = $contact['id'];
+ if($datarray['parent-uri'] === $datarray['uri'])
+ $datarray['private'] = 1;
+ if(($contact['network'] === NETWORK_MAIL) && (! get_pconfig($importer_uid,'system','allow_public_email_replies'))) {
+ $datarray['private'] = 1;
+ $datarray['allow_cid'] = '<' . $contact['id'] . '>';