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