27f758e4ffaa2fdb83053d8b66253ae65d53e5a3
bernd webinterface => /webinterface

bernd authored 17 years ago

1) <?php
2) 
bernd Logging aktiviert

bernd authored 16 years ago

3) require_once('inc/db_connect.php');
4) 
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

5) 
6) function db_query($query)
7) {
8)   DEBUG($query);
9)   $result = @mysql_query($query);
10)   if (mysql_error())
11)   {
12)     $error = mysql_error();
13)     logger("inc/base.php", "dberror", "mysql error: {$error}");
14)     system_failure('Beim Datenbankzugriff ist ein Fehler aufgetreten. Sollte dies wiederholt vorkommen, senden Sie bitte die Fehlermeldung ('.$error.') an einen Administrator.');
15)   }
16)   return $result; 
17) }
18) 
19) 
bernd Logging aktiviert

bernd authored 16 years ago

20) function logger($scriptname, $scope, $message)
21) {
22)   $user = 'NULL';
23)   if ($_SESSION['role'] == ROLE_SYSTEMUSER)
24)     $user = "'{$_SESSION['userinfo']['username']}'";
25)   elseif ($_SESSION['role'] == ROLE_CUSTOMER)
26)     $user = "'{$_SESSION['customerinfo']['customerno']}'";
27)   
28)   $remote = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
29) 
30)   $scriptname = mysql_real_escape_string($scriptname);
31)   $scope = mysql_real_escape_string($scope);
32)   $message = mysql_real_escape_string($message);
33) 
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

34)   db_query("INSERT INTO misc.scriptlog (remote, user,scriptname,scope,message) VALUES ('{$remote}', {$user}, '{$scriptname}', '{$scope}', '{$message}');");
bernd Logging aktiviert

bernd authored 16 years ago

35) }
36) 
37) 
bernd webinterface => /webinterface

bernd authored 17 years ago

38) function output($arg)
39) {
40)   global $output;
41)   $output .= $arg;
42) }
43) 
44) 
45) function random_string($nc, $a='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
46)     $l=strlen($a)-1; $r='';
47)     while($nc-->0) $r.=$a{mt_rand(0,$l)};
48)     return $r;
49)  }
50) 
51) 
52) function are_you_sure($query_string, $question)
53) {
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

54)   global $debugmode;
55)   if ($debugmode)
56)     $query_string = 'debug&amp;'.$query_string;
bernd webinterface => /webinterface

bernd authored 17 years ago

57)   $token = random_string(20);
bernd XSRF-kram fixed

bernd authored 16 years ago

58)   $_SESSION['are_you_sure_token'] = $token;
bernd webinterface => /webinterface

bernd authored 17 years ago

59)   output("<form action=\"?{$query_string}\" method=\"post\">\n");
60)   output("<p class=\"confirmation\">{$question}<br />\n");
61)   output("<input type=\"hidden\" name=\"random_token\" value=\"{$token}\" />\n");
bernd </form>

bernd authored 17 years ago

62)   output("<input type=\"submit\" name=\"really\" value=\"Ja\" />\n<input type=\"submit\" name=\"not_really\" value=\"Nein\" /></p>\n");
63)   output("</form>\n");
bernd webinterface => /webinterface

bernd authored 17 years ago

64) }
65) 
66) 
67) function user_is_sure()
68) {
69)   if (isset($_POST['really']))
70)   {
bernd XSRF-kram fixed

bernd authored 16 years ago

71)     if ($_POST['random_token'] == $_SESSION['are_you_sure_token'])
bernd webinterface => /webinterface

bernd authored 17 years ago

72)       return true;
73)     else
74)       system_failure("Possible Cross-site-request-forgery detected!");
75)   }
76)   elseif (isset($_POST['not_really']))
77)     return false;
78)   else
79)     return NULL;
80) }
81) 
82) 
83) 
bernd XSRF-kram fixed

bernd authored 16 years ago

84) function generate_form_token($form_id)
85) {
86)   require_once("inc/debug.php");
87)   $sessid = session_id();
88)   if ($sessid == "") 
89)   {
90)     DEBUG("Uh? Session not running? Wtf?");
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

91)     system_failure("Internal error!");
bernd XSRF-kram fixed

bernd authored 16 years ago

92)   }
93)   if (! isset($_SESSION['session_token']))
94)     $_SESSION['session_token'] = random_string(10);
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

95)   $formtoken = hash('sha256', $sessid.$form_id.$_SESSION['session_token']);
bernd XSRF-kram fixed

bernd authored 16 years ago

96)   return '<input type="hidden" name="formtoken" value="'.$formtoken.'" />'."\n";
97) }
98) 
99) 
100) function check_form_token($form_id)
101) {
102)   $formtoken = $_POST['formtoken'];
103)   $sessid = session_id();
104)   if ($sessid == "") 
105)   {
106)     DEBUG("Uh? Session not running? Wtf?");
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

107)     system_failure("Internal error!");
bernd XSRF-kram fixed

bernd authored 16 years ago

108)   }
109) 
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

110)   $correct_formtoken = hash('sha256', $sessid.$form_id.$_SESSION['session_token']);
bernd XSRF-kram fixed

bernd authored 16 years ago

111) 
112)   if (! ($formtoken == $correct_formtoken))
113)     system_failure("Possible cross-site-request-forgery!");
114) }
115) 
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

116) 
117) 
118) function internal_link($file, $label, $querystring = '')
119) {
120)   $debugstr = '';
121)   global $debugmode;
122)   if ($debugmode)
123)     $debugstr = 'debug&amp;';
124)   $querystring = str_replace('&', '&amp;', $querystring);
125) 
126)   return "<a href=\"{$file}?{$debugstr}${querystring}\">{$label}</a>";
127) }
128) 
129) 
130) function html_form($form_id, $scriptname, $querystring, $content)
131) {
132)   $debugstr = '';
133)   global $debugmode;
134)   if ($debugmode)
135)     $debugstr = 'debug&amp;';
136)   $querystring = str_replace('&', '&amp;', $querystring);
137)   $ret = '';
138)   $ret .= '<form action="'.$scriptname.'?'.$debugstr.$querystring.'" method="post">'."\n";
139)   $ret .= generate_form_token($form_id);
140)   $ret .= $content;
141)   $ret .= '</form>';
142)   return $ret;  
143) }
144) 
145) 
146) 
147)