1 | <?php |
||
2 | /** |
||
3 | * This file is part of the Shieldon package. |
||
4 | * |
||
5 | * (c) Terry L. <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | * |
||
10 | * php version 7.1.0 |
||
11 | * |
||
12 | * @category Web-security |
||
13 | * @package Shieldon |
||
14 | * @author Terry Lin <[email protected]> |
||
15 | * @copyright 2019 terrylinooo |
||
16 | * @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
||
17 | * @link https://github.com/terrylinooo/shieldon |
||
18 | * @see https://shieldon.io |
||
19 | */ |
||
20 | |||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace Shieldon\Firewall\Panel; |
||
24 | |||
25 | use Psr\Http\Message\ResponseInterface; |
||
26 | use Shieldon\Firewall\Panel\BaseController; |
||
27 | use Shieldon\Firewall\HttpFactory; |
||
28 | use function Shieldon\Firewall\__; |
||
29 | use function Shieldon\Firewall\get_request; |
||
30 | use function Shieldon\Firewall\get_response; |
||
31 | use function Shieldon\Firewall\get_session_instance; |
||
32 | use function Shieldon\Firewall\set_request; |
||
33 | use function array_map; |
||
34 | use function explode; |
||
35 | use function file_exists; |
||
36 | use function gethostname; |
||
37 | use function implode; |
||
38 | use function json_encode; |
||
39 | |||
40 | /** |
||
41 | * User |
||
42 | */ |
||
43 | class Ajax extends BaseController |
||
44 | { |
||
45 | /** |
||
46 | * Public methods | Desctiotion |
||
47 | * ----------------------|--------------------------------------------- |
||
48 | * __call | The magic method. |
||
49 | * changeLocale | Change the user's language of the UI. |
||
50 | * tryMessenger | Test messenger modules. |
||
51 | * ----------------------|--------------------------------------------- |
||
52 | */ |
||
53 | |||
54 | /** |
||
55 | * Constructor. |
||
56 | */ |
||
57 | 12 | public function __construct() |
|
58 | { |
||
59 | 12 | parent::__construct(); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Fallback for undefined methods. |
||
64 | * |
||
65 | * @param string $function The method name. |
||
66 | * @param array $args The arguments. |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | 11 | public function __call($function, $args): bool |
|
71 | { |
||
72 | 11 | $className = 'Shieldon\Firewall\Panel\Sandbox\\' . $function; |
|
73 | |||
74 | 11 | if (file_exists(__DIR__ . '/Sandbox/' . $function . '.php')) { |
|
75 | 10 | $sandbox = new $className(); |
|
76 | 10 | return $sandbox($args); |
|
77 | } |
||
78 | 1 | return false; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Change the user's language of the UI. |
||
83 | * |
||
84 | * @return ResponseInterface |
||
85 | */ |
||
86 | 1 | public function changeLocale(): ResponseInterface |
|
87 | { |
||
88 | 1 | $langCode = get_request()->getQueryParams()['langCode'] ?? 'en'; |
|
89 | 1 | get_session_instance()->set('shieldon_panel_lang', $langCode); |
|
90 | 1 | get_session_instance()->save(); |
|
91 | 1 | $data = []; |
|
92 | |||
93 | 1 | $data['status'] = 'success'; |
|
94 | 1 | $data['lang_code'] = $langCode; |
|
95 | 1 | $data['session_lang_code'] = $langCode; |
|
96 | |||
97 | 1 | $output = json_encode($data); |
|
98 | |||
99 | 1 | return $this->respondJson($output); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Test messenger modules. |
||
104 | * |
||
105 | * @return ResponseInterface |
||
106 | */ |
||
107 | 11 | public function tryMessenger(): ResponseInterface |
|
108 | { |
||
109 | 11 | $request = get_request(); |
|
110 | 11 | $message = []; |
|
111 | |||
112 | 11 | $getParams = $request->getQueryParams(); |
|
113 | 11 | $serverParams = $request->getServerParams(); |
|
114 | |||
115 | 11 | $serverName = $serverParams['SERVER_NAME'] ?? gethostname(); |
|
116 | 11 | $moduleName = $getParams['module'] ?? ''; |
|
117 | |||
118 | 11 | $data = []; |
|
119 | 11 | $data['status'] = 'undefined'; |
|
120 | 11 | $data['result']['moduleName'] = $moduleName; |
|
121 | |||
122 | 11 | $message['title'] = __('panel', 'test_msg_title', 'Testing Message from Host: ') . $serverName; |
|
123 | 11 | $message['body'] = __( |
|
124 | 'panel', |
||
125 | 'test_msg_body', |
||
126 | 'Messenger module "{0}" has been tested and confirmed successfully.', |
||
127 | [$moduleName] |
||
128 | ); |
||
129 | |||
130 | // @codeCoverageIgnoreStart |
||
131 | |||
132 | // Name the testing method. |
||
133 | $method = explode('-', $moduleName); |
||
134 | $method = implode( |
||
135 | '', |
||
136 | array_map( |
||
137 | function ($word) { |
||
138 | return ucwords($word); |
||
139 | }, |
||
140 | $method |
||
141 | ) |
||
142 | ); |
||
143 | |||
144 | $postParams = $request->getParsedBody(); |
||
145 | $postKey = 'messengers__' . $moduleName . '__confirm_test'; |
||
146 | |||
147 | // Call testing method if exists. |
||
148 | $status = $this->{$method}($getParams, $message); |
||
149 | |||
150 | if ($status) { |
||
151 | $data['status'] = 'success'; |
||
152 | $postParams[$postKey] = 'on'; |
||
153 | } else { |
||
154 | $data['status'] = 'error'; |
||
155 | $postParams[$postKey] = 'off'; |
||
156 | } |
||
157 | |||
158 | set_request($request->withParsedBody($postParams)); |
||
159 | 11 | ||
160 | $this->saveConfig(); |
||
161 | 11 | ||
162 | // @codeCoverageIgnoreEnd |
||
163 | 11 | ||
164 | $data['result']['postKey'] = $postKey; |
||
165 | |||
166 | $output = json_encode($data); |
||
167 | |||
168 | return $this->respondJson($output); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Respond the JSON format result. |
||
173 | 12 | * |
|
174 | * @param string $output The string you want to output to the browser. |
||
175 | 12 | * |
|
176 | * @return ResponseInterface |
||
177 | 12 | */ |
|
178 | 12 | private function respondJson($output): ResponseInterface |
|
179 | 12 | { |
|
180 | $response = get_response(); |
||
181 | 12 | ||
182 | 12 | $stream = HttpFactory::createStream(); |
|
183 | 12 | $stream->write($output); |
|
184 | $stream->rewind(); |
||
185 | 12 | ||
186 | $response = $response->withHeader('Content-Type', 'application/json'); |
||
187 | $response = $response->withAddedHeader('Content-Type', 'charset=utf-8'); |
||
188 | $response = $response->withBody($stream); |
||
189 | |||
190 | return $response; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
191 | } |
||
192 | } |
||
193 |