1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* PHP version 5 |
18
|
|
|
* |
19
|
|
|
* @category Payone |
20
|
|
|
* @package Payone_Magento2_Plugin |
21
|
|
|
* @author FATCHIP GmbH <[email protected]> |
22
|
|
|
* @copyright 2003 - 2016 Payone GmbH |
23
|
|
|
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License |
24
|
|
|
* @link http://www.payone.de |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Payone\Core\Model; |
28
|
|
|
|
29
|
|
|
use Magento\Framework\Exception\LocalizedException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Checksum check |
33
|
|
|
*/ |
34
|
|
|
class ChecksumCheck |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* module id |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $sModuleId = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* module name |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $sModuleName = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* module version |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $sModuleVersion = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returnes if composer.json was found |
59
|
|
|
* |
60
|
|
|
* @var bool |
61
|
|
|
*/ |
62
|
|
|
protected $blGotModuleInfo = null; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* URL to Fatchip checksum check |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $sVersionCheckUrl = 'http://version.fatchip.de/fcVerifyChecksum.php'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get module base path |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
protected function getBasePath() |
77
|
|
|
{ |
78
|
|
|
return dirname(__FILE__).'/../'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Read module info from composer.json |
83
|
|
|
* |
84
|
|
|
* @param string $sFilePath |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function handleComposerJson($sFilePath) |
88
|
|
|
{ |
89
|
|
|
$sFile = file_get_contents($sFilePath); |
90
|
|
|
if (!empty($sFile)) {// was file readable? |
91
|
|
|
$aFile = json_decode($sFile, true); |
92
|
|
|
if (isset($aFile['name'])) {// is name property set in composer.json? |
93
|
|
|
$this->sModuleId = preg_replace('#[^A-Za-z0-9]#', '_', $aFile['name']); |
94
|
|
|
$this->sModuleName = $aFile['name']; |
95
|
|
|
} |
96
|
|
|
if (isset($aFile['version'])) {// is version property set in composer.json? |
97
|
|
|
$this->sModuleVersion = $aFile['version']; |
98
|
|
|
} |
99
|
|
|
$this->blGotModuleInfo = true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Read module version from module.xml |
105
|
|
|
* |
106
|
|
|
* @param string $sFilePath |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
protected function handleModuleXml($sFilePath) { |
110
|
|
|
$oXml = simplexml_load_file($sFilePath); |
111
|
|
|
if ($oXml && $oXml->module) { |
112
|
|
|
$sVersion = $oXml->module->attributes()->setup_version; |
113
|
|
|
if ($sVersion) { |
114
|
|
|
$this->sModuleVersion = $sVersion; |
115
|
|
|
} |
116
|
|
|
$this->blGotModuleInfo = true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Request files existing in the module from Fatchip checksum server |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function getFilesToCheck() |
126
|
|
|
{ |
127
|
|
|
$aFiles = []; |
128
|
|
|
if (file_exists($this->getBasePath().'composer.json')) {// does composer.json exist here? |
129
|
|
|
$this->handleComposerJson($this->getBasePath().'composer.json'); // Read module information from the composer.json |
130
|
|
|
} |
131
|
|
|
if (file_exists($this->getBasePath()."/etc/module.xml")) {// does module.xml exist here? |
132
|
|
|
$this->handleModuleXml($this->getBasePath()."/etc/module.xml"); // Read module information from the module.xml |
133
|
|
|
} |
134
|
|
|
if ($this->blGotModuleInfo === true) { // was composer.json readable? |
135
|
|
|
$sRequestUrl = $this->sVersionCheckUrl.'?module='.$this->sModuleId.'&version='.$this->sModuleVersion; |
136
|
|
|
$sResponse = file_get_contents($sRequestUrl); // request info from fatchip checksum server |
137
|
|
|
if (!empty($sResponse)) {// Did the server answer? |
138
|
|
|
$aFiles = json_decode($sResponse); // Decode the json encoded answer from the server |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
return $aFiles; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Collect the md5 checksums for all the files given as content |
146
|
|
|
* of this module |
147
|
|
|
* |
148
|
|
|
* @param array $aFiles |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
protected function checkFiles($aFiles) |
152
|
|
|
{ |
153
|
|
|
$aChecksums = []; |
154
|
|
|
foreach ($aFiles as $sFilePath) { |
155
|
|
|
$sFullFilePath = $this->getBasePath().$sFilePath; |
156
|
|
|
if (file_exists($sFullFilePath)) { |
157
|
|
|
$aChecksums[md5($sFilePath)] = md5_file($sFullFilePath); // Create md5 checksum of the file |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
return $aChecksums; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Send collected checksums to Fatchip checksum server and receive |
165
|
|
|
* if all files are unchanged |
166
|
|
|
* |
167
|
|
|
* @param array $aChecksums |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
protected function getCheckResults($aChecksums) |
171
|
|
|
{ |
172
|
|
|
$oCurl = curl_init(); |
173
|
|
|
curl_setopt($oCurl, CURLOPT_URL, $this->sVersionCheckUrl); |
174
|
|
|
curl_setopt($oCurl, CURLOPT_HEADER, false); |
175
|
|
|
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true); |
176
|
|
|
curl_setopt($oCurl, CURLOPT_POST, true); |
177
|
|
|
curl_setopt($oCurl, CURLOPT_POSTFIELDS, [ |
178
|
|
|
'checkdata' => json_encode($aChecksums), // checksums of all module files |
179
|
|
|
'module' => $this->sModuleId, // module identification |
180
|
|
|
'version' => $this->sModuleVersion, // current module version |
181
|
|
|
]); |
182
|
|
|
$sResult = curl_exec($oCurl); |
183
|
|
|
curl_close($oCurl); |
184
|
|
|
return $sResult; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Main method executing checksum check |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
* @throws LocalizedException |
192
|
|
|
*/ |
193
|
|
|
public function checkChecksumXml() |
194
|
|
|
{ |
195
|
|
|
if (ini_get('allow_url_fopen') == 0) {// Is file_get_contents for urls active on this server? |
196
|
|
|
throw new LocalizedException(__("Cant verify checksums, allow_url_fopen is not activated on customer-server!")); |
197
|
|
|
} elseif (!function_exists('curl_init')) {// is curl usable on this server? |
198
|
|
|
throw new LocalizedException(__("Cant verify checksums, curl is not activated on customer-server!")); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$aFiles = $this->getFilesToCheck(); // Requests all files that need to be checked from the Fatchip Checksum Server |
202
|
|
|
$aChecksums = $this->checkFiles($aFiles); // Collect checksums of all files that need to be checked |
203
|
|
|
return $this->getCheckResults($aChecksums); // Send the checksums to the Fatchip Checksum Server and have them checked |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Execute the checksum check and return false for a correct check |
208
|
|
|
* or an array with all errors |
209
|
|
|
* |
210
|
|
|
* @return array|bool |
211
|
|
|
*/ |
212
|
|
|
public function getChecksumErrors() |
213
|
|
|
{ |
214
|
|
|
$sResult = $this->checkChecksumXml(); |
215
|
|
|
if ($sResult == 'correct') {// were all checksums correct? |
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$aErrors = json_decode(stripslashes($sResult)); |
220
|
|
|
if ($aErrors === null) {// fallback for when stripslashes is not needed |
221
|
|
|
$aErrors = json_decode($sResult); |
222
|
|
|
} |
223
|
|
|
if (is_array($aErrors)) { |
224
|
|
|
return $aErrors; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|