This commit is contained in:
fra-2107
2024-11-22 13:58:16 -05:00
parent c6cf2b07f6
commit d4ae3c236b
13 changed files with 524 additions and 0 deletions

7
web2/php/constants.php Normal file
View File

@ -0,0 +1,7 @@
<?php
// Database constants.
define('DB_USER', 'sidps');
define('DB_PASSWORD', 'sidps');
define('DB_NAME', 'sidps');
define('DB_SERVER', 'localhost');
?>

127
web2/php/database.php Normal file
View File

@ -0,0 +1,127 @@
<?php
require_once('constants.php');
//----------------------------------------------------------------------------
//--- dbConnect --------------------------------------------------------------
//----------------------------------------------------------------------------
// Create the connection to the database.
// \return False on error and the database otherwise.
function dbConnect()
{
try
{
$db = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME.';charset=utf8',
DB_USER, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $exception)
{
error_log('Connection error: '.$exception->getMessage());
return false;
}
return $db;
}
//----------------------------------------------------------------------------
//--- dbRequestAlertes --------------------------------------------------------
//----------------------------------------------------------------------------
// Function to get all alertes
// \param db The connected database.
// \return The list of alertes.
function dbRequestAlerts($db)
{
try
{
$request = 'SELECT * FROM alertes';
$statement = $db->prepare($request);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $exception)
{
error_log('Request error: '.$exception->getMessage());
return false;
}
return $result;
}
//----------------------------------------------------------------------------
//--- dbAddCTweet ------------------------------------------------------------
//----------------------------------------------------------------------------
// Add a tweet.
// \param db The connected database.
// \param login The login of the user.
// \param text The tweet to add.
// \return True on success, false otherwise.
function dbAddTweet($db, $login, $text)
{
try
{
$request = 'INSERT INTO tweets(login, text) VALUES(:login, :text)';
$statement = $db->prepare($request);
$statement->bindParam(':login', $login, PDO::PARAM_STR, 20);
$statement->bindParam(':text', $text, PDO::PARAM_STR, 80);
$statement->execute();
}
catch (PDOException $exception)
{
error_log('Request error: '.$exception->getMessage());
return false;
}
return true;
}
//----------------------------------------------------------------------------
//--- dbModifyTweet ----------------------------------------------------------
//----------------------------------------------------------------------------
// Function to modify a tweet.
// \param db The connected database.
// \param id The id of the tweet to update.
// \param login The login of the user.
// \param text The new tweet.
// \return True on success, false otherwise.
function dbModifyTweet($db, $id, $login, $text)
{
try
{
$request = 'UPDATE tweets SET text=:text WHERE id=:id AND login=:login ';
$statement = $db->prepare($request);
$statement->bindParam(':id', $id, PDO::PARAM_INT);
$statement->bindParam(':login', $login, PDO::PARAM_STR, 20);
$statement->bindParam(':text', $text, PDO::PARAM_STR, 80);
$statement->execute();
}
catch (PDOException $exception)
{
error_log('Request error: '.$exception->getMessage());
return false;
}
return true;
}
//----------------------------------------------------------------------------
//--- dbDeleteTweet ----------------------------------------------------------
//----------------------------------------------------------------------------
// Delete a tweet.
// \param db The connected database.
// \param id The id of the tweet.
// \param login The login of the user.
// \return True on success, false otherwise.
function dbDeleteTweet($db, $id, $login)
{
try
{
$request = 'DELETE FROM tweets WHERE id=:id AND login=:login';
$statement = $db->prepare($request);
$statement->bindParam(':id', $id, PDO::PARAM_INT);
$statement->bindParam(':login', $login, PDO::PARAM_STR, 20);
$statement->execute();
}
catch (PDOException $exception)
{
error_log('Request error: '.$exception->getMessage());
return false;
}
return true;
}
?>

46
web2/php/request.php Normal file
View File

@ -0,0 +1,46 @@
<?php
require_once('database.php');
// Database connexion.
$db = dbConnect();
if (!$db)
{
header ('HTTP/1.1 503 Service Unavailable');
exit;
}
// Check the request.
$requestMethod = $_SERVER['REQUEST_METHOD'];
$request = $_SERVER['PATH_INFO'];
$request = explode('/', $request);
if ($request[1] != 'alertes')
{
header('HTTP/1.1 400 Bad Request');
exit;
}
if ($requestMethod == 'GET')
{
$data = dbRequestAlerts($db);
}
if ($requestMethod == 'PUT')
{
parse_str(file_get_contents('php://input'), $_PUT);
if($id !=''&&isset($_PUT['login'])&&isset($_PUT['text']))
$data = dbModifyTweet($db, $id, $_PUT['login'], strip_tags($_PUT['text']));
}
// Send data to the client.
header('Content-Type: application/json; charset=utf-8');
header('Cache-control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
if($requestMethod == 'POST')
header('HTTP/1.1 201 Created');
else
header('HTTP/1.1 200 OK');
echo json_encode($data);
exit;
?>