fb92f399e1c896d31edec5dbc0ed57928f18d6ba
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) 
5) function logger($scriptname, $scope, $message)
6) {
7)   $user = 'NULL';
8)   if ($_SESSION['role'] == ROLE_SYSTEMUSER)
9)     $user = "'{$_SESSION['userinfo']['username']}'";
10)   elseif ($_SESSION['role'] == ROLE_CUSTOMER)
11)     $user = "'{$_SESSION['customerinfo']['customerno']}'";
12)   
13)   $remote = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
14) 
15)   $scriptname = mysql_real_escape_string($scriptname);
16)   $scope = mysql_real_escape_string($scope);
17)   $message = mysql_real_escape_string($message);
18) 
19)   $query = "INSERT INTO misc.scriptlog (remote, user,scriptname,scope,message) VALUES ('{$remote}', {$user}, '{$scriptname}', '{$scope}', '{$message}');";
20)   DEBUG($query);
21)   @mysql_query($query);
22)   if (mysql_error())
23)     system_failure(mysql_error());
24) 
25) }
26) 
27) 
bernd webinterface => /webinterface

bernd authored 17 years ago

28) function output($arg)
29) {
30)   global $output;
31)   $output .= $arg;
32) }
33) 
34) 
35) function random_string($nc, $a='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
36)     $l=strlen($a)-1; $r='';
37)     while($nc-->0) $r.=$a{mt_rand(0,$l)};
38)     return $r;
39)  }
40) 
41) 
42) function are_you_sure($query_string, $question)
43) {
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

44)   global $debugmode;
45)   if ($debugmode)
46)     $query_string = 'debug&amp;'.$query_string;
bernd webinterface => /webinterface

bernd authored 17 years ago

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

bernd authored 16 years ago

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

bernd authored 17 years ago

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

bernd authored 17 years ago

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

bernd authored 17 years ago

54) }
55) 
56) 
57) function user_is_sure()
58) {
59)   if (isset($_POST['really']))
60)   {
bernd XSRF-kram fixed

bernd authored 16 years ago

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

bernd authored 17 years ago

62)       return true;
63)     else
64)       system_failure("Possible Cross-site-request-forgery detected!");
65)   }
66)   elseif (isset($_POST['not_really']))
67)     return false;
68)   else
69)     return NULL;
70) }
71) 
72) 
73) 
bernd XSRF-kram fixed

bernd authored 16 years ago

74) function generate_form_token($form_id)
75) {
76)   require_once("inc/debug.php");
77)   $sessid = session_id();
78)   if ($sessid == "") 
79)   {
80)     DEBUG("Uh? Session not running? Wtf?");
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

86)   return '<input type="hidden" name="formtoken" value="'.$formtoken.'" />'."\n";
87) }
88) 
89) 
90) function check_form_token($form_id)
91) {
92)   $formtoken = $_POST['formtoken'];
93)   $sessid = session_id();
94)   if ($sessid == "") 
95)   {
96)     DEBUG("Uh? Session not running? Wtf?");
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

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

bernd authored 16 years ago

98)   }
99) 
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

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

bernd authored 16 years ago

101) 
102)   if (! ($formtoken == $correct_formtoken))
103)     system_failure("Possible cross-site-request-forgery!");
104) }
105) 
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

106) 
107) 
108) function internal_link($file, $label, $querystring = '')
109) {
110)   $debugstr = '';
111)   global $debugmode;
112)   if ($debugmode)
113)     $debugstr = 'debug&amp;';
114)   $querystring = str_replace('&', '&amp;', $querystring);
115) 
116)   return "<a href=\"{$file}?{$debugstr}${querystring}\">{$label}</a>";
117) }
118) 
119) 
120) function html_form($form_id, $scriptname, $querystring, $content)
121) {
122)   $debugstr = '';
123)   global $debugmode;
124)   if ($debugmode)
125)     $debugstr = 'debug&amp;';
126)   $querystring = str_replace('&', '&amp;', $querystring);
127)   $ret = '';
128)   $ret .= '<form action="'.$scriptname.'?'.$debugstr.$querystring.'" method="post">'."\n";
129)   $ret .= generate_form_token($form_id);
130)   $ret .= $content;
131)   $ret .= '</form>';
132)   return $ret;  
133) }
134) 
135) 
136) 
137)