Compare commits

25 Commits

Author SHA1 Message Date
fbfeeb7679 trying to resolve conflict 2023-01-18 08:39:45 +01:00
Johannes Kantz
2860a01cf8 fix: login with Token 2023-01-12 15:29:58 +01:00
Johannes Kantz
a887921e5d quick safe 2023-01-12 15:22:32 +01:00
Johannes Kantz
a2178733da fix: remove print 2023-01-12 15:09:39 +01:00
Johannes Kantz
5fea5ad15f fix: update vales with quotes 2023-01-12 15:09:08 +01:00
Johannes Kantz
fe5efbe8f1 fix: remove test 2023-01-12 15:04:49 +01:00
Johannes Kantz
f25b2a72f7 add: user login with password 2023-01-12 15:04:15 +01:00
Johannes Kantz
7b5672d36b quick safe 2023-01-12 14:44:52 +01:00
Johannes Kantz
21ebecac54 fix: select fetch 2023-01-12 14:41:51 +01:00
Johannes Kantz
acfae23029 fix: remove print 2023-01-12 14:37:11 +01:00
Johannes Kantz
e1a0f112a2 fix: select values with single quotes 2023-01-12 14:36:37 +01:00
Johannes Kantz
23cf1b54e4 fix: insert vales with single quotes 2023-01-12 14:28:40 +01:00
Johannes Kantz
95a2c3e3fe fix: add Body to Post request 2023-01-12 14:09:02 +01:00
Johannes Kantz
148f95087d add: login begin 2023-01-11 14:45:18 +01:00
Johannes Kantz
81cdae85b0 add: user 2023-01-11 14:45:18 +01:00
e31b7b6319 add: select 2023-01-11 14:41:30 +01:00
84bf6b2d36 add: select 2023-01-11 14:40:12 +01:00
143874af78 add: Ein paar Kommentare bei der DB 2023-01-11 14:36:18 +01:00
b0ef6fc88d add: select 2023-01-11 11:02:42 +01:00
85f49f1c8c add: DB Username Unique + ein paar Datentyp Fehler behoben 2023-01-11 10:48:57 +01:00
aa90447465 add: gettone in der DB (Token) 2023-01-11 10:45:08 +01:00
d4e275e28c add: alt + L 2023-01-11 10:33:20 +01:00
6dffa09e88 add: insert into 2022-12-21 11:19:40 +01:00
a568d36eb2 Merge remote-tracking branch 'origin/don' into don
# Conflicts:
#	BancaDati/BancaDati.php
2022-12-21 11:10:40 +01:00
d8154b9d0f add: insert into 2022-12-21 11:09:49 +01:00
5 changed files with 201 additions and 50 deletions

View File

@@ -1,71 +1,80 @@
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
SET
SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET
time_zone = "+00:00";
CREATE TABLE `ingredienti` ( /*Zutaten*/
`id` varchar(36) NOT NULL,
`cognome` varchar(200) NOT NULL, /*Name*/
`caloriePerCento` int(5) NOT NULL, /*Kalorien pro Gramm*/
`ilPeso` int(5) NULL, /*Gewicht*/
`prezzo` decimal(4,2) NOT NULL, /*Preis*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
CREATE TABLE `ingredienti`
( /*Zutaten*/
`id` varchar(36) NOT NULL,
`cognome` varchar(200) NOT NULL, /*Name*/
`caloriePerCento` integer(5) NOT NULL, /*Kalorien pro Gramm*/
`ilPeso` integer(5) NULL, /*Gewicht*/
`prezzo` decimal(4, 2) NOT NULL, /*Preis*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `folla` ( /*Menge*/
`id` varchar(36) NOT NULL,
`unita` varchar(200) NOT NULL, /*Einheit*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
CREATE TABLE `folla`
( /*Menge*/
`id` varchar(36) NOT NULL,
`unita` varchar(200) NOT NULL, /*Einheit*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `elenco` ( /*Liste*/
`id` varchar(36) NOT NULL,
`creatore` varchar(200) NOT NULL, /*Ersteller*/
`coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
CREATE TABLE `elenco`
( /*Liste*/
`id` varchar(36) NOT NULL,
`creatore` varchar(200) NOT NULL, /*Ersteller*/
`coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `utente` ( /*Benutzer*/
`id` varchar(36) NOT NULL,
`email` varchar(200) NOT NULL, /*Email*/
`parolaDordine` integer(10) NOT NULL, /*Passwort*/
`nomeUtente` integer(10) NOT NULL, /*Benutzernamen*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
CREATE TABLE `utente`
( /*Benutzer*/
`id` varchar(36) NOT NULL,
`email` varchar(200) NOT NULL, /*Email*/
`parolaDordine` varchar(255) NOT NULL, /*Passwort*/
`nomeUtente` varchar(50) UNIQUE NOT NULL, /*Benutzernamen*/
`gettone` varchar(255), /*Token für Session*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `elencoIngredienti` ( /*Liste_Zutaten*/
`id` varchar(36) NOT NULL,
`ingredientiID` varchar(36) NOT NULL, /*ZutatenID*/
`elencoID` varchar(36) NOT NULL, /*ListeID*/
`follaID` varchar(36) NOT NULL, /*MengeID*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
CREATE TABLE `elencoIngredienti`
( /*Liste_Zutaten*/
`id` varchar(36) NOT NULL,
`ingredientiID` varchar(36) NOT NULL, /*ZutatenID*/
`elencoID` varchar(36) NOT NULL, /*ListeID*/
`follaID` varchar(36) NOT NULL, /*MengeID*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `utenteElenco` ( /*Benutzer_Liste*/
`id` varchar(36) NOT NULL,
`elencoID` varchar(36) NOT NULL, /*ListeID*/
`utenteID` varchar(36) NOT NULL, /*BenutzerID*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
CREATE TABLE `utenteElenco`
( /*Benutzer_Liste*/
`id` varchar(36) NOT NULL,
`elencoID` varchar(36) NOT NULL, /*ListeID*/
`utenteID` varchar(36) NOT NULL, /*BenutzerID*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `ingredienti`
ADD PRIMARY KEY (`id`);
ALTER TABLE `folla`
ALTER TABLE `ingredienti` /*Zutaten*/
ADD PRIMARY KEY (`id`);
ALTER TABLE `elenco`
ALTER TABLE `folla` /*Menge*/
ADD PRIMARY KEY (`id`);
ALTER TABLE `utente`
ALTER TABLE `elenco` /*Liste*/
ADD PRIMARY KEY (`id`);
ALTER TABLE `elencoIngredienti`
ALTER TABLE `utente` /*Benutzer*/
ADD PRIMARY KEY (`id`);
ALTER TABLE `elencoIngredienti` /*Liste_Zutaten*/
ADD PRIMARY KEY (`id`),
ADD CONSTRAINT `FK_ElencoIngredienti_Ingredienti` FOREIGN KEY (`ingredientiID`) REFERENCES `ingredienti`(`id`), /*Liste_Zutaten hat Foreignkey von Zutaten(id)*/
ADD CONSTRAINT `FK_ElencoIngredienti_Elenco` FOREIGN KEY (`elencoID`) REFERENCES `elenco`(`id`), /*Liste_Zutaten hat Foreignkey von Liste(id)*/
ADD CONSTRAINT `FK_ElencoIngredienti_Folla` FOREIGN KEY (`follaID`) REFERENCES `folla`(`id`); /*Liste_Zutaten hat Foreignkey von Menge(id)*/
ALTER TABLE `utenteElenco`
ALTER TABLE `utenteElenco` /*Benutzer_Liste*/
ADD PRIMARY KEY (`id`),
ADD CONSTRAINT `FK_UtenteElenco_Utente` FOREIGN KEY (`utenteId`) REFERENCES `utente`(`id`), /*Benutzer_Liste hat Foreignkey von Benutzer(id)*/
ADD CONSTRAINT `FK_UtenteElenco_Elenco` FOREIGN KEY (`elencoId`) REFERENCES `elenco`(`id`); /*Benutzer_Liste hat Foreignkey von Liste(id)*/

View File

@@ -2,6 +2,9 @@
namespace BancaDati;
use PDO;
use PDOException;
class BancaDati {
private $dbName = "BancaDati";
private $linkName = "localhost";
@@ -14,11 +17,11 @@ class BancaDati {
}
private function linkDB() {
try {
$this->pdo = new \PDO("mysql:dbname=$this->dbName;host=$this->linkName"
$this->pdo = new PDO("mysql:dbname=$this->dbName;host=$this->linkName"
, $this->user
, $this->pw
, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
} catch (\PDOException $e) {
, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (PDOException $e) {
die;
}
}
@@ -30,6 +33,32 @@ class BancaDati {
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
/**
* Einheitliche Insert Funktion
* @param string $table
* @param array $values
* @return void
* author Simon Bock
*/
public function insert(string $table, array $values){
$value = "";
$column = "";
foreach ($values as $col => $v){
$value .= $v . ",";
$column .= $col . ",";
}
$value = trim($value, ",");
$column = trim($column, ",");
$sql = "INSERT INTO $table($column) VALUES ($value);";
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
}catch (PDOException $e){
die;
}
}
/**
* Einheitliche Update Funktion
* @param string $table
@@ -49,7 +78,7 @@ class BancaDati {
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
}catch (\PDOException $e){
}catch (PDOException $e){
die;
}
}
@@ -66,7 +95,37 @@ class BancaDati {
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
}catch (\PDOException $e){
}catch (PDOException $e){
die;
}
}
/**
* einheitliche Select Funktion
* @param string $table
* @param array $where ["column"]=>"value" es wird mit LIKE verglichen und mit AND verbunden
* @param array|null $order ["by"]=>"column"; ["order"]=>"ASC|DESC"
* @return void
* @author Malte Schulze Hobeling
*/
public function select(string $table, array $where, array $order = null){
$whereString = "";
foreach ($where as $col => $v) {
if($whereString != ""){
$whereString .= " AND ";
}
$whereString .= $col . " LIKE " . $v;
}
$sql = "SELECT * FROM ".$table." WHERE ".$whereString;
if(isset($order["by"])){
$sql .= " ORDER BY ".$order["by"];
}
if(isset($order["order"])){
$sql .= $order["order"];
}
try {
return $this->pdo->query($sql);
}catch (PDOException $e){
die;
}
}

View File

@@ -30,7 +30,7 @@ class Router
$this->request = $_SERVER;
}
if (isset($_POST)) {
$this->request["body"] = $_POST;
$this->request["body"] = json_decode(file_get_contents('php://input'), true);
}
if (isset($_POST)) {
$this->request["params"] = $_GET;

53
User.php Normal file
View File

@@ -0,0 +1,53 @@
<?php
require_once("BancaDati/BancaDati.php");
use BancaDati\BancaDati;
class User {
public string $id;
public string $username;
public string $email;
public string $token;
private string $password;
private BancaDati $db;
public function __construct() {
$this->db = new BancaDati();
return $this;
}
public function exists() {
return true;
}
public function loginWithUsername(string $username, string $password) : string {
$userObject = $this->db->select("utente", ["nomeUtente" => $username]);
if(!$userObject){
return false;
}
$this->id = $userObject["id"];
$this->username = $userObject["nomeUtente"];
$this->email = $userObject["email"];
$this->password = $userObject["parolaDordine"];
$this->token = $this->db->createUUID();
if($this->password != $password){
return false; // ungültiges password
}
$this->db->update("utente", $this->id, ["gettone" => $this->token]);
return $this->token;
}
public function loginWithToken(string $token){
$userObject = $this->db->select("utente", ["gettone" => $token]);
if(!$userObject){
return false;
}
$this->id = $userObject["id"];
$this->username = $userObject["nomeUtente"];
$this->email = $userObject["email"];
$this->password = $userObject["parolaDordine"];
$this->token = $token;
return $this;
}
}

View File

@@ -3,6 +3,7 @@
require_once("Router/Router.php");
require_once("Router/Response.php");
require_once("BancaDati/BancaDati.php");
require_once("User.php");
use Router\Response;
use Router\Router;
@@ -11,16 +12,45 @@ use BancaDati\BancaDati;
$app = new Router("/DirektiveDesDons");
$db = new BancaDati();
$app->use("/", function (array &$req, Response $res) {
if(isset($_COOKIE["TOKEN"])){
$user = new User();
if($user->loginWithToken($_COOKIE["TOKEN"])){
$req["user"] = $user;
}
}
});
$app->get("/", function (array $req, Response $res) {
$res->send("Hello World");
//var_dump($req["user"]);
});
$app->get("/user", function (array $req, Response $res) {
$res->send("user");
});
$app->get("/user/:id", function (array $req, Response $res) {
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
$db->select("utente", ["username" => $req["id"]]);
$res->send("user " . $req["params"]["id"]);
});
$app->post("/createuser", function (array $req, Response $res) use ($db) {
$db->insert("utente", ["email" => "test@email.com", "parolaDordine" => "password", "nomeUtente" => "testuser"]);
$res->send("user ");
});
$app->post("/login", function( array $req, Response $res) use ($db) {
$username = $req["body"]["username"];
$password = $req["body"]["password"];
$user = new User();
$usertoken = $user->loginWithUsername($username, $password);
if($usertoken){
setcookie("TOKEN", $usertoken, time()+3600); // 1h
$res->send("Login successful" . "token: " . $usertoken, 200);
}else{
$res->send("Login failed", 403);
}
});
$app->start();