request->getHttpMethod(); $uriParams=$this->request->getUriParams(); // Auth with token phase (id = 0 because not used when checking admin) $authResponse = $this->authUser(-1, 'admin'); if($authResponse->getCode()!=200){ return $authResponse; } 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; } }