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

640 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 'extra_ip_header'
458 ];
459
460 return $this->formHelper->getSettingsForm($parameters);
461 }
462
463 /**
464 * Returns the full settings from.
465 * @param array $types
466 * @param array $ignoredTypes
467 * @param Callable $formFunction
468 * @return array
469 */
470 private function getFullSettingsFrom(array $types, array $ignoredTypes, callable $formFunction): array
471 {
472 $groupForms = [];
473 $groupForms[MainConfig::DEFAULT_TYPE] = $formFunction();
474
475 foreach ($ignoredTypes as $ignoredType) {
476 unset($types[$ignoredType]);
477 }
478
479 foreach ($types as $type => $typeObject) {
480 $groupForms[$type] = $formFunction($type);
481 }
482
483 return $groupForms;
484 }
485
486 /**
487 * Returns the full taxonomy post forms.
488 * @return array
489 * @throws Exception
490 */
491 private function getFullPostSettingsForm(): array
492 {
493 return $this->getFullSettingsFrom(
494 $this->getPostTypes(),
495 [ObjectHandler::ATTACHMENT_OBJECT_TYPE],
496 function ($type = MainConfig::DEFAULT_TYPE) {
497 return $this->getPostSettingsForm($type);
498 }
499 );
500 }
501
502 /**
503 * Returns the full taxonomy settings forms.
504 * @return array
505 * @throws Exception
506 */
507 private function getFullTaxonomySettingsForm(): array
508 {
509 return $this->getFullSettingsFrom(
510 $this->getTaxonomies(),
511 [ObjectHandler::POST_FORMAT_TYPE],
512 function ($type = MainConfig::DEFAULT_TYPE) {
513 return $this->getTaxonomySettingsForm($type);
514 }
515 );
516 }
517
518 /**
519 * Returns the full cache providers froms.
520 * @return array
521 * @throws Exception
522 */
523 private function getFullCacheProvidersForm(): array
524 {
525 $groupForms = [];
526 $cacheProviders = $this->cache->getRegisteredCacheProviders();
527 $groupForms[MainConfig::CACHE_PROVIDER_NONE] = null;
528
529 foreach ($cacheProviders as $cacheProvider) {
530 $groupForms[$cacheProvider->getId()] = $this->formHelper->getSettingsFormByConfig(
531 $cacheProvider->getConfig()
532 );
533 }
534
535 return $groupForms;
536 }
537
538 /**
539 * Returns the current settings form.
540 * @return Form[]
541 */
542 public function getCurrentGroupForms(): array
543 {
544 $group = $this->getCurrentTabGroup();
545
546 try {
547 $formMap = [
548 self::GROUP_POST_TYPES => function () {
549 return $this->getFullPostSettingsForm();
550 },
551 self::GROUP_TAXONOMIES => function () {
552 return $this->getFullTaxonomySettingsForm();
553 },
554 self::GROUP_FILES => function () {
555 return [self::SECTION_FILES => $this->getFilesSettingsForm()];
556 },
557 self::GROUP_AUTHOR => function () {
558 return [self::SECTION_AUTHOR => $this->getAuthorSettingsForm()];
559 },
560 self::GROUP_CACHE => function () {
561 return $this->getFullCacheProvidersForm();
562 },
563 self::GROUP_OTHER => function () {
564 return [self::SECTION_OTHER => $this->getOtherSettingsForm()];
565 }
566 ];
567
568 if (isset($formMap[$group]) === true) {
569 return $formMap[$group]();
570 }
571 } catch (Exception $exception) {
572 $this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage()));
573 }
574
575 return [];
576 }
577
578 /**
579 * Updates the file handling file.
580 * @param array $configParameters
581 */
582 private function updateFileProtectionFile(array $configParameters)
583 {
584 $key = 'custom_file_handling_file';
585 $customFileHandlingFile = isset($configParameters[$key]) === true ? $configParameters[$key] : null;
586 unset($configParameters[$key]);
587
588 $this->mainConfig->setConfigParameters($configParameters);
589
590 if ($this->mainConfig->lockFile() === false) {
591 $this->fileHandler->deleteFileProtection();
592 } elseif ($this->mainConfig->useCustomFileHandlingFile() === false) {
593 $this->fileHandler->createFileProtection();
594 } elseif ($customFileHandlingFile !== null) {
595 $this->php->filePutContents(
596 $this->fileHandler->getFileProtectionFileName(),
597 htmlspecialchars_decode($customFileHandlingFile)
598 );
599 }
600 }
601
602 /**
603 * Update settings action.
604 */
605 public function updateSettingsAction()
606 {
607 $this->verifyNonce('uamUpdateSettings');
608 $group = $this->getCurrentTabGroup();
609 $newConfigParameters = $this->getRequestParameter('config_parameters');
610
611 if ($group === self::GROUP_CACHE) {
612 $section = $this->getCurrentTabGroupSection();
613 $cacheProviders = $this->cache->getRegisteredCacheProviders();
614
615 if (isset($cacheProviders[$section]) === true) {
616 $cacheProviders[$section]->getConfig()->setConfigParameters($newConfigParameters);
617 $newConfigParameters = ['active_cache_provider' => $section];
618 } elseif ($section === MainConfig::CACHE_PROVIDER_NONE) {
619 $newConfigParameters = ['active_cache_provider' => $section];
620 }
621 }
622
623 $this->updateFileProtectionFile($newConfigParameters);
624 $this->wordpress->doAction('uam_update_options', $this->mainConfig);
625 $this->setUpdateMessage(TXT_UAM_UPDATE_SETTINGS);
626 }
627
628 /**
629 * Checks if the group is a post type.
630 * @param string $key
631 * @return bool
632 */
633 public function isPostTypeGroup(string $key): bool
634 {
635 $postTypes = $this->getPostTypes();
636
637 return isset($postTypes[$key]);
638 }
639 }
640