/** * Initializes the hooks subsystem */ public static function init() { // verify if the loaded site provides a hooks-handler $hooks_class = Registry::get('namespace') . '\\Assets\\Hooks'; try { Registry::get('loader')->disable_bruteforce(Registry::get('site_src_path') . DS . 'Assets'); class_exists($hooks_class); // try initializing sites hooks-handler try { Registry::set('hooks', new $hooks_class()); } catch (Exception $e) { throw new Exception($e); } } catch (UberloaderException $e) { // site does not provide a hook-handler, just keep going Registry::set('hooks', null); } }
/** * Returns a response from a module. Determines module by the given path */ private static function get_response($rq) { // try to call the module list($request_params, $controller) = self::call_controller($rq); Registry::set('loaded_controller', $controller); // if the controller already responded with a Response-object, we can directly pass it up if (isset($controller->response) && $controller->response instanceof Response) { return $controller->response; } /** * if we have a second parameter besides the requested controller, * we try to call the appropriate method of the controller class. * If this does not work, we throw an exception. * If there is no second parameter, we just call the default_action() * of the controller. */ if (sizeof($request_params)) { // check the requests format if (!preg_match('#^[^\\d]\\w[\\d\\w]*$#', $request_params[0])) { // possible attack - issue 404 error throw new Error404NotFound("Invalid action name"); } /** * if the request-params exist of more than one parameters, and if the second of these * parameters is xss-safe., the dispatcher will first try to make a direct sub-action * call if the controller provides it * e.g. /users/new -> users_new_action */ if (isset($request_params[1]) && preg_match('#^[^\\d]\\w[\\d\\w]*$#', $request_params[1])) { $optional_requested_action = $request_params[0] . '_' . $request_params[1] . '_action'; if (method_exists($controller, $optional_requested_action)) { // action method found // the first two request parameters are not needed anymore */ array_shift($request_params); array_shift($request_params); Registry::set('loaded_action', $optional_requested_action); return $controller->{$optional_requested_action}($rq, $request_params); } } // apparently the direct sub-action call failed, proceed the ordinary way // build action-call $requested_action = $request_params[0] . '_action'; if (method_exists($controller, $requested_action)) { // action-method found, so it's name is not needed anymore array_shift($request_params); /** * let's call the action-method and hand-over the request-object * along with the remaining request parameters */ Registry::set('loaded_action', $requested_action); return $controller->{$requested_action}($rq, $request_params); } elseif (method_exists($controller, '__call')) { /** * We can still try to call the fallback method __call * of the requested controller, in order to handle this "invalid" * request (e.g. /users/3). If this helper method is available, * the respective controller has to decide itself what to do with the request */ // the "invalid" call still has "_action" appended - strip away $caught_request = preg_replace("/_action\$/", "", $requested_action); Registry::set('loaded_action', $requested_action); return $controller->__call($caught_request, array($rq, $request_params)); } else { /** * the requested action could not be found in our module, and * no __call-catcher is available -> 404 */ $class = get_class($controller); throw new Error404NotFound("Module '{$class}' does not provide the method ('{$requested_action}')"); } } else { // there's no second parameter, let's try to call the default_action() if (method_exists($controller, 'default_action')) { Registry::set('loaded_action', 'default_action'); return $controller->default_action($rq, $request_params); } else { /** * the default-action was not found in the appropriate module * don't know what to do */ $class = get_class($controller); throw new Error404NotFound("Controller '{$class}' does not provide a default_action"); } } }
public function __construct($debug = false) { define('DEBUG', $debug); /** * Initialize whoops to handle exceptions */ Registry::set('whoops', new Run()); if (DEBUG) { Registry::get('whoops')->pushHandler(new PrettyPageHandler()); } Registry::get('whoops')->pushHandler(new ExceptionHandler()); Registry::get('whoops')->register(); if (!defined('BACBOX_APP')) { throw new Exception("Please define the path to the app directory in your bootstrap by setting BACBOX_APP"); } /** * set some basic php configuration parameters * these are mainly used for new installations and * will be overriden later based on configuration tokens */ ini_set('display_errors', (bool) $debug); ini_set('error_reporting', E_ALL ^ E_STRICT); ini_set('max_execution_time', 30); // define paths define('DS', preg_match("/\\//", __DIR__) ? "/" : "\\"); define('BACBOX_LIB', __DIR__); define('BACBOX_SRC', BACBOX_APP . '../src' . DS); // set the bacbox urlbase, e.g. /bacbox/ $urlbase = dirname($_SERVER['SCRIPT_NAME']); $urlbase = preg_replace('#\\\\+#', '/', $urlbase); if (!preg_match('#/$#', $urlbase)) { $urlbase .= "/"; } define('BACBOX_URLBASE', $urlbase); // setup cache phpFastCache::setup('storage', 'files'); phpFastCache::setup('path', BACBOX_APP . 'cache'); phpFastCache::setup('securityKey', 'phpFastCache'); // initialize Uberloader $loader = new Uberloader(); $loader->set_cache_backend(new UberloaderCacheBackendFilesystem(BACBOX_APP . "cache" . DS)); $loader->add_path(BACBOX_LIB . DS . 'models'); $loader->add_path(BACBOX_LIB . DS . 'migrations'); $loader->add_path(BACBOX_SRC); $loader->register(); Registry::set('loader', $loader); // init basic configuration tokens to gain database-access Config::init(); // establish database link ORM::configure('mysql:host=' . Config::get('mysql.host') . ';dbname=' . Config::get('mysql.database')); ORM::configure('username', Config::get('mysql.user')); ORM::configure('password', Config::get('mysql.pass')); ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); ORM::configure('logging', true); // load remaining configuration tokens from database or cache Config::load(); // execute database migrations if .autoMigrate = true Config::get('migrations.autoMigrate') ? Migrator::run() : null; // initialize and register request helper Registry::set('Request', $request = new Request()); // run core controller to pre-process the user's request Controller::run(); // run site migrations if (Registry::get('site')->site_auto_migrate) { SiteMigrator::run(); } // initialize hooks subsystem Hooks::init(); Hooks::run('core.hooks.initialized'); // run config overrides in case the site specifies any Config::run_config_overrides(); // initialize and register session handler Registry::set('Session', new Session()); // initialize localization subsystem Registry::set('Localization', new Localization()); // run the user's request Hooks::run('core.response.before'); Registry::set('Response', $response = Dispatcher::run($request)); Hooks::run('core.response.after'); // send the response $response->send(); // that's it, folks exit; }