DataObjects/core.sql
[Pman.Core] / DataObjects / Core_mailing_list_message.php
index 751b863..026a1cc 100644 (file)
@@ -140,9 +140,6 @@ class Pman_Core_DataObjects_Core_mailing_list_message extends DB_DataObject
     
     function attachmentIds()
     {
-        
-         $roo = HTML_FlexyFramework::get()->page;
-        
         libxml_use_internal_errors (true);
         $doc = new DOMDocument('1.0', 'UTF-8');
         $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
@@ -162,7 +159,7 @@ class Pman_Core_DataObjects_Core_mailing_list_message extends DB_DataObject
             }
             $ret[] = $cid[1];
         }
-       // print_r($ret);
+        
         return $ret;
     }
     /**
@@ -200,14 +197,7 @@ class Pman_Core_DataObjects_Core_mailing_list_message extends DB_DataObject
     {    
         $contents = (array)$obj;
         
-        $q = DB_DataObject::factory('crm_mailing_list_queue');
-        $q->id = 'test-message-'. $this->id;
-        $q->message_id = $this->id;
-        $q->message_id_subject = $this->subject;
-        $q->message_id_from_email = $this->from_email;
-        $q->message_id_from_name = $this->from_name;
-        
-        $q->cachedMailWithOutImages(true, false);
+        $this->cachedMailWithOutImages(true, false);
         
         $contents['subject'] = $this->subject;
         
@@ -215,26 +205,150 @@ class Pman_Core_DataObjects_Core_mailing_list_message extends DB_DataObject
         
         $templateDir = session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') ;
         $r = new Pman_Core_Mailer(array(
-            'template'=> $q->id,
+            'template'=> $this->tableName() . '-' . $this->id,
             'templateDir' => $templateDir,
-            'page' => $q,
+            'page' => $this,
             'contents' => $contents
-            //array(
-            //    'person' => $person,
-            //    'subject' => $this->message_id_subject,
-           // )
         ));
         
-        
-         
-        ///print_r($r->toData());
         $ret = $r->toData();
-        $images = file_get_contents(session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $q->id . '-images.txt');
-       // var_dump($images);exit;
+        
+        $images = file_get_contents(session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt');
         
         $ret['body'] = str_replace('%Images%', $images, $ret['body']);
         
         return $r->send($ret);
     }
     
+    function cachedMailWithOutImages($force = false, $replace_links = true)
+    {  
+        $random_hash = md5(date('r', time()));
+        
+        $this->cachedImages($random_hash);
+        
+        $cachePath = session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
+          
+        if (!$force && $this->isGenerated($cachePath)) {
+            return;
+        }
+        
+        if (!file_exists(dirname($cachePath))) {
+            mkdir(dirname($cachePath), 0700, true);
+        }
+        
+        $this->processRelacements($replace_links);
+        
+        $fh = fopen($cachePath, 'w');
+
+        fwrite($fh, implode("\n", array(
+            "From: {t.messageFrom():h} ",
+            "To: {t.person.getEmailFrom():h} ",
+            "Subject: {t.subject} ",
+            "X-Message-ID: {t.id} "
+        ))."\n");
+        
+        
+// note the extra space to finish the last line..
+        fwrite($fh, " " . "
+Content-Type: multipart/alternative; boundary=alt-{$random_hash}
+
+--alt-{$random_hash}
+Content-Type: text/plain; charset=utf-8; format=flowed
+Content-Transfer-Encoding: 7bit
+
+{$this->plaintext}
+    
+--alt-{$random_hash}
+Content-Type: multipart/related; boundary=rel-{$random_hash}
+
+--rel-{$random_hash}
+Content-Type: text/html; charset=utf-8
+Content-Transfer-Encoding: 7bit
+
+{$this->bodytext}
+
+");  
+
+        fwrite($fh,"%Images%
+--rel-{$random_hash}--
+
+--alt-{$random_hash}--
+");
+        fclose($fh);
+        
+    }
+    
+    function cachedImages($random_hash)
+    {
+        $imageCache = session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
+        
+        $ids = $this->attachmentIds();
+        
+        //$this->jerr(print_r($ids,true));
+        
+         
+        $fh = fopen($imageCache, 'w');
+        
+        $i = DB_DataObject::factory('Images');
+        $i->onid = $this->id;
+        $i->ontable = $this->tableName();
+        $i->whereAddIn('id', $ids, 'int');
+        $i->find();
+        while ($i->fetch()){
+            if (!file_exists($i->getStoreName()) || !filesize($i->getStoreName())) {
+                continue;
+            }
+            $out = chunk_split(base64_encode(file_get_contents($i->getStoreName())));
+            if (empty($out)) {
+                continue;
+            }
+            $imgfn = urlencode(preg_replace('/#.*$/i', '' , $i->filename));
+            fwrite($fh, "--rel-{$random_hash}
+Content-Type: {$i->mimetype}; name={$imgfn}
+Content-Transfer-Encoding: base64
+Content-ID: <attachment-{$i->id}>
+Content-Disposition: inline; filename={$imgfn}
+
+" . $out  . "");
+
+            }
+        
+    }
+    
+    function isGenerated($cachePath)
+    {
+        if (!file_exists($cachePath) || !filesize($cachePath)) {
+            return false;
+        }
+        
+        
+        $ctime = filemtime($cachePath);
+        $mtime = array();
+        $mtime[] = $this->updated_dt;
+        $i = DB_DataObject::factory('Images');
+        $i->onid = $this->id;
+        $i->ontable = $this->tableName();
+        $i->selectAdd();
+        $i->selectAdd('max(created) as created');
+        $i->find(true);
+        $mtime[] = $i->created;
+        if($ctime >= strtotime(max($mtime))){
+            return true;
+        }
+        
+        return false;
+    }
+    
+    function messageFrom()
+    {
+        return '"' . addslashes($this->from_name) . '" <' . $this->from_email. '>'  ;
+    }
+    
+    function formatDate($dt, $format = 'd/M/Y')
+    {
+        return date($format, strtotime($dt));
+    } 
+    
+    
+    
 }