check file exists
[Pman.Core] / ConvertStyle.php
index 55f2cdd..4f52e10 100644 (file)
@@ -18,20 +18,22 @@ class Pman_Core_ConvertStyle extends Pman
     }
     
     function relPath($base, $url)
-    {   
-        //var_dump(array($base,$url));        
+    {  
+        
         if (preg_match('/^(http|https|mailto):/',$url)) {
             return $url;
         }
+        
         $ui = parse_url($base);
-        // if it starts with '/'...
-        // we do not handle ports...
+        
+        if($ui['host'] == 'localhost'){
+            return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/'. $url;
+        }
+        
         if (substr($url,0,2) == '//') {
             return $ui['scheme'] .':' .  $url;
         }
         
-        
-        
         if (substr($url,0,1) == '/') {
             return $ui['scheme'] .'://'.$ui['host']. $url;
         }
@@ -43,53 +45,17 @@ class Pman_Core_ConvertStyle extends Pman
             return $ui['scheme'] .'://'.$ui['host']. '/' . $url;
            
         }
-        /// not sure if this will work...
-        return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
-        
-    }
-    
-    
-    function post()
-    {
-        // Import from URL
-        if(isset($_REQUEST['importUrl']))
-        {
-            $this->checkHeader($_REQUEST['importUrl']);
-            $data = $this->convertStyle($_REQUEST['importUrl'], '');
-            $this->jok($data);
-        }
-     
-        // Import from file
-        $htmlFile = DB_DataObject::factory('images');
-        $htmlFile->setFrom(array(
-               'onid' => 0,
-               'ontable' =>'crm_mailing_list_message'
-        ));
-        $htmlFile->onUpload(false);
-       // print_r($htmlFile);
-        if($htmlFile->mimetype != 'text/html')
-        {
-            $this->jerr('accept html file only!');
-        }
-        if(!file_exists($htmlFile->getStoreName()))
-        {
-            $this->jerr('update failed!');
-        }
-        
-        $data = $this->convertStyle('', $htmlFile->getStoreName());
         
-        $htmlFile->delete();
-        unlink($htmlFile->getStoreName()) or die('Unable to delete the file');
+        return $ui['scheme'] .'://'.$ui['host']. $ui['path'] . '/../'. $url;
         
-        $this->jok($data);
     }
     
     function checkHeader($url)
     {
-        if(strpos($url, 'https') !== false)
-        {
-            $this->jerr('accept HTTP url only!');
-        }
+        // if(strpos($url, 'https') !== false)
+        // {
+        //     $this->jerr('accept HTTP url only!');
+        // }
         $headers = get_headers($url, 1);
         if(strpos(is_array($headers['Content-Type']) ? $headers['Content-Type'][0] : $headers['Content-Type'], 'text/html') === false)
         {
@@ -100,9 +66,11 @@ class Pman_Core_ConvertStyle extends Pman
     
     var $styleSheets = array();
     
-    function convertStyle($url, $file)
+    function convertStyle($url, $file, $is_url = true)
     {
-        if(!empty($url))
+        $inLineCss = true;
+        
+        if($is_url && !empty($url))
         {
             $host = parse_url($url);
             require_once 'System.php';
@@ -118,35 +86,20 @@ class Pman_Core_ConvertStyle extends Pman
             if (!trim(strlen($data))) {
                 $this->jerr("url returned an empty string");
             }
-           // $this->jerr($url);
-            /*require_once 'HTTP/Request.php';
-            $a = new HTTP_Request($url, array(
-                    'allowRedirects' => true,
-                    'maxRedirects' => 2, 
-                    'userAgent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
-                    ));
-            $a->sendRequest();
-            // if this results in an errorr or redirect..
-            // we should log that somewhere.. and display it on the feed...
-            
-            $data =  $a->getResponseBody();
-            */
-            
-            //$this->jerr($data);
-            
-        //    $data = file_get_contents($url);
         }
-        if(file_exists($file))
-        {
+        
+        if(!$is_url){
             $data = file_get_contents($file);
         }
         
+        if(preg_match('/^\s*<!--\s*NOT CONVERT STYLE\s*-->\s*/', $data)){
+            $inLineCss = false;
+        }
+        
         libxml_use_internal_errors (true);
         $doc = new DOMDocument('1.0', 'UTF-8');
         $doc->loadHTML('<?xml encoding="UTF-8">'.$data);
         $doc->formatOutput = true;
-
-      
         
         $xpath = new DOMXpath($doc);
         foreach ($xpath->query('//img[@src]') as $img) {
@@ -176,20 +129,27 @@ class Pman_Core_ConvertStyle extends Pman
             if($l->getAttribute('rel') == 'stylesheet'){
                 $href = $l->getAttribute('href');
                 
-                
-                if (empty($url) && !preg_match("/^http(.*)$/", $href, $matches)) {
-                    // import from file , must use absolutely url
-                    $this->jerr('Please use the absolutely url for link href!');
-                }
-                if (!empty($url)) {
-                    // import from URL
+                if(!preg_match("/^http(.*)$/", $href, $matches)){
+                    if(empty($url)){
+                        $this->jerr('Please use the absolutely url for link href!');
+                    }
                     $href = $this->relPath($url,  $href);
                 }
+                
                 $this->styleSheets[$href] = $this->replaceImageUrl(file_get_contents($href),$href);
             }
         }
+        
+        foreach ($xpath->query('//style') as $s){
+            $this->styleSheets[] = $this->replaceImageUrl($s->nodeValue, $url);
+        }
+        
         $data = $doc->saveHTML();
         
+        if(!$inLineCss){
+            return $data;
+        }
+        
         $htmldoc = new HTML_CSS_InlineStyle($data);
         if(count($this->styleSheets) > 0){
             foreach ($this->styleSheets as $styleSheet){
@@ -203,9 +163,6 @@ class Pman_Core_ConvertStyle extends Pman
             return "INSTALL TIDY ON SERVER " . $html;
         }
         
-        // finally clean it up... using tidy...
-       
         $html = tidy_repair_string(
                 $html,
                 array(