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

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