Completed
Push — master ( 07a487...fa6f45 )
by Daniel
02:33
created

Informator::getMySQLinfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 16
Bugs 0 Features 4
Metric Value
c 16
b 0
f 4
dl 0
loc 10
rs 9.4286
cc 2
eloc 7
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 = ['Engines Active', 'General', 'Variables Global'])
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
            switch ($value) {
124
                case 'General':
125
                    $sInfo['PHP'][$value] = [
126
                        'Version'             => phpversion(),
127
                        'Zend Engine Version' => zend_version(),
128
                    ];
129
                    break;
130
                case 'INI Settings':
131
                    $sInfo['PHP'][$value] = ini_get_all(null, false);
132
                    break;
133
                default:
134
                    $sInfo['PHP'][$value] = $this->callDynamicFunctionToGetResults($this->knownLabelsForPhp()[$value]);
135
                    break;
136
            }
137
        }
138
        ksort($sInfo['PHP']);
139
        return $sInfo['PHP'];
140
    }
141
142
    private function getServerDetails()
143
    {
144
        $serverMachineType = 'unknown';
145
        $hst               = $this->informatorInternalArray['superGlobals']->getHttpHost();
146
        $serverInfo        = [
147
            'name'    => 'undisclosed',
148
            'host'    => $hst,
149
            'release' => 'undisclosed',
150
            'version' => 'undisclosed',
151
        ];
152
        if (function_exists('php_uname')) {
153
            $infServerFromPhp  = $this->getServerDetailsFromPhp();
154
            $serverMachineType = $infServerFromPhp['OS Architecture'];
155
            $serverInfo        = $infServerFromPhp['OS Name+Host+Release+Version'];
156
        }
157
        $srvIp = filter_var(gethostbyname($hst), FILTER_VALIDATE_IP);
158
        return [
159
            'OS'              => php_uname(),
160
            'OS Architecture' => $serverMachineType,
161
            'OS Date/time'    => date('Y-m-d H:i:s'),
162
            'OS Ip'           => $srvIp,
163
            'OS Ip type'      => $this->checkIpIsPrivate($srvIp),
164
            'OS Ip v4/v6'     => $this->checkIpIsV4OrV6($srvIp),
165
            'OS Name'         => $serverInfo['name'],
166
            'OS Host'         => $serverInfo['host'],
167
            'OS Release'      => $serverInfo['release'],
168
            'OS Version'      => $serverInfo['version'],
169
        ];
170
    }
171
172
    private function getServerDetailsFromPhp()
173
    {
174
        $aReturn                    = [];
175
        $aReturn['OS Architecture'] = php_uname('m');
176
        $knownValues                = [
177
            'AMD64' => 'x64 (64 bit)',
178
            'i386'  => 'x86 (32 bit)',
179
            'i586'  => 'x86 (32 bit)',
180
        ];
181
        if (array_key_exists(php_uname('m'), $knownValues)) {
182
            $aReturn['OS Architecture'] = $knownValues[php_uname('m')];
183
        }
184
        $aReturn['OS Name+Host+Release+Version'] = [
185
            'name'    => php_uname('s'),
186
            'host'    => php_uname('n'),
187
            'release' => php_uname('r'),
188
            'version' => php_uname('v'),
189
        ];
190
        return $aReturn;
191
    }
192
193
    private function getTomcatDetails()
194
    {
195
        $sReturn           = [];
196
        $sReturn['Tomcat'] = '---';
197
        $url               = 'http://' . $this->informatorInternalArray['superGlobals']->getHttpHost()
198
                . ':8080/informator.Tomcat/index.jsp';
199
        $urlFeedback       = $this->getContentFromUrlThroughCurlAsArrayIfJson($url);
200
        if (is_array($urlFeedback) && isset($urlFeedback['response'])) {
201
            $sReturn['Tomcat'] = $urlFeedback['response'];
202
        }
203
        return $sReturn;
204
    }
205
206
    private function setInterface()
207
    {
208
        $requestedLabel = $this->informatorInternalArray['superGlobals']->get('Label');
209
        $showLabels     = true;
210
        $feedback       = '<span style="background-color:red;color:white;">Label not set...</span>';
211
        $arToReturn     = [];
212
        if (isset($requestedLabel)) {
213
            $feedback = '<span style="background-color:red;color:white;">'
214
                    . 'Unknown label transmited...'
215
                    . '</span>';
216
            if (array_key_exists($requestedLabel, $this->informatorInternalArray['knownLabels'])) {
217
                $showLabels = false;
218
                $feedback   = '';
219
                $lblValue   = $this->informatorInternalArray['knownLabels'][$requestedLabel];
220
                $arToReturn = $this->performLabelDefinition($requestedLabel, $lblValue);
221
            }
222
        }
223
        $outputArray = [
224
            'showLabels'    => $showLabels,
225
            'feedback'      => $feedback,
226
            'arrayToReturn' => $arToReturn,
227
        ];
228
        return $this->setOutputInterface($outputArray);
229
    }
230
231
    private function performLabelDefinition($requestedLabel, $lblValue)
232
    {
233
        switch ($requestedLabel) {
234
            case '--- List of known labels':
235
                $arToReturn = array_keys($this->informatorInternalArray['knownLabels']);
236
                break;
237
            case 'Auto Dependencies File':
238
                $arToReturn = $lblValue;
239
                break;
240
            case 'System Info':
241
                $arToReturn = $this->systemInfo();
242
                break;
243
            default:
244
                $arToReturn = $this->callDynamicFunctionToGetResults($lblValue);
245
                break;
246
        }
247
        return $arToReturn;
248
    }
249
250
    private function setOutputInterface($inArray)
251
    {
252
        if ($inArray['showLabels']) {
253
            $sReturn    = [];
254
            $sReturn[]  = $this->setHeaderCommon([
255
                'lang'  => 'en-US',
256
                'title' => 'Informator'
257
            ]);
258
            $sReturn[]  = $inArray['feedback'] . '<p style="background-color:green;color:white;">'
259
                    . 'So you might want to choose one from the list below:</p>'
260
                    . '<ul>';
261
            $arToReturn = array_keys($this->informatorInternalArray['knownLabels']);
262
            foreach ($arToReturn as $value) {
263
                $sReturn[] = '<li>'
264
                        . '<a href="?Label=' . urlencode($value) . '" target="_blank">' . $value . '</a>'
265
                        . '</li>';
266
            }
267
            $sReturn[] = '</ul>' . $this->setFooterCommon();
268
            return implode('', $sReturn);
269
        }
270
        $this->setHeaderGZiped();
271
        $this->setHeaderNoCache('application/json');
272
        echo $this->setArrayToJson($inArray['arrayToReturn']);
273
        $this->setFooterGZiped();
274
    }
275
276
    /**
277
     * Builds an array with most important key aspects of LAMP/WAMP
278
     * @return array
279
     */
280
    private function systemInfo()
281
    {
282
        $cFile = $this->informatorInternalArray['composerLockFile'];
283
        return [
284
            'Apache'            => $this->getApacheDetails(),
285
            'Auto Dependencies' => $this->getPackageDetailsFromGivenComposerLockFile($cFile),
286
            'Client'            => $this->getClientBrowserDetailsForInformator(),
287
            'InfoCompareFile'   => $this->getFileDetails(__FILE__),
288
            'MySQL'             => $this->getMySQLinfo(),
289
            'PHP'               => $this->getPhpDetails(),
290
            'Server'            => $this->getServerDetails(),
291
            'Tomcat'            => $this->getTomcatDetails()['Tomcat'],
292
        ];
293
    }
294
}
295