f1f231f5e074dfa038e70a67d39849e80f2b4b4d
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 Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

21)   if ($result->rowCount() == 0)
bernd webinterface => /webinterface

bernd authored 17 years ago

22)     return array();
23)   $list = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

24)   while ($item = $result->fetch())
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 13 years ago

34)   $result = db_query("SELECT id, name, description, created FROM misc.mysql_database WHERE useraccount=$UID ORDER BY name");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

35)   if ($result->rowCount() == 0)
bernd webinterface => /webinterface

bernd authored 17 years ago

36)     return array();
37)   $list = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

38)   while ($item = $result->fetch())
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();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

83)   while ($line = $result->fetch()) {
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

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 17 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 Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

99)     if ($result->rowCount() == 0)
bernd webinterface => /webinterface

bernd authored 17 years ago

100)       return false;
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

101)     while ($line = $result->fetch(PDO::FETCH_OBJ))
bernd webinterface => /webinterface

bernd authored 17 years ago

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'];
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

111)   $db = db_escape_string($db);
112)   $account = db_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 15 years ago

119)     $result = db_query("SELECT id FROM misc.mysql_database WHERE name='{$db}' AND useraccount={$uid} LIMIT 1");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

120)     if ($result->rowCount() != 1)
bernd stringlänge mit strlen und...

bernd authored 15 years ago

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 15 years ago

125)     $result = db_query("SELECT id FROM misc.mysql_accounts WHERE username='{$account}' AND useraccount={$uid} LIMIT 1");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

126)     if ($result->rowCount() != 1)
bernd stringlänge mit strlen und...

bernd authored 15 years ago

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 15 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 15 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 17 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 15 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'];
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

154)   $username = db_escape_string($username);
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 15 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) {
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

163)   $username = db_escape_string($username);
bernd webinterface => /webinterface

bernd authored 17 years ago

164)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 15 years ago

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

bernd authored 17 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 15 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)   }
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

178)   $dbname = db_escape_string($dbname);
bernd webinterface => /webinterface

bernd authored 17 years ago

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 15 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) {
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

192)   $dbname = db_escape_string($dbname);
bernd webinterface => /webinterface

bernd authored 17 years ago

193)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 15 years ago

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

bernd authored 17 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 15 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) {
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

215)   $username = db_escape_string($username);
216)   $password = db_escape_string($password);
bernd webinterface => /webinterface

bernd authored 17 years ago

217)   $uid = $_SESSION['userinfo']['uid'];
bernd Logger mit Logleveln

bernd authored 15 years ago

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

bernd authored 17 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 16 years ago

223) function has_mysql_database($dbname)
224) {
225)   $uid = $_SESSION['userinfo']['uid'];
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

226)   $dbname = db_escape_string($dbname);
bernd Fix of bugs #554 and #553

bernd authored 16 years ago

227)   $result = db_query("SELECT NULL FROM misc.mysql_database WHERE name='{$dbname}' AND useraccount='{$uid}' LIMIT 1;");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

228)   return ($result->rowCount() == 1);
bernd Fix of bugs #554 and #553

bernd authored 16 years ago

229) }
230) 
231) 
232) function has_mysql_user($username)
233) {
234)   $uid = $_SESSION['userinfo']['uid'];
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

235)   $userame = db_escape_string($username);
bernd Fix of bugs #554 and #553

bernd authored 16 years ago

236)   $result = db_query("SELECT NULL FROM misc.mysql_accounts WHERE username='{$username}' AND useraccount='{$uid}' LIMIT 1;");
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

237)   return ($result->rowCount() == 1);
bernd Fix of bugs #554 and #553

bernd authored 16 years ago

238) }
239) 
240)