PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/httpdocs/catalog/controller/payment/bankart_slovenia.SecureResource.php

https://gitlab.com/jo0054/bankart-opencart
PHP | 433 lines | 340 code | 55 blank | 38 comment | 55 complexity | 845c1e0e582bd030605de6b0d19a7cac MD5 | raw file
  1. <?php
  2. /******************************************************************************
  3. *
  4. * File: SecureResource.php
  5. * Description: Razred SecureResource je namenjen dekripciji podatkov iz
  6. * resource datoteke.
  7. * Author: Igor Rozman
  8. * Created: 26.11.2008
  9. * Modified: 01.12.2008
  10. * Language: PHP (v 5.2.6)
  11. * Extensions: ZIP
  12. * Package: paymentpipephp (testna koda ze testiranje spletnih transakcij)
  13. *
  14. * (c) Copyright 2008, Bankart d.o.o., all rights reserved.
  15. *
  16. ******************************************************************************/
  17. class SecureResource
  18. {
  19. var $strResourcePath;
  20. var $strAlias;
  21. var $termID;
  22. var $password;
  23. var $passwordHash;
  24. var $port;
  25. var $context;
  26. var $webAddress;
  27. var $error;
  28. var $bDebugOn;
  29. var $debugMsg;
  30. var $bSecureResourceDecoded;
  31. // Kljuc za odkriptiranje podatkov za dostop do placilnega serverja.
  32. var $key = array (1416130419, 1696626536, 1864396914, 1868981619, 1931506799, 543580534, 1869967904,
  33. 1718773093, 1685024032, 1634624544, 2036692000, 1684369522, 1701013857, 1952784481, 1734964321,
  34. 1953066862, 543257189, 544040302, 544696431, 544694638, 1948283489, 1768824951, 1769236591,
  35. 1970544756, 1752526436, 1701978209, 1852055660, 1768384628, 1852403303);
  36. function SecureResource()
  37. {
  38. $this->strResourcePath = "";
  39. $this->strAlias = "";
  40. $this->termID = "";
  41. $this->password = "";
  42. $this->passwordHash = "";
  43. $this->port = "";
  44. $this->context = "";
  45. $this->webAddress = "";
  46. $this->error = "";
  47. $this->bDebugOn = true;
  48. }
  49. // Metoda prebere podatke iz resource datoteke.
  50. function getSecureSettings()
  51. {
  52. if ($this->strResourcePath == "")
  53. {
  54. $this->error = "No resource path specified.";
  55. return false;
  56. }
  57. $strData = "";
  58. if(!$this->createReadableZip())
  59. return false;
  60. $strData = $this->readZip($this->getAlias() . ".xml");
  61. $this->destroyUnSecureResource();
  62. if($strData == "")
  63. return false;
  64. return $this->parseSettings($strData);
  65. }
  66. // Zbrise zacasno datoteko, ki je nastala pri dekripciji resource datoteke.
  67. function destroyUnSecureResource()
  68. {
  69. unlink($this->getResourcePath() . "resource.cgz");
  70. $this->bSecureResourceDecoded = false;
  71. if ($this->bDebugOn)
  72. $this->addDebugMessage("Decoded Resource Destroyed.");
  73. }
  74. // Iz resource datoteke zgradi zacasno zip datoteko.
  75. function createReadableZip()
  76. {
  77. if($this->bSecureResourceDecoded)
  78. return true;
  79. if ($this->bDebugOn)
  80. $this->addDebugMessage("Locating Secure Resource.");
  81. // 1. binarno preberemo podatke
  82. $fileInName = $this->getResourcePath() . "resource.cgn";
  83. $zip = fopen($fileInName, 'rb');
  84. if (!is_resource($zip))
  85. {
  86. $this->error = "Unable to open resource file (" . $fileInName . ")!";
  87. return false;
  88. }
  89. // - byte po byte preberemo vsebino datoteke v niz $stringData
  90. $bin = fread($zip, 1); // preberemo 1 stevilo
  91. $i = 0;
  92. $stringData = '';
  93. while (!feof($zip))
  94. {
  95. $stringData = $stringData.$bin;
  96. $bin = fread($zip, 1); // preberemo 1 stevilo
  97. }
  98. $numberOfBytes = strlen($stringData);
  99. // - odpakiramo znake v cela stevila
  100. $data = $this->odpakiraj($stringData);
  101. // 2. Naredimo XOR nad podatki
  102. $xorData = $this->simpleXOR($data);
  103. // 3. binarno zapisemo podatke, byte po byte
  104. $fileOutName = $this->getResourcePath() . "resource.cgz";
  105. $rzip = fopen($fileOutName, 'wb');
  106. if (!is_resource($rzip))
  107. {
  108. $this->error = "Unable to open temporaray resource file (" . $fileOutName . ")! Check permissions for writting files.";
  109. return false;
  110. }
  111. // - zapakiramo cela stevila v znake
  112. $stringData = $this->zapakiraj($xorData);
  113. for ($i = 0; $i < $numberOfBytes; $i++)
  114. {
  115. if (fwrite($rzip, $stringData[$i], 1) === FALSE)
  116. echo "Cannot write to file ($filename)";
  117. }
  118. fclose($rzip);
  119. $this->bSecureResourceDecoded = true;
  120. return true;
  121. }
  122. function odpakiraj($stringData)
  123. {
  124. // Dodamo prazne znake, da stevilo bayteov doseze veckratnik stevila 4, seveda ce to ze ni.
  125. while (strlen($stringData) % 4 != 0)
  126. $stringData .= ' ';
  127. // Po 4 znake skupaj odpakiramo v stevilo.
  128. for ($i = 0, $j = 0; $i < strlen($stringData); $i = $i + 4, $j++)
  129. {
  130. $y = unpack("Nx", substr($stringData, $i, 4)); // binarne podatke razpakiramo v tabelo; razporejeni so z "big endian order"
  131. $data[$j] = $y["x"];
  132. }
  133. return $data;
  134. }
  135. function zapakiraj($data)
  136. {
  137. $stringData = '';
  138. // Po 4 znake skupaj zapakiramo v stevilo.
  139. for ($i = 0; $i < count($data); $i++)
  140. {
  141. $bin = pack("N", $data[$i]); // iz tabele zapakiramo podatke nazaj v binarno obliko.
  142. $stringData = $stringData.$bin;
  143. }
  144. return $stringData;
  145. }
  146. // Iz zacasne zip datoteke "izluscimo" vsebino datoteke v obliki enega niza.
  147. function readZip($entryFileName)
  148. {
  149. $strData = "";
  150. $data;
  151. if ($this->getResourcePath() == null || $this->getResourcePath() == "")
  152. {
  153. $this->error = "Error Accessing Secure Resource. Resource Path not set.";
  154. return null;
  155. }
  156. if ($entryFileName == null || $entryFileName == "")
  157. {
  158. $this->error = "Error Accessing Secure Resource. Terminal Alias not set.";
  159. return null;
  160. }
  161. if ($this->bDebugOn)
  162. $this->addDebugMessage("Accessing Decoded Secure Resource.");
  163. $zipFile = zip_open($this->getResourcePath() . "resource.cgz");
  164. if (is_resource($zipFile))
  165. {
  166. $zip_entry_exist = false;
  167. while ($zipEntry = zip_read($zipFile))
  168. {
  169. // V zip datoteki iscemo XML datoteko z imenom trgovcevega terminala.
  170. if (zip_entry_name($zipEntry) === $entryFileName)
  171. {
  172. $zip_entry_exist = true;
  173. if (zip_entry_open($zipFile, $zipEntry))
  174. {
  175. if ($this->bDebugOn)
  176. $this->addDebugMessage("Resource Entry Retrieved.");
  177. $readStream = zip_entry_read($zipEntry);
  178. // binarno preberemo podatke kot stevila
  179. $data = unpack("N*", $readStream);
  180. for ($i=1; $i<count($data)+1; $i++)
  181. {
  182. $data1[$i-1] = $data[$i];
  183. }
  184. // naredimo XOR nad podatki
  185. $xorData = $this->simpleXOR($data1);
  186. // celostevilske podatke zapisemo v binarni niz
  187. $bin = null;
  188. for ($i=0; $i<count($xorData); $i++)
  189. {
  190. $bin .= pack("N", $xorData[$i]);
  191. }
  192. // podatke iz binarnega niza pretvorimo v niz znakov
  193. $decoded = unpack("C*", $bin);
  194. $xmlString = "";
  195. for ($i=1; $i<count($decoded)+1; $i++)
  196. {
  197. $xmlString .= chr($decoded[$i]);
  198. }
  199. //echo $xmlString . "<br>";
  200. // vrnem XML string
  201. $strData = $xmlString;
  202. if ($this->bDebugOn)
  203. $this->addDebugMessage("Resource Entry Parsed.");
  204. zip_entry_close($zipEntry);
  205. }
  206. }
  207. }
  208. zip_close($zipFile);
  209. }
  210. else
  211. {
  212. $this->error = "Failed to read ZIP " . $this->getResourcePath() . "resource.cgz" . "\n";
  213. return null;
  214. }
  215. if ($zip_entry_exist == false)
  216. {
  217. $this->error = "The ZIP Entry " . $this->getAlias() . ".xml does not exist.";
  218. return null;
  219. }
  220. return $strData;
  221. }
  222. // Niz pridobljen iz zacasne zip datoteke razparsamo.
  223. function parseSettings($settings)
  224. {
  225. if ($this->bDebugOn)
  226. $this->addDebugMessage("Parsing Settings.");
  227. $begin = strpos($settings, "<id>") + strlen("<id>");
  228. $end = strpos($settings, "</id>");
  229. if ($begin === false || $end === false)
  230. {
  231. $this->error = "Error parsing internal settings file. Error:" + $e.getString();
  232. return false;
  233. }
  234. $this->setTermID(substr($settings, $begin, $end-$begin));
  235. $begin = strpos($settings, "<password>") + strlen("<password>");
  236. $end = strpos($settings, "</password>");
  237. if ($begin === false || $end === false)
  238. {
  239. $this->error = "Error parsing internal settings file. Error:" + $e.getString();
  240. return false;
  241. }
  242. $this->setPassword(substr($settings, $begin, $end-$begin));
  243. $begin = strpos($settings, "<passwordhash>") + strlen("<passwordhash>");
  244. $end = strpos($settings, "</passwordhash>");
  245. if ($begin === false || $end === false)
  246. {
  247. $this->error = "Error parsing internal settings file. Error:" + $e.getString();
  248. return false;
  249. }
  250. $this->setPasswordHash(substr($settings, $begin, $end-$begin));
  251. $begin = strpos($settings, "<webaddress>") + strlen("<webaddress>");
  252. $end = strpos($settings, "</webaddress>");
  253. if ($begin === false || $end === false)
  254. {
  255. $this->error = "Error parsing internal settings file. Error:" + $e.getString();
  256. return false;
  257. }
  258. $this->setWebAddress(substr($settings, $begin, $end-$begin));
  259. $begin = strpos($settings, "<port>") + strlen("<port>");
  260. $end = strpos($settings, "</port>");
  261. if ($begin === false || $end === false)
  262. {
  263. $this->error = "Error parsing internal settings file. Error:" + $e.getString();
  264. return false;
  265. }
  266. $this->setPort(substr($settings, $begin, $end-$begin));
  267. $begin = strpos($settings, "<context>") + strlen("<context>");
  268. $end = strpos($settings, "</context>");
  269. if ($begin === false || $end === false)
  270. {
  271. $this->error = "Error parsing internal settings file. Error:" + $e.getString();
  272. return false;
  273. }
  274. $this->setContext(substr($settings, $begin, $end-$begin));
  275. return true;
  276. }
  277. // Nad binarnima podatkoma izvede xor funkcijo.
  278. function simpleXOR($byteInput)
  279. {
  280. if ($this->bDebugOn)
  281. $this->addDebugMessage("Decoding Buffer.");
  282. $k = 0;
  283. for ($m = 0; $m < count($byteInput); $m++)
  284. {
  285. if ($k >= count($this->key))
  286. $k = 0;
  287. $result[$m] = $byteInput[$m] ^ $this->key[$k];
  288. $k++;
  289. }
  290. return $result;
  291. }
  292. function isDebugOn()
  293. {
  294. return $this->bDebugOn;
  295. }
  296. function setDebugOn($val)
  297. {
  298. $this->bDebugOn = $val;
  299. }
  300. function addDebugMessage($val)
  301. {
  302. if($this->bDebugOn)
  303. $this->debugMsg .= $val;
  304. }
  305. function getDebugMsg()
  306. {
  307. return $this->debugMsg.toString();
  308. }
  309. function getResourcePath()
  310. {
  311. return $this->strResourcePath;
  312. }
  313. function setResourcePath($val)
  314. {
  315. $this->strResourcePath = $val;
  316. }
  317. function getAlias()
  318. {
  319. return $this->strAlias;
  320. }
  321. function setAlias($val)
  322. {
  323. $this->strAlias = $val;
  324. }
  325. function getContext()
  326. {
  327. return $this->context;
  328. }
  329. function setContext($val)
  330. {
  331. $this->context = $val;
  332. }
  333. function getPassword()
  334. {
  335. return $this->password;
  336. }
  337. function setPassword($val)
  338. {
  339. $this->password = $val;
  340. }
  341. function getPasswordHash()
  342. {
  343. return $this->passwordHash;
  344. }
  345. function setPasswordHash($val)
  346. {
  347. $this->passwordHash = $val;
  348. }
  349. function getPort()
  350. {
  351. return $this->port;
  352. }
  353. function setPort($val)
  354. {
  355. $this->port = $val;
  356. }
  357. function getTermID()
  358. {
  359. return $this->termID;
  360. }
  361. function setTermID($val)
  362. {
  363. $this->termID = $val;
  364. }
  365. function getWebAddress()
  366. {
  367. return $this->webAddress;
  368. }
  369. function setWebAddress($val)
  370. {
  371. $this->webAddress = $val;
  372. }
  373. function getError()
  374. {
  375. return $this->error;
  376. }
  377. }
  378. ?>