Files
thetool/lib/FronkDB/FronkDB.php
2025-08-22 17:49:54 +02:00

387 lines
9.9 KiB
PHP

<?php
class FronkDB {
public $link;
private $result;
private $lastError;
private $log;
public $insert_id;
private static $instances = [];
public $host;
public $user;
public $pass;
public $db;
public function __construct($host = false, $user = false, $pass = false, $db = false) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
if(!$host) $this->host = FRONKDB_DBHOST;
if(!$user) $this->user = FRONKDB_DBUSER;
if(!$pass) $this->pass = FRONKDB_DBPASS;
if(!$db) $this->db = FRONKDB_DBNAME;
$this->connect();
$this->log = mfLoghandler::singleton();
}
public static function singleton($host = false, $user = false, $pass = false, $db = false) {
$instance_name = $host . $user . $db;
if(!$instance_name) {
$instance_name = "default";
}
if(!isset(self::$instances[$instance_name])) {
//echo "new FronkDB instance $instance_name\n";
$c = __CLASS__;
self::$instances[$instance_name] = new $c($host, $user, $pass, $db);
}
return self::$instances[$instance_name];
}
private function connect() {
//echo "FronkDB connection to ".$this->host." (".$this->user.", ".$this->db.")\n";
if(!$this->link) {
$this->link = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die("Error connecting to database..." . mysqli_error($this->link));
}
mysqli_select_db($this->link, $this->db) or die("Error connecting to database..." . mysqli_error($this->link));
if(function_exists("mysqli_set_charset")) {
mysqli_set_charset($this->link, 'utf8mb4');
} else {
$this->query("SET NAMES utf8mb4");
}
}
public function reconnect() {
$this->instances = [];
$this->link = false;
$this->connect();
}
public function disconnect() {
if($this->link) {
mysqli_close($this->link);
$this->link = false;
}
}
public function escape($string) {
return mysqli_real_escape_string($this->link, $string);
}
public function query($sql, $print_error = false) {
// unset last insert id on qvery new query
$this->insert_id = null;
if(!$this->link) {
$this->connect();
}
if(FRONKDB_SQLDEBUG == true) {
/*$this->log->debug("[FronkDB] ================");
$this->log->debug("[FronkDB] START SQL QUERY");
$bt = debug_backtrace();
foreach($bt as $n => $b) {
$this->log->debug($n.") ".$b["file"]."(".$b['line']."): ".$b['class']."->".$b['function']."()" );
}*/
$this->log->debug("[FronkDB] $sql");
//$this->log->debug("[FronkDB] END SQL QUERY");
//echo "$sql\n";
}
try {
$this->result = mysqli_query($this->link, $sql);
$return = $this->result;
$log_query = false;
// check for errors
if(!$this->result) {
$return = false;
$log_query = true;
$this->lastError = mysqli_error($this->link);
if($print_error) {
echo "Error in SQL-query:<br />\n" . $sql . "<br />\n" . $this->lastError . "<br />\n";
}
$this->log->warn("SQL Last Error: ".$this->lastError);
}
$this->insert_id = mysqli_insert_id($this->link);
// check for warnings
if(mysqli_warning_count($this->link) > 0) {
$log_query = true;
$warning = mysqli_get_warnings($this->link);
if($warning !== false) {
do {
$this->log->warn("SQL Warning: ".$warning->errno." - ".$warning->message);
} while ($warning->next());
}
}
if($log_query) {
$this->log->warn("SQL Query was: $sql");
}
return $return;
} catch(Exception $e) {
$this->log->warn("SQL Query Exception " . $e->getCode() . ": " . $e->getMessage() . "\nQuery was:\n$sql\n".$e->getTraceAsString());
throw $e;
}
}
public function getLastError() {
return $this->lastError;
}
public function num_rows($_res = false) {
$rows = false;
$res = $this->result;
if($_res)
$res = $_res;
if(!$res)
return 0;
if($rows = mysqli_num_rows($res))
return $rows;
return 0;
}
public function fetch_array($_res = false) {
$array = false;
$res = $this->result;
if($_res)
$res = $_res;
if(!$res)
return false;
if($array = mysqli_fetch_assoc($res))
return $array;
return false;
}
public function fetch_object($_res = false) {
$obj = false;
$res = $this->result;
if($_res)
$res = $_res;
if(!$res)
return false;
if($obj = mysqli_fetch_object($res))
return $obj;
return false;
}
public function fetch_all_assoc($_res = false) {
$array = array();
$res = $this->result;
if($_res)
$res = $_res;
if(!$res)
return false;
while($row = mysqli_fetch_assoc($res)) {
$array[] = $row;
}
return $array;
}
public function insert($_table, $_data, $_forcestr = array(), $_forcebinary = array()) {
if(empty($_table)) {
$this->lastError = "Error constructing INSERT: tablename ommited";
return false;
}
$table = $_table;
$fields = "";
$values = "";
$STRINGS = $_forcestr;
// Build INSERT
foreach($_data as $f => $v) {
$_Q = "'";
if(is_string($v)) {
$v = $this->escape($v);
}
if(is_numeric($v)) {
if(!in_array($f, $STRINGS)) {
$_Q = "";
}
}
$fields .= ",`$f`";
if($v === null) {
$values .= ", NULL";
} else {
if(in_array($f, $_forcebinary)) {
$values .= ", _binary" . $_Q . $v . $_Q;
} else {
$values .= "," . $_Q . $v . $_Q;
}
}
}
$fields = preg_replace('/^,/', '', $fields);
$values = preg_replace('/^,/', '', $values);
$SQLstr = "INSERT INTO `$table` ($fields) VALUES($values)";
if(!$this->query($SQLstr)) {
return false;
}
return true;
}
public function update($_table, $_data, $_where, $_forcestr = array(), $_forcebinary = array()) {
if(empty($_table)) {
$this->lastError = "Error constructing UPDATE: tablename ommited";
return false;
}
if(empty($_where)) {
$this->lastError = "Security breach on all decks! UPDATE without WHERE clause.";
return false;
}
$table = $_table;
$Pairs = "";
$where = "WHERE $_where";
$STRINGS = $_forcestr;
// Build field/value pairs string
foreach($_data as $f => $v) {
$_Q = "'";
if(is_string($v)) {
$v = $this->escape($v);
}
if(is_numeric($v)) {
if(!in_array($f, $STRINGS)) {
$_Q = '';
}
}
if($v === null) {
$Pairs .= ", `$f`=NULL";
} else {
if(in_array($f, $_forcebinary)) {
$Pairs .= ", `$f`= _binary " . $_Q . $v . $_Q;
} else {
$Pairs .= ", `$f`=" . $_Q . $v . $_Q;
}
}
}
$Pairs = preg_replace('/^,/', '', $Pairs);
$SQLstr = "UPDATE `$table` SET $Pairs $where";
if(!$this->query($SQLstr)) {
return false;
}
return true;
}
public function select($_table, $_fields = "*", $_where = "") {
$table = $_table;
$fields = $_fields;
$where = "";
if(!empty($_where)) {
$where = "WHERE $_where";
}
if(empty($_table)) {
$this->lastError = "No table specified";
return false;
}
if(empty($_fields))
$fields = "*";
if(!is_array($fields)) {
$fields = preg_split('/ ?, ?/', $fields);
}
$fstr = "";
foreach($fields as $f) {
$fstr .= ",`$f`";
if($f == "*") $fstr = "*";
}
$fstr = preg_replace('/^,/', '', $fstr);
$SQLstr = "SELECT $fstr FROM `$table` $where";
if($res = $this->query($SQLstr)) {
return $res;
}
return false;
}
public function delete($_table, $_where, $_limit = false) {
if(empty($_where)) {
$this->lastError = "Security breach on all decks! DELETE without WHERE clause!";
return false;
}
if(empty($_table)) {
$this->lastError = "Keine Tabelle angegeben!";
return false;
}
$table = $_table;
$where = $_where;
$limit = "";
if($_limit) {
$limit = "LIMIT $_limit";
}
$sql = "DELETE FROM `$table` WHERE $where $limit";
//echo $sql;
if($this->query($sql)) {
return true;
} else {
return false;
}
}
public function startTransaction() {
$this->query("START TRANSACTION");
}
public function commitTransaction() {
$this->query("COMMIT");
}
public function rollbackTransaction() {
$this->query("ROLLBACK");
}
}