| Total Complexity | 100 |
| Total Lines | 622 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RequestHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RequestHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class RequestHandler extends ViewableData |
||
| 48 | { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Optional url_segment for this request handler |
||
| 52 | * |
||
| 53 | * @config |
||
| 54 | * @var string|null |
||
| 55 | */ |
||
| 56 | private static $url_segment = null; |
||
|
|
|||
| 57 | |||
| 58 | /** |
||
| 59 | * @var HTTPRequest $request The request object that the controller was called with. |
||
| 60 | * Set in {@link handleRequest()}. Useful to generate the {} |
||
| 61 | */ |
||
| 62 | protected $request = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The DataModel for this request |
||
| 66 | */ |
||
| 67 | protected $model = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * This variable records whether RequestHandler::__construct() |
||
| 71 | * was called or not. Useful for checking if subclasses have |
||
| 72 | * called parent::__construct() |
||
| 73 | * |
||
| 74 | * @var boolean |
||
| 75 | */ |
||
| 76 | protected $brokenOnConstruct = true; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The default URL handling rules. This specifies that the next component of the URL corresponds to a method to |
||
| 80 | * be called on this RequestHandlingData object. |
||
| 81 | * |
||
| 82 | * The keys of this array are parse rules. See {@link HTTPRequest::match()} for a description of the rules |
||
| 83 | * available. |
||
| 84 | * |
||
| 85 | * The values of the array are the method to be called if the rule matches. If this value starts with a '$', then |
||
| 86 | * the named parameter of the parsed URL wil be used to determine the method name. |
||
| 87 | * @config |
||
| 88 | */ |
||
| 89 | private static $url_handlers = array( |
||
| 90 | '$Action' => '$Action', |
||
| 91 | ); |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Define a list of action handling methods that are allowed to be called directly by URLs. |
||
| 96 | * The variable should be an array of action names. This sample shows the different values that it can contain: |
||
| 97 | * |
||
| 98 | * <code> |
||
| 99 | * array( |
||
| 100 | * // someaction can be accessed by anyone, any time |
||
| 101 | * 'someaction', |
||
| 102 | * // So can otheraction |
||
| 103 | * 'otheraction' => true, |
||
| 104 | * // restrictedaction can only be people with ADMIN privilege |
||
| 105 | * 'restrictedaction' => 'ADMIN', |
||
| 106 | * // complexaction can only be accessed if $this->canComplexAction() returns true |
||
| 107 | * 'complexaction' '->canComplexAction' |
||
| 108 | * ); |
||
| 109 | * </code> |
||
| 110 | * |
||
| 111 | * Form getters count as URL actions as well, and should be included in allowed_actions. |
||
| 112 | * Form actions on the other handed (first argument to {@link FormAction()} should NOT be included, |
||
| 113 | * these are handled separately through {@link Form->httpSubmission}. You can control access on form actions |
||
| 114 | * either by conditionally removing {@link FormAction} in the form construction, |
||
| 115 | * or by defining $allowed_actions in your {@link Form} class. |
||
| 116 | * @config |
||
| 117 | */ |
||
| 118 | private static $allowed_actions = null; |
||
| 119 | |||
| 120 | public function __construct() |
||
| 121 | { |
||
| 122 | $this->brokenOnConstruct = false; |
||
| 123 | |||
| 124 | $this->setRequest(new NullHTTPRequest()); |
||
| 125 | |||
| 126 | parent::__construct(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Handles URL requests. |
||
| 131 | * |
||
| 132 | * - ViewableData::handleRequest() iterates through each rule in {@link self::$url_handlers}. |
||
| 133 | * - If the rule matches, the named method will be called. |
||
| 134 | * - If there is still more URL to be processed, then handleRequest() |
||
| 135 | * is called on the object that that method returns. |
||
| 136 | * |
||
| 137 | * Once all of the URL has been processed, the final result is returned. |
||
| 138 | * However, if the final result is an array, this |
||
| 139 | * array is interpreted as being additional template data to customise the |
||
| 140 | * 2nd to last result with, rather than an object |
||
| 141 | * in its own right. This is most frequently used when a Controller's |
||
| 142 | * action will return an array of data with which to |
||
| 143 | * customise the controller. |
||
| 144 | * |
||
| 145 | * @param HTTPRequest $request The object that is reponsible for distributing URL parsing |
||
| 146 | * @return HTTPResponse|RequestHandler|string|array |
||
| 147 | */ |
||
| 148 | public function handleRequest(HTTPRequest $request) |
||
| 149 | { |
||
| 150 | // $handlerClass is used to step up the class hierarchy to implement url_handlers inheritance |
||
| 151 | if ($this->brokenOnConstruct) { |
||
| 152 | $handlerClass = static::class; |
||
| 153 | throw new BadMethodCallException( |
||
| 154 | "parent::__construct() needs to be called on {$handlerClass}::__construct()" |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->setRequest($request); |
||
| 159 | |||
| 160 | $match = $this->findAction($request); |
||
| 161 | |||
| 162 | // If nothing matches, return this object |
||
| 163 | if (!$match) { |
||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | // Start to find what action to call. Start by using what findAction returned |
||
| 168 | $action = $match['action']; |
||
| 169 | |||
| 170 | // We used to put "handleAction" as the action on controllers, but (a) this could only be called when |
||
| 171 | // you had $Action in your rule, and (b) RequestHandler didn't have one. $Action is better |
||
| 172 | if ($action == 'handleAction') { |
||
| 173 | // TODO Fix LeftAndMain usage |
||
| 174 | // Deprecation::notice('3.2.0', 'Calling handleAction directly is deprecated - use $Action instead'); |
||
| 175 | $action = '$Action'; |
||
| 176 | } |
||
| 177 | |||
| 178 | // Actions can reference URL parameters, eg, '$Action/$ID/$OtherID' => '$Action', |
||
| 179 | if ($action[0] == '$') { |
||
| 180 | $action = str_replace("-", "_", $request->latestParam(substr($action, 1))); |
||
| 181 | } |
||
| 182 | |||
| 183 | if (!$action) { |
||
| 184 | if (isset($_REQUEST['debug_request'])) { |
||
| 185 | Debug::message("Action not set; using default action method name 'index'"); |
||
| 186 | } |
||
| 187 | $action = "index"; |
||
| 188 | } elseif (!is_string($action)) { |
||
| 189 | user_error("Non-string method name: " . var_export($action, true), E_USER_ERROR); |
||
| 190 | } |
||
| 191 | |||
| 192 | $classMessage = Director::isLive() ? 'on this handler' : 'on class '.static::class; |
||
| 193 | |||
| 194 | try { |
||
| 195 | if (!$this->hasAction($action)) { |
||
| 196 | return $this->httpError(404, "Action '$action' isn't available $classMessage."); |
||
| 197 | } |
||
| 198 | if (!$this->checkAccessAction($action) || in_array(strtolower($action), array('run', 'doInit'))) { |
||
| 199 | return $this->httpError(403, "Action '$action' isn't allowed $classMessage."); |
||
| 200 | } |
||
| 201 | $result = $this->handleAction($request, $action); |
||
| 202 | } catch (HTTPResponse_Exception $e) { |
||
| 203 | return $e->getResponse(); |
||
| 204 | } catch (PermissionFailureException $e) { |
||
| 205 | $result = Security::permissionFailure(null, $e->getMessage()); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($result instanceof HTTPResponse && $result->isError()) { |
||
| 209 | if (isset($_REQUEST['debug_request'])) { |
||
| 210 | Debug::message("Rule resulted in HTTP error; breaking"); |
||
| 211 | } |
||
| 212 | return $result; |
||
| 213 | } |
||
| 214 | |||
| 215 | // If we return a RequestHandler, call handleRequest() on that, even if there is no more URL to |
||
| 216 | // parse. It might have its own handler. However, we only do this if we haven't just parsed an |
||
| 217 | // empty rule ourselves, to prevent infinite loops. Also prevent further handling of controller |
||
| 218 | // actions which return themselves to avoid infinite loops. |
||
| 219 | $matchedRuleWasEmpty = $request->isEmptyPattern($match['rule']); |
||
| 220 | if ($this !== $result && !$matchedRuleWasEmpty && ($result instanceof RequestHandler || $result instanceof HasRequestHandler)) { |
||
| 221 | // Expose delegated request handler |
||
| 222 | if ($result instanceof HasRequestHandler) { |
||
| 223 | $result = $result->getRequestHandler(); |
||
| 224 | } |
||
| 225 | $returnValue = $result->handleRequest($request); |
||
| 226 | |||
| 227 | // Array results can be used to handle |
||
| 228 | if (is_array($returnValue)) { |
||
| 229 | $returnValue = $this->customise($returnValue); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $returnValue; |
||
| 233 | |||
| 234 | // If we return some other data, and all the URL is parsed, then return that |
||
| 235 | } elseif ($request->allParsed()) { |
||
| 236 | return $result; |
||
| 237 | |||
| 238 | // But if we have more content on the URL and we don't know what to do with it, return an error. |
||
| 239 | } else { |
||
| 240 | return $this->httpError(404, "I can't handle sub-URLs $classMessage."); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param HTTPRequest $request |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | protected function findAction($request) |
||
| 249 | { |
||
| 250 | $handlerClass = static::class; |
||
| 251 | |||
| 252 | // We stop after RequestHandler; in other words, at ViewableData |
||
| 253 | while ($handlerClass && $handlerClass != ViewableData::class) { |
||
| 254 | $urlHandlers = Config::inst()->get($handlerClass, 'url_handlers', Config::UNINHERITED); |
||
| 255 | |||
| 256 | if ($urlHandlers) { |
||
| 257 | foreach ($urlHandlers as $rule => $action) { |
||
| 258 | if (isset($_REQUEST['debug_request'])) { |
||
| 259 | $class = static::class; |
||
| 260 | $remaining = $request->remaining(); |
||
| 261 | Debug::message("Testing '{$rule}' with '{$remaining}' on {$class}"); |
||
| 262 | } |
||
| 263 | |||
| 264 | if ($request->match($rule, true)) { |
||
| 265 | if (isset($_REQUEST['debug_request'])) { |
||
| 266 | $class = static::class; |
||
| 267 | $latestParams = var_export($request->latestParams(), true); |
||
| 268 | Debug::message( |
||
| 269 | "Rule '{$rule}' matched to action '{$action}' on {$class}. ". |
||
| 270 | "Latest request params: {$latestParams}" |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | |||
| 274 | return array('rule' => $rule, 'action' => $action); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | $handlerClass = get_parent_class($handlerClass); |
||
| 280 | } |
||
| 281 | return null; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $link |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | protected function addBackURLParam($link) |
||
| 289 | { |
||
| 290 | $backURL = $this->getBackURL(); |
||
| 291 | if ($backURL) { |
||
| 292 | return Controller::join_links($link, '?BackURL=' . urlencode($backURL)); |
||
| 293 | } |
||
| 294 | |||
| 295 | return $link; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Given a request, and an action name, call that action name on this RequestHandler |
||
| 300 | * |
||
| 301 | * Must not raise HTTPResponse_Exceptions - instead it should return |
||
| 302 | * |
||
| 303 | * @param $request |
||
| 304 | * @param $action |
||
| 305 | * @return HTTPResponse |
||
| 306 | */ |
||
| 307 | protected function handleAction($request, $action) |
||
| 308 | { |
||
| 309 | $classMessage = Director::isLive() ? 'on this handler' : 'on class '.static::class; |
||
| 310 | |||
| 311 | if (!$this->hasMethod($action)) { |
||
| 312 | return new HTTPResponse("Action '$action' isn't available $classMessage.", 404); |
||
| 313 | } |
||
| 314 | |||
| 315 | $res = $this->extend('beforeCallActionHandler', $request, $action); |
||
| 316 | if ($res) { |
||
| 317 | return reset($res); |
||
| 318 | } |
||
| 319 | |||
| 320 | $actionRes = $this->$action($request); |
||
| 321 | |||
| 322 | $res = $this->extend('afterCallActionHandler', $request, $action, $actionRes); |
||
| 323 | if ($res) { |
||
| 324 | return reset($res); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $actionRes; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get a array of allowed actions defined on this controller, |
||
| 332 | * any parent classes or extensions. |
||
| 333 | * |
||
| 334 | * Caution: Since 3.1, allowed_actions definitions only apply |
||
| 335 | * to methods on the controller they're defined on, |
||
| 336 | * so it is recommended to use the $class argument |
||
| 337 | * when invoking this method. |
||
| 338 | * |
||
| 339 | * @param string $limitToClass |
||
| 340 | * @return array|null |
||
| 341 | */ |
||
| 342 | public function allowedActions($limitToClass = null) |
||
| 343 | { |
||
| 344 | if ($limitToClass) { |
||
| 345 | $actions = Config::forClass($limitToClass)->get('allowed_actions', true); |
||
| 346 | } else { |
||
| 347 | $actions = $this->config()->get('allowed_actions'); |
||
| 348 | } |
||
| 349 | |||
| 350 | if (is_array($actions)) { |
||
| 351 | if (array_key_exists('*', $actions)) { |
||
| 352 | throw new InvalidArgumentException("Invalid allowed_action '*'"); |
||
| 353 | } |
||
| 354 | |||
| 355 | // convert all keys and values to lowercase to |
||
| 356 | // allow for easier comparison, unless it is a permission code |
||
| 357 | $actions = array_change_key_case($actions, CASE_LOWER); |
||
| 358 | |||
| 359 | foreach ($actions as $key => $value) { |
||
| 360 | if (is_numeric($key)) { |
||
| 361 | $actions[$key] = strtolower($value); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | return $actions; |
||
| 366 | } else { |
||
| 367 | return null; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Checks if this request handler has a specific action, |
||
| 373 | * even if the current user cannot access it. |
||
| 374 | * Includes class ancestry and extensions in the checks. |
||
| 375 | * |
||
| 376 | * @param string $action |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public function hasAction($action) |
||
| 380 | { |
||
| 381 | if ($action == 'index') { |
||
| 382 | return true; |
||
| 383 | } |
||
| 384 | |||
| 385 | // Don't allow access to any non-public methods (inspect instance plus all extensions) |
||
| 386 | $insts = array_merge(array($this), (array) $this->getExtensionInstances()); |
||
| 387 | foreach ($insts as $inst) { |
||
| 388 | if (!method_exists($inst, $action)) { |
||
| 389 | continue; |
||
| 390 | } |
||
| 391 | $r = new ReflectionClass(get_class($inst)); |
||
| 392 | $m = $r->getMethod($action); |
||
| 393 | if (!$m || !$m->isPublic()) { |
||
| 394 | return false; |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | $action = strtolower($action); |
||
| 399 | $actions = $this->allowedActions(); |
||
| 400 | |||
| 401 | // Check if the action is defined in the allowed actions of any ancestry class |
||
| 402 | // as either a key or value. Note that if the action is numeric, then keys are not |
||
| 403 | // searched for actions to prevent actual array keys being recognised as actions. |
||
| 404 | if (is_array($actions)) { |
||
| 405 | $isKey = !is_numeric($action) && array_key_exists($action, $actions); |
||
| 406 | $isValue = in_array($action, $actions, true); |
||
| 407 | if ($isKey || $isValue) { |
||
| 408 | return true; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | $actionsWithoutExtra = $this->config()->get('allowed_actions', true); |
||
| 413 | if (!is_array($actions) || !$actionsWithoutExtra) { |
||
| 414 | if ($action != 'doInit' && $action != 'run' && method_exists($this, $action)) { |
||
| 415 | return true; |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | return false; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Return the class that defines the given action, so that we know where to check allowed_actions. |
||
| 424 | * |
||
| 425 | * @param string $actionOrigCasing |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | protected function definingClassForAction($actionOrigCasing) |
||
| 429 | { |
||
| 430 | $action = strtolower($actionOrigCasing); |
||
| 431 | |||
| 432 | $definingClass = null; |
||
| 433 | $insts = array_merge(array($this), (array) $this->getExtensionInstances()); |
||
| 434 | foreach ($insts as $inst) { |
||
| 435 | if (!method_exists($inst, $action)) { |
||
| 436 | continue; |
||
| 437 | } |
||
| 438 | $r = new ReflectionClass(get_class($inst)); |
||
| 439 | $m = $r->getMethod($actionOrigCasing); |
||
| 440 | return $m->getDeclaringClass()->getName(); |
||
| 441 | } |
||
| 442 | return null; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Check that the given action is allowed to be called from a URL. |
||
| 447 | * It will interrogate {@link self::$allowed_actions} to determine this. |
||
| 448 | * |
||
| 449 | * @param string $action |
||
| 450 | * @return bool |
||
| 451 | * @throws Exception |
||
| 452 | */ |
||
| 453 | public function checkAccessAction($action) |
||
| 454 | { |
||
| 455 | $actionOrigCasing = $action; |
||
| 456 | $action = strtolower($action); |
||
| 457 | |||
| 458 | $isAllowed = false; |
||
| 459 | $isDefined = false; |
||
| 460 | |||
| 461 | // Get actions for this specific class (without inheritance) |
||
| 462 | $definingClass = $this->definingClassForAction($actionOrigCasing); |
||
| 463 | $allowedActions = $this->allowedActions($definingClass); |
||
| 464 | |||
| 465 | // check if specific action is set |
||
| 466 | if (isset($allowedActions[$action])) { |
||
| 467 | $isDefined = true; |
||
| 468 | $test = $allowedActions[$action]; |
||
| 469 | if ($test === true || $test === 1 || $test === '1') { |
||
| 470 | // TRUE should always allow access |
||
| 471 | $isAllowed = true; |
||
| 472 | } elseif (substr($test, 0, 2) == '->') { |
||
| 473 | // Determined by custom method with "->" prefix |
||
| 474 | list($method, $arguments) = ClassInfo::parse_class_spec(substr($test, 2)); |
||
| 475 | $isAllowed = call_user_func_array(array($this, $method), $arguments); |
||
| 476 | } else { |
||
| 477 | // Value is a permission code to check the current member against |
||
| 478 | $isAllowed = Permission::check($test); |
||
| 479 | } |
||
| 480 | } elseif (is_array($allowedActions) |
||
| 481 | && (($key = array_search($action, $allowedActions, true)) !== false) |
||
| 482 | && is_numeric($key) |
||
| 483 | ) { |
||
| 484 | // Allow numeric array notation (search for array value as action instead of key) |
||
| 485 | $isDefined = true; |
||
| 486 | $isAllowed = true; |
||
| 487 | } elseif (is_array($allowedActions) && !count($allowedActions)) { |
||
| 488 | // If defined as empty array, deny action |
||
| 489 | $isAllowed = false; |
||
| 490 | } elseif ($allowedActions === null) { |
||
| 491 | // If undefined, allow action based on configuration |
||
| 492 | $isAllowed = false; |
||
| 493 | } |
||
| 494 | |||
| 495 | // If we don't have a match in allowed_actions, |
||
| 496 | // whitelist the 'index' action as well as undefined actions based on configuration. |
||
| 497 | if (!$isDefined && ($action == 'index' || empty($action))) { |
||
| 498 | $isAllowed = true; |
||
| 499 | } |
||
| 500 | |||
| 501 | return $isAllowed; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Throws a HTTP error response encased in a {@link HTTPResponse_Exception}, which is later caught in |
||
| 506 | * {@link RequestHandler::handleAction()} and returned to the user. |
||
| 507 | * |
||
| 508 | * @param int $errorCode |
||
| 509 | * @param string $errorMessage Plaintext error message |
||
| 510 | * @uses HTTPResponse_Exception |
||
| 511 | * @throws HTTPResponse_Exception |
||
| 512 | */ |
||
| 513 | public function httpError($errorCode, $errorMessage = null) |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Returns the HTTPRequest object that this controller is using. |
||
| 529 | * Returns a placeholder {@link NullHTTPRequest} object unless |
||
| 530 | * {@link handleAction()} or {@link handleRequest()} have been called, |
||
| 531 | * which adds a reference to an actual {@link HTTPRequest} object. |
||
| 532 | * |
||
| 533 | * @return HTTPRequest |
||
| 534 | */ |
||
| 535 | public function getRequest() |
||
| 536 | { |
||
| 537 | return $this->request; |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Typically the request is set through {@link handleAction()} |
||
| 542 | * or {@link handleRequest()}, but in some based we want to set it manually. |
||
| 543 | * |
||
| 544 | * @param HTTPRequest $request |
||
| 545 | * @return $this |
||
| 546 | */ |
||
| 547 | public function setRequest($request) |
||
| 548 | { |
||
| 549 | $this->request = $request; |
||
| 550 | return $this; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Returns a link to this controller. Overload with your own Link rules if they exist. |
||
| 555 | * |
||
| 556 | * @param string $action Optional action |
||
| 557 | * @return string |
||
| 558 | */ |
||
| 559 | public function Link($action = null) |
||
| 560 | { |
||
| 561 | // Check configured url_segment |
||
| 562 | $url = $this->config()->get('url_segment'); |
||
| 563 | if ($url) { |
||
| 564 | return Controller::join_links($url, $action, '/'); |
||
| 565 | } |
||
| 566 | |||
| 567 | // no link defined by default |
||
| 568 | trigger_error( |
||
| 569 | 'Request handler '.static::class. ' does not have a url_segment defined. '. |
||
| 570 | 'Relying on this link may be an application error', |
||
| 571 | E_USER_WARNING |
||
| 572 | ); |
||
| 573 | return null; |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Redirect to the given URL. |
||
| 578 | * |
||
| 579 | * @param string $url |
||
| 580 | * @param int $code |
||
| 581 | * @return HTTPResponse |
||
| 582 | */ |
||
| 583 | public function redirect($url, $code = 302) |
||
| 584 | { |
||
| 585 | $url = Director::absoluteURL($url); |
||
| 586 | $response = new HTTPResponse(); |
||
| 587 | return $response->redirect($url, $code); |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Safely get the value of the BackURL param, if provided via querystring / posted var |
||
| 592 | * |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | public function getBackURL() |
||
| 596 | { |
||
| 597 | $request = $this->getRequest(); |
||
| 598 | if (!$request) { |
||
| 599 | return null; |
||
| 600 | } |
||
| 601 | $backURL = $request->requestVar('BackURL'); |
||
| 602 | // Fall back to X-Backurl header |
||
| 603 | if (!$backURL && $request->isAjax() && $request->getHeader('X-Backurl')) { |
||
| 604 | $backURL = $request->getHeader('X-Backurl'); |
||
| 605 | } |
||
| 606 | if (!$backURL) { |
||
| 607 | return null; |
||
| 608 | } |
||
| 609 | if (Director::is_site_url($backURL)) { |
||
| 610 | return $backURL; |
||
| 611 | } |
||
| 612 | return null; |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Returns the referer, if it is safely validated as an internal URL |
||
| 617 | * and can be redirected to. |
||
| 618 | * |
||
| 619 | * @internal called from {@see Form::getValidationErrorResponse} |
||
| 620 | * @return string|null |
||
| 621 | */ |
||
| 622 | public function getReturnReferer() |
||
| 623 | { |
||
| 624 | $referer = $this->getReferer(); |
||
| 625 | if ($referer && Director::is_site_url($referer)) { |
||
| 626 | return $referer; |
||
| 627 | } |
||
| 628 | return null; |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get referer |
||
| 633 | * |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | public function getReferer() |
||
| 637 | { |
||
| 638 | $request = $this->getRequest(); |
||
| 639 | if (!$request) { |
||
| 640 | return null; |
||
| 641 | } |
||
| 642 | return $request->getHeader('Referer'); |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Redirect back. Uses either the HTTP-Referer or a manually set request-variable called "BackURL". |
||
| 647 | * This variable is needed in scenarios where HTTP-Referer is not sent (e.g when calling a page by |
||
| 648 | * location.href in IE). If none of the two variables is available, it will redirect to the base |
||
| 649 | * URL (see {@link Director::baseURL()}). |
||
| 650 | * |
||
| 651 | * @uses redirect() |
||
| 652 | * |
||
| 653 | * @return HTTPResponse |
||
| 654 | */ |
||
| 655 | public function redirectBack() |
||
| 669 | } |
||
| 670 | } |
||
| 671 |