33a6604d4816bf8a8fde2a2d3194863490ac5b28
bernd input-filtering

bernd authored 16 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
Bernd Wurst Lizenzinfos in eigenes Modu...

Bernd Wurst authored 10 years ago

5) Written 2008-2014 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15) */
bernd input-filtering

bernd authored 16 years ago

16) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

17) require_once('inc/error.php');
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

18) require_once('vendor/autoload.php');
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

19) 
bernd input-filtering

bernd authored 16 years ago

20) 
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

21) function strong_password($password, $user = array())
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

22) {
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

23)   $passwordchecker = new ZxcvbnPhp\Zxcvbn();
24)   $strength = $passwordchecker->passwordStrength($password, $user);
Bernd Wurst Subusers-Modul in einer ers...

Bernd Wurst authored 12 years ago

25) 
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

26)   if ($strength['score'] < 2) {
27)     return "Das Passwort ist zu einfach!";
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

28)   }
29) 
Hanno Böck replace cracklib with zxcvbn

Hanno Böck authored 7 years ago

30)   return true;
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

31) }
32) 
33) 
bernd input-filtering

bernd authored 16 years ago

34) function filter_input_general( $input )
35) {
Bernd Wurst Weitere Prepared-Statements

Bernd Wurst authored 10 years ago

36)   if ($input === NULL) {
37)     return NULL;
38)   }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

39)   return htmlspecialchars(iconv('UTF-8', 'UTF-8', $input), ENT_QUOTES, 'UTF-8');
40) }
41) 
42) 
43) function verify_input_general( $input )
44) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

45)   if (filter_input_general($input) != $input) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

46)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Logger mit Logleveln

bernd authored 14 years ago

47)     logger(LOG_WARNING, 'inc/security', 'verify_input_general', 'Ungültige Daten: '.$input);
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

48)   }
bernd input-filtering

bernd authored 16 years ago

49) }
50) 
51) 
52) function filter_input_username( $input )
53) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

54)   return preg_replace("/[^[:alnum:]\_\.\+\-]/", "", $input );
hanno Hatte die Kompatibilität ge...

hanno authored 16 years ago

55) }
56) 
57) function verify_input_username( $input )
58) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

59)   if (filter_input_username( $input ) != $input) {
bernd Logger mit Logleveln

bernd authored 14 years ago

60)     logger(LOG_WARNING, 'inc/security', 'verify_input_username', 'Ungültige Daten: '.$input);
bernd Add IP-address patterns

bernd authored 15 years ago

61)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

62)   }
bernd input-filtering

bernd authored 16 years ago

63) }
64) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

65) 
66) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

67) function filter_input_hostname( $input, $wildcard=false )
bernd check auf hostname

bernd authored 16 years ago

68) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

69)   // FIXME: Eine "filter"-Funktion sollte keinen system_failure verursachen sondern einfach einen bereinigten String liefern.
70)   
Bernd Wurst Wildcard-CSRs können jetzt...

Bernd Wurst authored 11 years ago

71)   DEBUG('filter_input_hostname("'.$input.'", $wildcard='.$wildcard.')');
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

72)   $input = str_replace(array('Ä', 'Ö', 'Ü'), array('ä', 'ö', 'ü'), strtolower($input));
bernd Hostnames sollten nicht mit...

bernd authored 16 years ago

73)   $input = rtrim($input, "\t\n\r\x00 .");
74)   $input = ltrim($input, "\t\n\r\x00 .");
Bernd Wurst Hostnames dürfen keine Unte...

Bernd Wurst authored 7 years ago

75)   if (preg_replace("/[^.]_/", "", $input) != $input) {
76)     system_failure("Der Unterstrich ist nur als erstes Zeichen eines Hostnames erlaubt.");
77)   }
Bernd Wurst Erlaube underscore in hostn...

Bernd Wurst authored 9 years ago

78)   if (preg_replace("/[^[:alnum:]äöü_*\.\-]/", "", $input ) != $input)
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

79)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Erlaube wildcards bei vhosts

bernd authored 13 years ago

80)   if (preg_match("/^.+\*/", $input ))
81)     system_failure("Ihre Daten enthielten ungültige Zeichen (Wildcard-Stern muss ganz vorne stehen)!");
82)   if (! $wildcard && preg_replace("/^\*/", "", $input ) != $input)
bernd Erlaube * im Hostname

bernd authored 14 years ago

83)     system_failure("Ihre Daten enthielten ungültige Zeichen (Keine Wildcards erlaubt)!");
bernd Hostname darf kein »..« ent...

bernd authored 16 years ago

84)   if (strstr($input, '..'))
85)     system_failure("Ungültiger Hostname");
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

86)   return $input;
bernd check auf hostname

bernd authored 16 years ago

87) }
88) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

89) function verify_input_hostname( $input, $wildcard=false )
bernd Add IP-address patterns

bernd authored 15 years ago

90) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

91)   if (filter_input_hostname( $input, $wildcard ) != $input) {
bernd Logger mit Logleveln

bernd authored 14 years ago

92)     logger(LOG_WARNING, 'inc/security', 'verify_input_hostname', 'Ungültige Daten: '.$input);
bernd Add IP-address patterns

bernd authored 15 years ago

93)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
94)   }
95) }
96) 
97) 
98) function verify_input_ipv4( $input )
99) {
100)   if (! preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $input))
101)     system_failure('Keine IP-Adresse');
102) }
103) 
104) 
105) function verify_input_ipv6( $input )
106) {
107)   // ripped from Perl module Net-IPv6Addr v0.2
108)   if (! preg_match("/^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|[0-9a-f]{0,4}::|:(?::[a-f0-9]{1,4}){1,6}|(?:[a-f0-9]{1,4}:){1,6}:|(?:[a-f0-9]{1,4}:)(?::[a-f0-9]{1,4}){1,6}|(?:[a-f0-9]{1,4}:){2}(?::[a-f0-9]{1,4}){1,5}|(?:[a-f0-9]{1,4}:){3}(?::[a-f0-9]{1,4}){1,4}|(?:[a-f0-9]{1,4}:){4}(?::[a-f0-9]{1,4}){1,3}|(?:[a-f0-9]{1,4}:){5}(?::[a-f0-9]{1,4}){1,2}|(?:[a-f0-9]{1,4}:){6}(?::[a-f0-9]{1,4}))$/i", $input))
109)     system_failure("Ungültige IPv6-Adresse");
110) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

111) 
Hanno Böck Prüfe DNS-Records auf probl...

Hanno Böck authored 7 years ago

112) function verify_input_recorddata( $input )
113) {
114)   if (strstr($input, "\\") || strstr($input, '"'))
115)     system_failure("Ungültige Zeichen");
116) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

117) 
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

118) function filter_quotes( $input )
119) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

120)   return preg_replace('/["\'`]/', '', $input );
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

121) }
122) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

123) 
124) 
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

125) function filter_shell( $input )
126) {
bernd deprecation of ereg* functi...

bernd authored 14 years ago

127)   return preg_replace('/[^-[:alnum:]\_\.\+ßäöüÄÖÜ/%§=]/', '', $input );
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

128) }
129) 
130) function verify_shell( $input )
131) {
132)   if (filter_shell($input) != $input)
133)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

134) }
bernd input-filtering

bernd authored 16 years ago

135) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

136) 
137) 
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

138) function check_path( $input )
139) {
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

140)   DEBUG("checking {$input} for valid path name");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

141)   if ($input != filter_input_general($input))
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

142)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

143)     logger(LOG_WARNING, 'inc/security', 'check_path', 'HTML-Krams im Pfad: '.$input);
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

144)     DEBUG("HTML-Krams im Pfad");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

145)     return False;
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

146)   }
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

147)   $components = explode("/", $input);
148)   foreach ($components AS $item)
149)   {
150)     if ($item == '..')
151)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

152)       logger(LOG_WARNING, 'inc/security', 'check_path', '»..« im Pfad: '.$input);
bernd check auf hostname

bernd authored 16 years ago

153)       DEBUG("»..« im Pfad");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

154)       return False;
155)     }
156)   }
bernd Erlaube Leerzeichen im Pfad

bernd authored 13 years ago

157)   return (preg_match('/^[ A-Za-z0-9.@\/_-]*$/',$input) == 1);
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

158) }
159) 
160) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

161) function in_homedir($path)
162) {
163)   DEBUG("Prüfe »{$path}«");
164)   if (! check_path($path))
165)   {
166)     DEBUG('Kein Pfad');
167)     return False;
168)   }
169)   if (! isset($_SESSION['userinfo']['homedir']))
170)   {
171)     DEBUG("Kann homedir nicht ermitteln");
172)     return False;
173)   }
174)   return strncmp($_SESSION['userinfo']['homedir'], $path, count($_SESSION['userinfo']['homedir'])) == 0;
175) }
176) 
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

177) function check_date( $input )
178) {
Bernd Wurst Erlaube beim SEPA-Mandat au...

Bernd Wurst authored 9 years ago

179)   return (bool) preg_match("/[0-9]{4}-(0?[1-9]|10|11|12)-([012]?[0-9]|30|31)/", $input);
Bernd Wurst Erste Version des SEPA-Mand...

Bernd Wurst authored 10 years ago

180) }
181) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

182) 
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

183) function check_emailaddr( $input )
184) {
bernd Syntax-fehler

bernd authored 16 years ago

185)   return (bool) filter_var($input, FILTER_VALIDATE_EMAIL) == $input;
bernd don't be too complicated. /...

bernd authored 16 years ago

186) }
187) 
188) function check_domain( $input )
189) {
190)   return (bool) preg_match("/[a-z0-9\.\-]+\.[a-z]{2,4}$/i", $input );