Completed
Push — master ( fa6f45...27cd9e )
by Daniel
02:16
created

Informator::getPhpDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 15
Bugs 0 Features 4
Metric Value
c 15
b 0
f 4
dl 0
loc 9
rs 9.6667
cc 2
eloc 6
nc 2
nop 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
        InformatorKnownLabels,
37
        InformatorServer;
38
39
    private $informatorInternalArray;
40
41
    public function __construct()
42
    {
43
        $this->informatorInternalArray['composerLockFile'] = realpath('../') . DIRECTORY_SEPARATOR . 'composer.lock';
44
        $this->informatorInternalArray['knownLabels']      = $this->knownLabelsGlobal([
45
            'composerLockFile' => $this->informatorInternalArray['composerLockFile'],
46
            'informatorFile'   => __FILE__,
47
        ]);
48
        ksort($this->informatorInternalArray['knownLabels']);
49
        $rqst                                              = new \Symfony\Component\HttpFoundation\Request;
50
        $this->informatorInternalArray['superGlobals']     = $rqst->createFromGlobals();
51
        echo $this->setInterface();
52
    }
53
54
    private function connectToMySqlForInformation()
55
    {
56
        if (is_null($this->mySQLconnection)) {
57
            $this->connectToMySql([
58
                'host'     => MYSQL_HOST,
59
                'port'     => MYSQL_PORT,
60
                'username' => MYSQL_USERNAME,
61
                'password' => MYSQL_PASSWORD,
62
                'database' => MYSQL_DATABASE,
63
            ]);
64
        }
65
    }
66
67
    private function getApacheDetails()
68
    {
69
        $srvSoftwareArray = explode(' ', $this->getServerSoftware());
70
        $sInfo            = [];
71
        $tmp              = explode('/', $srvSoftwareArray[0]);
72
        if (strpos($srvSoftwareArray[0], 'Apache') !== false) {
73
            $sInfo['Apache'] = [
74
                'Name'      => $tmp[0],
75
                'Signature' => $this->getServerSoftware(),
76
                'Version'   => $tmp[1]
77
            ];
78
        }
79
        $modulesToDisregard         = [
80
            $srvSoftwareArray[0],
81
            '(Win64)',
82
        ];
83
        $sInfo['Apache']['Modules'] = $this->getApacheModules(array_diff($srvSoftwareArray, $modulesToDisregard));
84
        ksort($sInfo['Apache']);
85
        return $sInfo['Apache'];
86
    }
87
88
    private function getApacheModules(array $srvSoftwareArray)
89
    {
90
        $aReturn = [];
91
        foreach ($srvSoftwareArray as $value) {
92
            $tmp                  = explode('/', $value);
93
            $rootModule           = strtolower(str_replace(['mod_', 'OpenSSL'], ['', 'SSL'], $tmp[0]));
94
            $aReturn[$rootModule] = [
95
                'Name'    => $tmp[0],
96
                'Version' => $tmp[1]
97
            ];
98
        }
99
        ksort($aReturn);
100
        return $aReturn;
101
    }
102
103
    private function getClientBrowserDetailsForInformator()
104
    {
105
        return $this->getClientBrowserDetails(['Browser', 'Device', 'OS'], $this->getDoctrineCaheFolder());
106
    }
107
108
    private function getMySQLinfo($returnType)
109
    {
110
        $this->connectToMySqlForInformation();
111
        $sInfo = [];
112
        foreach ($returnType as $value) {
113
            $sInfo['MySQL'][$value] = $this->callDynamicFunctionToGetResults($this->knownLabelsForMySql()[$value]);
114
        }
115
        ksort($sInfo['MySQL']);
116
        return $sInfo['MySQL'];
117
    }
118
119
    private function getPhpDetails($returnType = ['General', 'INI Settings', 'Extensions Loaded', 'Temporary Folder'])
120
    {
121
        $sInfo = [];
122
        foreach ($returnType as $value) {
123
            $sInfo['PHP'][$value] = $this->getPhpDetailsIndividually($value);
124
        }
125
        ksort($sInfo['PHP']);
126
        return $sInfo['PHP'];
127
    }
128
129
    private function getPhpDetailsIndividually($value)
130
    {
131
        switch ($value) {
132
            case 'General':
133
                $sInfo = [
134
                    'Version'             => phpversion(),
135
                    'Zend Engine Version' => zend_version(),
136
                ];
137
                break;
138
            case 'INI Settings':
139
                $sInfo = ini_get_all(null, false);
140
                break;
141
            default:
142
                $sInfo = $this->callDynamicFunctionToGetResults($this->knownLabelsForPhp()[$value]);
143
                break;
144
        }
145
        return $sInfo;
146
    }
147
148
    private function getServerDetails()
149
    {
150
        $serverMachineType = 'unknown';
151
        $hst               = $this->informatorInternalArray['superGlobals']->getHttpHost();
152
        $serverInfo        = [
153
            'name'    => 'undisclosed',
154
            'host'    => $hst,
155
            'release' => 'undisclosed',
156
            'version' => 'undisclosed',
157
        ];
158
        if (function_exists('php_uname')) {
159
            $infServerFromPhp  = $this->getServerDetailsFromPhp();
160
            $serverMachineType = $infServerFromPhp['OS Architecture'];
161
            $serverInfo        = $infServerFromPhp['OS Name+Host+Release+Version'];
162
        }
163
        $srvIp = filter_var(gethostbyname($hst), FILTER_VALIDATE_IP);
164
        return [
165
            'OS'              => php_uname(),
166
            'OS Architecture' => $serverMachineType,
167
            'OS Date/time'    => date('Y-m-d H:i:s'),
168
            'OS Ip'           => $srvIp,
169
            'OS Ip type'      => $this->checkIpIsPrivate($srvIp),
170
            'OS Ip v4/v6'     => $this->checkIpIsV4OrV6($srvIp),
171
            'OS Name'         => $serverInfo['name'],
172
            'OS Host'         => $serverInfo['host'],
173
            'OS Release'      => $serverInfo['release'],
174
            'OS Version'      => $serverInfo['version'],
175
        ];
176
    }
177
178
    private function getServerDetailsFromPhp()
179
    {
180
        $aReturn                    = [];
181
        $aReturn['OS Architecture'] = php_uname('m');
182
        $knownValues                = [
183
            'AMD64' => 'x64 (64 bit)',
184
            'i386'  => 'x86 (32 bit)',
185
            'i586'  => 'x86 (32 bit)',
186
        ];
187
        if (array_key_exists(php_uname('m'), $knownValues)) {
188
            $aReturn['OS Architecture'] = $knownValues[php_uname('m')];
189
        }
190
        $aReturn['OS Name+Host+Release+Version'] = [
191
            'name'    => php_uname('s'),
192
            'host'    => php_uname('n'),
193
            'release' => php_uname('r'),
194
            'version' => php_uname('v'),
195
        ];
196
        return $aReturn;
197
    }
198
199
    private function getTomcatDetails()
200
    {
201
        $sReturn           = [];
202
        $sReturn['Tomcat'] = '---';
203
        $url               = 'http://' . $this->informatorInternalArray['superGlobals']->getHttpHost()
204
                . ':8080/informator.Tomcat/index.jsp';
205
        $urlFeedback       = $this->getContentFromUrlThroughCurlAsArrayIfJson($url);
206
        if (is_array($urlFeedback) && isset($urlFeedback['response'])) {
207
            $sReturn['Tomcat'] = $urlFeedback['response'];
208
        }
209
        return $sReturn;
210
    }
211
212
    private function setInterface()
213
    {
214
        $requestedLabel = $this->informatorInternalArray['superGlobals']->get('Label');
215
        $showLabels     = true;
216
        $feedback       = '<span style="background-color:red;color:white;">Label not set...</span>';
217
        $arToReturn     = [];
218
        if (isset($requestedLabel)) {
219
            $feedback = '<span style="background-color:red;color:white;">'
220
                    . 'Unknown label transmited...'
221
                    . '</span>';
222
            if (array_key_exists($requestedLabel, $this->informatorInternalArray['knownLabels'])) {
223
                $showLabels = false;
224
                $feedback   = '';
225
                $lblValue   = $this->informatorInternalArray['knownLabels'][$requestedLabel];
226
                $arToReturn = $this->performLabelDefinition($requestedLabel, $lblValue);
227
            }
228
        }
229
        $outputArray = [
230
            'showLabels'    => $showLabels,
231
            'feedback'      => $feedback,
232
            'arrayToReturn' => $arToReturn,
233
        ];
234
        return $this->setOutputInterface($outputArray);
235
    }
236
237
    private function performLabelDefinition($requestedLabel, $lblValue)
238
    {
239
        switch ($requestedLabel) {
240
            case '--- List of known labels':
241
                $arToReturn = array_keys($this->informatorInternalArray['knownLabels']);
242
                break;
243
            case 'Auto Dependencies File':
244
                $arToReturn = $lblValue;
245
                break;
246
            case 'System Info':
247
                $arToReturn = $this->systemInfo();
248
                break;
249
            default:
250
                $arToReturn = $this->callDynamicFunctionToGetResults($lblValue);
251
                break;
252
        }
253
        return $arToReturn;
254
    }
255
256
    private function setOutputInterface($inArray)
257
    {
258
        if ($inArray['showLabels']) {
259
            $sReturn    = [];
260
            $sReturn[]  = $this->setHeaderCommon([
261
                'lang'  => 'en-US',
262
                'title' => 'Informator'
263
            ]);
264
            $sReturn[]  = $inArray['feedback'] . '<p style="background-color:green;color:white;">'
265
                    . 'So you might want to choose one from the list below:</p>'
266
                    . '<ul>';
267
            $arToReturn = array_keys($this->informatorInternalArray['knownLabels']);
268
            foreach ($arToReturn as $value) {
269
                $sReturn[] = '<li>'
270
                        . '<a href="?Label=' . urlencode($value) . '" target="_blank">' . $value . '</a>'
271
                        . '</li>';
272
            }
273
            $sReturn[] = '</ul>' . $this->setFooterCommon();
274
            return implode('', $sReturn);
275
        }
276
        $this->setHeaderGZiped();
277
        $this->setHeaderNoCache('application/json');
278
        echo $this->setArrayToJson($inArray['arrayToReturn']);
279
        $this->setFooterGZiped();
280
    }
281
282
    /**
283
     * Builds an array with most important key aspects of LAMP/WAMP
284
     * @return array
285
     */
286
    private function systemInfo()
287
    {
288
        $cFile = $this->informatorInternalArray['composerLockFile'];
289
        return [
290
            'Apache'            => $this->getApacheDetails(),
291
            'Auto Dependencies' => $this->getPackageDetailsFromGivenComposerLockFile($cFile),
292
            'Client'            => $this->getClientBrowserDetailsForInformator(),
293
            'InfoCompareFile'   => $this->getFileDetails(__FILE__),
294
            'MySQL'             => $this->getMySQLinfo(),
0 ignored issues
show
Bug introduced by
The call to getMySQLinfo() misses a required argument $returnType.

This check looks for function calls that miss required arguments.

Loading history...
295
            'PHP'               => $this->getPhpDetails(),
296
            'Server'            => $this->getServerDetails(),
297
            'Tomcat'            => $this->getTomcatDetails()['Tomcat'],
298
        ];
299
    }
300
}
301