/** * Check whether all condition are met for this plugin to do its magic. * * @param Gdn_Request $request * @param int|bool $categoryID Category ID of the welcome category if it exists and this function return true * @return bool */ protected function isWelcomePostActive($request, &$categoryID) { static $cachedCategoryID = null; if ($cachedCategoryID === null) { $isWelcomePost = true; if ($request->get('welcomepost', false) !== "true") { $cachedCategoryID = false; return false; } if (!Gdn::session()->isValid()) { $cachedCategoryID = false; return false; } $category = (array) CategoryModel::instance()->getByCode('welcome'); $cachedCategoryID = val('CategoryID', $category, false); if (!$cachedCategoryID) { return false; } $categoryID = $cachedCategoryID; } else { $isWelcomePost = (bool) $cachedCategoryID; $categoryID = $cachedCategoryID; } return $isWelcomePost; }
/** * Render an exception as the sole output. * * @param Exception $Ex The exception to render. */ public function RenderException($Ex) { if ($this->DeliveryMethod() == DELIVERY_METHOD_XHTML) { try { switch ($Ex->getCode()) { case 401: Gdn::Dispatcher()->Dispatch('DefaultPermission'); break; case 404: Gdn::Dispatcher()->Dispatch('Default404'); break; default: Gdn_ExceptionHandler($Ex); } } catch (Exception $Ex2) { Gdn_ExceptionHandler($Ex); } return; } // Make sure the database connection is closed before exiting. $this->Finalize(); $this->SendHeaders(); $Code = $Ex->getCode(); if (Debug()) { $Message = $Ex->getMessage() . "\n\n" . $Ex->getTraceAsString(); } else { $Message = $Ex->getMessage(); } if ($Code >= 100 && $Code <= 505) { header("HTTP/1.0 {$Code}", TRUE, $Code); } else { header('HTTP/1.0 500', TRUE, 500); } $Data = array('Code' => $Code, 'Exception' => $Message); switch ($this->DeliveryMethod()) { case DELIVERY_METHOD_JSON: header('Content-Type: application/json', TRUE); if ($Callback = $this->Request->GetValueFrom(Gdn_Request::INPUT_GET, 'callback', FALSE)) { // This is a jsonp request. exit($Callback . '(' . json_encode($Data) . ');'); } else { // This is a regular json request. exit(json_encode($Data)); } break; // case DELIVERY_METHOD_XHTML: // Gdn_ExceptionHandler($Ex); // break; // case DELIVERY_METHOD_XHTML: // Gdn_ExceptionHandler($Ex); // break; case DELIVERY_METHOD_XML: header('Content-Type: text/xml', TRUE); array_map('htmlspecialchars', $Data); exit("<Exception><Code>{$Data['Code']}</Code><Message>{$Data['Exception']}</Message></Exception>"); break; } }
/** * 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(); if (Gdn::Session()->NewVisit()) { Gdn::UserModel()->FireEvent('Visit'); } // 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, '/^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')); } // Analze the request AFTER checking for update mode. $this->AnalyzeRequest($Request); $this->FireEvent('AfterAnalyzeRequest'); // If we're in updatemode and can block, redirect to signin if (C('Garden.PrivateCommunity') && $CanBlock > self::BLOCK_PERMISSION) { Redirect('/entry/signin?Target=' . urlencode($this->Request)); exit; } $ControllerName = $this->ControllerName(); if ($ControllerName != '' && class_exists($ControllerName)) { // Create it and call the appropriate method/action $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; // Take enabled plugins into account, as well $PluginReplacement = Gdn::PluginManager()->HasNewMethod($this->ControllerName(), $this->ControllerMethod); if (!$PluginReplacement && ($this->ControllerMethod == '' || !method_exists($Controller, $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->ControllerFolder = $this->ControllerFolder; $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'); // Call the requested method on the controller - error out if not defined. if ($PluginReplacement) { // Set the application folder to the plugin's key. // $PluginInfo = Gdn::PluginManager()->GetPluginInfo($PluginReplacement, Gdn_PluginManager::ACCESS_CLASSNAME); // if ($PluginInfo) { // $Controller->ApplicationFolder = 'plugins/'.GetValue('Index', $PluginInfo); // } // 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)); // decho(array_keys($InputArgs), 'InputArgs'); $Args = ReflectArgs($Callback, $InputArgs, $Request->Get()); $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)) { $Args = ReflectArgs(array($Controller, $ControllerMethod), $this->_ControllerMethodArgs, $Request->Get()); $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; } } } }
/** * 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(); if (Gdn::Config('Garden.UpdateMode', FALSE)) { if (!Gdn::Session()->CheckPermission('Garden.Settings.GlobalPrivs')) { // Updatemode, and this user is not root admin $Request->WithURI(Gdn::Router()->GetDestination('UpdateMode')); } } $this->FireEvent('BeforeDispatch'); $this->_AnalyzeRequest($Request); // Send user to login page if this is a private community if (C('Garden.PrivateCommunity') && $this->ControllerName() != 'EntryController' && !Gdn::Session()->IsValid()) { Redirect(Gdn::Authenticator()->SignInUrl($this->Request)); exit; } /* echo "<br />Gdn::Request thinks: ".Gdn::Request()->Path(); echo "<br />Gdn::Request also suggests: output=".Gdn::Request()->OutputFormat().", filename=".Gdn::Request()->Filename(); echo '<br />Request: '.$this->Request; echo '<br />App folder: '.$this->_ApplicationFolder; echo '<br />Controller folder: '.$this->_ControllerFolder; echo '<br />ControllerName: '.$this->_ControllerName; echo '<br />ControllerMethod: '.$this->_ControllerMethod; */ $ControllerName = $this->ControllerName(); if ($ControllerName != '' && class_exists($ControllerName)) { // Create it and call the appropriate method/action $Controller = new $ControllerName(); // 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; } // Set up a default controller method in case one isn't defined. $ControllerMethod = str_replace('_', '', $this->_ControllerMethod); $Controller->OriginalRequestMethod = $ControllerMethod; // Take enabled plugins into account, as well $PluginManagerHasReplacementMethod = Gdn::PluginManager()->HasNewMethod($this->ControllerName(), $this->_ControllerMethod); if (!$PluginManagerHasReplacementMethod && ($this->_ControllerMethod == '' || !method_exists($Controller, $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'; $PluginManagerHasReplacementMethod = Gdn::PluginManager()->HasNewMethod($this->ControllerName(), $this->_ControllerMethod); } } // Pass in the querystring values $Controller->ApplicationFolder = $this->_ApplicationFolder; $Controller->Application = $this->EnabledApplication(); $Controller->ControllerFolder = $this->_ControllerFolder; $Controller->RequestMethod = $this->_ControllerMethod; $Controller->RequestArgs = $this->_ControllerMethodArgs; $Controller->Request = $Request; $Controller->DeliveryType($Request->GetValue('DeliveryType', '')); $Controller->DeliveryMethod($Request->GetValue('DeliveryMethod', '')); $Controller->Initialize(); // Call the requested method on the controller - error out if not defined. if ($PluginManagerHasReplacementMethod || method_exists($Controller, $ControllerMethod)) { // call_user_func_array is too slow!! //call_user_func_array(array($Controller, $ControllerMethod), $this->_ControllerMethodArgs); if ($PluginManagerHasReplacementMethod) { Gdn::PluginManager()->CallNewMethod($Controller, $Controller->ControllerName, $ControllerMethod); } else { $Args = $this->_ControllerMethodArgs; $Count = count($Args); if ($Count == 0) { $Controller->{$ControllerMethod}(); } else { if ($Count == 1) { $Controller->{$ControllerMethod}($Args[0]); } else { if ($Count == 2) { $Controller->{$ControllerMethod}($Args[0], $Args[1]); } else { if ($Count == 3) { $Controller->{$ControllerMethod}($Args[0], $Args[1], $Args[2]); } else { if ($Count == 4) { $Controller->{$ControllerMethod}($Args[0], $Args[1], $Args[2], $Args[3]); } else { if ($Count == 5) { $Controller->{$ControllerMethod}($Args[0], $Args[1], $Args[2], $Args[3], $Args[4]); } else { if ($Count == 6) { $Controller->{$ControllerMethod}($Args[0], $Args[1], $Args[2], $Args[3], $Args[4], $Args[5]); } else { if ($Count == 7) { $Controller->{$ControllerMethod}($Args[0], $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6]); } else { if ($Count == 8) { $Controller->{$ControllerMethod}($Args[0], $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6], $Args[7]); } else { if ($Count == 9) { $Controller->{$ControllerMethod}($Args[0], $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6], $Args[7], $Args[8]); } else { $Controller->{$ControllerMethod}($Args[0], $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6], $Args[7], $Args[8], $Args[9]); } } } } } } } } } } } } else { Gdn::Request()->WithRoute('Default404'); return $this->Dispatch(); } } }
/** * Render an exception as the sole output. * * @param Exception $Ex The exception to render. */ public function RenderException($Ex) { if ($this->DeliveryMethod() == DELIVERY_METHOD_XHTML) { try { if (is_a($Ex, 'Gdn_UserException')) { Gdn::Dispatcher()->PassData('Code', $Ex->getCode())->PassData('Exception', $Ex->getMessage())->PassData('Message', $Ex->getMessage())->PassData('Trace', $Ex->getTraceAsString())->PassData('Url', Url())->PassData('Breadcrumbs', $this->Data('Breadcrumbs', array()))->Dispatch('/home/error'); } else { switch ($Ex->getCode()) { case 401: Gdn::Dispatcher()->PassData('Message', $Ex->getMessage())->PassData('Url', Url())->Dispatch('DefaultPermission'); break; case 404: Gdn::Dispatcher()->PassData('Message', $Ex->getMessage())->PassData('Url', Url())->Dispatch('Default404'); break; default: Gdn_ExceptionHandler($Ex); } } } catch (Exception $Ex2) { Gdn_ExceptionHandler($Ex); } return; } // Make sure the database connection is closed before exiting. $this->Finalize(); $this->SendHeaders(); $Code = $Ex->getCode(); $Data = array('Code' => $Code, 'Exception' => $Ex->getMessage(), 'Class' => get_class($Ex)); if (Debug()) { if ($Trace = Trace()) { // Clear passwords from the trace. array_walk_recursive($Trace, function (&$Value, $Key) { if (in_array(strtolower($Key), array('password'))) { $Value = '***'; } }); $Data['Trace'] = $Trace; } if (!is_a($Ex, 'Gdn_UserException')) { $Data['StackTrace'] = $Ex->getTraceAsString(); } $Data['Data'] = $this->Data; } // Try cleaning out any notices or errors. @ob_clean(); if ($Code >= 400 && $Code <= 505) { safeHeader("HTTP/1.0 {$Code}", TRUE, $Code); } else { safeHeader('HTTP/1.0 500', TRUE, 500); } switch ($this->DeliveryMethod()) { case DELIVERY_METHOD_JSON: if (($Callback = $this->Request->GetValueFrom(Gdn_Request::INPUT_GET, 'callback', FALSE)) && $this->AllowJSONP()) { safeHeader('Content-Type: application/javascript', TRUE); // This is a jsonp request. exit($Callback . '(' . json_encode($Data) . ');'); } else { safeHeader('Content-Type: application/json', TRUE); // This is a regular json request. exit(json_encode($Data)); } break; // case DELIVERY_METHOD_XHTML: // Gdn_ExceptionHandler($Ex); // break; // case DELIVERY_METHOD_XHTML: // Gdn_ExceptionHandler($Ex); // break; case DELIVERY_METHOD_XML: safeHeader('Content-Type: text/xml', TRUE); array_map('htmlspecialchars', $Data); exit("<Exception><Code>{$Data['Code']}</Code><Class>{$Data['Class']}</Class><Message>{$Data['Exception']}</Message></Exception>"); break; default: safeHeader('Content-Type: text/plain', TRUE); exit($Ex->getMessage()); } }
/** * 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; } } } }
/** * Rewrite the request based on rewrite rules (currently called routes in Vanilla). * * This method modifies the passed {@link $request} object. It can also cause a redirect if a rule matches that * specifies a redirect. * * @param Gdn_Request $request The request to rewrite. */ private function rewriteRequest($request) { $pathAndQuery = $request->PathAndQuery(); $matchRoute = Gdn::router()->matchRoute($pathAndQuery); // We have a route. Take action. if (!empty($matchRoute)) { $dest = $matchRoute['FinalDestination']; if (strpos($dest, '?') === false) { // The rewrite rule doesn't include a query string so keep the current one intact. $request->path($dest); } else { // The rewrite rule has a query string so rewrite that too. $request->pathAndQuery($dest); } switch ($matchRoute['Type']) { case 'Internal': // Do nothing. The request has been rewritten. break; case 'Temporary': safeHeader("HTTP/1.1 302 Moved Temporarily"); safeHeader("Location: " . url($matchRoute['FinalDestination'])); exit; break; case 'Permanent': safeHeader("HTTP/1.1 301 Moved Permanently"); safeHeader("Location: " . url($matchRoute['FinalDestination'])); exit; break; case 'NotAuthorized': safeHeader("HTTP/1.1 401 Not Authorized"); break; case 'NotFound': safeHeader("HTTP/1.1 404 Not Found"); break; case 'Drop': die; case 'Test': decho($matchRoute, 'Route'); decho(array('Path' => $request->path(), 'Get' => $request->get()), 'Request'); die; } } elseif (in_array($request->path(), ['', '/'])) { $this->isHomepage = true; $defaultController = Gdn::router()->getRoute('DefaultController'); $request->pathAndQuery($defaultController['Destination']); } return $request; }
/** * 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(); if (Gdn::Config('Garden.UpdateMode', FALSE)) { if (!Gdn::Session()->CheckPermission('Garden.Settings.GlobalPrivs')) { // Updatemode, and this user is not root admin $Request->WithURI(Gdn::Router()->GetDestination('UpdateMode')); } } $this->FireEvent('BeforeDispatch'); $this->AnalyzeRequest($Request); // Send user to login page if this is a private community (with some minor exceptions) if ( C('Garden.PrivateCommunity') && $this->ControllerName() != 'EntryController' && !Gdn::Session()->IsValid() && !InArrayI($this->ControllerMethod(), array('UsernameAvailable', 'EmailAvailable', 'TermsOfService')) ) { Redirect('/entry/signin?Target='.urlencode($this->Request)); exit(); } $ControllerName = $this->ControllerName(); if ($ControllerName != '' && class_exists($ControllerName)) { // Create it and call the appropriate method/action $Controller = new $ControllerName(); $this->EventArguments['Controller'] =& $Controller; // 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; } // Set up a default controller method in case one isn't defined. $ControllerMethod = str_replace('_', '', $this->_ControllerMethod); $Controller->OriginalRequestMethod = $ControllerMethod; $this->FireEvent('AfterAnalyzeRequest'); // Take enabled plugins into account, as well $PluginManagerHasReplacementMethod = Gdn::PluginManager()->HasNewMethod($this->ControllerName(), $this->_ControllerMethod); if (!$PluginManagerHasReplacementMethod && ($this->_ControllerMethod == '' || !method_exists($Controller, $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'; $PluginManagerHasReplacementMethod = Gdn::PluginManager()->HasNewMethod($this->ControllerName(), $this->_ControllerMethod); } } // Pass in the querystring values $Controller->ApplicationFolder = $this->_ApplicationFolder; $Controller->Application = $this->EnabledApplication(); $Controller->ControllerFolder = $this->_ControllerFolder; $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)); $this->FireEvent('BeforeControllerMethod'); // Set special controller method options for REST APIs. $this->_ReflectControllerArgs($Controller); Gdn::Controller($Controller); $Controller->Initialize(); // Call the requested method on the controller - error out if not defined. if ($PluginManagerHasReplacementMethod || method_exists($Controller, $ControllerMethod)) { // call_user_func_array is too slow!! //call_user_func_array(array($Controller, $ControllerMethod), $this->_ControllerMethodArgs); if ($PluginManagerHasReplacementMethod) { try { Gdn::PluginManager()->CallNewMethod($Controller, $Controller->ControllerName, $ControllerMethod); } catch (Exception $Ex) { $Controller->RenderException($Ex); } } else { $Args = $this->_ControllerMethodArgs; $Count = count($Args); try { call_user_func_array(array($Controller, $ControllerMethod), $Args); } catch (Exception $Ex) { $Controller->RenderException($Ex); exit(); } } } else { Gdn::Request()->WithRoute('Default404'); return $this->Dispatch(); } } }
public function EntryController_Land_Create($Sender) { if (!Gdn::Authenticator()->IsPrimary('proxy')) { return; } $LandingRequest = Gdn_Request::Create()->FromImport(Gdn::Request())->WithURI("/dashboard/entry/signin")->WithCustomArgs(array('Landing' => TRUE)); return Gdn::Dispatcher()->Dispatch($LandingRequest); }