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)) {
25 $ui = parse_url($base);
27 if (substr($url,0,2) == '//') {
28 return $ui['scheme'] .':' . $url;
33 if (substr($url,0,1) == '/') {
34 return $ui['scheme'] .'://'.$ui['host']. $url;
37 if (substr($ui['path'], -1) == '/') {
38 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . $url;
40 if (!strlen($ui['path'])) {
41 return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
45 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
49 function checkHeader($url)
51 if(strpos($url, 'https') !== false)
53 $this->jerr('accept HTTP url only!');
55 $headers = get_headers($url, 1);
56 if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
58 $this->jerr('accept html file only!');
63 var $styleSheets = array();
65 function convertStyle($url, $file, $is_url = false)
69 // $host = parse_url($url);
70 // require_once 'System.php';
71 // $wget = System::which('wget');
73 // $this->jerr("no wget");
75 // $cmd = $wget . ' -q -O - ' . escapeshellarg($url);
80 // if (!trim(strlen($data))) {
81 // $this->jerr("url returned an empty string");
86 $data = file_get_contents($path);
90 libxml_use_internal_errors (true);
91 $doc = new DOMDocument('1.0', 'UTF-8');
92 $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
93 $doc->formatOutput = true;
95 $xpath = new DOMXpath($doc);
96 // foreach ($xpath->query('//img[@src]') as $img) {
97 // $href = $img->getAttribute('src');
98 // if (!preg_match("/^http(.*)$/", $href, $matches)) {
100 // $img->setAttribute('src', $this->relPath($url, $href));
103 // $this->jerr('Please use the absolutely url for image src!');
108 // foreach ($xpath->query('//a[@href]') as $a) {
109 // $href = $a->getAttribute('href');
110 // if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
112 // $a->setAttribute('href', $this->relPath($url, $href));
115 // $this->jerr('Please use the absolutely url for a href!');
119 // foreach ($xpath->query('//link[@href]') as $l) {
120 // if($l->getAttribute('rel') == 'stylesheet'){
121 // $href = $l->getAttribute('href');
124 // if (empty($url) && !preg_match("/^http(.*)$/", $href, $matches)) {
125 // // import from file , must use absolutely url
126 // $this->jerr('Please use the absolutely url for link href!');
128 // if (!empty($url)) {
129 // // import from URL
130 // $href = $this->relPath($url, $href);
132 // $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
136 foreach ($xpath->query('//style') as $s){
137 $this->styleSheets[] = $this->$this->replaceImageUrl($s->nodeValue, $url);
141 $data = $doc->saveHTML();
143 $htmldoc = new HTML_CSS_InlineStyle($data);
144 if(count($this->styleSheets) > 0){
145 foreach ($this->styleSheets as $styleSheet){
146 $htmldoc->applyStylesheet($styleSheet);
149 $html = $htmldoc->getHTML();
150 libxml_use_internal_errors (false);
152 if (!function_exists('tidy_repair_string')) {
153 return "INSTALL TIDY ON SERVER " . $html;
156 $html = tidy_repair_string(
160 'output-xhtml' => TRUE,
171 function replaceImageUrl($stylesheet,$href)
173 $base = explode("/", $href);
174 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
175 foreach($s as $k => $v){
180 array_push($base, $v);
181 $s[$k] = implode("/", $base);
184 $r = implode("url(", $s);
186 $this->checkImportCss($r);
191 function checkImportCss($r)
193 if(preg_match("/@import url/", $r, $matches)){
194 $importCss = explode("@import url", $r);
195 foreach ($importCss as $css){
196 if(preg_match("/\.css/", $css, $matches)){
197 $cssFileName = explode(".css", $css);
198 $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
200 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);