Issues (13)

src/Module/SystemInfo.php (5 issues)

1
<?php
2
3
namespace BiffBangPow\SSMonitor\Client\Module;
4
5
use BiffBangPow\SSMonitor\Client\Core\ClientCommon;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Promise;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Environment;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\ORM\DB;
12
use SilverStripe\View\ArrayData;
13
use SilverStripe\View\SSViewer;
14
15
class SystemInfo implements \BiffBangPow\SSMonitor\Client\Core\ClientInterface
16
{
17
    use ClientCommon;
18
    use Configurable;
19
20
    /**
21
     * @var string
22
     */
23
    private string $clientName = 'systeminfo';
0 ignored issues
show
The private property $clientName is not used, and could be removed.
Loading history...
24
25
    /**
26
     * @config
27
     * @var string
28
     */
29
    private static $client_title = 'System info';
0 ignored issues
show
The private property $client_title is not used, and could be removed.
Loading history...
30
31
    /**
32
     * @config
33
     * @var array
34
     */
35
    private static $env_variables = [];
0 ignored issues
show
The private property $env_variables is not used, and could be removed.
Loading history...
36
37
    /**
38
     * @config
39
     * @var bool
40
     */
41
    private static $discover_public_ip = true;
0 ignored issues
show
The private property $discover_public_ip is not used, and could be removed.
Loading history...
42
43
44
    public function getResult($config = null): array
45
    {
46
        $conn = DB::get_conn();
47
        // Assumes database class is like "MySQLDatabase" or "MSSQLDatabase" (eg. suffixed with "Database")
48
        $dbType = substr(strrchr(get_class($conn), '\\'), 1, -8);
49
        $dbVersion = $conn->getVersion();
50
        $databaseName = $conn->getSelectedDatabase();
51
52
        $info = [
53
            'php' => [
54
                'label' => _t(__CLASS__ . '.phpversion', 'PHP version'),
55
                'value' => phpversion()
56
            ],
57
            'hostip' => [
58
                'label' => _t(__CLASS__ . '.hostip', 'IP address'),
59
                'value' => $_SERVER['SERVER_ADDR']
60
            ],
61
            'memorylimit' => [
62
                'label' => _t(__CLASS__ . '.memorylimit', 'Memory limit'),
63
                'value' => ini_get('memory_limit')
64
            ],
65
            'uploadlimit' => [
66
                'label' => _t(__CLASS__ . '.uploadlimit', 'Upload limit'),
67
                'value' => ini_get('upload_max_filesize')
68
            ],
69
            'maxscripttime' => [
70
                'label' => _t(__CLASS__ . '.scripttime', 'Script execution time limit'),
71
                'value' => ini_get('max_execution_time')
72
            ],
73
            'dbengine' => [
74
                'label' => _t(__CLASS__ . '.dbengine', 'Database engine'),
75
                'value' => $dbType
76
            ],
77
            'dbversion' => [
78
                'label' => _t(__CLASS__ . '.dbversion', 'Database version'),
79
                'value' => $dbVersion
80
            ],
81
            'databasename' => [
82
                'label' => _t(__CLASS__ . '.dbname', 'Database name'),
83
                'value' => $databaseName
84
            ]
85
        ];
86
87
        $envVars = $this->config()->get('env_variables');
88
        $env = [];
89
        foreach ($envVars as $envVar) {
90
            $envData = Environment::getEnv($envVar);
91
            $value = ($envData) ? $envData : _t(__CLASS__ . '.EnvNotSet', 'Not set');
92
            $env[$envVar] = $value;
93
        }
94
95
        if (count($env) > 0) {
96
            $info['environment'] = [
97
                'label' => _t(__CLASS__ . '.environment', 'Environment variables'),
98
                'value' => $env
99
            ];
100
        }
101
102
        if ($this->config()->get('discover_public_ip') === true) {
103
            $info['publicip'] = [
104
                'label' => _t(__CLASS__ . '.publicip', 'Public IP address'),
105
                'value' => $this->getPublicIP()
106
            ];
107
        }
108
109
        return [
110
            $this->getClientName() => $info
111
        ];
112
    }
113
114
    /**
115
     *
116
     * @return \SilverStripe\ORM\FieldType\DBHTMLText
117
     * @throws \Exception
118
     */
119
    public function forTemplate()
120
    {
121
        $data = $this->getResult()[$this->getClientName()];
122
        $variables = ArrayList::create();
123
        $envVariables = false;
124
125
        $env = (isset($data['environment'])) ? $data['environment'] : false;
126
        if ($env) {
127
            unset($data['environment']);
128
            $envVariables = ArrayList::create();
129
            foreach ($env['value'] as $variableName => $value) {
130
                $envVariables->push(ArrayData::create([
131
                    'Variable' => $variableName,
132
                    'Value' => $value
133
                ]));
134
            }
135
        }
136
137
        foreach ($data as $id => $values) {
138
            $variables->push(ArrayData::create([
139
                'Variable' => $values['label'],
140
                'Value' => $values['value']
141
            ]));
142
        }
143
144
        $viewer = new SSViewer('BiffBangPow/SSMonitor/Client/Module/SystemInfo');
145
        return $viewer->process(ArrayData::create([
146
            'Title' => $this->getClientTitle(),
147
            'Variables' => $variables,
148
            'Environment' => $envVariables
149
        ]));
150
    }
151
152
    function getPublicIP()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
    {
154
        $client = new Client();
155
        $promise = $client->getAsync('https://api.ipify.org');
156
157
        try {
158
            $response = $promise->wait();
159
            $ipAddress = $response->getBody()->getContents();
160
        } catch (\Exception $e) {
161
            // Handle exception if request fails
162
            $ipAddress = _t(__CLASS__ . '.publiciperror', 'Error getting IP address');
163
        }
164
165
        return $ipAddress;
166
    }
167
}
168