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