Exemple #1
0
 /**
  * Executes any code necessary after applying the filter patterns.
  *
  * @param string $text  The text after the filtering.
  *
  * @return string|Horde_Domhtml  The modified text or a Domhtml object if
  *                               the 'return_dom' parameter is set.
  * @throws Exception
  */
 public function postProcess($text)
 {
     $dom = new Horde_Domhtml($text, $this->_params['charset']);
     foreach ($dom as $node) {
         $this->_node($node);
     }
     if ($this->_params['noprefetch']) {
         $meta = $dom->dom->createElement('meta');
         $meta->setAttribute('http-equiv', 'x-dns-prefetch-control');
         $meta->setAttribute('value-equiv', 'off');
         $head = $dom->getHead();
         $head->appendChild($meta);
     }
     if ($this->_params['return_dom']) {
         return $dom;
     }
     return $this->_params['return_document'] ? $dom->returnHtml() : $dom->returnBody();
 }
Exemple #2
0
 /**
  * Executes any code necessary after applying the filter patterns.
  *
  * @param string $text  The text after the filtering.
  *
  * @return string  The modified text.
  */
 public function postProcess($text)
 {
     // We cannot find those elements via DOM because HTML doesn't know
     // about namespaces.
     $text = str_replace('<o:p>&nbsp;</o:p>', '', $text);
     try {
         $dom = new Horde_Domhtml($text, $this->_params['charset']);
     } catch (Exception $e) {
         return $text;
     }
     // Replace all <p> elements of class "MsoNormal" with <br> elements,
     // unless they contain other classes. Then replace with <div> elements.
     foreach ($dom as $child) {
         if ($child instanceof DOMElement && Horde_String::lower($child->tagName) == 'p') {
         }
         if (!$child instanceof DOMElement || Horde_String::lower($child->tagName) != 'p' || !($css = $child->getAttribute('class')) || strpos($css, 'MsoNormal') === false) {
             continue;
         }
         $css = trim(str_replace('MsoNormal', '', $css));
         if (strlen($css)) {
             $div = $dom->dom->createElement('div');
             $div->setAttribute('class', $css);
             foreach ($child->childNodes as $subchild) {
                 $div->appendChild($subchild);
             }
             $child->parentNode->insertBefore($div, $child);
         } elseif (strlen(preg_replace('/^\\s*(.*)\\s*$/u', '$1', $child->textContent))) {
             while ($child->hasChildNodes()) {
                 $tomove = $child->removeChild($child->firstChild);
                 $child->parentNode->insertBefore($tomove, $child);
             }
             $child->parentNode->insertBefore($dom->dom->createElement('br'), $child);
         }
         $child->parentNode->removeChild($child);
     }
     return $dom->returnHtml(array('charset' => $this->_params['charset']));
 }
Exemple #3
0
 public function testReturnHtmlMetaCharset()
 {
     $dom = new Horde_Domhtml('<html><body><div>foo</div></body></html>', 'UTF-8');
     $this->assertRegExp('/"text\\/html; charset=utf-8"/', $dom->returnHtml(array('metacharset' => true)));
 }
Exemple #4
0
 /**
  */
 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();
     $basic_headers = $injector->getInstance('IMP_Message_Ui')->basicHeaders();
     unset($basic_headers['bcc'], $basic_headers['reply-to']);
     $headerob = $this->_contents->getHeader();
     $d_param = Horde_Mime::decodeParam('content-type', $part['type']);
     $headers = array();
     foreach ($basic_headers as $key => $val) {
         if ($hdr_val = $headerob->getValue($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;
     $header_dom = new Horde_Domhtml(Horde_String::convertCharset($view->render('headers'), 'UTF-8', $d_param['params']['charset']), $d_param['params']['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_list = array();
         $cache_ob = $injector->getInstance('Horde_Cache');
         $css_list = $page_output->css->getStylesheets();
         foreach ($css_list as $val) {
             $cache_list[] = $val['fs'];
             $cache_list[] = filemtime($val['fs']);
         }
         $cache_id = 'imp_printcss_' . hash(PHP_MINOR_VERSION >= 4 ? 'fnv132' : 'sha1', implode('|', $cache_list));
         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));
     $pstring = Horde_Mime::decodeParam('content-type', $part['type']);
     $doc = new Horde_Domhtml($part['data'], $pstring['params']['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->getValue('subject')))));
     return array('data' => $doc->returnHtml(), 'name' => $part['name'], 'type' => $part['type']);
 }