Skip to content

Commit e937e9c

Browse files
committed
improve mysql8 support and user management
1 parent da40b49 commit e937e9c

7 files changed

Lines changed: 55 additions & 17 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ It has a simple and intuitive interface, quick record addition/editing/saving fe
66

77
Database Support
88
================
9-
As of the current version, MyWebSQL works with MySQL4 / MySQL5 ,SQLite and PostgreSQL databases.
9+
As of the current version, MyWebSQL works with MySQL (version 4, 5 and 8), SQLite and PostgreSQL databases.
1010

1111
Features
1212
========

js/users.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ function addNewUser() {
210210
hostname = $('#hostname').val();
211211
password = $('#userpass').val();
212212
password2 = $('#userpass2').val();
213+
nativepass = $('#nativepass').prop('checked') ? '1' : '0';
213214

214215
if (username == '' || hostname == '' || password == '') {
215216
jAlert(__("User information is incomplete or invalid"), __("User Manager"));
@@ -221,7 +222,7 @@ function addNewUser() {
221222
return false;
222223
}
223224

224-
json = {'username':username, 'hostname':hostname, 'pwd': password};
225+
json = {'username':username, 'hostname':hostname, 'pwd': password, native: nativepass};
225226
query = JSON.stringify(json);
226227

227228
setMessage(__('Please wait...'));
@@ -242,6 +243,7 @@ function updateUser() {
242243
password = $('#userpass').val();
243244
password2 = $('#userpass2').val();
244245
removepass = $('#nopass').prop('checked') ? '1' : '0';
246+
nativepass = $('#nativepass').prop('checked') ? '1' : '0';
245247

246248
if (removepass == '1')
247249
password = password2 = '';
@@ -263,6 +265,7 @@ function updateUser() {
263265
'hostname': hostname,
264266
'password': password,
265267
'removepass': removepass,
268+
'native': nativepass,
266269
'privileges': PRIVILEGES,
267270
'db_privileges': DB_PRIVILEGES
268271
};

lib/db/mysqli.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
define("SET_FLAG", 2048); /* Field is a set */
3030

3131
class DB_Mysqli {
32-
var $ip, $user, $password, $db;
32+
var $ip, $user, $password, $db, $port;
3333
var $conn;
3434
var $result; // array
3535
var $errMsg;
@@ -126,7 +126,14 @@ function connect($ip, $user, $password, $db="") {
126126
return $this->error(str_replace('{{NAME}}', 'MySQLi', __('{{NAME}} client library is not installed')));
127127
}
128128

129-
$this->conn = @mysqli_connect($ip, $user, $password);
129+
$port = ini_get("mysqli.default_port");
130+
if(strpos($ip, ':') !== FALSE) {
131+
list($ip, $port) = explode(':', $ip);
132+
} else {
133+
$port = ini_get("mysqli.default_port");
134+
}
135+
136+
$this->conn = mysqli_connect($ip, $user, $password, $db, $port);
130137
if (!$this->conn)
131138
return $this->error(__('Database connection failed to the server'));
132139

@@ -137,6 +144,7 @@ function connect($ip, $user, $password, $db="") {
137144
$this->user = $user;
138145
$this->password = $password;
139146
$this->db = $db;
147+
$this->port = $port;
140148

141149
$this->selectVersion();
142150
$this->query("SET CHARACTER SET 'utf8'");

lib/user.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public function __construct() {
4343
$this->dbPrivileges = array();
4444
}
4545

46-
public abstract function add();
46+
public abstract function add($native = false);
4747

4848
public abstract function update($newUsername, $newHost = '%');
4949

50-
public function updatePassword($newPassword = '') {
50+
public function updatePassword($newPassword = '', $native = false) {
5151
if( false == $this->userName || false == $this->host )
5252
return false;
5353

@@ -116,7 +116,7 @@ public function __construct() {
116116
parent::__construct();
117117
}
118118

119-
public function add() {
119+
public function add($native = false) {
120120
if( false == $this->userName || false == $this->host )
121121
return false;
122122

@@ -161,15 +161,19 @@ public function __construct() {
161161
parent::__construct();
162162
}
163163

164-
public function add() {
164+
public function add($native = false) {
165165
if( false == $this->userName || false == $this->host )
166166
return false;
167167

168168
$userName = self::$dbManager->escape( $this->userName );
169169
$host = self::$dbManager->escape( $this->host );
170170
$password = self::$dbManager->escape( $this->password );
171171

172-
$sql = "CREATE USER '$userName'@'$host' IDENTIFIED BY '$password'";
172+
$sql = "CREATE USER '$userName'@'$host' IDENTIFIED";
173+
if($native) {
174+
$sql .= " WITH mysql_native_password";
175+
}
176+
$sql .= " BY '$password'";
173177

174178
return self::$dbManager->query( $sql );
175179
}
@@ -191,4 +195,26 @@ public function update($newUsername, $newHost = '%') {
191195

192196
return true;
193197
}
198+
199+
public function updatePassword($newPassword = '', $native = false) {
200+
if( false == $this->userName || false == $this->host )
201+
return false;
202+
203+
$userName = self::$dbManager->escape( $this->userName );
204+
$host = self::$dbManager->escape( $this->host );
205+
$password = self::$dbManager->escape( $newPassword );
206+
207+
$sql = "ALTER USER '$userName'@'$host' IDENTIFIED";
208+
if($native) {
209+
$sql .= " WITH mysql_native_password";
210+
}
211+
$sql .= " BY '$password'";
212+
213+
if( false == self::$dbManager->query( $sql ) )
214+
return false;
215+
216+
$this->password = $newPassword;
217+
218+
return true;
219+
}
194220
}

lib/usermanager.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function getUser($userName, $host = '%') {
3333
return $user;
3434
}
3535

36-
public function add($userName, $host = '%', $password = '') {
36+
public function add($userName, $host = '%', $password = '', $native = false) {
3737
$user = User::factory( $this->legacy );
3838

3939
$user->userName = $userName;
4040
$user->host = $host;
4141
$user->password = $password;
4242

43-
return $user->add();
43+
return $user->add($native);
4444
}
4545

4646
public function update($currUsername, $currHost, $newUsername, $newHost) {
@@ -52,13 +52,13 @@ public function update($currUsername, $currHost, $newUsername, $newHost) {
5252
return $user->update( $newUsername, $newHost );
5353
}
5454

55-
public function updatePassword($userName, $host, $newPassword) {
55+
public function updatePassword($userName, $host, $newPassword, $native = false) {
5656
$user = User::factory( $this->legacy );
5757

5858
$user->userName = $userName;
5959
$user->host = $host;
6060

61-
return $user->updatePassword( $newPassword );
61+
return $user->updatePassword( $newPassword, $native );
6262
}
6363

6464
public function delete($userName, $host) {
@@ -73,7 +73,7 @@ public function delete($userName, $host) {
7373
public function getUsersList() {
7474
$tblName = Privileges::$privilegesTable;
7575

76-
$sql = "SELECT `User`, `Host`, `Password` FROM $tblName ORDER BY `User`, `Host`";
76+
$sql = "SELECT `User`, `Host` FROM $tblName ORDER BY `User`, `Host`";
7777

7878
if( false == $this->db->query( $sql ) )
7979
return array();
@@ -84,7 +84,7 @@ public function getUsersList() {
8484

8585
$user->userName = $row['User'];
8686
$user->host = $row['Host'];
87-
$user->password = $row['Password'];
87+
$user->password = '';
8888

8989
$users[] = $user;
9090
}

modules/usermanager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function addUser(&$db, $info, &$editor) {
9494
if (!is_object($info))
9595
return false;
9696

97-
return $editor->add($info->username, $info->hostname, $info->pwd);
97+
return $editor->add($info->username, $info->hostname, $info->pwd, v($info->native));
9898
}
9999

100100
function deleteUser(&$db, $info, &$editor) {
@@ -120,7 +120,7 @@ function updateUser(&$db, $info, &$editor) {
120120

121121
// change password only if requested
122122
if (isset($info->password) && $info->password != '') {
123-
$result = $editor->updatePassword($info->username, $info->hostname, $info->password);
123+
$result = $editor->updatePassword($info->username, $info->hostname, $info->password, v($info->native));
124124
if (!$result)
125125
return false;
126126
} else if (isset($info->removepass) && $info->removepass == '1') {

modules/views/usermanager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<div class="input"><span><?php echo __('Host'); ?>:</span><span><input type="text" size="30" name="hostname" id="hostname" /><span></div>
2626
<div class="input"><span><?php echo __('Password'); ?>:</span><span><input autocomplete="off" type="password" size="30" name="userpass" id="userpass" /><span></div>
2727
<div class="input"><span><?php echo __('Confirm Password'); ?>:</span><span><input autocomplete="off" type="password" size="30" name="userpass2" id="userpass2" /><span></div>
28+
<div class="input"><span><input type="checkbox" name="nativepass" id="nativepass" /><label class="right" for="nativepass"><?php echo __('Use Native Password'); ?></label></span></div>
2829
<div class="input"><span><input type="checkbox" name="nopass" id="nopass" /><label class="right" for="nopass"><?php echo __('Remove Password'); ?></label></span></div>
2930
</div>
3031
<div id="tab-global">

0 commit comments

Comments
 (0)