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