dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxContactForm.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxContact;
3
4require_once __DIR__ . '/dbxContactConfig.class.php';
5require_once __DIR__ . '/dbxContactTicket.class.php';
6
8
9 private $dd = 'dbxContact|contactRequest';
10
11 private function h($value) {
12 return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
13 }
14
15 private function mail_profile_options(array $extra = array()) {
16 $profile = trim((string) dbx()->get_config('dbxContact', 'mail_profile'));
17 if ($profile !== '') {
18 $extra['mail_profile'] = $profile;
19 }
20
21 return $extra;
22 }
23
24 private function mail_from_param() {
25 $from = trim((string) dbx()->get_config('dbxContact', 'mail_from'));
26 $fromName = trim((string) dbx()->get_config('dbxContact', 'mail_from_name'));
27 return ($from !== '') ? array('email' => $from, 'name' => $fromName) : '';
28 }
29
30 private function spam_reason(array $data): string {
31 $mail = dbx()->get_system_obj('dbxMail');
32 if (!is_object($mail) || !method_exists($mail, 'spam_reason_for_text')) {
33 return '';
34 }
35
36 $text = implode("\n", array(
37 (string) ($data['name'] ?? ''),
38 (string) ($data['email'] ?? ''),
39 (string) ($data['phone'] ?? ''),
40 (string) ($data['subject'] ?? ''),
41 (string) ($data['message'] ?? ''),
42 ));
43
44 return (string) $mail->spam_reason_for_text($text);
45 }
46
47 private function mail_request(array $data, int $rid) {
48 $to = trim((string) dbx()->get_config('dbxContact', 'mail_to'));
49 if ($to === '') {
50 return false;
51 }
52
53 $prefix = trim((string) dbx()->get_config('dbxContact', 'mail_subject_prefix'));
54 $subject = trim($prefix . ': ' . ($data['subject'] ?? ''));
55 if ($rid > 0) {
56 $subject .= ' #' . $rid;
57 }
58
59 $tpl = dbx()->get_system_obj('dbxTPL');
60 $html = $tpl->get_tpl('dbxContact|mail-contact-request', array(
61 'name' => $this->h($data['name'] ?? ''),
62 'email' => $this->h($data['email'] ?? ''),
63 'phone' => $this->h($data['phone'] ?? ''),
64 'subject' => $this->h($data['subject'] ?? ''),
65 'message' => $this->h($data['message'] ?? ''),
66 ));
67
68 $text = "Neue Kontaktanfrage #" . $rid . "\n\n"
69 . "Name: " . ($data['name'] ?? '') . "\n"
70 . "E-Mail: " . ($data['email'] ?? '') . "\n"
71 . "Telefon: " . ($data['phone'] ?? '') . "\n"
72 . "Betreff: " . ($data['subject'] ?? '') . "\n\n"
73 . ($data['message'] ?? '');
74
75 $options = $this->mail_profile_options(array(
76 'reply_to' => array('email' => (string) ($data['email'] ?? ''), 'name' => (string) ($data['name'] ?? '')),
77 'text' => $text,
78 ));
79
80 return dbx()->send_mail($this->mail_from_param(), $to, $subject, $html, 'html', array(), $options);
81 }
82
83 private function mail_confirm(array $data, int $rid) {
84 $to = trim((string) ($data['email'] ?? ''));
85 if ($to === '') {
86 return false;
87 }
88
89 $subject = trim((string) dbx()->get_config('dbxContact', 'mail_confirm_subject'));
90 if ($subject === '') {
91 $subject = 'Ihre Kontaktanfrage';
92 }
93 if ($rid > 0) {
94 $subject .= ' #' . $rid;
95 }
96
97 $phone = trim((string) ($data['phone'] ?? ''));
98
99 $tpl = dbx()->get_system_obj('dbxTPL');
100 $html = $tpl->get_tpl('dbxContact|mail-contact-confirm', array(
101 'rid' => (int) $rid,
102 'name' => $this->h($data['name'] ?? ''),
103 'phone' => $this->h($phone !== '' ? $phone : '-'),
104 'subject' => $this->h($data['subject'] ?? ''),
105 'message' => $this->h($data['message'] ?? ''),
106 ));
107
108 $text = "Ihre Anfrage ist eingegangen\n\n"
109 . "Hallo " . ($data['name'] ?? '') . ",\n\n"
110 . "vielen Dank fuer Ihre Nachricht. Wir haben Ihre Kontaktanfrage unter der Nummer #"
111 . $rid . " erhalten und werden sie bearbeiten.\n\n"
112 . "Betreff: " . ($data['subject'] ?? '') . "\n"
113 . "Telefon: " . ($phone !== '' ? $phone : '-') . "\n\n"
114 . ($data['message'] ?? '') . "\n\n"
115 . "Wir melden uns so bald wie moeglich bei Ihnen. Sie muessen nichts weiter unternehmen.";
116
117 return dbx()->send_mail(
118 $this->mail_from_param(),
119 $to,
120 $subject,
121 $html,
122 'html',
123 array(),
124 $this->mail_profile_options(array('text' => $text))
125 );
126 }
127
128 private function current_user_defaults() {
129 $uid = (int) dbx()->user();
130 $data = array(
131 'name' => '',
132 'email' => '',
133 'phone' => '',
134 'subject' => '',
135 'message' => '',
136 );
137
138 if ($uid > 0) {
139 $name = dbx()->user('name');
140 $email = dbx()->user('email');
141
142 if ($name !== 'undef') {
143 $data['name'] = (string) $name;
144 }
145
146 if ($email !== 'undef') {
147 $data['email'] = (string) $email;
148 }
149 }
150
151 return $data;
152 }
153
154 private function my_requests_button() {
155 if ((int) dbx()->user() <= 0) {
156 return '';
157 }
158
159 return dbx()->get_system_obj('dbxTPL')->get_tpl('dbxContact|contact-my-requests-button');
160 }
161
162 private function snapshot_submitted_data($oForm) {
163 $fields = array(
164 'name' => 'words|min=2|max=160',
165 'email' => 'email|max=180',
166 'phone' => 'varchar|max=80',
167 'subject' => 'varchar|min=3|max=220',
168 'message' => '*|min=8|max=5000',
169 );
170 $data = array();
171
172 foreach ($fields as $name => $rules) {
173 $value = trim((string) $oForm->get_post($name, '', $rules));
174 $data[$name] = $value;
175 $oForm->set_post($name, $value);
176 }
177
178 return $data;
179 }
180
181 private function confirm_notes(array $data, int $rid, ?bool $confirmMailOk) {
182 $notes = array();
183 $notes[] = 'Ihre Nachricht ist bei uns eingegangen und wird bearbeitet.';
184 $notes[] = 'Ihre Anfragenummer: <strong>#' . (int) $rid . '</strong>.';
185
186 $email = trim((string) ($data['email'] ?? ''));
188 if ($confirmMailOk === true && $email !== '') {
189 $notes[] = 'Eine Bestaetigung wurde an <strong>' . $this->h($email) . '</strong> gesendet.';
190 } elseif ($confirmMailOk === false && $email !== '') {
191 $notes[] = 'Die Bestaetigungs-E-Mail konnte gerade nicht versendet werden. Ihre Anfrage ist dennoch gespeichert.';
192 } elseif ($email !== '') {
193 $notes[] = 'Sie erhalten in Kuerze eine Bestaetigung an <strong>' . $this->h($email) . '</strong>.';
194 }
195 }
196
197 $notes[] = 'Wir melden uns so bald wie moeglich bei Ihnen.';
198
199 $html = '';
200 foreach ($notes as $note) {
201 $html .= '<li>' . $note . '</li>';
202 }
203
204 return $html;
205 }
206
207 private function render_confirm(array $data, int $rid, ?bool $confirmMailOk) {
208 $tpl = dbx()->get_system_obj('dbxTPL');
209 $phone = trim((string) ($data['phone'] ?? ''));
210
211 $html = $tpl->get_tpl('dbxContact|contact-confirm', array(
212 'rid' => (int) $rid,
213 'name' => $this->h($data['name'] ?? ''),
214 'email' => $this->h($data['email'] ?? ''),
215 'phone' => $this->h($phone !== '' ? $phone : '-'),
216 'subject' => $this->h($data['subject'] ?? ''),
217 'message' => $this->h($data['message'] ?? ''),
218 'confirm_lead' => 'Vielen Dank! Ihre Kontaktanfrage wurde uebernommen.',
219 'confirm_notes' => $this->confirm_notes($data, $rid, $confirmMailOk),
220 'my_requests' => $this->my_requests_button(),
221 'frame_id' => 'dbx_contact_confirm',
222 'frame_panel_class' => 'dbxForm_wrapper dbx-contact dbx-contact-confirm-panel',
223 'frame_panel_attrs' => '',
224 'frame_subbar' => '',
225 'frame_form_open' => '',
226 'frame_form_close' => '',
227 'frame_body_class' => '',
228 'frame_body_head' => '',
229 'frame_body_tail' => '',
230 'bar_class' => 'dbx-module-bar',
231 'bar_title_class' => 'dbx-module-bar-titleblock',
232 'bar_actions_class' => 'dbx-module-bar-actions',
233 'bar_title' => 'Anfrage gesendet',
234 'bar_icon' => 'bi-check2-circle',
235 'bar_subtitle' => 'Zusammenfassung Ihrer Nachricht',
236 'bar_title_pre' => '',
237 'bar_title_heading_attrs' => '',
238 'bar_middle' => '',
239 'bar_extra' => '',
240 'bar_actions' => $tpl->get_tpl('dbxContact|contact-confirm-bar-actions', array(
241 'my_requests' => $this->my_requests_button(),
242 )),
243 ));
244
245 return str_replace('[dbx:js]', '', $html);
246 }
247
248 public function run() {
249 $oForm = dbx()->get_system_obj('dbxForm');
250 $oDB = dbx()->get_system_obj('dbxDB');
251 $successContent = '';
252
253 $oForm->init('dbxContact_form', 'contact-form');
254 $oForm->set_editor_class_file(__FILE__);
255 $oForm->_dd = $this->dd;
256 $oForm->_fd = 'dbxContact|contact-form';
257 $oForm->_action = '?dbx_modul=dbxContact&dbx_run1=form';
258 $oForm->add_module_bar(
259 'Kontaktformular',
260 'bi-envelope-paper',
261 'Nachricht an uns senden'
262 );
263 $oForm->add_rep('bar_class', 'dbx-module-bar');
264 $oForm->add_rep('frame_panel_class', 'dbxForm_wrapper dbx-contact');
265 $oForm->prepare_form_shell(array('class' => 'dbx-contact-form'));
266 $oForm->add_rep('bar_actions', '{obj:contact_bar_actions}');
267 $oForm->add_obj('contact_bar_actions', 'dbxContact|contact-form-bar-actions', array(
268 'bar_form_id' => 'dbx_form_' . (int) $oForm->_next_i,
269 ));
270 $oForm->_data = $this->current_user_defaults();
271 $oForm->_msg_info = '';
272 $oForm->_msg_success = '';
273 $oForm->_msg_error = '';
274 $oForm->_msg_warning = '';
275 $oForm->_try_reset = 6;
276 $oForm->_try_max = 5;
277 $oForm->_try_msg = 'Zu viele fehlerhafte Versuche. Bitte warten Sie {sec} Sekunden und versuchen Sie es dann erneut.';
278
279 $oForm->add_fld('name');
280 $oForm->add_fld('email');
281 $oForm->add_fld('phone');
282 $oForm->add_fld('subject');
283 $oForm->add_fld('message');
284 if ((int) dbx()->user() > 0) {
285 $oForm->add_obj('my_requests', 'dbxContact|contact-my-requests-button');
286 } else {
287 $oForm->add_obj('my_requests', 'obj-value', '');
288 }
289
290 if ($oForm->submit()) {
291 if (!$oForm->errors()) {
292 $submitData = $this->snapshot_submitted_data($oForm);
293 $spamReason = $this->spam_reason($submitData);
294 if ($spamReason !== '') {
295 $oForm->set_msg_error('Ihre Anfrage wurde nicht versendet, weil die Eingabe als Spam erkannt wurde.');
296 $oForm->add_fld_error('message', 'Spam-Verdacht. Bitte pruefen Sie Ihre Eingabe.');
297 dbx()->sys_msg('security', 'dbxContact', 'spam_guard', 'Kontaktanfrage blockiert', $spamReason . ' email=' . ($submitData['email'] ?? ''));
298 }
299 }
300
301 if (!$oForm->errors()) {
302 $submitData = $this->snapshot_submitted_data($oForm);
303 $extra = array(
304 'uid' => (int) dbx()->user(),
305 'status' => 'open',
306 'request_ip' => substr((string) ($_SERVER['REMOTE_ADDR'] ?? ''), 0, 64),
307 'request_user_agent' => substr((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 512),
308 'mail_sent' => 0,
309 'mail_sent_date' => '',
310 'confirm_mail_sent' => 0,
311 'confirm_mail_sent_date' => '',
312 );
313
314 $ok = $oForm->save_post($this->dd, 0, $extra);
315 $rid = (int) $oForm->_rid;
316
317 if (!$ok || $rid <= 0) {
318 $oForm->_msg_error = 'Die Kontaktanfrage konnte nicht gespeichert werden.';
319 } else {
320 $data = $submitData;
321 $record = $oDB->select1($this->dd, $rid, '*', 0);
322 if (is_array($record)) {
323 foreach (array('name', 'email', 'phone', 'subject', 'message') as $field) {
324 if (trim((string) ($data[$field] ?? '')) === '' && trim((string) ($record[$field] ?? '')) !== '') {
325 $data[$field] = (string) $record[$field];
326 }
327 }
328 }
329 $confirmMailOk = null;
330
331 dbxContactTicket::addMessage($oDB, $rid, array(
332 'author_uid' => (int) dbx()->user(),
333 'author_type' => 'requester',
334 'message_type' => 'request',
335 'visibility' => 'public',
336 'body' => (string) ($data['message'] ?? ''),
337 'status_to' => 'open',
338 ));
339 dbxContactTicket::touch($oDB, $rid, array(
340 'priority' => 'normal',
341 'user_hidden' => 0,
342 ));
343
345 $adminMailOk = (bool) $this->mail_request($data, $rid);
346 if ($adminMailOk) {
347 $oDB->update($this->dd, array(
348 'mail_sent' => 1,
349 'mail_sent_date' => date('Y-m-d H:i:s'),
350 ), $rid, 0, 1, 1, 1);
351 } else {
352 $oForm->set_msg_error('Die Kontaktanfrage wurde gespeichert, aber die E-Mail-Benachrichtigung konnte nicht versendet werden.');
353 $oForm->add_fld_error('email', 'Die E-Mail konnte nicht versendet werden.');
354 }
355 }
356
357 if (!$oForm->errors() && dbxContactConfig::mailConfirmRequester()) {
358 $confirmMailOk = (bool) $this->mail_confirm($data, $rid);
359 if ($confirmMailOk) {
360 $oDB->update($this->dd, array(
361 'confirm_mail_sent' => 1,
362 'confirm_mail_sent_date' => date('Y-m-d H:i:s'),
363 ), $rid, 0, 1, 1, 1);
364 } else {
365 $oForm->set_msg_error('Die Kontaktanfrage wurde gespeichert, aber die Bestaetigungs-E-Mail konnte nicht versendet werden.');
366 $oForm->add_fld_error('email', 'Die Bestaetigungs-E-Mail konnte nicht versendet werden.');
367 }
368 }
369
370 if (!$oForm->errors()) {
371 $successContent = $this->render_confirm($data, $rid, $confirmMailOk);
372 }
373 }
374 }
375 }
376
377 if ($successContent !== '') {
378 return $successContent;
379 }
380
381 return $oForm->run();
382 }
383}
static touch($db, int $ticketId, array $values=array())
static addMessage($db, int $ticketId, array $data)
$fields[]
Definition config.dd.php:16
$field
Definition config.dd.php:4
DBX schema administration.
$_SERVER['REQUEST_URI']