c42127d3cab5c78430e578a07038f54f8cdff459
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 Copyright year update

Bernd Wurst authored 6 years ago

5) Written 2008-2018 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) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

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) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

19)     $result = db_query("SELECT id, username, description, created FROM misc.mysql_accounts WHERE useraccount=? ORDER BY username", array($UID));
20)     if ($result->rowCount() == 0) {
21)         return array();
22)     }
23)     $list = array();
24)     while ($item = $result->fetch()) {
25)         $list[] = $item;
26)     }
27)     return $list;
bernd webinterface => /webinterface

bernd authored 17 years ago

28) }
29) 
30) function get_mysql_databases($UID)
31) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

32)     $result = db_query("SELECT id, name, description, created FROM misc.mysql_database WHERE useraccount=? ORDER BY name", array($UID));
33)     if ($result->rowCount() == 0) {
34)         return array();
35)     }
36)     $list = array();
37)     while ($item = $result->fetch()) {
38)         $list[] = $item;
39)     }
40)     return $list;
bernd webinterface => /webinterface

bernd authored 17 years ago

41) }
42) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 11 years ago

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

Hanno authored 6 years ago

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

Bernd Wurst authored 11 years ago

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

Hanno authored 6 years ago

52)     if ($thisdb == null) {
53)         system_failure('Ungültige Datenbank');
54)     }
55)     $args = array(":id" => $thisdb['id'],
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 5 years ago

56)                 ":desc" => filter_input_oneline($description));
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 11 years ago

58) }
59) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 11 years ago

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

Hanno authored 6 years ago

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

Bernd Wurst authored 11 years ago

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

Hanno authored 6 years ago

72)     $args = array(":id" => $thisuser['id'],
Bernd Wurst Umstellung von filter_input...

Bernd Wurst authored 5 years ago

73)                 ":desc" => filter_input_oneline($description));
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 11 years ago

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) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

79)     $uid = (int) $_SESSION['userinfo']['uid'];
80)     $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=?", array($uid));
81)     $ret = array();
82)     while ($line = $result->fetch()) {
83)         $ret[$line['db']] = $line['hostname'];
84)     }
85)     DEBUG($ret);
86)     return $ret;
bernd Zeige Link zum phpMyAdmin i...

bernd authored 13 years ago

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

bernd authored 17 years ago

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

Hanno authored 6 years ago

92)     $uid = $_SESSION['userinfo']['uid'];
93)     global $mysql_access;
94)     if (!is_array($mysql_access)) {
95)         $mysql_access = array();
96)         $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", array(":uid" => $uid));
97)         if ($result->rowCount() == 0) {
98)             return false;
99)         }
100)         while ($line = $result->fetch(PDO::FETCH_OBJ)) {
101)             $mysql_access[$line->db][$line->user] = true;
102)         }
103)     }
104)     return (array_key_exists($db, $mysql_access) && array_key_exists($account, $mysql_access[$db]));
bernd webinterface => /webinterface

bernd authored 17 years ago

105) }
106) 
107) 
108) function set_mysql_access($db, $account, $status)
109) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

110)     $uid = $_SESSION['userinfo']['uid'];
111)     DEBUG("User »{$account}« soll ".($status ? "" : "NICHT ")."auf die Datenbank »{$db}« zugreifen");
112)     $query = '';
113)     if ($status) {
114)         if (get_mysql_access($db, $account)) {
115)             return null;
116)         }
117)         $args = array(":db" => $db, ":uid" => $uid);
118)         $result = db_query("SELECT id FROM misc.mysql_database WHERE name=:db AND useraccount=:uid", $args);
119)         if ($result->rowCount() != 1) {
120)             logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find database {$db}");
121)             system_failure("cannot find database »{$db}«");
122)         }
123)         $args = array(":account" => $account, ":uid" => $uid);
124)         $result = db_query("SELECT id FROM misc.mysql_accounts WHERE username=:account AND useraccount=:uid", $args);
125)         if ($result->rowCount() != 1) {
126)             logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find user {$account}");
127)             system_failure("cannot find database user »{$account}«");
128)         }
129)         $args = array(":db" => $db, ":uid" => $uid, ":account" => $account);
130)         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);
131)         logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "granting access on »{$db}« to »{$account}«");
132)     } else {
133)         if (! get_mysql_access($db, $account)) {
134)             return null;
135)         }
136)         $args = array(":db" => $db, ":account" => $account, ":uid" => $uid);
137)         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);
138)         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

139)     }
bernd webinterface => /webinterface

bernd authored 17 years ago

140) }
141) 
142) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

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

Hanno authored 6 years ago

145)     if (! validate_mysql_username($username)) {
146)         logger(LOG_WARNING, "modules/mysql/include/mysql", "mysql", "illegal username »{$username}«");
147)         system_failure("Der eingegebene Benutzername entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
148)         return null;
149)     }
150)     $args = array(":uid" => $_SESSION['userinfo']['uid'],
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

151)                 ":username" => $username,
152)                 ":desc" => $description);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

bernd authored 17 years ago

155) }
156) 
157) 
158) function delete_mysql_account($username)
159) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 10 years ago

161)                 ":username" => $username);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

bernd authored 17 years ago

164) }
165) 
166) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

bernd authored 17 years ago

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

Hanno authored 6 years ago

169)     if (! validate_mysql_dbname($dbname)) {
170)         logger(LOG_WARNING, "modules/mysql/include/mysql", "mysql", "illegal db-name »{$dbname}«");
171)         system_failure("Der eingegebene Datenbankname entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
172)         return null;
173)     }
174)     if (! in_array($server, additional_servers()) || ($server == my_server_id())) {
175)         $server = null;
176)     }
177)     $args = array(":dbname" => $dbname,
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

178)                 ":uid" => $_SESSION['userinfo']['uid'],
179)                 ":desc" => $description,
180)                 ":server" => $server);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

181)     logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "creating database »{$dbname}«");
182)     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

183) }
184) 
185) 
186) function delete_mysql_database($dbname)
187) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

188)     $args = array(":dbname" => $dbname,
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

189)                 ":uid" => $_SESSION['userinfo']['uid']);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

bernd authored 17 years ago

192) }
193) 
194) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

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

Hanno authored 6 years ago

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

bernd authored 17 years ago

199) }
200) 
201) 
bernd * Erlaube Beschreibung zu D...

bernd authored 15 years ago

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

bernd authored 17 years ago

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

Hanno authored 6 years ago

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

bernd authored 17 years ago

205) }
206) 
207) 
208) 
209) function set_mysql_password($username, $password)
210) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 10 years ago

212)                 ":username" => $username,
213)                 ":password" => $password);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

214)     logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "updating password for »{$username}«");
215)     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

216) }
217) 
218) 
bernd Fix of bugs #554 and #553

bernd authored 16 years ago

219) function has_mysql_database($dbname)
220) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 10 years ago

222)                 ":dbname" => $dbname);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

bernd authored 16 years ago

225) }
226) 
227) 
228) function has_mysql_user($username)
229) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 10 years ago

231)                 ":username" => $username);
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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