/** * Analyzes the supplied query string and decides how to dispatch the request. */ public function dispatch($ImportRequest = null, $Permanent = true) { if ($ImportRequest && is_string($ImportRequest)) { $ImportRequest = Gdn_Request::create()->fromEnvironment()->withURI($ImportRequest); } if (is_a($ImportRequest, 'Gdn_Request') && $Permanent) { Gdn::request($ImportRequest); } $Request = is_a($ImportRequest, 'Gdn_Request') ? $ImportRequest : Gdn::request(); $this->EventArguments['Request'] =& $Request; // Move this up to allow pre-routing $this->fireEvent('BeforeDispatch'); // By default, all requests can be blocked by UpdateMode/PrivateCommunity $CanBlock = self::BLOCK_ANY; try { $BlockExceptions = array('/^utility(\\/.*)?$/' => self::BLOCK_NEVER, '/^asset(\\/.*)?$/' => self::BLOCK_NEVER, '/^home\\/error(\\/.*)?/' => self::BLOCK_NEVER, '/^plugin(\\/.*)?$/' => self::BLOCK_NEVER, '/^sso(\\/.*)?$/' => self::BLOCK_NEVER, '/^discussions\\/getcommentcounts/' => self::BLOCK_NEVER, '/^entry(\\/.*)?$/' => self::BLOCK_PERMISSION, '/^user\\/usernameavailable(\\/.*)?$/' => self::BLOCK_PERMISSION, '/^user\\/emailavailable(\\/.*)?$/' => self::BLOCK_PERMISSION, '/^home\\/termsofservice(\\/.*)?$/' => self::BLOCK_PERMISSION); $this->EventArguments['BlockExceptions'] =& $BlockExceptions; $this->fireEvent('BeforeBlockDetect'); $PathRequest = Gdn::request()->path(); foreach ($BlockExceptions as $BlockException => $BlockLevel) { if (preg_match($BlockException, $PathRequest)) { throw new Exception("Block detected - {$BlockException}", $BlockLevel); } } // Never block an admin if (Gdn::session()->checkPermission('Garden.Settings.Manage')) { throw new Exception("Block detected", self::BLOCK_NEVER); } if (Gdn::session()->isValid()) { throw new Exception("Block detected", self::BLOCK_PERMISSION); } } catch (Exception $e) { // BlockLevel // TRUE = Block any time // FALSE = Absolutely no blocking // NULL = Block for permissions (e.g. PrivateCommunity) $CanBlock = $e->getCode(); } // If we're in updatemode and arent explicitly prevented from blocking, block if (Gdn::config('Garden.UpdateMode', false) && $CanBlock > self::BLOCK_NEVER) { $Request->withURI(Gdn::router()->getDestination('UpdateMode')); } // Analyze the request AFTER checking for update mode. $this->analyzeRequest($Request); $this->fireEvent('AfterAnalyzeRequest'); // If we're in update mode and can block, redirect to signin if (C('Garden.PrivateCommunity') && $CanBlock > self::BLOCK_PERMISSION) { if ($this->_DeliveryType === DELIVERY_TYPE_DATA) { safeHeader('HTTP/1.0 401 Unauthorized', true, 401); safeHeader('Content-Type: application/json; charset=' . c('Garden.Charset', 'utf-8'), true); echo json_encode(array('Code' => '401', 'Exception' => t('You must sign in.'))); } else { redirect('/entry/signin?Target=' . urlencode($this->Request)); } exit; } $ControllerName = $this->controllerName(); if ($ControllerName != '' && class_exists($ControllerName)) { // Create it and call the appropriate method/action /* @var Gdn_Controller $Controller */ $Controller = new $ControllerName(); Gdn::controller($Controller); $this->EventArguments['Controller'] =& $Controller; $this->fireEvent('AfterControllerCreate'); // Pass along any assets if (is_array($this->_AssetCollection)) { foreach ($this->_AssetCollection as $AssetName => $Assets) { foreach ($Assets as $Asset) { $Controller->addAsset($AssetName, $Asset); } } } // Instantiate Imported & Uses classes $Controller->getImports(); // Pass in the syndication method $Controller->SyndicationMethod = $this->_SyndicationMethod; // Pass along the request $Controller->SelfUrl = $this->Request; // Pass along any objects foreach ($this->_PropertyCollection as $Name => $Mixed) { $Controller->{$Name} = $Mixed; } // Pass along any data. if (is_array($this->_Data)) { $Controller->Data = $this->_Data; } // Set up a default controller method in case one isn't defined. $ControllerMethod = str_replace('_', '', $this->ControllerMethod); $Controller->OriginalRequestMethod = $ControllerMethod; $this->EventArguments['ControllerMethod'] =& $ControllerMethod; // Take enabled plugins into account, as well $PluginReplacement = Gdn::pluginManager()->hasNewMethod($this->controllerName(), $this->ControllerMethod); if (!$PluginReplacement && ($this->ControllerMethod == '' || !method_exists($Controller, $ControllerMethod)) && !$Controller->isInternal($ControllerMethod)) { // Check to see if there is an 'x' version of the method. if (method_exists($Controller, 'x' . $ControllerMethod)) { // $PluginManagerHasReplacementMethod = TRUE; $ControllerMethod = 'x' . $ControllerMethod; } else { if ($this->ControllerMethod != '') { array_unshift($this->_ControllerMethodArgs, $this->ControllerMethod); } $this->ControllerMethod = 'Index'; $ControllerMethod = 'Index'; $PluginReplacement = Gdn::pluginManager()->hasNewMethod($this->controllerName(), $this->ControllerMethod); } } // Pass in the querystring values $Controller->ApplicationFolder = $this->_ApplicationFolder; $Controller->Application = $this->enabledApplication(); $Controller->RequestMethod = $this->ControllerMethod; $Controller->RequestArgs = $this->_ControllerMethodArgs; $Controller->Request = $Request; $Controller->deliveryType($Request->getValue('DeliveryType', $this->_DeliveryType)); $Controller->deliveryMethod($Request->getValue('DeliveryMethod', $this->_DeliveryMethod)); // Set special controller method options for REST APIs. $Controller->initialize(); $this->EventArguments['Controller'] =& $Controller; $this->fireEvent('AfterControllerInit'); $ReflectionArguments = $Request->get(); $this->EventArguments['Arguments'] =& $ReflectionArguments; $this->fireEvent('BeforeReflect'); // Call the requested method on the controller - error out if not defined. if ($PluginReplacement) { // Reflect the args for the method. $Callback = Gdn::pluginManager()->getCallback($Controller->ControllerName, $ControllerMethod); // Augment the arguments to the plugin with the sender and these arguments. $InputArgs = array_merge(array($Controller), $this->_ControllerMethodArgs, array('Sender' => $Controller, 'Args' => $this->_ControllerMethodArgs)); $Args = reflectArgs($Callback, $InputArgs, $ReflectionArguments); $Controller->ReflectArgs = $Args; try { $this->fireEvent('BeforeControllerMethod'); Gdn::pluginManager()->callEventHandlers($Controller, $Controller->ControllerName, $ControllerMethod, 'Before'); call_user_func_array($Callback, $Args); } catch (Exception $Ex) { $Controller->renderException($Ex); } } elseif (method_exists($Controller, $ControllerMethod) && !$Controller->isInternal($ControllerMethod)) { $Args = reflectArgs(array($Controller, $ControllerMethod), $this->_ControllerMethodArgs, $ReflectionArguments); $this->_ControllerMethodArgs = $Args; $Controller->ReflectArgs = $Args; try { $this->fireEvent('BeforeControllerMethod'); Gdn::pluginManager()->callEventHandlers($Controller, $Controller->ControllerName, $ControllerMethod, 'Before'); call_user_func_array(array($Controller, $ControllerMethod), $Args); } catch (Exception $Ex) { $Controller->renderException($Ex); exit; } } else { $this->EventArguments['Handled'] = false; $Handled =& $this->EventArguments['Handled']; $this->fireEvent('NotFound'); if (!$Handled) { Gdn::request()->withRoute('Default404'); return $this->dispatch(); } else { return $Handled; } } } }
/** * Dispatch to a controller that's already been found with {@link Gdn_Dispatcher::analyzeRequest()}. * * Although the controller has been found, its method may not have been found and will render an error if so. * * @param Gdn_Request $request The request being dispatched. * @param array $routeArgs The result of {@link Gdn_Dispatcher::analyzeRequest()}. */ private function dispatchController($request, $routeArgs) { // Create the controller first. $controllerName = $routeArgs['controller']; $controller = $this->createController($controllerName, $request, $routeArgs); // Find the method to call. list($controllerMethod, $pathArgs) = $this->findControllerMethod($controller, $routeArgs['pathArgs']); if (!$controllerMethod) { // The controller method was not found. return $this->dispatchNotFound('method_notfound', $request); } // The method has been found, set it on the controller. $controller->RequestMethod = $controllerMethod; $controller->RequestArgs = $pathArgs; $controller->ResolvedPath = ($routeArgs['addon'] ? $routeArgs['addon']->getKey() . '/' : '') . strtolower(stringEndsWith($controllerName, 'Controller', true, true)) . '/' . strtolower($controllerMethod); $reflectionArguments = $request->get(); $this->EventArguments['Arguments'] =& $reflectionArguments; $this->fireEvent('BeforeReflect'); // Get the callback to call. if (Gdn::pluginManager()->hasNewMethod(get_class($controller), $controllerMethod)) { $callback = Gdn::pluginManager()->getCallback(get_class($controller), $controllerMethod); // Augment the arguments to the plugin with the sender and these arguments. // The named sender and args keys are an old legacy format before plugins could override controller methods properly. $InputArgs = array_merge([$controller], $pathArgs, ['sender' => $controller, 'args' => $pathArgs]); $args = reflectArgs($callback, $InputArgs, $reflectionArguments); } else { $callback = [$controller, $controllerMethod]; $args = reflectArgs($callback, $pathArgs, $reflectionArguments); } $controller->ReflectArgs = $args; // Now that we have everything its time to call the callback for the controller. try { $this->fireEvent('BeforeControllerMethod'); Gdn::pluginManager()->callEventHandlers($controller, $controllerName, $controllerMethod, 'before'); call_user_func_array($callback, $args); } catch (Exception $ex) { $controller->renderException($ex); exit; } }