-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
256 lines (210 loc) · 9.65 KB
/
Copy pathplugin.php
File metadata and controls
256 lines (210 loc) · 9.65 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
This is the MailContact plugin.
This file contains the MailContact plugin. It provides a mail submission
feature and a shortcode for the display of a contact form.
@package urlaube\mailcontact
@version 0.1a12
@author Yahe <hello@yahe.sh>
@since 0.1a0
*/
// ===== DO NOT EDIT HERE =====
// prevent script from getting called directly
if (!defined("URLAUBE")) { die(""); }
// use PHPMailer classes
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
class MailContact extends BaseSingleton implements Handler, Plugin {
// CONSTANTS
const REGEX = "~\/mailcontact\/~";
// INTERFACE FUNCTIONS
public static function getContent($metadata, &$pagecount) {
return null;
}
public static function getUri($metadata) {
return value(Main::class, ROOTURI)."mailcontact".US;
}
public static function parseUri($uri) {
$result = null;
$metadata = preparecontent(parseuri($uri, static::REGEX));
if ($metadata instanceof Content) {
$result = $metadata;
}
return $result;
}
// HELPER FUNCTIONS
protected static function configure() {
// captcha configuration
Plugins::preset("mailcontact_question", t("Wähle den Begriff, der nicht passt: Freund, Feind, Nudelsuppe", static::class));
Plugins::preset("mailcontact_answer", t("Nudelsuppe", static::class));
// SMTP configuration
Plugins::preset("mailcontact_host", "localhost");
Plugins::preset("mailcontact_password", "");
Plugins::preset("mailcontact_port", 587);
Plugins::preset("mailcontact_recipient", "root@localhost");
Plugins::preset("mailcontact_sender", "urlaube@localhost");
Plugins::preset("mailcontact_subject", t("Nachricht gesendet über MailContact", static::class));
Plugins::preset("mailcontact_username", "anonymous");
}
protected static function getForm($content) {
$result = $content;
if (is_string($result)) {
// generate form source code
$form = tfhtml("<link href=\"%s\" rel=\"stylesheet\">".NL.
"<p id=\"mailcontact-failure\"></p>".NL.
"<p id=\"mailcontact-success\"></p>".NL.
"<form action=\"%s\" id=\"mailcontact\" method=\"post\">".NL.
" <p class=\"mailcontact-author\">".NL.
" <label for=\"mailcontact-author\">%s*</label><br>".NL.
" <input id=\"mailcontact-author\" name=\"author\" required=\"required\" type=\"text\">".NL.
" </p>".NL.
" <p class=\"mailcontact-email\">".NL.
" <label for=\"mailcontact-email\">%s*</label><br>".NL.
" <input id=\"mailcontact-email\" name=\"email\" required=\"required\" type=\"email\">".NL.
" </p>".NL.
" <p class=\"mailcontact-message\">".NL.
" <label for=\"mailcontact-message\">%s*</label><br>".NL.
" <textarea autocomplete=\"nope\" id=\"mailcontact-message\" name=\"message\" required=\"required\"></textarea>".NL.
" </p>".NL.
" <p class=\"mailcontact-captcha\">".NL.
" <label for=\"mailcontact-captcha\">(%s*) %s</label><br>".NL.
" <input autocomplete=\"nope\" id=\"mailcontact-captcha\" name=\"captcha\" required=\"required\" type=\"text\">".NL.
" </p>".NL.
" <p class=\"mailcontact-gdpr\">".NL.
" <span class=\"mailcontact-gdpr-label\">%s</span><br>".NL.
" %s".NL.
" </p>".NL.
" <div class=\"alert alert-danger\" id=\"mailcontact-failure-alert\">%s</div>".NL.
" <div class=\"alert alert-success\" id=\"mailcontact-success-alert\">%s</div>".NL.
" <p class=\"mailcontact-submit\">".NL.
" <input name=\"referer\" type=\"hidden\" value=\"%s\">".NL.
" <input id=\"mailcontact-submit\" name=\"submit\" type=\"submit\" value=\"%s\">".NL.
" </p>".NL.
" <p class=\"mailcontact-info\">%s</p>".NL.
"</form>".NL.
"<script src=\"%s\"></script>",
static::class,
path2uri(__DIR__."/css/style.css"),
static::getUri(new Content()),
"Name",
"E-Mail",
"Nachricht",
"Captcha",
value(Plugins::class, "mailcontact_question"),
"Datenschutzerklärung",
"[DATENSCHUTZERKLÄRUNG]",
"Der Versand ist fehlgeschlagen!",
"Der Versand war erfolgreich!",
value(Main::class, URI),
"Anfrage absenden",
"Pflichtfelder sind mit * markiert.",
path2uri(__DIR__."/js/script.js"));
// replace shortcode with form
$result = str_ireplace("[mailcontact]", $form, $result);
}
return $result;
}
protected static function sendMail($author, $email, $message, $via) {
$phpmailer = new PHPMailer();
// configure mail server
$phpmailer->isSMTP();
$phpmailer->Host = value(Plugins::class, "mailcontact_host");
$phpmailer->Port = value(Plugins::class, "mailcontact_port");
// configure authentication
$phpmailer->SMTPAuth = true;
$phpmailer->Username = value(Plugins::class, "mailcontact_username");
$phpmailer->Password = value(Plugins::class, "mailcontact_password");
// configure sender and receiver
$phpmailer->setFrom(value(Plugins::class, "mailcontact_sender"));
$phpmailer->addAddress(value(Plugins::class, "mailcontact_recipient"));
// configure content
$phpmailer->isHTML(false);
$phpmailer->Body = sprintf("%s <%s> %s %s".NL.
NL.
"%s",
$author,
$email,
t("gesendet über", static::class),
$via,
$message);
$phpmailer->CharSet = value(Main::class, CHARSET);
$phpmailer->Subject = value(Plugins::class, "mailcontact_subject");
return ($phpmailer->send());
}
// RUNTIME FUNCTIONS
public static function handler() {
$result = false;
// preset plugin configuration
static::configure();
$metadata = static::parseUri(relativeuri());
if (null !== $metadata) {
// check if the URI is correct
$fixed = static::getUri($metadata);
if (0 !== strcmp(value(Main::class, URI), $fixed)) {
relocate($fixed.querystring(), false, true);
// we handled this page
$result = true;
} else {
// check if the request comes from the website itself
if (isset($_POST["referer"]) && isset($_SERVER["HTTP_REFERER"])) {
if (0 === strcmp(absoluteurl($_POST["referer"]), $_SERVER["HTTP_REFERER"])) {
// at least we can handle this request
$success = false;
// check if the captcha is correct
if (isset($_POST["captcha"])) {
if (0 === strcasecmp(value(Plugins::class, "mailcontact_answer"), trim($_POST["captcha"]))) {
// check if all mandatory fields are given
if (isset($_POST["author"]) && isset($_POST["email"]) && isset($_POST["message"])) {
// handle message
$success = static::sendMail($_POST["author"], $_POST["email"], $_POST["message"], $_SERVER["HTTP_REFERER"]);
}
}
}
// redirect to previous page
if ($success) {
relocate($_POST["referer"]."#mailcontact-success", false, false);
} else {
relocate($_POST["referer"]."#mailcontact-failure", false, false);
}
// we handled this page
$result = true;
}
}
}
}
return $result;
}
public static function plugin($content) {
$result = $content;
// preset plugin configuration
static::configure();
if ($result instanceof Content) {
if ($result->isset(CONTENT)) {
$result->set(CONTENT, static::getForm(value($result, CONTENT)));
}
} else {
if (is_array($result)) {
// iterate through all content items
foreach ($result as $result_item) {
if ($result_item instanceof Content) {
if ($result_item->isset(CONTENT)) {
$result_item->set(CONTENT, static::getForm(value($result_item, CONTENT)));
}
}
}
}
}
return $result;
}
}
// include PHPMailer
require_once(__DIR__."/vendors/phpmailer/Exception.php");
require_once(__DIR__."/vendors/phpmailer/PHPMailer.php");
require_once(__DIR__."/vendors/phpmailer/SMTP.php");
// register handler
Handlers::register(MailContact::class, "handler", MailContact::REGEX, [POST], USER);
// register plugin
Plugins::register(MailContact::class, "plugin", FILTER_CONTENT);
// register translation
Translate::register(__DIR__.DS."lang".DS, MailContact::class);