|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* You may not change or alter any portion of this comment or credits |
|
4
|
|
|
* of supporting developers from this source code or any supporting source code |
|
5
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
6
|
|
|
* |
|
7
|
|
|
* This program is distributed in the hope that it will be useful, |
|
8
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
|
14
|
|
|
* @license {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later} |
|
15
|
|
|
* @package efqdirectory |
|
16
|
|
|
* @since |
|
17
|
|
|
* @author Martijn Hertog (aka wtravel) |
|
18
|
|
|
* @author XOOPS Development Team, |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
require_once XOOPS_ROOT_PATH . '/class/logger.php'; |
|
|
|
|
|
|
22
|
|
|
require_once XOOPS_ROOT_PATH . '/class/database/databasefactory.php'; |
|
23
|
|
|
require_once XOOPS_ROOT_PATH . '/class/database/' . XOOPS_DB_TYPE . 'database.php'; |
|
|
|
|
|
|
24
|
|
|
require_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* database manager for XOOPS installer |
|
28
|
|
|
* |
|
29
|
|
|
* @author Haruki Setoyama <[email protected]> |
|
30
|
|
|
* @version $Id: dbmanager.php 19391 2010-06-13 06:53:15Z underdogg $ |
|
31
|
|
|
* @access public |
|
32
|
|
|
**/ |
|
33
|
|
|
class db_manager |
|
34
|
|
|
{ |
|
35
|
|
|
public $s_tables = array(); |
|
36
|
|
|
public $f_tables = array(); |
|
37
|
|
|
public $db; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* db_manager constructor. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->db = XoopsDatabaseFactory::getDatabase(); |
|
|
|
|
|
|
45
|
|
|
$this->db->setPrefix(XOOPS_DB_PREFIX); |
|
|
|
|
|
|
46
|
|
|
$this->db->setLogger(XoopsLogger::instance()); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
public function isConnectable() |
|
53
|
|
|
{ |
|
54
|
|
|
return ($this->db->connect(false) !== false) ? true : false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function dbExists() |
|
61
|
|
|
{ |
|
62
|
|
|
return ($this->db->connect() !== false) ? true : false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function createDB() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->db->connect(false); |
|
71
|
|
|
|
|
72
|
|
|
$result = $this->db->query('CREATE DATABASE ' . XOOPS_DB_NAME); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
return ($result !== false) ? true : false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $sql_file_path |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function queryFromFile($sql_file_path) |
|
82
|
|
|
{ |
|
83
|
|
|
global $pieces; |
|
84
|
|
|
$tables = array(); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if (!file_exists($sql_file_path)) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
$sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path))); |
|
90
|
|
|
SqlUtility::splitMySqlFile($pieces, $sql_query); |
|
|
|
|
|
|
91
|
|
|
$this->db->connect(); |
|
92
|
|
|
foreach ($pieces as $piece) { |
|
93
|
|
|
$piece = trim($piece); |
|
94
|
|
|
// [0] contains the prefixed query |
|
95
|
|
|
// [4] contains unprefixed table name |
|
96
|
|
|
$prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix()); |
|
97
|
|
|
if ($prefixed_query !== false) { |
|
98
|
|
|
$table = $this->db->prefix($prefixed_query[4]); |
|
99
|
|
|
if ($prefixed_query[1] === 'CREATE TABLE') { |
|
100
|
|
View Code Duplication |
if ($this->db->query($prefixed_query[0]) !== false) { |
|
|
|
|
|
|
101
|
|
|
if (!isset($this->s_tables['create'][$table])) { |
|
102
|
|
|
$this->s_tables['create'][$table] = 1; |
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
|
|
if (!isset($this->f_tables['create'][$table])) { |
|
106
|
|
|
$this->f_tables['create'][$table] = 1; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} elseif ($prefixed_query[1] === 'INSERT INTO') { |
|
110
|
|
|
if ($this->db->query($prefixed_query[0]) !== false) { |
|
111
|
|
View Code Duplication |
if (!isset($this->s_tables['insert'][$table])) { |
|
|
|
|
|
|
112
|
|
|
$this->s_tables['insert'][$table] = 1; |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->s_tables['insert'][$table]++; |
|
115
|
|
|
} |
|
116
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
117
|
|
|
if (!isset($this->f_tables['insert'][$table])) { |
|
118
|
|
|
$this->f_tables['insert'][$table] = 1; |
|
119
|
|
|
} else { |
|
120
|
|
|
$this->f_tables['insert'][$table]++; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
View Code Duplication |
} elseif ($prefixed_query[1] === 'ALTER TABLE') { |
|
|
|
|
|
|
124
|
|
|
if ($this->db->query($prefixed_query[0]) !== false) { |
|
125
|
|
|
if (!isset($this->s_tables['alter'][$table])) { |
|
126
|
|
|
$this->s_tables['alter'][$table] = 1; |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
|
|
if (!isset($this->s_tables['alter'][$table])) { |
|
130
|
|
|
$this->f_tables['alter'][$table] = 1; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} elseif ($prefixed_query[1] === 'DROP TABLE') { |
|
134
|
|
|
if ($this->db->query('DROP TABLE ' . $table) !== false) { |
|
135
|
|
|
if (!isset($this->s_tables['drop'][$table])) { |
|
136
|
|
|
$this->s_tables['drop'][$table] = 1; |
|
137
|
|
|
} |
|
138
|
|
|
} else { |
|
139
|
|
|
if (!isset($this->s_tables['drop'][$table])) { |
|
140
|
|
|
$this->f_tables['drop'][$table] = 1; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public function report() |
|
154
|
|
|
{ |
|
155
|
|
|
$content = "<table align='center'><tr><td align='left'>\n"; |
|
156
|
|
View Code Duplication |
if (isset($this->s_tables['create'])) { |
|
|
|
|
|
|
157
|
|
|
foreach ($this->s_tables['create'] as $key => $val) { |
|
158
|
|
|
$content .= sprintf(_INSTALL_L45, "<b>$key</b>") . "<br>\n"; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
View Code Duplication |
if (isset($this->s_tables['insert'])) { |
|
|
|
|
|
|
162
|
|
|
foreach ($this->s_tables['insert'] as $key => $val) { |
|
163
|
|
|
$content .= sprintf(_INSTALL_L119, $val, "<b>$key</b>") . "<br>\n"; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
View Code Duplication |
if (isset($this->s_tables['alter'])) { |
|
|
|
|
|
|
167
|
|
|
foreach ($this->s_tables['alter'] as $key => $val) { |
|
168
|
|
|
$content .= sprintf(_INSTALL_L133, "<b>$key</b>") . "<br>\n"; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
View Code Duplication |
if (isset($this->s_tables['drop'])) { |
|
|
|
|
|
|
172
|
|
|
foreach ($this->s_tables['drop'] as $key => $val) { |
|
173
|
|
|
$content .= sprintf(_INSTALL_L163, "<b>$key</b>") . "<br>\n"; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
$content .= "<br>\n"; |
|
177
|
|
View Code Duplication |
if (isset($this->f_tables['create'])) { |
|
|
|
|
|
|
178
|
|
|
foreach ($this->f_tables['create'] as $key => $val) { |
|
179
|
|
|
$content .= sprintf(_INSTALL_L118, "<b>$key</b>") . "<br>\n"; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
View Code Duplication |
if (isset($this->f_tables['insert'])) { |
|
|
|
|
|
|
183
|
|
|
foreach ($this->f_tables['insert'] as $key => $val) { |
|
184
|
|
|
$content .= sprintf(_INSTALL_L120, $val, "<b>$key</b>") . "<br>\n"; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
View Code Duplication |
if (isset($this->f_tables['alter'])) { |
|
|
|
|
|
|
188
|
|
|
foreach ($this->f_tables['alter'] as $key => $val) { |
|
189
|
|
|
$content .= sprintf(_INSTALL_L134, "<b>$key</b>") . "<br>\n"; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
View Code Duplication |
if (isset($this->f_tables['drop'])) { |
|
|
|
|
|
|
193
|
|
|
foreach ($this->f_tables['drop'] as $key => $val) { |
|
194
|
|
|
$content .= sprintf(_INSTALL_L164, "<b>$key</b>") . "<br>\n"; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
$content .= "</td></tr></table>\n"; |
|
198
|
|
|
|
|
199
|
|
|
return $content; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param $sql |
|
204
|
|
|
* @return mixed |
|
205
|
|
|
*/ |
|
206
|
|
|
public function query($sql) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->db->connect(); |
|
209
|
|
|
|
|
210
|
|
|
return $this->db->query($sql); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param $table |
|
215
|
|
|
* @return mixed |
|
216
|
|
|
*/ |
|
217
|
|
|
public function prefix($table) |
|
218
|
|
|
{ |
|
219
|
|
|
$this->db->connect(); |
|
220
|
|
|
|
|
221
|
|
|
return $this->db->prefix($table); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param $ret |
|
226
|
|
|
* @return mixed |
|
227
|
|
|
*/ |
|
228
|
|
|
public function fetchArray($ret) |
|
229
|
|
|
{ |
|
230
|
|
|
$this->db->connect(); |
|
231
|
|
|
|
|
232
|
|
|
return $this->db->fetchArray($ret); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param $table |
|
237
|
|
|
* @param $query |
|
238
|
|
|
* @return bool |
|
239
|
|
|
*/ |
|
240
|
|
|
public function insert($table, $query) |
|
241
|
|
|
{ |
|
242
|
|
|
$this->db->connect(); |
|
243
|
|
|
$table = $this->db->prefix($table); |
|
244
|
|
|
$query = 'INSERT INTO ' . $table . ' ' . $query; |
|
245
|
|
|
if (!$this->db->queryF($query)) { |
|
246
|
|
View Code Duplication |
if (!isset($this->f_tables['insert'][$table])) { |
|
|
|
|
|
|
247
|
|
|
$this->f_tables['insert'][$table] = 1; |
|
248
|
|
|
} else { |
|
249
|
|
|
$this->f_tables['insert'][$table]++; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return false; |
|
253
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
254
|
|
|
if (!isset($this->s_tables['insert'][$table])) { |
|
255
|
|
|
$this->s_tables['insert'][$table] = 1; |
|
256
|
|
|
} else { |
|
257
|
|
|
$this->s_tables['insert'][$table]++; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
return $this->db->getInsertId(); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return bool |
|
266
|
|
|
*/ |
|
267
|
|
|
public function isError() |
|
268
|
|
|
{ |
|
269
|
|
|
return isset($this->f_tables) ? true : false; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param $tables |
|
274
|
|
|
* @return array |
|
275
|
|
|
*/ |
|
276
|
|
|
public function deleteTables($tables) |
|
277
|
|
|
{ |
|
278
|
|
|
global $ct; |
|
279
|
|
|
$deleted = array(); |
|
280
|
|
|
$this->db->connect(); |
|
281
|
|
|
foreach ($tables as $key => $val) { |
|
282
|
|
|
if (!$this->db->query('DROP TABLE ' . $this->db->prefix($key))) { |
|
283
|
|
|
$deleted[] = $ct; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return $deleted; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param $table |
|
292
|
|
|
* @return bool |
|
293
|
|
|
*/ |
|
294
|
|
|
public function tableExists($table) |
|
295
|
|
|
{ |
|
296
|
|
|
$table = trim($table); |
|
297
|
|
|
$ret = false; |
|
298
|
|
|
if ($table !== '') { |
|
299
|
|
|
$this->db->connect(); |
|
300
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix($table); |
|
301
|
|
|
$ret = (false != $this->db->query($sql)) ? true : false; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
return $ret; |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|