<commit_before><?php

namespace Seat\EveApi;

use Seat\EveApi\Exception;
use Pheal\Pheal;
use Pheal\Core\Config as PhealConfig;
use Carbon\Carbon;

/*
|--------------------------------------------------------------------------
| BaseAPI
|--------------------------------------------------------------------------
|
| The base EVE API Class, with some helper functions.
| All API calls extend this class.
|
*/

class BaseApi {

	/*
	|--------------------------------------------------------------------------
	| bootstrap()
	|--------------------------------------------------------------------------
	|
	| Configure Pheal\Pheal
	|
	*/

	public static function bootstrap()
	{

		// Configure Pheal
		PhealConfig::getInstance()->cache = new \Pheal\Cache\FileStorage( storage_path(). '/cache/phealcache/' );
		PhealConfig::getInstance()->access = new \Pheal\Access\StaticCheck();
		PhealConfig::getInstance()->log = new \Pheal\Log\FileStorage( storage_path() . '/logs/' );
		PhealConfig::getInstance()->http_user_agent = 'SeAT ' . \Config::get('seat.version') . 'API Fetcher';
		PhealConfig::getInstance()->api_customkeys = true;
		PhealConfig::getInstance()->http_method = 'curl';

		// Should the EVE api be determined as 'down', set the cache value for 'eve_api_down'
		// with a expiration of 30 minutes. We will also decrement the current error count
		// by 10 to allow for some calls to happen after the value has expired out of
		// cache
		if (\Cache::get('eve_api_error_count') >= \Config::get('seat.error_limit')) {

			\Cache::set('eve_api_down', true, 30);
			\Cache::decrement('eve_api_error_count', 10);
		}

		// Check if the EVE Api has been detected as 'down'.
		if (\Cache::has('eve_api_down'))
			throw new Exception\APIServerDown;

		// Disable the Laravel query log. Some of the API calls do.. a lot of queries :P
		\DB::connection()->disableQueryLog();
	}

	/*
	|--------------------------------------------------------------------------
	| validateKeyPair()
	|--------------------------------------------------------------------------
	|
	| Check that a give keyID & vCode is valid
	|
	*/

	public static function validateKeyPair($keyID, $vCode)
	{
		if (!is_numeric($keyID))
			throw new \Exception('A API keyID must be a integer, we got: ' . $keyID);

		if (strlen($vCode) <> 64)
			throw new \Exception('A vCode should be 64 chars long, we got: ' . $vCode);
	}

	/*
	|--------------------------------------------------------------------------
	| makeCallHash()
	|--------------------------------------------------------------------------
	|
	| Generate a MD5 hash based on the 3 received arguements for caching related
	| information in the database.
	|
	*/

	public static function makeCallHash($api, $scope, $owner)
	{
		return md5(implode(',', array($api, $scope, $owner) ));
	}

	/*
	|--------------------------------------------------------------------------
	| disableKey()
	|--------------------------------------------------------------------------
	|
	| Sets the `isOk` field in the `seat_keys` table to 0, effectively marking the
	| key as inactive.
	|
	*/

	public static function disableKey($keyID, $error = null)
	{
		$key = \SeatKey::where('keyID', '=', $keyID)->first();

		if (!$key)
			throw new Exception('Unable to find the entry in `seat_keys` to disable key: ' . $keyID);

		$key->isOk = 0;
		$key->lastError = $error;
		$key->save();
	}

	/*
	|--------------------------------------------------------------------------
	| banCall()
	|--------------------------------------------------------------------------
	|
	| Records a API call as banned together with the access mask.
	|
	*/

	public static function banCall($api, $scope, $owner = 0, $accessMask = 0, $reason = null)
	{

		\Log::warning('Processing a ban request for api: ' . $api . ' scope: ' . $scope . ' owner: ' . $owner, array('src' => __CLASS__));

		// Check if we should retreive the current access mask
		if ($accessMask == 0)
			$accessMask = \EveAccountAPIKeyInfo::where('keyID', '=', $owner)->pluck('accessMask');

		// Generate a hash with which to ID this call
		$hash = BaseApi::makeCallHash($api, $scope, $owner . $accessMask);

		// Check the cache if a ban has been recorded
		if (!\Cache::has('call_ban_grace_count_' . $hash)) {

			// Record the new ban, getting the grance period from the seat config and return
			\Cache::put('call_ban_grace_count_' . $hash, 0, \Config::get('seat.ban_grace'));
			return;

		} else {

			// Check if we have reached the limit for the allowed bad calls from the config
			if (\Cache::get('call_ban_grace_count_' . $hash) < \Config::get('seat.ban_limit') - 1) {

				// Add another one to the amount of failed calls and return
				\Cache::increment('call_ban_grace_count_' . $hash);
				return;
			}
		}

		\Log::warning('Ban limit reached. Actioning ban for api: ' . $api . ' scope: ' . $scope . ' owner: ' . $owner, array('src' => __CLASS__));

		// We _should_ only get here once the ban limit has been reached
		$banned = \EveBannedCall::where('hash', '=', $hash)->first();
		if (!$banned)
			$banned = new \EveBannedCall;

		$banned->ownerID = $owner;
		$banned->api = $api;
		$banned->scope = $scope;
		$banned->accessMask = $accessMask;
		$banned->hash = $hash;
		$banned->reason = $reason;
		$banned->save();

		// We also need to keep in mind how many errors have occured so far.
		// This is mainly for the checks that are done in bootstrap()
		// allowing us to pause and not cause a IP to be banned.
		if (\Cache::has('eve_api_error_count'))
			\Cache::increment('eve_api_error_count');
		else
			\Cache::put('eve_api_error_count', 1);
	}

	/*
	|--------------------------------------------------------------------------
	| isBannedCall()
	|--------------------------------------------------------------------------
	|
	| Checks if a API call is banned
	|
	*/

	public static function isBannedCall($api, $scope, $owner = 0, $accessMask = 0)
	{

		// Check if we should retreive the current access mask
		if ($accessMask == 0)
			$accessMask = \EveAccountAPIKeyInfo::where('keyID', '=', $owner)->pluck('accessMask');

		$hash = BaseApi::makeCallHash($api, $scope, $owner . $accessMask);
		$banned = \EveBannedCall::where('hash', '=', $hash)->first();

		if ($banned)
			return true;
		else
			return false;
	}
<commit_msg>Add missing argument for new cache value<commit_after><?php

namespace Seat\EveApi;

use Seat\EveApi\Exception;
use Pheal\Pheal;
use Pheal\Core\Config as PhealConfig;
use Carbon\Carbon;

/*
|--------------------------------------------------------------------------
| BaseAPI
|--------------------------------------------------------------------------
|
| The base EVE API Class, with some helper functions.
| All API calls extend this class.
|
*/

class BaseApi {

	/*
	|--------------------------------------------------------------------------
	| bootstrap()
	|--------------------------------------------------------------------------
	|
	| Configure Pheal\Pheal
	|
	*/

	public static function bootstrap()
	{

		// Configure Pheal
		PhealConfig::getInstance()->cache = new \Pheal\Cache\FileStorage( storage_path(). '/cache/phealcache/' );
		PhealConfig::getInstance()->access = new \Pheal\Access\StaticCheck();
		PhealConfig::getInstance()->log = new \Pheal\Log\FileStorage( storage_path() . '/logs/' );
		PhealConfig::getInstance()->http_user_agent = 'SeAT ' . \Config::get('seat.version') . 'API Fetcher';
		PhealConfig::getInstance()->api_customkeys = true;
		PhealConfig::getInstance()->http_method = 'curl';

		// Should the EVE api be determined as 'down', set the cache value for 'eve_api_down'
		// with a expiration of 30 minutes. We will also decrement the current error count
		// by 10 to allow for some calls to happen after the value has expired out of
		// cache
		if (\Cache::get('eve_api_error_count') >= \Config::get('seat.error_limit')) {

			\Cache::set('eve_api_down', true, 30);
			\Cache::decrement('eve_api_error_count', 10);
		}

		// Check if the EVE Api has been detected as 'down'.
		if (\Cache::has('eve_api_down'))
			throw new Exception\APIServerDown;

		// Disable the Laravel query log. Some of the API calls do.. a lot of queries :P
		\DB::connection()->disableQueryLog();
	}

	/*
	|--------------------------------------------------------------------------
	| validateKeyPair()
	|--------------------------------------------------------------------------
	|
	| Check that a give keyID & vCode is valid
	|
	*/

	public static function validateKeyPair($keyID, $vCode)
	{
		if (!is_numeric($keyID))
			throw new \Exception('A API keyID must be a integer, we got: ' . $keyID);

		if (strlen($vCode) <> 64)
			throw new \Exception('A vCode should be 64 chars long, we got: ' . $vCode);
	}

	/*
	|--------------------------------------------------------------------------
	| makeCallHash()
	|--------------------------------------------------------------------------
	|
	| Generate a MD5 hash based on the 3 received arguements for caching related
	| information in the database.
	|
	*/

	public static function makeCallHash($api, $scope, $owner)
	{
		return md5(implode(',', array($api, $scope, $owner) ));
	}

	/*
	|--------------------------------------------------------------------------
	| disableKey()
	|--------------------------------------------------------------------------
	|
	| Sets the `isOk` field in the `seat_keys` table to 0, effectively marking the
	| key as inactive.
	|
	*/

	public static function disableKey($keyID, $error = null)
	{
		$key = \SeatKey::where('keyID', '=', $keyID)->first();

		if (!$key)
			throw new Exception('Unable to find the entry in `seat_keys` to disable key: ' . $keyID);

		$key->isOk = 0;
		$key->lastError = $error;
		$key->save();
	}

	/*
	|--------------------------------------------------------------------------
	| banCall()
	|--------------------------------------------------------------------------
	|
	| Records a API call as banned together with the access mask.
	|
	*/

	public static function banCall($api, $scope, $owner = 0, $accessMask = 0, $reason = null)
	{

		\Log::warning('Processing a ban request for api: ' . $api . ' scope: ' . $scope . ' owner: ' . $owner, array('src' => __CLASS__));

		// Check if we should retreive the current access mask
		if ($accessMask == 0)
			$accessMask = \EveAccountAPIKeyInfo::where('keyID', '=', $owner)->pluck('accessMask');

		// Generate a hash with which to ID this call
		$hash = BaseApi::makeCallHash($api, $scope, $owner . $accessMask);

		// Check the cache if a ban has been recorded
		if (!\Cache::has('call_ban_grace_count_' . $hash)) {

			// Record the new ban, getting the grance period from the seat config and return
			\Cache::put('call_ban_grace_count_' . $hash, 0, \Config::get('seat.ban_grace'));
			return;

		} else {

			// Check if we have reached the limit for the allowed bad calls from the config
			if (\Cache::get('call_ban_grace_count_' . $hash) < \Config::get('seat.ban_limit') - 1) {

				// Add another one to the amount of failed calls and return
				\Cache::increment('call_ban_grace_count_' . $hash);
				return;
			}
		}

		\Log::warning('Ban limit reached. Actioning ban for api: ' . $api . ' scope: ' . $scope . ' owner: ' . $owner, array('src' => __CLASS__));

		// We _should_ only get here once the ban limit has been reached
		$banned = \EveBannedCall::where('hash', '=', $hash)->first();
		if (!$banned)
			$banned = new \EveBannedCall;

		$banned->ownerID = $owner;
		$banned->api = $api;
		$banned->scope = $scope;
		$banned->accessMask = $accessMask;
		$banned->hash = $hash;
		$banned->reason = $reason;
		$banned->save();

		// We also need to keep in mind how many errors have occured so far.
		// This is mainly for the checks that are done in bootstrap()
		// allowing us to pause and not cause a IP to be banned.
		if (\Cache::has('eve_api_error_count'))
			\Cache::increment('eve_api_error_count');
		else
			\Cache::put('eve_api_error_count', 1, 30);
	}

	/*
	|--------------------------------------------------------------------------
	| isBannedCall()
	|--------------------------------------------------------------------------
	|
	| Checks if a API call is banned
	|
	*/

	public static function isBannedCall($api, $scope, $owner = 0, $accessMask = 0)
	{

		// Check if we should retreive the current access mask
		if ($accessMask == 0)
			$accessMask = \EveAccountAPIKeyInfo::where('keyID', '=', $owner)->pluck('accessMask');

		$hash = BaseApi::makeCallHash($api, $scope, $owner . $accessMask);
		$banned = \EveBannedCall::where('hash', '=', $hash)->first();

		if ($banned)
			return true;
		else
			return false;
	}

{'fraction_non_alphanumeric': 0.2939497264293077, 'fraction_numerical': 0.0042819760526524465, 'mean_word_length': 1.8637602179836512, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3704718', 'n_tokens_mistral': 4037, 'n_tokens_neox': 3648, 'n_words': 1073}