|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ZfcUser\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Mvc\Application; |
|
6
|
|
|
use Zend\Mvc\Router\RouteInterface; |
|
7
|
|
|
use Zend\Mvc\Router\Exception; |
|
8
|
|
|
use Zend\Http\PhpEnvironment\Response; |
|
9
|
|
|
use ZfcUser\Options\ModuleOptions; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Buils a redirect response based on the current routing and parameters |
|
13
|
|
|
*/ |
|
14
|
|
|
class RedirectCallback |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** @var RouteInterface */ |
|
18
|
|
|
private $router; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Application */ |
|
21
|
|
|
private $application; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ModuleOptions */ |
|
24
|
|
|
private $options; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Application $application |
|
28
|
|
|
* @param RouteInterface $router |
|
29
|
|
|
* @param ModuleOptions $options |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Application $application, RouteInterface $router, ModuleOptions $options) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->router = $router; |
|
34
|
|
|
$this->application = $application; |
|
35
|
|
|
$this->options = $options; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return Response |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __invoke() |
|
42
|
|
|
{ |
|
43
|
|
|
$routeMatch = $this->application->getMvcEvent()->getRouteMatch(); |
|
44
|
|
|
$redirect = $this->getRedirect($routeMatch->getMatchedRouteName(), $this->getRedirectRouteFromRequest()); |
|
45
|
|
|
|
|
46
|
|
|
$response = $this->application->getResponse(); |
|
47
|
|
|
$response->getHeaders()->addHeaderLine('Location', $redirect); |
|
48
|
|
|
$response->setStatusCode(302); |
|
49
|
|
|
return $response; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return the redirect from param. |
|
54
|
|
|
* First checks GET then POST |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
private function getRedirectRouteFromRequest() |
|
58
|
|
|
{ |
|
59
|
|
|
$request = $this->application->getRequest(); |
|
60
|
|
|
$redirect = $request->getQuery('redirect'); |
|
61
|
|
|
if ($redirect && $this->routeExists($redirect)) { |
|
62
|
|
|
return $redirect; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$redirect = $request->getPost('redirect'); |
|
66
|
|
|
if ($redirect && $this->routeExists($redirect)) { |
|
67
|
|
|
return $redirect; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $route |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
private function routeExists($route) |
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
|
|
$this->router->assemble(array(), array('name' => $route)); |
|
81
|
|
|
} catch (Exception\RuntimeException $e) { |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns the url to redirect to based on current route. |
|
89
|
|
|
* If $redirect is set and the option to use redirect is set to true, it will return the $redirect url. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $currentRoute |
|
92
|
|
|
* @param bool $redirect |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function getRedirect($currentRoute, $redirect = false) |
|
96
|
|
|
{ |
|
97
|
|
|
$useRedirect = $this->options->getUseRedirectParameterIfPresent(); |
|
98
|
|
|
$routeExists = ($redirect && $this->routeExists($redirect)); |
|
99
|
|
|
if (!$useRedirect || !$routeExists) { |
|
100
|
|
|
$redirect = false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
switch ($currentRoute) { |
|
104
|
|
|
case 'zfcuser/register': |
|
105
|
|
|
case 'zfcuser/login': |
|
106
|
|
|
case 'zfcuser/authenticate': |
|
107
|
|
|
$route = ($redirect) ?: $this->options->getLoginRedirectRoute(); |
|
108
|
|
|
return $this->router->assemble(array(), array('name' => $route)); |
|
109
|
|
|
break; |
|
110
|
|
|
case 'zfcuser/logout': |
|
111
|
|
|
$route = ($redirect) ?: $this->options->getLogoutRedirectRoute(); |
|
112
|
|
|
return $this->router->assemble(array(), array('name' => $route)); |
|
113
|
|
|
break; |
|
114
|
|
|
default: |
|
115
|
|
|
return $this->router->assemble(array(), array('name' => 'zfcuser')); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|