<commit_before><?php
/**
 * Module management class
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

namespace Skeleton\Core\Web;

use Skeleton\Core\Application;

abstract class Module {

	/**
	 * Login required
	 *
	 * @var $login_required
	 */
	protected $login_required = true;

	/**
	 * Template
	 *
	 * @var $template
	 */
	protected $template = null;

	/**
	 * Accept the request
	 *
	 * @access public
	 */
	public function accept_request() {
		/**
		 * Cleanup sticky session
		 */
		\Skeleton\Core\Web\Session\Sticky::cleanup();
		$sticky = \Skeleton\Core\Web\Session\Sticky::Get();

		// Bootstrap the application
		$application = \Skeleton\Core\Application::get();
		$application->call_event_if_exists('application', 'bootstrap', [ $this ]);

		// If we have the skeleton-template package installed, find the template and set it up
		if (class_exists('\Skeleton\Template\Template')) {
			$template = \Skeleton\Core\Web\Template::Get();
			$template->add_environment('module', $this);
			$template->add_environment('sticky_session', $sticky->get_as_array());
		}

		// Call our magic secure() method before passing on the request
		$allowed = true;
		if (method_exists($this, 'secure')) {
			$allowed = $this->secure();
		}

		// If the request is not allowed, make sure it gets handled properly
		if ($allowed === false) {
			$module_403 = strtolower(\Skeleton\Core\Config::$module_403);

			// Always check if it can not be handled by an event first
			if ($application->event_exists('module', 'access_denied')) {
				$application->call_event_if_exists('module', 'access_denied', [ $this ]);
			} elseif ($module_403 !== null and file_exists($application->module_path . '/' . $module_403 . '.php')) {
				require $application->module_path . '/' . $module_403 . '.php';
				$classname = 'Web_Module_' . $module_403;
				$module = new $classname;
				$module->accept_request();
			} else {
				throw new \Exception('Access denied');
			}
		} else {
			$this->handle_request();
		}

		// Call the teardown event if it exists
		$application->call_event_if_exists('application', 'teardown', [ $this ]);
	}

	/**
	 * get module_path
	 *
	 * @access public
	 * @return string $path
	 */
	public function get_module_path() {
		$reflection = new \ReflectionClass($this);
		$application = Application::Get();
		$path = '/' . str_replace($application->module_path, '', $reflection->getFileName());
		$path = str_replace('.php', '', $path);
		return $path;
	}

	/**
	 * Handle the request
	 *
	 * @access public
	 */
	public function handle_request() {
		// Find out which method to call, fall back to calling displa()
		if (isset($_REQUEST['action']) AND method_exists($this, 'display_' . $_REQUEST['action'])) {
			if (class_exists('\Skeleton\Template\Template')) {
				$template = \Skeleton\Core\Web\Template::Get();
				$template->assign('action', $_REQUEST['action']);
			}

			call_user_func([$this, 'display_'.$_REQUEST['action']]);
		} else {
			$this->display();
		}

		// If the module has defined a template, render it
		if ($this->template !== null and $this->template !== false) {
			$template = \Skeleton\Core\Web\Template::Get();
			$template->display($this->template);
		}
	}

	/**
	 * Is login required?
	 *
	 * @access public
	 */
	public function is_login_required() {
		return $this->login_required;
	}

	/**
	 * Get the classname of the current module
	 *
	 * @access public
	 */
	public function get_name() {
		if (strpos(get_class($this), 'Web_Module_') !== false) {
			return strtolower(substr(get_class($this),strlen('Web_Module_')));
		}

		return strtolower(get_class($this));
	}

	/**
	 * Display the function
	 *
	 * @access public
	 */
	public abstract function display();

	/**
	 * Get the requested module
	 *
	 * @param string Module name
	 * @access public
	 * @return Web_Module Requested module
	 * @throws Exception
	 */
	public static function get($request_relative_uri) {
		$application = \Skeleton\Core\Application::get();
		$relative_uri_parts = array_values(array_filter(explode('/', $request_relative_uri)));

		$autoloader = new \Skeleton\Core\Autoloader();
		$autoloader->add_include_path($application->module_path, 'Web_Module_');
		$autoloader->register();

		$classnames = [];
		$classnames[] = trim('Web_Module_' . implode('_', $relative_uri_parts), '_');
		$classnames[] = trim('Web_Module_' . implode('_', $relative_uri_parts), '_') . '_' . $application->config->module_default;

		try {
			$classnames[] = 'Web_Module_' . $application->config->module_404;
		} catch (\Exception $e) { }

		foreach ($classnames as $classname) {
			if (class_exists($classname)) {
				return new $classname;
			}
		}
		throw new \Exception('Module not found');
	}

	/**
	 * Check if a module exists
	 *
	 * @param string Name of the module
	 * @access public
	 * @return bool
	 */
	public static function exists($name) {
		if (Config::$application_dir === null) {
			throw new \Exception('No application_dir set. Please set Config::$application_dir');
		}

		return (file_exists(Config::$application_dir . '/module/' . $name . '.php'));
	}
}<commit_msg>Minor code cleanup<commit_after><?php
/**
 * Module management class
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */

namespace Skeleton\Core\Web;

use Skeleton\Core\Application;

abstract class Module {

	/**
	 * Login required
	 *
	 * @var $login_required
	 */
	protected $login_required = true;

	/**
	 * Template
	 *
	 * @var $template
	 */
	protected $template = null;

	/**
	 * Accept the request
	 *
	 * @access public
	 */
	public function accept_request() {
		/**
		 * Cleanup sticky session
		 */
		\Skeleton\Core\Web\Session\Sticky::cleanup();
		$sticky = \Skeleton\Core\Web\Session\Sticky::Get();

		// Bootstrap the application
		$application = \Skeleton\Core\Application::get();
		$application->call_event_if_exists('application', 'bootstrap', [ $this ]);

		// If we have the skeleton-template package installed, find the template and set it up
		if (class_exists('\Skeleton\Template\Template')) {
			$template = \Skeleton\Core\Web\Template::Get();
			$template->add_environment('module', $this);
			$template->add_environment('sticky_session', $sticky->get_as_array());
		}

		// Call our magic secure() method before passing on the request
		$allowed = true;
		if (method_exists($this, 'secure')) {
			$allowed = $this->secure();
		}

		// If the request is not allowed, make sure it gets handled properly
		if ($allowed === false) {
			$module_403 = strtolower(\Skeleton\Core\Config::$module_403);

			// Always check if it can not be handled by an event first
			if ($application->event_exists('module', 'access_denied')) {
				$application->call_event_if_exists('module', 'access_denied', [ $this ]);
			} elseif ($module_403 !== null and file_exists($application->module_path . '/' . $module_403 . '.php')) {
				require $application->module_path . '/' . $module_403 . '.php';
				$classname = 'Web_Module_' . $module_403;
				$module = new $classname;
				$module->accept_request();
			} else {
				throw new \Exception('Access denied');
			}
		} else {
			$this->handle_request();
		}

		// Call the teardown event if it exists
		$application->call_event_if_exists('application', 'teardown', [ $this ]);
	}

	/**
	 * get module_path
	 *
	 * @access public
	 * @return string $path
	 */
	public function get_module_path() {
		$reflection = new \ReflectionClass($this);
		$application = Application::Get();
		$path = '/' . str_replace($application->module_path, '', $reflection->getFileName());
		$path = str_replace('.php', '', $path);
		return $path;
	}

	/**
	 * Handle the request
	 *
	 * @access public
	 */
	public function handle_request() {
		// Find out which method to call, fall back to calling displa()
		if (isset($_REQUEST['action']) AND method_exists($this, 'display_' . $_REQUEST['action'])) {
			if (class_exists('\Skeleton\Template\Template')) {
				$template = \Skeleton\Core\Web\Template::Get();
				$template->assign('action', $_REQUEST['action']);
			}

			call_user_func([$this, 'display_'.$_REQUEST['action']]);
		} else {
			$this->display();
		}

		// If the module has defined a template, render it
		if ($this->template !== null and $this->template !== false) {
			$template = \Skeleton\Core\Web\Template::Get();
			$template->display($this->template);
		}
	}

	/**
	 * Is login required?
	 *
	 * @access public
	 */
	public function is_login_required() {
		return $this->login_required;
	}

	/**
	 * Get the classname of the current module
	 *
	 * @access public
	 */
	public function get_name() {
		if (strpos(get_class($this), 'Web_Module_') !== false) {
			return strtolower(substr(get_class($this), strlen('Web_Module_')));
		}

		return strtolower(get_class($this));
	}

	/**
	 * Display the function
	 *
	 * @access public
	 */
	public abstract function display();

	/**
	 * Get the requested module
	 *
	 * @param string Module name
	 * @access public
	 * @return Web_Module Requested module
	 * @throws Exception
	 */
	public static function get($request_relative_uri) {
		$application = \Skeleton\Core\Application::get();
		$relative_uri_parts = array_values(array_filter(explode('/', $request_relative_uri)));

		$autoloader = new \Skeleton\Core\Autoloader();
		$autoloader->add_include_path($application->module_path, 'Web_Module_');
		$autoloader->register();

		$classnames = [];
		$classnames[] = trim('Web_Module_' . implode('_', $relative_uri_parts), '_');
		$classnames[] = trim('Web_Module_' . implode('_', $relative_uri_parts), '_') . '_' . $application->config->module_default;

		try {
			$classnames[] = 'Web_Module_' . $application->config->module_404;
		} catch (\Exception $e) { }

		foreach ($classnames as $classname) {
			if (class_exists($classname)) {
				return new $classname;
			}
		}

		throw new \Exception('Module not found');
	}

	/**
	 * Check if a module exists
	 *
	 * @param string Name of the module
	 * @access public
	 * @return bool
	 */
	public static function exists($name) {
		if (Config::$application_dir === null) {
			throw new \Exception('No application_dir set. Please set Config::$application_dir');
		}

		return (file_exists(Config::$application_dir . '/module/' . $name . '.php'));
	}
}
{'fraction_non_alphanumeric': 0.16612792633950435, 'fraction_numerical': 0.004114017043784896, 'mean_word_length': 3.786685419596812, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 71, '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': '1342354', 'n_tokens_mistral': 3611, 'n_tokens_neox': 3173, 'n_words': 749}