Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
projet-cdaw
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Quentin Vrel
projet-cdaw
Commits
418247a6
Commit
418247a6
authored
Dec 01, 2020
by
Zohten
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
blacklist endpoint completed
parent
8fd3ef9c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
196 additions
and
0 deletions
+196
-0
BlacklistController.class.php
BackEnd/src/controller/BlacklistController.class.php
+105
-0
Blacklist.class.php
BackEnd/src/model/Blacklist.class.php
+45
-0
Blacklist.sql.php
BackEnd/src/sql/Blacklist.sql.php
+21
-0
blacklistRequest.http
BackEnd/src/testRequests/blacklistRequest.http
+25
-0
No files found.
BackEnd/src/controller/BlacklistController.class.php
0 → 100644
View file @
418247a6
<?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
;
}
}
BackEnd/src/model/Blacklist.class.php
0 → 100644
View file @
418247a6
<?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
BackEnd/src/sql/Blacklist.sql.php
0 → 100644
View file @
418247a6
<?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
BackEnd/src/testRequests/blacklistRequest.http
0 → 100644
View file @
418247a6
### 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment