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\info_compare; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Description of compare |
33
|
|
|
* |
34
|
|
|
* @author Transformer- |
35
|
|
|
*/ |
36
|
|
|
class Compare |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use \danielgp\common_lib\CommonCode, |
40
|
|
|
\danielgp\info_compare\ConfigurationCompare, |
41
|
|
|
\danielgp\info_compare\OutputFormBuilder; |
42
|
|
|
|
43
|
|
|
private $informatorInternalArray = []; |
44
|
|
|
private $localConfiguration; |
45
|
|
|
private $serverConfiguration; |
46
|
|
|
private $config; |
47
|
|
|
|
48
|
|
|
public function __construct() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this->getConfiguration(); |
51
|
|
|
$this->applicationFlags = [ |
|
|
|
|
52
|
|
|
'available_languages' => [ |
53
|
|
|
'en_US' => 'EN', |
54
|
|
|
'ro_RO' => 'RO', |
55
|
|
|
], |
56
|
|
|
'default_language' => 'ro_RO', |
57
|
|
|
'name' => 'Info-Compare' |
58
|
|
|
]; |
59
|
|
|
echo $this->setHeaderHtml(); |
60
|
|
|
$knownLabels = $this->prepareForOutputForm(); |
61
|
|
|
echo $this->setFormOptions($knownLabels); |
62
|
|
|
if (isset($_GET['Label'])) { |
63
|
|
|
$this->processInfos(); |
64
|
|
|
echo $this->setFormCurlInfos(); |
65
|
|
|
echo $this->setFormInfos(); |
66
|
|
|
} |
67
|
|
|
echo $this->setFooterHtml(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function displayTableFromMultiLevelArray($firstArray, $secondArray) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if ((!is_array($firstArray)) || (!is_array($secondArray))) { |
73
|
|
|
return ''; |
74
|
|
|
} |
75
|
|
|
$firstRow = $this->mergeArraysIntoFirstSecond($firstArray, $secondArray, ['first', 'second']); |
76
|
|
|
$secondRow = $this->mergeArraysIntoFirstSecond($secondArray, $firstArray, ['second', 'first']); |
77
|
|
|
$row = array_merge($firstRow, $secondRow); |
78
|
|
|
ksort($row); |
79
|
|
|
$urlArguments = '?Label=' . $_GET['Label']; |
80
|
|
|
$sString[] = '<table style="width:100%">' |
|
|
|
|
81
|
|
|
. '<thead><tr>' |
82
|
|
|
. '<th>Identifier</th>' |
83
|
|
|
. '<th><a href="' . $this->config['Servers'][$_GET['localConfig']]['url'] |
84
|
|
|
. $urlArguments . '" target="_blank">' |
85
|
|
|
. $this->config['Servers'][$_GET['localConfig']]['name'] . '</a></th>' |
86
|
|
|
. '<th><a href="' . $this->config['Servers'][$_GET['serverConfig']]['url'] |
87
|
|
|
. $urlArguments . '" target="_blank">' |
88
|
|
|
. $this->config['Servers'][$_GET['serverConfig']]['name'] . '</a></th>' |
89
|
|
|
. '</tr></thead>' |
90
|
|
|
. '<tbody>'; |
91
|
|
|
if ($_GET['displayOnlyDifferent'] == '1') { |
92
|
|
|
$displayOnlyDifferent = true; |
93
|
|
|
} else { |
94
|
|
|
$displayOnlyDifferent = false; |
95
|
|
|
} |
96
|
|
|
foreach ($row as $key => $value) { |
97
|
|
|
$rowString = '<tr><td style="width:20%;">' . $key . '</td><td style="width:40%;">' |
98
|
|
|
. str_replace(',', ', ', $value['first']) . '</td><td style="width:40%;">' |
99
|
|
|
. str_replace(',', ', ', $value['second']) . '</td></tr>'; |
100
|
|
|
if ($displayOnlyDifferent) { |
101
|
|
|
if ($value['first'] != $value['second']) { |
102
|
|
|
$sString[] = $rowString; |
103
|
|
|
} |
104
|
|
|
} else { |
105
|
|
|
$sString[] = $rowString; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
$sString[] = '</tbody></table>'; |
109
|
|
|
return implode('', $sString); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function getConfiguration() |
113
|
|
|
{ |
114
|
|
|
$storedConfiguration = $this->configuredDeployedInformators(); |
115
|
|
|
foreach ($storedConfiguration['informators'] as $key => $value) { |
116
|
|
|
$this->config['Servers'][] = [ |
117
|
|
|
'name' => $key, |
118
|
|
|
'url' => $value, |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
$haystack = array_keys($storedConfiguration['informators']); |
122
|
|
|
$this->config['Defaults']['Label'] = $storedConfiguration['default']['label']; |
123
|
|
|
$this->config['Defaults']['Source'] = array_search($storedConfiguration['default']['source'], $haystack); |
124
|
|
|
$this->config['Defaults']['Target'] = array_search($storedConfiguration['default']['target'], $haystack); |
125
|
|
|
$this->config['Defaults']['ResultsType'] = $storedConfiguration['default']['typeOfResults']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function mergeArraysIntoFirstSecond($firstArray, $secondArray, $pSequence = ['first', 'second']) |
129
|
|
|
{ |
130
|
|
|
$row = []; |
131
|
|
|
foreach ($firstArray as $key => $value) { |
132
|
|
|
if (is_array($value)) { |
133
|
|
|
foreach ($value as $key2 => $value2) { |
134
|
|
|
if (is_array($value2)) { |
135
|
|
|
foreach ($value2 as $key3 => $value3) { |
136
|
|
|
if (is_array($value3)) { |
137
|
|
|
foreach ($value3 as $key4 => $value4) { |
138
|
|
|
$keyCrt = $key . '_' . $key2 . '__' . $key3 . '__' . $key4; |
139
|
|
|
$row[$keyCrt][$pSequence[0]] = $value4; |
140
|
|
|
if (isset($secondArray[$key][$key2][$key3][$key4])) { |
141
|
|
|
$row[$keyCrt][$pSequence[1]] = $secondArray[$key][$key2][$key3][$key4]; |
142
|
|
|
} else { |
143
|
|
|
$row[$keyCrt][$pSequence[1]] = ''; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
$keyCrt = $key . '_' . $key2 . '__' . $key3; |
148
|
|
|
$row[$keyCrt][$pSequence[0]] = $value3; |
149
|
|
View Code Duplication |
if (isset($secondArray[$key][$key2][$key3])) { |
|
|
|
|
150
|
|
|
$row[$keyCrt][$pSequence[1]] = $secondArray[$key][$key2][$key3]; |
151
|
|
|
} else { |
152
|
|
|
$row[$keyCrt][$pSequence[1]] = ''; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} else { |
157
|
|
|
$keyCrt = $key . '_' . $key2; |
158
|
|
|
$row[$keyCrt][$pSequence[0]] = $value2; |
159
|
|
|
if (isset($secondArray[$key][$key2])) { |
160
|
|
|
$row[$keyCrt][$pSequence[1]] = $secondArray[$key][$key2]; |
161
|
|
|
} else { |
162
|
|
|
$row[$keyCrt][$pSequence[1]] = ''; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
View Code Duplication |
} else { |
|
|
|
|
167
|
|
|
$row[$key][$pSequence[0]] = $value; |
168
|
|
|
if (isset($secondArray[$key])) { |
169
|
|
|
$row[$key][$pSequence[1]] = $secondArray[$key]; |
170
|
|
|
} else { |
171
|
|
|
$row[$key][$pSequence[1]] = ''; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
return $row; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function prepareForOutputForm() |
179
|
|
|
{ |
180
|
|
|
$this->setDefaultOptions(); |
181
|
|
|
$rqst = new \Symfony\Component\HttpFoundation\Request; |
182
|
|
|
$this->informatorInternalArray['sGlobals'] = $rqst->createFromGlobals(); |
183
|
|
|
$urlToGetLbl = $this->config['Servers' |
184
|
|
|
. ''][$this->config['Defaults']['Source']]['url'] |
185
|
|
|
. '?Label=---' . urlencode(' List of known labels'); |
186
|
|
|
$knownLabels = $this->getContentFromUrlThroughCurlAsArrayIfJson($urlToGetLbl); |
187
|
|
|
return $knownLabels['response']; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private function processInfos() |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
if (isset($_GET['localConfig']) && isset($_GET['serverConfig'])) { |
193
|
|
|
$urlArguments = '?Label=' . urlencode($_GET['Label']); |
194
|
|
|
$source = $this->config['Servers'][$_GET['localConfig']]['url'] . $urlArguments; |
195
|
|
|
$this->localConfiguration = $this->getContentFromUrlThroughCurlAsArrayIfJson($source); |
196
|
|
|
$destination = $this->config['Servers'][$_GET['serverConfig']]['url'] . $urlArguments; |
197
|
|
|
$this->serverConfiguration = $this->getContentFromUrlThroughCurlAsArrayIfJson($destination); |
198
|
|
|
} else { |
199
|
|
|
$this->localConfiguration = ['response' => '', 'info' => '']; |
200
|
|
|
$this->serverConfiguration = ['response' => '', 'info' => '']; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
private function setDefaultOptions() |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
if (!isset($_GET['displayOnlyDifferent'])) { |
207
|
|
|
$_GET['displayOnlyDifferent'] = $this->config['Defaults']['ResultsType']; |
208
|
|
|
} |
209
|
|
|
if (!isset($_GET['localConfig'])) { |
210
|
|
|
$_GET['localConfig'] = $this->config['Defaults']['Source']; |
211
|
|
|
} |
212
|
|
|
if (!isset($_GET['serverConfig'])) { |
213
|
|
|
$_GET['serverConfig'] = $this->config['Defaults']['Target']; |
214
|
|
|
} |
215
|
|
|
if (!isset($_GET['Label'])) { |
216
|
|
|
$_GET['Label'] = $this->config['Defaults']['Label']; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private function setFooterHtml() |
221
|
|
|
{ |
222
|
|
|
$sReturn = []; |
223
|
|
|
$sReturn[] = '</div><!-- from main Tabber -->'; |
224
|
|
|
$sReturn[] = '<div class="resetOnly author">© 2015 Daniel Popiniuc</div>'; |
225
|
|
|
$sReturn[] = '<hr/>'; |
226
|
|
|
$sReturn[] = '<div class="disclaimer">' |
227
|
|
|
. 'The developer cannot be liable of any data input or results, ' |
228
|
|
|
. 'included but not limited to any implication of these ' |
229
|
|
|
. '(anywhere and whomever there might be these)!' |
230
|
|
|
. '</div>'; |
231
|
|
|
return $this->setFooterCommon(implode('', $sReturn)); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
private function setFormCurlInfos() |
235
|
|
|
{ |
236
|
|
|
$source = $this->localConfiguration['info']; |
237
|
|
|
$destination = $this->serverConfiguration['info']; |
238
|
|
|
return '<div class="tabbertab" id="tabCurl" title="CURL infos">' |
239
|
|
|
. $this->displayTableFromMultiLevelArray($source, $destination) |
240
|
|
|
. '</div><!--from tabCurl-->'; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
private function setFormInfos() |
|
|
|
|
244
|
|
|
{ |
245
|
|
|
$source = $this->localConfiguration['response']; |
246
|
|
|
$destination = $this->serverConfiguration['response']; |
247
|
|
|
return '<div class="tabbertab' |
248
|
|
|
. (isset($_GET['Label']) ? ' tabbertabdefault' : '') |
249
|
|
|
. '" id="tabConfigs" title="Informations">' |
250
|
|
|
. $this->displayTableFromMultiLevelArray($source, $destination) |
251
|
|
|
. '</div><!--from tabConfigs-->'; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
private function setHeaderHtml() |
255
|
|
|
{ |
256
|
|
|
return $this->setHeaderCommon([ |
257
|
|
|
'lang' => 'en-US', |
258
|
|
|
'title' => $this->applicationFlags['name'], |
259
|
|
|
'css' => 'css/main.css', |
260
|
|
|
'javascript' => 'js/tabber.min.js', |
261
|
|
|
]) |
262
|
|
|
. $this->setJavascriptContent('document.write(\'<style type="text/css">.tabber{display:none;}</style>\');') |
263
|
|
|
. '<h1>' . $this->applicationFlags['name'] . '</h1>' |
264
|
|
|
. '<div class="tabber" id="tab">'; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: