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;
55 if(isset($_REQUEST['importUrl']))
57 $this->checkHeader($_REQUEST['importUrl']);
58 $data = $this->convertStyle($_REQUEST['importUrl'], '');
63 $htmlFile = DB_DataObject::factory('images');
64 $htmlFile->setFrom(array(
66 'ontable' =>'crm_mailing_list_message'
68 $htmlFile->onUpload(false);
69 // print_r($htmlFile);
70 if($htmlFile->mimetype != 'text/html')
72 $this->jerr('accept html file only!');
74 if(!file_exists($htmlFile->getStoreName()))
76 $this->jerr('update failed!');
79 $data = $this->convertStyle('', $htmlFile->getStoreName());
82 unlink($htmlFile->getStoreName()) or die('Unable to delete the file');
87 function checkHeader($url)
89 if(strpos($url, 'https') !== false)
91 $this->jerr('accept HTTP url only!');
93 $headers = get_headers($url, 1);
94 if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
96 $this->jerr('accept html file only!');
101 var $styleSheets = array();
103 function convertStyle($url, $file)
107 $host = parse_url($url);
108 require_once 'System.php';
109 $wget = System::which('wget');
111 $this->jerr("no wget");
113 $cmd = $wget . ' -q -O - ' . escapeshellarg($url);
118 if (!trim(strlen($data))) {
119 $this->jerr("url returned an empty string");
121 // $this->jerr($url);
122 /*require_once 'HTTP/Request.php';
123 $a = new HTTP_Request($url, array(
124 'allowRedirects' => true,
126 'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
129 // if this results in an errorr or redirect..
130 // we should log that somewhere.. and display it on the feed...
132 $data = $a->getResponseBody();
135 //$this->jerr($data);
137 // $data = file_get_contents($url);
139 if(file_exists($file))
141 $data = file_get_contents($file);
144 libxml_use_internal_errors (true);
145 $doc = new DOMDocument('1.0', 'UTF-8');
146 $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
147 $doc->formatOutput = true;
151 $xpath = new DOMXpath($doc);
152 foreach ($xpath->query('//img[@src]') as $img) {
153 $href = $img->getAttribute('src');
154 if (!preg_match("/^http(.*)$/", $href, $matches)) {
156 $img->setAttribute('src', $this->relPath($url, $href));
159 $this->jerr('Please use the absolutely url for image src!');
164 foreach ($xpath->query('//a[@href]') as $a) {
165 $href = $a->getAttribute('href');
166 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
168 $a->setAttribute('href', $this->relPath($url, $href));
171 $this->jerr('Please use the absolutely url for a href!');
175 foreach ($xpath->query('//link[@href]') as $l) {
176 if($l->getAttribute('rel') == 'stylesheet'){
177 $href = $l->getAttribute('href');
180 if (empty($url) && !preg_match("/^http(.*)$/", $href, $matches)) {
181 // import from file , must use absolutely url
182 $this->jerr('Please use the absolutely url for link href!');
186 $href = $this->relPath($url, $href);
188 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
191 $data = $doc->saveHTML();
193 $htmldoc = new HTML_CSS_InlineStyle($data);
194 if(count($this->styleSheets) > 0){
195 foreach ($this->styleSheets as $styleSheet){
196 $htmldoc->applyStylesheet($styleSheet);
199 $html = $htmldoc->getHTML();
200 libxml_use_internal_errors (false);
202 if (!function_exists('tidy_repair_string')) {
203 return "INSTALL TIDY ON SERVER " . $html;
206 // finally clean it up... using tidy...
209 $html = tidy_repair_string(
213 'output-xhtml' => TRUE,
224 function replaceImageUrl($stylesheet,$href)
226 $base = explode("/", $href);
227 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
228 foreach($s as $k => $v){
233 array_push($base, $v);
234 $s[$k] = implode("/", $base);
237 $r = implode("url(", $s);
239 $this->checkImportCss($r);
244 function checkImportCss($r)
246 if(preg_match("/@import url/", $r, $matches)){
247 $importCss = explode("@import url", $r);
248 foreach ($importCss as $css){
249 if(preg_match("/\.css/", $css, $matches)){
250 $cssFileName = explode(".css", $css);
251 $name = preg_replace("/[\(\'\"]/", '', $cssFileName[0]);
253 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);