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