-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFcCheckChecksum.php
222 lines (199 loc) · 6.26 KB
/
FcCheckChecksum.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace Fatchip\PayOne;
use OxidEsales\Eshop\Core\Registry;
class FcCheckChecksum
{
/**
* @var string
*/
protected string $_sModuleId;
/**
* @var string
*/
protected string $_sModuleName;
/**
* @var string
*/
protected string $_sModuleVersion;
/**
* @var bool
*/
protected bool $_blGotModuleInfo;
/**
* @var string
*/
protected string $_sShopSystem;
/**
* @var string
*/
protected $_sVersionCheckUrl = 'http://version.fatchip.de/fcVerifyChecksum.php';
/**
* @param bool $blOutput
* @return bool|string|void
* @throws \JsonException
*/
public function checkChecksumXml(bool $blOutput = false)
{
if (ini_get('allow_url_fopen') == 0) {
die("Cant verify checksums, allow_url_fopen is not activated on customer-server!");
} elseif (!function_exists('curl_init')) {
die("Cant verify checksums, curl is not activated on customer-server!");
}
$aFiles = $this->_getFilesToCheck();
$aChecksums = $this->_checkFiles($aFiles);
$sResult = $this->_getCheckResults($aChecksums);
if ($blOutput === true) {
if ($sResult == 'correct') {
echo $sResult;
} else {
$aErrors = json_decode(stripslashes($sResult));
if (is_null($aErrors)) {
$aErrors = json_decode($sResult);
}
if (is_array($aErrors)) {
foreach ($aErrors as $aError) {
echo $aError . '<br>';
}
}
}
}
return $sResult;
}
/**
* @return array|mixed
* @throws \JsonException
*/
protected function _getFilesToCheck(): mixed
{
$aFiles = [];
if (file_exists($this->_getBasePath() . 'metadata.php')) {
$this->_handleMetadata($this->_getBasePath() . 'metadata.php');
}
if (file_exists($this->_getBasePath() . 'composer.json')) {
$this->_handleComposerJson($this->_getBasePath() . 'composer.json');
}
if ($this->_blGotModuleInfo === true) {
$sRequestUrl = $this->_sVersionCheckUrl . '?module=' . $this->_sModuleId . '&version=' . $this->_sModuleVersion;
$sResponse = file_get_contents($sRequestUrl);
if ($sResponse) {
$aFiles = json_decode($sResponse, null, 512, JSON_THROW_ON_ERROR);
}
}
return $aFiles;
}
/**
* @return string
*/
protected function _getBasePath(): string
{
return dirname(__FILE__) . '/';
}
/**
* @param string $sFilePath
* @return void
*/
protected function _handleMetadata(string $sFilePath): void
{
include $sFilePath;
if (isset($aModule)) {
if (isset($aModule['id'])) {
$this->_sModuleId = $aModule['id'];
}
if (isset($aModule['title'])) {
$this->_sModuleName = $aModule['title'];
}
if (isset($aModule['version'])) {
$this->_sModuleVersion = $aModule['version'];
}
$this->_sShopSystem = 'oxid';
$this->_blGotModuleInfo = true;
}
}
/**
* @param string $sFilePath
* @return void
* @throws \JsonException
*/
protected function _handleComposerJson(string $sFilePath): void
{
$sFile = file_get_contents($sFilePath);
if (!empty($sFile)) {
$aFile = json_decode($sFile, true, 512, JSON_THROW_ON_ERROR);
// decide which shopsystem
$blIsOxid = (isset($aFile['type']) && $aFile['type'] == 'oxideshop-module');
if ($blIsOxid) {
$this->_sShopSystem = 'oxid';
} else {
$this->_sShopSystem = 'magento2';
if (isset($aFile['name'])) {
$this->_sModuleId = preg_replace('#[^A-Za-z0-9]#', '_', (string)$aFile['name']);
$this->_sModuleName = $aFile['name'];
}
if (isset($aFile['version'])) {
$this->_sModuleVersion = $aFile['version'];
}
}
$this->_blGotModuleInfo = true;
}
}
/**
* @param array $aFiles
* @return string[]|false[]
*/
protected function _checkFiles(array $aFiles): array
{
$aChecksums = [];
foreach ($aFiles as $aFile) {
$sFullFilePath = $this->_getShopBasePath() . $aFile;
if (file_exists($sFullFilePath)) {
$aChecksums[md5((string)$aFile)] = md5_file($sFullFilePath);
}
}
return $aChecksums;
}
/**
* @return string
*/
protected function _getShopBasePath(): string
{
if ($this->_sShopSystem == 'oxid') {
return $this->_getBasePath() . '/../../../';
} elseif ($this->_sShopSystem == 'magento2') {
return $this->_getBasePath() . '../../../../';
} else {
return $this->_getBasePath();
}
}
/**
* @param array $aChecksums
* @return bool|string
* @throws \JsonException
*/
protected function _getCheckResults(array $aChecksums): bool|string
{
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $this->_sVersionCheckUrl);
curl_setopt($curlHandle, CURLOPT_HEADER, false);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_POST, true);
curl_setopt(
$curlHandle, CURLOPT_POSTFIELDS, [
'checkdata' => json_encode($aChecksums, JSON_THROW_ON_ERROR), // you'll have to change the name, here, I suppose
'module' => $this->_sModuleId,
'version' => $this->_sModuleVersion,
]
);
$sResult = curl_exec($curlHandle);
curl_close($curlHandle);
return $sResult;
}
}
if (!isset($blOutput) || $blOutput) {
try {
$oScript = new FcCheckChecksum();
$oScript->checkChecksumXml(true);
} catch (\JsonException $oEx) {
$oLogger = Registry::getLogger();
$oLogger->error($oEx->getTraceAsString());
}
}