Files
cwsvJudo/homepage/wkParticipo/api.php

141 lines
4.9 KiB
PHP

<?php
///---
/// Interface zum Wettkampfplaner
///
/// - zum externen Aufrufen über http
/// - Datenüberegabe über POST-Request
/// - alle (User-)Parameter werden in einem assoziativen Array
/// `parameter` erwartet
/// - die aufzurufende funktion wird als `action`-Parameter des
/// queueStrings erwartet.
/// - Für jede aufzurufende Funktion wird ein Wrapper inklusive
/// Inputdatenvaildierung bereitgestellt
///---
setlocale (LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
$defaultValues['backtoUrl'] = "http://cwsvjudo.bplaced.net/pages/desktop/wkParticipo";
/// @todo was wird hier wirklich benötigt?
require_once('./local/wkParticipoConf.php.inc');
require_once('./auth.php');
require_once('./local/db.php.inc');
require_once('./lib/wkParticipoLib.inc.php');
$basePath = "/users/cwsvjudo/www";
require_once($basePath."/config/cwsvJudo.config.php");
require_once($basePath."/config/phpcount.config.php");
require_once($basePath."/ressourcen/phpLib/phpcount/phpcount.php");
require_once($basePath."/ressourcen/phpLib/cwsvJudo/miscAssis.php");
/// Setzen der Farben des Users
function setUserColors($dbConn, $userId, $wantedColors = ['backgroundColor'=>"#FFAE00", 'highlightColor'=>"#FF8100", 'buttonColor'=>"#291670"]){
//$colorTypes = ["backgroundColor", "highlightColor", "buttonColor"];
$defaultColors =[ // unterstützte Farben und ihre Standardbelegung
'backgroundColor'=> "#FFAE00", // Hintergrundfarbe
'highlightColor'=>"#FF8100", // Markierungen zum hervorheben
'buttonColor'=>"#291670" // Interaktionsflächen
];
// Abfragen der gesamten Konfiguration des Users
$tmpBindArray = array(
":userId"=>array('value'=>$userId, 'data_type'=>PDO::PARAM_INT)
);
// echo("DBG: setColors-userId".strval($userId));
// echo("DBG: setColors-BindArray");var_dump($tmpBindArray);
$usersConfig = dbQuery(
$dbConn,
"SELECT config FROM `wkParticipo_Users` WHERE id = :userId;",
//"SELECT * FROM `wkParticipo_Users` WHERE id = :userId;",
array(
":userId"=>array('value'=>(int)$userId, 'data_type'=>PDO::PARAM_INT)
)
);
// echo("DBG: setUserColors-usersConfig"); var_dump($usersConfig);
// - json-formatierten Konfigurationsstring in ein Assoziatives Array
// laden
// - Im Fehlerfalle ein leeres Array weitergeben
try{
$usersConfig = json_decode($usersConfig[0]['config'], true);
if( empty($usersConfig) )
throw new Exception("No usersConfig available!");
}
catch(Exception $e) {
echo("Message: " .$e->getMessage());
$usersConfig = array();
}
// Nicht mehr unterstützte Farben entfernen
foreach($usersConfig['colors'] as $colorType=>$colorValue){
// echo("DBG: setUserColors-colorType: ".$colorType);
// echo("DBG: setUserColors-defaultColors:"); var_dump($defaultColors);
if( !array_key_exists($colorType, $defaultColors) ){
//echo("DBG: ".$colorType." is no defaultColor! DefaultColors are\n");var_dump($defaultColors);
unset($usersConfig['colors'][$colorType]);
}
}
// - Neue Farben setzen, sofern sie validiert werden können
// - Es werden nur Farben übernommen, die auch unterstützt werden
foreach($defaultColors as $key=>$value){ // Alle unterstützten Farben testen
if( !preg_match("/^#(?:[0-9a-fA-F]{3}){1,2}$/", $wantedColors[$key]) ){
//echo("DBG: ".$wantedColors[$key]." doesn't regmatches to a color!");
$usersConfig['colors'][$key] = $value;
}
else{
$usersConfig['colors'][$key] = $wantedColors[$key];
}
}
// Die aktualiserte Fassung der Konfiguration abspeicher
//echo("DBG: update\n"); var_dump($usersConfig);
dbQuery(
$dbConn,
"UPDATE `wkParticipo_Users` SET config = :jsonConfig WHERE id = :userId;",
array(
":jsonConfig"=>array('value'=>json_encode($usersConfig), 'data_type'=>PDO::PARAM_STR),
":userId"=>array('value'=>$userId, 'data_type'=>PDO::PARAM_INT)
)
);
// Auch die aktuellen sessionDaten noch anpassen
$_SESSION['user']['userConfig']['colors'] = $usersConfig['colors'];
}
function apiActionSetUserColors($parameter){
//echo("DBG: apiActionSetColors-parameter:");var_dump($parameter);
// Inputvalidierung
try{
if( !is_positive_integer($parameter['userId']) )
throw new Exception("EXP: userId has to be a positive integer, but is !");
if( !is_array($parameter['colors']) )
throw new Exception("EXP: colors not given in an array!");
setUserColors(getCwsvJudoDbConn(), $parameter['userId'], $parameter['colors']);
}
catch(Exception $e){
echo("apiActionSetUserColors - ".$e->getMessage()."\n");
echo("parameter:"); var_dump($parameter);
}
return;
}
// Als allererstes den Header setzen, damit wir auch Fehlermeldungen
// ausgeben können
if(isset($_POST['backtoUrl'])){
if (filter_var($_POST['backtoUrl'], FILTER_VALIDATE_URL)) {
header("Location: ".$_POST['backtoUrl']);
} else {
header("Location: ".$defaultValues['backtoUrl']);
}
}
//echo("DBG: api-POST"); var_dump($_POST);
/// @todo übersichtlichere Variante gewüscht
if($_POST['action'] == "setUserColors"){
apiActionSetUserColors(array('userId'=>$_POST['parameter']['userId'], 'colors'=>$_POST['parameter']['colors']));
}
?>