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 $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($q,$roo)
129 $i = DB_DataObject::factory('Images');
131 $i->ontable = $this->tableName();
135 $i->onid = $this->id;
139 // $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
140 $this->cachedMailWithOutImages(true, false);
144 function onUpdate($old, $q,$roo)
146 $this->cachedMailWithOutImages(true, false);
150 function attachmentIds()
152 libxml_use_internal_errors (true);
153 $doc = new DOMDocument('1.0', 'UTF-8');
154 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
156 $xpath = new DOMXpath($doc);
159 foreach ($xpath->query('//img[@src]') as $img) { // process images!
160 $href = $img->getAttribute('src');
161 $cid = explode('#', $href);
165 $cid = explode('-', $cid[1]);
166 if (!isset($cid[1])||!is_numeric($cid[1])) {
175 * process replacements is run to generate a template - not the final content..
179 function processRelacements($replace_links = true)
181 libxml_use_internal_errors (true);
182 $doc = new DOMDocument('1.0', 'UTF-8');
183 $doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
185 $xpath = new DOMXpath($doc);
187 foreach ($xpath->query('//img[@src]') as $img) { // process images!
188 $href = $img->getAttribute('src');
189 $cid = explode('#', $href);
191 $img->setAttribute('src', 'cid:' . $cid[1]);
195 $this->bodytext = $doc->saveHTML();
197 libxml_use_internal_errors (false);
199 $this->bodytext = str_replace('%7B', '{', $this->bodytext ); // kludge as template is not interpretated as html.
200 $this->bodytext = str_replace('%7D', '}', $this->bodytext ); // kludge as template is not interpretated as html.
207 $contents = (array)$obj;
209 $this->get('name', $contents['template']);
211 if(empty($this->id)){
215 $contents['subject'] = $this->subject;
217 $ui = posix_getpwuid(posix_geteuid());
219 $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
221 if(!$this->isGenerated($cachePath)){
222 $this->cachedMailWithOutImages(true, false);
225 require_once 'Pman/Core/Mailer.php';
227 $templateDir = session_save_path() . '/email-cache-' . $ui['name'] ;
228 $r = new Pman_Core_Mailer(array(
229 'template'=> $this->tableName() . '-' . $this->id,
230 'templateDir' => $templateDir,
232 'contents' => $contents
235 $imageCache = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
237 if(file_exists($imageCache) && filesize($imageCache)){
238 $images = json_decode(file_get_contents($imageCache), true);
239 $r->images = $images;
244 return $r->send($ret);
247 function cachedMailWithOutImages($force = false, $replace_links = true)
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 $random_hash = md5(date('r', time()));
265 $this->cachedImages();
267 $fh = fopen($cachePath, 'w');
269 fwrite($fh, implode("\n", array(
270 "From: {t.messageFrom():h} ",
271 "To: {t.person.getEmailFrom():h} ",
272 "Subject: {t.subject} ",
273 "X-Message-ID: {t.id} "
277 // note the extra space to finish the last line..
279 Content-Type: multipart/alternative; boundary=alt-{$random_hash}
282 Content-Type: text/plain; charset=utf-8; format=flowed
283 Content-Transfer-Encoding: 7bit
292 $this->processRelacements($replace_links);
294 $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.body.html';
296 if (!file_exists(dirname($cachePath))) {
297 mkdir(dirname($cachePath), 0700, true);
300 file_put_contents($cachePath, $this->bodytext);
304 function cachedImages()
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');
323 require_once 'File/MimeType.php';
324 $y = new File_MimeType();
327 if (!file_exists($i->getStoreName()) || !filesize($i->getStoreName())) {
331 $images["attachment-$i->id"] = array(
332 'file' => $i->getStoreName(),
333 'mimetype' => $i->mimetype,
334 'ext' => $y->toExt($i->mimetype),
335 'contentid' => "attachment-$i->id"
339 file_put_contents($imageCache, json_encode($images));
343 function isGenerated($cachePath)
345 if (!file_exists($cachePath) || !filesize($cachePath)) {
350 $ctime = filemtime($cachePath);
352 $mtime[] = $this->updated_dt;
353 $i = DB_DataObject::factory('Images');
354 $i->onid = $this->id;
355 $i->ontable = $this->tableName();
357 $i->selectAdd('max(created) as created');
359 $mtime[] = $i->created;
360 if($ctime >= strtotime(max($mtime))){
367 function messageFrom()
369 return '"' . addslashes($this->from_name) . '" <' . $this->from_email. '>' ;
372 function formatDate($dt, $format = 'd/M/Y')
374 return date($format, strtotime($dt));