|
|
@@ -115,6 +115,7 @@ if (!extension_loaded('mysqli')) {
|
|
115
|
115
|
error('Mysqli Extension not loaded.');
|
|
116
|
116
|
}
|
|
117
|
117
|
|
|
|
118
|
+
|
|
118
|
119
|
/**
|
|
119
|
120
|
* db_string
|
|
120
|
121
|
* Handles escaping
|
|
|
@@ -143,6 +144,7 @@ function db_string($String, $DisableWildcards = false)
|
|
143
|
144
|
return $String;
|
|
144
|
145
|
}
|
|
145
|
146
|
|
|
|
147
|
+
|
|
146
|
148
|
/**
|
|
147
|
149
|
* db_array
|
|
148
|
150
|
*/
|
|
|
@@ -160,6 +162,7 @@ function db_array($Array, $DontEscape = [], $Quote = false)
|
|
160
|
162
|
return $Array;
|
|
161
|
163
|
}
|
|
162
|
164
|
|
|
|
165
|
+
|
|
163
|
166
|
// todo: Revisit access levels once Drone is replaced by ZeRobot
|
|
164
|
167
|
class DB_MYSQL
|
|
165
|
168
|
{
|
|
|
@@ -182,20 +185,40 @@ class DB_MYSQL
|
|
182
|
185
|
protected $Port = 0;
|
|
183
|
186
|
protected $Socket = '';
|
|
184
|
187
|
|
|
|
188
|
+ protected $Key = '';
|
|
|
189
|
+ protected $Cert = '';
|
|
|
190
|
+ protected $CA = '';
|
|
|
191
|
+
|
|
|
192
|
+
|
|
185
|
193
|
/**
|
|
186
|
194
|
* __construct
|
|
187
|
195
|
*/
|
|
188
|
|
- public function __construct($Database = null, $User = null, $Pass = null, $Server = null, $Port = null, $Socket = null)
|
|
189
|
|
- {
|
|
|
196
|
+ public function __construct(
|
|
|
197
|
+ $Database = null,
|
|
|
198
|
+ $User = null,
|
|
|
199
|
+ $Pass = null,
|
|
|
200
|
+ $Server = null,
|
|
|
201
|
+ $Port = null,
|
|
|
202
|
+ $Socket = null,
|
|
|
203
|
+ $Key = null,
|
|
|
204
|
+ $Cert = null,
|
|
|
205
|
+ $CA = null
|
|
|
206
|
+ ) {
|
|
190
|
207
|
$ENV = ENV::go();
|
|
|
208
|
+
|
|
191
|
209
|
$this->Database = $ENV->getPriv('SQLDB');
|
|
192
|
210
|
$this->User = $ENV->getPriv('SQLLOGIN');
|
|
193
|
211
|
$this->Pass = $ENV->getPriv('SQLPASS');
|
|
194
|
212
|
$this->Server = $ENV->getPriv('SQLHOST');
|
|
195
|
213
|
$this->Port = $ENV->getPriv('SQLPORT');
|
|
196
|
214
|
$this->Socket = $ENV->getPriv('SQLSOCK');
|
|
|
215
|
+
|
|
|
216
|
+ $this->Key = $ENV->getPriv('SQL_KEY');
|
|
|
217
|
+ $this->Cert = $ENV->getPriv('SQL_CERT');
|
|
|
218
|
+ $this->CA = $ENV->getPriv('SQL_CA');
|
|
197
|
219
|
}
|
|
198
|
220
|
|
|
|
221
|
+
|
|
199
|
222
|
/**
|
|
200
|
223
|
* halt
|
|
201
|
224
|
*/
|
|
|
@@ -220,13 +243,35 @@ class DB_MYSQL
|
|
220
|
243
|
}
|
|
221
|
244
|
}
|
|
222
|
245
|
|
|
|
246
|
+
|
|
223
|
247
|
/**
|
|
224
|
248
|
* connect
|
|
225
|
249
|
*/
|
|
226
|
250
|
public function connect()
|
|
227
|
251
|
{
|
|
228
|
252
|
if (!$this->LinkID) {
|
|
229
|
|
- $this->LinkID = mysqli_connect($this->Server, $this->User, $this->Pass, $this->Database, $this->Port, $this->Socket); // defined in config.php
|
|
|
253
|
+ $this->LinkID = mysqli_init();
|
|
|
254
|
+
|
|
|
255
|
+ mysqli_ssl_set(
|
|
|
256
|
+ $this->LinkID,
|
|
|
257
|
+ $this->Key,
|
|
|
258
|
+ $this->Cert,
|
|
|
259
|
+ $this->Ca,
|
|
|
260
|
+ null,
|
|
|
261
|
+ null
|
|
|
262
|
+ );
|
|
|
263
|
+
|
|
|
264
|
+ mysqli_real_connect(
|
|
|
265
|
+ $this->LinkID,
|
|
|
266
|
+ $this->Server,
|
|
|
267
|
+ $this->User,
|
|
|
268
|
+ $this->Pass,
|
|
|
269
|
+ $this->Database,
|
|
|
270
|
+ $this->Port,
|
|
|
271
|
+ $this->Socket,
|
|
|
272
|
+ MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT
|
|
|
273
|
+ );
|
|
|
274
|
+
|
|
230
|
275
|
if (!$this->LinkID) {
|
|
231
|
276
|
$this->Errno = mysqli_connect_errno();
|
|
232
|
277
|
$this->Error = mysqli_connect_error();
|
|
|
@@ -235,7 +280,8 @@ class DB_MYSQL
|
|
235
|
280
|
}
|
|
236
|
281
|
mysqli_set_charset($this->LinkID, "utf8mb4");
|
|
237
|
282
|
}
|
|
238
|
|
-
|
|
|
283
|
+
|
|
|
284
|
+
|
|
239
|
285
|
/**
|
|
240
|
286
|
* prepare_query
|
|
241
|
287
|
*/
|
|
|
@@ -258,6 +304,7 @@ class DB_MYSQL
|
|
258
|
304
|
return $this->StatementID;
|
|
259
|
305
|
}
|
|
260
|
306
|
|
|
|
307
|
+
|
|
261
|
308
|
/**
|
|
262
|
309
|
* exec_prepared_query
|
|
263
|
310
|
*/
|
|
|
@@ -271,6 +318,7 @@ class DB_MYSQL
|
|
271
|
318
|
$this->Time += $QueryRunTime;
|
|
272
|
319
|
}
|
|
273
|
320
|
|
|
|
321
|
+
|
|
274
|
322
|
/**
|
|
275
|
323
|
* Runs a raw query assuming pre-sanitized input. However, attempting to self sanitize (such
|
|
276
|
324
|
* as via db_string) is still not as safe for using prepared statements so for queries
|
|
|
@@ -360,6 +408,7 @@ class DB_MYSQL
|
|
360
|
408
|
return $this->QueryID;
|
|
361
|
409
|
}
|
|
362
|
410
|
|
|
|
411
|
+
|
|
363
|
412
|
/**
|
|
364
|
413
|
* inserted_id
|
|
365
|
414
|
*/
|
|
|
@@ -370,6 +419,7 @@ class DB_MYSQL
|
|
370
|
419
|
}
|
|
371
|
420
|
}
|
|
372
|
421
|
|
|
|
422
|
+
|
|
373
|
423
|
/**
|
|
374
|
424
|
* next_record
|
|
375
|
425
|
*/
|
|
|
@@ -388,6 +438,7 @@ class DB_MYSQL
|
|
388
|
438
|
}
|
|
389
|
439
|
}
|
|
390
|
440
|
|
|
|
441
|
+
|
|
391
|
442
|
/**
|
|
392
|
443
|
* close
|
|
393
|
444
|
*/
|
|
|
@@ -401,6 +452,7 @@ class DB_MYSQL
|
|
401
|
452
|
}
|
|
402
|
453
|
}
|
|
403
|
454
|
|
|
|
455
|
+
|
|
404
|
456
|
/*
|
|
405
|
457
|
* Returns an integer with the number of rows found
|
|
406
|
458
|
* Returns a string if the number of rows found exceeds MAXINT
|
|
|
@@ -412,6 +464,7 @@ class DB_MYSQL
|
|
412
|
464
|
}
|
|
413
|
465
|
}
|
|
414
|
466
|
|
|
|
467
|
+
|
|
415
|
468
|
/*
|
|
416
|
469
|
* Returns true if the query exists and there were records found
|
|
417
|
470
|
* Returns false if the query does not exist or if there were 0 records returned
|
|
|
@@ -421,6 +474,7 @@ class DB_MYSQL
|
|
421
|
474
|
return ($this->QueryID && $this->record_count() !== 0);
|
|
422
|
475
|
}
|
|
423
|
476
|
|
|
|
477
|
+
|
|
424
|
478
|
/**
|
|
425
|
479
|
* affected_rows
|
|
426
|
480
|
*/
|
|
|
@@ -431,6 +485,7 @@ class DB_MYSQL
|
|
431
|
485
|
}
|
|
432
|
486
|
}
|
|
433
|
487
|
|
|
|
488
|
+
|
|
434
|
489
|
/**
|
|
435
|
490
|
* info
|
|
436
|
491
|
*/
|
|
|
@@ -439,6 +494,7 @@ class DB_MYSQL
|
|
439
|
494
|
return mysqli_get_host_info($this->LinkID);
|
|
440
|
495
|
}
|
|
441
|
496
|
|
|
|
497
|
+
|
|
442
|
498
|
// Creates an array from a result set
|
|
443
|
499
|
// If $Key is set, use the $Key column in the result set as the array key
|
|
444
|
500
|
// Otherwise, use an integer
|
|
|
@@ -461,6 +517,7 @@ class DB_MYSQL
|
|
461
|
517
|
return $Return;
|
|
462
|
518
|
}
|
|
463
|
519
|
|
|
|
520
|
+
|
|
464
|
521
|
// Loops through the result set, collecting the $ValField column into an array with $KeyField as keys
|
|
465
|
522
|
public function to_pair($KeyField, $ValField, $Escape = true)
|
|
466
|
523
|
{
|
|
|
@@ -480,6 +537,7 @@ class DB_MYSQL
|
|
480
|
537
|
return $Return;
|
|
481
|
538
|
}
|
|
482
|
539
|
|
|
|
540
|
+
|
|
483
|
541
|
// Loops through the result set, collecting the $Key column into an array
|
|
484
|
542
|
public function collect($Key, $Escape = true)
|
|
485
|
543
|
{
|
|
|
@@ -497,6 +555,7 @@ class DB_MYSQL
|
|
497
|
555
|
* Useful extras from OPS
|
|
498
|
556
|
*/
|
|
499
|
557
|
|
|
|
558
|
+
|
|
500
|
559
|
/**
|
|
501
|
560
|
* Runs a prepared_query using placeholders and returns the matched row.
|
|
502
|
561
|
* Stashes the current query id so that this can be used within a block
|
|
|
@@ -515,6 +574,7 @@ class DB_MYSQL
|
|
515
|
574
|
return $result;
|
|
516
|
575
|
}
|
|
517
|
576
|
|
|
|
577
|
+
|
|
518
|
578
|
/**
|
|
519
|
579
|
* Runs a prepared_query using placeholders and returns the first element
|
|
520
|
580
|
* of the first row.
|
|
|
@@ -545,6 +605,7 @@ class DB_MYSQL
|
|
545
|
605
|
$this->Row = 0;
|
|
546
|
606
|
}
|
|
547
|
607
|
|
|
|
608
|
+
|
|
548
|
609
|
/**
|
|
549
|
610
|
* get_query_id
|
|
550
|
611
|
*/
|
|
|
@@ -553,6 +614,7 @@ class DB_MYSQL
|
|
553
|
614
|
return $this->QueryID;
|
|
554
|
615
|
}
|
|
555
|
616
|
|
|
|
617
|
+
|
|
556
|
618
|
/**
|
|
557
|
619
|
* beginning
|
|
558
|
620
|
*/
|
|
|
@@ -562,6 +624,7 @@ class DB_MYSQL
|
|
562
|
624
|
$this->Row = 0;
|
|
563
|
625
|
}
|
|
564
|
626
|
|
|
|
627
|
+
|
|
565
|
628
|
/**
|
|
566
|
629
|
* This function determines whether the last query caused warning messages
|
|
567
|
630
|
* and stores them in $this->Queries
|
|
|
@@ -581,76 +644,4 @@ class DB_MYSQL
|
|
581
|
644
|
}
|
|
582
|
645
|
$this->Queries[count($this->Queries) - 1][2] = $Warnings;
|
|
583
|
646
|
}
|
|
584
|
|
-
|
|
585
|
|
-
|
|
586
|
|
- /**
|
|
587
|
|
- * todo: Work this into Bio Gazelle
|
|
588
|
|
- * @see https://github.com/OPSnet/Gazelle/blob/master/app/DB.php
|
|
589
|
|
- */
|
|
590
|
|
-
|
|
591
|
|
- /**
|
|
592
|
|
- * Soft delete a row from a table <t> by inserting it into deleted_<t> and then delete from <t>
|
|
593
|
|
- * @param string $schema the schema name
|
|
594
|
|
- * @param string $table the table name
|
|
595
|
|
- * @param array $condition Must be an array of arrays, e.g. [[column_name, column_value]] or [[col1, val1], [col2, val2]]
|
|
596
|
|
- * Will be used to identify the row (or rows) to delete
|
|
597
|
|
- * @param boolean $delete whether to delete the matched rows
|
|
598
|
|
- * @return array 2 elements, true/false and message if false
|
|
599
|
|
- * /
|
|
600
|
|
- public function softDelete($schema, $table, array $condition, $delete = true)
|
|
601
|
|
- {
|
|
602
|
|
- $sql = 'SELECT column_name, column_type FROM information_schema.columns WHERE table_schema = ? AND table_name = ? ORDER BY 1';
|
|
603
|
|
- $this->db->prepared_query($sql, $schema, $table);
|
|
604
|
|
- $t1 = $this->db->to_array();
|
|
605
|
|
- $n1 = count($t1);
|
|
606
|
|
-
|
|
607
|
|
- $softDeleteTable = 'deleted_' . $table;
|
|
608
|
|
- $this->db->prepared_query($sql, $schema, $softDeleteTable);
|
|
609
|
|
- $t2 = $this->db->to_array();
|
|
610
|
|
- $n2 = count($t2);
|
|
611
|
|
-
|
|
612
|
|
- if (!$n1) {
|
|
613
|
|
- return [false, "No such table $table"];
|
|
614
|
|
- } elseif (!$n2) {
|
|
615
|
|
- return [false, "No such table $softDeleteTable"];
|
|
616
|
|
- } elseif ($n1 != $n2) {
|
|
617
|
|
- // tables do not have the same number of columns
|
|
618
|
|
- return [false, "$table and $softDeleteTable column count mismatch ($n1 != $n2)"];
|
|
619
|
|
- }
|
|
620
|
|
-
|
|
621
|
|
- $column = [];
|
|
622
|
|
- for ($i = 0; $i < $n1; ++$i) {
|
|
623
|
|
- // a column does not have the same name or datatype
|
|
624
|
|
- if (strtolower($t1[$i][0]) != strtolower($t2[$i][0]) || $t1[$i][1] != $t2[$i][1]) {
|
|
625
|
|
- return [false, "{$table}: column {$t1[$i][0]} name or datatype mismatch {$t1[$i][0]}:{$t2[$i][0]} {$t1[$i][1]}:{$t2[$i][1]}"];
|
|
626
|
|
- }
|
|
627
|
|
- $column[] = $t1[$i][0];
|
|
628
|
|
- }
|
|
629
|
|
- $columnList = implode(', ', $column);
|
|
630
|
|
- $conditionList = implode(' AND ', array_map(function ($c) {
|
|
631
|
|
- return "{$c[0]} = ?";
|
|
632
|
|
- }, $condition));
|
|
633
|
|
- $argList = array_map(function ($c) {
|
|
634
|
|
- return $c[1];
|
|
635
|
|
- }, $condition);
|
|
636
|
|
-
|
|
637
|
|
- $sql = "INSERT INTO $softDeleteTable
|
|
638
|
|
- ($columnList)
|
|
639
|
|
- SELECT $columnList
|
|
640
|
|
- FROM $table
|
|
641
|
|
- WHERE $conditionList";
|
|
642
|
|
- $this->db->prepared_query($sql, ...$argList);
|
|
643
|
|
- if ($this->db->affected_rows() == 0) {
|
|
644
|
|
- return [false, "condition selected 0 rows"];
|
|
645
|
|
- }
|
|
646
|
|
-
|
|
647
|
|
- if (!$delete) {
|
|
648
|
|
- return [true, "rows affected: " . $this->db->affected_rows()];
|
|
649
|
|
- }
|
|
650
|
|
-
|
|
651
|
|
- $sql = "DELETE FROM $table WHERE $conditionList";
|
|
652
|
|
- $this->db->prepared_query($sql, ...$argList);
|
|
653
|
|
- return [true, "rows deleted: " . $this->db->affected_rows()];
|
|
654
|
|
- }
|
|
655
|
|
- */
|
|
656
|
647
|
}
|