public function printAttach($id)
{
global $injector, $page_output, $prefs, $registry;
if (is_null($id) || !($render = $this->_contents->renderMIMEPart($id, IMP_Contents::RENDER_FULL))) {
return array();
}
$part = reset($render);
/* Directly render part if this is not an HTML part or it is empty. */
if (stripos($part['type'], 'text/html') !== 0 || !strlen($part['data'])) {
return $part;
}
$imp_ui_mbox = new IMP_Mailbox_Ui();
$headerob = $this->_contents->getHeader();
$headers = array();
$header_labels = array('date' => _("Date"), 'from' => _("From"), 'to' => _("To"), 'cc' => _("Cc"), 'subject' => _("Subject"));
foreach ($header_labels as $key => $val) {
if ($hdr_val = $headerob[$key]) {
/* Format date string. */
if ($key == 'date') {
$date_ob = new IMP_Message_Date($hdr_val);
$hdr_val = $date_ob->format($date_ob::DATE_FORCE);
}
$headers[] = array('header' => $val, 'value' => $hdr_val);
}
}
if ($prefs->getValue('add_printedby')) {
$user_identity = $injector->getInstance('IMP_Identity');
$headers[] = array('header' => _("Printed By"), 'value' => $user_identity->getFullname() ?: $registry->getAuth());
}
$view = new Horde_View(array('templatePath' => IMP_TEMPLATES . '/print'));
$view->addHelper('Text');
$view->headers = $headers;
$ctype = new Horde_Mime_Headers_ContentParam('Content-Type', $part['type']);
$header_dom = new Horde_Domhtml(Horde_String::convertCharset($view->render('headers'), 'UTF-8', $ctype['charset']), $ctype['charset']);
$elt = $header_dom->dom->getElementById('headerblock');
$elt->removeAttribute('id');
if ($elt->hasAttribute('class')) {
$selectors = array('body');
foreach (explode(' ', $elt->getAttribute('class')) as $val) {
if (strlen($val = trim($val))) {
$selectors[] = '.' . $val;
}
}
/* Cache CSS. */
$cache_ob = $injector->getInstance('Horde_Cache');
$css_list = $page_output->css->getStylesheets();
$ctx = hash_init('md5');
foreach ($css_list as $val) {
hash_update($ctx, $val['fs']);
hash_update($ctx, filemtime($val['fs']));
}
$cache_id = 'imp_printcss_' . hash_final($ctx);
if (($style = $cache_ob->get($cache_id, 0)) === false) {
try {
$css_parser = new Horde_Css_Parser($page_output->css->loadCssFiles($page_output->css->getStylesheets()));
$style = '';
foreach ($css_parser->doc->getContents() as $val) {
if ($val instanceof Sabberworm\CSS\RuleSet\DeclarationBlock && array_intersect($selectors, array_map('strval', $val->getSelectors()))) {
$style .= implode('', array_map('strval', $val->getRules()));
}
}
$cache_ob->set($cache_id, $style, 86400);
} catch (Exception $e) {
// Ignore CSS if it can't be parsed.
}
}
if (strlen($style)) {
$elt->setAttribute('style', ($elt->hasAttribute('style') ? rtrim($elt->getAttribute('style'), ' ;') . ';' : '') . $style);
}
}
$elt->removeAttribute('class');
/* Need to wrap headers in another DIV. */
$newdiv = new DOMDocument();
$div = $newdiv->createElement('div');
$div->appendChild($newdiv->importNode($elt, true));
$doc = new Horde_Domhtml($part['data'], $ctype['charset']);
$bodyelt = $doc->dom->getElementsByTagName('body')->item(0);
$bodyelt->insertBefore($doc->dom->importNode($div, true), $bodyelt->firstChild);
/* Make the title the e-mail subject. */
$headelt = $doc->getHead();
foreach ($headelt->getElementsByTagName('title') as $node) {
$headelt->removeChild($node);
}
$headelt->appendChild($doc->dom->createElement('title', htmlspecialchars($imp_ui_mbox->getSubject($headerob['Subject']))));
return array('data' => $doc->returnHtml(), 'name' => $part['name'], 'type' => $part['type']);
}