-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnephthys_db.php
658 lines (537 loc) · 16.6 KB
/
nephthys_db.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
<?php
/***************************************************************************
*
* Nephthys - file sharing management
* Copyright (c) by Andreas Unterkircher, [email protected]
*
* This file is part of Nephthys.
*
* Nephthys is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Nephthys is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nephthys. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
class NEPHTHYS_DB {
private $db;
private $cfg;
private $is_connected;
private $last_error;
private $parent;
/**
* NEPHTHYS_DB class constructor
*
* This constructor initially connect to the database.
*/
public function __construct()
{
global $nephthys;
$this->parent =& $nephthys;
$this->cfg = $this->parent->cfg;
/* We are starting disconnected */
$this->setConnStatus(false);
/* Connect to MySQL Database */
$this->db_connect();
} // __construct()
/**
* NEPHTHYS_DB class deconstructor
*
* This destructor will close the current database connection.
*/
public function __destruct()
{
$this->db_disconnect();
} // _destruct()
/**
* NEPHTHYS_DB database connect
*
* This function will connect to the database via MDB2
*/
private function db_connect()
{
$options = array(
'debug' => 2,
'portability' => 'DB_PORTABILITY_ALL'
);
switch($this->cfg->db_type) {
default:
case 'mysql':
$dsn = "mysqli://"
. $this->cfg->mysql_user .":"
. $this->cfg->mysql_pass ."@"
. $this->cfg->mysql_host ."/"
. $this->cfg->mysql_db;
break;
case 'sqlite':
$dsn = "sqlite:///"
. $this->cfg->sqlite_path;
break;
}
$this->db = MDB2::connect($dsn, $options);
if(PEAR::isError($this->db)) {
$this->throwError("Unable to connect to database: ". $this->db->getMessage() .' - '. $this->db->getUserInfo());
$this->setConnStatus(false);
}
$this->setConnStatus(true);
} // db_connect()
/**
* NEPHTHYS_DB database disconnect
*
* This function will disconnected an established database connection.
*/
private function db_disconnect()
{
$this->db->disconnect();
} // db_disconnect()
/**
* NEPHTHYS_DB database query
*
* This function will execute a SQL query and return the result as
* object.
*/
public function db_query($query = "", $mode = MDB2_FETCHMODE_OBJECT)
{
if($this->getConnStatus()) {
$this->db->setFetchMode($mode);
/* for manipulating queries use exec instead of query. can save
* some resource because nothing has to be allocated for results.
*/
if(preg_match('/^(update|insert)i/', $query)) {
$result = $this->db->exec($query);
}
else {
$result = $this->db->query($query);
}
if(PEAR::isError($result))
$this->throwError($result->getMessage() .' - '. $result->getUserInfo());
return $result;
}
else
$this->throwError("Can't execute query - we are not connected!");
} // db_query()
/**
* NEPHTHYS_DB database prepare query
*
* This function will prepare a SQL query to be executed
* @param string $query
* @param int $mode
* @return mixed
*/
public function db_prepare($query = "")
{
if($this->getConnStatus()) {
$this->db->prepare($query);
/* for manipulating queries use exec instead of query. can save
* some resource because nothing has to be allocated for results.
*/
if(preg_match('/^(update|insert|delete)i/', $query)) {
$sth = $this->db->prepare($query, MDB2_PREPARE_MANIP);
}
else {
$sth = $this->db->prepare($query, MDB2_PREPARE_RESULT);
}
if(PEAR::isError($sth))
$this->throwError($sth->getMessage() .' - '. $sth->getUserInfo());
return $sth;
}
else
$this->throwError("Can't prepare query - we are not connected!");
} // db_prepare()
/**
* NEPHTHYS_DB database execute a prepared query
*
* This function will execute a previously prepared SQL query
* @param mixed $sth
* @param mixed $data
*/
public function db_execute($sth, $data)
{
if($this->getConnStatus()) {
$result = $sth->execute($data);
if(PEAR::isError($result))
$this->throwError($result->getMessage() .' - '. $result->getUserInfo());
}
else
$this->throwError("Can't prepare query - we are not connected!");
} // db_execute()
/**
* NEPHTHYS_DB database query & execute
*
* This function will execute a SQL query and return nothing.
*/
public function db_exec($query = "")
{
if(!$this->getConnStatus())
return false;
$affected =& $this->db->exec($query);
if(PEAR::isError($affected)) {
$this->throwError($affected->getMessage());
return false;
}
return true;
} // db_exec()
/**
* NEPHTHYS_DB fetch ONE row
*
* This function will execute the given but only return the
* first result.
*/
public function db_fetchSingleRow($query = "", $mode = MDB2_FETCHMODE_OBJECT)
{
if($this->getConnStatus()) {
$row = $this->db->queryRow($query, array(), $mode);
if(PEAR::isError($row))
$this->throwError($row->getMessage() .' - '. $row->getUserInfo());
return $row;
}
else {
$this->throwError("Can't fetch row - we are not connected!");
}
} // db_fetchSingleRow()
/**
* NEPHTHYS_DB number of affected rows
*
* This functions returns the number of affected rows but the
* given SQL query.
*/
public function db_getNumRows($query = "")
{
/* Execute query */
$result = $this->db_query($query);
/* Errors? */
if(PEAR::isError($result))
$this->throwError($result->getMessage() .' - '. $result->getUserInfo());
return $result->numRows();
} // db_getNumRows()
/**
* NEPHTHYS_DB get primary key
*
* This function returns the primary key of the last
* operated insert SQL query.
*/
public function db_getid($table_name = null)
{
/* Get the last primary key ID from execute query */
$id = $this->db->lastInsertID($table_name);
if (PEAR::isError($id)) {
$this->throwError($id->getMessage());
}
return $id;
} // db_getid()
/**
* NEPHTHYS_DB check table exists
*
* This function checks if the given table exists in the
* database
* @param string, table name
* @return true if table found otherwise false
*/
public function db_check_table_exists($table_name = "")
{
if(!$this->getConnStatus())
return false;
switch($this->cfg->db_type) {
default:
case 'mysql':
$result = $this->db_query("SHOW TABLES");
$tables_in = "Tables_in_". $this->cfg->mysql_db;
while($row = $result->fetchRow()) {
if($row->$tables_in == $table_name)
return true;
}
break;
case 'sqlite':
$result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
while($row = $result->fetchRow()) {
if($row->name == $table_name)
return true;
}
break;
}
return false;
} // db_check_table_exists()
/**
* NEPHTHYS_DB rename table
*
* This function will rename an database table
* @param old_name, new_name
*/
public function db_rename_table($old, $new)
{
if($this->db_check_table_exists($old)) {
if(!$this->db_check_table_exists($new))
$this->db_query("RENAME TABLE ". $old ." TO ". $new);
else
$this->throwError("Can't rename table ". $old ." - ". $new ." already exists!");
}
} // db_rename_table()
/**
* NEPHTHYS_DB drop table
*
* This function will delete the given table from database
*/
public function db_drop_table($table_name)
{
if($this->db_check_table_exists($table_name))
$this->db_query("DROP TABLE ". $table_name);
} // db_drop_table()
/**
* NEPHTHYS_DB truncate table
*
* This function will truncate (reset) the given table
*/
public function db_truncate_table($table_name)
{
if($this->db_check_table_exists($table_name))
$this->db_query("TRUNCATE TABLE ". $table_name);
} // db_truncate_table()
/**
* NEPHTHYS_DB check column exist
*
* This function checks if the given column exists within
* the specified table.
*/
public function db_check_column_exists($table_name, $column)
{
if(!$this->getConnStatus())
return false;
switch($this->cfg->db_type) {
default:
case 'mysql':
$result = $this->db_query("DESC ". $table_name, MDB2_FETCHMODE_ORDERED);
while($row = $result->fetchRow()) {
if(in_array($column, $row))
return true;
}
break;
case 'sqlite':
$result = $this->db_query("
SELECT sql
FROM
(SELECT * FROM sqlite_master UNION ALL
SELECT * FROM sqlite_temp_master)
WHERE
tbl_name LIKE '". $table_name ."'
AND type!='meta'
AND sql NOT NULL
AND name NOT LIKE 'sqlite_%'
ORDER BY substr(type,2,1), name
");
while($row = $result->fetchRow()) {
/* CREATE TABLE xx ( col1 int, col2 bool, col3 ...) */
if(strstr($row->sql, " ". $column ." ") !== false)
return true;
}
break;
}
return false;
} // db_check_column_exists()
/**
* NEPHTHYS_DB check index exists
*
* This function checks if the given index can be found
* within the specified table.
*/
public function db_check_index_exists($table_name, $index_name)
{
$result = $this->db_query("DESC ". $table_name, MDB2_FETCHMODE_ORDERED);
while($row = $result->fetchRow()) {
if(in_array("KEY `". $index_name ."`", $row))
return 1;
}
return 0;
} // db_check_index_exists()
/**
* NEPHTHYS_DB alter table
*
* This function offers multiple methods to alter a table.
* * add/modify/delete columns
* * drop index
*/
public function db_alter_table($table_name, $option, $column, $param1 = "", $param2 = "")
{
if(!$this->db_check_table_exists($table_name)) {
$this->throwError("Table ". $table_name ." does not exist!");
return false;
}
switch($this->cfg->db_type) {
default:
case 'mysql':
switch(strtolower($option)) {
case 'add':
if(!$this->db_check_column_exists($table_name, $column))
$this->db_query("ALTER TABLE ". $table_name ." ADD ". $column ." ". $param1);
break;
case 'change':
if($this->db_check_column_exists($table_name, $column))
$this->db_query("ALTER TABLE ". $table_name ." CHANGE ". $column ." ". $param1);
break;
case 'drop':
if($this->db_check_column_exists($table_name, $column))
$this->db_query("ALTER TABLE ". $table_name ." DROP ". $column);
break;
case 'dropidx':
if($this->db_check_index_exists($table_name, $column))
$this->db_query("ALTER TABLE ". $table_name ." DROP INDEX ". $column);
break;
}
break;
case 'sqlite':
$this->throwError("SQLite only support ALTER TABLE rudimentary with version 3, so no support here right now.");
break;
}
} // db_alter_table()
/**
* NEPHTHYS_DB get database version
*
* This functions returns the current database version
*/
public function getVersion()
{
if($this->db_check_table_exists(MYSQL_PREFIX ."settings")) {
$result = $this->db_fetchSingleRow("
SELECT setting_value
FROM ". MYSQL_PREFIX ."settings
WHERE setting_key LIKE 'version'
");
return $result->setting_value;
}
else
return 0;
} // getVersion()
/**
* NEPHTHYS_DB set version
*
* This function sets the version of database
*/
public function setVersion($version)
{
$this->db_query("
REPLACE INTO ". MYSQL_PREFIX ."settings
(setting_key, setting_value)
VALUES ('version', '". $version ."')
");
} // setVersion()
/**
* NEPHTHYS_DB get connection status
*
* This function checks the internal state variable if already
* connected to database.
*/
private function setConnStatus($status)
{
$this->is_connected = $status;
} // setConnStatus()
/**
* NEPHTHYS_DB set connection status
* This function sets the internal state variable to indicate
* current database connection status.
*/
private function getConnStatus()
{
return $this->is_connected;
} // getConnStatus()
/**
* NEPHTHYS_DB throw error
*
* This function shows up error messages and afterwards through exceptions.
*/
private function throwError($string)
{
if(!defined('DB_NOERROR')) {
$this->parent->_error($string);
try {
throw new NEPHTHYS_EXCEPTION;
}
catch(NEPHTHYS_EXCEPTION $e) {
print "<br /><br />\n";
$this->parent->_error($e);
die;
}
}
$this->last_error = $string;
} // throwError()
/**
* quote string
*
* this function handsover the provided string to the MDB2
* quote() function which will return the, for the selected
* database system, correctly quoted string.
*
* @param string $query
* @return string
*/
public function db_quote($obj)
{
return $this->db->quote($obj);
} // db_quote()
/**
* start transaction
*
* this will start a transaction on ACID-supporting database
* systems.
*
* @return bool
*/
public function db_start_transaction()
{
if(!$this->getConnStatus())
return false;
if(!$this->db->supports('transactions'))
return false;
$result = $this->db->beginTransaction();
/* Errors? */
if(PEAR::isError($result))
$this->throwError($result->getMessage() .' - '. $result->getUserInfo());
return true;
} // db_start_transaction()
/**
* commit transaction
*
* this will commit an ongoing transaction on ACID-supporting
* database systems
*
* @return bool
*/
public function db_commit_transaction()
{
if(!$this->getConnStatus())
return false;
if(!$this->db->inTransaction())
return false;
$result = $this->db->commit();
/* Errors? */
if(PEAR::isError($result))
$this->throwError($result->getMessage() .' - '. $result->getUserInfo());
return true;
} // db_commit_transaction()
/**
* rollback transaction()
*
* this function aborts a on going transaction
*
* @return bool
*/
public function db_rollback_transaction()
{
if(!$this->getConnStatus())
return false;
if(!$this->db->inTransaction())
return false;
$result = $this->db->rollback();
/* Errors? */
if(PEAR::isError($result))
$this->throwError($result->getMessage() .' - '. $result->getUserInfo());
return true;
} // db_rollback_transaction()
} // NEPHTHYS_DB()
// vim: set filetype=php expandtab softtabstop=3 tabstop=3 shiftwidth=3 autoindent smartindent:
?>