Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
email_format.inc
1 <?php
18 require_once SQ_ATTRIBUTES_PATH.'/wysiwyg/wysiwyg.inc';
19 require_once SQ_ATTRIBUTES_PATH.'/option_list/option_list.inc';
20 
36 {
37 
38 
46  function Asset_Attribute_Email_Format($attributeid=0, $value=NULL)
47  {
48  $this->Asset_Attribute_WYSIWYG($attributeid, $value);
49  $this->_edit_params['with_asset_finder'] = FALSE;
50  $this->_edit_params['show_from'] = TRUE;
51  $this->_edit_params['show_reply_to'] = TRUE;
52  $this->_edit_params['show_to'] = TRUE;
53  $this->_edit_params['show_inc_attachments'] = FALSE;
54  $this->_edit_params['show_asset_to_send'] = FALSE;
55 
56  }//end constructor
57 
58 
68  function setEditParams(&$node)
69  {
70  if (!parent::setEditParams($node)) return FALSE;
71  foreach ($this->_edit_params as $field => $val) {
72  if (isset($node->attributes()->$field)) {
73  // Cannot convert directly to boolean, since (bool) of any non-empty
74  // object is TRUE. Convert to int first, then to bool. (Bug #4095)
75  $this->_edit_params[$field] = (bool)(int)$node->attributes()->$field;
76  }
77  }
78  return TRUE;
79 
80  }//end setEditParams()
81 
82 
92  function paint($prefix, $read_only=FALSE)
93  {
94  $prefix = str_replace(':', '_', $prefix);
95 
96  $current_value = @unserialize($this->value);
97  if (!is_array($current_value)) {
98  $current_value = Array();
99  }
100  if (!isset($current_value['to'])) {
101  $current_value['to'] = Array();
102  }
103  if (!isset($current_value['to_assetids'])) {
104  $current_value['to_assetids'] = Array();
105  }
106  if (!isset($current_value['from'])) {
107  $current_value['from'] = '';
108  }
109  if (!isset($current_value['reply_to'])) {
110  $current_value['reply_to'] = '';
111  }
112  if (!isset($current_value['subject'])) {
113  $current_value['subject'] = '';
114  }
115  if (!isset($current_value['html_format'])) {
116  $current_value['html_format'] = '';
117  }
118  if (!isset($current_value['text_format'])) {
119  $current_value['text_format'] = '';
120  }
121  if (!isset($current_value['inc_attachments'])) {
122  $current_value['inc_attachments'] = Array();
123  }
124  if (!isset($current_value['attachments'])) {
125  $current_value['attachments'] = Array();
126  }
127  if (!isset($current_value['asset_to_send'])) {
128  $current_value['asset_to_send'] = '';
129  }
130  if (!isset($current_value['design_to_apply'])) {
131  $current_value['design_to_apply'] = '';
132  }
133  if (!isset($current_value['layout_to_apply'])) {
134  $current_value['layout_to_apply'] = '';
135  }
136  ?>
137  <script type="text/javascript" src="<?php echo sq_web_path('lib'); ?>/js/email_format.js"></script>
138  <table border="0" class="email-format">
139  <?php
140  if ($this->_edit_params['show_to']) {
141  ?>
142  <tr>
143  <th><?php echo translate('to'); ?></th>
144  <td>
145  <?php
146  $ol = new Asset_Attribute_Option_List();
147  $ol->value = implode("\r\n", $current_value['to']);
148  $ol->setEditParam('width', '50');
149  $ol->paint($prefix.'_to_emails', $read_only);
150 
151  if ($this->_edit_params['with_asset_finder']) {
152  if ($read_only) {
153  foreach ($current_value['to_assetids'] as $assetid) {
154  echo get_asset_tag_line($assetid).'<br />';
155  }
156  } else {
157  multiple_asset_finder($prefix.'_to_assetids', $current_value['to_assetids'], Array('user' => 'D', 'user_group' => 'I', 'bulkmail_user' => 'I'));
158  }
159  }
160  ?>
161  </td>
162  </tr>
163  <?php
164  }
165  if ($this->_edit_params['show_from']) {
166  ?>
167  <tr>
168  <th><?php echo translate('from'); ?></th>
169  <td>
170  <?php
171  if ($read_only) {
172  echo htmlspecialchars($current_value['from']);
173  } else {
174  text_box($prefix.'_from', $current_value['from'], 50);
175  }
176  ?>
177  </td>
178  </tr>
179  <?php
180  }
181  if ($this->_edit_params['show_reply_to']) {
182  ?>
183  <tr>
184  <th><?php echo translate('reply_to'); ?></th>
185  <td>
186  <?php
187  if ($read_only) {
188  echo htmlspecialchars($current_value['reply_to']);
189  } else {
190  text_box($prefix.'_reply_to', $current_value['reply_to'], 50);
191  }
192  ?>
193  </td>
194  </tr>
195  <?php
196  }
197  ?>
198  <tr>
199  <th><?php echo translate('subject'); ?></th>
200  <td>
201  <?php
202  if ($read_only) {
203  echo htmlspecialchars($current_value['subject']);
204  } else {
205  text_box($prefix.'_subject', $current_value['subject'], 50);
206  }
207  ?>
208  </td>
209  </tr>
210  <?php
211  if ($this->_edit_params['show_inc_attachments']) {
212  ?>
213  <tr>
214  <th><?php echo translate('attachments'); ?></th>
215  <td>
216  <?php
217  if ($read_only) {
218  echo '<img src="'.sq_web_path('lib').'/web/images/'.($current_value['inc_attachments'] ? 'tick' : 'cross').'.gif" width="15" height="15" />&nbsp;';
219  echo translate('include_file_attachments');
220  } else {
221  check_box($prefix.'_inc_attachments', TRUE, $current_value['inc_attachments']);
222  label(translate('include_file_attachments'), $prefix.'_inc_attachments');
223  }
224  ?>
225  </td>
226  </tr>
227  <?php
228  }
229  ?>
230  <tr>
231  <th><?php echo translate('body'); ?></th>
232  <td>
233  <table border="0" class="email-format-body">
234  <tr>
235  <td>
236  <?php
237  if ($read_only) {
238  sq_print_icon(sq_web_path('lib').'/web/images/icons/edit_mode.png', 16, 16, 'Toggle Email Format', 'Toggle Email Format','class="clickable" onclick="emailFormatSwitchReadingMode(\''.$prefix."', '".addslashes(translate('html_email_version'))."', '".addslashes(translate('text_email_version')).'\')"');
239  } else {
240  sq_print_icon(sq_web_path('lib').'/web/images/icons/edit_mode.png', 16, 16, 'Toggle Email Format', 'Toggle Email Format','class="clickable" onclick="emailFormatSwitchEditingMode(\''.$prefix."', '".addslashes(translate('html_email_version'))."', '".addslashes(translate('text_email_version')).'\')"');
241  }
242  ?>
243  </td>
244  <td id="<?php echo $prefix; ?>_format_mode" class="email-format-version">
245  <?php echo translate('text_email_version'); ?>
246  </td>
247  </tr>
248  <tr>
249  <td colspan="2">
250  <?php
251  if ($read_only) {
252  ?>
253  <div style="display: none" id="<?php echo $prefix?>_html_format">
254  <?php echo $current_value['html_format']; ?>
255  </div>
256  <div id="<?php echo $prefix?>_text_format">
257  <?php echo nl2br($current_value['text_format']); ?>
258  </div>
259  <?php
260  } else {
261  $wysiwyg = $this->_createEditor($prefix);
262  $wysiwyg->set_contents($current_value['html_format']);
263  ?>
264  <div style="display: none" id="<?php echo $prefix?>_html_body_div">
265  <?php echo $wysiwyg->paint()?>
266  </div>
267  <div id="<?php echo $prefix?>_text_body_div">
268  <?php text_area($prefix.'_text_body', $current_value['text_format'], 105, 15); ?>
269  </div>
270  <?php
271  }
272  ?>
273  </td>
274  </tr>
275  </table>
276  </td>
277  </tr>
278  <?php if ($this->_edit_params['show_asset_to_send']) {?>
279  <tr>
280  <th><?php echo translate('asset_to_send'); ?></th>
281  <td>
282  <?php asset_finder($prefix.'_asset_to_send', $current_value['asset_to_send'], Array('page' => 'D')); ?><br/>
283  <?php echo translate('asset_to_send_desc'); ?>
284  </td>
285  </tr>
286  <tr>
287  <th><?php echo translate('design_to_apply'); ?></th>
288  <td>
289  <?php asset_finder($prefix.'_design_to_apply', $current_value['design_to_apply'], Array('design' => 'D')); ?><br/>
290  <?php echo translate('design_to_apply_desc'); ?>
291  </td>
292  </tr>
293  <tr>
294  <th><?php echo translate('layout_to_apply'); ?></th>
295  <td>
296  <?php asset_finder($prefix.'_layout_to_apply', $current_value['layout_to_apply'], Array('paint_layout_page' => 'D')); ?><br/>
297  <?php echo translate('layout_to_apply_desc'); ?>
298  </td>
299  </tr>
300  <?php } ?>
301  </table>
302  <?php
303 
304  }//end paint()
305 
306 
315  function process($prefix)
316  {
317  $prefix = str_replace(':', '_', $prefix);
318 
319  if (!isset($_REQUEST[$prefix.'_to_emails']) && !isset($_REQUEST[$prefix.'_to_assetids']) && !isset($_REQUEST[$prefix.'_from']) && !isset($_REQUEST[$prefix.'_subject']) && !isset($_REQUEST[$prefix.'_text_body'])) {
320  return FALSE;
321  }
322 
323  $value = Array();
324 
325  $wysiwyg = $this->_createEditor($prefix);
326  $value['html_format'] = trim($wysiwyg->process());
327  $value['text_format'] = (isset($_REQUEST[$prefix.'_text_body'])) ? $_REQUEST[$prefix.'_text_body'] : '';
328 
329  $ol = new Asset_Attribute_Option_List();
330  $ol->process($prefix.'_to_emails');
331  $value['to'] = explode("\r\n", $ol->value);
332 
333  // Remove any blanks from the 'to' list
334  for (reset($value['to']); NULL !== ($k = key($value['to'])); next($value['to'])) {
335  if (trim($value['to'][$k]) == '') {
336  unset($value['to'][$k]);
337  }
338  }
339 
340  $value['to_assetids'] = Array();
341  if (isset($_REQUEST[$prefix.'_to_assetids']) && is_array($_REQUEST[$prefix.'_to_assetids'])) {
342  foreach ($_REQUEST[$prefix.'_to_assetids'] as $new_to_info) {
343  if (!empty($new_to_info['assetid']) && assert_valid_assetid($new_to_info['assetid'], 'Invalid Asset Id: #'.$new_to_info['assetid'].' supplied to attribute"'.$this->name.'"', FALSE, FALSE)) {
344  $value['to_assetids'][] = $new_to_info['assetid'];
345  }
346  }
347  }
348 
349  $value['from'] = (isset($_REQUEST[$prefix.'_from'])) ? $_REQUEST[$prefix.'_from'] : '';
350  $value['reply_to'] = (isset($_REQUEST[$prefix.'_reply_to'])) ? $_REQUEST[$prefix.'_reply_to'] : '';
351  $value['subject'] = (isset($_REQUEST[$prefix.'_subject'])) ? $_REQUEST[$prefix.'_subject'] : '';
352  $value['inc_attachments'] = (isset($_REQUEST[$prefix.'_inc_attachments'])) ? $_REQUEST[$prefix.'_inc_attachments'] : FALSE;
353  $value['asset_to_send'] = (isset($_REQUEST[$prefix.'_asset_to_send']['assetid'])) ? $_REQUEST[$prefix.'_asset_to_send']['assetid'] : '0';
354  $value['design_to_apply'] = (isset($_REQUEST[$prefix.'_design_to_apply']['assetid'])) ? $_REQUEST[$prefix.'_design_to_apply']['assetid'] : '0';
355  $value['layout_to_apply'] = (isset($_REQUEST[$prefix.'_layout_to_apply']['assetid'])) ? $_REQUEST[$prefix.'_layout_to_apply']['assetid'] : '0';
356 
357  // from address is not specified, print a warning message
358  if (isset($value['to']) && !empty($value['to']) && empty($value['from'])) {
359  trigger_localised_error('CORE0251', E_USER_WARNING, $this->name);
360  }
361 
362  $this->processed = $this->setValue($value);
363 
364  }//end process()
365 
366 
375  function validateValue(&$value)
376  {
377  // cant serilise scalar variables
378  if (is_scalar($value)) {
379  // see if it is already serilised
380  $unser = @unserialize(trim((string) $value));
381  if (is_scalar($unser)) {
382  return FALSE;
383  } else {
384  $value = $unser;
385  }
386  }
387 
388  if (!is_array($value)) return FALSE;
389 
390  if (!isset($value['to'])) {
391  $value['to'] = Array();
392  }
393  if (!isset($value['to_assetids'])) {
394  $value['to_assetids'] = Array();
395  }
396  if (!isset($value['from'])) {
397  $value['from'] = '';
398  }
399  if (!isset($value['reply_to'])) {
400  $value['reply_to'] = '';
401  }
402  if (!isset($value['subject'])) {
403  $value['subject'] = '';
404  }
405  if (!isset($value['html_format'])) {
406  $value['html_format'] = '';
407  }
408  if (!isset($value['text_format'])) {
409  $value['text_format'] = '';
410  }
411  if (!isset($value['inc_attachments'])) {
412  $value['inc_attachments'] = Array();
413  }
414  if (!isset($value['attachments'])) {
415  $value['attachments'] = Array();
416  }
417  if (!isset($value['asset_to_send'])) {
418  $value['asset_to_send'] = '';
419  }
420  if (!isset($value['design_to_apply'])) {
421  $value['design_to_apply'] = '';
422  }
423  if (!isset($value['layout_to_apply'])) {
424  $value['layout_to_apply'] = '';
425  }
426 
427  $value = serialize($value);
428  return TRUE;
429 
430  }//end validateValue()
431 
432 
443  function sendMail($replacements=Array(), $additional_attachments=Array())
444  {
445  require_once SQ_FUDGE_PATH.'/general/text.inc';
446  require_once SQ_FUDGE_PATH.'/general/www.inc';
447 
448  $current_value = @unserialize($this->value);
449 
450  // replace keywords in all our values
451  $assetid_emails = Array();
452  if (isset($current_value['to_assetids']) && !empty($current_value['to_assetids'])) {
453  foreach ($current_value['to_assetids'] as $assetid) {
454  $addressee = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
455 
456  if (is_null($addressee)) continue;
457 
458  if ($addressee->type() == 'user_group') {
459  // Include directly and indirectly-linked shadow assets
460  $users = $GLOBALS['SQ_SYSTEM']->am->getChildren($addressee->id, 'user', FALSE, NULL, NULL, NULL, TRUE, NULL, NULL, FALSE, NULL);
461 
462  foreach ($users as $user_id => $type_code) {
463  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($user_id);
464  $assetid_emails[] = $user->attr('email');
465  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($user);
466  }
467  } else {
468  $assetid_emails[] = $addressee->attr('email');
469  }
470  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($addressee);
471  }
472  }
473 
474  // Attachments
475  $attachments = Array();
476  if (isset($current_value['inc_attachments']) && $current_value['inc_attachments'] && isset($current_value['attachments']) && !empty($current_value['attachments'])) {
477  foreach ($current_value['attachments'] as $file_asset_id) {
478  $file_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($file_asset_id);
479  $file_info = $file_asset->getExistingFile();
480  $attachments[] = $file_info['path'];
481  }
482  }
483 
484  $to = replace_keywords($current_value['to'], $replacements);
485  $from = replace_keywords($current_value['from'], $replacements);
486  $reply_to = replace_keywords($current_value['reply_to'], $replacements);
487  $subject = replace_keywords($current_value['subject'], $replacements);
488  $text_body = replace_keywords($current_value['text_format'], $replacements);
489  $html_body = replace_keywords($current_value['html_format'], $replacements);
490 
491  // Firefox fix for html body
492  if (trim($html_body) == '<br>') $html_body = '';
493  if (isset($current_value['asset_to_send']) && $current_value['asset_to_send'] != 0) {
494  $html_body = '';
495  $text_body = '';
496  // make an array of asset_id, design_id, and layout_id obtained from asset_picker in trigger and
497  // put then in an array to be passed to generateRawContent()
498  $details = Array(
499  'content_id' => $current_value['asset_to_send'],
500  'content_design' => $current_value['design_to_apply'],
501  'content_layout' => $current_value['layout_to_apply'],
502  );
503  $html_body = $GLOBALS['SQ_SYSTEM']->am->generateRawContent($details);
504 
505  $old_current_asset = $GLOBALS['SQ_SYSTEM']->getGlobalDefine('CURRENT_ASSET', NULL);
506  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $GLOBALS['SQ_SYSTEM']->am->getAsset($current_value['asset_to_send']));
507 
508  foreach ($to as &$to_str) {
509  replace_global_keywords($to_str);
510  }
511  replace_global_keywords($from);
512  replace_global_keywords($reply_to);
513  replace_global_keywords($subject);
514  replace_global_keywords($html_body);
515 
516  if (is_null($old_current_asset)) {
517  $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
518  } else {
519  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $old_current_asset);
520  }
521 
522  } else {
523  // replace internal links to pages and images with their full URLs
524  $matches = Array();
525  preg_match_all('|\./\?a=([0-9]+)|', $html_body, $matches);
526  $urls = $GLOBALS['SQ_SYSTEM']->am->getAssetURL(array_unique($matches[1]));
527  foreach ($urls as $assetid => $url) {
528  $html_body = preg_replace('|\./\?a='.$assetid.'([^0-9])|', $url.'\\1', $html_body);
529  }
530 
531  foreach ($to as &$to_str) {
532  replace_global_keywords($to_str);
533  }
534  replace_global_keywords($from);
535  replace_global_keywords($reply_to);
536  replace_global_keywords($subject);
537  replace_global_keywords($text_body);
538  replace_global_keywords($html_body);
539 
540  }
541 
542  $to = array_unique(array_merge($to, $assetid_emails));
543 
544  // if no 'From:' address (possibly because of empty keyword replacement)
545  // send from the system's default email address
546  if (trim($from) == '') $from = SQ_CONF_DEFAULT_EMAIL;
547 
548  // can't come up with a From: email address - bail for this email
549  if (empty($from)) {
550  trigger_localised_error('SYS0315', E_USER_WARNING);
551  return;
552  }
553 
554  require_once 'Mail.php';
555  require_once 'Mail/mime.php';
556 
557  // new line is \n
558  $crlf = "\n";
559  $mime = new Mail_mime($crlf);
560 
561  $from_addr = preg_replace('|.*<(.*)>|', '\\1', $from);
562 
563  $tmp_mail = new Mail();
564  $mail = $tmp_mail->factory('mail', "-f$from_addr");
565 
566  $hdrs = Array(
567  'From' => $from,
568  'Subject' => $subject,
569  );
570 
571  $reply_to = trim($reply_to);
572  if (!empty($reply_to)) $hdrs['Reply-To'] = $reply_to;
573 
574  foreach ($to as $to_email) {
575 
576  $to_email = trim($to_email);
577 
578  $to_addr = preg_replace('|.*<(.*)>|', '\\1', $to_email);
579 
580  if (empty($to_email) || !valid_email($to_addr)) {
581  continue;
582  }
583 
584  // Unset the $mime->_parts (which holds all the attachments for the email)
585  // This was not done before, causing an incremental attachment of files
586  // for multiple recipients (please see Bug #2897)
587  if (count($mime->_parts) > 0) unset($mime->_parts);
588 
589  foreach ($attachments as $file_path) {
590  if (file_exists($file_path)) {
591  $mime_type = file_mime_type($file_path);
592  @$mime->addAttachment($file_path, $mime_type); // throws PHP5 strict warning (Non-static method PEAR::isError should not be called statically)
593  }
594  }
595 
596  // ADditional attachments.
597  foreach ($additional_attachments as $file_path) {
598  if (file_exists($file_path) && (strpos(realpath($file_path), SQ_DATA_PATH) === 0)) {
599  $mime_type = file_mime_type($file_path);
600  @$mime->addAttachment($file_path, $mime_type); // throws PHP5 strict warning
601  }
602  }
603 
604  $mime->setTXTBody($text_body);
605  $mime->setHTMLBody($html_body);
606 
607  // get() must be called before headers()
608  // set up the charsets, use matrix preferences
609  $params = Array(
610  'html_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
611  'text_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
612  'head_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
613  );
614  $body = $mime->get($params);
615 
616  $hdrs = $mime->headers($hdrs);
617 
618  $msg = @$mail->send($to_email, $hdrs, $body);
619 
620  }//end foreach
621 
622  }//end sendMail()
623 
624 
631  function getKeywords()
632  {
633  $current_value = @unserialize($this->value);
634  $keyword_fields = Array(
635  'to',
636  'from',
637  'reply_to',
638  'subject',
639  'text_format',
640  'html_format',
641  );
642 
643  $res = Array();
644  foreach ($keyword_fields as $field) {
645  $field_value = array_get_index($current_value, $field, '');
646  if (!empty($field_value)) {
647  if (is_array($field_value)) {
648  $value = Array();
649  foreach ($field_value as $idx => $val) {
650  $value[$idx] = retrieve_keywords_replacements($val);
651  $res = array_merge($res, $value[$idx]);
652  }
653  } else {
654  $value = retrieve_keywords_replacements($field_value);
655  $res = array_merge($res, $value);
656  }
657  }
658  }
659 
660  return $res;
661 
662  }//end getKeywords()
663 
664 
665 }//end class
666 
667 ?>