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         $ui = parse_url($base);
26         
27         if (substr($url,0,2) == '//') {
28             return $ui['scheme'] .':' .  $url;
29         }
30         
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)
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            // $this->jerr($url);
84             /*require_once 'HTTP/Request.php';
85             $a = new HTTP_Request($url, array(
86                     'allowRedirects' => true,
87                     'maxRedirects' => 2, 
88                     'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
89                     ));
90             $a->sendRequest();
91             // if this results in an errorr or redirect..
92             // we should log that somewhere.. and display it on the feed...
93             
94             $data =  $a->getResponseBody();
95             */
96             
97             //$this->jerr($data);
98             
99         //    $data = file_get_contents($url);
100         }
101         if(file_exists($file))
102         {
103             $data = file_get_contents($file);
104         }
105         
106         libxml_use_internal_errors (true);
107         $doc = new DOMDocument('1.0', 'UTF-8');
108         $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
109         $doc->formatOutput = true;
110
111       
112         
113         $xpath = new DOMXpath($doc);
114         foreach ($xpath->query('//img[@src]') as $img) {
115             $href = $img->getAttribute('src');
116             if (!preg_match("/^http(.*)$/", $href, $matches)) {
117                 if(!empty($url)){
118                     $img->setAttribute('src',  $this->relPath($url,  $href));
119                     continue;
120                 }
121                 $this->jerr('Please use the absolutely url for image src!');
122             }
123         }
124         
125         
126         foreach ($xpath->query('//a[@href]') as $a) {
127             $href = $a->getAttribute('href');
128             if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
129                 if(!empty($url)){
130                     $a->setAttribute('href', $this->relPath($url,  $href));
131                     continue;
132                 }
133                 $this->jerr('Please use the absolutely url for a href!');
134             }
135         }
136         
137         foreach ($xpath->query('//link[@href]') as $l) {
138             if($l->getAttribute('rel') == 'stylesheet'){
139                 $href = $l->getAttribute('href');
140                 
141                 
142                 if (empty($url) && !preg_match("/^http(.*)$/", $href, $matches)) {
143                     // import from file , must use absolutely url
144                     $this->jerr('Please use the absolutely url for link href!');
145                 }
146                 if (!empty($url)) {
147                     // import from URL
148                     $href = $this->relPath($url,  $href);
149                 }
150                 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
151             }
152         }
153         $data = $doc->saveHTML();
154         
155         $htmldoc = new HTML_CSS_InlineStyle($data);
156         if(count($this->styleSheets) > 0){
157             foreach ($this->styleSheets as $styleSheet){
158                 $htmldoc->applyStylesheet($styleSheet);
159             }
160         }
161         $html = $htmldoc->getHTML();
162         libxml_use_internal_errors (false);
163         
164         if (!function_exists('tidy_repair_string')) {
165             return "INSTALL TIDY ON SERVER " . $html;
166         }
167         
168         // finally clean it up... using tidy...
169        
170  
171         $html = tidy_repair_string(
172                 $html,
173                 array(
174                   'indent' => TRUE,
175                     'output-xhtml' => TRUE,
176                     'wrap' => 120
177                 ),
178                 'UTF8'
179         );
180         
181         
182         return $html;
183         
184     }
185     
186     function replaceImageUrl($stylesheet,$href)
187     {
188         $base = explode("/", $href);
189         $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
190         foreach($s as $k => $v){
191             if($k == 0){
192                 continue;
193             }
194             array_pop($base);
195             array_push($base, $v);
196             $s[$k] = implode("/", $base);
197         }
198         
199         $r = implode("url(", $s);
200         
201         $this->checkImportCss($r);
202         
203         return $r;
204     }
205     
206     function checkImportCss($r)
207     {
208         if(preg_match("/@import url/", $r, $matches)){
209             $importCss = explode("@import url", $r);
210             foreach ($importCss as $css){
211                 if(preg_match("/\.css/", $css, $matches)){
212                     $cssFileName = explode(".css", $css);
213                     $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
214                     $p = $name . '.css';
215                     $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);
216                 }
217             }
218         }
219         return;
220     }
221     
222 }