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