*/ use DevAAC\Models\IpBan; $meta = array('name' => 'IP Ban', 'description' => 'Disallows access to users who are IP banned. APC user cache is recommended for performance.', 'version' => '0.1', 'author' => 'Don Daniello', 'link' => 'https://github.com/DevelopersPL/DevAAC'); /* * This plugin strongly benefits from APC user cache! */ if (!in_array(basename(__FILE__), $DevAAC->enabled_plugins)) { return array_merge($meta, array('enabled' => false)); } // http://docs.slimframework.com/#How-to-Use-Hooks $DevAAC->hook('slim.before', function () use($DevAAC) { $req = $DevAAC->request; $apc = false; if (extension_loaded('apc') && ini_get('apc.enabled')) { $apc = true; $objname = 'ipban_' . $req->getIp(); } if ($apc && apc_fetch($objname)) { $DevAAC->halt(403, 'Your IP address is banned.'); } else { $ipban = IpBan::find(ip2long($req->getIp())); if ($ipban) { $DevAAC->halt(403, 'Your IP address is banned.'); if ($apc) { apc_store($objname, true, 10 * 60); } // THE INFORMATION WILL BE IN CACHE FOR 10 MINUTES SO WE CAN REJECT REQUESTS WITHOUT RUNNING ANY SQL QUERIES } } }); return array_merge($meta, array('enabled' => true));
throw new InputErrorException('This IP is already banned.', 409); } if (!filter_var($req->getAPIParam('ip'), FILTER_VALIDATE_IP)) { throw new InputErrorException('IP address is not valid.', 400); } $player = Player::find($req->getAPIParam('banned_by')); if (!$player) { throw new InputErrorException('The banned_by player not found.', 404); } if ($player->account->id !== $DevAAC->auth_account->id) { throw new InputErrorException('The banned_by player is not yours!', 406); } if ($player->group_id < 2) { throw new InputErrorException('The banned_by player must have group_id > 1.', 406); } $ban = new IpBan(array('ip' => $req->getAPIParam('ip'), 'reason' => $req->getAPIParam('reason'), 'banned_at' => new \DevAAC\Helpers\DateTime(), 'expires_at' => $req->getAPIParam('expires_at', 0), 'banned_by' => $player->id)); $ban->save(); $DevAAC->response->headers->set('Content-Type', 'application/json'); $DevAAC->response->setBody($ban->toJson(JSON_PRETTY_PRINT)); }); /** * @SWG\Resource( * basePath="/api/v1", * resourcePath="/server", * @SWG\Api( * path="/server/ipBans/{ip}", * description="Operations on server", * @SWG\Operation( * summary="Delete IP ban", * notes="Need to have admin rights", * method="DELETE",