]> git.p6c8.net - form-email.git/blob - form-email/mailer.php
Pruefung ob indiv. Feld gesetzt wurde, funktioniert.
[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: 7.11.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 require('config.php');
25 require('functions.php');
26 require('class.Template.php');
27
28 if($only_post && $_SERVER['REQUEST_METHOD'] != 'POST') show_fatal($err_only_post);
29
30 # Wenn Captchas aktiviert sind, Session starten
31
32 if($captcha_enable) {
33 session_start();
34
35 # Pruefen, ob die zulaessige Zahl der Falscheingaben bei Captchas ueberschritten wurde
36
37 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
38 show_fatal($err_captcha_max);
39 }
40 }
41
42 # Bestimmte Werte in Integers umwandeln
43
44 $name_min = intval($name_min);
45 $subject_min = intval($subject_min);
46 $text_min = intval($text_min);
47 $name_max = intval($name_max);
48 $subject_max = intval($subject_max);
49 $text_max = intval($text_max);
50 $line_break = intval($line_break);
51
52 # Formulardaten auslesen
53
54 $email = formdata('email');
55 $name = formdata('name');
56 $recipient = formdata('recipient');
57 $subject = formdata('subject');
58 $text = formdata('text');
59
60 # Pruefen, ob ueberhaupt was ausgefuellt wurde
61
62 if(!$name && !$email && !$text) show_user_error($err_nothing);
63
64 # Pruefen, ob alle wichtigen Formular-Felder ausgefuellt wurden
65
66 if(!$name) show_user_error($err_no_name);
67 if(!$email) show_user_error($err_no_email);
68 if(!$text) show_user_error($err_no_text);
69
70 if(!$subject) $subject = $default_subject; # Standard-Betreff
71
72 # Pruefen, ob Name, Betreff und Text lang genug sind
73
74 if($name_min && strlen($name) < $name_min) show_user_error($err_name_short);
75 if($subject_min && strlen($subject) < $subject_min) show_user_error($err_subject_short);
76 if($text_min && strlen($text) < $text_min) show_user_error($err_text_short);
77
78 # Pruefen, ob Name, Betreff und Text nicht zu lang sind
79
80 if($name_max && strlen($name) > $name_max) show_user_error($err_name_long);
81 if($subject_max && strlen($subject) > $subject_max) show_user_error($err_subject_long);
82 if($text_max && strlen($text) > $text_max) show_user_error($err_text_long);
83
84 # Pruefen, ob eingegebene E-Mail-Adresse gueltig ist
85 # Danke an "ich" aus dem SELFHTML Forum fuer die Muehe
86
87 $mail_regexp = ($no_tld)
88 ? '/^([a-z0-9]+([\.]?|[-]?|[_]?)?)*([a-z0-9]+([\.]?[_]?)?)@([a-z0-9]+([\.]?|[-]?)?)*[a-z0-9]$/i'
89 : '/^([a-z0-9]+([\.]?|[-]?|[_]?)?)*([a-z0-9]+([\.]?[_]?)?)@([a-z0-9]+([\.]?|[-]?)?)*[a-z0-9]+\.{1}[a-z]{2,6}$/i';
90
91 if(!preg_match($mail_regexp,$email)) show_user_error($err_invalid_email);
92
93 # Individuelle Felder einlesen
94
95 $prepared_user_fields = array();
96
97 while(list($user_field,$user_field_data) = each($user_fields)) {
98 $user_field_content = formdata($user_field);
99
100 if((!$user_field_data['optional']) && (!$user_field_content)) {
101 show_user_error($user_field_data['errors']['not_set']);
102 }
103 elseif($user_field_data['optional'] && (!$user_field_content)) {
104 $prepared_user_fields[$user_field_data['tpl_var']] = '';
105 continue;
106 }
107
108 if(isset($user_field_data['check']) && $user_field_data['check'] != '') {
109 if(function_exists($user_field_data['check']) && call_user_func($user_field_data['check'],$user_field_content) === false) {
110 show_user_error($user_field_data['errors']['check_fail']);
111 }
112 }
113
114 if(isset($user_field_data['regex']) && $user_field_data['regex'] != '') {
115 if(!preg_match($user_field_data['regex'],$user_field_content)) {
116 show_user_error($user_field_data['errors']['match_fail']);
117 }
118 }
119
120 $prepared_user_fields[$user_field_data['tpl_var']] = $user_field_content;
121 }
122
123 # Captcha ueberpruefen
124
125 if($captcha_enable) {
126 $captcha_input = formdata('captcha');
127 $captcha_incorrect = false;
128
129 if(isset($_SESSION) && isset($_SESSION['captcha'])) {
130 if($captcha_case) {
131 if(!$captcha_input || empty($captcha_input) || $captcha_input != $_SESSION['captcha']) $captcha_incorrect = true;
132 }
133 else {
134 if(!$captcha_input || empty($captcha_input) || strtolower($captcha_input) != strtolower($_SESSION['captcha'])) $captcha_incorrect = true;
135 }
136
137 if($captcha_incorrect) {
138 if($captcha_max) {
139 if(isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed']) {
140 $_SESSION['captcha_failed']++;
141 }
142 else {
143 $_SESSION['captcha_failed'] = 1;
144 }
145 }
146
147 if($captcha_max && isset($_SESSION['captcha_failed']) && $_SESSION['captcha_failed'] >= $captcha_max) {
148 show_fatal($err_captcha_max);
149 }
150 else {
151 show_user_error($err_captcha_incorrect);
152 }
153 }
154 }
155 else {
156 show_fatal($err_captcha_session_failure);
157 }
158 }
159
160 # Referer ueberpruefen
161
162 if($check_referer) {
163 $referer_ok = semicolon_split($referer_ok);
164
165 $referer_parts = parse_url($_SERVER['HTTP_REFERER']);
166 $referer_host = $referer_parts['host'];
167
168 if(in_array($referer_host,$referer_ok) === false) show_fatal($err_referer_not_ok);
169 }
170
171 # Zeilenumbrueche in Namen und Betreff durch Leerzeichen ersetzen
172
173 $name = preg_replace("/\015\012|\012|\015/",' ',$name);
174 $subject = preg_replace("/\015\012|\012|\015/",' ',$subject);
175
176 # Uhrzeit formatieren
177
178 date_default_timezone_set($timezone);
179 $time = strftime($timeformat);
180
181 # Header der Mail
182
183 $header = '';
184
185 if($friendly_sender) {
186 if($friendly_sender == 2) {
187 if(preg_match("/[\200-\377]/",$name)) {
188 # Absendernamen mit Base64 codieren
189
190 $encoded_name = base64_encode($name);
191 $encoded_name = wordwrap($encoded_name,56,"\015\012",1);
192 $encoded_name = "=?$charset?B?$encoded_name?=";
193 $encoded_name = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_name);
194 }
195 else {
196 # Bestimmte Zeichen im Absendernamen codieren
197
198 $encoded_name = str_replace('"','\"',$name);
199 $encoded_name = str_replace('(','\\(',$encoded_name);
200 $encoded_name = str_replace(')','\\)',$encoded_name);
201 $encoded_name = '"'.$encoded_name.'"';
202 }
203
204 $header = "From: $encoded_name <$email>\n";
205 }
206 else $header = "From: $email\n";
207 }
208
209 if($add_header) {
210 if(!$friendly_sender) $header .= "Reply-To: $email\n";
211 $header .= 'Content-Type: text/plain; charset="'.$charset.'"'."\n";
212 $header .= 'Content-Transfer-Encoding: 8bit'."\n";
213 $header .= 'MIME-Version: 1.0'."\n";
214 $header .= 'X-Mailer: Form E-Mail 3.4.1'."\n";
215 $header .= 'X-Sender-IP: ['.$_SERVER['REMOTE_ADDR'].']';
216 }
217 else $header .= "Reply-To: $email";
218
219 $header = preg_replace("/\015\012|\012|\015/","\n",$header);
220
221 # Betreff mit Base64 codieren, wenn er Zeichen enthaelt,
222 # die nicht aus dem ASCII-Zeichensatz stammen
223
224 if(preg_match("/[\200-\377]/",$subject)) {
225 $encoded_subject = base64_encode($subject);
226 $encoded_subject = wordwrap($encoded_subject,56,"\015\012",1);
227 $encoded_subject = "=?$charset?B?$encoded_subject?=";
228 $encoded_subject = str_replace("\015\012","?=\015\012 =?$charset?B?",$encoded_subject);
229 }
230 else $encoded_subject = $subject;
231
232 # Mailtext formatieren...
233
234 $mtpl = new Template;
235 $mtpl->read_file($tpl_mail);
236
237 $mtpl->set_var('EMAIL', $email);
238 $mtpl->set_var('NAME', $name);
239 $mtpl->set_var('SUBJECT',$subject);
240 $mtpl->set_var('TIME', $time);
241
242 # ... Umgebungsvariablen einfuegen...
243
244 if(isset($allowed_envs) && is_array($allowed_envs)) {
245 foreach($allowed_envs as $allowed_env) {
246 if(isset($_SERVER[$allowed_env])) {
247 $mtpl->set_var('ENV_'.$allowed_env,$_SERVER[$allowed_env]);
248 }
249 else {
250 $mtpl->set_var('ENV_'.$allowed_env,'');
251 }
252 }
253 }
254
255 # ... eigene Felder einfuegen
256
257 while(list($tpl_var,$user_field_content) = each($prepared_user_fields)) {
258 $mtpl->set_var($tpl_var,$user_field_content);
259 }
260
261 $mtpl->set_var('TEXT',$text);
262
263 $mtpl->parse();
264
265 $mailtext = $mtpl->get_template();
266
267 if($line_break) $mailtext = wordwrap($mailtext,$line_break);
268
269 $mailtext = preg_replace("/\015\012|\012|\015/","\n",$mailtext);
270
271 # ... und die Mail abschicken (bei einem Fehler erscheint eine Meldung)
272
273 if($recipient && is_array($aliases) && $aliases[$recipient]) {
274 # Empfaenger mit Alias-Namen
275
276 $mailto = $aliases[$recipient];
277 }
278
279 $mailto = semicolon_split($mailto);
280
281 for($x=0;$x<count($mailto);$x++) {
282 mail($mailto[$x],$encoded_subject,$mailtext,$header) or die($err_send_error);
283 }
284
285 if($captcha_enable) session_destroy();
286
287 # Mail wurde erfolgreich versendet, also wird die Dankesseite angezeigt
288
289 if($return_url) {
290 # Per Redirect auf Seite umleiten
291
292 if(!preg_match('!://!',$return_url)) {
293 # URL vervollstaendigen
294
295 $http_host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
296 $protocol = '';
297 $port = '';
298
299 if($_SERVER['HTTPS']) {
300 # SSL-verschluesseltes HTTP
301
302 $protocol = 'https://';
303 if($_SERVER['SERVER_PORT'] != 443) $port = ':'.$_SERVER['SERVER_PORT'];
304 }
305 else {
306 # Normales HTTP
307
308 $protocol = 'http://';
309 if($_SERVER['SERVER_PORT'] != 80) $port = ':'.$_SERVER['SERVER_PORT'];
310 }
311
312 if(preg_match('!^/!',$return_url)) {
313 $return_url = $protocol.$http_host.$port.$return_url;
314 }
315 else {
316 if(preg_match('!/$!',$_SERVER['SCRIPT_NAME'])) $path = substr($_SERVER['SCRIPT_NAME'],0,-1);
317 $path = substr($path,0,strrpos($path,'/')+1);
318 if(!preg_match('!^/!',$path)) $path = '/'.$path;
319
320 $return_url = $protocol.$http_host.$port.$path.$return_url;
321 }
322 }
323
324 header('Status: 303 See Other');
325 header('Location: '.$return_url);
326 }
327 else {
328 # Mit Template formatierte Seite anzeigen
329
330 $stpl = new Template;
331 $stpl->read_file($tpl_sent);
332
333 $stpl->set_var('EMAIL', plain($email));
334 $stpl->set_var('MAIL', plain($mailtext));
335 $stpl->set_var('NAME', plain($name));
336 $stpl->set_var('SUBJECT',plain($subject));
337 $stpl->set_var('TEXT', plain($text));
338 $stpl->set_var('TIME', plain($time));
339
340 $stpl->parse();
341
342 print $stpl->get_template();
343 }
344
345 #
346 ### Ende ###
347
348 ?>

patrick-canterino.de