| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Common; |
| 4 |
|
| 5 |
use ipl\Html\Contract\FormElement; |
| 6 |
use ipl\Html\Form; |
| 7 |
use LogicException; |
| 8 |
|
| 9 |
trait RedirectOption |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Create a form element to retrieve the redirect target upon form submit |
| 13 |
* |
| 14 |
* @return FormElement |
| 15 |
*/ |
| 16 |
protected function createRedirectOption() |
| 17 |
{ |
| 18 |
/** @var Form $this */ |
| 19 |
return $this->createElement('hidden', 'redirect'); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @see Form::getRedirectUrl() |
| 24 |
*/ |
| 25 |
public function getRedirectUrl() |
| 26 |
{ |
| 27 |
/** @var Form $this */ |
| 28 |
$redirectOption = $this->getValue('redirect'); |
| 29 |
if (! $redirectOption) { |
| 30 |
return parent::getRedirectUrl(); |
| 31 |
} |
| 32 |
|
| 33 |
if (! $this->hasElement('CSRFToken') || ! $this->getElement('CSRFToken')->isValid()) { |
| 34 |
throw new LogicException( |
| 35 |
'It is not safe to accept redirect targets from submit values without CSRF protection' |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
return $redirectOption; |
| 40 |
} |
| 41 |
} |