451f7cfdff3597d4eed8b2c5f2a984f7c340eea5
bernd webinterface => /webinterface

bernd authored 17 years ago

1) <?php
2) 
3) function get_mysql_accounts($UID)
4) {
5)   $UID = (int) $UID;
hanno sortiere in mysql-ansicht u...

hanno authored 13 years ago

6)   $result = db_query("SELECT username, description, created FROM misc.mysql_accounts WHERE useraccount=$UID ORDER BY username");
bernd webinterface => /webinterface

bernd authored 17 years ago

7)   if (mysql_num_rows($result) == 0)
8)     return array();
9)   $list = array();
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

10)   while ($item = mysql_fetch_assoc($result))
bernd webinterface => /webinterface

bernd authored 17 years ago

11)   {
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

12)     $list[] = $item;
bernd webinterface => /webinterface

bernd authored 17 years ago

13)   }
14)   return $list;
15) }
16) 
17) function get_mysql_databases($UID)
18) {
19)   $UID = (int) $UID;
hanno sortiere in mysql-ansicht u...

hanno authored 13 years ago

20)   $result = db_query("SELECT id, name, description, created FROM misc.mysql_database WHERE useraccount=$UID ORDER BY name");
bernd webinterface => /webinterface

bernd authored 17 years ago

21)   if (mysql_num_rows($result) == 0)
22)     return array();
23)   $list = array();
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

24)   while ($item = mysql_fetch_assoc($result))
bernd webinterface => /webinterface

bernd authored 17 years ago

25)   {
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

26)     $list[] = $item;
bernd webinterface => /webinterface

bernd authored 17 years ago

27)   }
28)   return $list;
29) }
30) 
31) 
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

32) function servers_for_databases()
bernd Zeige Link zum phpMyAdmin i...

bernd authored 13 years ago

33) {
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

34)   $uid = (int) $_SESSION['userinfo']['uid'];
bernd Zeige Link zum phpMyAdmin i...

bernd authored 13 years ago

35)   
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

36)   $result = db_query("SELECT db.name AS db, hostname FROM misc.mysql_database AS db LEFT JOIN system.useraccounts AS u ON (db.useraccount=u.uid) LEFT JOIN system.servers ON (COALESCE(db.server, u.server) = servers.id) WHERE db.useraccount={$uid}");
37)   $ret = array();
38)   while ($line = mysql_fetch_assoc($result)) {
39)     $ret[$line['db']] = $line['hostname'];
40)   }
41)   DEBUG($ret);
42)   return $ret;
bernd Zeige Link zum phpMyAdmin i...

bernd authored 13 years ago

43) }
44) 
45) 
bernd webinterface => /webinterface

bernd authored 17 years ago

46) function get_mysql_access($db, $account)
47) {
48)   $uid = $_SESSION['userinfo']['uid'];
49)   global $mysql_access;
50)   if (!is_array($mysql_access))
51)   {
52)     $mysql_access = array();
bernd sql-abfragen abstrahiert

bernd authored 17 years ago

53)     $result = db_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};");
bernd webinterface => /webinterface

bernd authored 17 years ago

54)     if (mysql_num_rows($result) == 0)
55)       return false;
56)     while ($line = mysql_fetch_object($result))
57)       $mysql_access[$line->db][$line->user] = true;
58)   }
59)   return (array_key_exists($db, $mysql_access) && array_key_exists($account, $mysql_access[$db]));
60) }
61) 
62) 
63) function set_mysql_access($db, $account, $status)
64) {
65)   $uid = $_SESSION['userinfo']['uid'];
66)   $db = mysql_real_escape_string($db);
67)   $account = mysql_real_escape_string($account);
bernd Fehlende geschweifte Klamme...

bernd authored 13 years ago

68)   DEBUG("User »{$account}« soll ".($status ? "" : "NICHT ")."auf die Datenbank »{$db}« zugreifen");
bernd webinterface => /webinterface

bernd authored 17 years ago

69)   $query = '';
70)   if ($status)
71)   {
72)     if (get_mysql_access($db, $account))
73)       return NULL;
bernd stringlänge mit strlen und...

bernd authored 15 years ago

74)     $result = db_query("SELECT id FROM misc.mysql_database WHERE name='{$db}' AND useraccount={$uid} LIMIT 1");
75)     if (mysql_num_rows($result) != 1)
76)     {
77)       logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find database {$db}");
78)       system_failure("cannot find database »{$db}«");
79)     }
bernd typo

bernd authored 15 years ago

80)     $result = db_query("SELECT id FROM misc.mysql_accounts WHERE username='{$account}' AND useraccount={$uid} LIMIT 1");
bernd stringlänge mit strlen und...

bernd authored 15 years ago

81)     if (mysql_num_rows($result) != 1)
82)     {
83)       logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find user {$account}");
84)       system_failure("cannot find database user »{$account}«");
85)     }
bernd webinterface => /webinterface

bernd authored 17 years ago

86)     $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 Logger mit Logleveln

bernd authored 15 years ago

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

bernd authored 17 years ago

88)   }
89)   else
90)   {
91)     if (! get_mysql_access($db, $account))
92)       return NULL;
93)     $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 Logger mit Logleveln

bernd authored 15 years ago

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

bernd authored 17 years ago

95)   }
bernd sql-abfragen abstrahiert

bernd authored 17 years ago

96)   db_query($query);
bernd webinterface => /webinterface

bernd authored 17 years ago

97) }
98) 
99) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

100) function create_mysql_account($username, $description = '')
bernd webinterface => /webinterface

bernd authored 17 years ago

101) {
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

102)   if (! validate_mysql_username($username))
bernd webinterface => /webinterface

bernd authored 17 years ago

103)   {
bernd Logger mit Logleveln

bernd authored 15 years ago

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

bernd authored 17 years ago

105)     input_error("Der eingegebene Benutzername entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
106)     return NULL;
107)   }
108)   $uid = $_SESSION['userinfo']['uid'];
109)   $username = mysql_real_escape_string($username);
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

110)   $description = maybe_null($description);
bernd Logger mit Logleveln

bernd authored 15 years ago

111)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "creating user »{$username}«");
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

112)   db_query("INSERT INTO misc.mysql_accounts (username, password, useraccount, description) VALUES ('$username', '!', $uid, $description);");
bernd webinterface => /webinterface

bernd authored 17 years ago

113) }
114) 
115) 
116) function delete_mysql_account($username)
117) {
118)   $username = mysql_real_escape_string($username);
119)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 15 years ago

120)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "deleting user »{$username}«");
bernd sql-abfragen abstrahiert

bernd authored 17 years ago

121)   db_query("DELETE FROM misc.mysql_accounts WHERE username='{$username}' AND useraccount='{$uid}' LIMIT 1;");
bernd webinterface => /webinterface

bernd authored 17 years ago

122) }
123) 
124) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

125) function create_mysql_database($dbname, $description = '')
bernd webinterface => /webinterface

bernd authored 17 years ago

126) {
127)   if (! validate_mysql_dbname($dbname))
128)   {
bernd Logger mit Logleveln

bernd authored 15 years ago

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

bernd authored 17 years ago

130)     input_error("Der eingegebene Datenbankname entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
131)     return NULL;
132)   }
133)   $dbname = mysql_real_escape_string($dbname);
134)   $uid = $_SESSION['userinfo']['uid'];
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

135)   $description = maybe_null($description);
bernd Logger mit Logleveln

bernd authored 15 years ago

136)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "creating database »{$dbname}«");
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

137)   db_query("INSERT INTO misc.mysql_database (name, useraccount, description) VALUES ('$dbname', $uid, $description);");
bernd webinterface => /webinterface

bernd authored 17 years ago

138) }
139) 
140) 
141) function delete_mysql_database($dbname)
142) {
143)   $dbname = mysql_real_escape_string($dbname);
144)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 15 years ago

145)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "removing database »{$dbname}«");
bernd sql-abfragen abstrahiert

bernd authored 17 years ago

146)   db_query("DELETE FROM misc.mysql_database WHERE name='{$dbname}' AND useraccount='{$uid}' LIMIT 1;");
bernd webinterface => /webinterface

bernd authored 17 years ago

147) }
148) 
149) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

150) function validate_mysql_dbname($dbname)
bernd webinterface => /webinterface

bernd authored 17 years ago

151) {
152)   $sys_username = $_SESSION['userinfo']['username'];
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

153)   return preg_match("/^{$sys_username}(_[a-zA-Z0-9_-]+)?$/", $dbname);
bernd webinterface => /webinterface

bernd authored 17 years ago

154) }
155) 
156) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

157) function validate_mysql_username($username)
bernd webinterface => /webinterface

bernd authored 17 years ago

158) {
bernd stringlänge mit strlen und...

bernd authored 15 years ago

159)   return validate_mysql_dbname($username) && (strlen($username) <= 16);
bernd webinterface => /webinterface

bernd authored 17 years ago

160) }
161) 
162) 
163) 
164) function set_mysql_password($username, $password)
165) {
166)   $username = mysql_real_escape_string($username);
167)   $password = mysql_real_escape_string($password);
168)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 15 years ago

169)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "updating password for »{$username}«");
bernd sql-abfragen abstrahiert

bernd authored 17 years ago

170)   db_query("UPDATE misc.mysql_accounts SET password=PASSWORD('$password') WHERE username='$username' AND useraccount=$uid;");
bernd webinterface => /webinterface

bernd authored 17 years ago

171) }
172) 
173) 
bernd Fix of bugs #554 and #553

bernd authored 16 years ago

174) function has_mysql_database($dbname)
175) {
176)   $uid = $_SESSION['userinfo']['uid'];
177)   $dbname = mysql_real_escape_string($dbname);
178)   $result = db_query("SELECT NULL FROM misc.mysql_database WHERE name='{$dbname}' AND useraccount='{$uid}' LIMIT 1;");
179)   return (mysql_num_rows($result) == 1);
180) }
181) 
182) 
183) function has_mysql_user($username)
184) {
185)   $uid = $_SESSION['userinfo']['uid'];
186)   $userame = mysql_real_escape_string($username);
187)   $result = db_query("SELECT NULL FROM misc.mysql_accounts WHERE username='{$username}' AND useraccount='{$uid}' LIMIT 1;");
188)   return (mysql_num_rows($result) == 1);
189) }
190) 
191)