PluginProbe
User Access Manager / 2.2.1
User Access Manager v2.2.1
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 / Controller.php

Controller.php in User Access Manager 2.2.1, at src/Controller/Controller.php

186 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Controller.php
4 *
5 * The Controller class 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 UserAccessManager\Config\WordpressConfig;
21 use UserAccessManager\Controller\Backend\BackendController;
22 use UserAccessManager\Wrapper\Php;
23 use UserAccessManager\Wrapper\Wordpress;
24
25 /**
26 * Class Controller
27 *
28 * @package UserAccessManager\Controller
29 */
30 abstract class Controller
31 {
32 use BaseControllerTrait {
33 render as traitRender;
34 }
35
36 const ACTION_PARAMETER = 'uam_action';
37 const ACTION_SUFFIX = 'Action';
38
39 /**
40 * @var Php
41 */
42 protected $php;
43
44 /**
45 * @var WordpressConfig
46 */
47 protected $wordpressConfig;
48
49 /**
50 * @var Wordpress
51 */
52 protected $wordpress;
53
54 /**
55 * @var string
56 */
57 protected $updateMessage = null;
58
59 /**
60 * Controller constructor.
61 * @param Php $php
62 * @param Wordpress $wordpress
63 * @param WordpressConfig $wordpressConfig
64 */
65 public function __construct(Php $php, Wordpress $wordpress, WordpressConfig $wordpressConfig)
66 {
67 $this->php = $php;
68 $this->wordpress = $wordpress;
69 $this->wordpressConfig = $wordpressConfig;
70 }
71
72 /**
73 * @return Php
74 */
75 protected function getPhp(): Php
76 {
77 return $this->php;
78 }
79
80 /**
81 * @return WordpressConfig
82 */
83 protected function getWordpressConfig(): WordpressConfig
84 {
85 return $this->wordpressConfig;
86 }
87
88 /**
89 * Returns the nonce field.
90 * @param string $name
91 * @return string
92 */
93 public function createNonceField(string $name): string
94 {
95 return $this->wordpress->getNonceField($name, $name.'Nonce');
96 }
97
98 /**
99 * Returns the nonce.
100 * @param string $name
101 * @return string
102 */
103 public function getNonce(string $name): string
104 {
105 return $this->wordpress->createNonce($name);
106 }
107
108 /**
109 * Verifies the nonce and terminates the application if the nonce is wrong.
110 * @param string $name
111 */
112 protected function verifyNonce(string $name)
113 {
114 $nonce = $this->getRequestParameter($name.'Nonce');
115
116 if ($this->wordpress->verifyNonce($nonce, $name) === false) {
117 $this->wordpress->wpDie(TXT_UAM_NONCE_FAILURE_MESSAGE, TXT_UAM_NONCE_FAILURE_TITLE, ['response' => 401]);
118 }
119 }
120
121 /**
122 * Sets the update message.
123 * @param string $message
124 */
125 protected function setUpdateMessage(string $message)
126 {
127 $this->updateMessage = $message;
128 }
129
130 /**
131 * Adds an error message.
132 * @param string $message
133 */
134 protected function addErrorMessage(string $message)
135 {
136 if (isset($_SESSION[BackendController::UAM_ERRORS]) === false) {
137 $_SESSION[BackendController::UAM_ERRORS] = [];
138 }
139
140 $_SESSION[BackendController::UAM_ERRORS][] = $message;
141 }
142
143 /**
144 * Returns the update message.
145 * @return string
146 */
147 public function getUpdateMessage(): ?string
148 {
149 return $this->updateMessage;
150 }
151
152 /**
153 * Returns true if a update message is set.
154 * @return bool
155 */
156 public function hasUpdateMessage(): bool
157 {
158 return $this->updateMessage !== null;
159 }
160
161 /**
162 * Process the action.
163 */
164 protected function processAction()
165 {
166 $postAction = (string) $this->getRequestParameter(self::ACTION_PARAMETER);
167 $postActionSplit = explode('_', $postAction);
168 $postAction = array_shift($postActionSplit);
169 $postAction .= implode('', array_map('ucfirst', $postActionSplit));
170 $actionMethod = $postAction.self::ACTION_SUFFIX;
171
172 if (method_exists($this, $actionMethod) === true) {
173 $this->{$actionMethod}();
174 }
175 }
176
177 /**
178 * Renders the given template
179 */
180 public function render()
181 {
182 $this->processAction();
183 $this->traitRender();
184 }
185 }
186