18 require_once SQ_ATTRIBUTES_PATH.
'/wysiwyg/wysiwyg.inc';
19 require_once SQ_ATTRIBUTES_PATH.
'/option_list/option_list.inc';
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;
70 if (!parent::setEditParams($node))
return FALSE;
71 foreach ($this->_edit_params as $field => $val) {
72 if (isset($node->attributes()->$field)) {
75 $this->_edit_params[$field] = (bool)(
int)$node->attributes()->$field;
92 function paint($prefix, $read_only=FALSE)
94 $prefix = str_replace(
':',
'_', $prefix);
96 $current_value = @unserialize($this->value);
97 if (!is_array($current_value)) {
98 $current_value = Array();
100 if (!isset($current_value[
'to'])) {
101 $current_value[
'to'] = Array();
103 if (!isset($current_value[
'to_assetids'])) {
104 $current_value[
'to_assetids'] = Array();
106 if (!isset($current_value[
'from'])) {
107 $current_value[
'from'] =
'';
109 if (!isset($current_value[
'reply_to'])) {
110 $current_value[
'reply_to'] =
'';
112 if (!isset($current_value[
'subject'])) {
113 $current_value[
'subject'] =
'';
115 if (!isset($current_value[
'html_format'])) {
116 $current_value[
'html_format'] =
'';
118 if (!isset($current_value[
'text_format'])) {
119 $current_value[
'text_format'] =
'';
121 if (!isset($current_value[
'inc_attachments'])) {
122 $current_value[
'inc_attachments'] = Array();
124 if (!isset($current_value[
'attachments'])) {
125 $current_value[
'attachments'] = Array();
127 if (!isset($current_value[
'asset_to_send'])) {
128 $current_value[
'asset_to_send'] =
'';
130 if (!isset($current_value[
'design_to_apply'])) {
131 $current_value[
'design_to_apply'] =
'';
133 if (!isset($current_value[
'layout_to_apply'])) {
134 $current_value[
'layout_to_apply'] =
'';
137 <script
type=
"text/javascript" src=
"<?php echo sq_web_path('lib'); ?>/js/email_format.js"></script>
138 <table border=
"0" class=
"email-format">
140 if ($this->_edit_params[
'show_to']) {
143 <th><?php echo translate(
'to'); ?></th>
147 $ol->value = implode(
"\r\n", $current_value[
'to']);
148 $ol->setEditParam(
'width',
'50');
149 $ol->paint($prefix.
'_to_emails', $read_only);
151 if ($this->_edit_params[
'with_asset_finder']) {
153 foreach ($current_value[
'to_assetids'] as $assetid) {
154 echo get_asset_tag_line($assetid).
'<br />';
157 multiple_asset_finder($prefix.
'_to_assetids', $current_value[
'to_assetids'], Array(
'user' =>
'D',
'user_group' =>
'I',
'bulkmail_user' =>
'I'));
165 if ($this->_edit_params[
'show_from']) {
168 <th><?php echo translate(
'from'); ?></th>
172 echo htmlspecialchars($current_value[
'from']);
174 text_box($prefix.
'_from', $current_value[
'from'], 50);
181 if ($this->_edit_params[
'show_reply_to']) {
184 <th><?php echo translate(
'reply_to'); ?></th>
188 echo htmlspecialchars($current_value[
'reply_to']);
190 text_box($prefix.
'_reply_to', $current_value[
'reply_to'], 50);
199 <th><?php echo translate(
'subject'); ?></th>
203 echo htmlspecialchars($current_value[
'subject']);
205 text_box($prefix.
'_subject', $current_value[
'subject'], 50);
211 if ($this->_edit_params[
'show_inc_attachments']) {
214 <th><?php echo translate(
'attachments'); ?></th>
218 echo
'<img src="'.sq_web_path(
'lib').
'/web/images/'.($current_value[
'inc_attachments'] ?
'tick' :
'cross').
'.gif" width="15" height="15" /> ';
219 echo translate(
'include_file_attachments');
221 check_box($prefix.
'_inc_attachments', TRUE, $current_value[
'inc_attachments']);
222 label(translate(
'include_file_attachments'), $prefix.
'_inc_attachments');
231 <th><?php echo translate(
'body'); ?></th>
233 <table border=
"0" class=
"email-format-body">
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')).
'\')
"');
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')).
'\')
"');
244 <td id="<?php echo $prefix; ?>_format_mode
" class="email-format-version
">
245 <?php echo translate('text_email_version'); ?>
253 <div style="display: none
" id="<?php echo $prefix?>_html_format
">
254 <?php echo $current_value['html_format']; ?>
256 <div id="<?php echo $prefix?>_text_format
">
257 <?php echo nl2br($current_value['text_format']); ?>
261 $wysiwyg = $this->_createEditor($prefix);
262 $wysiwyg->set_contents($current_value['html_format']);
264 <div style="display: none
" id="<?php echo $prefix?>_html_body_div
">
265 <?php echo $wysiwyg->paint()?>
267 <div id="<?php echo $prefix?>_text_body_div
">
268 <?php text_area($prefix.'_text_body', $current_value['text_format'], 105, 15); ?>
278 <?php if ($this->_edit_params['show_asset_to_send']) {?>
280 <th><?php echo translate('asset_to_send'); ?></th>
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'); ?>
287 <th><?php echo translate('design_to_apply'); ?></th>
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'); ?>
294 <th><?php echo translate('layout_to_apply'); ?></th>
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'); ?>
315 function process($prefix)
317 $prefix = str_replace(':', '_', $prefix);
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'])) {
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'] : '';
329 $ol = new Asset_Attribute_Option_List();
330 $ol->process($prefix.'_to_emails');
331 $value['to'] = explode("\r\n
", $ol->value);
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]);
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'];
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';
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);
362 $this->processed = $this->setValue($value);
375 function validateValue(&$value)
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)) {
388 if (!is_array($value)) return FALSE;
390 if (!isset($value['to'])) {
391 $value['to'] = Array();
393 if (!isset($value['to_assetids'])) {
394 $value['to_assetids'] = Array();
396 if (!isset($value['from'])) {
399 if (!isset($value['reply_to'])) {
400 $value['reply_to'] = '';
402 if (!isset($value['subject'])) {
403 $value['subject'] = '';
405 if (!isset($value['html_format'])) {
406 $value['html_format'] = '';
408 if (!isset($value['text_format'])) {
409 $value['text_format'] = '';
411 if (!isset($value['inc_attachments'])) {
412 $value['inc_attachments'] = Array();
414 if (!isset($value['attachments'])) {
415 $value['attachments'] = Array();
417 if (!isset($value['asset_to_send'])) {
418 $value['asset_to_send'] = '';
420 if (!isset($value['design_to_apply'])) {
421 $value['design_to_apply'] = '';
423 if (!isset($value['layout_to_apply'])) {
424 $value['layout_to_apply'] = '';
427 $value = serialize($value);
430 }//end validateValue()
443 function sendMail($replacements=Array(), $additional_attachments=Array())
445 require_once SQ_FUDGE_PATH.'/general/text.inc';
446 require_once SQ_FUDGE_PATH.'/general/www.inc';
448 $current_value = @unserialize($this->value);
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);
456 if (is_null($addressee)) continue;
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);
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);
468 $assetid_emails[] = $addressee->attr('email');
470 $GLOBALS['SQ_SYSTEM']->am->forgetAsset($addressee);
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'];
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);
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) {
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()
499 'content_id' => $current_value['asset_to_send'],
500 'content_design' => $current_value['design_to_apply'],
501 'content_layout' => $current_value['layout_to_apply'],
503 $html_body = $GLOBALS['SQ_SYSTEM']->am->generateRawContent($details);
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']));
508 foreach ($to as &$to_str) {
509 replace_global_keywords($to_str);
511 replace_global_keywords($from);
512 replace_global_keywords($reply_to);
513 replace_global_keywords($subject);
514 replace_global_keywords($html_body);
516 if (is_null($old_current_asset)) {
517 $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
519 $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $old_current_asset);
523 // replace internal links to pages and images with their full URLs
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);
531 foreach ($to as &$to_str) {
532 replace_global_keywords($to_str);
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);
542 $to = array_unique(array_merge($to, $assetid_emails));
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;
548 // can't come up with a From: email address - bail for this email
550 trigger_localised_error('SYS0315', E_USER_WARNING);
554 require_once 'Mail.php';
555 require_once 'Mail/mime.php';
559 $mime = new Mail_mime($crlf);
561 $from_addr = preg_replace('|.*<(.*)>|', '\\1', $from);
563 $tmp_mail = new Mail();
564 $mail = $tmp_mail->factory('mail', "-f$from_addr
");
568 'Subject' => $subject,
571 $reply_to = trim($reply_to);
572 if (!empty($reply_to)) $hdrs['Reply-To'] = $reply_to;
574 foreach ($to as $to_email) {
576 $to_email = trim($to_email);
578 $to_addr = preg_replace('|.*<(.*)>|', '\\1', $to_email);
580 if (empty($to_email) || !valid_email($to_addr)) {
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);
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)
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
604 $mime->setTXTBody($text_body);
605 $mime->setHTMLBody($html_body);
607 // get() must be called before headers()
608 // set up the charsets, use matrix preferences
610 'html_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
611 'text_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
612 'head_charset' => SQ_CONF_DEFAULT_CHARACTER_SET,
614 $body = $mime->get($params);
616 $hdrs = $mime->headers($hdrs);
618 $msg = @$mail->send($to_email, $hdrs, $body);
631 function getKeywords()
633 $current_value = @unserialize($this->value);
634 $keyword_fields = 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)) {
649 foreach ($field_value as $idx => $val) {
650 $value[$idx] = retrieve_keywords_replacements($val);
651 $res = array_merge($res, $value[$idx]);
654 $value = retrieve_keywords_replacements($field_value);
655 $res = array_merge($res, $value);