66
67
* Check CSRF token is valid.
67
* @throws TokenExpiredException
69
public function check()
71
$session = $this->app->session;
72
/* @var \Xibo\Helper\Session $session */
74
if (!$session->get($this->key)) {
75
$session->set($this->key, sha1(serialize($_SERVER) . rand(0, 0xffffffff)));
78
$token = $session->get($this->key);
69
public function check() {
70
// Check sessions are enabled.
71
if (session_id() === '') {
72
throw new \Exception('Sessions are required to use the CSRF Guard middleware.');
75
if (! isset($_SESSION[$this->key])) {
76
$_SESSION[$this->key] = sha1(serialize($_SERVER) . rand(0, 0xffffffff));
79
$token = $_SESSION[$this->key];
80
81
// Validate the CSRF token.
81
82
if (in_array($this->app->request()->getMethod(), array('POST', 'PUT', 'DELETE'))) {
82
// Validate the token unless we are on an excluded route
83
$route = $this->app->router()->getCurrentRoute()->getPattern();
85
if ($this->app->excludedCsrfRoutes == null || ($route != null && !in_array($route, $this->app->excludedCsrfRoutes))) {
87
$userToken = $this->app->request()->headers('X-XSRF-TOKEN');
88
if ($userToken == '') {
89
$userToken = $this->app->request()->params($this->key);
83
$userToken = $this->app->request()->post($this->key);
84
if ($token !== $userToken) {
85
if ($this->app->request->isAjax()) {
86
// Return a JSON error response
87
$this->app->state->Error(__('Sorry the form has expired. Please refresh.'));
88
$this->app->render('response', array('response' => $this->app->state));
89
$this->app->halt(200);
92
if ($token !== $userToken) {
93
throw new TokenExpiredException('Sorry the form has expired. Please refresh.');
93
$this->app->flash('login_message', __('Sorry the form has expired. Please refresh.'));
94
throw new AccessDeniedException();
98
99
// Assign CSRF token key and value to view.
99
$this->app->view()->appendData(array(
100
'csrfKey'=> $this->key,
101
'csrfToken' => $token
100
Theme::Set('csrfKey', $this->key);
101
Theme::Set('csrfToken', $token);
b'\\ No newline at end of file'