ed72341ed529cc499b65b6198f916d05f2a28ef5
bernd webinterface => /webinterface

bernd authored 17 years ago

1) <?php
2) 
3) function get_mysql_accounts($UID)
4) {
5)   $UID = (int) $UID;
6)   $result = mysql_query("SELECT username FROM misc.mysql_accounts WHERE useraccount=$UID");
7)   if (mysql_num_rows($result) == 0)
8)     return array();
9)   $list = array();
10)   while ($item = mysql_fetch_object($result))
11)   {
12)     array_push($list, $item->username);
13)   }
14)   return $list;
15) }
16) 
17) function get_mysql_databases($UID)
18) {
19)   $UID = (int) $UID;
20)   $result = mysql_query("SELECT name FROM misc.mysql_database WHERE useraccount=$UID");
21)   if (mysql_num_rows($result) == 0)
22)     return array();
23)   $list = array();
24)   while ($item = mysql_fetch_object($result))
25)   {
26)     array_push($list, $item->name);
27)   }
28)   return $list;
29) }
30) 
31) 
32) function get_mysql_access($db, $account)
33) {
34)   $uid = $_SESSION['userinfo']['uid'];
35)   global $mysql_access;
36)   if (!is_array($mysql_access))
37)   {
38)     $mysql_access = array();
39)     $result = mysql_query("SELECT db.name AS db, acc.username AS user FROM misc.mysql_access AS access LEFT JOIN misc.mysql_database AS db ON (db.id=access.database) LEFT JOIN misc.mysql_accounts AS acc ON (acc.id = access.user) WHERE acc.useraccount={$uid} OR db.useraccount={$uid};");
40)     if (mysql_num_rows($result) == 0)
41)       return false;
42)     while ($line = mysql_fetch_object($result))
43)       $mysql_access[$line->db][$line->user] = true;
44)   }
45)   return (array_key_exists($db, $mysql_access) && array_key_exists($account, $mysql_access[$db]));
46) }
47) 
48) 
49) function set_mysql_access($db, $account, $status)
50) {
51)   $uid = $_SESSION['userinfo']['uid'];
52)   $db = mysql_real_escape_string($db);
53)   $account = mysql_real_escape_string($account);
54)   $query = '';
55)   if ($status)
56)   {
57)     if (get_mysql_access($db, $account))
58)       return NULL;
59)     $query = "INSERT INTO misc.mysql_access (`database`,user) VALUES ((SELECT id FROM misc.mysql_database WHERE name='{$db}' AND useraccount={$uid} LIMIT 1), (SELECT id FROM misc.mysql_accounts WHERE username='{$account}' AND useraccount={$uid}));";
bernd Logging in allen Modulen

bernd authored 16 years ago

60)     logger("modules/mysql/include/mysql.php", "mysql", "granting access on »{$db}« to »{$account}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

61)   }
62)   else
63)   {
64)     if (! get_mysql_access($db, $account))
65)       return NULL;
66)     $query = "DELETE FROM misc.mysql_access WHERE `database`=(SELECT id FROM misc.mysql_database WHERE name='{$db}' AND useraccount={$uid} LIMIT 1) AND user=(SELECT id FROM misc.mysql_accounts WHERE username='{$account}' AND useraccount={$uid});";
bernd Logging in allen Modulen

bernd authored 16 years ago

67)     logger("modules/mysql/include/mysql.php", "mysql", "revoking access on »{$db}« from »{$account}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

68)   }
69)   DEBUG($query);
70)   mysql_query($query);
71)   if (mysql_error())
72)     system_failure(mysql_error());
73) }
74) 
75) 
76) function create_mysql_account($username)
77) {
78)   if (! validate_mysql_dbname($username))
79)   {
bernd Logging in allen Modulen

bernd authored 16 years ago

80)     logger("modules/mysql/include/mysql.php", "mysql", "illegal username »{$username}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

81)     input_error("Der eingegebene Benutzername entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
82)     return NULL;
83)   }
84)   $uid = $_SESSION['userinfo']['uid'];
85)   $username = mysql_real_escape_string($username);
bernd Logging in allen Modulen

bernd authored 16 years ago

86)   logger("modules/mysql/include/mysql.php", "mysql", "creating user »{$username}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

87)   mysql_query("INSERT INTO misc.mysql_accounts (username, password, useraccount) VALUES ('$username', '!', $uid);");
88)   if (mysql_error())
89)     system_failure(mysql_error());
90) }
91) 
92) 
93) function delete_mysql_account($username)
94) {
95)   $username = mysql_real_escape_string($username);
96)   $uid = $_SESSION['userinfo']['uid'];
bernd Logging in allen Modulen

bernd authored 16 years ago

97)   logger("modules/mysql/include/mysql.php", "mysql", "deleting user »{$username}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

98)   mysql_query("DELETE FROM misc.mysql_accounts WHERE username='{$username}' AND useraccount='{$uid}' LIMIT 1;");
99)   if (mysql_error())
100)     system_failure(mysql_error());
101) }
102) 
103) 
104) function create_mysql_database($dbname)
105) {
106)   if (! validate_mysql_dbname($dbname))
107)   {
bernd Logging in allen Modulen

bernd authored 16 years ago

108)     logger("modules/mysql/include/mysql.php", "mysql", "illegal db-name »{$dbname}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

109)     input_error("Der eingegebene Datenbankname entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
110)     return NULL;
111)   }
112)   $dbname = mysql_real_escape_string($dbname);
113)   $uid = $_SESSION['userinfo']['uid'];
bernd Logging in allen Modulen

bernd authored 16 years ago

114)   logger("modules/mysql/include/mysql.php", "mysql", "creating database »{$dbname}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

115)   mysql_query("INSERT INTO misc.mysql_database (name, useraccount) VALUES ('$dbname', $uid);");
116)   if (mysql_error())
117)     system_failure(mysql_error());
118) }
119) 
120) 
121) function delete_mysql_database($dbname)
122) {
123)   $dbname = mysql_real_escape_string($dbname);
124)   $uid = $_SESSION['userinfo']['uid'];
bernd Logging in allen Modulen

bernd authored 16 years ago

125)   logger("modules/mysql/include/mysql.php", "mysql", "removing database »{$dbname}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

126)   mysql_query("DELETE FROM misc.mysql_database WHERE name='{$dbname}' AND useraccount='{$uid}' LIMIT 1;");
127)   if (mysql_error())
128)     system_failure(mysql_error());
129) }
130) 
131) 
132) function validate_mysql_username($username)
133) {
134)   $sys_username = $_SESSION['userinfo']['username'];
135)   return preg_match("/^{$sys_username}(_[a-zA-Z0-9_-]+)?$/", $username);
136) }
137) 
138) 
139) function validate_mysql_dbname($dbname)
140) {
141)   // Funktioniert! ;-)
142)   return validate_mysql_username($dbname);
143) }
144) 
145) 
146) 
147) function set_mysql_password($username, $password)
148) {
149)   $username = mysql_real_escape_string($username);
150)   $password = mysql_real_escape_string($password);
151)   $uid = $_SESSION['userinfo']['uid'];
bernd Logging in allen Modulen

bernd authored 16 years ago

152)   logger("modules/mysql/include/mysql.php", "mysql", "updating password for »{$username}«");