ConvertStyle.php
[Pman.Core] / ConvertStyle.php
1 <?php
2
3 require_once 'Pman.php';
4 require_once 'HTML/CSS/InlineStyle.php';
5
6 class Pman_Core_ConvertStyle extends Pman 
7 {
8     function getAuth()
9     {
10         if (HTML_FlexyFramework::get()->cli) {
11             return true;
12         }
13         $this->authUser = $this->getAuthUser();
14         if (!$this->authUser) {
15             return false;
16         }
17         return true;
18     }
19     
20     function relPath($base, $url)
21     {  
22         if (preg_match('/^(http|https|mailto):/',$url)) {
23             return $url;
24         }
25         
26         $ui = parse_url($base);
27         
28         if (substr($url,0,2) == '//') {
29             return $ui['scheme'] .':' .  $url;
30         }
31         
32         if (substr($url,0,1) == '/') {
33             return $ui['scheme'] .'://'.$ui['host']. $url;
34         }
35         
36         if (substr($ui['path'], -1) == '/') {
37            return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . $url;
38         }
39         if (!strlen($ui['path'])) {
40             return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
41            
42         }
43         
44         return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
45         
46     }
47     
48     function checkHeader($url)
49     {
50         if(strpos($url, 'https') !== false)
51         {
52             $this->jerr('accept HTTP url only!');
53         }
54         $headers = get_headers($url, 1);
55         if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
56         {
57             $this->jerr('accept html file only!');
58         }
59         return;
60     }
61     
62     var $styleSheets = array();
63     
64     function convertStyle($url, $file, $is_url = true)
65     {
66         if($is_url && !empty($url))
67         {
68             $host = parse_url($url);
69             require_once 'System.php';
70             $wget = System::which('wget');
71             if (!$wget) {
72                 $this->jerr("no wget");
73             }
74             $cmd =  $wget . ' -q -O -  ' . escapeshellarg($url);
75             
76             //echo $cmd; exit;
77             $data = `$cmd`;
78             
79             if (!trim(strlen($data))) {
80                 $this->jerr("url returned an empty string");
81             }
82         }
83         
84         if(!$is_url){
85             $data = file_get_contents($file);
86         }
87         
88         
89         libxml_use_internal_errors (true);
90         $doc = new DOMDocument('1.0', 'UTF-8');
91         $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
92         $doc->formatOutput = true;
93         
94         $xpath = new DOMXpath($doc);
95         foreach ($xpath->query('//img[@src]') as $img) {
96             $href = $img->getAttribute('src');
97             if (!preg_match("/^http(.*)$/", $href, $matches)) {
98                 if(!empty($url)){
99                     $img->setAttribute('src',  $this->relPath($url,  $href));
100                     continue;
101                 }
102                 $this->jerr('Please use the absolutely url for image src!');
103             }
104         }
105         
106         
107         foreach ($xpath->query('//a[@href]') as $a) {
108             $href = $a->getAttribute('href');
109             if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
110                 if(!empty($url)){
111                     $a->setAttribute('href', $this->relPath($url,  $href));
112                     continue;
113                 }
114                 $this->jerr('Please use the absolutely url for a href!');
115             }
116         }
117         
118         foreach ($xpath->query('//link[@href]') as $l) {
119             if($l->getAttribute('rel') == 'stylesheet'){
120                 $href = $l->getAttribute('href');
121                 
122                 if(!preg_match("/^http(.*)$/", $href, $matches)){
123                     if(empty($url)){
124                         $this->jerr('Please use the absolutely url for link href!');
125                     }
126                     $href = $this->relPath($url,  $href);
127                 }
128                 
129                 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
130             }
131         }
132         
133         foreach ($xpath->query('//style') as $s){
134             $this->styleSheets[] = $this->replaceImageUrl($s->nodeValue, $url);
135         }
136         
137         $data = $doc->saveHTML();
138         
139         $htmldoc = new HTML_CSS_InlineStyle($data);
140         if(count($this->styleSheets) > 0){
141             foreach ($this->styleSheets as $styleSheet){
142                 $htmldoc->applyStylesheet($styleSheet);
143             }
144         }
145         $html = $htmldoc->getHTML();
146         libxml_use_internal_errors (false);
147         
148         if (!function_exists('tidy_repair_string')) {
149             return "INSTALL TIDY ON SERVER " . $html;
150         }
151         
152         $html = tidy_repair_string(
153                 $html,
154                 array(
155                   'indent' => TRUE,
156                     'output-xhtml' => TRUE,
157                     'wrap' => 120
158                 ),
159                 'UTF8'
160         );
161         
162         
163         return $html;
164         
165     }
166     
167     function replaceImageUrl($stylesheet,$href)
168     {
169         $base = explode("/", $href);
170         $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
171         foreach($s as $k => $v){
172             if($k == 0){
173                 continue;
174             }
175             array_pop($base);
176             array_push($base, $v);
177             $s[$k] = implode("/", $base);
178         }
179         
180         $r = implode("url(", $s);
181         
182         $this->checkImportCss($r);
183         
184         return $r;
185     }
186     
187     function checkImportCss($r)
188     {
189         if(preg_match("/@import url/", $r, $matches)){
190             $importCss = explode("@import url", $r);
191             foreach ($importCss as $css){
192                 if(preg_match("/\.css/", $css, $matches)){
193                     $cssFileName = explode(".css", $css);
194                     $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
195                     $p = $name . '.css';
196                     $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);
197                 }
198             }
199         }
200         return;
201     }
202     
203 }