forked from PAYONE-GmbH/oxid-6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcCheckChecksum.php
157 lines (141 loc) · 5.06 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
<?php
class fcCheckChecksum
{
protected $_sModuleId = null;
protected $_sModuleName = null;
protected $_sModuleVersion = null;
protected $_blGotModuleInfo = null;
protected $_sShopSystem = null;
protected $_sVersionCheckUrl = 'http://version.fatchip.de/fcVerifyChecksum.php';
protected function _getBasePath()
{
return dirname(__FILE__).'/';
}
protected function _getShopBasePath()
{
if($this->_sShopSystem == 'oxid') {
return $this->_getBasePath().'/../../../';
} elseif($this->_sShopSystem == 'magento2') {
return $this->_getBasePath().'../../../../';
} else {
return $this->_getBasePath();
}
}
protected function _handleMetadata($sFilePath)
{
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;
}
}
protected function _handleComposerJson($sFilePath)
{
$sFile = file_get_contents($sFilePath);
if(!empty($sFile)) {
$aFile = json_decode($sFile, true);
// 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]#', '_', $aFile['name']);
$this->_sModuleName = $aFile['name'];
}
if(isset($aFile['version'])) {
$this->_sModuleVersion = $aFile['version'];
}
}
$this->_blGotModuleInfo = true;
}
}
protected function _getFilesToCheck()
{
$aFiles = array();
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);
}
}
return $aFiles;
}
protected function _checkFiles($aFiles)
{
$aChecksums = array();
foreach ($aFiles as $sFilePath) {
$sFullFilePath = $this->_getShopBasePath().$sFilePath;
if(file_exists($sFullFilePath)) {
$aChecksums[md5($sFilePath)] = md5_file($sFullFilePath);
}
}
return $aChecksums;
}
protected function _getCheckResults($aChecksums)
{
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $this->_sVersionCheckUrl);
curl_setopt($oCurl, CURLOPT_HEADER, false);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt(
$oCurl, CURLOPT_POSTFIELDS, array(
'checkdata' => json_encode($aChecksums), // you'll have to change the name, here, I suppose
'module' => $this->_sModuleId,
'version' => $this->_sModuleVersion,
)
);
$sResult = curl_exec($oCurl);
curl_close($oCurl);
return $sResult;
}
public function checkChecksumXml($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 $sError) {
echo $sError.'<br>';
}
}
}
}
return $sResult;
}
}
if(!isset($blOutput) || $blOutput == true) {
$oScript = new fcCheckChecksum();
$oScript->checkChecksumXml(true);
}