PluginProbe
User Access Manager / 2.3.10
User Access Manager v2.3.10
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Controller / Backend / SettingsController.php

SettingsController.php in User Access Manager 2.3.10, at src/Controller/Backend/SettingsController.php

498 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Controller\Backend;
6
7 use Exception;
8 use UserAccessManager\Cache\Cache;
9 use UserAccessManager\Config\MainConfig;
10 use UserAccessManager\Config\WordpressConfig;
11 use UserAccessManager\Controller\Controller;
12 use UserAccessManager\File\FileHandler;
13 use UserAccessManager\Form\Form;
14 use UserAccessManager\Form\FormFactory;
15 use UserAccessManager\Form\FormHelper;
16 use UserAccessManager\Form\ValueSetFormElement;
17 use UserAccessManager\Object\ObjectHandler;
18 use UserAccessManager\Wrapper\Php;
19 use UserAccessManager\Wrapper\Wordpress;
20 use WP_Post_Type;
21 use WP_Taxonomy;
22
23 class SettingsController extends Controller
24 {
25 use ControllerTabNavigationTrait;
26
27 public const GROUP_POST_TYPES = 'post_types';
28 public const GROUP_TAXONOMIES = 'taxonomies';
29 public const GROUP_FILES = 'file';
30 public const SECTION_FILES = 'file';
31 public const GROUP_AUTHOR = 'author';
32 public const SECTION_AUTHOR = 'author';
33 public const GROUP_CACHE = 'cache';
34 public const GROUP_OTHER = 'other';
35 public const SECTION_OTHER = 'other';
36
37 protected ?string $template = 'AdminSettings.php';
38
39 public function __construct(
40 Php $php,
41 Wordpress $wordpress,
42 WordpressConfig $wordpressConfig,
43 private MainConfig $mainConfig,
44 private Cache $cache,
45 private FileHandler $fileHandler,
46 private FormFactory $formFactory,
47 private FormHelper $formHelper
48 ) {
49 parent::__construct($php, $wordpress, $wordpressConfig);
50 }
51
52 public function getTabGroups(): array
53 {
54 $activeCacheProvider = $this->mainConfig->getActiveCacheProvider();
55 $cacheProviderSections = [$activeCacheProvider];
56 $cacheProviders = $this->cache->getRegisteredCacheProviders();
57
58 foreach ($cacheProviders as $cacheProvider) {
59 if ($cacheProvider->getId() !== $activeCacheProvider) {
60 $cacheProviderSections[] = $cacheProvider->getId();
61 }
62 }
63
64 if (!in_array(MainConfig::CACHE_PROVIDER_NONE, $cacheProviderSections)) {
65 $cacheProviderSections[] = MainConfig::CACHE_PROVIDER_NONE;
66 }
67
68 return [
69 self::GROUP_POST_TYPES => array_merge([MainConfig::DEFAULT_TYPE], array_keys($this->getPostTypes())),
70 self::GROUP_TAXONOMIES => array_merge([MainConfig::DEFAULT_TYPE], array_keys($this->getTaxonomies())),
71 self::GROUP_FILES => [self::SECTION_FILES],
72 self::GROUP_AUTHOR => [self::SECTION_AUTHOR],
73 self::GROUP_CACHE => $cacheProviderSections,
74 self::GROUP_OTHER => [self::SECTION_OTHER]
75 ];
76 }
77
78
79 private function getPages(): array
80 {
81 $pages = $this->wordpress->getPages('sort_column=menu_order');
82 return is_array($pages) !== false ? $pages : [];
83 }
84
85 /**
86 * @return WP_Post_Type[]
87 */
88 private function getPostTypes(): array
89 {
90 return $this->wordpress->getPostTypes(['public' => true], 'objects');
91 }
92
93 /**
94 * @return WP_Taxonomy[]
95 */
96 private function getTaxonomies(): array
97 {
98 return $this->wordpress->getTaxonomies(['public' => true], 'objects');
99 }
100
101 public function getText(string $key, bool $description = false): string
102 {
103 return $this->formHelper->getText($key, $description);
104 }
105
106 public function getGroupText(string $key): string
107 {
108 return $this->getText($key);
109 }
110
111 public function getGroupSectionText(string $key): string
112 {
113 return ($key === MainConfig::DEFAULT_TYPE) ?
114 TXT_UAM_SETTINGS_GROUP_SECTION_DEFAULT : $this->getObjectName($key);
115 }
116
117 public function getObjectName(string $objectKey): string
118 {
119 $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
120 + $this->wordpress->getTaxonomies(['public' => true], 'objects');
121
122 return (isset($objects[$objectKey]) === true) ? $objects[$objectKey]->labels->name : $objectKey;
123 }
124
125 /**
126 * @throws Exception
127 */
128 private function getPostSettingsForm(string $postType = MainConfig::DEFAULT_TYPE): Form
129 {
130 $textarea = null;
131 $configParameters = $this->mainConfig->getConfigParameters();
132
133 if (isset($configParameters["{$postType}_content"]) === true) {
134 $configParameter = $configParameters["{$postType}_content"];
135 $textarea = $this->formFactory->createTextarea(
136 $configParameter->getId(),
137 $configParameter->getValue(),
138 $this->formHelper->getParameterText($configParameter, false, $postType),
139 $this->formHelper->getParameterText($configParameter, true, $postType)
140 );
141 }
142
143 $parameters = ($postType !== MainConfig::DEFAULT_TYPE) ? ["{$postType}_use_default"] : [];
144 $parameters = array_merge($parameters, [
145 "hide_$postType",
146 "hide_{$postType}_title",
147 "{$postType}_title",
148 $textarea,
149 "hide_{$postType}_comment",
150 "{$postType}_comment_content",
151 "{$postType}_comments_locked",
152 "show_{$postType}_content_before_more"
153 ]);
154
155 return $this->formHelper->getSettingsForm($parameters, $postType);
156 }
157
158 /**
159 * @throws Exception
160 */
161 private function getTaxonomySettingsForm(string $taxonomy = MainConfig::DEFAULT_TYPE): Form
162 {
163 $parameters = ($taxonomy !== MainConfig::DEFAULT_TYPE) ? ["{$taxonomy}_use_default"] : [];
164 $parameters = array_merge($parameters, [
165 "hide_empty_$taxonomy"
166 ]);
167
168 return $this->formHelper->getSettingsForm($parameters, $taxonomy);
169 }
170
171 private function isXSendFileAvailable(): bool
172 {
173 $content = @file_get_contents($this->wordpress->getSiteUrl() . '?testXSendFile');
174 $this->fileHandler->removeXSendFileTestFile();
175
176 return ($content === 'success');
177 }
178
179 private function disableXSendFileOption(Form $form): void
180 {
181 $formElements = $form->getElements();
182
183 if (isset($formElements['download_type']) === true) {
184 /** @var ValueSetFormElement $downloadType */
185 $downloadType = $formElements['download_type'];
186 $possibleValues = $downloadType->getPossibleValues();
187
188 if (isset($possibleValues['xsendfile']) === true) {
189 $possibleValues['xsendfile']->markDisabled();
190 }
191 }
192 }
193
194 private function addLockFileTypes(array $configParameters, array &$parameters): void
195 {
196 if (isset($configParameters['lock_file_types']) === true
197 && $this->wordpress->isNginx() === false
198 ) {
199 $parameters['lock_file_types'] = [
200 'selected' => 'locked_file_types',
201 'not_selected' => 'not_locked_file_types'
202 ];
203
204 if ($this->wordpress->gotModRewrite() === false) {
205 $parameters[] = 'file_pass_type';
206 }
207 }
208 }
209
210 /**
211 * @throws Exception
212 */
213 private function getFilesSettingsForm(): Form
214 {
215 $fileProtectionFileName = $this->fileHandler->getFileProtectionFileName();
216 $fileContent = (file_exists($fileProtectionFileName) === true) ?
217 file_get_contents($fileProtectionFileName) : '';
218
219 $configParameters = $this->mainConfig->getConfigParameters();
220
221 $parameters = [
222 'lock_file',
223 'download_type',
224 'inline_files',
225 'no_access_image_type' => ['custom' => 'custom_no_access_image'],
226 'use_custom_file_handling_file',
227 $this->formFactory->createTextarea(
228 'custom_file_handling_file',
229 $fileContent,
230 TXT_UAM_CUSTOM_FILE_HANDLING_FILE,
231 TXT_UAM_CUSTOM_FILE_HANDLING_FILE_DESC
232 ),
233 'locked_directory_type' => ['custom' => 'custom_locked_directories']
234 ];
235
236 $this->addLockFileTypes($configParameters, $parameters);
237
238 $form = $this->formHelper->getSettingsForm($parameters);
239
240 if ($this->isXSendFileAvailable() === false) {
241 $this->disableXSendFileOption($form);
242 }
243
244 return $form;
245 }
246
247 /**
248 * @throws Exception
249 */
250 private function getAuthorSettingsForm(): Form
251 {
252 $parameters = [
253 'authors_has_access_to_own',
254 'authors_can_add_posts_to_groups',
255 'full_access_role'
256 ];
257
258 return $this->formHelper->getSettingsForm($parameters);
259 }
260
261 private function addCustomPageRedirectFormElement(array $configParameters, array &$values): void
262 {
263 if (isset($configParameters['redirect_custom_page']) === true) {
264 $redirectCustomPage = $configParameters['redirect_custom_page'];
265 $redirectCustomPageValue = $this->formFactory->createMultipleFormElementValue(
266 'custom_page',
267 TXT_UAM_REDIRECT_TO_PAGE
268 );
269
270 $possibleValues = [];
271 $pages = $this->getPages();
272
273 foreach ($pages as $page) {
274 $possibleValues[] = $this->formFactory->createValueSetFromElementValue(
275 (int) $page->ID,
276 $page->post_title
277 );
278 }
279
280 $formElement = $this->formFactory->createSelect(
281 $redirectCustomPage->getId(),
282 $possibleValues,
283 (int) $redirectCustomPage->getValue()
284 );
285
286 try {
287 $redirectCustomPageValue->setSubElement($formElement);
288 $values[] = $redirectCustomPageValue;
289 } catch (Exception) {
290 }
291 }
292 }
293
294 /**
295 * @throws Exception
296 */
297 private function getOtherSettingsForm(): Form
298 {
299 $redirect = null;
300 $configParameters = $this->mainConfig->getConfigParameters();
301
302 if (isset($configParameters['redirect'])) {
303 $values = [
304 $this->formFactory->createMultipleFormElementValue('false', TXT_UAM_NO),
305 $this->formFactory->createMultipleFormElementValue('blog', TXT_UAM_REDIRECT_TO_BLOG),
306 $this->formFactory->createMultipleFormElementValue('login', TXT_UAM_REDIRECT_TO_LOGIN)
307 ];
308
309 $this->addCustomPageRedirectFormElement($configParameters, $values);
310
311 if (isset($configParameters['redirect_custom_url']) === true) {
312 try {
313 $values[] = $this->formHelper->createMultipleFromElement(
314 'custom_url',
315 TXT_UAM_REDIRECT_TO_URL,
316 $configParameters['redirect_custom_url']
317 );
318 } catch (Exception) {
319 }
320 }
321
322 $configParameter = $configParameters['redirect'];
323
324 $redirect = $this->formFactory->createRadio(
325 $configParameter->getId(),
326 $values,
327 $configParameter->getValue(),
328 TXT_UAM_REDIRECT,
329 TXT_UAM_REDIRECT_DESC
330 );
331 }
332
333 $parameters = [
334 'lock_recursive',
335 'protect_feed',
336 $redirect,
337 'blog_admin_hint',
338 'blog_admin_hint_text',
339 'show_assigned_groups',
340 'hide_edit_link_on_no_access',
341 'extra_ip_header'
342 ];
343
344 return $this->formHelper->getSettingsForm($parameters);
345 }
346
347 private function getFullSettingsFrom(array $types, array $ignoredTypes, callable $formFunction): array
348 {
349 $groupForms = [];
350 $groupForms[MainConfig::DEFAULT_TYPE] = $formFunction();
351
352 foreach ($ignoredTypes as $ignoredType) {
353 unset($types[$ignoredType]);
354 }
355
356 foreach ($types as $type => $typeObject) {
357 $groupForms[$type] = $formFunction($type);
358 }
359
360 return $groupForms;
361 }
362
363 /**
364 * @throws Exception
365 */
366 private function getFullPostSettingsForm(): array
367 {
368 return $this->getFullSettingsFrom(
369 $this->getPostTypes(),
370 [ObjectHandler::ATTACHMENT_OBJECT_TYPE],
371 function ($type = MainConfig::DEFAULT_TYPE) {
372 return $this->getPostSettingsForm($type);
373 }
374 );
375 }
376
377 /**
378 * @throws Exception
379 */
380 private function getFullTaxonomySettingsForm(): array
381 {
382 return $this->getFullSettingsFrom(
383 $this->getTaxonomies(),
384 [ObjectHandler::POST_FORMAT_TYPE],
385 function ($type = MainConfig::DEFAULT_TYPE) {
386 return $this->getTaxonomySettingsForm($type);
387 }
388 );
389 }
390
391 /**
392 * @throws Exception
393 */
394 private function getFullCacheProvidersForm(): array
395 {
396 $groupForms = [];
397 $cacheProviders = $this->cache->getRegisteredCacheProviders();
398 $groupForms[MainConfig::CACHE_PROVIDER_NONE] = null;
399
400 foreach ($cacheProviders as $cacheProvider) {
401 $groupForms[$cacheProvider->getId()] = $this->formHelper->getSettingsFormByConfig(
402 $cacheProvider->getConfig()
403 );
404 }
405
406 return $groupForms;
407 }
408
409 /**
410 * @return Form[]
411 */
412 public function getCurrentGroupForms(): array
413 {
414 $group = $this->getCurrentTabGroup();
415
416 try {
417 $formMap = [
418 self::GROUP_POST_TYPES => function () {
419 return $this->getFullPostSettingsForm();
420 },
421 self::GROUP_TAXONOMIES => function () {
422 return $this->getFullTaxonomySettingsForm();
423 },
424 self::GROUP_FILES => function () {
425 return [self::SECTION_FILES => $this->getFilesSettingsForm()];
426 },
427 self::GROUP_AUTHOR => function () {
428 return [self::SECTION_AUTHOR => $this->getAuthorSettingsForm()];
429 },
430 self::GROUP_CACHE => function () {
431 return $this->getFullCacheProvidersForm();
432 },
433 self::GROUP_OTHER => function () {
434 return [self::SECTION_OTHER => $this->getOtherSettingsForm()];
435 }
436 ];
437
438 if (isset($formMap[$group]) === true) {
439 return $formMap[$group]();
440 }
441 } catch (Exception $exception) {
442 $this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage()));
443 }
444
445 return [];
446 }
447
448 private function updateFileProtectionFile(array $configParameters): void
449 {
450 $key = 'custom_file_handling_file';
451 $customFileHandlingFile = isset($configParameters[$key]) === true ? $configParameters[$key] : null;
452 unset($configParameters[$key]);
453
454 $this->mainConfig->setConfigParameters($configParameters);
455
456 if ($this->mainConfig->lockFile() === false) {
457 $this->fileHandler->deleteFileProtection();
458 } elseif ($this->mainConfig->useCustomFileHandlingFile() === false) {
459 $this->fileHandler->createFileProtection();
460 } elseif ($customFileHandlingFile !== null) {
461 $this->php->filePutContents(
462 $this->fileHandler->getFileProtectionFileName(),
463 htmlspecialchars_decode($customFileHandlingFile)
464 );
465 }
466 }
467
468 public function updateSettingsAction(): void
469 {
470 $this->verifyNonce('uamUpdateSettings');
471 $group = $this->getCurrentTabGroup();
472 $newConfigParameters = $this->getRequestParameter('config_parameters');
473
474 if ($group === self::GROUP_CACHE) {
475 $section = $this->getCurrentTabGroupSection();
476 $cacheProviders = $this->cache->getRegisteredCacheProviders();
477
478 if (isset($cacheProviders[$section]) === true) {
479 $cacheProviders[$section]->getConfig()->setConfigParameters($newConfigParameters);
480 $newConfigParameters = ['active_cache_provider' => $section];
481 } elseif ($section === MainConfig::CACHE_PROVIDER_NONE) {
482 $newConfigParameters = ['active_cache_provider' => $section];
483 }
484 }
485
486 $this->updateFileProtectionFile($newConfigParameters);
487 $this->wordpress->doAction('uam_update_options', $this->mainConfig);
488 $this->setUpdateMessage(TXT_UAM_UPDATE_SETTINGS);
489 }
490
491 public function isPostTypeGroup(string $key): bool
492 {
493 $postTypes = $this->getPostTypes();
494
495 return isset($postTypes[$key]);
496 }
497 }
498