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 / BaseControllerTrait.php

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

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