Completed
Push — master ( cd768d...07a487 )
by Daniel
02:29
created

Informator   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 308
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 41
Bugs 0 Features 10
Metric Value
wmc 34
c 41
b 0
f 10
lcom 1
cbo 4
dl 0
loc 308
rs 9.2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getApacheModules() 0 14 2
A getClientBrowserDetailsForInformator() 0 10 3
B getMySQLinfo() 0 26 3
B getPhpDetails() 0 29 4
B getServerDetails() 0 29 2
B __construct() 0 36 1
A getApacheDetails() 0 20 2
A getServerDetailsFromPhp() 0 20 2
A getTemporaryFolder() 0 4 1
A getTomcatDetails() 0 12 3
B setInterface() 0 24 3
A performLabelDefinition() 0 18 4
B setOutputInterface() 0 25 3
A systemInfo() 0 14 1
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\informator;
30
31
class Informator
32
{
33
34
    use \danielgp\common_lib\CommonCode,
35
        InformatorDynamicFunctions,
36
        InformatorServer;
37
38
    private $informatorInternalArray;
39
40
    public function __construct()
41
    {
42
        $this->informatorInternalArray['composerLockFile'] = realpath('../') . DIRECTORY_SEPARATOR . 'composer.lock';
43
        $this->informatorInternalArray['knownLabels']      = [
44
            '--- List of known labels' => '',
45
            'Apache Info'              => ['getApacheDetails'],
46
            'Auto Dependencies'        => [
47
                'getPackageDetailsFromGivenComposerLockFile',
48
                $this->informatorInternalArray['composerLockFile'],
49
            ],
50
            'Auto Dependencies File'   => [$this->informatorInternalArray['composerLockFile']],
51
            'Client Info'              => ['getClientBrowserDetailsForInformator', null],
52
            'Informator File Details'  => ['getFileDetails', __FILE__],
53
            'MySQL Databases All'      => ['getMySQLinfo', ['Databases All']],
54
            'MySQL Databases Client'   => ['getMySQLinfo', ['Databases Client']],
55
            'MySQL Engines Active'     => ['getMySQLinfo', ['Engines Active']],
56
            'MySQL Engines All'        => ['getMySQLinfo', ['Engines All']],
57
            'MySQL General'            => ['getMySQLinfo', ['General']],
58
            'MySQL Variables Global'   => ['getMySQLinfo', ['Variables Global']],
59
            'MySQL Info'               => ['getMySQLinfo', ['Engines Active', 'General', 'Variables Global']],
60
            'Php Extensions Loaded'    => ['getPhpDetails', ['Extensions Loaded']],
61
            'Php General'              => ['getPhpDetails', ['General']],
62
            'Php INI Settings'         => ['getPhpDetails', ['INI Settings']],
63
            'Php Stream Filters'       => ['getPhpDetails', ['Stream Filters']],
64
            'Php Stream Transports'    => ['getPhpDetails', ['Stream Transports']],
65
            'Php Stream Wrappers'      => ['getPhpDetails', ['Stream Wrappers']],
66
            'Php Info'                 => ['getPhpDetails'],
67
            'Server Info'              => ['getServerDetails'],
68
            'System Info'              => ['systemInfo'],
69
            'Tomcat Info'              => ['getTomcatDetails'],
70
        ];
71
        ksort($this->informatorInternalArray['knownLabels']);
72
        $rqst                                              = new \Symfony\Component\HttpFoundation\Request;
73
        $this->informatorInternalArray['superGlobals']     = $rqst->createFromGlobals();
74
        echo $this->setInterface();
75
    }
76
77
    private function getApacheDetails()
78
    {
79
        $srvSoftwareArray = explode(' ', $this->getServerSoftware());
80
        $sInfo            = [];
81
        $tmp              = explode('/', $srvSoftwareArray[0]);
82
        if (strpos($srvSoftwareArray[0], 'Apache') !== false) {
83
            $sInfo['Apache'] = [
84
                'Name'      => $tmp[0],
85
                'Signature' => $this->getServerSoftware(),
86
                'Version'   => $tmp[1]
87
            ];
88
        }
89
        $modulesToDisregard         = [
90
            $srvSoftwareArray[0],
91
            '(Win64)',
92
        ];
93
        $sInfo['Apache']['Modules'] = $this->getApacheModules(array_diff($srvSoftwareArray, $modulesToDisregard));
94
        ksort($sInfo['Apache']);
95
        return $sInfo['Apache'];
96
    }
97
98
    private function getApacheModules(array $srvSoftwareArray)
99
    {
100
        $aReturn = [];
101
        foreach ($srvSoftwareArray as $value) {
102
            $tmp                  = explode('/', $value);
103
            $rootModule           = strtolower(str_replace(['mod_', 'OpenSSL'], ['', 'SSL'], $tmp[0]));
104
            $aReturn[$rootModule] = [
105
                'Name'    => $tmp[0],
106
                'Version' => $tmp[1]
107
            ];
108
        }
109
        ksort($aReturn);
110
        return $aReturn;
111
    }
112
113
    private function getClientBrowserDetailsForInformator()
114
    {
115
        $tmpFolder        = $this->getTemporaryFolder();
116
        $tmpDoctrineCache = null;
117
        clearstatcache();
118
        if (is_dir($tmpFolder) && is_writable($tmpFolder)) {
119
            $tmpDoctrineCache = $tmpFolder . DIRECTORY_SEPARATOR . 'DoctrineCache';
120
        }
121
        return $this->getClientBrowserDetails(['Browser', 'Device', 'OS'], $tmpDoctrineCache);
122
    }
123
124
    private function getMySQLinfo($returnType = ['Engines Active', 'General', 'Variables Global'])
125
    {
126
        if (is_null($this->mySQLconnection)) {
127
            $this->connectToMySql([
128
                'host'     => MYSQL_HOST,
129
                'port'     => MYSQL_PORT,
130
                'username' => MYSQL_USERNAME,
131
                'password' => MYSQL_PASSWORD,
132
                'database' => MYSQL_DATABASE,
133
            ]);
134
        }
135
        $sInfo           = [];
136
        $aMySQLinfoLabel = [
137
            'Databases All'    => ['getMySQLlistDatabases', false],
138
            'Databases Client' => ['getMySQLlistDatabases', true],
139
            'Engines Active'   => ['getMySQLlistEngines', true],
140
            'Engines All'      => ['getMySQLlistEngines', false],
141
            'General'          => ['getMySQLgenericInformations'],
142
            'Variables Global' => ['getMySQLglobalVariables'],
143
        ];
144
        foreach ($returnType as $value) {
145
            $sInfo['MySQL'][$value] = $this->callDynamicFunctionToGetResults($aMySQLinfoLabel[$value]);
146
        }
147
        ksort($sInfo['MySQL']);
148
        return $sInfo['MySQL'];
149
    }
150
151
    private function getPhpDetails($returnType = ['General', 'INI Settings', 'Extensions Loaded', 'Temporary Folder'])
152
    {
153
        $sInfo = [];
154
        foreach ($returnType as $value) {
155
            switch ($value) {
156
                case 'General':
157
                    $sInfo['PHP'][$value] = [
158
                        'Version'             => phpversion(),
159
                        'Zend Engine Version' => zend_version(),
160
                    ];
161
                    break;
162
                case 'INI Settings':
163
                    $sInfo['PHP'][$value] = ini_get_all(null, false);
164
                    break;
165
                default:
166
                    $stLabels             = [
167
                        'Extensions Loaded' => ['setArrayValuesAsKey', 'get_loaded_extensions'],
168
                        'Stream Filters'    => ['setArrayValuesAsKey', 'stream_get_filters'],
169
                        'Stream Transports' => ['setArrayValuesAsKey', 'stream_get_transports'],
170
                        'Stream Wrappers'   => ['setArrayValuesAsKey', 'stream_get_wrappers'],
171
                        'Temporary Folder'  => ['getTemporaryFolder'],
172
                    ];
173
                    $sInfo['PHP'][$value] = $this->callDynamicFunctionToGetResults($stLabels[$value]);
174
                    break;
175
            }
176
        }
177
        ksort($sInfo['PHP']);
178
        return $sInfo['PHP'];
179
    }
180
181
    private function getServerDetails()
182
    {
183
        $serverMachineType = 'unknown';
184
        $hst               = $this->informatorInternalArray['superGlobals']->getHttpHost();
185
        $serverInfo        = [
186
            'name'    => 'undisclosed',
187
            'host'    => $hst,
188
            'release' => 'undisclosed',
189
            'version' => 'undisclosed',
190
        ];
191
        if (function_exists('php_uname')) {
192
            $infServerFromPhp  = $this->getServerDetailsFromPhp();
193
            $serverMachineType = $infServerFromPhp['OS Architecture'];
194
            $serverInfo        = $infServerFromPhp['OS Name+Host+Release+Version'];
195
        }
196
        $srvIp = filter_var(gethostbyname($hst), FILTER_VALIDATE_IP);
197
        return [
198
            'OS'              => php_uname(),
199
            'OS Architecture' => $serverMachineType,
200
            'OS Date/time'    => date('Y-m-d H:i:s'),
201
            'OS Ip'           => $srvIp,
202
            'OS Ip type'      => $this->checkIpIsPrivate($srvIp),
203
            'OS Ip v4/v6'     => $this->checkIpIsV4OrV6($srvIp),
204
            'OS Name'         => $serverInfo['name'],
205
            'OS Host'         => $serverInfo['host'],
206
            'OS Release'      => $serverInfo['release'],
207
            'OS Version'      => $serverInfo['version'],
208
        ];
209
    }
210
211
    private function getServerDetailsFromPhp()
212
    {
213
        $aReturn                    = [];
214
        $aReturn['OS Architecture'] = php_uname('m');
215
        $knownValues                = [
216
            'AMD64' => 'x64 (64 bit)',
217
            'i386'  => 'x86 (32 bit)',
218
            'i586'  => 'x86 (32 bit)',
219
        ];
220
        if (array_key_exists(php_uname('m'), $knownValues)) {
221
            $aReturn['OS Architecture'] = $knownValues[php_uname('m')];
222
        }
223
        $aReturn['OS Name+Host+Release+Version'] = [
224
            'name'    => php_uname('s'),
225
            'host'    => php_uname('n'),
226
            'release' => php_uname('r'),
227
            'version' => php_uname('v'),
228
        ];
229
        return $aReturn;
230
    }
231
232
    private function getTemporaryFolder()
233
    {
234
        return sys_get_temp_dir() . DIRECTORY_SEPARATOR;
235
    }
236
237
    private function getTomcatDetails()
238
    {
239
        $sReturn           = [];
240
        $sReturn['Tomcat'] = '---';
241
        $url               = 'http://' . $this->informatorInternalArray['superGlobals']->getHttpHost()
242
                . ':8080/informator.Tomcat/index.jsp';
243
        $urlFeedback       = $this->getContentFromUrlThroughCurlAsArrayIfJson($url);
244
        if (is_array($urlFeedback) && isset($urlFeedback['response'])) {
245
            $sReturn['Tomcat'] = $urlFeedback['response'];
246
        }
247
        return $sReturn;
248
    }
249
250
    private function setInterface()
251
    {
252
        $requestedLabel = $this->informatorInternalArray['superGlobals']->get('Label');
253
        $showLabels     = true;
254
        $feedback       = '<span style="background-color:red;color:white;">Label not set...</span>';
255
        $arToReturn     = [];
256
        if (isset($requestedLabel)) {
257
            $feedback = '<span style="background-color:red;color:white;">'
258
                    . 'Unknown label transmited...'
259
                    . '</span>';
260
            if (array_key_exists($requestedLabel, $this->informatorInternalArray['knownLabels'])) {
261
                $showLabels = false;
262
                $feedback   = '';
263
                $lblValue   = $this->informatorInternalArray['knownLabels'][$requestedLabel];
264
                $arToReturn = $this->performLabelDefinition($requestedLabel, $lblValue);
265
            }
266
        }
267
        $outputArray = [
268
            'showLabels'    => $showLabels,
269
            'feedback'      => $feedback,
270
            'arrayToReturn' => $arToReturn,
271
        ];
272
        return $this->setOutputInterface($outputArray);
273
    }
274
275
    private function performLabelDefinition($requestedLabel, $lblValue)
276
    {
277
        switch ($requestedLabel) {
278
            case '--- List of known labels':
279
                $arToReturn = array_keys($this->informatorInternalArray['knownLabels']);
280
                break;
281
            case 'Auto Dependencies File':
282
                $arToReturn = $lblValue;
283
                break;
284
            case 'System Info':
285
                $arToReturn = $this->systemInfo();
286
                break;
287
            default:
288
                $arToReturn = $this->callDynamicFunctionToGetResults($lblValue);
289
                break;
290
        }
291
        return $arToReturn;
292
    }
293
294
    private function setOutputInterface($inArray)
295
    {
296
        if ($inArray['showLabels']) {
297
            $sReturn    = [];
298
            $sReturn[]  = $this->setHeaderCommon([
299
                'lang'  => 'en-US',
300
                'title' => 'Informator'
301
            ]);
302
            $sReturn[]  = $inArray['feedback'] . '<p style="background-color:green;color:white;">'
303
                    . 'So you might want to choose one from the list below:</p>'
304
                    . '<ul>';
305
            $arToReturn = array_keys($this->informatorInternalArray['knownLabels']);
306
            foreach ($arToReturn as $value) {
307
                $sReturn[] = '<li>'
308
                        . '<a href="?Label=' . urlencode($value) . '" target="_blank">' . $value . '</a>'
309
                        . '</li>';
310
            }
311
            $sReturn[] = '</ul>' . $this->setFooterCommon();
312
            return implode('', $sReturn);
313
        }
314
        $this->setHeaderGZiped();
315
        $this->setHeaderNoCache('application/json');
316
        echo $this->setArrayToJson($inArray['arrayToReturn']);
317
        $this->setFooterGZiped();
318
    }
319
320
    /**
321
     * Builds an array with most important key aspects of LAMP/WAMP
322
     * @return array
323
     */
324
    private function systemInfo()
325
    {
326
        $cFile = $this->informatorInternalArray['composerLockFile'];
327
        return [
328
            'Apache'            => $this->getApacheDetails(),
329
            'Auto Dependencies' => $this->getPackageDetailsFromGivenComposerLockFile($cFile),
330
            'Client'            => $this->getClientBrowserDetailsForInformator(),
331
            'InfoCompareFile'   => $this->getFileDetails(__FILE__),
332
            'MySQL'             => $this->getMySQLinfo(),
333
            'PHP'               => $this->getPhpDetails(),
334
            'Server'            => $this->getServerDetails(),
335
            'Tomcat'            => $this->getTomcatDetails()['Tomcat'],
336
        ];
337
    }
338
}
339