PageRenderTime 595ms CodeModel.GetById 121ms app.highlight 303ms RepoModel.GetById 159ms app.codeStats 1ms

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