Completed
Push — master ( afd3b6...42211a )
by Daniel
02:11
created

Informator::setOutputWithLabels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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