/** * Get the base URL, possibly with a controller script * @method baseUrl * @static * @param {boolean} [$with_possible_controller=false] If this is true, and if the URL contains * the controller script, then the controller script is included * in the return value. You can also pass a string here, which * will then be simply appended as the controller. */ static function baseUrl($with_possible_controller = false) { if (isset(self::$base_url)) { if (is_string($with_possible_controller)) { if (empty($with_possible_controller)) { return self::$app_root_url; } return self::$app_root_url . "/" . $with_possible_controller; } if ($with_possible_controller) { return self::$base_url; } return self::$app_root_url; } if (isset($_SERVER['SERVER_NAME'])) { // This is a web request, so we can automatically determine // the app root URL. If you want the canonical one which the developer // may have specified in the config field "Q"/"web"/"appRootUrl" // then just query it via Q_Config::get(). // Infer things self::$controller_url = self::inferControllerUrl($script_name); // Get the app root URL self::$app_root_url = self::getAppRootUrl(); // Automatically figure out whether to omit // the controller name from the url self::$controller_present = 0 == strncmp($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'], strlen($_SERVER['SCRIPT_NAME'])); // Special case for action.php controller // in case a misconfigured web server executes index.php even though // a url of the form $base_url/action.php/... was requested, // we will try to detect action.php anyway, and set it accordingly $slashpos = strrpos($script_name, '/'); $action_script_name = ($slashpos ? substr($script_name, 0, $slashpos) : '') . '/action.php'; if (0 == strncmp($_SERVER['REQUEST_URI'], $action_script_name, strlen($action_script_name))) { self::$controller_url = substr(self::$controller_url, 0, strrpos(self::$controller_url, '/')) . '/action.php'; self::$controller_present = true; Q::$controller = 'Q_ActionController'; } // Set the base url self::$base_url = self::$controller_present ? self::$controller_url : self::$app_root_url; } else { // This is not a web request, and we absolutely need // the canonical app root URL to have been specified. $ar = Q_Config::get('Q', 'web', 'appRootUrl', false); if (!$ar) { throw new Q_Exception_MissingConfig(array('fieldpath' => 'Q/web/appRootUrl')); } $cs = Q_Config::get('Q', 'web', 'controllerSuffix', ''); self::$app_root_url = $ar; self::$controller_url = $ar . $cs; self::$controller_present = false; self::$base_url = self::$app_root_url; } if (is_string($with_possible_controller)) { return self::$app_root_url . "/" . $with_possible_controller; } if ($with_possible_controller) { return self::$base_url; } return self::$app_root_url; }
/** * Excecute web request * @method execute * @static */ static function execute() { // Fixes for different platforms: if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // ISAPI 3.0 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL']; } // Get the base URL $base_url = Q_Request::baseUrl(); if (Q::$controller === 'Q_ActionController') { // we detected action.php in the URL, but // a misconfigured web server executed index.php instead return Q_ActionController::execute(); } // Set the controller that is being used if (!isset(Q::$controller)) { Q::$controller = 'Q_WebController'; } try { $slots = Q_Request::slotNames(false); $slots = $slots ? ' slots: (' . implode(',', $slots) . ') from' : ''; $method = Q_Request::method(); Q::log("{$method}{$slots} url: " . Q_Request::url(true), null, null, array('maxLength' => 10000)); Q_Dispatcher::dispatch(); $dispatchResult = Q_Dispatcher::result(); if (!isset($dispatchResult)) { $dispatchResult = 'Ran dispatcher'; } $uri = Q_Request::uri(); $module = $uri->module; $action = $uri->action; if ($module and $action) { $slotNames = Q_Request::slotNames(); $returned_slots = empty($slotNames) ? '' : implode(',', $slotNames); Q::log("~" . ceil(Q::milliseconds()) . 'ms+' . ceil(memory_get_peak_usage() / 1000) . 'kb.' . " {$dispatchResult} for {$module}/{$action}" . " ({$returned_slots})", null, null, array('maxLength' => 10000)); } else { Q::log("~" . ceil(Q::milliseconds()) . 'ms+' . ceil(memory_get_peak_usage() / 1000) . 'kb.' . " {$dispatchResult} No route for " . $_SERVER['REQUEST_URI'], null, null, array('maxLength' => 10000)); } } catch (Exception $exception) { /** * @event Q/exception * @param {Exception} exception */ Q::event('Q/exception', compact('exception')); } }
/** * The standard action front controller * @method execute * @static * @throws {Q_Exception_BadUrl} * @throws {Q_Exception} * @throws {Q_Exception_MissingConfig} */ static function execute($url = null) { // Fixes for different platforms: if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // ISAPI 3.0 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL']; } // Set the controller that is being used if (!isset(Q::$controller)) { Q::$controller = 'Q_ActionController'; } try { $slots = Q_Request::slotNames(false); $slots = $slots ? ' slots: (' . implode(',', $slots) . ') from' : ''; $method = Q_Request::method(); Q::log("{$method}{$slots} url: " . Q_Request::url(true)); $tail = Q_Request::tail($url); if (!isset($tail)) { // Bad url was requested somehow $url = Q_Request::url(true); $base_url = Q_Request::baseUrl(true); throw new Q_Exception_BadUrl(compact('base_url', 'url')); } $parts = explode('/', $tail); $parts_len = count($parts); if ($parts_len >= 1) { $module = $parts[0]; } if ($parts_len >= 2) { $action = $parts[1]; } if (empty($module) or empty($action)) { throw new Q_Exception("Not implemented"); } // Make sure the 'Q'/'web' config fields are set, // otherwise URLs will be formed pointing to the wrong // controller script. $ar = Q_Config::get('Q', 'web', 'appRootUrl', null); if (!isset($ar)) { throw new Q_Exception_MissingConfig(array('fieldpath' => 'Q/web/appRootUrl')); } // Dispatch the request $uri = Q_Uri::from(compact('module', 'action')); Q_Dispatcher::dispatch($uri); $dispatchResult = Q_Dispatcher::result(); if (!isset($dispatchResult)) { $dispatchResult = 'Ran dispatcher'; } if ($module and $action) { $slotNames = Q_Request::slotNames(); $requestedSlots = empty($slotNames) ? '' : implode(',', $slotNames); Q::log("~" . ceil(Q::milliseconds()) . 'ms+' . ceil(memory_get_peak_usage() / 1000) . 'kb.' . " {$dispatchResult} for {$module}/{$action}" . " ({$requestedSlots})"); } else { Q::log("~" . ceil(Q::milliseconds()) . 'ms+' . ceil(memory_get_peak_usage() / 1000) . 'kb.' . " No route for " . $_SERVER['REQUEST_URI']); } } catch (Exception $exception) { /** * @event Q/exception * @param {Exception} exception */ Q::event('Q/exception', compact('exception')); } }