Пример #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();
 }
Пример #2
0
 public function testHeadGeneration()
 {
     $dom = new Horde_Domhtml('<div>foo</div>');
     $head = $dom->getHead();
     $this->assertNull($head->previousSibling);
     $this->assertEquals('iso-8859-1', $dom->getCharset());
 }
Пример #3
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']);
 }