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         //var_dump(array($base,$url));        
23         if (preg_match('/^(http|https|mailto):/',$url)) {
24             return $url;
25         }
26         $ui = parse_url($base);
27         // if it starts with '/'...
28         // we do not handle ports...
29         if (substr($url,0,2) == '//') {
30             return $ui['scheme'] .':' .  $url;
31         }
32         
33         
34         
35         if (substr($url,0,1) == '/') {
36             return $ui['scheme'] .'://'.$ui['host']. $url;
37         }
38         
39         if (substr($ui['path'], -1) == '/') {
40            return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . $url;
41         }
42         if (!strlen($ui['path'])) {
43             return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
44            
45         }
46         /// not sure if this will work...
47         return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
48         
49     }
50     
51     
52     function post()
53     {
54         
55         
56         if(isset($_REQUEST['importUrl']))
57         {
58             $this->checkHeader($_REQUEST['importUrl']);
59             $data = $this->convertStyle($_REQUEST['importUrl'], '');
60             $this->jok($data);
61         }
62      
63         // Import from file
64         $htmlFile = DB_DataObject::factory('images');
65         $htmlFile->setFrom(array(
66                'onid' => 0,
67                'ontable' =>'crm_mailing_list_message'
68         ));
69         $htmlFile->onUpload(false);
70        // print_r($htmlFile);
71         if($htmlFile->mimetype != 'text/html')
72         {
73             $this->jerr('accept html file only!');
74         }
75         if(!file_exists($htmlFile->getStoreName()))
76         {
77             $this->jerr('update failed!');
78         }
79         
80         $data = $this->convertStyle('', $htmlFile->getStoreName());
81         
82         $htmlFile->delete();
83         unlink($htmlFile->getStoreName()) or die('Unable to delete the file');
84         
85         $this->jok($data);
86     }
87     
88     function checkHeader($url)
89     {
90         if(strpos($url, 'https') !== false)
91         {
92             $this->jerr('accept HTTP url only!');
93         }
94         $headers = get_headers($url, 1);
95         if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
96         {
97             $this->jerr('accept html file only!');
98         }
99         return;
100     }
101     
102     var $styleSheets = array();
103     
104     function convertStyle($url, $file)
105     {
106         if(!empty($url))
107         {
108             $host = parse_url($url);
109             require_once 'System.php';
110             $wget = System::which('wget');
111             if (!$wget) {
112                 $this->jerr("no wget");
113             }
114             $cmd =  $wget . ' -q -O -  ' . escapeshellarg($url);
115             
116             //echo $cmd; exit;
117             $data = `$cmd`;
118             
119             if (!trim(strlen($data))) {
120                 $this->jerr("url returned an empty string");
121             }
122            // $this->jerr($url);
123             /*require_once 'HTTP/Request.php';
124             $a = new HTTP_Request($url, array(
125                     'allowRedirects' => true,
126                     'maxRedirects' => 2, 
127                     'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
128                     ));
129             $a->sendRequest();
130             // if this results in an errorr or redirect..
131             // we should log that somewhere.. and display it on the feed...
132             
133             $data =  $a->getResponseBody();
134             */
135             
136             //$this->jerr($data);
137             
138         //    $data = file_get_contents($url);
139         }
140         if(file_exists($file))
141         {
142             $data = file_get_contents($file);
143         }
144         
145         libxml_use_internal_errors (true);
146         $doc = new DOMDocument('1.0', 'UTF-8');
147         $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
148         $doc->formatOutput = true;
149
150       
151         
152         $xpath = new DOMXpath($doc);
153         foreach ($xpath->query('//img[@src]') as $img) {
154             $href = $img->getAttribute('src');
155             if (!preg_match("/^http(.*)$/", $href, $matches)) {
156                 if(!empty($url)){
157                     $img->setAttribute('src',  $this->relPath($url,  $href));
158                     continue;
159                 }
160                 $this->jerr('Please use the absolutely url for image src!');
161             }
162         }
163         
164         
165         foreach ($xpath->query('//a[@href]') as $a) {
166             $href = $a->getAttribute('href');
167             if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
168                 if(!empty($url)){
169                     $a->setAttribute('href', $this->relPath($url,  $href));
170                     continue;
171                 }
172                 $this->jerr('Please use the absolutely url for a href!');
173             }
174         }
175         
176         foreach ($xpath->query('//link[@href]') as $l) {
177             if($l->getAttribute('rel') == 'stylesheet'){
178                 $href = $l->getAttribute('href');
179                 
180                 
181                 if (empty($url) && !preg_match("/^http(.*)$/", $href, $matches)) {
182                     // import from file , must use absolutely url
183                     $this->jerr('Please use the absolutely url for link href!');
184                 }
185                 if (!empty($url)) {
186                     // import from URL
187                     $href = $this->relPath($url,  $href);
188                 }
189                 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
190             }
191         }
192         $data = $doc->saveHTML();
193         
194         $htmldoc = new HTML_CSS_InlineStyle($data);
195         if(count($this->styleSheets) > 0){
196             foreach ($this->styleSheets as $styleSheet){
197                 $htmldoc->applyStylesheet($styleSheet);
198             }
199         }
200         $html = $htmldoc->getHTML();
201         libxml_use_internal_errors (false);
202         
203         if (!function_exists('tidy_repair_string')) {
204             return "INSTALL TIDY ON SERVER " . $html;
205         }
206         
207         // finally clean it up... using tidy...
208        
209  
210         $html = tidy_repair_string(
211                 $html,
212                 array(
213                   'indent' => TRUE,
214                     'output-xhtml' => TRUE,
215                     'wrap' => 120
216                 ),
217                 'UTF8'
218         );
219         
220         
221         return $html;
222         
223     }
224     
225     function replaceImageUrl($stylesheet,$href)
226     {
227         $base = explode("/", $href);
228         $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
229         foreach($s as $k => $v){
230             if($k == 0){
231                 continue;
232             }
233             array_pop($base);
234             array_push($base, $v);
235             $s[$k] = implode("/", $base);
236         }
237         
238         $r = implode("url(", $s);
239         
240         $this->checkImportCss($r);
241         
242         return $r;
243     }
244     
245     function checkImportCss($r)
246     {
247         if(preg_match("/@import url/", $r, $matches)){
248             $importCss = explode("@import url", $r);
249             foreach ($importCss as $css){
250                 if(preg_match("/\.css/", $css, $matches)){
251                     $cssFileName = explode(".css", $css);
252                     $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
253                     $p = $name . '.css';
254                     $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);
255                 }
256             }
257         }
258         return;
259     }
260     
261 }