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