PluginProbe
User Access Manager / 2.3.20
User Access Manager v2.3.20
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 / BaseControllerTrait.php

BaseControllerTrait.php in User Access Manager 2.3.20, at src/Controller/BaseControllerTrait.php

83 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Controller;
6
7 use Exception;
8 use UserAccessManager\Config\WordpressConfig;
9 use UserAccessManager\Wrapper\Php;
10
11 trait BaseControllerTrait
12 {
13 protected ?string $template = null;
14
15 abstract protected function getPhp(): Php;
16
17 abstract protected function getWordpressConfig(): WordpressConfig;
18
19 public function getRequestUrl(): string
20 {
21 return htmlentities($_SERVER['REQUEST_URI'] ?? '', ENT_QUOTES);
22 }
23
24 private function sanitizeValue(mixed $value): mixed
25 {
26 if (is_array($value) === true) {
27 $sanitized = [];
28
29 foreach ($value as $key => $arrayValue) {
30 $sanitized[$this->sanitizeValue($key)] = $this->sanitizeValue($arrayValue);
31 }
32
33 return $sanitized;
34 }
35
36 if (is_string($value) === true) {
37 return htmlspecialchars(stripslashes($value), ENT_QUOTES);
38 }
39
40 return $value;
41 }
42
43 public function getRequestParameter(string $name, mixed $default = null): mixed
44 {
45 $return = (isset($_POST[$name]) === true) ? $this->sanitizeValue($_POST[$name]) : null;
46
47 if ($return === null) {
48 $return = (isset($_GET[$name]) === true) ? $this->sanitizeValue($_GET[$name]) : $default;
49 }
50
51 return $return;
52 }
53
54 protected function getIncludeContents(string $fileName): string
55 {
56 $realPath = rtrim($this->getWordpressConfig()->getRealPath(), DIRECTORY_SEPARATOR);
57 $fileWithPath = implode(DIRECTORY_SEPARATOR, [$realPath, 'src', 'View', $fileName]);
58
59 if (is_file($fileWithPath) === false) {
60 return '';
61 }
62
63 try {
64 ob_start();
65 $this->getPhp()->includeFile($this, $fileWithPath);
66 $contents = ob_get_contents();
67 } catch (Exception $exception) {
68 $contents = "Error on including content '$fileWithPath': {$exception->getMessage()}";
69 }
70
71 ob_end_clean();
72
73 return $contents;
74 }
75
76 public function render(): void
77 {
78 if ($this->template !== null) {
79 echo $this->getIncludeContents($this->template);
80 }
81 }
82 }
83