]> git.p6c8.net - form-email.git/blob - form-email/mailer.php
Wenn in der Konfiguration kein Name fuer die Template-Variable eines indiv.
[form-email.git] / form-email / mailer.php
1 <?php
2
3 #
4 # Form E-Mail 3.5
5 #
6 # Versenden einer E-Mail, die ueber ein Kontaktformular
7 # geschrieben wurde.
8 #
9 # Autor: Patrick Canterino <patrick@patshaping.de>
10 # Letzte Aenderung: 12.12.2011
11 #
12 # Copyright (C) 2002-2011 Patrick Canterino
13 #
14 # Diese Datei kann unter den Bedingungen der "Artistic License 2.0"
15 # weitergegeben und / oder veraendert werden.
16 # Siehe:
17 # http://www.opensource.org/licenses/artistic-license-2.0
18 #
19
20 # ===========
21 # Hauptteil
22 # ===========
23
24 $VERSION = '3.5-dev';
25
26 require('config.php');
27 require('functions.php');
28 require('class.Template.php');
29
30 if($_SERVER['REQUEST_METHOD'] != 'POST') show_fatal($err_only_post);
31
32 # Wenn Captchas aktiviert sind, Session starten
33
34 if($captcha_enable) {
35 session_start();
36
37 # Pruefen, ob die zulaessige Zahl der Falscheingaben bei Captchas ueberschritten wurde
38
39 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
40 show_fatal($err_captcha_max);
41 }
42 }
43
44 # Bestimmte Werte in Integers umwandeln
45
46 $name_min = intval($name_min);
47 $subject_min = intval($subject_min);
48 $text_min = intval($text_min);
49 $name_max = intval($name_max);
50 $subject_max = intval($subject_max);
51 $text_max = intval($text_max);
52 $line_break = intval($line_break);
53
54 # Formulardaten auslesen
55
56 $email = formdata('email');
57 $name = formdata('name');
58 $recipient = formdata('recipient');
59 $subject = formdata('subject');
60 $text = formdata('text');
61
62 # Pruefen, ob ueberhaupt was ausgefuellt wurde
63
64 if(!$name && !$email && !$text) show_user_error($err_nothing);
65
66 # Pruefen, ob alle wichtigen Formular-Felder ausgefuellt wurden
67
68 if(!$name) show_user_error($err_no_name);
69 if(!$email) show_user_error($err_no_email);
70 if(!$text) show_user_error($err_no_text);
71
72 if(!$subject) $subject = $default_subject; # Standard-Betreff
73
74 # Pruefen, ob Name, Betreff und Text lang genug sind
75
76 if($name_min && strlen($name) < $name_min) show_user_error($err_name_short);
77 if($subject_min && strlen($subject) < $subject_min) show_user_error($err_subject_short);
78 if($text_min && strlen($text) < $text_min) show_user_error($err_text_short);
79
80 # Pruefen, ob Name, Betreff und Text nicht zu lang sind
81
82 if($name_max && strlen($name) > $name_max) show_user_error($err_name_long);
83 if($subject_max && strlen($subject) > $subject_max) show_user_error($err_subject_long);
84 if($text_max && strlen($text) > $text_max) show_user_error($err_text_long);
85
86 # Pruefen, ob eingegebene E-Mail-Adresse gueltig ist
87 # Danke an "ich" aus dem SELFHTML Forum fuer die Muehe
88
89 $mail_regexp = ($no_tld)
90 ? '/^([a-z0-9]+([\.]?|[-]?|[_]?)?)*([a-z0-9]+([\.]?[_]?)?)@([a-z0-9]+([\.]?|[-]?)?)*[a-z0-9]$/i'
91 : '/^([a-z0-9]+([\.]?|[-]?|[_]?)?)*([a-z0-9]+([\.]?[_]?)?)@([a-z0-9]+([\.]?|[-]?)?)*[a-z0-9]+\.{1}[a-z]{2,6}$/i';
92
93 if(!preg_match($mail_regexp,$email)) show_user_error($err_invalid_email);
94
95 # Individuelle Felder einlesen
96
97 $prepared_user_fields = array();
98
99 while(list($user_field,$user_field_data) = each($user_fields)) {
100 $user_field_content = formdata($user_field);
101
102 # Pruefung, ob das Feld gesetzt wurde und ob es optional ist
103
104 if((!$user_field_data['optional']) && (!$user_field_content)) {
105 show_user_error($user_field_data['errors']['not_set']);
106 }
107 elseif($user_field_data['optional'] && (!$user_field_content)) {
108 # Wenn das Feld optional ist und nicht gesetzt wurde, brechen wir hier ab
109 # und machen mit dem naechsten Feld weiter
110
111 $prepared_user_fields[$user_field_data['tpl_var']] = '';
112 continue;
113 }
114
115 # Laengen-Pruefung
116
117 if(isset($user_field_data['min']) && $user_field_data['min']) {
118 if(strlen($user_field_content) < $user_field_data['min']) {
119 if(isset($user_field_data['errors']['too_short'])) {
120 show_user_error($user_field_data['errors']['too_short']);
121 }
122 else {
123 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'too_short', 'INDIVIDUAL' => $user_field));
124 }
125 }
126 }
127
128 if(isset($user_field_data['min']) && $user_field_data['max']) {
129 if(strlen($user_field_content) > $user_field_data['max']) {
130 if(isset($user_field_data['errors']['too_long'])) {
131 show_user_error($user_field_data['errors']['too_long']);
132 }
133 else {
134 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'too_long', 'INDIVIDUAL' => $user_field));
135 }
136 }
137 }
138
139 # Pruefung gegen frei definierbare Pruef-Funktion
140
141 if(isset($user_field_data['check']) && $user_field_data['check'] != '') {
142 if(function_exists($user_field_data['check']) && call_user_func($user_field_data['check'],$user_field_content) === false) {
143 if(isset($user_field_data['errors']['check_fail'])) {
144 show_user_error($user_field_data['errors']['check_fail']);
145 }
146 else {
147 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'check_fail', 'INDIVIDUAL' => $user_field));
148 }
149 }
150 }
151
152 # Pruefung gegen frei definierbaren regulaeren Ausdruck
153
154 if(isset($user_field_data['regex']) && $user_field_data['regex'] != '') {
155 if(!preg_match($user_field_data['regex'],$user_field_content)) {
156 if(isset($user_field_data['errors']['match_fail'])) {
157 show_user_error($user_field_data['errors']['match_fail']);
158 }
159 else {
160 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'match_fail', 'INDIVIDUAL' => $user_field));
161 }
162 }
163 }
164
165 # Formularfeld durch frei definierbare Funktion bearbeiten
166
167 if(isset($user_field_data['prepare']) && $user_field_data['prepare'] != '') {
168 if(function_exists($user_field_data['check'])) {
169 $user_field_content = call_user_func($user_field_data['prepare'],$user_field_content);
170 }
171 }
172
173 # Name der Template-Variable ermitteln
174
175 if(isset($user_field_data['tpl_var']) && $user_field_data['tpl_var'] != '') {
176 $tpl_var = $user_field_data['tpl_var'];
177 }
178 else {
179 $tpl_var = 'USER_'.$user_field;
180 }
181
182 # Fertige Formulardaten wegkopieren und fuer spaeter zur Verfuegung stellen
183
184 $prepared_user_fields[$tpl_var] = $user_field_content;
185 }
186
187 # Captcha ueberpruefen
188
189 if($captcha_enable) {
190 $captcha_input = formdata('captcha');
191 $captcha_incorrect = false;
192
193 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
194 if($captcha_case) {
195 if(!$captcha_input || empty($captcha_input) || $captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
196 }
197 else {
198 if(!$captcha_input || empty($captcha_input) || strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
199 }
200
201 if($captcha_incorrect) {
202 if($captcha_max) {
203 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
204 $_SESSION['captcha_failed']++;
205 }
206 else {
207 $_SESSION['captcha_failed'] = 1;
208 }
209 }
210
211 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
212 show_fatal($err_captcha_max);
213 }
214 else {
215 show_user_error($err_captcha_incorrect);
216 }
217 }
218 }
219 else {
220 show_fatal($err_captcha_session_failure);
221 }
222 }
223
224 # Referer ueberpruefen
225
226 if($check_referer) {
227 $referer_ok = semicolon_split($referer_ok);
228
229 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
230 $referer_host = $referer_parts['host'];
231
232 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
233 }
234
235 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
236
237 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
238 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
239
240 # Uhrzeit formatieren
241
242 date_default_timezone_set($timezone);
243 $time = strftime($timeformat);
244
245 # Header der Mail
246
247 $header = '';
248
249 if($friendly_sender) {
250 if($friendly_sender == 2) {
251 if(preg_match("/[\200-\377]/",$name)) {
252 # Absendernamen mit Base64 codieren
253
254 $encoded_name = base64_encode($name);
255 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
256 $encoded_name = "=?$charset?B?$encoded_name?=";
257 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
258 }
259 else {
260 # Bestimmte Zeichen im Absendernamen codieren
261
262 $encoded_name = str_replace('"','\"',$name);
263 $encoded_name = str_replace('(','\\(',$encoded_name);
264 $encoded_name = str_replace(')','\\)',$encoded_name);
265 $encoded_name = '"'.$encoded_name.'"';
266 }
267
268 $header = "From: $encoded_name <$email>\n";
269 }
270 else $header = "From: $email\n";
271 }
272
273 if($add_header) {
274 if(!$friendly_sender) $header .= "Reply-To: $email\n";
275 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
276 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
277 $header .= 'MIME-Version: 1.0'."\n";
278 $header .= 'X-Mailer: Form E-Mail '.$VERSION."\n";
279 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
280 }
281 else $header .= "Reply-To: $email";
282
283 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
284
285 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
286 # die nicht aus dem ASCII-Zeichensatz stammen
287
288 if(preg_match("/[\200-\377]/",$subject)) {
289 $encoded_subject = base64_encode($subject);
290 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
291 $encoded_subject = "=?$charset?B?$encoded_subject?=";
292 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
293 }
294 else $encoded_subject = $subject;
295
296 # Mailtext formatieren...
297
298 $mtpl = new Template;
299 $mtpl->read_file($tpl_mail);
300
301 $mtpl->set_var('EMAIL', $email);
302 $mtpl->set_var('NAME', $name);
303 $mtpl->set_var('SUBJECT',$subject);
304 $mtpl->set_var('TIME', $time);
305
306 # ... Umgebungsvariablen einfuegen...
307
308 if(isset($allowed_envs) && is_array($allowed_envs)) {
309 foreach($allowed_envs as $allowed_env) {
310 if(isset($_SERVER[$allowed_env])) {
311 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
312 }
313 else {
314 $mtpl->set_var('ENV_'.$allowed_env,'');
315 }
316 }
317 }
318
319 # ... eigene Felder einfuegen
320
321 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
322 $mtpl->set_var($tpl_var,$user_field_content);
323 }
324
325 $mtpl->set_var('TEXT',$text);
326
327 $mtpl->parse();
328
329 $mailtext = $mtpl->get_template();
330
331 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
332
333 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
334
335 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
336
337 if($recipient && is_array($aliases) && $aliases[$recipient]) {
338 # Empfaenger mit Alias-Namen
339
340 $mailto = $aliases[$recipient];
341 }
342
343 $mailto = semicolon_split($mailto);
344
345 for($x=0;$x<count($mailto);$x++) {
346 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
347 }
348
349 if($captcha_enable) session_destroy();
350
351 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
352
353 if($return_url) {
354 # Per Redirect auf Seite umleiten
355
356 if(!preg_match('!://!',$return_url)) {
357 # URL vervollstaendigen
358
359 $http_host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
360 $protocol = '';
361 $port = '';
362
363 if($_SERVER['HTTPS']) {
364 # SSL-verschluesseltes HTTP
365
366 $protocol = 'https://';
367 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
368 }
369 else {
370 # Normales HTTP
371
372 $protocol = 'http://';
373 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
374 }
375
376 if(preg_match('!^/!',$return_url)) {
377 $return_url = $protocol.$http_host.$port.$return_url;
378 }
379 else {
380 if(preg_match('!/$!',$_SERVER['SCRIPT_NAME'])) $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
381 $path = substr($path,0,strrpos($path,'/')+1);
382 if(!preg_match('!^/!',$path)) $path = '/'.$path;
383
384 $return_url = $protocol.$http_host.$port.$path.$return_url;
385 }
386 }
387
388 header('Status: 303 See Other');
389 header('Location: '.$return_url);
390 }
391 else {
392 # Mit Template formatierte Seite anzeigen
393
394 $stpl = new Template;
395 $stpl->read_file($tpl_sent);
396
397 $stpl->set_var('EMAIL', plain($email));
398 $stpl->set_var('MAIL', plain($mailtext));
399 $stpl->set_var('NAME', plain($name));
400 $stpl->set_var('SUBJECT',plain($subject));
401 $stpl->set_var('TEXT', plain($text));
402 $stpl->set_var('TIME', plain($time));
403
404 $stpl->parse();
405
406 print $stpl->get_template();
407 }
408
409 #
410 ### Ende ###
411
412 ?>

patrick-canterino.de