X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=ConvertStyle.php;h=4f52e1016cee579761774899efbaa04e21ed42c7;hp=7c57ec73dd497f0b4bc919e83fdf59beb1b44e21;hb=refs%2Fheads%2Fwip_alan_T6343_generic_progress_bar_delete;hpb=dc5ca1714ef6f1367614d6264429eb70d194a159 diff --git a/ConvertStyle.php b/ConvertStyle.php index 7c57ec73..4f52e101 100644 --- a/ConvertStyle.php +++ b/ConvertStyle.php @@ -18,18 +18,22 @@ class Pman_Core_ConvertStyle extends Pman } function relPath($base, $url) - { + { + if (preg_match('/^(http|https|mailto):/',$url)) { return $url; } + $ui = parse_url($base); + if($ui['host'] == 'localhost'){ + return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/'. $url; + } + if (substr($url,0,2) == '//') { return $ui['scheme'] .':' . $url; } - - if (substr($url,0,1) == '/') { return $ui['scheme'] .'://'.$ui['host']. $url; } @@ -48,10 +52,10 @@ class Pman_Core_ConvertStyle extends Pman function checkHeader($url) { - if(strpos($url, 'https') !== false) - { - $this->jerr('accept HTTP url only!'); - } + // if(strpos($url, 'https') !== false) + // { + // $this->jerr('accept HTTP url only!'); + // } $headers = get_headers($url, 1); if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false) { @@ -62,30 +66,35 @@ class Pman_Core_ConvertStyle extends Pman var $styleSheets = array(); - function convertStyle($base, $path, $is_url = false) + function convertStyle($url, $file, $is_url = true) { -// if(!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"); -// } -// } + $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($path); + $data = file_get_contents($file); } + if(preg_match('/^\s*\s*/', $data)){ + $inLineCss = false; + } libxml_use_internal_errors (true); $doc = new DOMDocument('1.0', 'UTF-8'); @@ -93,56 +102,54 @@ class Pman_Core_ConvertStyle extends Pman $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 (empty($url) && !preg_match("/^http(.*)$/", $href, $matches)) { -// // import from file , must use absolutely url -// $this->jerr('Please use the absolutely url for link href!'); -// } -// if (!empty($url)) { -// // import from URL -// $href = $this->relPath($url, $href); -// } -// $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href); -// } -// } + 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){ - $ss = $doc->saveHTML($s); - print_r($ss); -// $this->styleSheets[] = $this->$this->replaceImageUrl($s) - echo '
'; + $this->styleSheets[] = $this->replaceImageUrl($s->nodeValue, $url); } - exit; $data = $doc->saveHTML(); + if(!$inLineCss){ + return $data; + } + $htmldoc = new HTML_CSS_InlineStyle($data); if(count($this->styleSheets) > 0){ foreach ($this->styleSheets as $styleSheet){