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)
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");
84 /*require_once 'HTTP/Request.php';
85 $a = new HTTP_Request($url, array(
86 'allowRedirects' => true,
88 'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
91 // if this results in an errorr or redirect..
92 // we should log that somewhere.. and display it on the feed...
94 $data = $a->getResponseBody();
99 // $data = file_get_contents($url);
101 if(file_exists($file))
103 $data = file_get_contents($file);
106 libxml_use_internal_errors (true);
107 $doc = new DOMDocument('1.0', 'UTF-8');
108 $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
109 $doc->formatOutput = true;
113 $xpath = new DOMXpath($doc);
114 foreach ($xpath->query('//img[@src]') as $img) {
115 $href = $img->getAttribute('src');
116 if (!preg_match("/^http(.*)$/", $href, $matches)) {
118 $img->setAttribute('src', $this->relPath($url, $href));
121 $this->jerr('Please use the absolutely url for image src!');
126 foreach ($xpath->query('//a[@href]') as $a) {
127 $href = $a->getAttribute('href');
128 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
130 $a->setAttribute('href', $this->relPath($url, $href));
133 $this->jerr('Please use the absolutely url for a href!');
137 foreach ($xpath->query('//link[@href]') as $l) {
138 if($l->getAttribute('rel') == 'stylesheet'){
139 $href = $l->getAttribute('href');
142 if (empty($url) && !preg_match("/^http(.*)$/", $href, $matches)) {
143 // import from file , must use absolutely url
144 $this->jerr('Please use the absolutely url for link href!');
148 $href = $this->relPath($url, $href);
150 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
153 $data = $doc->saveHTML();
155 $htmldoc = new HTML_CSS_InlineStyle($data);
156 if(count($this->styleSheets) > 0){
157 foreach ($this->styleSheets as $styleSheet){
158 $htmldoc->applyStylesheet($styleSheet);
161 $html = $htmldoc->getHTML();
162 libxml_use_internal_errors (false);
164 if (!function_exists('tidy_repair_string')) {
165 return "INSTALL TIDY ON SERVER " . $html;
168 // finally clean it up... using tidy...
171 $html = tidy_repair_string(
175 'output-xhtml' => TRUE,
186 function replaceImageUrl($stylesheet,$href)
188 $base = explode("/", $href);
189 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
190 foreach($s as $k => $v){
195 array_push($base, $v);
196 $s[$k] = implode("/", $base);
199 $r = implode("url(", $s);
201 $this->checkImportCss($r);
206 function checkImportCss($r)
208 if(preg_match("/@import url/", $r, $matches)){
209 $importCss = explode("@import url", $r);
210 foreach ($importCss as $css){
211 if(preg_match("/\.css/", $css, $matches)){
212 $cssFileName = explode(".css", $css);
213 $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
215 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);