<commit_before><?php

class AppMiddleware extends \Slim\Middleware
{

	public static function decode_query_string() {
		$app = Slim\Slim::getInstance();

		// Parse incoming JSON QUERY_STRING
		// OBS: that's pretty much an uggly thing, but we need data types here.
		// Every param is string on query string (srsly?)
		$query_string = $app->environment->offsetGet('QUERY_STRING');
		$query_data = array();

		if (strlen($query_string)>0) {
			$query_data = array();
			// Parse JSON content on query string
			if (preg_match('/([^&]+)(&.*)?/', $query_string, $query)) {
				$query_data = json_decode(urldecode($query[1]), true) ?: array();
			}
			// Parse remaining regular string variables
			if (isset($query[2])) {
				parse_str($query[2], $additional_query_data);
				$query_data = array_merge($query_data, $additional_query_data);
			}
			$app->environment->offsetSet('slim.request.query_hash', $query_data);
		}

		return $query_data;
	}

	public function call()
	{
		// The Slim application
		$app = $this->app;

		$referer = parse_url($app->environment->offsetGet('HTTP_REFERER'));

		// Enable Cross-Origin Resource Sharing
		$app->response->headers->set('Access-Control-Allow-Origin', (isset($referer['host'])) ? 'http://'.$referer['host'] : '*' );
		$app->response->headers->set('Access-Control-Allow-Credentials', 'true');
		$app->response->headers->set('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
		$app->response->headers->set('Access-Control-Allow-Headers', 'x-app-id, x-app-key, x-auth-token, content-type, user-agent, accept');

		self::decode_query_string();

		// Don't proceed on CORS requests.
		if (!$app->request->isOptions()) {
			$app->key = models\AppKey::where('app_id', $app->request->headers->get('X-App-Id') ?: $app->request->get('X-App-Id'))
				->where('key', $app->request->headers->get('X-App-Key') ?: $app->request->get('X-App-Key'))
				->first();

			if ($app->key) {
				if ($custom_routes = models\Module::currentApp()->where('type', models\Module::TYPE_ROUTE)->get()) {
					foreach($custom_routes as $custom_route) {
						$custom_route->compile();
					}
				}
			} else if (preg_match('/$app/', $app->request->getResourceUri())) {
				if (!$this->validatePublicKey($app->request->headers->get('X-Public-Key'))) {
					// http_response_code(403);
					// die(json_encode(array('error' => "Public key not authorized.")));
					// throw new ForbiddenException("Invalid credentials.");
				}

			} else {
				// http_response_code(403);
				$app->response->setStatus(403);
				$app->response->setBody(json_encode(array('error' => "Invalid credentials.")));
				return;
			}

			//<commit_msg>fix checking for commandline requests<commit_after><?php

class AppMiddleware extends \Slim\Middleware
{

	public static function decode_query_string() {
		$app = Slim\Slim::getInstance();

		// Parse incoming JSON QUERY_STRING
		// OBS: that's pretty much an uggly thing, but we need data types here.
		// Every param is string on query string (srsly?)
		$query_string = $app->environment->offsetGet('QUERY_STRING');
		$query_data = array();

		if (strlen($query_string)>0) {
			$query_data = array();
			// Parse JSON content on query string
			if (preg_match('/([^&]+)(&.*)?/', $query_string, $query)) {
				$query_data = json_decode(urldecode($query[1]), true) ?: array();
			}
			// Parse remaining regular string variables
			if (isset($query[2])) {
				parse_str($query[2], $additional_query_data);
				$query_data = array_merge($query_data, $additional_query_data);
			}
			$app->environment->offsetSet('slim.request.query_hash', $query_data);
		}

		return $query_data;
	}

	public function call()
	{
		// The Slim application
		$app = $this->app;

		$referer = parse_url($app->environment->offsetGet('HTTP_REFERER'));

		// Enable Cross-Origin Resource Sharing
		$app->response->headers->set('Access-Control-Allow-Origin', (isset($referer['host'])) ? 'http://'.$referer['host'] : '*' );
		$app->response->headers->set('Access-Control-Allow-Credentials', 'true');
		$app->response->headers->set('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
		$app->response->headers->set('Access-Control-Allow-Headers', 'x-app-id, x-app-key, x-auth-token, content-type, user-agent, accept');

		self::decode_query_string();

		// Don't proceed on CORS requests.
		if (!$app->request->isOptions()) {
			$app->key = models\AppKey::where('app_id', $app->request->headers->get('X-App-Id') ?: $app->request->get('X-App-Id'))
				->where('key', $app->request->headers->get('X-App-Key') ?: $app->request->get('X-App-Key'))
				->first();

			if ($app->key) {
				if ($custom_routes = models\Module::currentApp()->where('type', models\Module::TYPE_ROUTE)->get()) {
					foreach($custom_routes as $custom_route) {
						$custom_route->compile();
					}
				}
			} else if (preg_match('/^\/app/', $app->request->getResourceUri())) {
				if (!$this->validatePublicKey($app->request->headers->get('X-Public-Key'))) {
					// http_response_code(403);
					// die(json_encode(array('error' => "Public key not authorized.")));
					// throw new ForbiddenException("Invalid credentials.");
				}

			} else {
				// http_response_code(403);
				$app->response->setStatus(403);
				$app->response->setBody(json_encode(array('error' => "Invalid credentials.")));
				return;
			}

			//
{'fraction_non_alphanumeric': 0.20217963171739947, 'fraction_numerical': 0.004885381435550545, 'mean_word_length': 4.123195380173244, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, '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': '3486533', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1762, 'n_words': 312}