cf54502a10b16c985ea28db17885d377226b6145
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) 
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) {
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

19)   $result = db_query("SELECT id, username, description, created FROM misc.mysql_accounts WHERE useraccount=? ORDER BY username", array($UID));
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

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

bernd authored 17 years ago

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

Bernd Wurst authored 10 years ago

23)   while ($item = $result->fetch())
bernd webinterface => /webinterface

bernd authored 17 years ago

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

bernd authored 15 years ago

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

bernd authored 17 years ago

26)   }
27)   return $list;
28) }
29) 
30) function get_mysql_databases($UID)
31) {
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

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

Bernd Wurst authored 10 years ago

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

bernd authored 17 years ago

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

Bernd Wurst authored 10 years ago

36)   while ($item = $result->fetch())
bernd webinterface => /webinterface

bernd authored 17 years ago

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

bernd authored 15 years ago

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

bernd authored 17 years ago

39)   }
40)   return $list;
41) }
42) 
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

43) function set_database_description($dbname, $description) 
44) {
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)     }
51)   }
52)   if ($thisdb == NULL) {
53)     system_failure('Ungültige Datenbank');
54)   }
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

55)   $args = array(":id" => $thisdb['id'],
56)                 ":desc" => filter_input_general($description));
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) 
60) function set_dbuser_description($username, $description) 
61) {
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');
71)   }
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

72)   $args = array(":id" => $thisuser['id'],
73)                 ":desc" => filter_input_general($description));
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) {
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

79)   $uid = (int) $_SESSION['userinfo']['uid'];
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

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));
bernd Link zu phpMyAdmin prominen...

bernd authored 13 years ago

81)   $ret = array();
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

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

bernd authored 13 years ago

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) {
92)   $uid = $_SESSION['userinfo']['uid'];
93)   global $mysql_access;
94)   if (!is_array($mysql_access))
95)   {
96)     $mysql_access = array();
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

97)     $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));
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

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

bernd authored 17 years ago

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

Bernd Wurst authored 10 years ago

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

bernd authored 17 years ago

101)       $mysql_access[$line->db][$line->user] = true;
102)   }
103)   return (array_key_exists($db, $mysql_access) && array_key_exists($account, $mysql_access[$db]));
104) }
105) 
106) 
107) function set_mysql_access($db, $account, $status)
108) {
109)   $uid = $_SESSION['userinfo']['uid'];
bernd Fehlende geschweifte Klamme...

bernd authored 13 years ago

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

bernd authored 17 years ago

111)   $query = '';
112)   if ($status)
113)   {
114)     if (get_mysql_access($db, $account))
115)       return NULL;
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

116)     $args = array(":db" => $db, ":uid" => $uid);
117)     $result = db_query("SELECT id FROM misc.mysql_database WHERE name=:db AND useraccount=:uid", $args);
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

119)     {
120)       logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find database {$db}");
121)       system_failure("cannot find database »{$db}«");
122)     }
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

123)     $args = array(":account" => $account, ":uid" => $uid);
124)     $result = db_query("SELECT id FROM misc.mysql_accounts WHERE username=:account AND useraccount=:uid", $args);
Bernd Wurst Umstellung auf PDO-Datenban...

Bernd Wurst authored 10 years ago

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

bernd authored 14 years ago

126)     {
127)       logger(LOG_ERR, "modules/mysql/include/mysql", "mysql", "cannot find user {$account}");
128)       system_failure("cannot find database user »{$account}«");
129)     }
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

130)     $args = array(":db" => $db, ":uid" => $uid, ":account" => $account);
131)     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);
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;
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

138)     $args = array(":db" => $db, ":account" => $account, ":uid" => $uid);
139)     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);
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 17 years ago

141)   }
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 Wurst Zeige deutlich an, welche M...

Bernd Wurst authored 10 years ago

150)     system_failure("Der eingegebene Benutzername entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
bernd webinterface => /webinterface

bernd authored 17 years ago

151)     return NULL;
152)   }
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

153)   $args = array(":uid" => $_SESSION['userinfo']['uid'],
154)                 ":username" => $username,
155)                 ":desc" => $description);
bernd Logger mit Logleveln

bernd authored 14 years ago

156)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "creating user »{$username}«");
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

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

bernd authored 17 years ago

158) }
159) 
160) 
161) function delete_mysql_account($username)
162) {
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

163)   $args = array(":uid" => $_SESSION['userinfo']['uid'],
164)                 ":username" => $username);
bernd Logger mit Logleveln

bernd authored 14 years ago

165)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "deleting user »{$username}«");
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

166)   db_query("DELETE FROM misc.mysql_accounts WHERE username=:username AND useraccount=:uid", $args);
bernd webinterface => /webinterface

bernd authored 17 years ago

167) }
168) 
169) 
Bernd Wurst String 'NULL' eliminiert

Bernd Wurst authored 10 years ago

170) function create_mysql_database($dbname, $description = NULL, $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 Wurst Zeige deutlich an, welche M...

Bernd Wurst authored 10 years ago

175)     system_failure("Der eingegebene Datenbankname entspricht leider nicht der Konvention. Bitte tragen Sie einen passenden Namen ein.");
bernd webinterface => /webinterface

bernd authored 17 years ago

176)     return NULL;
177)   }
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

178)   if (! in_array($server, additional_servers()) || ($server == my_server_id())) {
Bernd Wurst String 'NULL' eliminiert

Bernd Wurst authored 10 years ago

179)     $server = NULL;
Bernd Wurst Neue MySQL-Verwaltung

Bernd Wurst authored 11 years ago

180)   }
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

181)   $args = array(":dbname" => $dbname,
182)                 ":uid" => $_SESSION['userinfo']['uid'],
183)                 ":desc" => $description,
184)                 ":server" => $server);
bernd Logger mit Logleveln

bernd authored 14 years ago

185)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "creating database »{$dbname}«");
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

186)   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

187) }
188) 
189) 
190) function delete_mysql_database($dbname)
191) {
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

192)   $args = array(":dbname" => $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 Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

195)   db_query("DELETE FROM misc.mysql_database WHERE name=:dbname AND useraccount=:uid", $args);
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) {
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

215)   $args = array(":uid" => $_SESSION['userinfo']['uid'],
216)                 ":username" => $username,
217)                 ":password" => $password);
bernd Logger mit Logleveln

bernd authored 14 years ago

218)   logger(LOG_INFO, "modules/mysql/include/mysql", "mysql", "updating password for »{$username}«");
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

219)   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

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

bernd authored 15 years ago

223) function has_mysql_database($dbname)
224) {
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

225)   $args = array(":uid" => $_SESSION['userinfo']['uid'],
226)                 ":dbname" => $dbname);
227)   $result = db_query("SELECT NULL FROM misc.mysql_database WHERE name=:dbname AND useraccount=:uid", $args);
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 15 years ago

229) }
230) 
231) 
232) function has_mysql_user($username)
233) {
Bernd Wurst Modul mysql auf prepared st...

Bernd Wurst authored 10 years ago

234)   $args = array(":uid" => $_SESSION['userinfo']['uid'],
235)                 ":username" => $username);
236)   $result = db_query("SELECT NULL FROM misc.mysql_accounts WHERE username=:username AND useraccount=:uid", $args);
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 15 years ago

238) }
239) 
240)