This repository has been archived by the owner on Dec 18, 2021. It is now read-only.
/
class.rex_xform_emailtemplate.inc.php
123 lines (103 loc) · 3.83 KB
/
class.rex_xform_emailtemplate.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* XForm
* @author jan.kristinus[at]redaxo[dot]org Jan Kristinus
* @author <a href="http://www.yakamara.de">www.yakamara.de</a>
*/
class rex_xform_emailtemplate
{
static function getTemplate($name)
{
global $REX;
$gt = rex_sql::factory();
$gt->setQuery('select * from ' . $REX['TABLE_PREFIX'] . 'xform_email_template where name="' . mysql_real_escape_string($name) . '"');
if ($gt->getRows() == 1) {
$b = $gt->getArray();
return current($b);
}
return false;
}
static function replaceVars($template, $er = array())
{
global $REX;
$r = rex_register_extension_point('XFORM_EMAIL_BEFORE_REPLACEVARS', array(
'template' => $template,
'search_replace' => $er,
'status' => false
)
);
$template = $r['template'];
$er = $r['search_replace'];
$status = $r['status'];
if ($status) {
return true;
}
$er['REX_SERVER'] = $REX['SERVER'];
$er['REX_ERROR_EMAIL'] = $REX['ERROR_EMAIL'];
$er['REX_SERVERNAME'] = $REX['SERVERNAME'];
$er['REX_NOTFOUND_ARTICLE_ID'] = $REX['NOTFOUND_ARTICLE_ID'];
$er['REX_ARTICLE_ID'] = $REX['ARTICLE_ID'];
foreach ($er as $search => $replace) {
foreach ($template as $k => $v) {
$template[$k] = str_replace('###' . $search . '###', $replace, $template[$k]);
$template[$k] = str_replace('***' . $search . '***', urlencode($replace), $template[$k]);
$template[$k] = str_replace('+++' . $search . '+++', self::makeSingleLine($replace), $template[$k]);
$template[$k] = preg_replace_callback(
'@redaxo://(\d+)(?:-(\d+))?/?@i',
create_function(
'$matches',
'return rex_getUrl($matches[1], isset($matches[2]) ? $matches[2] : ' . $REX['CUR_CLANG'] . ');'
),
$template[$k]
);
}
}
return $template;
}
static function makeSingleLine($str)
{
$str = str_replace("\n", '', $str);
$str = str_replace("\r", '', $str);
return $str;
}
static function sendMail($template, $template_name = '')
{
$r = rex_register_extension_point('XFORM_EMAIL_BEFORE_SEND', array(
'template' => $template,
'template_name' => $template_name,
'status' => false
)
);
$template = $r['template'];
$template_name = $r['template_name'];
$status = $r['status'];
if ($status) {
return true;
}
$mail = new rex_mailer();
$mail->AddAddress($template['mail_to'], $template['mail_to_name']);
$mail->SetFrom($template['mail_from'], $template['mail_from_name']);
$mail->Subject = $template['subject'];
$mail->Body = $template['body'];
if ($template['body_html'] != '') {
$mail->AltBody = $template['body'];
$mail->MsgHTML($template['body_html']);
} else {
$mail->Body = strip_tags($template['body']);
}
if (is_array($template['attachments'])) {
foreach ($template['attachments'] as $f) {
$mail->AddAttachment($f['path'], $f['name']);
}
}
if ($mail->Send()) {
$template['email_subject'] = $template['subject'];
rex_register_extension_point('XFORM_EMAIL_SENT', $template_name, $template, true); // read only
return true;
} else {
$template['email_subject'] = $template['subject'];
rex_register_extension_point('XFORM_EMAIL_SENT_FAILED', $template_name, $template, true); // read only
return false;
}
}
}