Skip to content

Commit

Permalink
Merge pull request #14 from jikan-me/development
Browse files Browse the repository at this point in the history
Update changes
  • Loading branch information
irfan-dahir authored Jun 30, 2018
2 parents d0d80c0 + 0e4ca7d commit cec8818
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
20 changes: 17 additions & 3 deletions app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,24 @@ public function request($type = null, $query = null, $page = 1) {

$antiXss = new \voku\helper\AntiXSS();


$this->type = $type;
$this->query = urlencode($antiXss->xss_clean($query));

if (!is_null($query)) {
$this->query = $antiXss->xss_clean($query);
} else {
if (isset($_GET['q']) && !empty($_GET['q'])) {
$this->query = $antiXss->xss_clean($_GET['q']);
}
}

$this->page = $page;

if (isset($_GET['page'])) {
$this->page = (int) $_GET['page'];
if ($this->page < 1) { $this->page = 1; }
}

$jikan = new \Jikan\Jikan;

if ($type == 'anime' || $type == 'manga') {
Expand All @@ -87,7 +101,7 @@ public function request($type = null, $query = null, $page = 1) {
if (app('redis')->exists($this->hash)) {
$this->response['request_cached'] = true;
return response()->json(
$this->response + json_decode(app('redis')->get($this->hash), true)
$this->response + json_decode(app('redis')->get($this->hash), true), 200, [], JSON_UNESCAPED_UNICODE
);
}

Expand Down Expand Up @@ -172,7 +186,7 @@ public function request($type = null, $query = null, $page = 1) {
}

return response()->json(
$this->response + $jikan->response
$this->response + $jikan->response, 200, [], JSON_UNESCAPED_UNICODE // fix utf8 issues
);
}

Expand Down
39 changes: 39 additions & 0 deletions app/Http/Middleware/Blacklist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Middleware;

use Closure;

class Blacklist
{

private $request;
private $blacklist = [];

public function handle($request, Closure $next)
{
$this->loadList();

if ($this->inList()) {
return response()->json([
'error' => 'This IP has been blacklisted'
]);
}

return $next($request);
}

private function loadList() {
if (!file_exists(BLACKLIST_PATH)) {
file_put_contents(BLACKLIST_PATH, json_encode([]));
}

$this->blacklist = json_decode(file_get_contents(BLACKLIST_PATH), true);
}

private function inList() {
$ip = $_SERVER['REMOTE_ADDR'];
return in_array($ip, $this->blacklist) ? true : false;
}

}
1 change: 0 additions & 1 deletion app/Http/Middleware/Throttle.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function hit() {
$this->ip = $_SERVER['REMOTE_ADDR'];
$date = date("d-m-Y");


if (!isset($this->sessions[$this->ip])) { // register the session
$this->sessions[$this->ip] = [
$date => 0
Expand Down
4 changes: 3 additions & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
Defines
*/
define('SESSION_STORAGE_PATH', '/var/www/api.jikan/storage/app/sessions.json'); // depreciated. Using Redis now
define('BLACKLIST_PATH', '/var/www/api.jikan/storage/app/blacklist.json');
define('RATE_LIMIT', 5000); // per day
define('CACHE_EXPIRE', 3600 * 24 * 3); // 3 days
define('CACHE_EXPIRE_SEARCH', 3600 * 6); // 6 hours
//define('CACHE_EXPIRE', 4); // 60 seconds | dev
//define('CACHE_EXPIRE_SEARCH', 4); // 60 seconds | dev

define('REST_VERSION', '2.2');
define('SOURCE_VERSION', '1.15.9');
define('SOURCE_VERSION', '1.15.12');

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -80,6 +81,7 @@
// ]);

$app->routeMiddleware([
'blacklist' => App\Http\Middleware\Blacklist::class,
'meta' => App\Http\Middleware\Meta::class,
'throttle' => App\Http\Middleware\Throttle::class
]);
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'uses' => 'MetaLiteController@request'
]);

$router->group(['middleware' => ['meta', 'throttle']], function() use ($router) {
$router->group(['middleware' => ['blacklist', 'meta', 'throttle']], function() use ($router) {

$router->get('anime[/{id:[0-9]+}[/{extend:[A-Za-z_]+}[/{extendArgs}]]]', [
'uses' => 'AnimeController@request'
Expand All @@ -52,7 +52,7 @@
'uses' => 'CharacterController@request'
]);

$router->get('search[/{type}/{query}[/{page:[0-9]+}]]', [
$router->get('search[/{type}[/{query}[/{page:[0-9]+}]]]', [
'uses' => 'SearchController@request'
]);

Expand Down
2 changes: 2 additions & 0 deletions storage/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*
!.gitignore
sessions.json
blacklist.json

0 comments on commit cec8818

Please sign in to comment.