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

617 lines 18.2 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 private function getPostSettingsForm($postType = MainConfig::DEFAULT_TYPE)
217 {
218 $textarea = null;
219 $configParameters = $this->mainConfig->getConfigParameters();
220
221 if (isset($configParameters["{$postType}_content"]) === true) {
222 $configParameter = $configParameters["{$postType}_content"];
223 $textarea = $this->formFactory->createTextarea(
224 $configParameter->getId(),
225 $configParameter->getValue(),
226 $this->formHelper->getParameterText($configParameter, false, $postType),
227 $this->formHelper->getParameterText($configParameter, true, $postType)
228 );
229 }
230
231 $parameters = ($postType !== MainConfig::DEFAULT_TYPE) ? ["{$postType}_use_default"] : [];
232 $parameters = array_merge($parameters, [
233 "hide_{$postType}",
234 "hide_{$postType}_title",
235 "{$postType}_title",
236 $textarea,
237 "hide_{$postType}_comment",
238 "{$postType}_comment_content",
239 "{$postType}_comments_locked",
240 "show_{$postType}_content_before_more"
241 ]);
242
243 return $this->formHelper->getSettingsForm($parameters, $postType);
244 }
245
246 /**
247 * Returns the taxonomy settings form.
248 *
249 * @param string $taxonomy
250 *
251 * @return \UserAccessManager\Form\Form
252 */
253 private function getTaxonomySettingsForm($taxonomy = MainConfig::DEFAULT_TYPE)
254 {
255 $parameters = ($taxonomy !== MainConfig::DEFAULT_TYPE) ? ["{$taxonomy}_use_default"] : [];
256 $parameters = array_merge($parameters, [
257 "hide_empty_{$taxonomy}"
258 ]);
259
260 return $this->formHelper->getSettingsForm($parameters, $taxonomy);
261 }
262
263 /**
264 * Checks if x send file is available.
265 *
266 * @return bool
267 */
268 private function isXSendFileAvailable()
269 {
270 $content = file_get_contents($this->wordpress->getSiteUrl().'?testXSendFile');
271 $this->fileHandler->removeXSendFileTestFile();
272
273 return ($content === 'success');
274 }
275
276 /**
277 * Disables the xSendFileOption
278 *
279 * @param Form $form
280 */
281 private function disableXSendFileOption(Form $form)
282 {
283 $formElements = $form->getElements();
284
285 if (isset($formElements['download_type']) === true) {
286 /** @var ValueSetFormElement $downloadType */
287 $downloadType = $formElements['download_type'];
288 $possibleValues = $downloadType->getPossibleValues();
289
290 if (isset($possibleValues['xsendfile']) === true) {
291 $possibleValues['xsendfile']->markDisabled();
292 }
293 }
294 }
295
296 /**
297 * Adds the lock file types config parameter to the parameters.
298 *
299 * @param array $configParameters
300 * @param array $parameters
301 */
302 private function addLockFileTypes(array $configParameters, array &$parameters)
303 {
304 if (isset($configParameters['lock_file_types']) === true
305 && $this->wordpress->isNginx() === false
306 ) {
307 $parameters['lock_file_types'] = [
308 'selected' => 'locked_file_types',
309 'not_selected' => 'not_locked_file_types'
310 ];
311
312 if ($this->wordpress->gotModRewrite() === false) {
313 $parameters[] = 'file_pass_type';
314 }
315 }
316 }
317
318 /**
319 * Returns the files settings form.
320 *
321 * @return \UserAccessManager\Form\Form
322 */
323 private function getFilesSettingsForm()
324 {
325 $fileProtectionFileName = $this->fileHandler->getFileProtectionFileName();
326 $fileContent = (file_exists($fileProtectionFileName) === true) ?
327 file_get_contents($fileProtectionFileName) : '';
328
329 $configParameters = $this->mainConfig->getConfigParameters();
330
331 $parameters = [
332 'lock_file',
333 'download_type',
334 'inline_files',
335 'no_access_image_type' => ['custom' => 'custom_no_access_image'],
336 'use_custom_file_handling_file',
337 $this->formFactory->createTextarea(
338 'custom_file_handling_file',
339 $fileContent,
340 TXT_UAM_CUSTOM_FILE_HANDLING_FILE,
341 TXT_UAM_CUSTOM_FILE_HANDLING_FILE_DESC
342 ),
343 'locked_directory_type' => ['custom' => 'custom_locked_directories']
344 ];
345
346 $this->addLockFileTypes($configParameters, $parameters);
347
348 $form = $this->formHelper->getSettingsForm($parameters);
349
350 if ($this->isXSendFileAvailable() === false) {
351 $this->disableXSendFileOption($form);
352 }
353
354 return $form;
355 }
356
357 /**
358 * Returns the author settings form.
359 *
360 * @return \UserAccessManager\Form\Form
361 */
362 private function getAuthorSettingsForm()
363 {
364 $parameters = [
365 'authors_has_access_to_own',
366 'authors_can_add_posts_to_groups',
367 'full_access_role'
368 ];
369
370 return $this->formHelper->getSettingsForm($parameters);
371 }
372
373 /**
374 * Returns the author settings form.
375 *
376 * @return \UserAccessManager\Form\Form
377 */
378 private function getOtherSettingsForm()
379 {
380 $redirect = null;
381 $configParameters = $this->mainConfig->getConfigParameters();
382
383 if (isset($configParameters['redirect'])) {
384 $values = [
385 $this->formFactory->createMultipleFormElementValue('false', TXT_UAM_NO),
386 $this->formFactory->createMultipleFormElementValue('blog', TXT_UAM_REDIRECT_TO_BLOG),
387 ];
388
389 if (isset($configParameters['redirect_custom_page']) === true) {
390 $redirectCustomPage = $configParameters['redirect_custom_page'];
391 $redirectCustomPageValue = $this->formFactory->createMultipleFormElementValue(
392 'selected',
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 $redirectCustomPageValue->setSubElement($formElement);
413 $values[] = $redirectCustomPageValue;
414 }
415
416 if (isset($configParameters['redirect_custom_url']) === true) {
417 $values[] = $this->formHelper->createMultipleFromElement(
418 'custom_url',
419 TXT_UAM_REDIRECT_TO_URL,
420 $configParameters['redirect_custom_url']
421 );
422 }
423
424 $configParameter = $configParameters['redirect'];
425
426 $redirect = $this->formFactory->createRadio(
427 $configParameter->getId(),
428 $values,
429 $configParameter->getValue(),
430 TXT_UAM_REDIRECT,
431 TXT_UAM_REDIRECT_DESC
432 );
433 }
434
435 $parameters = [
436 'lock_recursive',
437 'protect_feed',
438 $redirect,
439 'blog_admin_hint',
440 'blog_admin_hint_text',
441 'show_assigned_groups',
442 'hide_edit_link_on_no_access'
443 ];
444
445 return $this->formHelper->getSettingsForm($parameters);
446 }
447
448 /**
449 * Returns the full settings from.
450 *
451 * @param array $types
452 * @param array $ignoredTypes
453 * @param Callable $formFunction
454 *
455 * @return array
456 */
457 private function getFullSettingsFrom(array $types, array $ignoredTypes, $formFunction)
458 {
459 $groupForms = [];
460 $groupForms[MainConfig::DEFAULT_TYPE] = $formFunction();
461
462 foreach ($ignoredTypes as $ignoredType) {
463 unset($types[$ignoredType]);
464 }
465
466 foreach ($types as $type => $typeObject) {
467 $groupForms[$type] = $formFunction($type);
468 }
469
470 return $groupForms;
471 }
472
473 /**
474 * Returns the full taxonomy post forms.
475 *
476 * @return array
477 */
478 private function getFullPostSettingsForm()
479 {
480 return $this->getFullSettingsFrom(
481 $this->getPostTypes(),
482 [ObjectHandler::ATTACHMENT_OBJECT_TYPE],
483 function ($type = MainConfig::DEFAULT_TYPE) {
484 return $this->getPostSettingsForm($type);
485 }
486 );
487 }
488
489 /**
490 * Returns the full taxonomy settings forms.
491 *
492 * @return array
493 */
494 private function getFullTaxonomySettingsForm()
495 {
496 return $this->getFullSettingsFrom(
497 $this->getTaxonomies(),
498 [ObjectHandler::POST_FORMAT_TYPE],
499 function ($type = MainConfig::DEFAULT_TYPE) {
500 return $this->getTaxonomySettingsForm($type);
501 }
502 );
503 }
504
505 /**
506 * Returns the full cache providers froms.
507 *
508 * @return array
509 */
510 private function getFullCacheProvidersFrom()
511 {
512 $groupForms = [];
513 $cacheProviders = $this->cache->getRegisteredCacheProviders();
514 $groupForms[MainConfig::CACHE_PROVIDER_NONE] = null;
515
516 foreach ($cacheProviders as $cacheProvider) {
517 $groupForms[$cacheProvider->getId()] = $this->formHelper->getSettingsFormByConfig(
518 $cacheProvider->getConfig()
519 );
520 }
521
522 return $groupForms;
523 }
524
525 /**
526 * Returns the current settings form.
527 *
528 * @return \UserAccessManager\Form\Form[]
529 */
530 public function getCurrentGroupForms()
531 {
532 $group = $this->getCurrentTabGroup();
533 $groupForms = [];
534
535 if ($group === self::GROUP_POST_TYPES) {
536 $groupForms = $this->getFullPostSettingsForm();
537 } elseif ($group === self::GROUP_TAXONOMIES) {
538 $groupForms = $this->getFullTaxonomySettingsForm();
539 } elseif ($group === self::GROUP_FILES) {
540 $groupForms = [self::SECTION_FILES => $this->getFilesSettingsForm()];
541 } elseif ($group === self::GROUP_AUTHOR) {
542 $groupForms = [self::SECTION_AUTHOR => $this->getAuthorSettingsForm()];
543 } elseif ($group === self::GROUP_CACHE) {
544 $groupForms = $this->getFullCacheProvidersFrom();
545 } elseif ($group === self::GROUP_OTHER) {
546 $groupForms = [self::SECTION_OTHER => $this->getOtherSettingsForm()];
547 }
548
549 return $groupForms;
550 }
551
552 /**
553 * Updates the file handling file.
554 *
555 * @param array $configParameters
556 */
557 private function updateFileProtectionFile(array $configParameters)
558 {
559 $key = 'custom_file_handling_file';
560 $customFileHandlingFile = isset($configParameters[$key]) === true ? $configParameters[$key] : null;
561 unset($configParameters[$key]);
562
563 $this->mainConfig->setConfigParameters($configParameters);
564
565 if ($this->mainConfig->lockFile() === false) {
566 $this->fileHandler->deleteFileProtection();
567 } elseif ($this->mainConfig->useCustomFileHandlingFile() === false) {
568 $this->fileHandler->createFileProtection();
569 } elseif ($customFileHandlingFile !== null) {
570 $this->php->filePutContents(
571 $this->fileHandler->getFileProtectionFileName(),
572 htmlspecialchars_decode($customFileHandlingFile)
573 );
574 }
575 }
576
577 /**
578 * Update settings action.
579 */
580 public function updateSettingsAction()
581 {
582 $this->verifyNonce('uamUpdateSettings');
583 $group = $this->getCurrentTabGroup();
584 $newConfigParameters = $this->getRequestParameter('config_parameters');
585
586 if ($group === self::GROUP_CACHE) {
587 $section = $this->getCurrentTabGroupSection();
588 $cacheProviders = $this->cache->getRegisteredCacheProviders();
589
590 if (isset($cacheProviders[$section]) === true) {
591 $cacheProviders[$section]->getConfig()->setConfigParameters($newConfigParameters);
592 $newConfigParameters = ['active_cache_provider' => $section];
593 } elseif ($section === MainConfig::CACHE_PROVIDER_NONE) {
594 $newConfigParameters = ['active_cache_provider' => $section];
595 }
596 }
597
598 $this->updateFileProtectionFile($newConfigParameters);
599 $this->wordpress->doAction('uam_update_options', $this->mainConfig);
600 $this->setUpdateMessage(TXT_UAM_UPDATE_SETTINGS);
601 }
602
603 /**
604 * Checks if the group is a post type.
605 *
606 * @param string $key
607 *
608 * @return bool
609 */
610 public function isPostTypeGroup($key)
611 {
612 $postTypes = $this->getPostTypes();
613
614 return isset($postTypes[$key]);
615 }
616 }
617