3 require_once 'Pman.php';
4 require_once 'HTML/CSS/InlineStyle.php';
6 class Pman_Core_ConvertStyle extends Pman
10 if (HTML_FlexyFramework::get()->cli) {
13 $this->authUser = $this->getAuthUser();
14 if (!$this->authUser) {
20 function relPath($base, $url)
22 if (preg_match('/^(http|https|mailto):/',$url)) {
26 $ui = parse_url($base);
28 if (substr($url,0,2) == '//') {
29 return $ui['scheme'] .':' . $url;
32 if (substr($url,0,1) == '/') {
33 return $ui['scheme'] .'://'.$ui['host']. $url;
36 if (substr($ui['path'], -1) == '/') {
37 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . $url;
39 if (!strlen($ui['path'])) {
40 return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
44 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
48 function checkHeader($url)
50 if(strpos($url, 'https') !== false)
52 $this->jerr('accept HTTP url only!');
54 $headers = get_headers($url, 1);
55 if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
57 $this->jerr('accept html file only!');
62 var $styleSheets = array();
64 function convertStyle($url, $file, $is_url = true)
66 if($is_url && !empty($url))
68 $host = parse_url($url);
69 require_once 'System.php';
70 $wget = System::which('wget');
72 $this->jerr("no wget");
74 $cmd = $wget . ' -q -O - ' . escapeshellarg($url);
79 if (!trim(strlen($data))) {
80 $this->jerr("url returned an empty string");
85 $data = file_get_contents($file);
89 libxml_use_internal_errors (true);
90 $doc = new DOMDocument('1.0', 'UTF-8');
91 $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
92 $doc->formatOutput = true;
94 $xpath = new DOMXpath($doc);
95 foreach ($xpath->query('//img[@src]') as $img) {
96 $href = $img->getAttribute('src');
97 if (!preg_match("/^http(.*)$/", $href, $matches)) {
99 $img->setAttribute('src', $this->relPath($url, $href));
102 $this->jerr('Please use the absolutely url for image src!');
107 foreach ($xpath->query('//a[@href]') as $a) {
108 $href = $a->getAttribute('href');
109 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
111 $a->setAttribute('href', $this->relPath($url, $href));
114 $this->jerr('Please use the absolutely url for a href!');
118 foreach ($xpath->query('//link[@href]') as $l) {
119 if($l->getAttribute('rel') == 'stylesheet'){
120 $href = $l->getAttribute('href');
122 if(!preg_match("/^http(.*)$/", $href, $matches)){
124 $href = $this->relPath($url, $href);
126 $this->jerr('Please use the absolutely url for link href!');
129 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
133 foreach ($xpath->query('//style') as $s){
134 $this->styleSheets[] = $this->replaceImageUrl($s->nodeValue, $url);
137 $data = $doc->saveHTML();
139 $htmldoc = new HTML_CSS_InlineStyle($data);
140 if(count($this->styleSheets) > 0){
141 foreach ($this->styleSheets as $styleSheet){
142 $htmldoc->applyStylesheet($styleSheet);
145 $html = $htmldoc->getHTML();
146 libxml_use_internal_errors (false);
148 if (!function_exists('tidy_repair_string')) {
149 return "INSTALL TIDY ON SERVER " . $html;
152 $html = tidy_repair_string(
156 'output-xhtml' => TRUE,
167 function replaceImageUrl($stylesheet,$href)
169 $base = explode("/", $href);
170 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
171 foreach($s as $k => $v){
176 array_push($base, $v);
177 $s[$k] = implode("/", $base);
180 $r = implode("url(", $s);
182 $this->checkImportCss($r);
187 function checkImportCss($r)
189 if(preg_match("/@import url/", $r, $matches)){
190 $importCss = explode("@import url", $r);
191 foreach ($importCss as $css){
192 if(preg_match("/\.css/", $css, $matches)){
193 $cssFileName = explode(".css", $css);
194 $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
196 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);