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)
29 $c = DB_DataObject::factory($this->tableName());
36 $tn = $this->tableName();
38 if(!empty($q['search']['nameortitle'])){
40 $tn.name LIKE '%{$this->escape($q['search']['nameortitle'])}%'
42 $tn.subject LIKE '%{$this->escape($q['search']['nameortitle'])}%'
47 function beforeDelete($dependants_array, $roo)
49 $i = DB_DataObject::factory('Images');
51 $i->ontable = $this->tableName();
59 function beforeUpdate($old, $request,$roo)
61 if (!empty($request['_make_copy'])) {
62 $this->makeCopy($roo);
67 function makeCopy($roo)
69 $c = DB_DataObject::Factory($this->tableName());
71 $c->name = "COPY of " . $this->name;
72 $c->updated_dt = $this->sqlValue('NOW()');
75 $c = DB_DataObject::Factory($this->tableName());
81 $i = DB_DataObject::factory('Images');
83 $i->ontable = $this->tableName();
87 $new_image = DB_DataObject::factory('Images');
88 $new_image->onid = $c->id;
89 $new_image->ontable = $this->tableName();
90 $new_image->createFrom($i->getStoreName(), $i->filename);
92 $map[$i->id] = $new_image->id;
96 libxml_use_internal_errors (true);
97 $doc = new DOMDocument('1.0', 'UTF-8');
98 $doc->loadHTML('<?xml encoding="UTF-8"><HTML><BODY>'.$this->bodytext.'</BODY></HTML>');
99 $doc->formatOutput = true;
101 //echo '<PRE>'; print_R($doc);
104 $xpath = new DOMXpath($doc);
105 foreach ($xpath->query('//img[@src]') as $img) {
106 $href = $img->getAttribute('src');
109 if (preg_match("/Images\/([0-9]+)\/([^#]+)\#image\-([0-9]+)$/", $href, $matches)) {
113 if (!isset($map[$oid])) {
114 //echo "skip no new id for $oid";
118 $nstr = "/Images/$nid/{$matches[2]}/#image-{$nid}";
120 $img->setAttribute('src', str_replace($matches[0], $nstr, $href ));
126 $c->bodytext = $doc->saveHTML();
128 libxml_use_internal_errors (false);
131 $roo->jok("duplicated");
134 function onInsert($q,$roo)
136 $i = DB_DataObject::factory('Images');
138 $i->ontable = $this->tableName();
142 $i->onid = $this->id;
146 // $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
147 $this->cachedMailWithOutImages(true, false);
151 function onUpdate($old, $q,$roo)
153 $this->cachedMailWithOutImages(true, false);
157 function attachmentIds()
159 libxml_use_internal_errors (true);
160 $doc = new DOMDocument('1.0', 'UTF-8');
161 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
163 $xpath = new DOMXpath($doc);
166 foreach ($xpath->query('//img[@src]') as $img) { // process images!
167 $href = $img->getAttribute('src');
168 $cid = explode('#', $href);
172 $cid = explode('-', $cid[1]);
173 if (!isset($cid[1])||!is_numeric($cid[1])) {
182 * process replacements is run to generate a template - not the final content..
186 function processRelacements($replace_links = true)
188 libxml_use_internal_errors (true);
189 $doc = new DOMDocument('1.0', 'UTF-8');
190 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
192 $xpath = new DOMXpath($doc);
194 foreach ($xpath->query('//img[@src]') as $img) { // process images!
195 $href = $img->getAttribute('src');
196 $cid = explode('#', $href);
198 $img->setAttribute('src', 'cid:' . $cid[1]);
202 $this->bodytext = $doc->saveHTML();
204 libxml_use_internal_errors (false);
206 $this->bodytext = str_replace('%7B', '{', $this->bodytext ); // kludge as template is not interpretated as html.
207 $this->bodytext = str_replace('%7D', '}', $this->bodytext ); // kludge as template is not interpretated as html.
215 $contents = (array)$obj;
217 $this->cachedMailWithOutImages(true, false);
219 $contents['subject'] = $this->subject;
221 require_once 'Pman/Core/Mailer.php';
223 $ui = posix_getpwuid(posix_geteuid());
225 $templateDir = session_save_path() . '/email-cache-' . $ui['name'] ;
226 $r = new Pman_Core_Mailer(array(
227 'template'=> $this->tableName() . '-' . $this->id,
228 'templateDir' => $templateDir,
230 'contents' => $contents
235 $ui = posix_getpwuid(posix_geteuid());
237 $images = file_get_contents(session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt');
239 $ret['body'] = str_replace('%Images%', $images, $ret['body']);
241 return $r->send($ret);
244 function cachedMailWithOutImages($force = false, $replace_links = true)
246 $random_hash = md5(date('r', time()));
248 $this->cachedImages($random_hash);
250 $ui = posix_getpwuid(posix_geteuid());
252 $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
253 print_r($cachePath);exit;
254 if (!$force && $this->isGenerated($cachePath)) {
258 if (!file_exists(dirname($cachePath))) {
259 mkdir(dirname($cachePath), 0700, true);
262 $this->processRelacements($replace_links);
264 $fh = fopen($cachePath, 'w');
266 fwrite($fh, implode("\n", array(
267 "From: {t.messageFrom():h} ",
268 "To: {t.person.getEmailFrom():h} ",
269 "Subject: {t.subject} ",
270 "X-Message-ID: {t.id} "
274 // note the extra space to finish the last line..
276 Content-Type: multipart/alternative; boundary=alt-{$random_hash}
279 Content-Type: text/plain; charset=utf-8; format=flowed
280 Content-Transfer-Encoding: 7bit
285 Content-Type: multipart/related; boundary=rel-{$random_hash}
288 Content-Type: text/html; charset=utf-8
289 Content-Transfer-Encoding: 7bit
296 --rel-{$random_hash}--
298 --alt-{$random_hash}--
304 function cachedImages($random_hash)
306 $ui = posix_getpwuid(posix_geteuid());
308 $imageCache = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
310 $ids = $this->attachmentIds();
313 $fh = fopen($imageCache, 'w');
315 $i = DB_DataObject::factory('Images');
316 $i->onid = $this->id;
317 $i->ontable = $this->tableName();
318 $i->whereAddIn('id', $ids, 'int');
321 if (!file_exists($i->getStoreName()) || !filesize($i->getStoreName())) {
324 $out = chunk_split(base64_encode(file_get_contents($i->getStoreName())));
328 $imgfn = urlencode(preg_replace('/#.*$/i', '' , $i->filename));
329 fwrite($fh, "--rel-{$random_hash}
330 Content-Type: {$i->mimetype}; name={$imgfn}
331 Content-Transfer-Encoding: base64
332 Content-ID: <attachment-{$i->id}>
333 Content-Disposition: inline; filename={$imgfn}
341 function isGenerated($cachePath)
343 if (!file_exists($cachePath) || !filesize($cachePath)) {
348 $ctime = filemtime($cachePath);
350 $mtime[] = $this->updated_dt;
351 $i = DB_DataObject::factory('Images');
352 $i->onid = $this->id;
353 $i->ontable = $this->tableName();
355 $i->selectAdd('max(created) as created');
357 $mtime[] = $i->created;
358 if($ctime >= strtotime(max($mtime))){
365 function messageFrom()
367 return '"' . addslashes($this->from_name) . '" <' . $this->from_email. '>' ;
370 function formatDate($dt, $format = 'd/M/Y')
372 return date($format, strtotime($dt));