Issues (3627)

InstallBundle/Controller/InstallController.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\InstallBundle\Controller;
13
14
use Doctrine\DBAL\DBALException;
15
use Mautic\CoreBundle\Configurator\Configurator;
16
use Mautic\CoreBundle\Controller\CommonController;
17
use Mautic\InstallBundle\Helper\SchemaHelper;
18
use Mautic\InstallBundle\Install\InstallService;
19
use Symfony\Component\Form\Form;
20
use Symfony\Component\Form\FormError;
21
use Symfony\Component\HttpFoundation\JsonResponse;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
24
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
25
26
class InstallController extends CommonController
27
{
28
    private $configurator;
29
30
    private $installer;
31
32
    /**
33
     * Initialize controller.
34
     */
35
    public function initialize(FilterControllerEvent $event)
36
    {
37
        $this->configurator = $this->container->get('mautic.configurator');
38
        $this->installer    = $this->container->get('mautic.install.service');
39
    }
40
41
    /**
42
     * Controller action for install steps.
43
     *
44
     * @param int $index The step number to process
45
     *
46
     * @return JsonResponse|Response
47
     *
48
     * @throws DBALException
49
     */
50
    public function stepAction($index = 0)
51
    {
52
        // We're going to assume a bit here; if the config file exists already and DB info is provided, assume the app
53
        // is installed and redirect
54
        if ($this->installer->checkIfInstalled()) {
55
            return $this->redirect($this->generateUrl('mautic_dashboard_index'));
56
        }
57
58
        if (false !== strpos($index, '.')) {
59
            [$index, $subIndex] = explode('.', $index);
60
        }
61
62
        $params = $this->configurator->getParameters();
63
64
        $session        = $this->get('session');
65
        $completedSteps = $session->get('mautic.installer.completedsteps', []);
66
67
        // Check to ensure the installer is in the right place
68
        if ((empty($params) || empty($params['db_driver'])) && $index > 1) {
69
            $session->set('mautic.installer.completedsteps', [0]);
70
71
            return $this->redirect($this->generateUrl('mautic_installer_step', ['index' => 1]));
72
        }
73
74
        /** @var \Mautic\CoreBundle\Configurator\Step\StepInterface $step */
75
        $step   = $this->configurator->getStep($index)[0];
76
        $action = $this->generateUrl('mautic_installer_step', ['index' => $index]);
77
78
        $form = $this->createForm($step->getFormType(), $step, ['action' => $action]);
79
        $tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';
80
81
        // Note if this step is complete
82
        $complete = false;
83
84
        if ('POST' === $this->request->getMethod()) {
85
            $form->handleRequest($this->request);
86
            if ($form->isValid()) {
87
                // Post-step processing
88
                $formData = $form->getData();
89
90
                switch ($index) {
91
                    case InstallService::CHECK_STEP:
92
                        $complete = true;
93
94
                        break;
95
                    case InstallService::DOCTRINE_STEP:
96
                        // password field does not retain configured defaults
97
                        if (empty($formData->password) && !empty($params['db_password'])) {
98
                            $formData->password = $params['db_password'];
99
                        }
100
                        $dbParams = (array) $formData;
101
102
                        $messages = $this->installer->createDatabaseStep($step, $dbParams);
103
                        if (is_bool($messages) && true === $messages) {
104
                            // XXX Regression: we used to also get this if database created but configuration not saved
105
                            $schemaHelper             = new SchemaHelper($dbParams);
106
                            $formData->server_version = $schemaHelper->getServerVersion();
107
108
                            // Refresh to install schema with new connection information in the container
109
                            return $this->redirect($this->generateUrl('mautic_installer_step', ['index' => 1.1]));
110
                        } elseif (is_array($messages) && !empty($messages)) {
111
                            $this->handleInstallerErrors($form, $messages);
112
                        }
113
                        break;
114
115
                    case InstallService::USER_STEP:
116
                        $adminParam = (array) $formData;
117
                        $messages   = $this->installer->createAdminUserStep($adminParam);
118
119
                        if (is_bool($messages) && true === $messages) {
120
                            // Store the data to repopulate the form
121
                            unset($formData->password);
122
                            $session->set('mautic.installer.user', $formData);
123
124
                            $complete = true;
125
                        } elseif (is_array($messages) && !empty($messages)) {
126
                            $this->handleInstallerErrors($form, $messages);
127
                        }
128
                        break;
129
130
                    case InstallService::EMAIL_STEP:
131
                        $emailParam = (array) $formData;
132
                        $messages   = $this->installer->setupEmailStep($step, $emailParam);
133
                        if (is_bool($messages)) {
134
                            $complete = $messages;
135
                        } elseif (is_array($messages) && !empty($messages)) {
136
                            $this->handleInstallerErrors($form, $messages);
137
                        }
138
                        break;
139
                }
140
            }
141
        } elseif (!empty($subIndex)) {
142
            switch ($index) {
143
                case InstallService::DOCTRINE_STEP:
144
                    $dbParams = (array) $step;
145
146
                    switch ((int) $subIndex) {
147
                        case 1:
148
                            $messages = $this->installer->createSchemaStep($dbParams);
149
150
                            if (is_bool($messages) && true === $messages) {
151
                                return $this->redirect($this->generateUrl('mautic_installer_step', ['index' => 1.2]));
152
                            } elseif (is_array($messages) && !empty($messages)) {
153
                                $this->handleInstallerErrors($form, $messages);
154
                            }
155
156
                            return $this->redirect($this->generateUrl('mautic_installer_step', ['index' => 1]));
157
                        case 2:
158
                            $messages = $this->installer->createFixturesStep($this->container);
159
160
                            if (is_bool($messages) && true === $messages) {
161
                                $complete = true;
162
                            } elseif (is_array($messages) && !empty($messages)) {
163
                                $this->handleInstallerErrors($form, $messages);
164
165
                                return $this->redirect($this->generateUrl('mautic_installer_step', ['index' => 1]));
166
                            }
167
                            break;
168
                    }
169
                    break;
170
            }
171
        }
172
173
        if ($complete) {
174
            $completedSteps[] = $index;
175
            $session->set('mautic.installer.completedsteps', $completedSteps);
176
            ++$index;
177
178
            if ($index < $this->configurator->getStepCount()) {
179
                // On to the next step
180
181
                return $this->redirect($this->generateUrl('mautic_installer_step', ['index' => (int) $index]));
182
            } else {
183
                $siteUrl  = $this->request->getSchemeAndHttpHost().$this->request->getBaseUrl();
184
                $messages = $this->installer->createFinalConfigStep($siteUrl);
185
186
                if (is_array($messages) && !empty($messages)) {
187
                    $this->handleInstallerErrors($form, $messages);
188
                }
189
190
                return $this->postActionRedirect(
191
                    [
192
                        'viewParameters' => [
193
                            'welcome_url' => $this->generateUrl('mautic_dashboard_index'),
194
                            'parameters'  => $this->configurator->render(),
195
                            'version'     => MAUTIC_VERSION,
196
                            'tmpl'        => $tmpl,
197
                        ],
198
                        'returnUrl'         => $this->generateUrl('mautic_installer_final'),
199
                        'contentTemplate'   => 'MauticInstallBundle:Install:final.html.php',
200
                        'forwardController' => false,
201
                    ]
202
                );
203
            }
204
        } else {
205
            // Redirect back to last step if the user advanced ahead via the URL
206
            $last = (int) end($completedSteps) + 1;
207
            if ($index && $index > $last) {
208
                return $this->redirect($this->generateUrl('mautic_installer_step', ['index' => (int) $last]));
209
            }
210
        }
211
212
        return $this->delegateView(
213
            [
214
                'viewParameters' => [
215
                    'form'           => $form->createView(),
216
                    'index'          => $index,
217
                    'count'          => $this->configurator->getStepCount(),
218
                    'version'        => MAUTIC_VERSION,
219
                    'tmpl'           => $tmpl,
220
                    'majors'         => $this->configurator->getRequirements(),
221
                    'minors'         => $this->configurator->getOptionalSettings(),
222
                    'appRoot'        => $this->container->getParameter('kernel.root_dir'),
223
                    'cacheDir'       => $this->container->getParameter('kernel.cache_dir'),
224
                    'logDir'         => $this->container->getParameter('kernel.logs_dir'),
225
                    'configFile'     => $this->get('mautic.helper.paths')->getSystemPath('local_config'),
226
                    'completedSteps' => $completedSteps,
227
                ],
228
                'contentTemplate' => $step->getTemplate(),
229
                'passthroughVars' => [
230
                    'route' => $this->generateUrl('mautic_installer_step', ['index' => $index]),
231
                ],
232
            ]
233
        );
234
    }
235
236
    /**
237
     * Controller action for the final step.
238
     *
239
     * @return JsonResponse|Response
240
     *
241
     * @throws \Exception
242
     */
243
    public function finalAction()
244
    {
245
        $session = $this->get('session');
246
247
        // We're going to assume a bit here; if the config file exists already and DB info is provided, assume the app is installed and redirect
248
        if ($this->installer->checkIfInstalled()) {
249
            if (!$session->has('mautic.installer.completedsteps')) {
250
                // Arrived here by directly browsing to URL so redirect to the dashboard
251
252
                return $this->redirect($this->generateUrl('mautic_dashboard_index'));
253
            }
254
        } else {
255
            // Shouldn't have made it to this step without having a successful install
256
            return $this->redirect($this->generateUrl('mautic_installer_home'));
257
        }
258
259
        // Remove installer session variables
260
        $session->remove('mautic.installer.completedsteps');
261
        $session->remove('mautic.installer.user');
262
263
        $this->installer->finalMigrationStep();
264
265
        $welcomeUrl = $this->generateUrl('mautic_dashboard_index');
266
267
        $tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';
268
269
        return $this->delegateView(
270
            [
271
                'viewParameters' => [
272
                    'welcome_url' => $welcomeUrl,
273
                    'parameters'  => $this->configurator->render(),
274
                    'config_path' => $this->get('mautic.helper.paths')->getSystemPath('local_config'),
275
                    'is_writable' => $this->configurator->isFileWritable(),
276
                    'version'     => MAUTIC_VERSION,
277
                    'tmpl'        => $tmpl,
278
                ],
279
                'contentTemplate' => 'MauticInstallBundle:Install:final.html.php',
280
                'passthroughVars' => [
281
                    'activeLink'    => '#mautic_installer_index',
282
                    'mauticContent' => 'installer',
283
                    'route'         => $this->generateUrl('mautic_installer_final'),
284
                ],
285
            ]
286
        );
287
    }
288
289
    /**
290
     * Handle installer errors.
291
     */
292
    private function handleInstallerErrors(Form $form, array $messages)
293
    {
294
        foreach ($messages as $type => $message) {
295
            switch ($type) {
296
                case 'warning':
297
                case 'error':
298
                case 'notice':
299
                    $this->addFlash($message, [], $type);
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

299
                    /** @scrutinizer ignore-deprecated */ $this->addFlash($message, [], $type);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
300
                    break;
301
                default:
302
                    // If type not a flash type, assume form field error
303
                    $form[$type]->addError(new FormError($message));
304
                    break;
305
            }
306
        }
307
    }
308
}
309