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)) {
26 $ui = parse_url($base);
28 if (substr($url,0,2) == '//') {
29 return $ui['scheme'] .':' . $url;
34 if (substr($url,0,1) == '/') {
35 return $ui['scheme'] .'://'.$ui['host']. $url;
38 if (substr($ui['path'], -1) == '/') {
39 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . $url;
41 if (!strlen($ui['path'])) {
42 return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
46 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
50 function checkHeader($url)
52 if(strpos($url, 'https') !== false)
54 $this->jerr('accept HTTP url only!');
56 $headers = get_headers($url, 1);
57 if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
59 $this->jerr('accept html file only!');
64 var $styleSheets = array();
66 function convertStyle($url, $file, $is_url = false)
70 // $host = parse_url($url);
71 // require_once 'System.php';
72 // $wget = System::which('wget');
74 // $this->jerr("no wget");
76 // $cmd = $wget . ' -q -O - ' . escapeshellarg($url);
81 // if (!trim(strlen($data))) {
82 // $this->jerr("url returned an empty string");
87 $data = file_get_contents($file);
91 libxml_use_internal_errors (true);
92 $doc = new DOMDocument('1.0', 'UTF-8');
93 $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
94 $doc->formatOutput = true;
96 $xpath = new DOMXpath($doc);
97 foreach ($xpath->query('//img[@src]') as $img) {
98 $href = $img->getAttribute('src');
99 if (!preg_match("/^http(.*)$/", $href, $matches)) {
101 $img->setAttribute('src', $this->relPath($url, $href));
104 $this->jerr('Please use the absolutely url for image src!');
109 foreach ($xpath->query('//a[@href]') as $a) {
110 $href = $a->getAttribute('href');
111 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
113 $a->setAttribute('href', $this->relPath($url, $href));
116 $this->jerr('Please use the absolutely url for a href!');
120 foreach ($xpath->query('//link[@href]') as $l) {
121 if($l->getAttribute('rel') == 'stylesheet'){
122 $href = $l->getAttribute('href');
124 if(!preg_match("/^http(.*)$/", $href, $matches)){
126 $href = $this->relPath($url, $href);
128 $this->jerr('Please use the absolutely url for link href!');
131 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
135 foreach ($xpath->query('//style') as $s){
136 $this->styleSheets[] = $this->replaceImageUrl($s->nodeValue, $url);
139 $data = $doc->saveHTML();
141 $htmldoc = new HTML_CSS_InlineStyle($data);
142 if(count($this->styleSheets) > 0){
143 foreach ($this->styleSheets as $styleSheet){
144 $htmldoc->applyStylesheet($styleSheet);
147 $html = $htmldoc->getHTML();
148 libxml_use_internal_errors (false);
150 if (!function_exists('tidy_repair_string')) {
151 return "INSTALL TIDY ON SERVER " . $html;
154 $html = tidy_repair_string(
158 'output-xhtml' => TRUE,
169 function replaceImageUrl($stylesheet,$href)
171 $base = explode("/", $href);
172 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
173 foreach($s as $k => $v){
178 array_push($base, $v);
179 $s[$k] = implode("/", $base);
182 $r = implode("url(", $s);
184 $this->checkImportCss($r);
189 function checkImportCss($r)
191 if(preg_match("/@import url/", $r, $matches)){
192 $importCss = explode("@import url", $r);
193 foreach ($importCss as $css){
194 if(preg_match("/\.css/", $css, $matches)){
195 $cssFileName = explode(".css", $css);
196 $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
198 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);