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

134 lines 3.1 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 * @var Php
29 */
30 protected $php;
31
32 /**
33 * @var WordpressConfig
34 */
35 protected $wordpressConfig;
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 = htmlspecialchars($value);
75 }
76
77 return $value;
78 }
79
80 /**
81 * Returns the request parameter.
82 *
83 * @param string $name
84 * @param mixed $default
85 *
86 * @return mixed
87 */
88 public function getRequestParameter($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 *
102 * @param string $fileName The view file name
103 *
104 * @return string
105 */
106 protected function getIncludeContents($fileName)
107 {
108 $contents = '';
109 $realPath = rtrim($this->wordpressConfig->getRealPath(), DIRECTORY_SEPARATOR);
110 $path = [$realPath, 'src', 'View'];
111 $path = implode(DIRECTORY_SEPARATOR, $path).DIRECTORY_SEPARATOR;
112 $fileWithPath = $path.$fileName;
113
114 if (is_file($fileWithPath) === true) {
115 ob_start();
116 $this->php->includeFile($this, $fileWithPath);
117 $contents = ob_get_contents();
118 ob_end_clean();
119 }
120
121 return $contents;
122 }
123
124 /**
125 * Renders the given template
126 */
127 public function render()
128 {
129 if ($this->template !== null) {
130 echo $this->getIncludeContents($this->template);
131 }
132 }
133 }
134