dcc202fb249a446ac15c7cf413b9f1b4a3f31b58
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) 
Hanno Böck Change license from CC0 to...

Hanno Böck authored 2 years ago

5) Written 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) 
Hanno Böck Change license from CC0 to...

Hanno Böck authored 2 years ago

9) This code is published under a 0BSD license.
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

10) 
11) 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.
12) */
bernd webinterface => /webinterface

bernd authored 17 years ago

13) 
14) function get_mysql_accounts($UID)
15) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

16)     $result = db_query("SELECT id, username, description, created FROM misc.mysql_accounts WHERE useraccount=? ORDER BY username", [$UID]);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

17)     if ($result->rowCount() == 0) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

18)         return [];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

19)     }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

20)     $list = [];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

21)     while ($item = $result->fetch()) {
22)         $list[] = $item;
23)     }
24)     return $list;
bernd webinterface => /webinterface

bernd authored 17 years ago

25) }
26) 
27) function get_mysql_databases($UID)
28) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

29)     $result = db_query("SELECT id, name, description, created FROM misc.mysql_database WHERE useraccount=? ORDER BY name", [$UID]);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

30)     if ($result->rowCount() == 0) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

31)         return [];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

32)     }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

33)     $list = [];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

34)     while ($item = $result->fetch()) {
35)         $list[] = $item;
36)     }
37)     return $list;
bernd webinterface => /webinterface

bernd authored 17 years ago

38) }
39) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

40) function set_database_description($dbname, $description)
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

41) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

42)     $dbs = get_mysql_databases($_SESSION['userinfo']['uid']);
43)     $thisdb = null;
44)     foreach ($dbs as $db) {
45)         if ($db['name'] == $dbname) {
46)             $thisdb = $db;
47)         }
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

48)     }
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

49)     if ($thisdb == null) {
50)         system_failure('Ungültige Datenbank');
51)     }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

52)     $args = [":id" => $thisdb['id'],
53)                 ":desc" => filter_input_oneline($description), ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

54)     db_query("UPDATE misc.mysql_database SET description=:desc WHERE id=:id", $args);
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

55) }
56) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

57) function set_dbuser_description($username, $description)
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

58) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

59)     $users = get_mysql_accounts($_SESSION['userinfo']['uid']);
60)     $thisuser = null;
61)     foreach ($users as $user) {
62)         if ($user['username'] == $username) {
63)             $thisuser = $user;
64)         }
65)     }
66)     if ($thisuser == null) {
67)         system_failure('Ungültiger Benutzer');
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

68)     }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

69)     $args = [":id" => $thisuser['id'],
70)                 ":desc" => filter_input_oneline($description), ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

71)     db_query("UPDATE misc.mysql_accounts SET description=:desc WHERE id=:id", $args);
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

72) }
bernd webinterface => /webinterface

bernd authored 17 years ago

73) 
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

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

bernd authored 13 years ago

75) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

76)     $uid = (int) $_SESSION['userinfo']['uid'];
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

77)     $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]);
78)     $ret = [];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

79)     while ($line = $result->fetch()) {
80)         $ret[$line['db']] = $line['hostname'];
81)     }
82)     DEBUG($ret);
83)     return $ret;
bernd Zeige Link zum phpMyAdmin i...

bernd authored 13 years ago

84) }
85) 
86) 
bernd webinterface => /webinterface

bernd authored 17 years ago

87) function get_mysql_access($db, $account)
88) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

89)     $uid = $_SESSION['userinfo']['uid'];
90)     global $mysql_access;
91)     if (!is_array($mysql_access)) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

92)         $mysql_access = [];
93)         $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", [":uid" => $uid]);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

94)         if ($result->rowCount() == 0) {
95)             return false;
96)         }
97)         while ($line = $result->fetch(PDO::FETCH_OBJ)) {
98)             $mysql_access[$line->db][$line->user] = true;
99)         }
100)     }
101)     return (array_key_exists($db, $mysql_access) && array_key_exists($account, $mysql_access[$db]));
bernd webinterface => /webinterface

bernd authored 17 years ago

102) }
103) 
104) 
105) function set_mysql_access($db, $account, $status)
106) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

107)     $uid = $_SESSION['userinfo']['uid'];
108)     DEBUG("User »{$account}« soll ".($status ? "" : "NICHT ")."auf die Datenbank »{$db}« zugreifen");
109)     $query = '';
110)     if ($status) {
111)         if (get_mysql_access($db, $account)) {
112)             return null;
113)         }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

114)         $args = [":db" => $db, ":uid" => $uid];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

115)         $result = db_query("SELECT id FROM misc.mysql_database WHERE name=:db AND useraccount=:uid", $args);
116)         if ($result->rowCount() != 1) {
117)             logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find database {$db}");
118)             system_failure("cannot find database »{$db}«");
119)         }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

120)         $args = [":account" => $account, ":uid" => $uid];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

121)         $result = db_query("SELECT id FROM misc.mysql_accounts WHERE username=:account AND useraccount=:uid", $args);
122)         if ($result->rowCount() != 1) {
123)             logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find user {$account}");
124)             system_failure("cannot find database user »{$account}«");
125)         }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

126)         $args = [":db" => $db, ":uid" => $uid, ":account" => $account];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

127)         db_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))", $args);
128)         logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "granting access on »{$db}« to »{$account}«");
129)     } else {
130)         if (! get_mysql_access($db, $account)) {
131)             return null;
132)         }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

133)         $args = [":db" => $db, ":account" => $account, ":uid" => $uid];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

134)         db_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)", $args);
135)         logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "revoking access on »{$db}« from »{$account}«");
bernd stringlänge mit strlen und...

bernd authored 15 years ago

136)     }
bernd webinterface => /webinterface

bernd authored 17 years ago

137) }
138) 
139) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

141) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

142)     if (! validate_mysql_username($username)) {
143)         logger(LOG_WARNING, "modules/mysql/include/mysql", "mysql", "illegal username »{$username}«");
144)         system_failure("Der eingegebene Benutzername entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
145)         return null;
146)     }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

147)     $args = [":uid" => $_SESSION['userinfo']['uid'],
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

148)                 ":username" => $username,
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

149)                 ":desc" => $description, ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

150)     logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "creating user »{$username}«");
151)     db_query("INSERT INTO misc.mysql_accounts (username, password, useraccount, description) VALUES (:username, '!', :uid, :desc)", $args);
bernd webinterface => /webinterface

bernd authored 17 years ago

152) }
153) 
154) 
155) function delete_mysql_account($username)
156) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

157)     $args = [":uid" => $_SESSION['userinfo']['uid'],
158)                 ":username" => $username, ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

159)     logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "deleting user »{$username}«");
160)     db_query("DELETE FROM misc.mysql_accounts WHERE username=:username AND useraccount=:uid", $args);
bernd webinterface => /webinterface

bernd authored 17 years ago

161) }
162) 
163) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

164) function create_mysql_database($dbname, $description = null, $server = null)
bernd webinterface => /webinterface

bernd authored 17 years ago

165) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

166)     if (! validate_mysql_dbname($dbname)) {
167)         logger(LOG_WARNING, "modules/mysql/include/mysql", "mysql", "illegal db-name »{$dbname}«");
168)         system_failure("Der eingegebene Datenbankname entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
169)         return null;
170)     }
171)     if (! in_array($server, additional_servers()) || ($server == my_server_id())) {
172)         $server = null;
173)     }
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

174)     $args = [":dbname" => $dbname,
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

175)                 ":uid" => $_SESSION['userinfo']['uid'],
176)                 ":desc" => $description,
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

177)                 ":server" => $server, ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

178)     logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "creating database »{$dbname}«");
179)     db_query("INSERT INTO misc.mysql_database (name, useraccount, server, description) VALUES (:dbname, :uid, :server, :desc)", $args);
bernd webinterface => /webinterface

bernd authored 17 years ago

180) }
181) 
182) 
183) function delete_mysql_database($dbname)
184) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

185)     $args = [":dbname" => $dbname,
186)                 ":uid" => $_SESSION['userinfo']['uid'], ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

187)     logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "removing database »{$dbname}«");
188)     db_query("DELETE FROM misc.mysql_database WHERE name=:dbname AND useraccount=:uid", $args);
bernd webinterface => /webinterface

bernd authored 17 years ago

189) }
190) 
191) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

193) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

194)     $sys_username = $_SESSION['userinfo']['username'];
195)     return preg_match("/^{$sys_username}(_[a-zA-Z0-9_-]+)?$/", $dbname);
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_username($username)
bernd webinterface => /webinterface

bernd authored 17 years ago

200) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

bernd authored 17 years ago

202) }
203) 
204) 
205) 
206) function set_mysql_password($username, $password)
207) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

208)     $args = [":uid" => $_SESSION['userinfo']['uid'],
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

209)                 ":username" => $username,
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

210)                 ":password" => $password, ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

211)     logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "updating password for »{$username}«");
212)     db_query("UPDATE misc.mysql_accounts SET password=PASSWORD(:password) WHERE username=:username AND useraccount=:uid", $args);
bernd webinterface => /webinterface

bernd authored 17 years ago

213) }
214) 
215) 
bernd Fix of bugs #554 and #553

bernd authored 16 years ago

216) function has_mysql_database($dbname)
217) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

218)     $args = [":uid" => $_SESSION['userinfo']['uid'],
219)                 ":dbname" => $dbname, ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

220)     $result = db_query("SELECT NULL FROM misc.mysql_database WHERE name=:dbname AND useraccount=:uid", $args);
221)     return ($result->rowCount() == 1);
bernd Fix of bugs #554 and #553

bernd authored 16 years ago

222) }
223) 
224) 
225) function has_mysql_user($username)
226) {
Hanno Böck Codingstyle PSR12 + array s...

Hanno Böck authored 3 years ago

227)     $args = [":uid" => $_SESSION['userinfo']['uid'],
228)                 ":username" => $username, ];
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

229)     $result = db_query("SELECT NULL FROM misc.mysql_accounts WHERE username=:username AND useraccount=:uid", $args);
230)     return ($result->rowCount() == 1);