3 require_once 'Pman.php';
4 require_once 'HTML/CSS/InlineStyle.php';
6 class Pman_Core_ConvertStyle extends Pman
10 if (HTML_FlexyFramework::get()->cli) {
13 $this->authUser = $this->getAuthUser();
14 if (!$this->authUser) {
20 function relPath($base, $url)
22 //var_dump(array($base,$url));
23 if (preg_match('/^(http|https|mailto):/',$url)) {
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;
35 if (substr($url,0,1) == '/') {
36 return $ui['scheme'] .'://'.$ui['host']. $url;
39 if (substr($ui['path'], -1) == '/') {
40 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . $url;
42 if (!strlen($ui['path'])) {
43 return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
46 /// not sure if this will work...
47 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
56 if(isset($_REQUEST['importUrl']))
58 $this->checkHeader($_REQUEST['importUrl']);
59 $data = $this->convertStyle($_REQUEST['importUrl'], '');
64 $htmlFile = DB_DataObject::factory('images');
65 $htmlFile->setFrom(array(
67 'ontable' =>'crm_mailing_list_message'
69 $htmlFile->onUpload(false);
70 // print_r($htmlFile);
71 if($htmlFile->mimetype != 'text/html')
73 $this->jerr('accept html file only!');
75 if(!file_exists($htmlFile->getStoreName()))
77 $this->jerr('update failed!');
80 $data = $this->convertStyle('', $htmlFile->getStoreName());
83 unlink($htmlFile->getStoreName()) or die('Unable to delete the file');
88 function checkHeader($url)
90 if(strpos($url, 'https') !== false)
92 $this->jerr('accept HTTP url only!');
94 $headers = get_headers($url, 1);
95 if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
97 $this->jerr('accept html file only!');
102 var $styleSheets = array();
104 function convertStyle($url, $file)
108 $host = parse_url($url);
109 require_once 'System.php';
110 $wget = System::which('wget');
112 $this->jerr("no wget");
114 $cmd = $wget . ' -q -O - ' . escapeshellarg($url);
119 if (!trim(strlen($data))) {
120 $this->jerr("url returned an empty string");
122 // $this->jerr($url);
123 /*require_once 'HTTP/Request.php';
124 $a = new HTTP_Request($url, array(
125 'allowRedirects' => true,
127 'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
130 // if this results in an errorr or redirect..
131 // we should log that somewhere.. and display it on the feed...
133 $data = $a->getResponseBody();
136 //$this->jerr($data);
138 // $data = file_get_contents($url);
140 if(file_exists($file))
142 $data = file_get_contents($file);
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;
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)) {
157 $img->setAttribute('src', $this->relPath($url, $href));
160 $this->jerr('Please use the absolutely url for image src!');
165 foreach ($xpath->query('//a[@href]') as $a) {
166 $href = $a->getAttribute('href');
167 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
169 $a->setAttribute('href', $this->relPath($url, $href));
172 $this->jerr('Please use the absolutely url for a href!');
176 foreach ($xpath->query('//link[@href]') as $l) {
177 if($l->getAttribute('rel') == 'stylesheet'){
178 $href = $l->getAttribute('href');
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!');
187 $href = $this->relPath($url, $href);
189 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
192 $data = $doc->saveHTML();
194 $htmldoc = new HTML_CSS_InlineStyle($data);
195 if(count($this->styleSheets) > 0){
196 foreach ($this->styleSheets as $styleSheet){
197 $htmldoc->applyStylesheet($styleSheet);
200 $html = $htmldoc->getHTML();
201 libxml_use_internal_errors (false);
203 if (!function_exists('tidy_repair_string')) {
204 return "INSTALL TIDY ON SERVER " . $html;
207 // finally clean it up... using tidy...
210 $html = tidy_repair_string(
214 'output-xhtml' => TRUE,
225 function replaceImageUrl($stylesheet,$href)
227 $base = explode("/", $href);
228 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
229 foreach($s as $k => $v){
234 array_push($base, $v);
235 $s[$k] = implode("/", $base);
238 $r = implode("url(", $s);
240 $this->checkImportCss($r);
245 function checkImportCss($r)
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]);
254 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);