5a312ede689a3941c8f5ebe772516c9120a1d9d7
bernd webinterface => /webinterface

bernd authored 17 years ago

1) <?php
2) 
bernd Logging aktiviert

bernd authored 16 years ago

3) require_once('inc/base.php');
bernd webinterface => /webinterface

bernd authored 17 years ago

4) require_once('inc/debug.php');
5) require_once('inc/error.php');
6) 
7) require_once('inc/db_connect.php');
8) 
9) define('ROLE_ANONYMOUS', 0);
bernd Auch mailaccounts können si...

bernd authored 16 years ago

10) define('ROLE_MAILACCOUNT', 1);
bernd VMail-accounts können sich...

bernd authored 16 years ago

11) define('ROLE_VMAIL_ACCOUNT', 2);
12) define('ROLE_SYSTEMUSER', 4);
13) define('ROLE_CUSTOMER', 8);
14) define('ROLE_SYSADMIN', 16);
bernd webinterface => /webinterface

bernd authored 17 years ago

15) 
16) 
17) // Gibt die Rolle aus, wenn das Passwort stimmt
18) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

19) function find_role($login, $password, $i_am_admin = False)
bernd webinterface => /webinterface

bernd authored 17 years ago

20) {
21)   $login = mysql_real_escape_string($login);
22)   // Domain-Admin?  <not implemented>
23)   // System-User?
24)   $uid = (int) $login;
25)   if ($uid == 0)
26)     $uid = 'NULL';
bernd nicht mehr der user mit der...

bernd authored 16 years ago

27)   $result = db_query("SELECT passwort AS password, kundenaccount AS `primary`, ((SELECT acc.uid FROM system.v_useraccounts AS acc LEFT JOIN system.gruppenzugehoerigkeit USING (uid) LEFT JOIN system.gruppen AS g ON (g.gid=gruppenzugehoerigkeit.gid) WHERE g.name='admin' AND acc.uid=u.uid) IS NOT NULL) AS admin FROM system.v_useraccounts AS u LEFT JOIN system.passwoerter USING(uid) WHERE u.uid={$uid} OR username='{$login}' LIMIT 1;");
bernd webinterface => /webinterface

bernd authored 17 years ago

28)   if (@mysql_num_rows($result) > 0)
29)   {
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

30)     $entry = mysql_fetch_object($result);
31)     $db_password = $entry->password;
bernd webinterface => /webinterface

bernd authored 17 years ago

32)     $hash = crypt($password, $db_password);
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

33)     if ($hash == $db_password || $i_am_admin)
34)     {
35)       $role = ROLE_SYSTEMUSER;
36)       if ($entry->primary)
37)         $role = $role | ROLE_CUSTOMER;
38)       if ($entry->admin)
39)         $role = $role | ROLE_SYSADMIN;
bernd Logger mit Logleveln

bernd authored 14 years ago

40)       logger(LOG_INFO, "session/checkuser", "login", "logged in systemuser »{$login}«.");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

41)       return $role;
42)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

43)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing useraccount »{$login}«.");
bernd Mehr logging

bernd authored 15 years ago

44)   } else {
bernd Logger mit Logleveln

bernd authored 14 years ago

45)     logger(LOG_WARNING, "session/checkuser", "login", "did not find useraccount »{$login}«. trying other roles...");
bernd webinterface => /webinterface

bernd authored 17 years ago

46)   }
47) 
48)   // Customer?
49)   $customerno = (int) $login;
50)   $pass = sha1($password);
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

51)   $result = db_query("SELECT passwort AS password FROM kundendaten.kunden WHERE status=0 AND id={$customerno} AND passwort='{$pass}';");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

52)   if ($i_am_admin)
53)     $result = db_query("SELECT passwort AS password FROM kundendaten.kunden WHERE status=0 AND id={$customerno}");
bernd webinterface => /webinterface

bernd authored 17 years ago

54)   if (@mysql_num_rows($result) > 0)
55)   {
56)     return ROLE_CUSTOMER;
57)   }
58) 
bernd Auch mailaccounts können si...

bernd authored 16 years ago

59)   // Mail-Account
60)   $account = $login;
61)   if (! strstr($account, '@')) {
bernd Mehr config-optionen und co...

bernd authored 14 years ago

62)     $account .= '@'.config('masterdomain');
bernd Auch mailaccounts können si...

bernd authored 16 years ago

63)   }
64)   $result = db_query("SELECT cryptpass FROM mail.courier_mailaccounts WHERE account='{$account}' LIMIT 1;");
65)   if (@mysql_num_rows($result) > 0)
66)   {
67)     $entry = mysql_fetch_object($result);
68)     $db_password = $entry->cryptpass;
69)     $hash = crypt($password, $db_password);
70)     if ($hash == $db_password || $i_am_admin)
71)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

72)       logger(LOG_INFO, "session/checkuser", "login", "logged in e-mail-account »{$account}«.");
bernd Auch mailaccounts können si...

bernd authored 16 years ago

73)       return ROLE_MAILACCOUNT;
74)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

75)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing e-mail-account »{$account}«.");
bernd Auch mailaccounts können si...

bernd authored 16 years ago

76)   }
77)   
bernd VMail-accounts können sich...

bernd authored 16 years ago

78)   // virtueller Mail-Account
79)   $account = $login;
80)   $result = db_query("SELECT cryptpass FROM mail.courier_virtual_accounts WHERE account='{$account}' LIMIT 1;");
81)   if (@mysql_num_rows($result) > 0)
82)   {
83)     $entry = mysql_fetch_object($result);
84)     $db_password = $entry->cryptpass;
85)     $hash = crypt($password, $db_password);
86)     if ($hash == $db_password || $i_am_admin)
87)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

88)       logger(LOG_INFO, "session/checkuser", "login", "logged in virtual e-mail-account »{$account}«.");
bernd VMail-accounts können sich...

bernd authored 16 years ago

89)       return ROLE_VMAIL_ACCOUNT;
90)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

91)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing virtual e-mail-account »{$account}«.");
bernd VMail-accounts können sich...

bernd authored 16 years ago

92)   }
93)   
bernd Auch mailaccounts können si...

bernd authored 16 years ago

94) 
95) 
bernd webinterface => /webinterface

bernd authored 17 years ago

96)   // Nothing?
97)   return NULL;
98) }
99) 
100) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

101) function get_customer_info($customer)
bernd webinterface => /webinterface

bernd authored 17 years ago

102) {
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

103)   if (! $_SESSION['role'] & ROLE_CUSTOMER)
104)     return array();
bernd webinterface => /webinterface

bernd authored 17 years ago

105)   $ret = array();
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

106)   $customerno = (int) $customer;
107)   if ($customerno != 0)
108)   {
109)     DEBUG('Looking up customerinfo for customer no. '.$customerno);
110)     $result = db_query("SELECT id, anrede, firma, CONCAT_WS(' ', vorname, nachname) AS name FROM kundendaten.kunden WHERE id={$customerno} LIMIT 1;");
111)   }
112)   else
113)   {
114)     $username = mysql_real_escape_string($customer);
115)     DEBUG('looking up customer info for username '.$username);
bernd Tabelle 'kundenkontakt' kom...

bernd authored 14 years ago

116)     $result = db_query("SELECT id, anrede, firma, CONCAT_WS(' ', vorname, nachname) AS name, email FROM kundendaten.kunden AS k JOIN system.v_useraccounts AS u ON (u.kunde=k.id) WHERE u.username='{$username}'");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

117)   }
bernd webinterface => /webinterface

bernd authored 17 years ago

118)   if (@mysql_num_rows($result) == 0)
119)     system_failure("Konnte Kundendaten nicht auslesen!");
120)   $data = mysql_fetch_object($result);
121) 
122)   $ret['customerno'] = $data->id;
123)   $ret['title'] = $data->anrede;
124)   $ret['company'] = $data->firma;
125)   $ret['name'] = $data->name;
bernd Tabelle 'kundenkontakt' kom...

bernd authored 14 years ago

126)   $ret['email'] = $data->email;
bernd webinterface => /webinterface

bernd authored 17 years ago

127)   
128)   return $ret;
129) }
130) 
131) 
132) function get_user_info($username)
133) {
134)   $username = mysql_real_escape_string($username);
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

135)   $result = db_query("SELECT kunde AS customerno, username, uid, homedir, name
136)                       FROM system.v_useraccounts WHERE username='{$username}' OR uid='{$username}' LIMIT 1");
bernd webinterface => /webinterface

bernd authored 17 years ago

137)   if (mysql_num_rows($result) < 1)
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

138)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

139)     logger(LOG_ERR, "session/checkuser", "login", "error reading user's data: »{$username}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

140)     system_failure('Das Auslesen Ihrer Benutzerdaten ist fehlgeschlagen. Bitte melden Sie dies einem Administrator');
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

141)   }
bernd webinterface => /webinterface

bernd authored 17 years ago

142)   $val = @mysql_fetch_object($result);
143)   return array(
144)           'username'      => $val->username,
145)           'customerno'    => $val->customerno,
146)           'uid'           => $val->uid,
147)           'homedir'       => $val->homedir,
148)           'name'          => $val->name,
149)           );
150) }
151) 
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

152) function set_customer_verified($customerno)
153) {
154)   $customerno = (int) $customerno;
155)   db_query("UPDATE kundendaten.kunden SET status=0 WHERE id={$customerno};");
bernd Logger mit Logleveln

bernd authored 14 years ago

156)   logger(LOG_INFO, "session/checkuser", "register", "set customer's status to 0.");
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

157) }
158) 
159) function set_customer_lastlogin($customerno)
160) {
161)   $customerno = (int) $customerno;
162)   db_query("UPDATE kundendaten.kunden SET lastlogin=NOW() WHERE id={$customerno};");
163) }
164) 
bernd webinterface => /webinterface

bernd authored 17 years ago

165) function set_customer_password($customerno, $newpass)
166) {
167)   $customerno = (int) $customerno;
168)   $newpass = sha1($newpass);
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

169)   db_query("UPDATE kundendaten.kunden SET passwort='$newpass' WHERE id='".$customerno."' LIMIT 1");
bernd Logger mit Logleveln

bernd authored 14 years ago

170)   logger(LOG_INFO, "session/checkuser", "pwchange", "changed customer's password.");
bernd webinterface => /webinterface

bernd authored 17 years ago

171) }
172) 
173) 
174) function set_systemuser_password($uid, $newpass)
175) {
176)   $uid = (int) $uid;
177)   require_once('inc/base.php');
178)   $salt = random_string(8);
179)   $newpass = crypt($newpass, "\$1\${$salt}\$");
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

180)   db_query("UPDATE system.passwoerter SET passwort='$newpass' WHERE uid='".$uid."' LIMIT 1");
bernd Logger mit Logleveln

bernd authored 14 years ago

181)   logger(LOG_INFO, "session/checkuser", "pwchange", "changed user's password.");
bernd webinterface => /webinterface

bernd authored 17 years ago

182) }
183) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

184) 
185) function setup_session($role, $useridentity)
186) {
187)   session_regenerate_id();
188)   $_SESSION['role'] = $role;
189)   if ($role & ROLE_SYSTEMUSER)
190)   {
191)     DEBUG("We are system user");
192)     $info = get_user_info($useridentity);
193)     $_SESSION['userinfo'] = $info;
bernd Logger mit Logleveln

bernd authored 14 years ago

194)     logger(LOG_INFO, "session/start", "login", "logged in user »{$info['username']}«");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

195)     $useridentity = $info['customerno'];
196)   }
197)   if ($role & ROLE_CUSTOMER)
198)   {
199)     $info = get_customer_info($useridentity);
200)     $_SESSION['customerinfo'] = $info;
201)     set_customer_lastlogin($info['customerno']);
bernd Logger mit Logleveln

bernd authored 14 years ago

202)     logger(LOG_INFO, "session/start", "login", "logged in customer no »{$info['customerno']}«");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

203)   }
bernd Auch mailaccounts können si...

bernd authored 16 years ago

204)   if ($role & ROLE_MAILACCOUNT)
205)   {
206)     $id = $useridentity;
207)     if (! strstr($id, '@'))
bernd Mehr config-optionen und co...

bernd authored 14 years ago

208)       $id .= '@'.config('masterdomain');
bernd Auch mailaccounts können si...

bernd authored 16 years ago

209)     $_SESSION['mailaccount'] = $id;
210)     DEBUG("We are mailaccount: {$_SESSION['mailaccount']}");
211)   }
bernd VMail-accounts können sich...

bernd authored 16 years ago

212)   if ($role & ROLE_VMAIL_ACCOUNT)
213)   {
214)     $id = $useridentity;
215)     $_SESSION['mailaccount'] = $id;
216)     DEBUG("We are virtual mailaccount: {$_SESSION['mailaccount']}");
217)   }
bernd Auch mailaccounts können si...

bernd authored 16 years ago

218) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

219) }
220)