1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace Install\Controller; |
12
|
|
|
|
13
|
|
|
use Zend\Form\FormElementManager\FormElementManagerV3Polyfill as FormElementManager; |
14
|
|
|
use Zend\Json\Json; |
15
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
16
|
|
|
use Zend\Mvc\MvcEvent; |
17
|
|
|
use Zend\Stdlib\ResponseInterface; |
18
|
|
|
use Zend\View\Model\ViewModel; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Install module main controller. |
22
|
|
|
* |
23
|
|
|
* @author Mathias Gelhausen <[email protected]> |
24
|
|
|
* @todo write test |
25
|
|
|
* @since 0.20 |
26
|
|
|
*/ |
27
|
|
|
class Index extends AbstractActionController |
28
|
|
|
{ |
29
|
|
|
protected $installForm; |
30
|
|
|
|
31
|
|
|
public function __construct(FormElementManager $formElementManager) |
32
|
|
|
{ |
33
|
|
|
$this->installForm = $formElementManager->get('Install/Installation'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Hook for custom preDispatch event. |
38
|
|
|
* |
39
|
|
|
* @param MvcEvent $event |
40
|
|
|
*/ |
41
|
|
|
public function preDispatch(MvcEvent $event) |
42
|
|
|
{ |
43
|
|
|
$this->layout()->setVariable('lang', $this->params('lang')); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$p = $this->params()->fromQuery('p'); |
46
|
|
|
$request = $this->getRequest(); |
47
|
|
|
|
48
|
|
|
if ($p && $request->isXmlHttpRequest()) { |
49
|
|
|
$routeMatch = $event->getRouteMatch(); |
50
|
|
|
$routeMatch->setParam('action', $p); |
51
|
|
|
$response = $this->getResponse(); |
52
|
|
|
$response->getHeaders() |
53
|
|
|
->addHeaderLine('Content-Type', 'application/json') |
54
|
|
|
->addHeaderLine('Content-Encoding', 'utf8'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Index action. |
60
|
|
|
* |
61
|
|
|
* @return ViewModel |
62
|
|
|
*/ |
63
|
|
|
public function indexAction() |
64
|
|
|
{ |
65
|
|
|
// Clear the user identity, if any. (#370) |
66
|
|
|
if (PHP_SESSION_ACTIVE !== session_status()) { session_start(); } |
67
|
|
|
session_destroy(); |
68
|
|
|
|
69
|
|
|
$form = $this->installForm; |
70
|
|
|
$prereqs = $this->plugin('Install/Prerequisites')->check(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return $this->createViewModel( |
73
|
|
|
array( |
74
|
|
|
'prerequisites' => $prereqs, |
75
|
|
|
'form' => $form, |
76
|
|
|
'lang' => $this->params('lang'), |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Action to check prerequisites via ajax request. |
83
|
|
|
* |
84
|
|
|
* @return ViewModel|ResponseInterface |
85
|
|
|
*/ |
86
|
|
|
public function prereqAction() |
87
|
|
|
{ |
88
|
|
|
$prereqs = $this->plugin('Install/Prerequisites')->check(); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$model = $this->createViewModel(array('prerequisites' => $prereqs), true); |
91
|
|
|
$model->setTemplate('install/index/prerequisites.ajax.phtml'); |
92
|
|
|
|
93
|
|
|
return $model; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Main working action. Creates the configuration. |
98
|
|
|
* |
99
|
|
|
* @return ResponseInterface|ViewModel |
100
|
|
|
*/ |
101
|
|
|
public function installAction() |
102
|
|
|
{ |
103
|
|
|
$form = $this->installForm; |
104
|
|
|
$form->setData($_POST); |
105
|
|
|
|
106
|
|
|
if (!$form->isValid()) { |
107
|
|
|
return $this->createJsonResponse( |
108
|
|
|
array( |
109
|
|
|
'ok' => false, |
110
|
|
|
'errors' => $form->getMessages(), |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$data = $form->getData(); |
116
|
|
|
|
117
|
|
|
try{ |
118
|
|
|
$options = [ |
119
|
|
|
'connection' => $data['db_conn'], |
120
|
|
|
]; |
121
|
|
|
$userOk = $this->plugin('Install/UserCreator',$options)->process($data['username'], $data['password'], $data['email']); |
|
|
|
|
122
|
|
|
$ok = $this->plugin('Install/ConfigCreator')->process($data['db_conn'], $data['email']); |
|
|
|
|
123
|
|
|
}catch (\Exception $exception){ |
124
|
|
|
/* @TODO: provide a way to handle global error message */ |
125
|
|
|
return $this->createJsonResponse([ |
126
|
|
|
'ok' => false, |
127
|
|
|
'errors' => [ |
128
|
|
|
'global' => [$exception->getMessage()] |
129
|
|
|
] |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/* |
134
|
|
|
* Make sure there's no cached config files |
135
|
|
|
*/ |
136
|
|
|
$classmapCacheFile = 'cache/module-classmap-cache.module_map.php'; |
137
|
|
|
file_exists($classmapCacheFile) && @unlink($classmapCacheFile); |
138
|
|
|
|
139
|
|
|
$configCacheFile = 'cache/module-config-cache.production.php'; |
140
|
|
|
file_exists($configCacheFile) && @unlink($configCacheFile); |
141
|
|
|
|
142
|
|
|
$model = $this->createViewModel(array('ok' => $ok), true); |
143
|
|
|
$model->setTemplate('install/index/install.ajax.phtml'); |
144
|
|
|
|
145
|
|
|
return $model; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Attaches default listeners to the event manager. |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
protected function attachDefaultListeners() |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
parent::attachDefaultListeners(); |
154
|
|
|
|
155
|
|
|
$events = $this->getEventManager(); |
156
|
|
|
$events->attach( MvcEvent::EVENT_DISPATCH, array( $this, 'preDispatch' ), 100 ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Creates a view model |
161
|
|
|
* |
162
|
|
|
* @param array $params |
163
|
|
|
* @param bool $terminal |
164
|
|
|
* |
165
|
|
|
* @return ViewModel |
166
|
|
|
*/ |
167
|
|
|
protected function createViewModel(array $params, $terminal = false) |
168
|
|
|
{ |
169
|
|
|
if (!isset($params['lang'])) { |
170
|
|
|
$params['lang'] = $this->params('lang'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
$model = new ViewModel($params); |
175
|
|
|
$terminal && $model->setTerminal($terminal); |
176
|
|
|
|
177
|
|
|
return $model; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Create a json response object for ajax requests. |
182
|
|
|
* |
183
|
|
|
* @param array $variables |
184
|
|
|
* |
185
|
|
|
* @return \Zend\Stdlib\ResponseInterface |
186
|
|
|
*/ |
187
|
|
|
protected function createJsonResponse(array $variables) |
188
|
|
|
{ |
189
|
|
|
$response = $this->getResponse(); |
190
|
|
|
$json = Json::encode($variables); |
191
|
|
|
$response->setContent($json); |
192
|
|
|
|
193
|
|
|
return $response; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: