<commit_before><?php defined('C5_EXECUTE') or die('Access Denied');

class ApiRouter {

	public $requestedPath;

	public $requestedRoute;

	public static function get() {
		static $req;
		if (!isset($req)) {
			$req = new ApiRouter();
			$req->parseRequest();
		}
		return $req;
	}

	public function parseRequest() {

		$pk = Package::getByHandle(C5_API_HANDLE);
		if(!$pk->config('ENABLED')) {
			return; //if we arn't enabled, kill it. should we render an api response instead?
		}
		$req = Request::get();
		$path = $req->getRequestPath();

		$path = trim($path, '/');

		$basepath = trim(BASE_API_PATH, '/');

		if (substr($path, 0, strlen($basepath)) == $basepath) {
			$dirrel = strlen(DIR_REL.'/'.DISPATCHER_FILENAME);
			if(substr($_SERVER['REQUEST_URI'], 0, $dirrel) == DIR_REL.'/'.DISPATCHER_FILENAME) { //pretty url hack
				$path = DIR_REL.'/'.DISPATCHER_FILENAME.'/'.BASE_API_PATH;
			} else {
				$path = DIR_REL.'/'.BASE_API_PATH;
			}
			//This is a path like /derp/thing/ha?params=1
			$this->requestedPath =  trim(str_replace($path, '', $_SERVER['REQUEST_URI']), '/');

			$request = $this->requestedPath;
			if(($pos = strpos($request, '?')) !== false) {
            	$request =  trim(substr($request, 0, $pos), '/');
        	}
        	$this->requestedRoute = $request;
			$this->dispatch();
		}
	}

	public function dispatch() {
		$txt = Loader::helper('text');
		$error = false;
		$route = ApiRouteList::getRouteByPath($this->requestedRoute);
		if(is_object($route) && $route->ID && !$route->internal) { //valid route
			$class = $txt->camelcase($route->route).'ApiRouteController';
			try {
				$env = Environment::get();
				$pkg = Package::getByID($route->pkgID);
				$path = $env->getPath(C5_API_DIRNAME_ROUTES.'/'.$route->file, $pkg);
				require_once($path);
				if(class_exists($class)) {
					$cl = new $class;
					$data = $cl->setupAndRun();
					$cl->respond($data);
				}
			} catch(Exception $e) {
				$error = 500;
			}

		} else {
			$error = 400;
		}

		if($error) {
			switch($error) {
				case 500:
					$route = ApiRouteList::getRouteByPath('server_error');
					$class = $txt->camelcase($route->route).'ApiRouteController';
					$cl = new $class;
					$cl->setupAndRun();
					break;
				case 400:
					$route = ApiRouteList::getRouteByPath('bad_request');
					$class = $txt->camelcase($route->route).'ApiRouteController';
					$cl = new $class;
					$cl->setupAndRun();
					break;<commit_msg>make easy request method<commit_after><?php defined('C5_EXECUTE') or die('Access Denied');

class ApiRouter {

	public $requestedPath;

	public $requestedRoute;

	public static function get() {
		static $req;
		if (!isset($req)) {
			$req = new ApiRouter();
			$req->parseRequest();
		}
		return $req;
	}

	public function parseRequest() {

		$pk = Package::getByHandle(C5_API_HANDLE);
		if(!$pk->config('ENABLED')) {
			return; //if we arn't enabled, kill it. should we render an api response instead?
		}

		if(!defined('API_REQUEST_METHOD')) {
			define('API_REQUEST_METHOD', $_SERVER['REQUEST_METHOD']);
		} else {
			$_SERVER['REQUEST_METHOD'] = API_REQUEST_METHOD;
		}
		
		$req = Request::get();
		$path = $req->getRequestPath();

		$path = trim($path, '/');

		$basepath = trim(BASE_API_PATH, '/');

		if (substr($path, 0, strlen($basepath)) == $basepath) {
			$dirrel = strlen(DIR_REL.'/'.DISPATCHER_FILENAME);
			if(substr($_SERVER['REQUEST_URI'], 0, $dirrel) == DIR_REL.'/'.DISPATCHER_FILENAME) { //pretty url hack
				$path = DIR_REL.'/'.DISPATCHER_FILENAME.'/'.BASE_API_PATH;
			} else {
				$path = DIR_REL.'/'.BASE_API_PATH;
			}
			//This is a path like /derp/thing/ha?params=1
			$this->requestedPath =  trim(str_replace($path, '', $_SERVER['REQUEST_URI']), '/');

			$request = $this->requestedPath;
			if(($pos = strpos($request, '?')) !== false) {
            	$request =  trim(substr($request, 0, $pos), '/');
        	}
        	$this->requestedRoute = $request;
			$this->dispatch();
		}
	}

	public function dispatch() {
		$txt = Loader::helper('text');
		$error = false;
		$route = ApiRouteList::getRouteByPath($this->requestedRoute);
		if(is_object($route) && $route->ID && !$route->internal) { //valid route
			$class = $txt->camelcase($route->route).'ApiRouteController';
			try {
				$env = Environment::get();
				$pkg = Package::getByID($route->pkgID);
				$path = $env->getPath(C5_API_DIRNAME_ROUTES.'/'.$route->file, $pkg);
				require_once($path);
				if(class_exists($class)) {
					$cl = new $class;
					$data = $cl->setupAndRun();
					$cl->respond($data);
				}
			} catch(Exception $e) {
				$error = 500;
			}

		} else {
			$error = 400;
		}

		if($error) {
			switch($error) {
				case 500:
					$route = ApiRouteList::getRouteByPath('server_error');
					$class = $txt->camelcase($route->route).'ApiRouteController';
					$cl = new $class;
					$cl->setupAndRun();
					break;
				default:
					$route = ApiRouteList::getRouteByPath('bad_request');
					$class = $txt->camelcase($route->route).'ApiRouteController';
					$cl = new $class;
					$cl->setupAndRun();
					break;
{'fraction_non_alphanumeric': 0.19517500494364248, 'fraction_numerical': 0.006921099466086613, 'mean_word_length': 3.532258064516129, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '2447018', 'n_tokens_mistral': 2135, 'n_tokens_neox': 1797, 'n_words': 319}