PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/idealcheckout/includes/library.php

https://github.com/dedavidd/piratenpartij.nl
PHP | 1288 lines | 1005 code | 234 blank | 49 comment | 122 complexity | b92aa1672db220913bc09e826c170355 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. if(file_exists(dirname(__FILE__) . '/debug.php'))
  3. {
  4. include_once(dirname(__FILE__) . '/debug.php');
  5. }
  6. if(file_exists(dirname(__FILE__) . '/update.order.status.php'))
  7. {
  8. include_once(dirname(__FILE__) . '/update.order.status.php');
  9. }
  10. // Create a random code with N digits.
  11. function idealcheckout_getRandomCode($iLength = 64)
  12. {
  13. $aCharacters = array('a', 'b', 'c', 'd', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  14. $sResult = '';
  15. for($i = 0; $i < $iLength; $i++)
  16. {
  17. $sResult .= $aCharacters[rand(0, sizeof($aCharacters) - 1)];
  18. }
  19. return $sResult;
  20. }
  21. // Find HASH salt
  22. function idealcheckout_getHashSalt($sStoreCode = false)
  23. {
  24. $aData = idealcheckout_getDatabaseSettings();
  25. return md5((is_string($sStoreCode) ? $sStoreCode : idealcheckout_getStoreCode()) . idealcheckout_serialize($aData));
  26. }
  27. // Find default store code
  28. function idealcheckout_getStoreCode()
  29. {
  30. return md5($_SERVER['SERVER_NAME']);
  31. }
  32. // Retrieve ROOT url of script
  33. function idealcheckout_getRootUrl($iParent = 0)
  34. {
  35. // Use a fixed ROOT_URL
  36. // return 'http://www.example.com/';
  37. $aWebsiteSettings = idealcheckout_getWebsiteSettings();
  38. if(!empty($aWebsiteSettings['root_url']))
  39. {
  40. if(substr($aWebsiteSettings['root_url'], -1, 1) == '/')
  41. {
  42. return $aWebsiteSettings['root_url'];
  43. }
  44. else
  45. {
  46. return $aWebsiteSettings['root_url'] . '/';
  47. }
  48. }
  49. // Detect installation directory based on current URL
  50. $sRootUrl = '';
  51. // Detect scheme
  52. if(isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'ON') === 0))
  53. {
  54. $sRootUrl .= 'https://';
  55. }
  56. else
  57. {
  58. $sRootUrl .= 'http://';
  59. }
  60. // Detect domain
  61. $sRootUrl .= $_SERVER['HTTP_HOST'];
  62. // Detect port
  63. if((strpos($_SERVER['HTTP_HOST'], ':') === false) && isset($_SERVER['SERVER_PORT']) && (strcmp($_SERVER['SERVER_PORT'], '80') !== 0))
  64. {
  65. $sRootUrl .= ':' . $_SERVER['SERVER_PORT'];
  66. }
  67. $sRootUrl .= '/';
  68. // Detect path
  69. if(isset($_SERVER['SCRIPT_NAME']))
  70. {
  71. $a = explode('/', substr($_SERVER['SCRIPT_NAME'], 1));
  72. while(sizeof($a) > ($iParent + 1))
  73. {
  74. $sRootUrl .= $a[0] . '/';
  75. array_shift($a);
  76. }
  77. }
  78. return $sRootUrl;
  79. }
  80. // Retrieve ROOT url of script
  81. function idealcheckout_getRootPath()
  82. {
  83. return dirname(dirname(__FILE__)) . '/';
  84. }
  85. // Replace characters with accents
  86. function idealcheckout_getDebugMode()
  87. {
  88. return (is_file(dirname(__FILE__) . '/debug.php') == true);
  89. }
  90. // Escape SQL values
  91. function idealcheckout_escapeSql($sString, $bEscapeLike = false)
  92. {
  93. if($bEscapeLike)
  94. {
  95. // _ : represents a single character in a LIKE value
  96. // % : represents 0 or more character in a LIKE value
  97. $sString = str_replace(array('\\', '\'', '_', '%'), array('\\\\', '\\\'', '\\_', '\\%'), $sString);
  98. }
  99. else
  100. {
  101. $sString = str_replace(array('\\', '\''), array('\\\\', '\\\''), $sString);
  102. }
  103. return $sString;
  104. }
  105. // Serialize data
  106. function idealcheckout_serialize($sString)
  107. {
  108. return serialize($sString);
  109. }
  110. // Unserialize data
  111. function idealcheckout_unserialize($sString)
  112. {
  113. // Recalculate multibyte strings
  114. $sString = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $sString);
  115. return unserialize($sString);
  116. }
  117. // Replace characters with accents
  118. function idealcheckout_escapeAccents($sString)
  119. {
  120. return str_replace(array('ŕ', 'á', 'â', 'ă', 'ä', 'ĺ', 'ć', 'ç', 'č', 'é', 'ę', 'ë', 'đ', 'ě', 'í', 'î', 'ď', 'ń', 'ň', 'ó', 'ô', 'ő', 'ö', 'ř', '§', 'ů', 'ú', 'ű', 'ü', 'ý', '˙', 'Ŕ', 'Á', 'Â', 'Ă', 'Ä', 'Ĺ', 'Ć', 'Ç', 'Č', 'É', 'Ę', 'Ë', '€', 'Đ', 'Ě', 'Í', 'Î', 'Ď', 'Ń', 'Ň', 'Ó', 'Ô', 'Ő', 'Ö', 'Ř', '§', 'Ů', 'Ú', 'Ű', 'Ü', 'Ý', 'Ÿ', chr(96), chr(132), chr(133), chr(145), chr(146), chr(147), chr(148), chr(150), chr(151)), array('a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'ed', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 's', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'EUR', 'ED', 'I', 'I', 'I', 'I', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'S', 'U', 'U', 'U', 'U', 'Y', 'Y', '\'', '"', '...', '\'', '\'', '"', '"', '-', '-'), $sString);
  121. }
  122. // Load data from an URL
  123. function idealcheckout_doHttpRequest($sUrl, $sPostData = false, $bRemoveHeaders = false, $iTimeout = 30, $bDebug = false)
  124. {
  125. if(!empty($sUrl))
  126. {
  127. if(in_array('sockets', get_loaded_extensions())) // Prefer FSOCK
  128. {
  129. return idealcheckout_doHttpRequest_fsock($sUrl, $sPostData, $bRemoveHeaders, $iTimeout, $bDebug);
  130. }
  131. elseif(in_array('curl', get_loaded_extensions()) && function_exists('curl_init'))
  132. {
  133. return idealcheckout_doHttpRequest_curl($sUrl, $sPostData, $bRemoveHeaders, $iTimeout, $bDebug);
  134. }
  135. else
  136. {
  137. idealcheckout_die('idealcheckout_doHttpRequest: Cannot detect sockets or curl.', __FILE__, __LINE__, false);
  138. }
  139. }
  140. }
  141. // doHttpRequest (Uses sockets-library)
  142. function idealcheckout_doHttpRequest_fsock($sUrl, $sPostData = false, $bRemoveHeaders = false, $iTimeout = 30, $bDebug = false)
  143. {
  144. $aUrl = parse_url($sUrl);
  145. $sRequestUrl = '';
  146. if(in_array($aUrl['scheme'], array('ssl', 'https')))
  147. {
  148. $sRequestUrl .= 'ssl://';
  149. if(empty($aUrl['port']))
  150. {
  151. $aUrl['port'] = 443;
  152. }
  153. }
  154. elseif(empty($aUrl['port']))
  155. {
  156. $aUrl['port'] = 80;
  157. }
  158. $sRequestUrl .= $aUrl['host'];
  159. $iRequestPort = intval($aUrl['port']);
  160. $sErrorNumber = 0;
  161. $sErrorMessage = '';
  162. $oSocket = fsockopen($sRequestUrl, $iRequestPort, $sErrorNumber, $sErrorMessage, $iTimeout);
  163. $sResponse = '';
  164. if($oSocket)
  165. {
  166. $sRequest = ($sPostData ? 'POST' : 'GET') . ' ' . (empty($aUrl['path']) ? '/' : $aUrl['path']) . (empty($aUrl['query']) ? '' : '?' . $aUrl['query']) . ' HTTP/1.0' . "\r\n";
  167. $sRequest .= 'Host: ' . $aUrl['host'] . "\r\n";
  168. $sRequest .= 'Accept: text/html' . "\r\n";
  169. $sRequest .= 'Accept-Charset: charset=ISO-8859-1,utf-8' . "\r\n";
  170. if(is_array($sPostData))
  171. {
  172. $sPostData = str_replace(array('%5B', '%5D'), array('[', ']'), http_build_query($sPostData));
  173. }
  174. if($sPostData)
  175. {
  176. $sRequest .= 'Content-Length: ' . strlen($sPostData) . "\r\n";
  177. $sRequest .= 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' . "\r\n" . "\r\n";
  178. $sRequest .= $sPostData;
  179. }
  180. else
  181. {
  182. $sRequest .= "\r\n";
  183. }
  184. if($bDebug === true)
  185. {
  186. echo "\r\n" . "\r\n" . '<h1>SEND DATA:</h1>' . "\r\n" . '<code style="display: block; background: #E0E0E0; border: #000000 solid 1px; padding: 10px;">' . str_replace(array("\n", "\r"), array('<br>' . "\r\n", ''), htmlspecialchars($sRequest)) . '</code>' . "\r\n" . "\r\n";
  187. }
  188. // Send data
  189. fputs($oSocket, $sRequest);
  190. // Recieve data
  191. while(!feof($oSocket))
  192. {
  193. $sResponse .= @fgets($oSocket, 128);
  194. }
  195. fclose($oSocket);
  196. if($bDebug === true)
  197. {
  198. echo "\r\n" . "\r\n" . '<h1>RECIEVED DATA:</h1>' . "\r\n" . '<code style="display: block; background: #E0E0E0; border: #000000 solid 1px; padding: 10px;">' . str_replace(array("\n", "\r"), array('<br>' . "\r\n", ''), htmlspecialchars($sResponse)) . '</code>' . "\r\n" . "\r\n";
  199. }
  200. if($bRemoveHeaders) // Remove headers from reply?
  201. {
  202. list($sHeader, $sBody) = preg_split('/(\\r?\\n){2,2}/', $sResponse, 2);
  203. return $sBody;
  204. }
  205. else
  206. {
  207. return $sResponse;
  208. }
  209. }
  210. else
  211. {
  212. if($bDebug)
  213. {
  214. echo "\r\n" . "\r\n" . 'Cannot connect to: ' . htmlspecialchars($sRequestUrl);
  215. }
  216. die('Socket error: ' . htmlspecialchars($sErrorMessage));
  217. }
  218. }
  219. // doHttpRequest (Uses curl-library)
  220. function idealcheckout_doHttpRequest_curl($sUrl, $sPostData = false, $bRemoveHeaders = false, $iTimeout = 30, $bDebug = false)
  221. {
  222. $aUrl = parse_url($sUrl);
  223. $sRequestUrl = '';
  224. if(in_array($aUrl['scheme'], array('ssl', 'https')))
  225. {
  226. $sRequestUrl .= 'https://';
  227. if(empty($aUrl['port']))
  228. {
  229. $aUrl['port'] = 443;
  230. }
  231. }
  232. else
  233. {
  234. $sRequestUrl .= 'http://';
  235. if(empty($aUrl['port']))
  236. {
  237. $aUrl['port'] = 80;
  238. }
  239. }
  240. $sRequestUrl .= $aUrl['host'] . (empty($aUrl['path']) ? '/' : $aUrl['path']) . (empty($aUrl['query']) ? '' : '?' . $aUrl['query']);
  241. if(is_array($sPostData))
  242. {
  243. $sPostData = str_replace(array('%5B', '%5D'), array('[', ']'), http_build_query($sPostData));
  244. }
  245. if($bDebug === true)
  246. {
  247. $sRequest = 'Requested URL: ' . $sRequestUrl . "\r\n";
  248. $sRequest .= 'Portnumber: ' . $aUrl['port'] . "\r\n";
  249. if($sPostData)
  250. {
  251. $sRequest .= 'Posted data: ' . $sPostData . "\r\n";
  252. }
  253. echo "\r\n" . "\r\n" . '<h1>SEND DATA:</h1>' . "\r\n" . '<code style="display: block; background: #E0E0E0; border: #000000 solid 1px; padding: 10px;">' . str_replace(array("\n", "\r"), array('<br>' . "\r\n", ''), htmlspecialchars($sRequest)) . '</code>' . "\r\n" . "\r\n";
  254. }
  255. $oCurl = curl_init();
  256. curl_setopt($oCurl, CURLOPT_URL, $sRequestUrl);
  257. curl_setopt($oCurl, CURLOPT_PORT, $aUrl['port']);
  258. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  259. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  260. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  261. curl_setopt($oCurl, CURLOPT_TIMEOUT, $iTimeout);
  262. curl_setopt($oCurl, CURLOPT_HEADER, $bRemoveHeaders == false);
  263. if($sPostData != false)
  264. {
  265. curl_setopt($oCurl, CURLOPT_POST, true);
  266. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $sPostData);
  267. }
  268. $sResponse = curl_exec($oCurl);
  269. curl_close($oCurl);
  270. if($bDebug === true)
  271. {
  272. echo "\r\n" . "\r\n" . '<h1>RECIEVED DATA:</h1>' . "\r\n" . '<code style="display: block; background: #E0E0E0; border: #000000 solid 1px; padding: 10px;">' . str_replace(array("\n", "\r"), array('<br>' . "\r\n", ''), htmlspecialchars($sResponse)) . '</code>' . "\r\n" . "\r\n";
  273. }
  274. if(empty($sResponse))
  275. {
  276. return '';
  277. }
  278. return $sResponse;
  279. }
  280. // Print html to screen
  281. function idealcheckout_output($sHtml, $bImage = true)
  282. {
  283. global $aIdealCheckout;
  284. // Detect idealcheckout folder
  285. $sRootUrl = idealcheckout_getRootUrl();
  286. if(($iStrPos = strpos($sRootUrl, '/idealcheckout/')) !== false)
  287. {
  288. $sRootUrl = substr($sRootUrl, 0, $iStrPos) . '/';
  289. }
  290. // Detect gateway name & image
  291. $sTitle = 'Checkout';
  292. $sImage = 'gateway.png';
  293. $sColor = '#999999';
  294. if(!empty($aIdealCheckout['record']['gateway_code']))
  295. {
  296. if(strcasecmp($aIdealCheckout['record']['gateway_code'], 'afterpay') === 0)
  297. {
  298. $sTitle = 'AfterPay';
  299. $sImage = 'afterpay.png';
  300. $sColor = '#759D41';
  301. }
  302. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'authorizedtransfer') === 0)
  303. {
  304. $sTitle = 'Eenmalige machtiging / Incasso';
  305. }
  306. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'cartebleue') === 0)
  307. {
  308. $sTitle = 'Carte Bleue Checkout';
  309. $sImage = 'cartebleue.png';
  310. $sColor = '#01468B';
  311. }
  312. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'clickandbuy') === 0)
  313. {
  314. $sTitle = 'Click and Buy Checkout';
  315. $sImage = 'clickandbuy.png';
  316. $sColor = '#FD8A13';
  317. }
  318. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'creditcard') === 0)
  319. {
  320. $sTitle = 'CreditCard Checkout';
  321. $sImage = 'creditcard.png';
  322. }
  323. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'directebanking') === 0)
  324. {
  325. $sTitle = 'Direct E-Banking Checkout';
  326. $sImage = 'directebanking.png';
  327. $sColor = '#F18E00';
  328. }
  329. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'ebon') === 0)
  330. {
  331. $sTitle = 'E-Bon Checkout';
  332. $sImage = 'ebon.png';
  333. $sColor = '#F2672A';
  334. }
  335. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'fasterpay') === 0)
  336. {
  337. $sTitle = 'FasterPay Checkout';
  338. $sImage = 'fasterpay.png';
  339. $sColor = '#0023A1';
  340. }
  341. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'giropay') === 0)
  342. {
  343. $sTitle = 'GiroPay Checkout';
  344. $sImage = 'giropay.png';
  345. $sColor = '#000269';
  346. }
  347. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'ideal') === 0)
  348. {
  349. $sTitle = 'iDEAL Checkout';
  350. $sImage = 'ideal.png';
  351. $sColor = '#CC0066';
  352. }
  353. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'maestro') === 0)
  354. {
  355. $sTitle = 'Maestro Checkout';
  356. $sImage = 'maestro.png';
  357. $sColor = '#CC0000';
  358. }
  359. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'mastercard') === 0)
  360. {
  361. $sTitle = 'Mastercard Checkout';
  362. $sImage = 'mastercard.png';
  363. $sColor = '#FFAA18';
  364. }
  365. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'minitix') === 0)
  366. {
  367. $sTitle = 'MiniTix Checkout';
  368. $sImage = 'minitix.png';
  369. $sColor = '#FFCC00';
  370. }
  371. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'mistercash') === 0)
  372. {
  373. $sTitle = 'MisterCash Checkout';
  374. $sImage = 'mistercash.png';
  375. $sColor = '#0083C6';
  376. }
  377. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'manualtransfer') === 0)
  378. {
  379. $sTitle = 'Overboeking';
  380. }
  381. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'paypal') === 0)
  382. {
  383. $sTitle = 'PayPal Checkout';
  384. $sImage = 'paypal.png';
  385. $sColor = '#0E569F';
  386. }
  387. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'paysafecard') === 0)
  388. {
  389. $sTitle = 'PaySafeCard Checkout';
  390. $sImage = 'paysafecard.png';
  391. $sColor = '#008ACA';
  392. }
  393. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'postepay') === 0)
  394. {
  395. $sTitle = 'Postepay Checkout';
  396. $sImage = 'postepay.png';
  397. $sColor = '#F0EF02';
  398. }
  399. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'visa') === 0)
  400. {
  401. $sTitle = 'Visa Checkout';
  402. $sImage = 'visa.png';
  403. $sColor = '#1C1E75';
  404. }
  405. elseif(strcasecmp($aIdealCheckout['record']['gateway_code'], 'vpay') === 0)
  406. {
  407. $sTitle = 'V PAY Checkout';
  408. $sImage = 'vpay.png';
  409. $sColor = '#0023A1';
  410. }
  411. }
  412. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  413. <html>
  414. <head>
  415. <title>' . $sTitle . '</title>
  416. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15">
  417. <style type="text/css">
  418. html, body, form, div
  419. {
  420. margin: 0px;
  421. padding: 0px;
  422. }
  423. div.wrapper
  424. {
  425. padding: 50px 0px 0px 0px;
  426. text-align: center;
  427. }
  428. p
  429. {
  430. font-family: Arial;
  431. font-size: 15px;
  432. }
  433. a
  434. {
  435. color: ' . $sColor . ' !important;
  436. }
  437. td
  438. {
  439. font-family: Arial;
  440. font-size: 12px;
  441. }
  442. </style>
  443. </head>
  444. <body>
  445. <!--
  446. This ' . $sTitle . ' script is developed by:
  447. iDEAL Checkout
  448. Support & Information:
  449. E. info@ideal-checkout.nl
  450. W. http://www.ideal-checkout.nl
  451. T. +31614707337
  452. -->
  453. <div class="wrapper">
  454. ' . ($bImage ? '<p><img alt="' . $sTitle . '" border="0" src="' . $sRootUrl . 'idealcheckout/images/' . $sImage . '"></p>' : '') . '
  455. ' . $sHtml . '
  456. </div>
  457. </body>
  458. </html>';
  459. exit;
  460. }
  461. // Translate text using language files
  462. function idealcheckout_getTranslation($sLanguageCode = false, $sGroup, $sKey, $aParams = array())
  463. {
  464. global $aIdealCheckout;
  465. if(empty($sLanguageCode))
  466. {
  467. if(!empty($aIdealCheckout['record']['language']))
  468. {
  469. $sLanguageCode = strtolower($aIdealCheckout['record']['language']);
  470. }
  471. elseif(!empty($aIdealCheckout['language']))
  472. {
  473. $sLanguageCode = strtolower($aIdealCheckout['language']);
  474. }
  475. else
  476. {
  477. $sLanguageCode = 'en';
  478. }
  479. }
  480. if(!isset($aIdealCheckout['translations'][$sLanguageCode][$sGroup]))
  481. {
  482. $sTranslationFile = dirname(dirname(__FILE__)) . '/translations/' . $sGroup . '.' . $sLanguageCode . '.php';
  483. if(file_exists($sTranslationFile))
  484. {
  485. $aIdealCheckout['translations'][$sLanguageCode][$sGroup] = include_once($sTranslationFile);
  486. }
  487. }
  488. if(isset($aIdealCheckout['translations'][$sLanguageCode][$sGroup][$sKey]))
  489. {
  490. $sText = $aIdealCheckout['translations'][$sLanguageCode][$sGroup][$sKey];
  491. }
  492. else
  493. {
  494. $sText = $sKey;
  495. }
  496. if(is_array($aParams) && sizeof($aParams))
  497. {
  498. foreach($aParams as $k => $v)
  499. {
  500. $sText = str_replace('{' . $k . '}', $v, $sText);
  501. }
  502. }
  503. return $sText;
  504. }
  505. // Load database settings
  506. function idealcheckout_getDatabaseSettings($sStoreCode = false)
  507. {
  508. global $aIdealCheckout;
  509. if(empty($sStoreCode))
  510. {
  511. if(!empty($aIdealCheckout['record']['store_code']))
  512. {
  513. $sStoreCode = $aIdealCheckout['record']['store_code'];
  514. }
  515. else
  516. {
  517. $sStoreCode = idealcheckout_getStoreCode();
  518. }
  519. }
  520. $sDatabaseFile1 = dirname(dirname(__FILE__)) . '/configuration/database.' . strtolower($sStoreCode) . '.php';
  521. $sDatabaseFile2 = dirname(dirname(__FILE__)) . '/configuration/database.php';
  522. $sDatabaseError = 'No configuration file available for database.';
  523. $aSettings = array();
  524. // Database Server/Host
  525. $aSettings['host'] = 'localhost';
  526. // Database Type
  527. $aSettings['type'] = 'mysql';
  528. // Database Username
  529. $aSettings['user'] = '';
  530. // Database Password
  531. $aSettings['pass'] = '';
  532. // Database Name
  533. $aSettings['name'] = '';
  534. // Database Table Prefix (if any)
  535. $aSettings['prefix'] = '';
  536. // iDEAL Checkout Table
  537. $aSettings['table'] = '';
  538. if(file_exists($sDatabaseFile1) && @is_file($sDatabaseFile1) && @is_readable($sDatabaseFile1))
  539. {
  540. include($sDatabaseFile1);
  541. }
  542. elseif(file_exists($sDatabaseFile2) && @is_file($sDatabaseFile2) && @is_readable($sDatabaseFile2))
  543. {
  544. include($sDatabaseFile2);
  545. }
  546. else
  547. {
  548. idealcheckout_die('ERROR: ' . $sDatabaseError, __FILE__, __LINE__, false);
  549. }
  550. // iDEAL Checkout Table
  551. if(empty($aSettings['table']))
  552. {
  553. $aSettings['table'] = $aSettings['prefix'] . 'idealcheckout';
  554. }
  555. return $aSettings;
  556. }
  557. // Load database settings
  558. function idealcheckout_getWebsiteSettings($sStoreCode = false)
  559. {
  560. global $aIdealCheckout;
  561. if(empty($sStoreCode))
  562. {
  563. if(!empty($aIdealCheckout['record']['store_code']))
  564. {
  565. $sStoreCode = $aIdealCheckout['record']['store_code'];
  566. }
  567. else
  568. {
  569. $sStoreCode = idealcheckout_getStoreCode();
  570. }
  571. }
  572. $sWebsiteFile1 = dirname(dirname(__FILE__)) . '/configuration/website.' . strtolower($sStoreCode) . '.php';
  573. $sWebsiteFile2 = dirname(dirname(__FILE__)) . '/configuration/website.php';
  574. $sWebsiteError = 'No configuration file available for website.';
  575. $aSettings = array();
  576. if(file_exists($sWebsiteFile1) && @is_file($sWebsiteFile1) && @is_readable($sWebsiteFile1))
  577. {
  578. include($sWebsiteFile1);
  579. }
  580. elseif(file_exists($sWebsiteFile2) && @is_file($sWebsiteFile2) && @is_readable($sWebsiteFile2))
  581. {
  582. include($sWebsiteFile2);
  583. }
  584. else
  585. {
  586. // idealcheckout_die('ERROR: ' . $sWebsiteError, __FILE__, __LINE__, false);
  587. }
  588. return $aSettings;
  589. }
  590. // Load gateway settings
  591. function idealcheckout_getGatewaySettings($sStoreCode = false, $sGatewayCode = false)
  592. {
  593. global $aIdealCheckout;
  594. if(empty($sStoreCode))
  595. {
  596. if(!empty($aIdealCheckout['record']['store_code']))
  597. {
  598. $sStoreCode = $aIdealCheckout['record']['store_code'];
  599. }
  600. else
  601. {
  602. $sStoreCode = idealcheckout_getStoreCode();
  603. }
  604. }
  605. if(empty($sGatewayCode))
  606. {
  607. if(!empty($aIdealCheckout['record']['gateway_code']))
  608. {
  609. $sGatewayCode = $aIdealCheckout['record']['gateway_code'];
  610. }
  611. else
  612. {
  613. $sGatewayCode = 'ideal';
  614. }
  615. }
  616. if(!preg_match('/^([a-zA-Z0-9_\-]+)$/', $sGatewayCode))
  617. {
  618. idealcheckout_die('INVALID GATEWAY: ' . $sGatewayCode, __FILE__, __LINE__, false);
  619. }
  620. elseif(!preg_match('/^([a-zA-Z0-9_\-]+)$/', $sStoreCode))
  621. {
  622. idealcheckout_die('INVALID STORE CODE: ' . $sStoreCode, __FILE__, __LINE__, false);
  623. }
  624. $sConfigurationPath = dirname(dirname(__FILE__)) . '/configuration/';
  625. $sConfigFile1 = $sConfigurationPath . strtolower($sGatewayCode) . '.' . strtolower($sStoreCode) . '.php';
  626. $sConfigFile2 = $sConfigurationPath . strtolower($sGatewayCode) . '.php';
  627. $sConfigError = 'No configuration file available for ' . $sGatewayCode . '.';
  628. $aSettings = array();
  629. if(file_exists($sConfigFile1) && @is_file($sConfigFile1) && @is_readable($sConfigFile1))
  630. {
  631. include($sConfigFile1);
  632. }
  633. elseif(file_exists($sConfigFile2) && @is_file($sConfigFile2) && @is_readable($sConfigFile2))
  634. {
  635. include($sConfigFile2);
  636. }
  637. else
  638. {
  639. idealcheckout_die('ERROR: ' . $sConfigError, __FILE__, __LINE__, false);
  640. }
  641. if(empty($aSettings['TEST_MODE']))
  642. {
  643. $aSettings['TEST_MODE'] = false;
  644. }
  645. // Fix temp path
  646. if(empty($aSettings['TEMP_PATH']))
  647. {
  648. $aSettings['TEMP_PATH'] = dirname(dirname(__FILE__)) . '/temp/';
  649. }
  650. // Fix certificate path
  651. if(empty($aSettings['CERTIFICATE_PATH']))
  652. {
  653. $aSettings['CERTIFICATE_PATH'] = dirname(dirname(__FILE__)) . '/certificates/';
  654. }
  655. // Fix gateway path
  656. if(!empty($aSettings['GATEWAY_METHOD']))
  657. {
  658. $aSettings['GATEWAY_FILE'] = dirname(dirname(__FILE__)) . '/gateways/' . $aSettings['GATEWAY_METHOD'] . '/gateway.cls.php';
  659. }
  660. elseif(strcasecmp(substr($aSettings['GATEWAY_FILE'], 0, 10), '/gateways/') === 0)
  661. {
  662. $aSettings['GATEWAY_FILE'] = dirname(dirname(__FILE__)) . $aSettings['GATEWAY_FILE'];
  663. }
  664. elseif(strcasecmp(substr($aSettings['GATEWAY_FILE'], 0, 9), 'gateways/') === 0)
  665. {
  666. $aSettings['GATEWAY_FILE'] = dirname(dirname(__FILE__)) . '/' . $aSettings['GATEWAY_FILE'];
  667. }
  668. return $aSettings;
  669. }
  670. function idealcheckout_die($sError, $sFile = false, $iLine = false, $sGatewayCode = 'ideal')
  671. {
  672. $bDebugMode = false;
  673. if(file_exists(dirname(__FILE__) . '/debug.php'))
  674. {
  675. $bDebugMode = true;
  676. }
  677. elseif($sGatewayCode)
  678. {
  679. $aGatewaySettings = idealcheckout_getGatewaySettings(false, $sGatewayCode);
  680. if(!empty($aGatewaySettings['test_mode']))
  681. {
  682. $bDebugMode = true;
  683. }
  684. }
  685. if($bDebugMode)
  686. {
  687. $sError = str_replace(array("\r\n", "\r", "\n"), '<br>', htmlentities($sError));
  688. echo $sError;
  689. if($sFile !== false)
  690. {
  691. echo '<br><br>FILE: ' . $sFile;
  692. }
  693. if($iLine !== false)
  694. {
  695. echo '<br><br>LINE: ' . $iLine;
  696. }
  697. }
  698. else
  699. {
  700. echo 'A fatal error has occured. Please check your log files.';
  701. idealcheckout_log($sError, $sFile, $iLine);
  702. }
  703. exit;
  704. }
  705. function idealcheckout_log($sText, $sFile = false, $iLine = false)
  706. {
  707. if(is_array($sText) || is_object($sText))
  708. {
  709. $sText = var_export($sText, true);
  710. }
  711. // Reformat text
  712. $sText = str_replace("\n", "\n ", trim($sText));
  713. $sLog = "\n" . 'TEXT: ' . $sText . "\n";
  714. if($sFile !== false)
  715. {
  716. $sLog .= 'FILE: ' . $sFile . "\n";
  717. }
  718. if($sFile !== false)
  719. {
  720. $sLog .= 'LINE: ' . $iLine . "\n";
  721. }
  722. $sLog .= "\n";
  723. $sLogFile = dirname(dirname(__FILE__)) . '/temp/' . date('Ymd.His') . '.log';
  724. file_put_contents($sLogFile, $sLog, FILE_APPEND);
  725. chmod($sLogFile, 0777);
  726. }
  727. // Streetname 1a => array('Streetname', '1a')
  728. function idealcheckout_splitAddress($sAddress)
  729. {
  730. $sAddress = trim($sAddress);
  731. $a = preg_split('/([0-9]+)/', $sAddress, 2, PREG_SPLIT_DELIM_CAPTURE);
  732. $sStreetName = trim(array_shift($a));
  733. $sStreetNumber = trim(implode('', $a));
  734. if(empty($sStreetName)) // American address notation
  735. {
  736. $a = preg_split('/([a-zA-Z]{2,})/', $sAddress, 2, PREG_SPLIT_DELIM_CAPTURE);
  737. $sStreetNumber = trim(implode('', $a));
  738. $sStreetName = trim(array_shift($a));
  739. }
  740. return array($sStreetName, $sStreetNumber);
  741. }
  742. function idealcheckout_database_setup($oDatabaseConnection = false)
  743. {
  744. global $aIdealCheckout;
  745. if(empty($aIdealCheckout['database']['connection']))
  746. {
  747. // Find database configuration
  748. $aIdealCheckout['database'] = idealcheckout_getDatabaseSettings();
  749. // Connect to database
  750. $aIdealCheckout['database']['connection'] = idealcheckout_database_connect($aIdealCheckout['database']['host'], $aIdealCheckout['database']['user'], $aIdealCheckout['database']['pass']) or idealcheckout_die('ERROR: Cannot connect to ' . $aIdealCheckout['database']['type'] . ' server. Error in hostname, username and/or password.', __FILE__, __LINE__, false);
  751. idealcheckout_database_select_db($aIdealCheckout['database']['connection'], $aIdealCheckout['database']['name']) or idealcheckout_die('ERROR: Cannot find database `' . $aIdealCheckout['database']['name'] . '` on ' . $aIdealCheckout['database']['host'] . '.', __FILE__, __LINE__, false);
  752. }
  753. return $aIdealCheckout['database']['connection'];
  754. }
  755. function idealcheckout_database_query($sQuery, $oDatabaseConnection = false)
  756. {
  757. global $aIdealCheckout;
  758. if($oDatabaseConnection === false)
  759. {
  760. $oDatabaseConnection = idealcheckout_database_setup();
  761. }
  762. if(!empty($aIdealCheckout['database']['type']) && (strcmp($aIdealCheckout['database']['type'], 'mysqli') === 0))
  763. {
  764. return mysqli_query($oDatabaseConnection, $sQuery);
  765. }
  766. else
  767. {
  768. return mysql_query($sQuery, $oDatabaseConnection);
  769. }
  770. }
  771. function idealcheckout_database_getRecord($sQuery, $oDatabaseConnection = false)
  772. {
  773. $aRecords = idealcheckout_database_getRecords($sQuery, $oDatabaseConnection);
  774. if(sizeof($aRecords) > 0)
  775. {
  776. return $aRecords[0];
  777. }
  778. return false;
  779. }
  780. function idealcheckout_database_getRecords($sQuery, $oDatabaseConnection = false)
  781. {
  782. global $aIdealCheckout;
  783. if($oDatabaseConnection === false)
  784. {
  785. $oDatabaseConnection = idealcheckout_database_setup();
  786. }
  787. $aRecords = array();
  788. if(!empty($aIdealCheckout['database']['type']) && (strcmp($aIdealCheckout['database']['type'], 'mysqli') === 0))
  789. {
  790. if($oRecordset = mysqli_query($oDatabaseConnection, $sQuery))
  791. {
  792. while($aRecord = mysqli_fetch_assoc($oRecordset))
  793. {
  794. $aRecords[] = $aRecord;
  795. }
  796. mysqli_free_result($oRecordset);
  797. }
  798. }
  799. else
  800. {
  801. if($oRecordset = mysql_query($sQuery, $oDatabaseConnection))
  802. {
  803. while($aRecord = mysql_fetch_assoc($oRecordset))
  804. {
  805. $aRecords[] = $aRecord;
  806. }
  807. mysql_free_result($oRecordset);
  808. }
  809. }
  810. return $aRecords;
  811. }
  812. function idealcheckout_database_error($oDatabaseConnection = false)
  813. {
  814. global $aIdealCheckout;
  815. if($oDatabaseConnection === false)
  816. {
  817. $oDatabaseConnection = idealcheckout_database_setup();
  818. }
  819. if(!empty($aIdealCheckout['database']['type']) && (strcmp($aIdealCheckout['database']['type'], 'mysqli') === 0))
  820. {
  821. return @mysqli_error($oDatabaseConnection);
  822. }
  823. else
  824. {
  825. return @mysql_error($oDatabaseConnection);
  826. }
  827. }
  828. function idealcheckout_database_fetch_assoc($oRecordSet)
  829. {
  830. global $aIdealCheckout;
  831. if(!empty($aIdealCheckout['database']['type']) && (strcmp($aIdealCheckout['database']['type'], 'mysqli') === 0))
  832. {
  833. return mysqli_fetch_assoc($oRecordSet);
  834. }
  835. else
  836. {
  837. return mysql_fetch_assoc($oRecordSet);
  838. }
  839. }
  840. function idealcheckout_database_connect($oDatabaseConnection = false)
  841. {
  842. global $aIdealCheckout;
  843. if(!empty($aIdealCheckout['database']['type']) && (strcmp($aIdealCheckout['database']['type'], 'mysqli') === 0))
  844. {
  845. return mysqli_connect($aIdealCheckout['database']['host'], $aIdealCheckout['database']['user'], $aIdealCheckout['database']['pass']);
  846. }
  847. else
  848. {
  849. return mysql_connect($aIdealCheckout['database']['host'], $aIdealCheckout['database']['user'], $aIdealCheckout['database']['pass']);
  850. }
  851. }
  852. function idealcheckout_database_select_db($oDatabaseConnection = false, $sDatabaseName = false)
  853. {
  854. global $aIdealCheckout;
  855. if($oDatabaseConnection === false)
  856. {
  857. $oDatabaseConnection = idealcheckout_database_setup();
  858. }
  859. if(!empty($aIdealCheckout['database']['type']) && (strcmp($aIdealCheckout['database']['type'], 'mysqli') === 0))
  860. {
  861. return mysqli_select_db($oDatabaseConnection, $sDatabaseName);
  862. }
  863. else
  864. {
  865. return mysql_select_db($sDatabaseName, $oDatabaseConnection);
  866. }
  867. }
  868. function idealcheckout_database_num_rows($oRecordSet)
  869. {
  870. global $aIdealCheckout;
  871. if(!empty($aIdealCheckout['database']['type']) && (strcmp($aIdealCheckout['database']['type'], 'mysqli') === 0))
  872. {
  873. return mysqli_num_rows($oRecordSet);
  874. }
  875. else
  876. {
  877. return mysql_num_rows($oRecordSet);
  878. }
  879. }
  880. function idealcheckout_database_insert_id($oDatabaseConnection = false)
  881. {
  882. global $aIdealCheckout;
  883. if($oDatabaseConnection === false)
  884. {
  885. $oDatabaseConnection = idealcheckout_database_setup();
  886. }
  887. if(!empty($aIdealCheckout['database']['type']) && (strcmp($aIdealCheckout['database']['type'], 'mysqli') === 0))
  888. {
  889. return mysqli_insert_id($oDatabaseConnection);
  890. }
  891. else
  892. {
  893. return mysql_insert_id($oDatabaseConnection);
  894. }
  895. }
  896. function idealcheckout_getPaymentButton($aParams, $sSubmitButton = 'Afrekenen', $sFormUrl = 'idealcheckout/checkout.php')
  897. {
  898. $sHtml = '<form action="' . htmlspecialchars($sFormUrl) . '" method="post">';
  899. foreach($aParams as $k => $v)
  900. {
  901. $sHtml .= '<input name="' . htmlspecialchars($k) . '" type="hidden" value="' . htmlspecialchars($v) . '">';
  902. }
  903. if(strpos($sSubmitButton, '://') !== false)
  904. {
  905. $sHtml .= '<input type="image" src="' . htmlspecialchars($sSubmitButton) . '">';
  906. }
  907. elseif(strpos($sSubmitButton, '<input') !== false)
  908. {
  909. $sHtml .= $sSubmitButton;
  910. }
  911. else
  912. {
  913. $sHtml .= '<input type="submit" value="' . htmlspecialchars($sSubmitButton) . '">';
  914. }
  915. $sHtml .= '</form>';
  916. return $sHtml;
  917. }
  918. function idealcheckout_php_execute($_____CODE, $_____PARAMS = array())
  919. {
  920. foreach($_____PARAMS as $k => $v)
  921. {
  922. ${$k} = $v;
  923. }
  924. $_____CODE = trim($_____CODE);
  925. if(strcasecmp(substr($_____CODE, 0, 5), '<' . '?' . 'php') === 0)
  926. {
  927. $_____CODE = substr($_____CODE, 5);
  928. }
  929. elseif(strcasecmp(substr($_____CODE, 0, 2), '<' . '?') === 0)
  930. {
  931. $_____CODE = substr($_____CODE, 2);
  932. }
  933. if(strcasecmp(substr($_____CODE, -2, 2), '?' . '>') === 0)
  934. {
  935. $_____CODE = substr($_____CODE, 0, -2);
  936. }
  937. $_____CODE = trim($_____CODE);
  938. eval($_____CODE);
  939. }
  940. function idealcheckout_sendMail($oRecord)
  941. {
  942. $aGatewaySettings = idealcheckout_getGatewaySettings($oRecord['store_code'], $oRecord['gateway_code']);
  943. $sWebsiteUrl = idealcheckout_getRootUrl(1);
  944. if(!empty($aGatewaySettings['TRANSACTION_UPDATE_EMAILS']))
  945. {
  946. if(strpos($aGatewaySettings['TRANSACTION_UPDATE_EMAILS'], ',') !== false)
  947. {
  948. $aEmails = explode(',', $aGatewaySettings['TRANSACTION_UPDATE_EMAILS']);
  949. }
  950. elseif(strpos($aGatewaySettings['TRANSACTION_UPDATE_EMAILS'], ';') !== false)
  951. {
  952. $aEmails = explode(';', $aGatewaySettings['TRANSACTION_UPDATE_EMAILS']);
  953. }
  954. else
  955. {
  956. $aEmails = array($aGatewaySettings['TRANSACTION_UPDATE_EMAILS']);
  957. }
  958. foreach($aEmails as $k => $sEmail)
  959. {
  960. $sMailTo = trim($sEmail);
  961. if(preg_match('/^([a-z0-9\-_\.]+)@([a-z0-9\-_\.]+)\.[a-z]{2,6}$/i', $sMailTo)) // Validate e-mail address
  962. {
  963. $sMailSubject = 'Transaction Update: ' . $oRecord['transaction_description'];
  964. $sMailHeaders = 'From: "' . $sWebsiteUrl . '" <' . $sMailTo . '>';
  965. $sMailMessage = 'TRANSACTION UPDATE
  966. Order: ' . $oRecord['order_id'] . '
  967. Bedrag: ' . $oRecord['transaction_amount'] . '
  968. Omschrijving: ' . $oRecord['transaction_description'] . '
  969. Transactie: ' . $oRecord['transaction_id'] . '
  970. Status: ' . $oRecord['transaction_status'] . '
  971. Controleer de definitieve status van transacties ALTIJD via uw Dashboard of bankafschrift.
  972. Deze e-mail is gegenereerd door ' . $sWebsiteUrl . ' op ' . date('d-m-Y, H:i') . '.
  973. ';
  974. if(@mail($sMailTo, $sMailSubject, $sMailMessage, $sMailHeaders))
  975. {
  976. // idealcheckout_log('Transaction update send to: ' . $sMailTo, __FILE__, __LINE__);
  977. }
  978. else
  979. {
  980. idealcheckout_log('Error while sending e-mail to: ' . $sMailTo, __FILE__, __LINE__);
  981. }
  982. }
  983. else
  984. {
  985. idealcheckout_log('Invalid e-mail address: ' . $sMailTo, __FILE__, __LINE__);
  986. }
  987. }
  988. }
  989. }
  990. function idealcheckout_arrayToText($aArray, $iWhiteSpace = 0)
  991. {
  992. $sData = '';
  993. if(is_array($aArray) && sizeof($aArray))
  994. {
  995. foreach($aArray as $k1 => $v1)
  996. {
  997. if(strlen($sData))
  998. {
  999. $sData .= "\n";
  1000. }
  1001. $sData .= str_repeat(' ', $iWhiteSpace) . $k1 . ': ';
  1002. if(is_object($v1))
  1003. {
  1004. $sData .= '[' . get_class($v1) . ' object], ';
  1005. }
  1006. elseif(is_array($v1))
  1007. {
  1008. $sData .= "\n" . idealcheckout_arrayToText($v1, $iWhiteSpace + strlen($k1) + 2) . ', ';
  1009. }
  1010. elseif($v1 === true)
  1011. {
  1012. $sData .= 'TRUE, ';
  1013. }
  1014. elseif($v1 === false)
  1015. {
  1016. $sData .= 'FALSE, ';
  1017. }
  1018. elseif($v1 === null)
  1019. {
  1020. $sData .= 'NULL, ';
  1021. }
  1022. else
  1023. {
  1024. $sData .= $v1 . ', ';
  1025. }
  1026. }
  1027. $sData = substr($sData, 0, -2); // Remove last comma-space
  1028. }
  1029. return $sData;
  1030. }
  1031. ?>