PluginProbe
User Access Manager / 2.2.15
User Access Manager v2.2.15
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.2.15, at src/Controller/BaseControllerTrait.php

137 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * BaseControllerTrait.php
4 *
5 * The BaseControllerTrait trait 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;
19
20 use Exception;
21 use UserAccessManager\Config\WordpressConfig;
22 use UserAccessManager\Wrapper\Php;
23
24 /**
25 * Trait BaseControllerTrait
26 *
27 * @package UserAccessManager\Controller
28 */
29 trait BaseControllerTrait
30 {
31 /**
32 * @return Php
33 */
34 abstract protected function getPhp(): Php;
35
36 /**
37 * @return WordpressConfig
38 */
39 abstract protected function getWordpressConfig(): WordpressConfig;
40
41 /**
42 * @var string
43 */
44 protected $template = null;
45
46 /**
47 * Returns the current request url.
48 * @return string
49 */
50 public function getRequestUrl(): string
51 {
52 return htmlentities($_SERVER['REQUEST_URI']);
53 }
54
55 /**
56 * Sanitize the given value.
57 * @param mixed $value
58 * @return array|string
59 */
60 private function sanitizeValue($value)
61 {
62 if (is_object($value) === true) {
63 return $value;
64 } elseif (is_array($value) === true) {
65 $newValue = [];
66
67 foreach ($value as $key => $arrayValue) {
68 $sanitizedKey = $this->sanitizeValue($key);
69 $newValue[$sanitizedKey] = $this->sanitizeValue($arrayValue);
70 }
71
72 $value = $newValue;
73 } elseif (is_string($value) === true) {
74 $value = preg_replace('/[\\\\]+(["|\'])/', '$1', $value);
75 $value = stripslashes($value);
76 $value = htmlspecialchars($value);
77 }
78
79 return $value;
80 }
81
82 /**
83 * Returns the request parameter.
84 * @param string $name
85 * @param mixed $default
86 * @return mixed
87 */
88 public function getRequestParameter(string $name, $default = null)
89 {
90 $return = (isset($_POST[$name]) === true) ? $this->sanitizeValue($_POST[$name]) : null;
91
92 if ($return === null) {
93 $return = (isset($_GET[$name]) === true) ? $this->sanitizeValue($_GET[$name]) : $default;
94 }
95
96 return $return;
97 }
98
99 /**
100 * Returns the content of the excluded php file.
101 * @param string $fileName The view file name
102 * @return string
103 */
104 protected function getIncludeContents(string $fileName): string
105 {
106 $contents = '';
107 $realPath = rtrim($this->getWordpressConfig()->getRealPath(), DIRECTORY_SEPARATOR);
108 $path = [$realPath, 'src', 'View'];
109 $path = implode(DIRECTORY_SEPARATOR, $path).DIRECTORY_SEPARATOR;
110 $fileWithPath = $path.$fileName;
111
112 if (is_file($fileWithPath) === true) {
113 try {
114 ob_start();
115 $this->getPhp()->includeFile($this, $fileWithPath);
116 $contents = ob_get_contents();
117 ob_end_clean();
118 } catch (Exception $exception) {
119 $contents = "Error on including content '{$fileWithPath}': {$exception->getMessage()}";
120 ob_end_clean();
121 }
122 }
123
124 return $contents;
125 }
126
127 /**
128 * Renders the given template
129 */
130 public function render()
131 {
132 if ($this->template !== null) {
133 echo $this->getIncludeContents($this->template);
134 }
135 }
136 }
137