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
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?php

declare(strict_types=1);

namespace Mautic\InstallBundle\Install;

use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Configurator\Configurator;
use Mautic\CoreBundle\Configurator\Step\StepInterface;
use Mautic\CoreBundle\Doctrine\Loader\FixturesLoaderInterface;
use Mautic\CoreBundle\Helper\CacheHelper;
use Mautic\CoreBundle\Helper\EncryptionHelper;
use Mautic\CoreBundle\Helper\InputHelper;
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\CoreBundle\Loader\ParameterLoader;
use Mautic\CoreBundle\Release\ThisRelease;
use Mautic\InstallBundle\Configurator\Step\DoctrineStep;
use Mautic\InstallBundle\Exception\AlreadyInstalledException;
use Mautic\InstallBundle\Exception\DatabaseVersionTooOldException;
use Mautic\InstallBundle\Helper\SchemaHelper;
use Mautic\UserBundle\Entity\Role;
use Mautic\UserBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class InstallService
{
    public const CHECK_STEP = 0;

    public const DOCTRINE_STEP = 1;

    public const USER_STEP = 2;

    public const FINAL_STEP = 3;

    public function __construct(
        private Configurator $configurator,
        private CacheHelper $cacheHelper,
        protected PathsHelper $pathsHelper,
        private EntityManager $entityManager,
        private TranslatorInterface $translator,
        private KernelInterface $kernel,
        private ValidatorInterface $validator,
        private UserPasswordHasher $hasher,
        private FixturesLoaderInterface $fixturesLoader
    ) {
    }

    /**
     * Get step object for given index or appropriate step index.
     *
     * @param int $index The step number to retrieve
     *
     * @return StepInterface the valid step given installation status
     *
     * @throws \InvalidArgumentException|AlreadyInstalledException
     */
    public function getStep(int $index = 0): StepInterface
    {
        // 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
        if ($this->checkIfInstalled()) {
            throw new AlreadyInstalledException();
        }

        $params = $this->configurator->getParameters();

        // Check to ensure the installer is in the right place
        if ((empty($params)
                || !isset($params['db_driver'])
                || empty($params['db_driver'])) && $index > 1) {
            return $this->configurator->getStep(self::DOCTRINE_STEP)[0];
        }

        return $this->configurator->getStep($index)[0];
    }

    /**
     * Get local config file location.
     */
    private function localConfig(): string
    {
        return ParameterLoader::getLocalConfigFile($this->pathsHelper->getSystemPath('root').'/app');
    }

    /**
     * Get local config parameters.
     */
    public function localConfigParameters(): array
    {
        $localConfigFile = $this->localConfig();

        if (file_exists($localConfigFile)) {
            /** @var array $parameters */
            $parameters = [];

            // Load local config to override parameters
            include $localConfigFile;
            $localParameters = $parameters;
        } else {
            $localParameters = [];
        }

        return $localParameters;
    }

    /**
     * Checks if the application has been installed and redirects if so.
     */
    public function checkIfInstalled(): bool
    {
        // If the config file doesn't even exist, no point in checking further
        $localConfigFile = $this->localConfig();
        if (!file_exists($localConfigFile)) {
            return false;
        }

        $params = $this->configurator->getParameters();

        // if db_driver and site_url are present then it is assumed all the steps of the installation have been
        // performed; manually deleting these values or deleting the config file will be required to re-enter
        // installation.
        if (empty($params['db_driver']) || empty($params['site_url'])) {
            return false;
        }

        return true;
    }

    /**
     * Translation messages array.
     */
    private function translateMessages(array $messages): array
    {
        if (empty($messages)) {
            return $messages;
        }

        foreach ($messages as $key => $value) {
            $messages[$key] = $this->translator->trans($value);
        }

        return $messages;
    }

    /**
     * Checks for step's requirements.
     */
    public function checkRequirements(StepInterface $step): array
    {
        $messages = $step->checkRequirements();

        return $this->translateMessages($messages);
    }

    /**
     * Checks for step's optional settings.
     */
    public function checkOptionalSettings(StepInterface $step): array
    {
        $messages = $step->checkOptionalSettings();

        return $this->translateMessages($messages);
    }

    public function saveConfiguration($params, StepInterface $step = null, $clearCache = false): array
    {
        if ($step instanceof StepInterface) {
            $params = $step->update($step);
        }

        $this->configurator->mergeParameters($params);

        $messages = [];
        try {
            $this->configurator->write();
        } catch (\RuntimeException) {
            $messages = [
                'error' => $this->translator->trans(
                    'mautic.installer.error.writing.configuration',
                    [],
                    'flashes'
                ),
            ];
        }

        if ($clearCache) {
            $this->cacheHelper->refreshConfig();
        }

        return $messages;
    }

    /**
     * @return array Validation errors
     */
    public function validateDatabaseParams(array $dbParams): array
    {
        $required = [
            'driver',
            'host',
            'name',
            'user',
        ];

        $messages = [];
        foreach ($required as $r) {
            if (!isset($dbParams[$r]) || empty($dbParams[$r])) {
                $messages[$r] = $this->translator->trans(
                    'mautic.core.value.required',
                    [],
                    'validators'
                );
            }
        }

        if (!isset($dbParams['port']) || (int) $dbParams['port'] <= 0) {
            $messages['port'] = $this->translator->trans(
                'mautic.install.database.port.invalid',
                [],
                'validators'
            );
        }

        if (!empty($dbParams['driver']) && !in_array($dbParams['driver'], DoctrineStep::getDriverKeys())) {
            $messages['driver'] = $this->translator->trans(
                'mautic.install.database.driver.invalid',
                ['%drivers%' => implode(', ', DoctrineStep::getDriverKeys())],
                'validators'
            );
        }

        return $messages;
    }

    /**
     * Create the database.
     */
    public function createDatabaseStep(StepInterface $step, array $dbParams): array
    {
        $messages = $this->validateDatabaseParams($dbParams);

        if (!empty($messages)) {
            return $messages;
        }

        // Check if connection works and/or create database if applicable
        $schemaHelper = new SchemaHelper($dbParams);

        try {
            $schemaHelper->testConnection();
            $schemaHelper->validateDatabaseVersion();

            if ($schemaHelper->createDatabase()) {
                $messages = $this->saveConfiguration($dbParams, $step, true);
                if (empty($messages)) {
                    return $messages;
                }
            }

            $messages['error'] = $this->translator->trans(
                'mautic.installer.error.creating.database',
                ['%name%' => $dbParams['name']],
                'flashes'
            );
        } catch (DatabaseVersionTooOldException $e) {
            $metadata = ThisRelease::getMetadata();

            $messages['error'] = $this->translator->trans(
                'mautic.installer.error.database.version',
                [
                    '%currentversion%'    => $e->getCurrentVersion(),
                    '%mysqlminversion%'   => $metadata->getMinSupportedMySqlVersion(),
                    '%mariadbminversion%' => $metadata->getMinSupportedMariaDbVersion(),
                ],
                'flashes'
            );
        } catch (\Exception $exception) {
            $messages['error'] = $this->translator->trans(
                'mautic.installer.error.connecting.database',
                ['%exception%' => $exception->getMessage()],
                'flashes'
            );
        }

        return $messages;
    }

    /**
     * Create the database schema.
     */
    public function createSchemaStep(array $dbParams): array
    {
        $schemaHelper = new SchemaHelper($dbParams);
        $schemaHelper->setEntityManager($this->entityManager);

        $messages = [];
        try {
            if (!$schemaHelper->installSchema()) {
                $messages['error'] = $this->translator->trans(
                    'mautic.installer.error.no.metadata',
                    [],
                    'flashes'
                );
            }
        } catch (\Exception $exception) {
            $messages['error'] = $this->translator->trans(
                'mautic.installer.error.installing.data',
                ['%exception%' => $exception->getMessage()],
                'flashes'
            );
        }

        return $messages;
    }

    /**
     * Load the database fixtures in the database.
     */
    public function createFixturesStep(): array
    {
        $messages = [];

        try {
            $this->installDatabaseFixtures();
        } catch (\Exception $exception) {
            $messages['error'] = $this->translator->trans(
                'mautic.installer.error.adding.fixtures',
                ['%exception%' => $exception->getMessage()],
                'flashes'
            );
        }

        return $messages;
    }

    /**
     * Installs data fixtures for the application.
     *
     * @throws \InvalidArgumentException
     */
    public function installDatabaseFixtures(): void
    {
        $fixtures = $this->fixturesLoader->getFixtures(['group_install']);

        if (!$fixtures) {
            throw new \InvalidArgumentException('Could not find any fixtures to load with the "group_install" group.');
        }

        $purger = new ORMPurger($this->entityManager);
        $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE);
        $executor = new ORMExecutor($this->entityManager, $purger);
        /*
         * FIXME entity manager does not load configuration if local.php just created by CLI install
         * [error] An error occurred while attempting to add default data
         * An exception occured in driver:
         * SQLSTATE[HY000] [1045] Access refused for user: ''@'@localhost' (mot de passe: NON)
         */
        $executor->execute($fixtures, true);
    }

    /**
     * Create the administrator user.
     */
    public function createAdminUserStep(array $data): array
    {
        $entityManager = $this->entityManager;

        // ensure the username and email are unique
        try {
            /** @var User $existingUser */
            $existingUser = $entityManager->getRepository(User::class)->find(1);
        } catch (\Exception) {
            $existingUser = null;
        }

        if (null !== $existingUser) {
            $user = $existingUser;
        } else {
            $user = new User();
        }

        $required = [
            'firstname',
            'lastname',
            'username',
            'email',
            'password',
        ];

        $messages = [];
        foreach ($required as $r) {
            if (!isset($data[$r])) {
                $messages[$r] = $this->translator->trans(
                    'mautic.core.value.required',
                    [],
                    'validators'
                );
            }
        }

        if (!empty($messages)) {
            return $messages;
        }

        $validations = [];

        $emailConstraint          = new Assert\Email();
        $emailConstraint->message = $this->translator->trans('mautic.core.email.required', [], 'validators');

        $passwordConstraint             = new Assert\Length(['min' => 6]);
        $passwordConstraint->minMessage = $this->translator->trans('mautic.install.password.minlength', [], 'validators');

        $validations[] = $this->validator->validate($data['email'], $emailConstraint);
        $validations[] = $this->validator->validate($data['password'], $passwordConstraint);

        $messages = [];
        foreach ($validations as $errors) {
            foreach ($errors as $error) {
                $messages[] = $error->getMessage();
            }
        }

        if (!empty($messages)) {
            return $messages;
        }

        $hasher = $this->hasher;

        $user->setFirstName(InputHelper::clean($data['firstname']));
        $user->setLastName(InputHelper::clean($data['lastname']));
        $user->setUsername(InputHelper::clean($data['username']));
        $user->setEmail(InputHelper::email($data['email']));
        $user->setPassword($hasher->hashPassword($user, $data['password']));

        $adminRole = null;
        try {
            $adminRole = $entityManager->getReference(Role::class, 1);
        } catch (\Exception $exception) {
            $messages['error'] = $this->translator->trans(
                'mautic.installer.error.getting.role',
                ['%exception%' => $exception->getMessage()],
                'flashes'
            );
        }

        if (!empty($adminRole)) {
            $user->setRole($adminRole);

            try {
                $entityManager->persist($user);
                $entityManager->flush();
            } catch (\Exception $exception) {
                $messages['error'] = $this->translator->trans(
                    'mautic.installer.error.creating.user',
                    ['%exception%' => $exception->getMessage()],
                    'flashes'
                );
            }
        }

        return $messages;
    }

    /**
     * Create the final configuration.
     */
    public function createFinalConfigStep(string $siteUrl): array
    {
        // Merge final things into the config, wipe the container, and we're done!
        $finalConfigVars = [
            'secret_key' => EncryptionHelper::generateKey(),
            'site_url'   => $siteUrl,
        ];

        return $this->saveConfiguration($finalConfigVars, null, true);
    }

    /**
     * Final migration step for install.
     */
    public function finalMigrationStep(): void
    {
        // Add database migrations up to this point since this is a fresh install (must be done at this point
        // after the cache has been rebuilt
        $input  = new ArgvInput(['console', 'doctrine:migrations:version', '--add', '--all', '--no-interaction']);
        $output = new BufferedOutput();

        $application = new Application($this->kernel);
        $application->setAutoExit(false);
        $application->run($input, $output);
    }
}