1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
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 <contact@terryl.in>
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 3
    public function __construct() 
58
    {
59 3
        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 3
    public function __call($function , $args)
71
    {
72 3
        $className = 'Shieldon\Firewall\Panel\Sandbox\\' . $function;
73

74 3
        if (file_exists(__DIR__ . '/Sandbox/' . $function . '.php')) {
75 3
            $sandbox = new $className();
76 3
            return $sandbox($args);
77
        }
78 3
        return false;
79
    }
80

81
    /**
82
     * Change the user's language of the UI.
83
     *
84
     * @return ResponseInterface
85
     */
86 3
    public function changeLocale(): ResponseInterface
87
    {
88 3
        $langCode = get_request()->getQueryParams()['langCode'] ?? 'en';
89 3
        get_session_instance()->set('shieldon_panel_lang', $langCode);
90 3
        get_session_instance()->save();
91 3
        $data = [];
92

93 3
        $data['status'] = 'success';
94 3
        $data['lang_code'] = $langCode;
95 3
        $data['session_lang_code'] = $langCode;
96
 
97 3
        $output = json_encode($data);
98

99 3
        return $this->respondJson($output);
100
    }
101

102
    /**
103
     * Test messenger modules.
104
     *
105
     * @return ResponseInterface
106
     */
107 3
    public function tryMessenger(): ResponseInterface
108
    {
109 3
        $request = get_request();
110 3
        $message = [];
111

112 3
        $getParams = $request->getQueryParams();
113 3
        $serverParams = $request->getServerParams();
114

115 3
        $serverName = $serverParams['SERVER_NAME'] ?? gethostname();
116 3
        $moduleName = $getParams['module'] ?? '';
117

118 3
        $data = [];
119 3
        $data['status'] = 'undefined';
120 3
        $data['result']['moduleName'] = $moduleName;
121

122 3
        $message['title'] = __('panel', 'test_msg_title', 'Testing Message from Host: ') . $serverName;
123 3
        $message['body'] = __('panel', 'test_msg_body', 'Messenger module "{0}" has been tested and confirmed successfully.', [$moduleName]);
124
    
125
        // @codeCoverageIgnoreStart
126

127
        // Name the testing method.
128
        $method = explode('-', $moduleName);
129
        $method = implode(
130
            '', 
131
            array_map(
132
                function ($word) {
133
                    return ucwords($word); 
134
                }, 
135
                $method
136
            )
137
        );
138

139
        $postParams = $request->getParsedBody();
140
        $postKey = 'messengers__' . $moduleName . '__confirm_test';
141

142
        // Call testing method if exists.
143
        $status = $this->{$method}($getParams, $message);
144

145
        if ($status) {
146
            $data['status'] = 'success';
147
            $postParams[$postKey] = 'on';
148
        } else {
149
            $data['status'] = 'error';
150
            $postParams[$postKey] = 'off'; 
151
        }
152

153
        set_request($request->withParsedBody($postParams));
154

155
        $this->saveConfig();
156

157
        // @codeCoverageIgnoreEnd
158

159 3
        $data['result']['postKey'] = $postKey;
160

161 3
        $output = json_encode($data);
162

163 3
        return $this->respondJson($output);
164
    }
165

166
    /**
167
     * Respond the JSON format result.
168
     * 
169
     * @param string $output The string you want to output to the browser.
170
     *
171
     * @return ResponseInterface
172
     */
173 3
    private function respondJson($output): ResponseInterface
174
    {
175 3
        $response = get_response();
176

177 3
        $stream = HttpFactory::createStream();
178 3
        $stream->write($output);
179 3
        $stream->rewind();
180

181 3
        $response = $response->withHeader('Content-Type', 'application/json');
182 3
        $response = $response->withAddedHeader('Content-Type', 'charset=utf-8');
183 3
        $response = $response->withBody($stream);
184

185 3
        return $response;
186
    }
187
}

Read our documentation on viewing source code .

Loading