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);
26 // if it starts with '/'...
27 // we do not handle ports...
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;
45 /// not sure if this will work...
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)
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");
85 /*require_once 'HTTP/Request.php';
86 $a = new HTTP_Request($url, array(
87 'allowRedirects' => true,
89 'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
92 // if this results in an errorr or redirect..
93 // we should log that somewhere.. and display it on the feed...
95 $data = $a->getResponseBody();
100 // $data = file_get_contents($url);
102 if(file_exists($file))
104 $data = file_get_contents($file);
107 libxml_use_internal_errors (true);
108 $doc = new DOMDocument('1.0', 'UTF-8');
109 $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
110 $doc->formatOutput = true;
114 $xpath = new DOMXpath($doc);
115 foreach ($xpath->query('//img[@src]') as $img) {
116 $href = $img->getAttribute('src');
117 if (!preg_match("/^http(.*)$/", $href, $matches)) {
119 $img->setAttribute('src', $this->relPath($url, $href));
122 $this->jerr('Please use the absolutely url for image src!');
127 foreach ($xpath->query('//a[@href]') as $a) {
128 $href = $a->getAttribute('href');
129 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
131 $a->setAttribute('href', $this->relPath($url, $href));
134 $this->jerr('Please use the absolutely url for a href!');
138 foreach ($xpath->query('//link[@href]') as $l) {
139 if($l->getAttribute('rel') == 'stylesheet'){
140 $href = $l->getAttribute('href');
143 if (empty($url) && !preg_match("/^http(.*)$/", $href, $matches)) {
144 // import from file , must use absolutely url
145 $this->jerr('Please use the absolutely url for link href!');
149 $href = $this->relPath($url, $href);
151 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
154 $data = $doc->saveHTML();
156 $htmldoc = new HTML_CSS_InlineStyle($data);
157 if(count($this->styleSheets) > 0){
158 foreach ($this->styleSheets as $styleSheet){
159 $htmldoc->applyStylesheet($styleSheet);
162 $html = $htmldoc->getHTML();
163 libxml_use_internal_errors (false);
165 if (!function_exists('tidy_repair_string')) {
166 return "INSTALL TIDY ON SERVER " . $html;
169 // finally clean it up... using tidy...
172 $html = tidy_repair_string(
176 'output-xhtml' => TRUE,
187 function replaceImageUrl($stylesheet,$href)
189 $base = explode("/", $href);
190 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
191 foreach($s as $k => $v){
196 array_push($base, $v);
197 $s[$k] = implode("/", $base);
200 $r = implode("url(", $s);
202 $this->checkImportCss($r);
207 function checkImportCss($r)
209 if(preg_match("/@import url/", $r, $matches)){
210 $importCss = explode("@import url", $r);
211 foreach ($importCss as $css){
212 if(preg_match("/\.css/", $css, $matches)){
213 $cssFileName = explode(".css", $css);
214 $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
216 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);