3 * Table Definition for core_email
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Core_email extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'core_email'; // 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)
30 'template' => 'REGISTER'
33 $this->send($content);
37 $tn = $this->tableName();
39 if(!empty($q['search']['nameortitle'])){
41 $tn.name LIKE '%{$this->escape($q['search']['nameortitle'])}%'
43 $tn.subject LIKE '%{$this->escape($q['search']['nameortitle'])}%'
48 function beforeDelete($dependants_array, $roo)
50 $i = DB_DataObject::factory('Images');
52 $i->ontable = $this->tableName();
60 function beforeUpdate($old, $request,$roo)
62 if (!empty($request['_make_copy'])) {
63 $this->makeCopy($roo);
68 function makeCopy($roo)
70 $c = DB_DataObject::Factory($this->tableName());
72 $c->name = "COPY of " . $this->name;
73 $c->updated_dt = $this->sqlValue('NOW()');
76 $c = DB_DataObject::Factory($this->tableName());
82 $i = DB_DataObject::factory('Images');
84 $i->ontable = $this->tableName();
88 $new_image = DB_DataObject::factory('Images');
89 $new_image->onid = $c->id;
90 $new_image->ontable = $this->tableName();
91 $new_image->createFrom($i->getStoreName(), $i->filename);
93 $map[$i->id] = $new_image->id;
97 libxml_use_internal_errors (true);
98 $doc = new DOMDocument('1.0', 'UTF-8');
99 $doc->loadHTML('<?xml encoding="UTF-8"><HTML><BODY>'.$this->bodytext.'</BODY></HTML>');
100 $doc->formatOutput = true;
102 //echo '<PRE>'; print_R($doc);
105 $xpath = new DOMXpath($doc);
106 foreach ($xpath->query('//img[@src]') as $img) {
107 $href = $img->getAttribute('src');
110 if (preg_match("/Images\/([0-9]+)\/([^#]+)\#image\-([0-9]+)$/", $href, $matches)) {
114 if (!isset($map[$oid])) {
115 //echo "skip no new id for $oid";
119 $nstr = "/Images/$nid/{$matches[2]}/#image-{$nid}";
121 $img->setAttribute('src', str_replace($matches[0], $nstr, $href ));
127 $c->bodytext = $doc->saveHTML();
129 libxml_use_internal_errors (false);
132 $roo->jok("duplicated");
135 function onInsert($q,$roo)
137 $i = DB_DataObject::factory('Images');
139 $i->ontable = $this->tableName();
143 $i->onid = $this->id;
147 // $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
148 $this->cachedMailWithOutImages(true, false);
152 function onUpdate($old, $q,$roo)
154 $this->cachedMailWithOutImages(true, false);
158 function attachmentIds()
160 libxml_use_internal_errors (true);
161 $doc = new DOMDocument('1.0', 'UTF-8');
162 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
164 $xpath = new DOMXpath($doc);
167 foreach ($xpath->query('//img[@src]') as $img) { // process images!
168 $href = $img->getAttribute('src');
169 $cid = explode('#', $href);
173 $cid = explode('-', $cid[1]);
174 if (!isset($cid[1])||!is_numeric($cid[1])) {
183 * process replacements is run to generate a template - not the final content..
187 function processRelacements($replace_links = true)
189 libxml_use_internal_errors (true);
190 $doc = new DOMDocument('1.0', 'UTF-8');
191 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
193 $xpath = new DOMXpath($doc);
195 foreach ($xpath->query('//img[@src]') as $img) { // process images!
196 $href = $img->getAttribute('src');
197 $cid = explode('#', $href);
199 $img->setAttribute('src', 'cid:' . $cid[1]);
203 $this->bodytext = $doc->saveHTML();
205 libxml_use_internal_errors (false);
207 $this->bodytext = str_replace('%7B', '{', $this->bodytext ); // kludge as template is not interpretated as html.
208 $this->bodytext = str_replace('%7D', '}', $this->bodytext ); // kludge as template is not interpretated as html.
216 $contents = (array)$obj;
218 $this->cachedMailWithOutImages(true, false);
220 $contents['subject'] = $this->subject;
222 require_once 'Pman/Core/Mailer.php';
224 $ui = posix_getpwuid(posix_geteuid());
226 $templateDir = session_save_path() . '/email-cache-' . $ui['name'] ;
227 $r = new Pman_Core_Mailer(array(
228 'template'=> $this->tableName() . '-' . $this->id,
229 'templateDir' => $templateDir,
231 'contents' => $contents
236 $ui = posix_getpwuid(posix_geteuid());
238 $images = file_get_contents(session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt');
240 $ret['body'] = str_replace('%Images%', $images, $ret['body']);
242 return $r->send($ret);
245 function cachedMailWithOutImages($force = false, $replace_links = true)
247 $random_hash = md5(date('r', time()));
249 $this->cachedImages($random_hash);
251 $ui = posix_getpwuid(posix_geteuid());
253 $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
255 if (!$force && $this->isGenerated($cachePath)) {
259 if (!file_exists(dirname($cachePath))) {
260 mkdir(dirname($cachePath), 0700, true);
263 $this->processRelacements($replace_links);
265 $fh = fopen($cachePath, 'w');
267 fwrite($fh, implode("\n", array(
268 "From: {t.messageFrom():h} ",
269 "To: {t.person.getEmailFrom():h} ",
270 "Subject: {t.subject} ",
271 "X-Message-ID: {t.id} "
275 // note the extra space to finish the last line..
277 Content-Type: multipart/alternative; boundary=alt-{$random_hash}
280 Content-Type: text/plain; charset=utf-8; format=flowed
281 Content-Transfer-Encoding: 7bit
286 Content-Type: multipart/related; boundary=rel-{$random_hash}
289 Content-Type: text/html; charset=utf-8
290 Content-Transfer-Encoding: 7bit
297 --rel-{$random_hash}--
299 --alt-{$random_hash}--
305 function cachedImages($random_hash)
307 $ui = posix_getpwuid(posix_geteuid());
309 $imageCache = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
311 $ids = $this->attachmentIds();
314 $fh = fopen($imageCache, 'w');
316 $i = DB_DataObject::factory('Images');
317 $i->onid = $this->id;
318 $i->ontable = $this->tableName();
319 $i->whereAddIn('id', $ids, 'int');
322 if (!file_exists($i->getStoreName()) || !filesize($i->getStoreName())) {
325 $out = chunk_split(base64_encode(file_get_contents($i->getStoreName())));
329 $imgfn = urlencode(preg_replace('/#.*$/i', '' , $i->filename));
330 fwrite($fh, "--rel-{$random_hash}
331 Content-Type: {$i->mimetype}; name={$imgfn}
332 Content-Transfer-Encoding: base64
333 Content-ID: <attachment-{$i->id}>
334 Content-Disposition: inline; filename={$imgfn}
342 function isGenerated($cachePath)
344 if (!file_exists($cachePath) || !filesize($cachePath)) {
349 $ctime = filemtime($cachePath);
351 $mtime[] = $this->updated_dt;
352 $i = DB_DataObject::factory('Images');
353 $i->onid = $this->id;
354 $i->ontable = $this->tableName();
356 $i->selectAdd('max(created) as created');
358 $mtime[] = $i->created;
359 if($ctime >= strtotime(max($mtime))){
366 function messageFrom()
368 return '"' . addslashes($this->from_name) . '" <' . $this->from_email. '>' ;
371 function formatDate($dt, $format = 'd/M/Y')
373 return date($format, strtotime($dt));