PluginProbe
User Access Manager / 2.2.12
User Access Manager v2.2.12
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.2.12, at src/Controller/Backend/SettingsController.php

639 lines 19.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SettingsController.php
4 *
5 * The SettingsController class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Controller\Backend;
19
20 use Exception;
21 use UserAccessManager\Cache\Cache;
22 use UserAccessManager\Config\MainConfig;
23 use UserAccessManager\Config\WordpressConfig;
24 use UserAccessManager\Controller\Controller;
25 use UserAccessManager\File\FileHandler;
26 use UserAccessManager\Form\Form;
27 use UserAccessManager\Form\FormFactory;
28 use UserAccessManager\Form\FormHelper;
29 use UserAccessManager\Form\ValueSetFormElement;
30 use UserAccessManager\Object\ObjectHandler;
31 use UserAccessManager\Wrapper\Php;
32 use UserAccessManager\Wrapper\Wordpress;
33 use WP_Post_Type;
34 use WP_Taxonomy;
35
36 class SettingsController extends Controller
37 {
38 use ControllerTabNavigationTrait;
39
40 const GROUP_POST_TYPES = 'post_types';
41 const GROUP_TAXONOMIES = 'taxonomies';
42 const GROUP_FILES = 'file';
43 const SECTION_FILES = 'file';
44 const GROUP_AUTHOR = 'author';
45 const SECTION_AUTHOR = 'author';
46 const GROUP_CACHE = 'cache';
47 const GROUP_OTHER = 'other';
48 const SECTION_OTHER = 'other';
49
50 /**
51 * @var MainConfig
52 */
53 private $mainConfig;
54
55 /**
56 * @var string
57 */
58 protected $template = 'AdminSettings.php';
59
60 /**
61 * @var Cache
62 */
63 private $cache;
64
65 /**
66 * @var FileHandler
67 */
68 private $fileHandler;
69
70 /**
71 * @var FormFactory
72 */
73 private $formFactory;
74
75 /**
76 * @var FormHelper
77 */
78 private $formHelper;
79
80 /**
81 * SettingsController constructor.
82 * @param Php $php
83 * @param Wordpress $wordpress
84 * @param WordpressConfig $wordpressConfig
85 * @param MainConfig $mainConfig
86 * @param Cache $cache
87 * @param FileHandler $fileHandler
88 * @param FormFactory $formFactory
89 * @param FormHelper $formHelper
90 */
91 public function __construct(
92 Php $php,
93 Wordpress $wordpress,
94 WordpressConfig $wordpressConfig,
95 MainConfig $mainConfig,
96 Cache $cache,
97 FileHandler $fileHandler,
98 FormFactory $formFactory,
99 FormHelper $formHelper
100 ) {
101 parent::__construct($php, $wordpress, $wordpressConfig);
102 $this->mainConfig = $mainConfig;
103 $this->cache = $cache;
104 $this->fileHandler = $fileHandler;
105 $this->formFactory = $formFactory;
106 $this->formHelper = $formHelper;
107 }
108
109 /**
110 * Returns the tab groups.
111 * @return array
112 */
113 public function getTabGroups(): array
114 {
115 $activeCacheProvider = $this->mainConfig->getActiveCacheProvider();
116 $cacheProviderSections = [$activeCacheProvider];
117 $cacheProviders = $this->cache->getRegisteredCacheProviders();
118
119 foreach ($cacheProviders as $cacheProvider) {
120 if ($cacheProvider->getId() !== $activeCacheProvider) {
121 $cacheProviderSections[] = $cacheProvider->getId();
122 }
123 }
124
125 if (!in_array(MainConfig::CACHE_PROVIDER_NONE, $cacheProviderSections)) {
126 $cacheProviderSections[] = MainConfig::CACHE_PROVIDER_NONE;
127 }
128
129 return [
130 self::GROUP_POST_TYPES => array_merge([MainConfig::DEFAULT_TYPE], array_keys($this->getPostTypes())),
131 self::GROUP_TAXONOMIES => array_merge([MainConfig::DEFAULT_TYPE], array_keys($this->getTaxonomies())),
132 self::GROUP_FILES => [self::SECTION_FILES],
133 self::GROUP_AUTHOR => [self::SECTION_AUTHOR],
134 self::GROUP_CACHE => $cacheProviderSections,
135 self::GROUP_OTHER => [self::SECTION_OTHER]
136 ];
137 }
138
139 /**
140 * Returns the pages.
141 * @return array
142 */
143 private function getPages(): array
144 {
145 $pages = $this->wordpress->getPages('sort_column=menu_order');
146 return is_array($pages) !== false ? $pages : [];
147 }
148
149 /**
150 * Returns the post types as object.
151 * @return WP_Post_Type[]
152 */
153 private function getPostTypes(): array
154 {
155 return $this->wordpress->getPostTypes(['public' => true], 'objects');
156 }
157
158 /**
159 * Returns the taxonomies as objects.
160 * @return WP_Taxonomy[]
161 */
162 private function getTaxonomies(): array
163 {
164 return $this->wordpress->getTaxonomies(['public' => true], 'objects');
165 }
166
167 /**
168 * @param string $key
169 * @param bool $description
170 * @return string
171 */
172 public function getText(string $key, $description = false): string
173 {
174 return $this->formHelper->getText($key, $description);
175 }
176
177 /**
178 * @param string $key
179 * @return string
180 */
181 public function getGroupText(string $key): string
182 {
183 return $this->getText($key);
184 }
185
186 /**
187 * @param string $key
188 * @return string
189 */
190 public function getGroupSectionText(string $key): string
191 {
192 return ($key === MainConfig::DEFAULT_TYPE) ?
193 TXT_UAM_SETTINGS_GROUP_SECTION_DEFAULT : $this->getObjectName($key);
194 }
195
196 /**
197 * Returns the object name.
198 * @param string $objectKey
199 * @return string
200 */
201 public function getObjectName(string $objectKey): string
202 {
203 $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
204 + $this->wordpress->getTaxonomies(['public' => true], 'objects');
205
206 return (isset($objects[$objectKey]) === true) ? $objects[$objectKey]->labels->name : $objectKey;
207 }
208
209 /**
210 * Returns the post settings form.
211 * @param string $postType
212 * @return Form
213 * @throws Exception
214 */
215 private function getPostSettingsForm($postType = MainConfig::DEFAULT_TYPE): Form
216 {
217 $textarea = null;
218 $configParameters = $this->mainConfig->getConfigParameters();
219
220 if (isset($configParameters["{$postType}_content"]) === true) {
221 $configParameter = $configParameters["{$postType}_content"];
222 $textarea = $this->formFactory->createTextarea(
223 $configParameter->getId(),
224 $configParameter->getValue(),
225 $this->formHelper->getParameterText($configParameter, false, $postType),
226 $this->formHelper->getParameterText($configParameter, true, $postType)
227 );
228 }
229
230 $parameters = ($postType !== MainConfig::DEFAULT_TYPE) ? ["{$postType}_use_default"] : [];
231 $parameters = array_merge($parameters, [
232 "hide_{$postType}",
233 "hide_{$postType}_title",
234 "{$postType}_title",
235 $textarea,
236 "hide_{$postType}_comment",
237 "{$postType}_comment_content",
238 "{$postType}_comments_locked",
239 "show_{$postType}_content_before_more"
240 ]);
241
242 return $this->formHelper->getSettingsForm($parameters, $postType);
243 }
244
245 /**
246 * Returns the taxonomy settings form.
247 * @param string $taxonomy
248 * @return Form
249 * @throws Exception
250 */
251 private function getTaxonomySettingsForm($taxonomy = MainConfig::DEFAULT_TYPE): Form
252 {
253 $parameters = ($taxonomy !== MainConfig::DEFAULT_TYPE) ? ["{$taxonomy}_use_default"] : [];
254 $parameters = array_merge($parameters, [
255 "hide_empty_{$taxonomy}"
256 ]);
257
258 return $this->formHelper->getSettingsForm($parameters, $taxonomy);
259 }
260
261 /**
262 * Checks if x send file is available.
263 * @return bool
264 */
265 private function isXSendFileAvailable(): bool
266 {
267 $content = @file_get_contents($this->wordpress->getSiteUrl() . '?testXSendFile');
268 $this->fileHandler->removeXSendFileTestFile();
269
270 return ($content === 'success');
271 }
272
273 /**
274 * Disables the xSendFileOption
275 * @param Form $form
276 */
277 private function disableXSendFileOption(Form $form)
278 {
279 $formElements = $form->getElements();
280
281 if (isset($formElements['download_type']) === true) {
282 /** @var ValueSetFormElement $downloadType */
283 $downloadType = $formElements['download_type'];
284 $possibleValues = $downloadType->getPossibleValues();
285
286 if (isset($possibleValues['xsendfile']) === true) {
287 $possibleValues['xsendfile']->markDisabled();
288 }
289 }
290 }
291
292 /**
293 * Adds the lock file types config parameter to the parameters.
294 * @param array $configParameters
295 * @param array $parameters
296 */
297 private function addLockFileTypes(array $configParameters, array &$parameters)
298 {
299 if (isset($configParameters['lock_file_types']) === true
300 && $this->wordpress->isNginx() === false
301 ) {
302 $parameters['lock_file_types'] = [
303 'selected' => 'locked_file_types',
304 'not_selected' => 'not_locked_file_types'
305 ];
306
307 if ($this->wordpress->gotModRewrite() === false) {
308 $parameters[] = 'file_pass_type';
309 }
310 }
311 }
312
313 /**
314 * Returns the files settings form.
315 * @return Form
316 * @throws Exception
317 */
318 private function getFilesSettingsForm(): Form
319 {
320 $fileProtectionFileName = $this->fileHandler->getFileProtectionFileName();
321 $fileContent = (file_exists($fileProtectionFileName) === true) ?
322 file_get_contents($fileProtectionFileName) : '';
323
324 $configParameters = $this->mainConfig->getConfigParameters();
325
326 $parameters = [
327 'lock_file',
328 'download_type',
329 'inline_files',
330 'no_access_image_type' => ['custom' => 'custom_no_access_image'],
331 'use_custom_file_handling_file',
332 $this->formFactory->createTextarea(
333 'custom_file_handling_file',
334 $fileContent,
335 TXT_UAM_CUSTOM_FILE_HANDLING_FILE,
336 TXT_UAM_CUSTOM_FILE_HANDLING_FILE_DESC
337 ),
338 'locked_directory_type' => ['custom' => 'custom_locked_directories']
339 ];
340
341 $this->addLockFileTypes($configParameters, $parameters);
342
343 $form = $this->formHelper->getSettingsForm($parameters);
344
345 if ($this->isXSendFileAvailable() === false) {
346 $this->disableXSendFileOption($form);
347 }
348
349 return $form;
350 }
351
352 /**
353 * Returns the author settings form.
354 * @return Form
355 * @throws Exception
356 */
357 private function getAuthorSettingsForm(): Form
358 {
359 $parameters = [
360 'authors_has_access_to_own',
361 'authors_can_add_posts_to_groups',
362 'full_access_role'
363 ];
364
365 return $this->formHelper->getSettingsForm($parameters);
366 }
367
368 /**
369 * Adds the custom page redirect from element.
370 * @param array $configParameters
371 * @param array $values
372 */
373 private function addCustomPageRedirectFormElement(array $configParameters, array &$values)
374 {
375 if (isset($configParameters['redirect_custom_page']) === true) {
376 $redirectCustomPage = $configParameters['redirect_custom_page'];
377 $redirectCustomPageValue = $this->formFactory->createMultipleFormElementValue(
378 'custom_page',
379 TXT_UAM_REDIRECT_TO_PAGE
380 );
381
382 $possibleValues = [];
383 $pages = $this->getPages();
384
385 foreach ($pages as $page) {
386 $possibleValues[] = $this->formFactory->createValueSetFromElementValue(
387 (int) $page->ID,
388 $page->post_title
389 );
390 }
391
392 $formElement = $this->formFactory->createSelect(
393 $redirectCustomPage->getId(),
394 $possibleValues,
395 (int) $redirectCustomPage->getValue()
396 );
397
398 try {
399 $redirectCustomPageValue->setSubElement($formElement);
400 $values[] = $redirectCustomPageValue;
401 } catch (Exception $exception) {
402 // Do Nothing
403 }
404 }
405 }
406
407 /**
408 * Returns the author settings form.
409 * @return Form
410 * @throws Exception
411 */
412 private function getOtherSettingsForm(): Form
413 {
414 $redirect = null;
415 $configParameters = $this->mainConfig->getConfigParameters();
416
417 if (isset($configParameters['redirect'])) {
418 $values = [
419 $this->formFactory->createMultipleFormElementValue('false', TXT_UAM_NO),
420 $this->formFactory->createMultipleFormElementValue('blog', TXT_UAM_REDIRECT_TO_BLOG),
421 $this->formFactory->createMultipleFormElementValue('login', TXT_UAM_REDIRECT_TO_LOGIN)
422 ];
423
424 $this->addCustomPageRedirectFormElement($configParameters, $values);
425
426 if (isset($configParameters['redirect_custom_url']) === true) {
427 try {
428 $values[] = $this->formHelper->createMultipleFromElement(
429 'custom_url',
430 TXT_UAM_REDIRECT_TO_URL,
431 $configParameters['redirect_custom_url']
432 );
433 } catch (Exception $exception) {
434 // Do nothing.
435 }
436 }
437
438 $configParameter = $configParameters['redirect'];
439
440 $redirect = $this->formFactory->createRadio(
441 $configParameter->getId(),
442 $values,
443 $configParameter->getValue(),
444 TXT_UAM_REDIRECT,
445 TXT_UAM_REDIRECT_DESC
446 );
447 }
448
449 $parameters = [
450 'lock_recursive',
451 'protect_feed',
452 $redirect,
453 'blog_admin_hint',
454 'blog_admin_hint_text',
455 'show_assigned_groups',
456 'hide_edit_link_on_no_access'
457 ];
458
459 return $this->formHelper->getSettingsForm($parameters);
460 }
461
462 /**
463 * Returns the full settings from.
464 * @param array $types
465 * @param array $ignoredTypes
466 * @param Callable $formFunction
467 * @return array
468 */
469 private function getFullSettingsFrom(array $types, array $ignoredTypes, callable $formFunction): array
470 {
471 $groupForms = [];
472 $groupForms[MainConfig::DEFAULT_TYPE] = $formFunction();
473
474 foreach ($ignoredTypes as $ignoredType) {
475 unset($types[$ignoredType]);
476 }
477
478 foreach ($types as $type => $typeObject) {
479 $groupForms[$type] = $formFunction($type);
480 }
481
482 return $groupForms;
483 }
484
485 /**
486 * Returns the full taxonomy post forms.
487 * @return array
488 * @throws Exception
489 */
490 private function getFullPostSettingsForm(): array
491 {
492 return $this->getFullSettingsFrom(
493 $this->getPostTypes(),
494 [ObjectHandler::ATTACHMENT_OBJECT_TYPE],
495 function ($type = MainConfig::DEFAULT_TYPE) {
496 return $this->getPostSettingsForm($type);
497 }
498 );
499 }
500
501 /**
502 * Returns the full taxonomy settings forms.
503 * @return array
504 * @throws Exception
505 */
506 private function getFullTaxonomySettingsForm(): array
507 {
508 return $this->getFullSettingsFrom(
509 $this->getTaxonomies(),
510 [ObjectHandler::POST_FORMAT_TYPE],
511 function ($type = MainConfig::DEFAULT_TYPE) {
512 return $this->getTaxonomySettingsForm($type);
513 }
514 );
515 }
516
517 /**
518 * Returns the full cache providers froms.
519 * @return array
520 * @throws Exception
521 */
522 private function getFullCacheProvidersForm(): array
523 {
524 $groupForms = [];
525 $cacheProviders = $this->cache->getRegisteredCacheProviders();
526 $groupForms[MainConfig::CACHE_PROVIDER_NONE] = null;
527
528 foreach ($cacheProviders as $cacheProvider) {
529 $groupForms[$cacheProvider->getId()] = $this->formHelper->getSettingsFormByConfig(
530 $cacheProvider->getConfig()
531 );
532 }
533
534 return $groupForms;
535 }
536
537 /**
538 * Returns the current settings form.
539 * @return Form[]
540 */
541 public function getCurrentGroupForms(): array
542 {
543 $group = $this->getCurrentTabGroup();
544
545 try {
546 $formMap = [
547 self::GROUP_POST_TYPES => function () {
548 return $this->getFullPostSettingsForm();
549 },
550 self::GROUP_TAXONOMIES => function () {
551 return $this->getFullTaxonomySettingsForm();
552 },
553 self::GROUP_FILES => function () {
554 return [self::SECTION_FILES => $this->getFilesSettingsForm()];
555 },
556 self::GROUP_AUTHOR => function () {
557 return [self::SECTION_AUTHOR => $this->getAuthorSettingsForm()];
558 },
559 self::GROUP_CACHE => function () {
560 return $this->getFullCacheProvidersForm();
561 },
562 self::GROUP_OTHER => function () {
563 return [self::SECTION_OTHER => $this->getOtherSettingsForm()];
564 }
565 ];
566
567 if (isset($formMap[$group]) === true) {
568 return $formMap[$group]();
569 }
570 } catch (Exception $exception) {
571 $this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage()));
572 }
573
574 return [];
575 }
576
577 /**
578 * Updates the file handling file.
579 * @param array $configParameters
580 */
581 private function updateFileProtectionFile(array $configParameters)
582 {
583 $key = 'custom_file_handling_file';
584 $customFileHandlingFile = isset($configParameters[$key]) === true ? $configParameters[$key] : null;
585 unset($configParameters[$key]);
586
587 $this->mainConfig->setConfigParameters($configParameters);
588
589 if ($this->mainConfig->lockFile() === false) {
590 $this->fileHandler->deleteFileProtection();
591 } elseif ($this->mainConfig->useCustomFileHandlingFile() === false) {
592 $this->fileHandler->createFileProtection();
593 } elseif ($customFileHandlingFile !== null) {
594 $this->php->filePutContents(
595 $this->fileHandler->getFileProtectionFileName(),
596 htmlspecialchars_decode($customFileHandlingFile)
597 );
598 }
599 }
600
601 /**
602 * Update settings action.
603 */
604 public function updateSettingsAction()
605 {
606 $this->verifyNonce('uamUpdateSettings');
607 $group = $this->getCurrentTabGroup();
608 $newConfigParameters = $this->getRequestParameter('config_parameters');
609
610 if ($group === self::GROUP_CACHE) {
611 $section = $this->getCurrentTabGroupSection();
612 $cacheProviders = $this->cache->getRegisteredCacheProviders();
613
614 if (isset($cacheProviders[$section]) === true) {
615 $cacheProviders[$section]->getConfig()->setConfigParameters($newConfigParameters);
616 $newConfigParameters = ['active_cache_provider' => $section];
617 } elseif ($section === MainConfig::CACHE_PROVIDER_NONE) {
618 $newConfigParameters = ['active_cache_provider' => $section];
619 }
620 }
621
622 $this->updateFileProtectionFile($newConfigParameters);
623 $this->wordpress->doAction('uam_update_options', $this->mainConfig);
624 $this->setUpdateMessage(TXT_UAM_UPDATE_SETTINGS);
625 }
626
627 /**
628 * Checks if the group is a post type.
629 * @param string $key
630 * @return bool
631 */
632 public function isPostTypeGroup(string $key): bool
633 {
634 $postTypes = $this->getPostTypes();
635
636 return isset($postTypes[$key]);
637 }
638 }
639