Conditions | 31 |
Paths | > 20000 |
Total Lines | 193 |
Code Lines | 118 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
55 | public function config( |
||
56 | ZikulaSecurityCenterModule $securityCenterModule, |
||
57 | Request $request, |
||
58 | RouterInterface $router, |
||
59 | VariableApiInterface $variableApi, |
||
60 | CacheClearer $cacheClearer, |
||
61 | AccessHelper $accessHelper, |
||
62 | string $projectDir |
||
63 | ) { |
||
64 | $modVars = $variableApi->getAll(VariableApi::CONFIG); |
||
65 | |||
66 | $sessionName = $this->getParameter('zikula.session.name'); |
||
67 | $modVars['sessionname'] = $sessionName; |
||
68 | $modVars['idshtmlfields'] = implode(PHP_EOL, $modVars['idshtmlfields']); |
||
69 | $modVars['idsjsonfields'] = implode(PHP_EOL, $modVars['idsjsonfields']); |
||
70 | $modVars['idsexceptions'] = implode(PHP_EOL, $modVars['idsexceptions']); |
||
71 | |||
72 | $form = $this->createForm(ConfigType::class, $modVars); |
||
73 | $form->handleRequest($request); |
||
74 | if ($form->isSubmitted() && $form->isValid()) { |
||
75 | if ($form->get('save')->isClicked()) { |
||
|
|||
76 | $formData = $form->getData(); |
||
77 | |||
78 | $updateCheck = $formData['updatecheck'] ?? 1; |
||
79 | $variableApi->set(VariableApi::CONFIG, 'updatecheck', $updateCheck); |
||
80 | if (0 === $updateCheck) { |
||
81 | // if update checks are disabled, reset values to force new update check if re-enabled |
||
82 | $variableApi->set(VariableApi::CONFIG, 'updateversion', ZikulaKernel::VERSION); |
||
83 | $variableApi->set(VariableApi::CONFIG, 'updatelastchecked', 0); |
||
84 | } |
||
85 | $variableApi->set(VariableApi::CONFIG, 'updatefrequency', $formData['updatefrequency'] ?? 7); |
||
86 | |||
87 | $variableApi->set(VariableApi::CONFIG, 'seclevel', $formData['seclevel'] ?? 'Medium'); |
||
88 | |||
89 | $secMedDays = $formData['secmeddays'] ?? 7; |
||
90 | if ($secMedDays < 1 || $secMedDays > 365) { |
||
91 | $secMedDays = 7; |
||
92 | } |
||
93 | $variableApi->set(VariableApi::CONFIG, 'secmeddays', $secMedDays); |
||
94 | |||
95 | $secInactiveMinutes = $formData['secinactivemins'] ?? 20; |
||
96 | if ($secInactiveMinutes < 1 || $secInactiveMinutes > 1440) { |
||
97 | $secInactiveMinutes = 7; |
||
98 | } |
||
99 | $variableApi->set(VariableApi::CONFIG, 'secinactivemins', $secInactiveMinutes); |
||
100 | |||
101 | $sessionStoreToFile = $formData['sessionstoretofile'] ?? 0; |
||
102 | $sessionSavePath = $formData['sessionsavepath'] ?? ''; |
||
103 | |||
104 | // check session path config is writable (if method is being changed to session file storage) |
||
105 | $causeLogout = false; |
||
106 | $storeTypeCanBeWritten = true; |
||
107 | if (1 === $sessionStoreToFile && !empty($sessionSavePath)) { |
||
108 | // fix path on windows systems |
||
109 | $sessionSavePath = str_replace('\\', '/', $sessionSavePath); |
||
110 | // sanitize the path |
||
111 | $sessionSavePath = trim(stripslashes($sessionSavePath)); |
||
112 | |||
113 | // check if sessionsavepath is a dir and if it is writable |
||
114 | // if yes, we need to logout |
||
115 | $storeTypeCanBeWritten = is_dir($sessionSavePath) ? is_writable($sessionSavePath) : false; |
||
116 | $causeLogout = $storeTypeCanBeWritten; |
||
117 | |||
118 | if (false === $storeTypeCanBeWritten) { |
||
119 | // an error occured - we do not change the way of storing session data |
||
120 | $this->addFlash('error', 'Error! Session path not writeable!'); |
||
121 | $sessionSavePath = ''; |
||
122 | } |
||
123 | } |
||
124 | if (true === $storeTypeCanBeWritten) { |
||
125 | $variableApi->set(VariableApi::CONFIG, 'sessionstoretofile', $sessionStoreToFile); |
||
126 | $variableApi->set(VariableApi::CONFIG, 'sessionsavepath', $sessionSavePath); |
||
127 | } |
||
128 | |||
129 | if ((bool)$sessionStoreToFile !== (bool)$variableApi->getSystemVar('sessionstoretofile')) { |
||
130 | // logout if going from one storage to another one |
||
131 | $causeLogout = true; |
||
132 | } |
||
133 | |||
134 | $newSessionName = $formData['sessionname'] ?? $sessionName; |
||
135 | if (mb_strlen($newSessionName) < 3) { |
||
136 | $newSessionName = $sessionName; |
||
137 | } |
||
138 | |||
139 | // cause logout if we changed session name |
||
140 | if ($newSessionName !== $modVars['sessionname']) { |
||
141 | $causeLogout = true; |
||
142 | } |
||
143 | |||
144 | $configurator = new Configurator($projectDir); |
||
145 | $configurator->loadPackages('zikula_security_center'); |
||
146 | $sessionConfig = $configurator->get('zikula_security_center', 'session'); |
||
147 | $sessionConfig['name'] = $newSessionName; |
||
148 | $sessionConfig['handler_id'] = Constant::SESSION_STORAGE_FILE === $sessionStoreToFile ? 'session.handler.native_file' : 'zikula_core.bridge.http_foundation.doctrine_session_handler'; |
||
149 | $sessionConfig['storage_id'] = Constant::SESSION_STORAGE_FILE === $sessionStoreToFile ? 'zikula_core.bridge.http_foundation.zikula_session_storage_file' : 'zikula_core.bridge.http_foundation.zikula_session_storage_doctrine'; |
||
150 | $sessionConfig['save_path'] = empty($sessionSavePath) ? '%kernel.cache_dir%/sessions' : $sessionSavePath; |
||
151 | $configurator->set('zikula_security_center', 'session', $sessionConfig); |
||
152 | $configurator->write(); |
||
153 | |||
154 | $variableApi->set(VariableApi::CONFIG, 'sessionname', $newSessionName); |
||
155 | $variableApi->set(VariableApi::CONFIG, 'sessionstoretofile', $sessionStoreToFile); |
||
156 | |||
157 | $variableApi->set(VariableApi::CONFIG, 'outputfilter', $formData['outputfilter'] ?? 1); |
||
158 | |||
159 | $useIds = $formData['useids'] ?? 0; |
||
160 | $variableApi->set(VariableApi::CONFIG, 'useids', $useIds); |
||
161 | |||
162 | // create tmp directory for PHPIDS |
||
163 | if (1 === $useIds) { |
||
164 | $idsTmpDir = $this->getParameter('kernel.cache_dir') . '/idsTmp'; |
||
165 | $fs = new Filesystem(); |
||
166 | if (!$fs->exists($idsTmpDir)) { |
||
167 | $fs->mkdir($idsTmpDir); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | $variableApi->set(VariableApi::CONFIG, 'idssoftblock', $formData['idssoftblock'] ?? 1); |
||
172 | $variableApi->set(VariableApi::CONFIG, 'idsmail', $formData['idsmail'] ?? 0); |
||
173 | $variableApi->set(VariableApi::CONFIG, 'idsfilter', $formData['idsfilter'] ?? 'xml'); |
||
174 | |||
175 | $idsRulePath = $formData['idsrulepath'] ?? 'Resources/config/phpids_zikula_default.xml'; |
||
176 | if (is_readable($securityCenterModule->getPath() . '/' . $idsRulePath)) { |
||
177 | $variableApi->set(VariableApi::CONFIG, 'idsrulepath', $idsRulePath); |
||
178 | } else { |
||
179 | $this->addFlash('error', $this->trans('Error! PHPIDS rule file %filePath% does not exist or is not readable.', ['%filePath%' => $idsRulePath])); |
||
180 | } |
||
181 | |||
182 | $variableApi->set(VariableApi::CONFIG, 'idsimpactthresholdone', $formData['idsimpactthresholdone'] ?? 1); |
||
183 | $variableApi->set(VariableApi::CONFIG, 'idsimpactthresholdtwo', $formData['idsimpactthresholdtwo'] ?? 10); |
||
184 | $variableApi->set(VariableApi::CONFIG, 'idsimpactthresholdthree', $formData['idsimpactthresholdthree'] ?? 25); |
||
185 | $variableApi->set(VariableApi::CONFIG, 'idsimpactthresholdfour', $formData['idsimpactthresholdfour'] ?? 75); |
||
186 | |||
187 | $variableApi->set(VariableApi::CONFIG, 'idsimpactmode', $formData['idsimpactmode'] ?? 1); |
||
188 | |||
189 | $idsHtmlFields = $formData['idshtmlfields'] ?? ''; |
||
190 | $idsHtmlFields = explode(PHP_EOL, $idsHtmlFields); |
||
191 | $idsHtmlArray = []; |
||
192 | foreach ($idsHtmlFields as $idsHtmlField) { |
||
193 | $idsHtmlField = trim($idsHtmlField); |
||
194 | if (!empty($idsHtmlField)) { |
||
195 | $idsHtmlArray[] = $idsHtmlField; |
||
196 | } |
||
197 | } |
||
198 | $variableApi->set(VariableApi::CONFIG, 'idshtmlfields', $idsHtmlArray); |
||
199 | |||
200 | $idsJsonFields = $formData['idsjsonfields'] ?? ''; |
||
201 | $idsJsonFields = explode(PHP_EOL, $idsJsonFields); |
||
202 | $idsJsonArray = []; |
||
203 | foreach ($idsJsonFields as $idsJsonField) { |
||
204 | $idsJsonField = trim($idsJsonField); |
||
205 | if (!empty($idsJsonField)) { |
||
206 | $idsJsonArray[] = $idsJsonField; |
||
207 | } |
||
208 | } |
||
209 | $variableApi->set(VariableApi::CONFIG, 'idsjsonfields', $idsJsonArray); |
||
210 | |||
211 | $idsExceptions = $formData['idsexceptions'] ?? ''; |
||
212 | $idsExceptions = explode(PHP_EOL, $idsExceptions); |
||
213 | $idsExceptionsArray = []; |
||
214 | foreach ($idsExceptions as $idsException) { |
||
215 | $idsException = trim($idsException); |
||
216 | if (!empty($idsException)) { |
||
217 | $idsExceptionsArray[] = $idsException; |
||
218 | } |
||
219 | } |
||
220 | $variableApi->set(VariableApi::CONFIG, 'idsexceptions', $idsExceptionsArray); |
||
221 | |||
222 | $variableApi->set(VariableApi::CONFIG, 'idscachingtype', $formData['idscachingtype'] ?? 'none'); |
||
223 | $variableApi->set(VariableApi::CONFIG, 'idscachingexpiration', $formData['idscachingexpiration'] ?? 600); |
||
224 | |||
225 | // clear cache |
||
226 | $cacheClearer->clear('symfony'); |
||
227 | |||
228 | // the module configuration has been updated successfuly |
||
229 | $this->addFlash('status', 'Done! Configuration updated.'); |
||
230 | |||
231 | // we need to auto logout the user if essential session settings have been changed |
||
232 | if (true === $causeLogout) { |
||
233 | $accessHelper->logout(); |
||
234 | $this->addFlash('status', 'Session handling variables have changed. You must log in again.'); |
||
235 | $returnPage = urlencode($router->generate('zikulasecuritycentermodule_config_config')); |
||
236 | |||
237 | return $this->redirectToRoute('zikulausersmodule_access_login', ['returnUrl' => $returnPage]); |
||
238 | } |
||
239 | } elseif ($form->get('cancel')->isClicked()) { |
||
240 | $this->addFlash('status', 'Operation cancelled.'); |
||
241 | } |
||
242 | |||
243 | return $this->redirectToRoute('zikulasecuritycentermodule_config_config'); |
||
244 | } |
||
245 | |||
246 | return [ |
||
247 | 'form' => $form->createView() |
||
248 | ]; |
||
507 |