978a7d07f77d4ef51c15448bb74caf4b2d163759
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');
bernd add a function to handle re...

bernd authored 14 years ago

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

bernd authored 16 years ago

5) 
bernd Mehr config-optionen und co...

bernd authored 14 years ago

6) function config($key)
7) {
8)   global $config;
bernd config() liest die Datenban...

bernd authored 14 years ago

9)   if (array_key_exists($key, $config))
10)     return $config[$key];
11)   
12)   /* read configuration from database */
13)   $options = db_query( "SELECT `key`, value FROM misc.config" );
14)   
15)   while( $object = mysql_fetch_assoc( $options ) ) {
16) 	  $config[$object['key']]=$object['value'];
17)   }
18)   DEBUG($config);
bernd Mehr config-optionen und co...

bernd authored 14 years ago

19)   if (array_key_exists($key, $config))
20)     return $config[$key];
21)   else
bernd Logger mit Logleveln

bernd authored 14 years ago

22)     logger(LOG_ERR, "inc/base", "config", "Request to read nonexistant config option »{$key}«.");
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

23)     return NULL;
bernd Mehr config-optionen und co...

bernd authored 14 years ago

24) }
25) 
26) 
bernd add a function to handle re...

bernd authored 14 years ago

27) function redirect($target)
28) {
29)   global $debugmode;
30)   if (! $debugmode)
31)     header("Location: {$target}");
32)   die();
33) }
34) 
35) 
bernd Hilfsfunktionen bzgl. mehre...

bernd authored 14 years ago

36) function my_server_id()
37) {
38)   $uid = (int) $_SESSION['userinfo']['uid'];
39)   $result = db_query("SELECT server FROM system.useraccounts WHERE uid={$uid}");
40)   $r = mysql_fetch_assoc($result);
41)   DEBUG($r);
42)   return $r['server'];
43) }
44) 
45) 
46) function additional_servers()
47) {
48)   $uid = (int) $_SESSION['userinfo']['uid'];
49)   $result = db_query("SELECT server FROM system.user_server WHERE uid={$uid}");
50)   $servers = array();
51)   while ($s = mysql_fetch_assoc($result))
52)     $servers[] = $s['server'];
53)   DEBUG($servers);
54)   return $servers;
55) }
56) 
57) 
58) function server_names()
59) {
60)   $result = db_query("SELECT id, hostname FROM system.servers");
61)   $servers = array();
62)   while ($s = mysql_fetch_assoc($result))
63)     $servers[$s['id']] = $s['hostname'];
64)   DEBUG($servers);
65)   return $servers;
66) }
67) 
68) 
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

69) function db_query($query)
70) {
71)   DEBUG($query);
72)   $result = @mysql_query($query);
73)   if (mysql_error())
74)   {
75)     $error = mysql_error();
bernd Logger mit Logleveln

bernd authored 14 years ago

76)     logger(LOG_ERR, "inc/base", "dberror", "mysql error: {$error}");
bernd MySQL-Fehler kommen als Lat...

bernd authored 16 years ago

77)     system_failure('Interner Datenbankfehler: »'.iconv('ISO-8859-1', 'UTF-8', $error).'«.');
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

78)   }
bernd * wie viele Reihen wurden a...

bernd authored 15 years ago

79)   $count = @mysql_num_rows($result);
80)   if (! $count)
81)     $count = 'no';
82)   DEBUG("=> {$count} rows");
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

83)   return $result; 
84) }
85) 
86) 
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

87) 
88) function maybe_null($value)
89) {
bernd maybe_null behandelt jetzt...

bernd authored 14 years ago

90)   if ($value == NULL)
91)     return 'NULL';
92) 
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

93)   if (strlen( (string) $value ) > 0)
bernd Strings für mysql esapen (z...

bernd authored 16 years ago

94)     return "'".mysql_real_escape_string($value)."'";
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

95)   else
96)     return 'NULL';
97) }
98) 
bernd Logger mit Logleveln

bernd authored 14 years ago

99) #define('LOG_ERR', 3);
100) #define('LOG_WARNING', 4);
101) #define('LOG_INFO', 6);
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

102) 
bernd Logger mit Logleveln

bernd authored 14 years ago

103) function logger($severity, $scriptname, $scope, $message)
bernd Logging aktiviert

bernd authored 16 years ago

104) {
bernd Logger mit Logleveln

bernd authored 14 years ago

105)   if (config('logging') <= $severity)
bernd Logging konfigurierbar

bernd authored 16 years ago

106)     return;
107) 
bernd Logging aktiviert

bernd authored 16 years ago

108)   $user = 'NULL';
bernd Bugfix: Logger zeigt wieder...

bernd authored 16 years ago

109)   if ($_SESSION['role'] & ROLE_SYSTEMUSER)
bernd Logging aktiviert

bernd authored 16 years ago

110)     $user = "'{$_SESSION['userinfo']['username']}'";
bernd Bugfix: Logger zeigt wieder...

bernd authored 16 years ago

111)   elseif ($_SESSION['role'] & ROLE_CUSTOMER)
bernd Logging aktiviert

bernd authored 16 years ago

112)     $user = "'{$_SESSION['customerinfo']['customerno']}'";
113)   
114)   $remote = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
115) 
116)   $scriptname = mysql_real_escape_string($scriptname);
117)   $scope = mysql_real_escape_string($scope);
118)   $message = mysql_real_escape_string($message);
119) 
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

120)   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

121) }
122) 
bernd Allow Header entries and AJAX

bernd authored 15 years ago

123) function html_header($arg)
124) {
125)   global $html_header;
126)   $html_header .= $arg;
127) }
bernd Logging aktiviert

bernd authored 16 years ago

128) 
bernd webinterface => /webinterface

bernd authored 17 years ago

129) function output($arg)
130) {
131)   global $output;
132)   $output .= $arg;
133) }
134) 
135) 
136) function random_string($nc, $a='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
137)     $l=strlen($a)-1; $r='';
138)     while($nc-->0) $r.=$a{mt_rand(0,$l)};
139)     return $r;
140)  }
141) 
142) 
143) function are_you_sure($query_string, $question)
144) {
bernd * alle internen Links sinnv...

bernd authored 15 years ago

145)   $query_string = encode_querystring($query_string);
bernd webinterface => /webinterface

bernd authored 17 years ago

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

bernd authored 16 years ago

147)   $_SESSION['are_you_sure_token'] = $token;
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

148)   output("<h3>Sicherheitsabfrage</h3>
bernd * alle internen Links sinnv...

bernd authored 15 years ago

149)     <form action=\"{$query_string}\" method=\"post\">
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

150)     <div class=\"confirmation\">
151)       <div class=\"question\">{$question}</div>
152)       <p class=\"buttons\">
153)         <input type=\"hidden\" name=\"random_token\" value=\"{$token}\" />
154)         <input type=\"submit\" name=\"really\" value=\"Ja\" />
bernd Entities repariert

bernd authored 16 years ago

155)         &#160; &#160;
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

156)         <input type=\"submit\" name=\"not_really\" value=\"Nein\" />
157)       </p>
158)     </div>");
bernd </form>

bernd authored 16 years ago

159)   output("</form>\n");
bernd webinterface => /webinterface

bernd authored 17 years ago

160) }
161) 
162) 
163) function user_is_sure()
164) {
165)   if (isset($_POST['really']))
166)   {
bernd XSRF-kram fixed

bernd authored 16 years ago

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

bernd authored 17 years ago

168)       return true;
169)     else
170)       system_failure("Possible Cross-site-request-forgery detected!");
171)   }
172)   elseif (isset($_POST['not_really']))
173)     return false;
174)   else
175)     return NULL;
176) }
177) 
178) 
179) 
bernd XSRF-kram fixed

bernd authored 16 years ago

180) function generate_form_token($form_id)
181) {
182)   require_once("inc/debug.php");
183)   $sessid = session_id();
184)   if ($sessid == "") 
185)   {
186)     DEBUG("Uh? Session not running? Wtf?");
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

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

bernd authored 16 years ago

188)   }
189)   if (! isset($_SESSION['session_token']))
190)     $_SESSION['session_token'] = random_string(10);
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

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

bernd authored 16 years ago

192) }
193) 
194) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

195) function check_form_token($form_id, $formtoken = NULL)
bernd XSRF-kram fixed

bernd authored 16 years ago

196) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

197)   if ($formtoken == NULL)
198)     $formtoken = $_POST['formtoken'];
bernd XSRF-kram fixed

bernd authored 16 years ago

199)   $sessid = session_id();
200)   if ($sessid == "") 
201)   {
202)     DEBUG("Uh? Session not running? Wtf?");
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

203)     system_failure("Internal error! (Session not running)");
bernd XSRF-kram fixed

bernd authored 16 years ago

204)   }
205) 
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

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

bernd authored 16 years ago

207) 
208)   if (! ($formtoken == $correct_formtoken))
209)     system_failure("Possible cross-site-request-forgery!");
210) }
211) 
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

212) 
bernd Zeige Links auf dem Startse...

bernd authored 14 years ago

213) function have_module($modname)
214) {
bernd Mehr config-optionen und co...

bernd authored 14 years ago

215)   return in_array($modname, config('modules'));
bernd Zeige Links auf dem Startse...

bernd authored 14 years ago

216) }
217) 
218) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

219) function encode_querystring($querystring)
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

220) {
221)   global $debugmode;
222)   if ($debugmode)
bernd * alle internen Links sinnv...

bernd authored 15 years ago

223)     $querystring = 'debug&'.$querystring;
bernd Bug im Client-Zertfikat-Man...

bernd authored 15 years ago

224)   DEBUG($querystring);
bernd * alle internen Links sinnv...

bernd authored 15 years ago

225)   $query = explode('&', $querystring);
226)   $new_query = array();
227)   foreach ($query AS $item)
228)     if ($item != '')
229)     {
bernd fix a warning

bernd authored 14 years ago

230)       $split = explode('=', $item, 2);
231)       if (count($split) == 1)
232)         $new_query[] = $split[0];
bernd Bug im Client-Zertfikat-Man...

bernd authored 15 years ago

233)       else
bernd fix a warning

bernd authored 14 years ago

234)         $new_query[] = $split[0].'='.urlencode($split[1]);
bernd * alle internen Links sinnv...

bernd authored 15 years ago

235)     }
236)   $querystring = implode('&amp;', $new_query);
237)   if ($querystring)
238)     $querystring = '?'.$querystring;
bernd Bug im Client-Zertfikat-Man...

bernd authored 15 years ago

239)   DEBUG($querystring);
bernd * alle internen Links sinnv...

bernd authored 15 years ago

240)   return $querystring;
241) }
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

242) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

243) 
bernd addnew() eingeführt

bernd authored 14 years ago

244) function addnew($file, $label, $querystring = '')
245) {
bernd Parameter querystring bei a...

bernd authored 14 years ago

246)   output('<p class="addnew">'.internal_link($file, $label, $querystring).'</p>');
bernd addnew() eingeführt

bernd authored 14 years ago

247) }
248) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

249) 
250) function internal_link($file, $label, $querystring = '', $attribs = '')
251) {
bernd Erlaube absolute Links

bernd authored 14 years ago

252)   global $prefix;
bernd Das böse ==0

bernd authored 14 years ago

253)   if (strpos($file, '/') === 0)
bernd Erlaube absolute Links

bernd authored 14 years ago

254)   {
255)     $file = $prefix.substr($file, 1);
256)   }
bernd * alle internen Links sinnv...

bernd authored 15 years ago

257)   $querystring = encode_querystring($querystring);
258)   return "<a href=\"{$file}{$querystring}\" {$attribs} >{$label}</a>";
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

259) }
260) 
261) 
262) function html_form($form_id, $scriptname, $querystring, $content)
263) {
bernd * alle internen Links sinnv...

bernd authored 15 years ago

264)   $querystring = encode_querystring($querystring);
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

265)   $ret = '';
bernd * alle internen Links sinnv...

bernd authored 15 years ago

266)   $ret .= '<form action="'.$scriptname.$querystring.'" method="post">'."\n";
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

267)   $ret .= '<p style="display: none;"><input type="hidden" name="formtoken" value="'.generate_form_token($form_id).'" /></p>'."\n";
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

268)   $ret .= $content;
269)   $ret .= '</form>';
270)   return $ret;  
271) }
272) 
273) 
bernd * wie viele Reihen wurden a...

bernd authored 15 years ago

274) function html_select($name, $options, $default='', $free='')
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

275) {
276)   require_once('inc/security.php');
bernd * wie viele Reihen wurden a...

bernd authored 15 years ago

277)   $ret = "<select name=\"{$name}\" id=\"{$name}\" size=\"1\" {$free} >\n";
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

278)   foreach ($options as $key => $value)
279)   {
280)     $selected = '';
281)     if ($default == $key)
282)       $selected = ' selected="selected" ';
283)     $key = filter_input_general($key);
284)     $value = filter_input_general($value);
285)     $ret .= "  <option value=\"{$key}\"{$selected}>{$value}</option>\n";
286)   }
287)   $ret .= '</select>';
288)   return $ret;
289) }
290) 
bernd Neues Jabber-Modul (noch ni...

bernd authored 16 years ago

291) 
292)