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)
23 //var_dump(array($base,$url));
24 if (preg_match('/^(http|https|mailto):/',$url)) {
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;
36 if (substr($url,0,1) == '/') {
37 return $ui['scheme'] .'://'.$ui['host']. $url;
40 if (substr($ui['path'], -1) == '/') {
41 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . $url;
43 if (!strlen($ui['path'])) {
44 return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
47 /// not sure if this will work...
48 return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
56 if(isset($_REQUEST['_convertToPlain']))
58 require_once 'System.php';
59 $tmpdir = System::mktemp("-d convertPlain");
60 $path = $tmpdir . '/' . time() . '.html';
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');
70 if(!preg_match('/^#unsubscribe/', $href)){
73 $a->parentNode->replaceChild($doc->createTextNode($a->nodeValue . ' {unsubscribe_link}'), $a);
76 $_REQUEST['bodytext'] = $doc->saveHTML();
77 libxml_use_internal_errors (false);
80 if(!file_exists($path)){
81 file_put_contents($path, $_REQUEST['bodytext']);
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));
89 if(isset($_REQUEST['importUrl']))
91 // $host = parse_url($_REQUEST['importUrl']);
92 // if($host['host'] != 'localhost' && $host['host'] != 'roojs-edward.com' && $host['host'] != $_SERVER['HTTP_HOST'])
94 // $this->jerr('Invalid URL!');
96 $this->checkHeader($_REQUEST['importUrl']);
97 $data = $this->convertStyle($_REQUEST['importUrl'], '');
98 // print_r($data);exit;
104 $htmlFile = DB_DataObject::factory('images');
105 $htmlFile->setFrom(array(
107 'ontable' =>'crm_mailing_list_message'
109 $htmlFile->onUpload(false);
110 // print_r($htmlFile);
111 if($htmlFile->mimetype != 'text/html')
113 $this->jerr('accept html file only!');
115 if(!file_exists($htmlFile->getStoreName()))
117 $this->jerr('update failed!');
120 $data = $this->convertStyle('', $htmlFile->getStoreName());
123 unlink($htmlFile->getStoreName()) or die('Unable to delete the file');
128 function checkHeader($url)
130 if(strpos($url, 'https') !== false)
132 $this->jerr('accept HTTP url only!');
134 $headers = get_headers($url, 1);
135 if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
137 $this->jerr('accept html file only!');
142 var $styleSheets = array();
144 function convertStyle($url, $file)
148 $host = parse_url($url);
149 require_once 'System.php';
150 $wget = System::which('wget');
152 $this->jerr("no wget");
154 $cmd = $wget . ' -q -O - ' . escapeshellarg($url);
159 if (!trim(strlen($data))) {
160 $this->jerr("url returned an empty string");
162 // $this->jerr($url);
163 /*require_once 'HTTP/Request.php';
164 $a = new HTTP_Request($url, array(
165 'allowRedirects' => true,
167 'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
170 // if this results in an errorr or redirect..
171 // we should log that somewhere.. and display it on the feed...
173 $data = $a->getResponseBody();
176 //$this->jerr($data);
178 // $data = file_get_contents($url);
180 if(file_exists($file))
182 $data = file_get_contents($file);
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;
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)) {
197 $img->setAttribute('src', $this->relPath($url, $href));
200 $this->jerr('Please use the absolutely url for image src!');
205 foreach ($xpath->query('//a[@href]') as $a) {
206 $href = $a->getAttribute('href');
207 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
209 $a->setAttribute('href', $this->relPath($url, $href));
212 $this->jerr('Please use the absolutely url for a href!');
216 foreach ($xpath->query('//link[@href]') as $l) {
217 if($l->getAttribute('rel') == 'stylesheet'){
218 $href = $l->getAttribute('href');
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!');
227 $href = $this->relPath($url, $href);
229 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
232 $data = $doc->saveHTML();
234 $htmldoc = new HTML_CSS_InlineStyle($data);
235 if(count($this->styleSheets) > 0){
236 foreach ($this->styleSheets as $styleSheet){
237 $htmldoc->applyStylesheet($styleSheet);
240 $html = $htmldoc->getHTML();
241 libxml_use_internal_errors (false);
243 if (!function_exists('tidy_repair_string')) {
244 return "INSTALL TIDY ON SERVER " . $html;
247 // finally clean it up... using tidy...
250 $html = tidy_repair_string(
254 'output-xhtml' => TRUE,
265 function replaceImageUrl($stylesheet,$href)
267 $base = explode("/", $href);
268 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
269 foreach($s as $k => $v){
274 array_push($base, $v);
275 $s[$k] = implode("/", $base);
278 $r = implode("url(", $s);
280 $this->checkImportCss($r);
285 function checkImportCss($r)
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]);
294 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);