|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\Bundle\CoreInstallerBundle\Helper; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
17
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
22
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
23
|
|
|
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel; |
|
24
|
|
|
use Zikula\Bundle\CoreBundle\Response\PlainResponse; |
|
25
|
|
|
use Zikula\Bundle\CoreBundle\YamlDumper; |
|
26
|
|
|
use Zikula\Component\Wizard\AbortStageException; |
|
27
|
|
|
use Zikula\Component\Wizard\FormHandlerInterface; |
|
28
|
|
|
use Zikula\Component\Wizard\StageInterface; |
|
29
|
|
|
use Zikula\Component\Wizard\Wizard; |
|
30
|
|
|
use Zikula\Component\Wizard\WizardCompleteInterface; |
|
31
|
|
|
|
|
32
|
|
|
class ControllerHelper |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var ContainerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $container; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var RouterInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $router; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var TranslatorInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $translator; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
ContainerInterface $container, |
|
51
|
|
|
RouterInterface $router, |
|
52
|
|
|
TranslatorInterface $translator |
|
53
|
|
|
) { |
|
54
|
|
|
$this->container = $container; |
|
55
|
|
|
$this->router = $router; |
|
56
|
|
|
$this->translator = $translator; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return an array of variables to assign to all installer templates. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getTemplateGlobals(StageInterface $currentStage): array |
|
63
|
|
|
{ |
|
64
|
|
|
$globals = [ |
|
65
|
|
|
'version' => ZikulaKernel::VERSION, |
|
66
|
|
|
'currentstage' => $currentStage->getName() |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
return array_merge($globals, $currentStage->getTemplateParams()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set up PHP for Zikula installation. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function initPhp(): array |
|
76
|
|
|
{ |
|
77
|
|
|
$warnings = []; |
|
78
|
|
|
if (false === ini_set('default_charset', 'UTF-8')) { |
|
79
|
|
|
$currentSetting = ini_get('default_charset'); |
|
80
|
|
|
$warnings[] = $this->translator->trans('Could not use %command% to set the %setting% to the value of %requiredValue%. The install or upgrade process may fail at your current setting of %currentValue%.', [ |
|
81
|
|
|
'%command%' => 'ini_set', |
|
82
|
|
|
'%setting%' => 'default_charset', |
|
83
|
|
|
'%requiredValue%' => 'UTF-8', |
|
84
|
|
|
'%currentValue%' => $currentSetting |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
if (false === mb_regex_encoding('UTF-8')) { |
|
88
|
|
|
$currentSetting = mb_regex_encoding(); |
|
89
|
|
|
$warnings[] = $this->translator->trans('Could not set %setting% to the value of %requiredValue%. The install or upgrade process may fail at your current setting of %currentValue%.', [ |
|
90
|
|
|
'%setting%' => 'mb_regex_encoding', |
|
91
|
|
|
'%requiredValue%' => 'UTF-8', |
|
92
|
|
|
'%currentValue%' => $currentSetting |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
if (false === ini_set('memory_limit', '128M')) { |
|
96
|
|
|
$currentSetting = ini_get('memory_limit'); |
|
97
|
|
|
$warnings[] = $this->translator->trans('Could not use %command% to set the %setting% to the value of %requiredValue%. The install or upgrade process may fail at your current setting of %currentValue%.', [ |
|
98
|
|
|
'%command%' => 'ini_set', |
|
99
|
|
|
'%setting%' => 'memory_limit', |
|
100
|
|
|
'%requiredValue%' => '128M', |
|
101
|
|
|
'%currentValue%' => $currentSetting |
|
102
|
|
|
]); |
|
103
|
|
|
} |
|
104
|
|
|
if (false === ini_set('max_execution_time', '86400')) { |
|
105
|
|
|
// 86400 = 24 hours |
|
106
|
|
|
$currentSetting = ini_get('max_execution_time'); |
|
107
|
|
|
if ($currentSetting > 0) { |
|
108
|
|
|
// 0 = unlimited time |
|
109
|
|
|
$warnings[] = $this->translator->trans('Could not use %command% to set the %setting% to the value of %requiredValue%. The install or upgrade process may fail at your current setting of %currentValue%.', [ |
|
110
|
|
|
'%command%' => 'ini_set', |
|
111
|
|
|
'%setting%' => 'max_execution_time', |
|
112
|
|
|
'%requiredValue%' => '86400', |
|
113
|
|
|
'%currentValue%' => $currentSetting |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $warnings; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return array|bool |
|
123
|
|
|
*/ |
|
124
|
|
|
public function requirementsMet() |
|
125
|
|
|
{ |
|
126
|
|
|
// several other requirements are checked before Symfony is loaded. |
|
127
|
|
|
// @see /var/SymfonyRequirements.php |
|
128
|
|
|
// @see \Zikula\Bundle\CoreInstallerBundle\Util\ZikulaRequirements::runSymfonyChecks |
|
129
|
|
|
$results = []; |
|
130
|
|
|
|
|
131
|
|
|
$x = explode('.', str_replace('-', '.', PHP_VERSION)); |
|
132
|
|
|
$phpVersion = "{$x[0]}.{$x[1]}.{$x[2]}"; |
|
133
|
|
|
$results['phpsatisfied'] = version_compare($phpVersion, ZikulaKernel::PHP_MINIMUM_VERSION, '>='); |
|
134
|
|
|
$results['pdo'] = extension_loaded('pdo'); |
|
135
|
|
|
$supportsUnicode = preg_match('/^\p{L}+$/u', 'TheseAreLetters'); |
|
136
|
|
|
$results['pcreUnicodePropertiesEnabled'] = (isset($supportsUnicode) && (bool)$supportsUnicode); |
|
137
|
|
|
$requirementsMet = true; |
|
138
|
|
|
foreach ($results as $check) { |
|
139
|
|
|
if (!$check) { |
|
140
|
|
|
$requirementsMet = false; |
|
141
|
|
|
break; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
if ($requirementsMet) { |
|
145
|
|
|
return true; |
|
146
|
|
|
} |
|
147
|
|
|
$results['phpversion'] = PHP_VERSION; |
|
148
|
|
|
$results['phpcoreminversion'] = ZikulaKernel::PHP_MINIMUM_VERSION; |
|
149
|
|
|
|
|
150
|
|
|
return $results; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Write admin credentials to param file as encoded values. |
|
155
|
|
|
* |
|
156
|
|
|
* @throws AbortStageException |
|
157
|
|
|
*/ |
|
158
|
|
|
public function writeEncodedAdminCredentials(YamlDumper $yamlManager, array $data = []): void |
|
159
|
|
|
{ |
|
160
|
|
|
foreach ($data as $k => $v) { |
|
161
|
|
|
$data[$k] = base64_encode($v); // encode so values are 'safe' for json |
|
162
|
|
|
} |
|
163
|
|
|
$params = array_merge($yamlManager->getParameters(), $data); |
|
164
|
|
|
try { |
|
165
|
|
|
$yamlManager->setParameters($params); |
|
166
|
|
|
} catch (IOException $exception) { |
|
167
|
|
|
throw new AbortStageException($this->translator->trans('Cannot write parameters to %fileName% file.', ['%fileName%' => 'services_custom.yaml'])); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Executes the wizard for installation or upgrade process. |
|
173
|
|
|
*/ |
|
174
|
|
|
public function processWizard(Request $request, $stage, $mode = 'install', YamlDumper $yamlDumper = null): Response |
|
175
|
|
|
{ |
|
176
|
|
|
if (!in_array($mode, ['install', 'upgrade'])) { |
|
177
|
|
|
$mode = 'install'; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$session = $request->hasSession() ? $request->getSession() : null; |
|
181
|
|
|
|
|
182
|
|
|
// check php |
|
183
|
|
|
$iniWarnings = $this->initPhp(); |
|
184
|
|
|
if (null !== $session && 0 < count($iniWarnings)) { |
|
185
|
|
|
$session->getFlashBag()->add('warning', implode('<hr />', $iniWarnings)); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// begin the wizard |
|
189
|
|
|
$wizard = new Wizard($this->container, dirname(__DIR__) . '/Resources/config/' . $mode . '_stages.yaml'); |
|
190
|
|
|
$currentStage = $wizard->getCurrentStage($stage); |
|
191
|
|
|
if ($currentStage instanceof WizardCompleteInterface) { |
|
192
|
|
|
if ('upgrade' === $mode && null !== $yamlDumper) { |
|
193
|
|
|
$yamlDumper->setParameter('upgrading', false); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $currentStage->getResponse($request); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$templateParams = $this->getTemplateGlobals($currentStage); |
|
200
|
|
|
$templateParams['headertemplate'] = '@ZikulaCoreInstaller/' . $mode . 'Header.html.twig'; |
|
201
|
|
|
if ($wizard->isHalted()) { |
|
202
|
|
|
if (null !== $session) { |
|
203
|
|
|
$session->getFlashBag()->add('danger', $wizard->getWarning()); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $this->renderResponse('@ZikulaCoreInstaller/error.html.twig', $templateParams); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// handle the form |
|
210
|
|
|
if ($currentStage instanceof FormHandlerInterface) { |
|
211
|
|
|
$form = $this->form->create($currentStage->getFormType(), null, $currentStage->getFormOptions()); |
|
|
|
|
|
|
212
|
|
|
$form->handleRequest($request); |
|
213
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
214
|
|
|
$currentStage->handleFormResult($form); |
|
215
|
|
|
$params = [ |
|
216
|
|
|
'stage' => $wizard->getNextStage()->getName(), |
|
217
|
|
|
'_locale' => $this->container->getParameter('locale') |
|
218
|
|
|
]; |
|
219
|
|
|
|
|
220
|
|
|
$url = $this->router->generate($mode, $params); |
|
221
|
|
|
|
|
222
|
|
|
return new RedirectResponse($url); |
|
223
|
|
|
} |
|
224
|
|
|
$templateParams['form'] = $form->createView(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $this->renderResponse($currentStage->getTemplateName(), $templateParams); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
protected function renderResponse(string $view, array $parameters = [], Response $response = null): Response |
|
231
|
|
|
{ |
|
232
|
|
|
if (null === $response) { |
|
233
|
|
|
$response = new PlainResponse(); |
|
234
|
|
|
} |
|
235
|
|
|
$response->setContent($this->container->get('twig')->render($view, $parameters)); |
|
236
|
|
|
|
|
237
|
|
|
return $response; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|