This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
FormiculaModuleInstaller.php
254 lines (224 loc) · 7.99 KB
/
FormiculaModuleInstaller.php
1
2
3
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
<?php
declare(strict_types=1);
/*
* This file is part of the Formicula package.
*
* Copyright Formicula Development Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\FormiculaModule;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Zikula\Bundle\CoreBundle\Doctrine\Helper\SchemaHelper;
use Zikula\ExtensionsModule\AbstractExtension;
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
use Zikula\FormiculaModule\Entity\ContactEntity;
use Zikula\FormiculaModule\Entity\SubmissionEntity;
class FormiculaModuleInstaller extends AbstractExtensionInstaller
{
/**
* @var array
*/
private $entities = [
ContactEntity::class,
SubmissionEntity::class
];
/**
* @var string
*/
private $cacheDirectory;
/**
* @var string
*/
private $uploadDirectory;
public function __construct(
AbstractExtension $extension,
ManagerRegistry $managerRegistry,
SchemaHelper $schemaTool,
RequestStack $requestStack,
TranslatorInterface $translator,
VariableApiInterface $variableApi,
string $cacheDir
) {
parent::__construct($extension, $managerRegistry, $schemaTool, $requestStack, $translator, $variableApi);
$this->cacheDirectory = $cacheDir;
$this->uploadDirectory = str_replace('cache', 'uploads', $cacheDir);
}
public function install(): bool
{
$this->schemaTool->create($this->entities);
$variableApi = $this->getVariableApi();
// create a contact for the webmaster
$contact = new ContactEntity();
$contact->setName($this->trans('Webmaster'));
$contact->setEmail($variableApi->get('ZConfig', 'adminmail'));
$contact->setPublic(true);
$contact->setSenderName($contact->getName());
$contact->setSenderEmail($contact->getEmail());
$contact->setSendingSubject($this->trans('Your mail to %s'));
$this->entityManager->persist($contact);
$this->entityManager->flush();
// try to create required directories
$this->createCacheDirectory();
$this->createUploadDirectory();
$this->setVars($this->getDefaultSettings());
// initialisation successful
return true;
}
public function upgrade($oldVersion): bool
{
switch ($oldVersion) {
case '4.0.0':
case '4.0.1':
// nothing to do
case '4.0.2':
// fields have changed: createdUserId becomes createdBy, updatedUserId becomes updatedBy
$connection = $this->entityManager->getConnection();
$tableName = 'submission';
$sql = '
ALTER TABLE `formicula_' . $tableName . '`
CHANGE `createdUserId` `createdBy` INT(11) DEFAULT NULL
';
$stmt = $connection->prepare($sql);
$stmt->execute();
$sql = '
ALTER TABLE `formicula_' . $tableName . '`
CHANGE `updatedUserId` `updatedBy` INT(11) DEFAULT NULL
';
$stmt = $connection->prepare($sql);
$stmt->execute();
// no break
case '5.0.0':
// added forgotten company field
try {
$this->schemaTool->update([
SubmissionEntity::class
]);
} catch (\Exception $exception) {
$this->addFlash('error', $this->trans('Doctrine Exception') . ': ' . $exception->getMessage());
return false;
}
case '5.0.1':
$this->setVar('uploadDirectory', $this->uploadDirectory);
// no break
case '5.0.2':
// nothing yet
}
// Update successful
return true;
}
public function uninstall(): bool
{
$this->schemaTool->drop($this->entities);
$this->delVars();
if (is_dir($this->cacheDirectory)) {
$fs = new Filesystem();
try {
$fs->remove($this->cacheDirectory);
} catch (IOExceptionInterface $exception) {
$this->addFlash(
'error',
$this->trans(
'An error occurred while removing the cache directory at %s%.',
['%s%' => $this->cacheDirectory]
)
);
}
}
// upload directory is currently not deleted, since the files may still be of interest
return true;
}
private function createCacheDirectory(): void
{
$fs = new Filesystem();
try {
if (!$fs->exists($this->cacheDirectory)) {
$fs->mkdir($this->cacheDirectory);
$fs->chmod($this->cacheDirectory, 0777);
}
} catch (IOExceptionInterface $exception) {
$this->addFlash(
'error',
$this->trans(
'An error occurred while creating the cache directory at %s%.',
['%s%' => $this->cacheDirectory]
)
);
}
try {
if ($fs->exists($this->cacheDirectory . '/.htaccess')) {
return;
}
$fs->dumpFile($this->cacheDirectory . '/.htaccess', 'SetEnvIf Request_URI "\.gif$" object_is_gif=gif
SetEnvIf Request_URI "\.png$" object_is_png=png
SetEnvIf Request_URI "\.jpg$" object_is_jpg=jpg
SetEnvIf Request_URI "\.jpeg$" object_is_jpeg=jpeg
Order deny,allow
Deny from all
Allow from env=object_is_gif
Allow from env=object_is_png
Allow from env=object_is_jpg
Allow from env=object_is_jpeg
');
$this->addFlash(
'status',
$this->trans('Successfully created the cache directory with a .htaccess file in it.')
);
} catch (IOExceptionInterface $exception) {
$this->addFlash(
'error',
$this->trans(
'Could not create .htaccess file in %s%, please refer to the manual before using the module!',
['%s%' => $this->cacheDirectory]
)
);
}
}
private function createUploadDirectory(): void
{
$fs = new Filesystem();
try {
if (!$fs->exists($this->uploadDirectory)) {
$fs->mkdir($this->uploadDirectory);
$fs->chmod($this->uploadDirectory, 0777);
}
} catch (IOExceptionInterface $exception) {
$this->addFlash(
'error',
$this->trans(
'An error occurred while creating the upload directory at %s%.',
['%s%' => $this->uploadDirectory]
)
);
}
}
private function getDefaultSettings(): array
{
return [
'defaultForm' => 0,
'showCompany' => true,
'showPhone' => true,
'showUrl' => true,
'showLocation' => true,
'showComment' => true,
'showFileAttachment' => false,
'uploadDirectory' => $this->uploadDirectory,
'deleteUploadedFiles' => true,
'sendConfirmationToUser' => true,
'defaultAdminFormat' => 'html',
'defaultUserFormat' => 'html',
'showUserFormat' => true,
'useContactsAsSender' => true,
'enableSpamCheck' => true,
'excludeSpamCheck' => '',
'storeSubmissionData' => false,
'storeSubmissionDataForms' => ''
];
}
}