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['_convertToPlain']))
57 require_once 'System.php';
58 $tmpdir = System::mktemp("-d convertPlain");
59 $path = $tmpdir . '/' . time() . '.html';
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');
69 if(!preg_match('/^#unsubscribe/', $href)){
72 $a->parentNode->replaceChild($doc->createTextNode($a->nodeValue . ' {unsubscribe_link}'), $a);
75 $_REQUEST['bodytext'] = $doc->saveHTML();
76 libxml_use_internal_errors (false);
79 if(!file_exists($path)){
80 file_put_contents($path, $_REQUEST['bodytext']);
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));
88 if(isset($_REQUEST['importUrl']))
90 // $host = parse_url($_REQUEST['importUrl']);
91 // if($host['host'] != 'localhost' && $host['host'] != 'roojs-edward.com' && $host['host'] != $_SERVER['HTTP_HOST'])
93 // $this->jerr('Invalid URL!');
95 $this->checkHeader($_REQUEST['importUrl']);
96 $data = $this->convertStyle($_REQUEST['importUrl'], '');
97 // print_r($data);exit;
103 $htmlFile = DB_DataObject::factory('images');
104 $htmlFile->setFrom(array(
106 'ontable' =>'crm_mailing_list_message'
108 $htmlFile->onUpload(false);
109 // print_r($htmlFile);
110 if($htmlFile->mimetype != 'text/html')
112 $this->jerr('accept html file only!');
114 if(!file_exists($htmlFile->getStoreName()))
116 $this->jerr('update failed!');
119 $data = $this->convertStyle('', $htmlFile->getStoreName());
122 unlink($htmlFile->getStoreName()) or die('Unable to delete the file');
127 function checkHeader($url)
129 if(strpos($url, 'https') !== false)
131 $this->jerr('accept HTTP url only!');
133 $headers = get_headers($url, 1);
134 if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
136 $this->jerr('accept html file only!');
141 var $styleSheets = array();
143 function convertStyle($url, $file)
147 $host = parse_url($url);
148 require_once 'System.php';
149 $wget = System::which('wget');
151 $this->jerr("no wget");
153 $cmd = $wget . ' -q -O - ' . escapeshellarg($url);
158 if (!trim(strlen($data))) {
159 $this->jerr("url returned an empty string");
161 // $this->jerr($url);
162 /*require_once 'HTTP/Request.php';
163 $a = new HTTP_Request($url, array(
164 'allowRedirects' => true,
166 'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
169 // if this results in an errorr or redirect..
170 // we should log that somewhere.. and display it on the feed...
172 $data = $a->getResponseBody();
175 //$this->jerr($data);
177 // $data = file_get_contents($url);
179 if(file_exists($file))
181 $data = file_get_contents($file);
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;
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)) {
196 $img->setAttribute('src', $this->relPath($url, $href));
199 $this->jerr('Please use the absolutely url for image src!');
204 foreach ($xpath->query('//a[@href]') as $a) {
205 $href = $a->getAttribute('href');
206 if (!preg_match("/^http|mailto|#(.*)$/", $href, $matches)) {
208 $a->setAttribute('href', $this->relPath($url, $href));
211 $this->jerr('Please use the absolutely url for a href!');
215 foreach ($xpath->query('//link[@href]') as $l) {
216 if($l->getAttribute('rel') == 'stylesheet'){
217 $href = $l->getAttribute('href');
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!');
226 $href = $this->relPath($url, $href);
228 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
231 $data = $doc->saveHTML();
233 $htmldoc = new HTML_CSS_InlineStyle($data);
234 if(count($this->styleSheets) > 0){
235 foreach ($this->styleSheets as $styleSheet){
236 $htmldoc->applyStylesheet($styleSheet);
239 $html = $htmldoc->getHTML();
240 libxml_use_internal_errors (false);
242 if (!function_exists('tidy_repair_string')) {
243 return "INSTALL TIDY ON SERVER " . $html;
246 // finally clean it up... using tidy...
249 $html = tidy_repair_string(
253 'output-xhtml' => TRUE,
264 function replaceImageUrl($stylesheet,$href)
266 $base = explode("/", $href);
267 $s = preg_split('/url\(([\'\"]?)/', $stylesheet);
268 foreach($s as $k => $v){
273 array_push($base, $v);
274 $s[$k] = implode("/", $base);
277 $r = implode("url(", $s);
279 $this->checkImportCss($r);
284 function checkImportCss($r)
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]);
293 $this->styleSheets[$p] = $this->replaceImageUrl(file_get_contents($p),$p);