-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLManager.php
261 lines (234 loc) · 6.34 KB
/
MySQLManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace SQL;
use kernel;
use mysqli;
class MySQLManager extends \Core\Module
{
public $connection = null;
private $host = 'localhost';
private $port = 3306;
private $username = null;
private $password = null;
private $database = null;
/**
* Constructor.
*
* Can set parameters from an array that can contain following keys:
* - host (default localhost)
* - port (default 3306)
* - username (required)
* - password (required)
* - database
*
* @param $parameters optional array of connection parameters
*/
public function __construct($parameters = null)
{
if (is_array($parameters))
{
/* get parameters from given array */
$this->host = isset($parameters['host']) ? $parameters['host'] : 'localhost';
$this->port = isset($parameters['port']) ? intval($parameters['port']) : 3306;
$this->username = isset($parameters['user']) ? $parameters['user'] : null;
$this->password = isset($parameters['password']) ? $parameters['password'] : null;
$this->database = isset($parameters['dbname']) ? $parameters['dbname'] : null;
}
else
{
$this->host = $this->getModuleValue('host') ? $this->getModuleValue('host') : 'localhost';
$this->port = $this->getModuleValue('port') ? $this->getModuleValue('port') : 'localhost';
$this->username = $this->getModuleValue('user');
$this->password = $this->getModuleValue('password');
$this->database = $this->getModuleValue('dbname');
}
}
public function connect()
{
if ($this->username === null || $this->password === null)
{
kernel::log(LOG_ERR, 'Username or password missing.');
return false;
}
$connection = @new mysqli($this->host, $this->username, $this->password, $this->database, $this->port);
if ($connection->connect_errno)
{
kernel::log(LOG_ERR, 'Mysql connection failed to host ' . $this->host . ', port: ' . $this->port . ', username ' . $this->username . ' (' . $connection->connect_error . ').');
return false;
}
$connection->set_charset('utf8');
$this->connection = $connection;
return true;
}
public function dbUse($database)
{
$err = $this->connection->select_db($database);
if ($err === true)
{
$this->database = $database;
return true;
}
kernel::log(LOG_ERR, 'Database not found ' . $database . '.');
return false;
}
public function dbGetAll(&$results)
{
$query = 'SHOW DATABASES';
$result = $this->connection->query($query);
if (!$result)
{
kernel::log(LOG_ERR, 'Query failed, unable to fetch databases.');
return false;
}
$results = array();
$skip = array('mysql', 'information_schema', 'performance_schema');
while (($data = $result->fetch_array(MYSQLI_NUM)))
{
if (in_array($data[0], $skip))
{
continue;
}
$results[] = $data[0];
}
$result->close();
return true;
}
public function dbCreate($database)
{
$query = 'CREATE DATABASE ' . $database;
$result = $this->connection->query($query);
if (!$result)
{
kernel::log(LOG_ERR, 'Query failed, unable to create new database ' . $database . '.');
return false;
}
return true;
}
public function dbExport(&$filename)
{
if ($this->database === null)
{
throw new Exception('Cannot export, database not set.');
}
/* create filename if not given */
if (!$filename)
{
$tmp = $this->kernel->expand('{path:tmp}');
$filename = tempnam($tmp, 'mysql-manager-dump');
}
if (!$filename)
{
kernel::log(LOG_ERR, 'Export failed, cannot create temporary file.');
return false;
}
$cmd = 'mysqldump -h ' . $this->host . ' -P ' . $this->port;
$cmd .= ' -u ' . escapeshellarg($this->username);
$cmd .= ' -p' . escapeshellarg($this->password);
$cmd .= ' ' . escapeshellarg($this->database);
$cmd .= ' > ' . $filename;
$ret = exec($cmd, $none, $err);
if ($ret === false || $err !== 0)
{
kernel::log(LOG_ERR, 'Export failed from database ' . $this->database . '.');
return false;
}
return true;
}
public function dbImport($filename)
{
if ($this->database === null)
{
throw new Exception('Cannot import, database not set.');
}
if (!file_exists($filename))
{
kernel::log(LOG_ERR, 'Import failed to database ' . $this->database . ', dump file ' . $filename . ' does not exist.');
return false;
}
$cmd = 'mysql -h ' . $this->host . ' -P ' . $this->port;
$cmd .= ' -u ' . escapeshellarg($this->username);
$cmd .= ' -p' . escapeshellarg($this->password);
$cmd .= ' -D ' . escapeshellarg($this->database);
$cmd .= ' < ' . $filename;
$ret = exec($cmd, $none, $err);
if ($ret === false || $err !== 0)
{
kernel::log(LOG_ERR, 'Import failed to database ' . $this->database . '.');
return false;
}
return true;
}
public function dbCopy($from, $to)
{
/* try to export data from source database */
$err = $this->dbUse($from);
if ($err !== true)
{
return false;
}
$err = $this->dbExport($filename);
if ($err !== true)
{
return false;
}
/* change to destination database */
$err = $this->dbUse($to);
if ($err !== true)
{
/* try to create destination database */
$err = $this->dbCreate($to);
if ($err !== true)
{
unlink($filename);
return false;
}
$err = $this->dbUse($to);
if ($err !== true)
{
unlink($filename);
return false;
}
}
/* import data that was just exported */
$err = $this->dbImport($filename);
if ($err !== true)
{
unlink($filename);
return false;
}
unlink($filename);
return true;
}
public function dbGrantAll($user, $host = 'localhost')
{
$query = 'GRANT ALL PRIVILEGES ON ' . $this->database . '.* TO ' . $user . '@' . $host;
$result = $this->connection->query($query);
if (!$result)
{
kernel::log(LOG_ERR, 'Query failed, unable to grant privileges to database ' . $this->database . ' (' . $query . ').');
return false;
}
return true;
}
public function query($query, $return_data = false)
{
if (!$this->connection->multi_query($query))
{
kernel::log(LOG_ERR, 'Query failed, database: ' . $this->database . ', query: "' . $query . '", error: ' . $this->connection->error);
return false;
}
$data = $return_data ? array() : true;
do
{
$result = $this->connection->store_result();
if ($result)
{
while (is_array($data) && ($row = $result->fetch_assoc()))
{
$data[] = $row;
}
$result->close();
}
} while ($this->connection->next_result());
return $data;
}
}