891087335eda66373b3766558fdffcb7d8f8a749
bernd webinterface => /webinterface

bernd authored 17 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
Bernd Wurst Updated copyright notice (2...

Bernd Wurst authored 11 years ago

5) Written 2008-2013 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15) */
bernd webinterface => /webinterface

bernd authored 17 years ago

16) 
17) function get_mysql_accounts($UID)
18) {
19)   $UID = (int) $UID;
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

20)   $result = db_query("SELECT id, username, description, created FROM misc.mysql_accounts WHERE useraccount=$UID ORDER BY username");
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) function get_mysql_databases($UID)
32) {
33)   $UID = (int) $UID;
hanno sortiere in mysql-ansicht u...

hanno authored 12 years ago

34)   $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

35)   if (mysql_num_rows($result) == 0)
36)     return array();
37)   $list = array();
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

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

bernd authored 15 years ago

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

bernd authored 17 years ago

41)   }
42)   return $list;
43) }
44) 
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

45) function set_database_description($dbname, $description) 
46) {
47)   $dbs = get_mysql_databases($_SESSION['userinfo']['uid']);
48)   $thisdb = NULL;
49)   foreach ($dbs as $db) {
50)     if ($db['name'] == $dbname) {
51)       $thisdb = $db;
52)     }
53)   }
54)   if ($thisdb == NULL) {
55)     system_failure('Ungültige Datenbank');
56)   }
57)   $description = maybe_null(filter_input_general($description));
58)   db_query("UPDATE misc.mysql_database SET description={$description} WHERE id={$thisdb['id']}");
59) }
60) 
61) function set_dbuser_description($username, $description) 
62) {
63)   $users = get_mysql_accounts($_SESSION['userinfo']['uid']);
64)   $thisuser = NULL;
65)   foreach ($users as $user) {
66)     if ($user['username'] == $username) {
67)       $thisuser = $user;
68)     }
69)   }
70)   if ($thisuser == NULL) {
71)     system_failure('Ungültiger Benutzer');
72)   }
73)   $description = maybe_null(filter_input_general($description));
74)   db_query("UPDATE misc.mysql_accounts SET description={$description} WHERE id={$thisuser['id']}");
75) }
bernd webinterface => /webinterface

bernd authored 17 years ago

76) 
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

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

bernd authored 13 years ago

78) {
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

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

bernd authored 13 years ago

80)   
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

81)   $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}");
82)   $ret = array();
83)   while ($line = mysql_fetch_assoc($result)) {
84)     $ret[$line['db']] = $line['hostname'];
85)   }
86)   DEBUG($ret);
87)   return $ret;
bernd Zeige Link zum phpMyAdmin i...

bernd authored 13 years ago

88) }
89) 
90) 
bernd webinterface => /webinterface

bernd authored 17 years ago

91) function get_mysql_access($db, $account)
92) {
93)   $uid = $_SESSION['userinfo']['uid'];
94)   global $mysql_access;
95)   if (!is_array($mysql_access))
96)   {
97)     $mysql_access = array();
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

98)     $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

99)     if (mysql_num_rows($result) == 0)
100)       return false;
101)     while ($line = mysql_fetch_object($result))
102)       $mysql_access[$line->db][$line->user] = true;
103)   }
104)   return (array_key_exists($db, $mysql_access) && array_key_exists($account, $mysql_access[$db]));
105) }
106) 
107) 
108) function set_mysql_access($db, $account, $status)
109) {
110)   $uid = $_SESSION['userinfo']['uid'];
111)   $db = mysql_real_escape_string($db);
112)   $account = mysql_real_escape_string($account);
bernd Fehlende geschweifte Klamme...

bernd authored 13 years ago

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

bernd authored 17 years ago

114)   $query = '';
115)   if ($status)
116)   {
117)     if (get_mysql_access($db, $account))
118)       return NULL;
bernd stringlänge mit strlen und...

bernd authored 14 years ago

119)     $result = db_query("SELECT id FROM misc.mysql_database WHERE name='{$db}' AND useraccount={$uid} LIMIT 1");
120)     if (mysql_num_rows($result) != 1)
121)     {
122)       logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find database {$db}");
123)       system_failure("cannot find database »{$db}«");
124)     }
bernd typo

bernd authored 14 years ago

125)     $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 14 years ago

126)     if (mysql_num_rows($result) != 1)
127)     {
128)       logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find user {$account}");
129)       system_failure("cannot find database user »{$account}«");
130)     }
bernd webinterface => /webinterface

bernd authored 17 years ago

131)     $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 14 years ago

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

bernd authored 17 years ago

133)   }
134)   else
135)   {
136)     if (! get_mysql_access($db, $account))
137)       return NULL;
138)     $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 14 years ago

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

bernd authored 17 years ago

140)   }
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

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

bernd authored 17 years ago

142) }
143) 
144) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

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

bernd authored 15 years ago

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

bernd authored 17 years ago

148)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 17 years ago

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

bernd authored 15 years ago

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

bernd authored 14 years ago

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

bernd authored 15 years ago

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

bernd authored 17 years ago

158) }
159) 
160) 
161) function delete_mysql_account($username)
162) {
163)   $username = mysql_real_escape_string($username);
164)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 17 years ago

167) }
168) 
169) 
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

170) function create_mysql_database($dbname, $description = '', $server = NULL)
bernd webinterface => /webinterface

bernd authored 17 years ago

171) {
172)   if (! validate_mysql_dbname($dbname))
173)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 17 years ago

175)     input_error("Der eingegebene Datenbankname entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
176)     return NULL;
177)   }
178)   $dbname = mysql_real_escape_string($dbname);
179)   $uid = $_SESSION['userinfo']['uid'];
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

180)   $description = maybe_null($description); 
181)   $server = (int) $server;
182)   if (! in_array($server, additional_servers()) || ($server == my_server_id())) {
183)     $server = 'NULL';
184)   }
bernd Logger mit Logleveln

bernd authored 14 years ago

185)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "creating database »{$dbname}«");
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

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

bernd authored 17 years ago

187) }
188) 
189) 
190) function delete_mysql_database($dbname)
191) {
192)   $dbname = mysql_real_escape_string($dbname);
193)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 17 years ago

196) }
197) 
198) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

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

bernd authored 15 years ago

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

bernd authored 17 years ago

203) }
204) 
205) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

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

bernd authored 14 years ago

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

bernd authored 17 years ago

209) }
210) 
211) 
212) 
213) function set_mysql_password($username, $password)
214) {
215)   $username = mysql_real_escape_string($username);
216)   $password = mysql_real_escape_string($password);
217)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 17 years ago

220) }
221) 
222) 
bernd Fix of bugs #554 and #553

bernd authored 15 years ago

223) function has_mysql_database($dbname)
224) {
225)   $uid = $_SESSION['userinfo']['uid'];
226)   $dbname = mysql_real_escape_string($dbname);
227)   $result = db_query("SELECT NULL FROM misc.mysql_database WHERE name='{$dbname}' AND useraccount='{$uid}' LIMIT 1;");
228)   return (mysql_num_rows($result) == 1);
229) }
230) 
231) 
232) function has_mysql_user($username)
233) {
234)   $uid = $_SESSION['userinfo']['uid'];
235)   $userame = mysql_real_escape_string($username);
236)   $result = db_query("SELECT NULL FROM misc.mysql_accounts WHERE username='{$username}' AND useraccount='{$uid}' LIMIT 1;");
237)   return (mysql_num_rows($result) == 1);
238) }
239) 
240)