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
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Panel; |
14
|
|
|
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Shieldon\Firewall\Panel\BaseController; |
17
|
|
|
use Shieldon\Messenger as Messenger; |
18
|
|
|
use function Shieldon\Firewall\__; |
19
|
|
|
use function Shieldon\Firewall\get_request; |
20
|
|
|
use function Shieldon\Firewall\get_response; |
21
|
|
|
use function Shieldon\Firewall\get_session; |
22
|
|
|
use function Shieldon\Firewall\set_request; |
23
|
|
|
use function explode; |
24
|
|
|
use function filter_var; |
25
|
|
|
use function gethostname; |
26
|
|
|
use function is_numeric; |
27
|
|
|
use function json_encode; |
28
|
|
|
use function str_replace; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* User |
32
|
|
|
*/ |
33
|
|
|
class Ajax extends BaseController |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
parent::__construct(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Fallback for undefined methods. |
45
|
|
|
* |
46
|
|
|
* @param string $function The method name. |
47
|
|
|
* @param array $args The arguments. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function __call($function , $args) |
52
|
|
|
{ |
53
|
|
|
$className = 'Shieldon\Firewall\Panel\Sandbox\\' . $function; |
54
|
|
|
|
55
|
|
|
if (file_exists(__DIR__ . '/Sandbox/' . $function . '.php')) { |
56
|
|
|
$sandbox = new $className(); |
57
|
|
|
return $sandbox($args); |
58
|
|
|
} |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Change the user's language of the UI. |
64
|
|
|
* |
65
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
66
|
|
|
*/ |
67
|
|
|
public function changeLocale(): ResponseInterface |
68
|
|
|
{ |
69
|
|
|
$langCode = get_request()->getQueryParams()['langCode'] ?? 'en'; |
70
|
|
|
get_session()->set('shieldon_panel_lang', $langCode); |
71
|
|
|
|
72
|
|
|
$data['status'] = 'success'; |
|
|
|
|
73
|
|
|
$data['lang_code'] = $langCode; |
74
|
|
|
$data['session_lang_code'] = $langCode; |
75
|
|
|
|
76
|
|
|
$output = json_encode($data); |
77
|
|
|
|
78
|
|
|
return $this->respondJson($output); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Test messenger modules. |
83
|
|
|
* |
84
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
85
|
|
|
*/ |
86
|
|
|
public function tryMessenger(): ResponseInterface |
87
|
|
|
{ |
88
|
|
|
$request = get_request(); |
89
|
|
|
$message = []; |
90
|
|
|
|
91
|
|
|
$getParams = $request->getQueryParams(); |
92
|
|
|
$serverParams = $request->getServerParams(); |
93
|
|
|
|
94
|
|
|
$serverName = $serverParams['SERVER_NAME'] ?? gethostname(); |
95
|
|
|
$moduleName = $getParams['module'] ?? ''; |
96
|
|
|
|
97
|
|
|
$data = []; |
98
|
|
|
$data['status'] = 'undefined'; |
99
|
|
|
$data['result']['moduleName'] = $moduleName; |
100
|
|
|
|
101
|
|
|
$message['title'] = __('panel', 'test_msg_title', 'Testing Message from Host: ') . $serverName; |
102
|
|
|
$message['body'] = __('panel', 'test_msg_body', 'Messenger module "{0}" has been tested and confirmed successfully.', [$moduleName]); |
103
|
|
|
|
104
|
|
|
// @codeCoverageIgnoreStart |
105
|
|
|
|
106
|
|
|
// Name the testing method. |
107
|
|
|
$method = explode('-', $moduleName); |
108
|
|
|
$method = implode('', array_map(function($word) { |
109
|
|
|
return ucwords($word); |
110
|
|
|
}, $method)); |
111
|
|
|
|
112
|
|
|
// Call testing method if exists. |
113
|
|
|
if ($this->{$method}($getParams, $message)) { |
114
|
|
|
$data['status'] = 'success'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$postParams = $request->getParsedBody(); |
118
|
|
|
$postKey = 'messengers__' . $moduleName . '__confirm_test'; |
119
|
|
|
|
120
|
|
|
if ('success' === $data['status']) { |
121
|
|
|
$postParams[$postKey] = 'on'; |
122
|
|
|
$this->saveConfig(); |
123
|
|
|
|
124
|
|
|
} elseif ('error' === $data['status']) { |
125
|
|
|
$postParams[$postKey] = 'off'; |
126
|
|
|
$this->saveConfig(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
set_request($request->withParsedBody($postParams)); |
130
|
|
|
|
131
|
|
|
// @codeCoverageIgnoreEnd |
132
|
|
|
|
133
|
|
|
$data['result']['postKey'] = $postKey; |
134
|
|
|
|
135
|
|
|
$output = json_encode($data); |
136
|
|
|
|
137
|
|
|
return $this->respondJson($output); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Respond the JSON format result. |
142
|
|
|
* |
143
|
|
|
* @param string $output The string you want to output to the browser. |
144
|
|
|
* |
145
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
146
|
|
|
*/ |
147
|
|
|
private function respondJson($output): ResponseInterface |
148
|
|
|
{ |
149
|
|
|
$response = get_response(); |
150
|
|
|
|
151
|
|
|
$stream = $response->getBody(); |
152
|
|
|
$stream->write($output); |
153
|
|
|
$stream->rewind(); |
154
|
|
|
|
155
|
|
|
$response = $response->withHeader('Content-Type', 'application/json'); |
156
|
|
|
$response = $response->withAddedHeader('Content-Type', 'charset=utf-8'); |
157
|
|
|
$response = $response->withBody($stream); |
158
|
|
|
|
159
|
|
|
return $response; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|