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