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)
23 if (preg_match('/^(http|https|mailto):/',$url)) {
27 $ui = parse_url($base);
29 if($ui['host'] == 'localhost'){
30 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/'. $url;
33 if (substr($url,0,2) == '//') {
34 return $ui['scheme'] .':' . $url;
37 if (substr($url,0,1) == '/') {
38 return $ui['scheme'] .'://'.$ui['host']. $url;
41 if (substr($ui['path'], -1) == '/') {
42 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . $url;
44 if (!strlen($ui['path'])) {
45 return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
49 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
53 function checkHeader($url)
55 if(strpos($url, 'https') !== false)
57 $this->jerr('accept HTTP url only!');
59 $headers = get_headers($url, 1);
60 if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
62 $this->jerr('accept html file only!');
67 var $styleSheets = array();
69 function convertStyle($url, $file, $is_url = true)
71 if($is_url && !empty($url))
73 $host = parse_url($url);
74 require_once 'System.php';
75 $wget = System::which('wget');
77 $this->jerr("no wget");
79 $cmd = $wget . ' -q -O - ' . escapeshellarg($url);
84 if (!trim(strlen($data))) {
85 $this->jerr("url returned an empty string");
90 $data = file_get_contents($file);
93 if(preg_match('/^\s<!--\sNOT CONVERT STYLE\s-->\s/', $data)){
97 libxml_use_internal_errors (true);
98 $doc = new DOMDocument('1.0', 'UTF-8');
99 $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
100 $doc->formatOutput = true;
102 $xpath = new DOMXpath($doc);
103 foreach ($xpath->query('//img[@src]') as $img) {
104 $href = $img->getAttribute('src');
105 if (!preg_match("/^http(.*)$/", $href, $matches)) {
107 $img->setAttribute('src', $this->relPath($url, $href));
110 $this->jerr('Please use the absolutely url for image src!');
115 foreach ($xpath->query('//a[@href]') as $a) {
116 $href = $a->getAttribute('href');
117 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
119 $a->setAttribute('href', $this->relPath($url, $href));
122 $this->jerr('Please use the absolutely url for a href!');
126 foreach ($xpath->query('//link[@href]') as $l) {
127 if($l->getAttribute('rel') == 'stylesheet'){
128 $href = $l->getAttribute('href');
130 if(!preg_match("/^http(.*)$/", $href, $matches)){
132 $this->jerr('Please use the absolutely url for link href!');
134 $href = $this->relPath($url, $href);
137 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
141 foreach ($xpath->query('//style') as $s){
142 $this->styleSheets[] = $this->replaceImageUrl($s->nodeValue, $url);
145 $data = $doc->saveHTML();
147 $htmldoc = new HTML_CSS_InlineStyle($data);
148 if(count($this->styleSheets) > 0){
149 foreach ($this->styleSheets as $styleSheet){
150 $htmldoc->applyStylesheet($styleSheet);
153 $html = $htmldoc->getHTML();
154 libxml_use_internal_errors (false);
156 if (!function_exists('tidy_repair_string')) {
157 return "INSTALL TIDY ON SERVER " . $html;
160 $html = tidy_repair_string(
164 'output-xhtml' => TRUE,
175 function replaceImageUrl($stylesheet,$href)
177 $base = explode("/", $href);
178 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
179 foreach($s as $k => $v){
184 array_push($base, $v);
185 $s[$k] = implode("/", $base);
188 $r = implode("url(", $s);
190 $this->checkImportCss($r);
195 function checkImportCss($r)
197 if(preg_match("/@import url/", $r, $matches)){
198 $importCss = explode("@import url", $r);
199 foreach ($importCss as $css){
200 if(preg_match("/\.css/", $css, $matches)){
201 $cssFileName = explode(".css", $css);
202 $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
204 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);