dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
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(): array {
25 $from = trim((string) dbx()->get_config('dbxContact', 'mail_from'));
26 $fromName = trim((string) dbx()->get_config('dbxContact', 'mail_from_name'));
27 return 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 $from = $this->mail_from_param();
53 if (filter_var((string)($from['email'] ?? ''), FILTER_VALIDATE_EMAIL) === false) {
54 return false;
55 }
56
57 $prefix = trim((string) dbx()->get_config('dbxContact', 'mail_subject_prefix'));
58 $subject = trim($prefix . ': ' . ($data['subject'] ?? ''));
59 if ($rid > 0) {
60 $subject .= ' #' . $rid;
61 }
62
63 $tpl = dbx()->get_system_obj('dbxTPL');
64 $html = $tpl->get_tpl('dbxContact|mail-contact-request', array(
65 'name' => $this->h($data['name'] ?? ''),
66 'email' => $this->h($data['email'] ?? ''),
67 'phone' => $this->h($data['phone'] ?? ''),
68 'subject' => $this->h($data['subject'] ?? ''),
69 'message' => $this->h($data['message'] ?? ''),
70 ));
71
72 $text = "Neue Kontaktanfrage #" . $rid . "\n\n"
73 . "Name: " . ($data['name'] ?? '') . "\n"
74 . "E-Mail: " . ($data['email'] ?? '') . "\n"
75 . "Telefon: " . ($data['phone'] ?? '') . "\n"
76 . "Betreff: " . ($data['subject'] ?? '') . "\n\n"
77 . ($data['message'] ?? '');
78
79 $options = $this->mail_profile_options(array(
80 'reply_to' => array('email' => (string) ($data['email'] ?? ''), 'name' => (string) ($data['name'] ?? '')),
81 'text' => $text,
82 ));
83
84 return dbx()->send_mail($from, $to, $subject, $html, 'html', array(), $options);
85 }
86
87 private function mail_confirm(array $data, int $rid) {
88 $to = trim((string) ($data['email'] ?? ''));
89 if ($to === '') {
90 return false;
91 }
92 $from = $this->mail_from_param();
93 if (filter_var((string)($from['email'] ?? ''), FILTER_VALIDATE_EMAIL) === false) {
94 return false;
95 }
96
97 $subject = trim((string) dbx()->get_config('dbxContact', 'mail_confirm_subject'));
98 if ($subject === '') {
99 $subject = 'Ihre Kontaktanfrage';
100 }
101 if ($rid > 0) {
102 $subject .= ' #' . $rid;
103 }
104
105 $phone = trim((string) ($data['phone'] ?? ''));
106
107 $tpl = dbx()->get_system_obj('dbxTPL');
108 $html = $tpl->get_tpl('dbxContact|mail-contact-confirm', array(
109 'rid' => (int) $rid,
110 'name' => $this->h($data['name'] ?? ''),
111 'phone' => $this->h($phone !== '' ? $phone : '-'),
112 'subject' => $this->h($data['subject'] ?? ''),
113 'message' => $this->h($data['message'] ?? ''),
114 ));
115
116 $text = "Ihre Anfrage ist eingegangen\n\n"
117 . "Hallo " . ($data['name'] ?? '') . ",\n\n"
118 . "vielen Dank fuer Ihre Nachricht. Wir haben Ihre Kontaktanfrage unter der Nummer #"
119 . $rid . " erhalten und werden sie bearbeiten.\n\n"
120 . "Betreff: " . ($data['subject'] ?? '') . "\n"
121 . "Telefon: " . ($phone !== '' ? $phone : '-') . "\n\n"
122 . ($data['message'] ?? '') . "\n\n"
123 . "Wir melden uns so bald wie moeglich bei Ihnen. Sie muessen nichts weiter unternehmen.";
124
125 return dbx()->send_mail(
126 $from,
127 $to,
128 $subject,
129 $html,
130 'html',
131 array(),
132 $this->mail_profile_options(array('text' => $text))
133 );
134 }
135
136 private function current_user_defaults() {
137 $uid = (int) dbx()->user();
138 $data = array(
139 'name' => '',
140 'email' => '',
141 'phone' => '',
142 'subject' => '',
143 'message' => '',
144 );
145
146 if ($uid > 0) {
147 $name = dbx()->user('name');
148 $email = dbx()->user('email');
149
150 if ($name !== 'undef') {
151 $data['name'] = (string) $name;
152 }
153
154 if ($email !== 'undef') {
155 $data['email'] = (string) $email;
156 }
157 }
158
159 return $data;
160 }
161
162 private function my_requests_button() {
163 if ((int) dbx()->user() <= 0) {
164 return '';
165 }
166
167 return dbx()->get_system_obj('dbxTPL')->get_tpl('dbxContact|contact-my-requests-button');
168 }
169
170 private function snapshot_submitted_data($oForm) {
171 $fields = array(
172 'name' => 'words|min=2|max=160',
173 'email' => 'email|max=180',
174 'phone' => 'varchar|max=80',
175 'subject' => 'varchar|min=3|max=220',
176 'message' => '*|min=8|max=5000',
177 );
178 $data = array();
179
180 foreach ($fields as $name => $rules) {
181 $value = trim((string) $oForm->get_post($name, '', $rules));
182 $data[$name] = $value;
183 $oForm->set_post($name, $value);
184 }
185
186 return $data;
187 }
188
189 private function confirm_notes(array $data, int $rid, ?bool $confirmMailOk, $form) {
190 $notes = array();
191 $notes[] = $form->get_fd_message('confirm_received');
192 $notes[] = $form->format_fd_message(
193 'confirm_number',
194 array('rid' => $rid)
195 );
196
197 $email = trim((string) ($data['email'] ?? ''));
198 $safeEmail = $this->h($email);
200 if ($confirmMailOk === true && $email !== '') {
201 $notes[] = $form->format_fd_message(
202 'confirm_mail_sent',
203 array('email' => $safeEmail)
204 );
205 } elseif ($confirmMailOk === false && $email !== '') {
206 $notes[] = $form->get_fd_message('confirm_mail_failed');
207 } elseif ($email !== '') {
208 $notes[] = $form->format_fd_message(
209 'confirm_mail_pending',
210 array('email' => $safeEmail)
211 );
212 }
213 }
214
215 $notes[] = $form->get_fd_message('confirm_reply');
216
217 $html = '';
218 foreach ($notes as $note) {
219 $html .= '<li>' . $note . '</li>';
220 }
221
222 return $html;
223 }
224
225 private function render_confirm(array $data, int $rid, ?bool $confirmMailOk, $form) {
226 $tpl = dbx()->get_system_obj('dbxTPL');
227 $phone = trim((string) ($data['phone'] ?? ''));
228
229 $html = $tpl->get_tpl('dbxContact|contact-confirm', array(
230 'rid' => (int) $rid,
231 'name' => $this->h($data['name'] ?? ''),
232 'email' => $this->h($data['email'] ?? ''),
233 'phone' => $this->h($phone !== '' ? $phone : '-'),
234 'subject' => $this->h($data['subject'] ?? ''),
235 'message' => $this->h($data['message'] ?? ''),
236 'confirm_lead' => $form->get_fd_message('confirm_lead'),
237 'confirm_notes' => $this->confirm_notes(
238 $data,
239 $rid,
240 $confirmMailOk,
241 $form
242 ),
243 'my_requests' => $this->my_requests_button(),
244 'frame_id' => 'dbx_contact_confirm',
245 'frame_panel_class' => 'dbxForm_wrapper dbx-contact dbx-contact-confirm-panel',
246 'frame_panel_attrs' => '',
247 'frame_subbar' => '',
248 'frame_form_open' => '',
249 'frame_form_close' => '',
250 'frame_body_class' => '',
251 'frame_body_head' => '',
252 'frame_body_tail' => '',
253 'bar_class' => 'dbx-module-bar',
254 'bar_title_class' => 'dbx-module-bar-titleblock',
255 'bar_actions_class' => 'dbx-module-bar-actions',
256 'bar_title' => $form->get_fd_message('confirm_title'),
257 'bar_icon' => 'bi-check2-circle',
258 'bar_subtitle' => $form->get_fd_message(
259 'confirm_subtitle'
260 ),
261 'bar_title_pre' => '',
262 'bar_title_heading_attrs' => '',
263 'bar_middle' => '',
264 'bar_extra' => '',
265 'bar_actions' => $tpl->get_tpl('dbxContact|contact-confirm-bar-actions', array(
266 'my_requests' => $this->my_requests_button(),
267 )),
268 ));
269
270 return str_replace('[dbx:js]', '', $html);
271 }
272
273 public function run() {
274 $oForm = dbx()->get_system_obj('dbxForm');
275 $oDB = dbx()->get_system_obj('dbxDB');
276 $successContent = '';
277
278 $oForm->init('dbxContact_form', 'contact-form');
279 $oForm->set_editor_class_file(__FILE__);
280 $oForm->_dd = $this->dd;
281 $oForm->_fd = 'dbxContact|contact-form';
282 $oForm->load_fd_messages();
283 $oForm->_action = '?dbx_modul=dbxContact&dbx_run1=form';
284 $oForm->add_module_bar(
285 $oForm->get_fd_message('bar_title'),
286 'bi-envelope-paper',
287 $oForm->get_fd_message('bar_subtitle')
288 );
289 $oForm->add_rep('bar_class', 'dbx-module-bar');
290 $oForm->add_rep('frame_panel_class', 'dbxForm_wrapper dbx-contact');
291 $oForm->prepare_form_shell(array('class' => 'dbx-contact-form'));
292 $oForm->add_rep('bar_actions', '{obj:contact_bar_actions}');
293 $oForm->add_obj('contact_bar_actions', 'dbxContact|contact-form-bar-actions', array(
294 'bar_form_id' => 'dbx_form_' . (int) $oForm->_next_i,
295 ));
296 $oForm->_data = $this->current_user_defaults();
297 $oForm->set_msg_info('');
298 $oForm->set_msg_ok($oForm->get_fd_message('request_success'));
299 $oForm->set_msg_error($oForm->get_fd_message('validation_error'));
300 $oForm->set_msg_warning($oForm->get_fd_message('form_warning'));
301 $oForm->_try_reset = 6;
302 $oForm->_try_max = 5;
303 $oForm->_try_msg = $oForm->get_fd_message('try_limit');
304
305 $oForm->add_fld('name');
306 $oForm->add_fld('email');
307 $oForm->add_fld('phone');
308 $oForm->add_fld('subject');
309 $oForm->add_fld('message');
310 if ((int) dbx()->user() > 0) {
311 $oForm->add_obj('my_requests', 'dbxContact|contact-my-requests-button');
312 } else {
313 $oForm->add_obj('my_requests', 'obj-value', '');
314 }
315
316 if ($oForm->submit()) {
317 if (!$oForm->errors()) {
318 $submitData = $this->snapshot_submitted_data($oForm);
319 $spamReason = $this->spam_reason($submitData);
320 if ($spamReason !== '') {
321 $oForm->set_msg_error(
322 $oForm->get_fd_message('spam_error')
323 );
324 $oForm->add_fld_error(
325 'message',
326 $oForm->get_fd_message('spam_field_error')
327 );
328 dbx()->sys_msg('security', 'dbxContact', 'spam_guard', 'Kontaktanfrage blockiert', $spamReason . ' email=' . ($submitData['email'] ?? ''));
329 }
330 }
331
332 if (!$oForm->errors()) {
333 $submitData = $this->snapshot_submitted_data($oForm);
334 $extra = array(
335 'uid' => (int) dbx()->user(),
336 'status' => 'open',
337 'request_ip' => substr((string) ($_SERVER['REMOTE_ADDR'] ?? ''), 0, 64),
338 'request_user_agent' => substr((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 512),
339 'mail_sent' => 0,
340 'mail_sent_date' => '',
341 'confirm_mail_sent' => 0,
342 'confirm_mail_sent_date' => '',
343 );
344
345 $ok = $oForm->save_post($this->dd, 0, $extra);
346 $rid = (int) $oForm->_rid;
347
348 if (!$ok || $rid <= 0) {
349 $oForm->_msg_error = $oForm->get_fd_message(
350 'store_error'
351 );
352 } else {
353 $data = $submitData;
354 $record = $oDB->select1($this->dd, $rid, '*', 0);
355 if (is_array($record)) {
356 foreach (array('name', 'email', 'phone', 'subject', 'message') as $field) {
357 if (trim((string) ($data[$field] ?? '')) === '' && trim((string) ($record[$field] ?? '')) !== '') {
358 $data[$field] = (string) $record[$field];
359 }
360 }
361 }
362 $confirmMailOk = null;
363
364 dbxContactTicket::addMessage($oDB, $rid, array(
365 'author_uid' => (int) dbx()->user(),
366 'author_type' => 'requester',
367 'message_type' => 'request',
368 'visibility' => 'public',
369 'body' => (string) ($data['message'] ?? ''),
370 'status_to' => 'open',
371 ));
372 dbxContactTicket::touch($oDB, $rid, array(
373 'priority' => 'normal',
374 'user_hidden' => 0,
375 ));
376
378 $adminMailOk = (bool) $this->mail_request($data, $rid);
379 if ($adminMailOk) {
380 $oDB->update($this->dd, array(
381 'mail_sent' => 1,
382 'mail_sent_date' => date('Y-m-d H:i:s'),
383 ), $rid, 0, 1, 1, 1);
384 } else {
385 $oForm->set_msg_error(
386 $oForm->get_fd_message('admin_mail_error')
387 );
388 $oForm->add_fld_error(
389 'email',
390 $oForm->get_fd_message('email_field_error')
391 );
392 }
393 }
394
395 if (!$oForm->errors() && dbxContactConfig::mailConfirmRequester()) {
396 $confirmMailOk = (bool) $this->mail_confirm($data, $rid);
397 if ($confirmMailOk) {
398 $oDB->update($this->dd, array(
399 'confirm_mail_sent' => 1,
400 'confirm_mail_sent_date' => date('Y-m-d H:i:s'),
401 ), $rid, 0, 1, 1, 1);
402 } else {
403 $oForm->set_msg_error(
404 $oForm->get_fd_message('confirm_mail_error')
405 );
406 $oForm->add_fld_error(
407 'email',
408 $oForm->get_fd_message(
409 'confirm_email_field_error'
410 )
411 );
412 }
413 }
414
415 if (!$oForm->errors()) {
416 $successContent = $this->render_confirm(
417 $data,
418 $rid,
419 $confirmMailOk,
420 $oForm
421 );
422 }
423 }
424 }
425 }
426
427 if ($successContent !== '') {
428 return $successContent;
429 }
430
431 return $oForm->run();
432 }
433}
static touch($db, int $ticketId, array $values=array())
static addMessage($db, int $ticketId, array $data)
user($key='id')
Liest einen Wert des aktuellen Benutzers.
Definition dbxApi.php:1305
$_SERVER['REQUEST_METHOD']
if($web->content_permalink_redirect_target() !=='') $tpl
DBX schema administration.
$profile
Definition run.php:6