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         if($is_url && !empty($url))
72         {
73             $host = parse_url($url);
74             require_once 'System.php';
75             $wget = System::which('wget');
76             if (!$wget) {
77                 $this->jerr("no wget");
78             }
79             $cmd =  $wget . ' -q -O -  ' . escapeshellarg($url);
80             
81             //echo $cmd; exit;
82             $data = `$cmd`;
83             
84             if (!trim(strlen($data))) {
85                 $this->jerr("url returned an empty string");
86             }
87         }
88         
89         if(!$is_url){
90             $data = file_get_contents($file);
91         }
92         
93         
94         libxml_use_internal_errors (true);
95         $doc = new DOMDocument('1.0', 'UTF-8');
96         $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
97         $doc->formatOutput = true;
98         
99         $xpath = new DOMXpath($doc);
100         foreach ($xpath->query('//img[@src]') as $img) {
101             $href = $img->getAttribute('src');
102             if (!preg_match("/^http(.*)$/", $href, $matches)) {
103                 if(!empty($url)){
104                     $img->setAttribute('src',  $this->relPath($url,  $href));
105                     continue;
106                 }
107                 $this->jerr('Please use the absolutely url for image src!');
108             }
109         }
110         
111         
112         foreach ($xpath->query('//a[@href]') as $a) {
113             $href = $a->getAttribute('href');
114             if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
115                 if(!empty($url)){
116                     $a->setAttribute('href', $this->relPath($url,  $href));
117                     continue;
118                 }
119                 $this->jerr('Please use the absolutely url for a href!');
120             }
121         }
122         
123         foreach ($xpath->query('//link[@href]') as $l) {
124             if($l->getAttribute('rel') == 'stylesheet'){
125                 $href = $l->getAttribute('href');
126                 
127                 if(!preg_match("/^http(.*)$/", $href, $matches)){
128                     if(empty($url)){
129                         $this->jerr('Please use the absolutely url for link href!');
130                     }
131                     $href = $this->relPath($url,  $href);
132                 }
133                 
134                 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
135             }
136         }
137         
138         foreach ($xpath->query('//style') as $s){
139             $this->styleSheets[] = $this->replaceImageUrl($s->nodeValue, $url);
140         }
141         
142         $data = $doc->saveHTML();
143         
144         $htmldoc = new HTML_CSS_InlineStyle($data);
145         if(count($this->styleSheets) > 0){
146             foreach ($this->styleSheets as $styleSheet){
147                 $htmldoc->applyStylesheet($styleSheet);
148             }
149         }
150         $html = $htmldoc->getHTML();
151         libxml_use_internal_errors (false);
152         
153         if (!function_exists('tidy_repair_string')) {
154             return "INSTALL TIDY ON SERVER " . $html;
155         }
156         
157         $html = tidy_repair_string(
158                 $html,
159                 array(
160                   'indent' => TRUE,
161                     'output-xhtml' => TRUE,
162                     'wrap' => 120
163                 ),
164                 'UTF8'
165         );
166         
167         
168         return $html;
169         
170     }
171     
172     function replaceImageUrl($stylesheet,$href)
173     {
174         $base = explode("/", $href);
175         $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
176         foreach($s as $k => $v){
177             if($k == 0){
178                 continue;
179             }
180             array_pop($base);
181             array_push($base, $v);
182             $s[$k] = implode("/", $base);
183         }
184         
185         $r = implode("url(", $s);
186         
187         $this->checkImportCss($r);
188         
189         return $r;
190     }
191     
192     function checkImportCss($r)
193     {
194         if(preg_match("/@import url/", $r, $matches)){
195             $importCss = explode("@import url", $r);
196             foreach ($importCss as $css){
197                 if(preg_match("/\.css/", $css, $matches)){
198                     $cssFileName = explode(".css", $css);
199                     $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
200                     $p = $name . '.css';
201                     $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);
202                 }
203             }
204         }
205         return;
206     }
207     
208 }