Beispiel #1
0
 function convertStyle($url, $file, $is_url = true)
 {
     $inLineCss = true;
     if ($is_url && !empty($url)) {
         $host = parse_url($url);
         require_once 'System.php';
         $wget = System::which('wget');
         if (!$wget) {
             $this->jerr("no wget");
         }
         $cmd = $wget . ' -q -O -  ' . escapeshellarg($url);
         //echo $cmd; exit;
         $data = `{$cmd}`;
         if (!trim(strlen($data))) {
             $this->jerr("url returned an empty string");
         }
     }
     if (!$is_url) {
         $data = file_get_contents($file);
     }
     if (preg_match('/^\\s*<!--\\s*NOT CONVERT STYLE\\s*-->\\s*/', $data)) {
         $inLineCss = false;
     }
     libxml_use_internal_errors(true);
     $doc = new DOMDocument('1.0', 'UTF-8');
     $doc->loadHTML('<?xml encoding="UTF-8">' . $data);
     $doc->formatOutput = true;
     $xpath = new DOMXpath($doc);
     foreach ($xpath->query('//img[@src]') as $img) {
         $href = $img->getAttribute('src');
         if (!preg_match("/^http(.*)\$/", $href, $matches)) {
             if (!empty($url)) {
                 $img->setAttribute('src', $this->relPath($url, $href));
                 continue;
             }
             $this->jerr('Please use the absolutely url for image src!');
         }
     }
     foreach ($xpath->query('//a[@href]') as $a) {
         $href = $a->getAttribute('href');
         if (!preg_match("/^http|mailto|#(.*)\$/", $href, $matches)) {
             if (!empty($url)) {
                 $a->setAttribute('href', $this->relPath($url, $href));
                 continue;
             }
             $this->jerr('Please use the absolutely url for a href!');
         }
     }
     foreach ($xpath->query('//link[@href]') as $l) {
         if ($l->getAttribute('rel') == 'stylesheet') {
             $href = $l->getAttribute('href');
             if (!preg_match("/^http(.*)\$/", $href, $matches)) {
                 if (empty($url)) {
                     $this->jerr('Please use the absolutely url for link href!');
                 }
                 $href = $this->relPath($url, $href);
             }
             $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href), $href);
         }
     }
     foreach ($xpath->query('//style') as $s) {
         $this->styleSheets[] = $this->replaceImageUrl($s->nodeValue, $url);
     }
     $data = $doc->saveHTML();
     if (!$inLineCss) {
         return $data;
     }
     $htmldoc = new HTML_CSS_InlineStyle($data);
     if (count($this->styleSheets) > 0) {
         foreach ($this->styleSheets as $styleSheet) {
             $htmldoc->applyStylesheet($styleSheet);
         }
     }
     $html = $htmldoc->getHTML();
     libxml_use_internal_errors(false);
     if (!function_exists('tidy_repair_string')) {
         return "INSTALL TIDY ON SERVER " . $html;
     }
     $html = tidy_repair_string($html, array('indent' => TRUE, 'output-xhtml' => TRUE, 'wrap' => 120), 'UTF8');
     return $html;
 }
Beispiel #2
0
 function cleanStyle($str)
 {
     static $is = false;
     if (!$is) {
         require_once 'HTML/CSS/InlineStyle.php';
         $is = new HTML_CSS_InlineStyle();
     }
     $ar = $is->_styleToArray($str);
     foreach ($ar as $k => $v) {
         if (in_array(strtolower(trim($k)), $this->cssKeywords)) {
             //echo "Trashing BL css keyword $k=$v <br/>";
             unset($ar[$k]);
             continue;
         }
         foreach ($this->_protoRegexps as $proto) {
             if (preg_match($proto, $v)) {
                 echo "{$proto} - Trashing {$k}={$v} <br/>";
                 unset($ar[$k]);
                 continue 2;
             }
         }
     }
     $st = array();
     foreach ($ar as $prop => $val) {
         $st[] = "{$prop}:{$val}";
     }
     return implode(';', $st);
 }