phpstan level 0 error free - fixes for kyu subpage - move mams into participo framework - remove legacy `lib/db.php` usage - add attributer admin function - add newsposter - fixing apiKey creation
171 lines
5.3 KiB
PHP
171 lines
5.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* interface for connecting and communicating with a database
|
|
*/
|
|
class dbConnector
|
|
{
|
|
private static $db = null;
|
|
|
|
public static $options = [
|
|
"dbCharset" => "ISO-8859-1",
|
|
"outCharset" => "UTF-8",
|
|
];
|
|
|
|
// connect to the database
|
|
public static function connect($hostname, $dbName, $user, $password)
|
|
{
|
|
return self::setDbConnection(
|
|
self::connectToPdo($hostname, $dbName, $user, $password),
|
|
);
|
|
}
|
|
public static function setOptions($options)
|
|
{
|
|
self::$options = $options;
|
|
}
|
|
|
|
public static function getDbConnection()
|
|
{
|
|
return self::$db;
|
|
}
|
|
|
|
/// perform a pdo-query
|
|
///
|
|
/// @param $aQueryString
|
|
/// @param $aBindArray e.g. array(
|
|
/// ':userId' => array('value'=>$anUserId, 'data_type'=>PDO::PARAM_INT),
|
|
/// ':attributeId'=> array('value'=>$anAttributeId, 'data_type'=>PDO::PARAM_INT) )
|
|
/// @param $someOption
|
|
public static function query(
|
|
$aQueryString,
|
|
$aBindArray = [],
|
|
$someOptions = [],
|
|
) {
|
|
// var_dump($aQueryString, $aBindArray);
|
|
// Standardbelegungen
|
|
$someOptions["dbCharset"] =
|
|
$someOptions["dbCharset"] ??
|
|
(self::$options["dbCharset"] ?? "ISO-8859-1");
|
|
$someOptions["outCharset"] =
|
|
$someOptions["outCharset"] ??
|
|
(self::$options["outCharset"] ?? "UTF-8");
|
|
$someOptions["dontFetch"] = $someOptions["dontFetch"] ?? false;
|
|
|
|
$ignoreErrors = $someOptions["ignoreErrors"] ?? false;
|
|
|
|
/// @toDo: Bisher wird nur die Rückgabe konvertiert. Eigentlich muss
|
|
/// doch auch die Eingabe konvertiert werden. Aber das jetzt
|
|
/// umzustellen wird schwer! Die User im Wettkampfplaner sind ja z.B.
|
|
/// als UTF8 in latin1(?) gespeichert.
|
|
/// @toDo: Die Standardwerte sollten vielleicht aus einer config
|
|
/// kommen, nicht hardcoded
|
|
|
|
try {
|
|
$pdoStatement = self::$db->prepare($aQueryString);
|
|
foreach ($aBindArray as $bindName => $bind) {
|
|
if (
|
|
$bind["data_type"] == PDO::PARAM_STR &&
|
|
$bind["value"] !== null
|
|
) {
|
|
$bind["value"] = iconv(
|
|
$someOptions["outCharset"],
|
|
$someOptions["dbCharset"],
|
|
$bind["value"],
|
|
);
|
|
}
|
|
$pdoStatement->bindValue(
|
|
$bindName,
|
|
$bind["value"],
|
|
$bind["data_type"] ?? PDO::PARAM_STR,
|
|
);
|
|
}
|
|
$pdoResult = $pdoStatement->execute();
|
|
if (!$ignoreErrors && !$pdoResult) {
|
|
echo "Error during dbQuery!\n";
|
|
echo "DB-Error:\n";
|
|
var_dump(self::$db->errorInfo());
|
|
}
|
|
$ret = !$someOptions["dontFetch"]
|
|
? $pdoStatement->fetchAll(PDO::FETCH_ASSOC)
|
|
: null;
|
|
} catch (PDOException $db_error) {
|
|
print "Error!: " . $db_error->getMessage() . "<br/>";
|
|
return null;
|
|
}
|
|
|
|
// Zeichensatzkonvertierung
|
|
if (is_array($ret)) {
|
|
foreach ($ret as &$entry) {
|
|
array_walk(
|
|
$entry,
|
|
function (&$value, $key, $someOptions) {
|
|
if ($value !== null) {
|
|
$value = iconv(
|
|
$someOptions["dbCharset"],
|
|
$someOptions["outCharset"],
|
|
$value,
|
|
);
|
|
}
|
|
},
|
|
$someOptions,
|
|
);
|
|
}
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
// @todo docu
|
|
public static function getLastInsertId()
|
|
{
|
|
return self::$db->lastInsertId();
|
|
}
|
|
|
|
// get a Connection to the database
|
|
private static function connectToPdo($hostname, $dbName, $user, $password)
|
|
{
|
|
// var_dump($hostname, $dbName, $user, $password);
|
|
$dbConnection = null;
|
|
try {
|
|
$dbConnection = new PDO(
|
|
"mysql:host=" . $hostname . ";dbname=" . $dbName,
|
|
$user,
|
|
$password,
|
|
);
|
|
} catch (PDOException $dbError) {
|
|
echo "Error whilst getting a dbConnection!: " .
|
|
$dbError->getMessage();
|
|
}
|
|
return $dbConnection;
|
|
}
|
|
|
|
// set the dbConnection (just setting, no establishing)
|
|
private static function setDbConnection($dbConnection)
|
|
{
|
|
$success = false;
|
|
if ($dbConnection instanceof PDO) {
|
|
self::$db = $dbConnection;
|
|
$success = true;
|
|
} else {
|
|
self::$db = null;
|
|
}
|
|
return $success;
|
|
}
|
|
|
|
public static function debugEchoQuery($query, $params)
|
|
{
|
|
foreach ($params as $key => $value) {
|
|
switch ($value["data_type"]) {
|
|
case PDO::PARAM_STR:
|
|
$query = str_replace(
|
|
$key,
|
|
'\'' . $value["value"] . '\'',
|
|
$query,
|
|
);
|
|
default:
|
|
$query = str_replace($key, $value["value"], $query);
|
|
}
|
|
}
|
|
echo "query: " . $query . PHP_EOL;
|
|
}
|
|
}
|