3 * Table Definition for core_mailinglist_message
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Core_mailing_list_message extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'core_mailing_list_message'; // table name
13 public $id; // int(11) not_null primary_key auto_increment
15 public $subject; // blob(65535) blob
16 public $bodytext; // blob(65535) blob
18 public $updated_dt; //datetime not_null
24 /* the code above is auto generated do not remove the tag below */
27 function applyFilters($q, $au, $roo)
29 $tn = $this->tableName();
31 if(!empty($q['search']['nameortitle'])){
33 $tn.name LIKE '%{$this->escape($q['search']['nameortitle'])}%'
35 $tn.subject LIKE '%{$this->escape($q['search']['nameortitle'])}%'
40 function beforeDelete($dependants_array, $roo)
42 $i = DB_DataObject::factory('Images');
44 $i->ontable = $this->tableName();
52 function beforeUpdate($old, $request,$roo)
54 if (!empty($request['_make_copy'])) {
55 $this->makeCopy($roo);
60 function makeCopy($roo)
62 $c = DB_DataObject::Factory($this->tableName());
64 $c->name = "COPY of " . $this->name;
65 $c->updated_dt = $this->sqlValue('NOW()');
68 $c = DB_DataObject::Factory($this->tableName());
74 $i = DB_DataObject::factory('Images');
76 $i->ontable = $this->tableName();
80 $new_image = DB_DataObject::factory('Images');
81 $new_image->onid = $c->id;
82 $new_image->ontable = $this->tableName();
83 $new_image->createFrom($i->getStoreName(), $i->filename);
85 $map[$i->id] = $new_image->id;
89 libxml_use_internal_errors (true);
90 $doc = new DOMDocument('1.0', 'UTF-8');
91 $doc->loadHTML('<?xml encoding="UTF-8"><HTML><BODY>'.$this->bodytext.'</BODY></HTML>');
92 $doc->formatOutput = true;
94 //echo '<PRE>'; print_R($doc);
97 $xpath = new DOMXpath($doc);
98 foreach ($xpath->query('//img[@src]') as $img) {
99 $href = $img->getAttribute('src');
102 if (preg_match("/Images\/([0-9]+)\/([^#]+)\#image\-([0-9]+)$/", $href, $matches)) {
106 if (!isset($map[$oid])) {
107 //echo "skip no new id for $oid";
111 $nstr = "/Images/$nid/{$matches[2]}/#image-{$nid}";
113 $img->setAttribute('src', str_replace($matches[0], $nstr, $href ));
119 $c->bodytext = $doc->saveHTML();
121 libxml_use_internal_errors (false);
124 $roo->jok("duplicated");
127 function onInsert($request,$roo)
129 $i = DB_DataObject::factory('Images');
131 $i->ontable = $this->tableName();
135 $i->onid = $this->id;
141 function attachmentIds()
143 libxml_use_internal_errors (true);
144 $doc = new DOMDocument('1.0', 'UTF-8');
145 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
147 $xpath = new DOMXpath($doc);
150 foreach ($xpath->query('//img[@src]') as $img) { // process images!
151 $href = $img->getAttribute('src');
152 $cid = explode('#', $href);
156 $cid = explode('-', $cid[1]);
157 if (!isset($cid[1])||!is_numeric($cid[1])) {
166 * process replacements is run to generate a template - not the final content..
170 function processRelacements($replace_links = true)
172 libxml_use_internal_errors (true);
173 $doc = new DOMDocument('1.0', 'UTF-8');
174 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
176 $xpath = new DOMXpath($doc);
178 foreach ($xpath->query('//img[@src]') as $img) { // process images!
179 $href = $img->getAttribute('src');
180 $cid = explode('#', $href);
182 $img->setAttribute('src', 'cid:' . $cid[1]);
186 $this->bodytext = $doc->saveHTML();
188 libxml_use_internal_errors (false);
190 $this->bodytext = str_replace('%7B', '{', $this->bodytext ); // kludge as template is not interpretated as html.
191 $this->bodytext = str_replace('%7D', '}', $this->bodytext ); // kludge as template is not interpretated as html.
198 $contents = (array)$obj;
200 $this->cachedMailWithOutImages(true, false);
202 $contents['subject'] = $this->subject;
204 require_once 'Pman/Core/Mailer.php';
206 $templateDir = session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') ;
207 $r = new Pman_Core_Mailer(array(
208 'template'=> $this->tableName() . '-' . $this->id,
209 'templateDir' => $templateDir,
211 'contents' => $contents
216 $images = file_get_contents(session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt');
218 $ret['body'] = str_replace('%Images%', $images, $ret['body']);
220 return $r->send($ret);
223 function cachedMailWithOutImages($force = false, $replace_links = true)
225 $random_hash = md5(date('r', time()));
227 $this->cachedImages($random_hash);
229 $cachePath = session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
231 if (!$force && $this->isGenerated($cachePath)) {
235 if (!file_exists(dirname($cachePath))) {
236 mkdir(dirname($cachePath), 0700, true);
239 $this->processRelacements($replace_links);
241 $fh = fopen($cachePath, 'w');
243 fwrite($fh, implode("\n", array(
244 "From: {t.messageFrom():h} ",
245 "To: {t.person.getEmailFrom():h} ",
246 "Subject: {t.subject} ",
247 "X-Message-ID: {t.id} "
251 // note the extra space to finish the last line..
253 Content-Type: multipart/alternative; boundary=alt-{$random_hash}
256 Content-Type: text/plain; charset=utf-8; format=flowed
257 Content-Transfer-Encoding: 7bit
262 Content-Type: multipart/related; boundary=rel-{$random_hash}
265 Content-Type: text/html; charset=utf-8
266 Content-Transfer-Encoding: 7bit
273 --rel-{$random_hash}--
275 --alt-{$random_hash}--
281 function cachedImages($random_hash)
283 $imageCache = session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
285 $ids = $this->attachmentIds();
287 //$this->jerr(print_r($ids,true));
290 $fh = fopen($imageCache, 'w');
292 $i = DB_DataObject::factory('Images');
293 $i->onid = $this->id;
294 $i->ontable = $this->tableName();
295 $i->whereAddIn('id', $ids, 'int');
298 if (!file_exists($i->getStoreName()) || !filesize($i->getStoreName())) {
301 $out = chunk_split(base64_encode(file_get_contents($i->getStoreName())));
305 $imgfn = urlencode(preg_replace('/#.*$/i', '' , $i->filename));
306 fwrite($fh, "--rel-{$random_hash}
307 Content-Type: {$i->mimetype}; name={$imgfn}
308 Content-Transfer-Encoding: base64
309 Content-ID: <attachment-{$i->id}>
310 Content-Disposition: inline; filename={$imgfn}
318 function isGenerated($cachePath)
320 if (!file_exists($cachePath) || !filesize($cachePath)) {
325 $ctime = filemtime($cachePath);
327 $mtime[] = $this->updated_dt;
328 $i = DB_DataObject::factory('Images');
329 $i->onid = $this->id;
330 $i->ontable = $this->tableName();
332 $i->selectAdd('max(created) as created');
334 $mtime[] = $i->created;
335 if($ctime >= strtotime(max($mtime))){
342 function messageFrom()
344 return '"' . addslashes($this->from_name) . '" <' . $this->from_email. '>' ;
347 function formatDate($dt, $format = 'd/M/Y')
349 return date($format, strtotime($dt));