]> git.p6c8.net - form-email.git/blob - form-email/mailer.php
Auch mit Traditionen sollte man irgendwann mal brechen:
[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: 15.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
88 if(!filter_var($email,FILTER_VALIDATE_EMAIL)) show_user_error($err_invalid_email);
89
90 # Individuelle Felder einlesen
91
92 $prepared_user_fields = array();
93
94 while(list($user_field,$user_field_data) = each($user_fields)) {
95 $user_field_content = formdata($user_field);
96
97 # Pruefung, ob das Feld gesetzt wurde und ob es optional ist
98
99 if((!$user_field_data['optional']) && (!$user_field_content)) {
100 show_user_error($user_field_data['errors']['not_set']);
101 }
102 elseif($user_field_data['optional'] && (!$user_field_content)) {
103 # Wenn das Feld optional ist und nicht gesetzt wurde, brechen wir hier ab
104 # und machen mit dem naechsten Feld weiter
105
106 $prepared_user_fields[$user_field_data['tpl_var']] = '';
107 continue;
108 }
109
110 # Laengen-Pruefung
111
112 if(isset($user_field_data['min']) && $user_field_data['min']) {
113 if(strlen($user_field_content) < $user_field_data['min']) {
114 if(isset($user_field_data['errors']['too_short'])) {
115 show_user_error($user_field_data['errors']['too_short']);
116 }
117 else {
118 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'too_short', 'INDIVIDUAL' => $user_field));
119 }
120 }
121 }
122
123 if(isset($user_field_data['min']) && $user_field_data['max']) {
124 if(strlen($user_field_content) > $user_field_data['max']) {
125 if(isset($user_field_data['errors']['too_long'])) {
126 show_user_error($user_field_data['errors']['too_long']);
127 }
128 else {
129 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'too_long', 'INDIVIDUAL' => $user_field));
130 }
131 }
132 }
133
134 # Pruefung gegen frei definierbare Pruef-Funktion
135
136 if(isset($user_field_data['check']) && $user_field_data['check'] != '') {
137 if(function_exists($user_field_data['check']) && call_user_func($user_field_data['check'],$user_field_content) === false) {
138 if(isset($user_field_data['errors']['check_fail'])) {
139 show_user_error($user_field_data['errors']['check_fail']);
140 }
141 else {
142 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'check_fail', 'INDIVIDUAL' => $user_field));
143 }
144 }
145 }
146
147 # Pruefung gegen frei definierbaren regulaeren Ausdruck
148
149 if(isset($user_field_data['regex']) && $user_field_data['regex'] != '') {
150 if(!preg_match($user_field_data['regex'],$user_field_content)) {
151 if(isset($user_field_data['errors']['match_fail'])) {
152 show_user_error($user_field_data['errors']['match_fail']);
153 }
154 else {
155 show_fatal($err_indiv_errmsg_miss,array('ERRMSG' => 'match_fail', 'INDIVIDUAL' => $user_field));
156 }
157 }
158 }
159
160 # Formularfeld durch frei definierbare Funktion bearbeiten
161
162 if(isset($user_field_data['prepare']) && $user_field_data['prepare'] != '') {
163 if(function_exists($user_field_data['check'])) {
164 $user_field_content = call_user_func($user_field_data['prepare'],$user_field_content);
165 }
166 }
167
168 # Name der Template-Variable ermitteln
169
170 if(isset($user_field_data['tpl_var']) && $user_field_data['tpl_var'] != '') {
171 $tpl_var = $user_field_data['tpl_var'];
172 }
173 else {
174 $tpl_var = 'USER_'.$user_field;
175 }
176
177 # Fertige Formulardaten wegkopieren und fuer spaeter zur Verfuegung stellen
178
179 $prepared_user_fields[$tpl_var] = $user_field_content;
180 }
181
182 # Captcha ueberpruefen
183
184 if($captcha_enable) {
185 $captcha_input = formdata('captcha');
186 $captcha_incorrect = false;
187
188 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
189 if($captcha_case) {
190 if(!$captcha_input || empty($captcha_input) || $captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
191 }
192 else {
193 if(!$captcha_input || empty($captcha_input) || strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
194 }
195
196 if($captcha_incorrect) {
197 if($captcha_max) {
198 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
199 $_SESSION['captcha_failed']++;
200 }
201 else {
202 $_SESSION['captcha_failed'] = 1;
203 }
204 }
205
206 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
207 show_fatal($err_captcha_max);
208 }
209 else {
210 show_user_error($err_captcha_incorrect);
211 }
212 }
213 }
214 else {
215 show_fatal($err_captcha_session_failure);
216 }
217 }
218
219 # Referer ueberpruefen
220
221 if($check_referer) {
222 $referer_ok = semicolon_split($referer_ok);
223
224 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
225 $referer_host = $referer_parts['host'];
226
227 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
228 }
229
230 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
231
232 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
233 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
234
235 # Uhrzeit formatieren
236
237 date_default_timezone_set($timezone);
238 $time = strftime($timeformat);
239
240 # Header der Mail
241
242 $header = '';
243
244 if($friendly_sender) {
245 if($friendly_sender == 2) {
246 if(preg_match("/[\200-\377]/",$name)) {
247 # Absendernamen mit Base64 codieren
248
249 $encoded_name = base64_encode($name);
250 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
251 $encoded_name = "=?$charset?B?$encoded_name?=";
252 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
253 }
254 else {
255 # Bestimmte Zeichen im Absendernamen codieren
256
257 $encoded_name = str_replace('"','\"',$name);
258 $encoded_name = str_replace('(','\\(',$encoded_name);
259 $encoded_name = str_replace(')','\\)',$encoded_name);
260 $encoded_name = '"'.$encoded_name.'"';
261 }
262
263 $header = "From: $encoded_name <$email>\n";
264 }
265 else $header = "From: $email\n";
266 }
267
268 if($add_header) {
269 if(!$friendly_sender) $header .= "Reply-To: $email\n";
270 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
271 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
272 $header .= 'MIME-Version: 1.0'."\n";
273 $header .= 'X-Mailer: Form E-Mail '.$VERSION."\n";
274 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
275 }
276 else $header .= "Reply-To: $email";
277
278 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
279
280 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
281 # die nicht aus dem ASCII-Zeichensatz stammen
282
283 if(preg_match("/[\200-\377]/",$subject)) {
284 $encoded_subject = base64_encode($subject);
285 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
286 $encoded_subject = "=?$charset?B?$encoded_subject?=";
287 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
288 }
289 else $encoded_subject = $subject;
290
291 # Mailtext formatieren...
292
293 $mtpl = new Template;
294 $mtpl->read_file($tpl_mail);
295
296 $mtpl->set_var('EMAIL', $email);
297 $mtpl->set_var('NAME', $name);
298 $mtpl->set_var('SUBJECT',$subject);
299 $mtpl->set_var('TIME', $time);
300
301 # ... Umgebungsvariablen einfuegen...
302
303 if(isset($allowed_envs) && is_array($allowed_envs)) {
304 foreach($allowed_envs as $allowed_env) {
305 if(isset($_SERVER[$allowed_env])) {
306 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
307 }
308 else {
309 $mtpl->set_var('ENV_'.$allowed_env,'');
310 }
311 }
312 }
313
314 # ... eigene Felder einfuegen
315
316 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
317 $mtpl->set_var($tpl_var,$user_field_content);
318 }
319
320 $mtpl->set_var('TEXT',$text);
321
322 $mtpl->parse();
323
324 $mailtext = $mtpl->get_template();
325
326 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
327
328 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
329
330 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
331
332 if($recipient && is_array($aliases) && $aliases[$recipient]) {
333 # Empfaenger mit Alias-Namen
334
335 $mailto = $aliases[$recipient];
336 }
337
338 $mailto = semicolon_split($mailto);
339
340 for($x=0;$x<count($mailto);$x++) {
341 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
342 }
343
344 if($captcha_enable) session_destroy();
345
346 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
347
348 if($return_url) {
349 # Per Redirect auf Seite umleiten
350
351 if(!preg_match('!://!',$return_url)) {
352 # URL vervollstaendigen
353
354 $http_host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
355 $protocol = '';
356 $port = '';
357
358 if($_SERVER['HTTPS']) {
359 # SSL-verschluesseltes HTTP
360
361 $protocol = 'https://';
362 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
363 }
364 else {
365 # Normales HTTP
366
367 $protocol = 'http://';
368 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
369 }
370
371 if(preg_match('!^/!',$return_url)) {
372 $return_url = $protocol.$http_host.$port.$return_url;
373 }
374 else {
375 if(preg_match('!/$!',$_SERVER['SCRIPT_NAME'])) $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
376 $path = substr($path,0,strrpos($path,'/')+1);
377 if(!preg_match('!^/!',$path)) $path = '/'.$path;
378
379 $return_url = $protocol.$http_host.$port.$path.$return_url;
380 }
381 }
382
383 header('Status: 303 See Other');
384 header('Location: '.$return_url);
385 }
386 else {
387 # Mit Template formatierte Seite anzeigen
388
389 $stpl = new Template;
390 $stpl->read_file($tpl_sent);
391
392 $stpl->set_var('EMAIL', plain($email));
393 $stpl->set_var('MAIL', plain($mailtext));
394 $stpl->set_var('NAME', plain($name));
395 $stpl->set_var('SUBJECT',plain($subject));
396 $stpl->set_var('TEXT', plain($text));
397 $stpl->set_var('TIME', plain($time));
398
399 $stpl->parse();
400
401 print $stpl->get_template();
402 }
403
404 #
405 ### Ende ###
406
407 ?>

patrick-canterino.de