<commit_before><?php
	namespace Bolt;

	class Api extends Base
	{
		public $request;
		public $response;
		public $auth;

		protected $files;
		protected $dbo;
		
		public $connections;

		public $route;

		private $permissions;
		private $whitelist;

		public function __construct($connections)
		{
			$this->files = new Files();
			$this->connections = new Api\Connections($connections);

			$this->response = new Api\Response();
			$this->request = new Api\Request();
			$this->auth = new Api\Authorization();

			if ($this->request->format != "json")
			{
				$this->response->setView($this->request->format);
			}

			$this->route = new Api\Route(true);
			$this->routing();

			if ($this->route->info->verb == "OPTIONS")
			{
				$this->handleOptions($this->route->controller);
			}

			$this->loadWhitelist();

			if ($this->checkWhitelist() === false)
			{
				$this->authenticate();

				// allow aliasing logged in user id as 'me'
				if ($this->route->info->id == "me")
				{
					global $_USERID;
					$this->route->info->id = $_USERID;
				}

				if ($this->auth->scheme() == "Global")
				{
					$this->enforcePermission($this->route->controller, $this->route->method);
				}
			}
		}

		public function enforcePermission($controller, $name)
		{
			$result = $this->checkPermission($controller, $name);

			if ($result === false)
			{
				$this->response->status(403);
			}

			return true;
		}

		public function checkPermission($controller, $name)
		{
			$result = false;

			$permissions = $this->permissions->system->$controller;
			$requiredPermission = constant("\\Controllers\\Permissions\\" . $controller . "::" . strtoupper($name));

			if (($permissions & $requiredPermission) === $requiredPermission)
			{
				$result = true;
			}

			return $result;
		}

		private function checkWhitelist()
		{
			if ($this->route->controller == "")
			{
				return true;
			}

			for ($loop = 0; $loop < count($this->whitelist); $loop++)
			{
				$rule = $this->whitelist[$loop];

				if ($rule->controller == $this->route->controller)
				{
					if (!isset($rule->methods))
					{
						return true;
					}

					foreach ($rule->methods as $method)
					{
						if ($method == $this->route->method)
						{
							return true;
						}
					}
				}
			}

			return false;
		}

		private function loadWhitelist()
		{
			$this->whitelist = $this->loadJsonConfig(ROOT_SERVER . "/library/whitelist.json");
		}

		private function loadJsonConfig($filename)
		{
			return json_decode($this->files->load($filename));
		}

		public function fetchAvailableOptions()
		{
			$possibleMethods = array("GET", "POST", "PUT", "DELETE", "HEAD", "PATCH");
			$available = array();

			$methodTail = str_replace(strtolower($this->route->info->verb), "", $this->route->method);

			foreach ($possibleMethods as $next)
			{<commit_msg>Remove unrequired properties from Api Class<commit_after><?php
	namespace Bolt;

	class Api extends Base
	{
		public $request;
		public $response;
		public $auth;

		public $connections;

		public $route;

		private $permissions;
		private $whitelist;

		public function __construct($connections)
		{
			$this->connections = new Api\Connections($connections);

			$this->response = new Api\Response();
			$this->request = new Api\Request();
			$this->auth = new Api\Authorization();

			if ($this->request->format != "json")
			{
				$this->response->setView($this->request->format);
			}

			$this->route = new Api\Route(true);
			$this->routing();

			if ($this->route->info->verb == "OPTIONS")
			{
				$this->handleOptions($this->route->controller);
			}

			$this->loadWhitelist();

			if ($this->checkWhitelist() === false)
			{
				$this->authenticate();

				// allow aliasing logged in user id as 'me'
				if ($this->route->info->id == "me")
				{
					global $_USERID;
					$this->route->info->id = $_USERID;
				}

				if ($this->auth->scheme() == "Global")
				{
					$this->enforcePermission($this->route->controller, $this->route->method);
				}
			}
		}

		public function enforcePermission($controller, $name)
		{
			$result = $this->checkPermission($controller, $name);

			if ($result === false)
			{
				$this->response->status(403);
			}

			return true;
		}

		public function checkPermission($controller, $name)
		{
			$result = false;

			$permissions = $this->permissions->system->$controller;
			$requiredPermission = constant("\\Controllers\\Permissions\\" . $controller . "::" . strtoupper($name));

			if (($permissions & $requiredPermission) === $requiredPermission)
			{
				$result = true;
			}

			return $result;
		}

		private function checkWhitelist()
		{
			if ($this->route->controller == "")
			{
				return true;
			}

			for ($loop = 0; $loop < count($this->whitelist); $loop++)
			{
				$rule = $this->whitelist[$loop];

				if ($rule->controller == $this->route->controller)
				{
					if (!isset($rule->methods))
					{
						return true;
					}

					foreach ($rule->methods as $method)
					{
						if ($method == $this->route->method)
						{
							return true;
						}
					}
				}
			}

			return false;
		}

		private function loadWhitelist()
		{
			$this->whitelist = $this->loadJsonConfig(ROOT_SERVER . "/library/whitelist.json");
		}

		private function loadJsonConfig($filename)
		{
			$fileHandler = new Files();
			return json_decode($fileHandler->load($filename));
		}

		public function fetchAvailableOptions()
		{
			$possibleMethods = array("GET", "POST", "PUT", "DELETE", "HEAD", "PATCH");
			$available = array();

			$methodTail = str_replace(strtolower($this->route->info->verb), "", $this->route->method);

			foreach ($possibleMethods as $next)
			{
{'fraction_non_alphanumeric': 0.178337174259883, 'fraction_numerical': 0.0014181882644921113, 'mean_word_length': 3.0386542591267003, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 123, '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': '3359180', 'n_tokens_mistral': 2303, 'n_tokens_neox': 1784, 'n_words': 353}