Commit 418247a6 authored by Zohten's avatar Zohten

blacklist endpoint completed

parent 8fd3ef9c
<?php
class BlacklistController extends Controller
{
public function __construct($name, $request)
{
parent::__construct($name, $request);
}
/**
* Process incoming request for the /blacklist endpoint
*
* @return Response
*/
public function processRequest()
{
$httpMethod=$this->request->getHttpMethod();
$uriParams=$this->request->getUriParams();
switch ($httpMethod) {
case 'GET':
// If there is a uriParams, it is the /blacklist/{ip} endpoint
if ($uriParams) {
return $this->checkBlacklist($uriParams[0]);
}
// Else, it is the /blacklist endpoint
return $this->getAllBlacklistedIP();
break;
case 'POST':
$body=$this->request->getData();
return $this->addBlacklist($body);
break;
case 'DELETE':
if ($uriParams) {
return $this->deleteBlacklist($uriParams[0]);
}
break;
}
$message = json_encode(["message" => "unsupported parameters or method in blacklists"]);
return Response::errorResponse($message);
}
/**
* (GET) Get all IP in BLACKLIST table
*
* @return Response
*/
protected function getAllBlacklistedIP()
{
$blacklists = Blacklist::getList();
$response = Response::okResponse(json_encode($blacklists, JSON_PRETTY_PRINT));
return $response;
}
/**
* (GET) Check if a specific IP is in BLACKLIST table
*
* @param int $id id of the Blacklist
* @return Response
*/
protected function checkBlacklist($ip)
{
$isblacklisted = Blacklist::isBannedIP($ip);
$response = Response::okResponse(json_encode(['isBlacklisted'=>$isblacklisted]));
return $response;
}
/**
* (POST) Add a specific IP in BLACKLIST table
*
* @param array $array array containing
* @return Response
*/
protected function addBlacklist($array)
{
$ip = $array['ip'];
// Filter if it is not valid IP
if(filter_var($ip, FILTER_VALIDATE_IP)){
Blacklist::addIP($ip);
$message = json_encode(["message" => 'IP succesfully blacklisted!']);
$response = Response::createdResponse($message);
}else{
$message = json_encode(["message" => 'IP is not in valid format']);
$response = Response::errorResponse($message);
}
return $response;
}
/**
* (DELETE) Delete a specific IP in BLACKLIST
*
* @param int $ip ip to delete
* @return Response
*/
protected function deleteBlacklist($ip)
{
// Delete phase
Blacklist::removeIP($ip);
$message = json_encode(["message" => 'Blacklist succesfully deleted!']);
$response = Response::okResponse($message);
return $response;
}
}
<?php
class Blacklist extends Model
{
// ===========
// = Statics =
// ===========
protected static $table_name = 'MJ_BLACKLIST';
public static function getList()
{
$stm = parent::exec('GET_BLACKLIST');
return $stm->fetchAll();
}
public static function isBannedIP($ip)
{
$stm = parent::exec('CHECK_BLACKLIST', ['ip' => $ip]);
$result = $stm->fetch();
if($result){
return true;
}
return false;
}
public static function addIP($ip)
{
$begin_date = date('Y-m-d');
$pimped_array = ['ip'=>$ip,'date'=>$begin_date];
$stm = parent::exec('ADD_BLACKLIST', $pimped_array);
}
public static function removeIP($ip)
{
$stm = parent::exec('REMOVE_BLACKLIST', ['ip' => $ip]);
}
public function ip()
{
return trim($this->IP);
}
}
\ No newline at end of file
<?php
Blacklist::addSqlQuery(
'GET_BLACKLIST',
'SELECT * FROM MJ_BLACKLIST_IP'
);
Blacklist::addSqlQuery(
'CHECK_BLACKLIST',
'SELECT IP FROM MJ_BLACKLIST_IP WHERE IP=:ip'
);
Blacklist::addSqlQuery(
'ADD_BLACKLIST',
'INSERT INTO MJ_BLACKLIST_IP (ID_BLACKLIST, IP, DATE_BAN)
VALUES (NULL, :ip, :date)'
);
Blacklist::addSqlQuery(
'REMOVE_BLACKLIST',
'DELETE FROM MJ_BLACKLIST_IP WHERE IP=:ip'
);
\ No newline at end of file
### Récupérer toutes les ip bannies
GET http://localhost/index.php/blacklist
### Vérifie si l'ip appartient à la blacklist (oui)
GET http://localhost/index.php/blacklist/192.1.1.51
### Vérifie si l'ip appartient à la blacklist (non)
GET http://localhost/index.php/blacklist/192.1.1.5
### Ajouter une ip valide à la blacklist
POST http://localhost/index.php/blacklist
{
"ip":"192.1.1.4"
}
### Ajouter une ip non valide à la blacklist
POST http://localhost/index.php/blacklist
{
"ip":"pouet"
}
###
DELETE http://localhost/index.php/blacklist/192.1.1.4
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment