<?php

	class customer {
		var $shop_domain = 'pizza.com';
		var $local_path = '/';
		var $show_paypal = true;
		var $show_sofort = true;
		var $show_voucher = true;
		var $show_jobs = true;
		var $showPImages = true;
		var $send_fax = true;
		var $fax_username = 'XXXXXXXXXXX';
		var $fax_apikey = 'XXXXXXXXXXXXXXXXX';
		var $fax_reciever = '0049XXXXXX';
		var $fax_sender = '004XXXXXXX';
		var $fax_sendername = 'pizza.com';

		function __construct($ws = null) {
			$this->shopPath = ($this->local_path == '/' ? $_SERVER['DOCUMENT_ROOT'] . $this->local_path : $_SERVER['DOCUMENT_ROOT'] . $this->local_path . '/');
			$this->shopRoot = 'http://www.' . $this->shop_domain . $this->local_path;

			if (( $ws || session_id(  ) == '' )) {
				session_set_cookie_params( 0, '/', '.' . $this->shop_domain );
				session_start(  );
			}

			$this->check_for_holiday(  );
		}

		function check_debug($settings, $ws = null) {
			if (( !$settings['set_debug_state'] || ( $settings['set_debug_state'] && $_SERVER['REMOTE_ADDR'] == $settings['set_debug_ip'] ) )) {
				return true;
			}


			if (!$ws) {
				header( 'Content-Type: text/html; charset=utf-8' );
				echo $settings['set_debug_text'];
				exit(  );
				return null;
			}

			header( 'Content-Type: application/json' );
			$text = (!empty( $settings['set_debug_text'] ) ? strip_tags( $settings['set_debug_text'] ) : 'Webservice is offline. Please contact administrator!');
			echo '{"state":"error","message":"' . str_replace( array( '"' ), array( '' ), $text ) . '"}';
			exit(  );
		}

		function dbConnect() {
			global $connected;

			if ($connected === true) {
				return true;
			}

			require_once( 'database.inc.php' );
			$connected = true;
			return true;
		}

		function query($sql, $modType = null) {
			if (empty( $$sql )) {
				return false;
			}

			$this->dbConnect(  );
			mysql_query( $sql );
			( $que = mysql_query( $sql ) || mysql_error(  ) );

			if ($modType == 'insert') {
				return mysql_insert_id(  );
			}


			if ($modType == 'update') {
				return mysql_affected_rows(  );
			}


			if ($que) {
				return $que;
			}

			return false;
		}

		function fetch($que, $type = null) {
			if (!$type) {
				$assoc = mysql_fetch_assoc( $que );
				return $assoc;
			}


			if ($type == 'array') {
				$arr = mysql_fetch_assoc( $que );
				return $arr;
			}


			if ($type == 'row') {
				$row = mysql_fetch_row( $que );
				return $row;
			}

			return false;
		}

		function fetch_2_array($que) {
			if (empty( $$que )) {
				return false;
			}

			$arr = array(  );

			if ($row = mysql_fetch_assoc( $que )) {
				$arr[] = $row;
			}

			return $arr;
		}

		function fetch_id_array($que) {
			if (empty( $$que )) {
				return false;
			}

			$arr = array(  );

			while ($row = mysql_fetch_assoc( $que )) {
				$arr[$row['id']] = true;
			}

			return $arr;
		}

		function fetch_and_make_assoc($que, $fieldname, $value) {
			if (empty( $$que )) {
				return false;
			}

			$arr = array(  );

			if ($row = mysql_fetch_assoc( $que )) {
				$arr[$row[$fieldname]] = $row[$value];
			}

			return $arr;
		}

		function fetch_and_make_trans($que, $fieldname, $value) {
			if (empty( $$que )) {
				return false;
			}

			$arr = array(  );

			if ($row = mysql_fetch_assoc( $que )) {
				$arr[$row[$fieldname]] = str_replace( array( '
', '
', '' ), ' ', $row[$value] );
			}

			return $arr;
		}

		function fetch_and_make_assoc_deals($que, $val1, $val2, $val3, $val4) {
			if (empty( $$que )) {
				return false;
			}

			$arr = array(  );

			if ($row = mysql_fetch_assoc( $que )) {
				$arr[$row[$val1]] = array( 'type' => $row[$val2], 'value' => $row[$val3], 'only1' => $row[$val4] );
			}

			return $arr;
		}

		function check_for_only1_deal($cat, $ws) {
			if (( empty( $$cat ) || empty( $_SESSION['cart'] ) )) {
				return null;
			}


			if (!isset( $this->deals )) {
				$this->set_deals_data(  );
			}


			if (empty( $this->deals )) {
				return null;
			}


			if (( isset( $this->deals[$cat] ) && $this->deals[$cat]['only1'] == '1' )) {
				foreach ($_SESSION['cart'] as $pro) {

					if ($pro['cat'] == $cat) {
						if (!$ws) {
							exit( 'only1' );
							continue;
						}

						exit( '{"state":"only1","message":"Only 1 Product please"}' );
						continue;
					}
				}
			}

		}

		function check_for_deal($price, $cat) {
			if (!isset( $this->deals )) {
				$this->set_deals_data(  );
			}


			if (empty( $$cat )) {
				return $price;
			}


			if (( isset( $this->deals[$cat]['type'] ) && is_numeric( $this->deals[$cat]['value'] ) )) {
				if ($this->deals[$cat]['type'] == 'fixed') {
					return $this->deals[$cat]['value'];
				}


				if (( $this->deals[$cat]['type'] == 'minus' && 0 < $price - $this->deals[$cat]['value'] )) {
					return $price - $this->deals[$cat]['value'];
				}


				if (( ( $this->deals[$cat]['type'] == 'percent' && 0 < $this->deals[$cat]['value'] ) && $this->deals[$cat]['value'] < 100 )) {
					return round( $price - $price / 100 * $this->deals[$cat]['value'], 1 );
				}
			}

			return $price;
		}

		function set_deals_data() {
			if (!isset( $customer->deals )) {
				$checkDeals = $this->query( 'SELECT * FROM deals WHERE state=\'1\' AND  days LIKE \'%' . date( 'w' ) . '%\' LIMIT 6' );

				if (mysql_num_rows( $checkDeals>=0)) {
					$this->deals = $this->fetch_and_make_assoc_deals( $checkDeals, 'cat', 'type', 'value', 'only1' );
				}
			}

		}

		function fetch_postcodes($que, $fieldname) {
			if (empty( $$que )) {
				return false;
			}

			$arr = array(  );

			if ($row = mysql_fetch_assoc( $que )) {
				if (strpos( $row[$fieldname], 'codes' ) === false) {
					$arr[$row[$fieldname]] = $row;
				}

				$arr['set_mov' . substr( $row['type'], -1, 1 )]['codes'] = $row['value'];
				$arr['set_mov' . substr( $row['type'], -1, 1 )]['codesClean'] = explode( ',', trim( $row['value'] ) );
			}

			return $arr;
		}

		function fetch_images_and_make_assoc($que, $fieldname, $value) {
			if (empty( $$que )) {
				return false;
			}

			$arr = array(  );

			if ($row = mysql_fetch_assoc( $que )) {
				$explode = explode( '--', $row['value'] );
				$arr[$row[$fieldname]] = $explode;
			}

			return $arr;
		}

		function fetch_menu_2_arrays($que) {
			if (empty( $$que )) {
				return false;
			}

			$arr = array(  );
			$arr['main'] = array(  );
			$arr['sub'] = array(  );

			if ($row = mysql_fetch_assoc( $que )) {
				if ($row['type'] == 'main') {
					array_push( $arr['main'], $row );
				}


				if ($row['type'] == 'sub') {
					array_push( $arr['sub'], $row );
				}
			}

			return $arr;
		}

		function updateAllPrice($ws = null, $json_string_add = null) {
			$this->pProtection(  );
			$currQue = $this->query( 'SELECT value,type FROM settings WHERE type=\'set_currency\' OR type=\'set_mov\' OR type=\'set_scosts\' OR type=\'set_fscosts\'' );
			$curr = $this->fetch_and_make_assoc( $currQue, 'type', 'value' );
			$arr = array(  );

			if (( $curr['set_mov'] != '0' || ( $curr['set_mov'] == '0' && empty( $curr['set_scosts'] ) ) )) {
				if (!$ws) {
					$arr['state'] = 'ok';
					$res = $this->calculateAllPrice(  );
					$arr['price'] = sprintf( '%1$.2f', $res[0] ) . $curr['set_currency'];
				} 
else {
					$arr['shipping_costs'] = 'false';
					$res = $this->calculateAllPrice(  );
					$arr['all_price'] = sprintf( '%1$.2f', $res[0] );
				}
			} 
else {
				if (!$ws) {
					$res = $this->calculateAllPrice( $curr['set_scosts'], $curr['set_fscosts'] );
					$arr['state'] = (!$res[1] ? 'ok' : 'scosts');
					$arr['price'] = sprintf( '%1$.2f', $res[0] ) . $curr['set_currency'];
				} 
else {
					$res = $this->calculateAllPrice( $curr['set_scosts'], $curr['set_fscosts'] );
					$arr['shipping_costs'] = (!$res[1] ? 'false' : $curr['set_scosts']);
					$arr['all_price'] = sprintf( '%1$.2f', $res[0] );
				}
			}


			if (( !$ws && !$json_string_add )) {
				return json_encode( $arr );
			}


			if (( $ws && !$json_string_add )) {
				$shipping_costs = (empty( $arr['shipping_costs'] ) ? 0 : $arr['shipping_costs']);
				return '{"all_price":' . $arr['all_price'] . ',"shipping_costs": ' . $arr['shipping_costs'] . '}';
			}


			if ($json_string_add) {
				$arr['shipping_costs'];
				$arr['shipping_costs'] = 'false';
				return '"shipping_costs":' . $arr['shipping_costs'] . ' , "all_price":' . $arr['all_price'] . ' ';
			}

		}

		function removeProductFromCart($thiskey, $ws = null) {
			$this->pProtection(  );
			$currQue = $this->query( 'SELECT value,type FROM settings WHERE type=\'set_currency\' OR type=\'set_mov\' OR type=\'set_scosts\' OR type=\'set_fscosts\'' );
			$curr = $this->fetch_and_make_assoc( $currQue, 'type', 'value' );
			unset( $_SESSION['cart'][$thiskey] );

			if ($ws) {
				return '{"state":"ok", ' . $this->updateAllPrice( true, true ) . ' }';
			}

			$arr = array(  );

			if ($curr['set_mov'] != '0') {
				$arr['state'] = 'ok';
				$res = $this->calculateAllPrice(  );
				$arr['price'] = sprintf( '%1$.2f', $res[0] ) . $curr['set_currency'];
			} 
else {
				$res = $this->calculateAllPrice( $curr['set_scosts'], $curr['set_fscosts'] );
				$arr['state'] = (!$res[1] ? 'ok' : 'scosts');
				$arr['price'] = sprintf( '%1$.2f', $res[0] ) . $curr['set_currency'];
			}

			return json_encode( $arr );
		}

		function removeExtraFromProduct() {
			$this->pProtection(  );
			$currQue = $this->query( 'SELECT value FROM settings WHERE type=\'set_currency\' LIMIT 1' );
			$curr = $this->fetch( $currQue, 'row' );

			if (isset( $_SESSION['cart'][$_POST['key']]['extras'][(int)$_POST['myRel']] )) {
				unset( $_SESSION['cart'][$_POST['key']]['extras'][(int)$_POST['myRel']] );
				exit( sprintf( '%1$.2f', $this->calculatePPrice( $_POST['key'] ) ) . $curr[0] );
				return null;
			}

			exit( 'error' );
		}

		function count_extras_from_cart_product($key) {
			if (( !isset( $_SESSION['cart'][$key]['extras'] ) || ( isset( $_SESSION['cart'][$key]['extras'] ) && empty( $_SESSION['cart'][$key]['extras'] ) ) )) {
				return (int)0;
			}

			$int = (int)0;

			if (( isset( $_SESSION['cart'][$key]['extras'] ) && count( $_SESSION['cart'][$key]['extras'] ) )) {
				$int = $int + count( $_SESSION['cart'][$key]['extras'] );
			}

			return $int;
		}

		function addProductWithoutExtra($pro_id, $pos, $ws = null) {
			$this->pProtection(  );
			$currQue = $this->query( 'SELECT value FROM settings WHERE type=\'set_currency\' LIMIT 1' );
			$curr = $this->fetch( $currQue, 'row' );
			$getPData = $this->query( 'SELECT * FROM products WHERE id=' . $pro_id . ' AND type=\'pro\' AND active=\'1\' LIMIT 1' );

			if (mysql_num_rows( $getPData )) {
				$data = $this->fetch( $getPData );

				if (( !isset( $_SESSION['cart'] ) || !is_array( $_SESSION['cart'] ) )) {
					$_SESSION['cart'] = array(  );
				}

				$curr_price = $this->check_for_only1_deal( $data['cat' . $pos], $ws );
				$curr_price = $this->check_for_deal( (double)$data['price' . $pos], $data['cat' . $pos] );
				$_SESSION['cart'][] = array( 'pro' => $data['id'], 'intID' => $data['intID'], 'cat' => $data['cat' . $pos], 'name' => $data['name'], 'sName' => $data['sName' . $pos], 'des' => $data['des'], 'price' => $curr_price, 'vat' => $data['vat'], 'hasExtras' => false );
				$keys = array_keys( $_SESSION['cart'] );
				$key = array_pop( $keys );
			} 
else {
				exit(  );
			}


			if (( $ws && mysql_num_rows( $getPData ) )) {
				return '{"state":"ok", ' . $this->updateAllPrice( true, true ) . ' }';
			}


			if (( $ws && !mysql_num_rows( $getPData ) )) {
				return '{"state":"error","message":"Product with id ' . $pro_id . ' not found in database"}';
			}

			$str = '';
			$str .= '<li class="item hidden rounded" rel="' . $key . '">';
			$str .= '<p>';
			$str .= '<b>' . $data['name'] . ' ' . $data['sName' . $pos] . '</b>';
			$str .= '<span class="preprice">' . sprintf( '%1$.2f', $curr_price ) . $curr[0] . '</span>';
			$str .= '</p>';
			$str .= '<ul id="extras"></ul>';
			$str .= '<p>';
			$str .= '<button class="delItem" rel="' . $key . '">Löschen</button>';
			$str .= '<span class="price">' . sprintf( '%1$.2f', $this->calculatePPrice( $key ) ) . $curr[0] . '</span>';
			$str .= '</p>';
			$str .= '</li>';
			return $str;
		}

		function addProductWithExtra($id, $extras, $pos, $ws = null) {
			global $key;

			$this->pProtection(  );
			$currQue = $this->query( 'SELECT value FROM settings WHERE type=\'set_currency\' LIMIT 1' );
			$curr = $this->fetch( $currQue, 'row' );
			$getFromSession = $this->saveNewProductWithExtra( $id, $extras, $pos, null, $ws );

			if (!$getFromSession) {
				exit( 'error1' );
			}


			if (( $ws && $getFromSession )) {
				return '{"state":"ok", ' . $this->updateAllPrice( true, true ) . ', "last_insert_key": ' . $key . ' }';
			}

			$str = '';
			$str .= '<li rel="' . $key . '" class="item rounded" style="display: none;">';
			$str .= '<p><b>' . $getFromSession['name'] . ' ' . $getFromSession['sName'] . '</b><span class="preprice">' . sprintf( '%1$.2f', $getFromSession['price'] ) . $curr[0] . '</span></p>';
			$str .= '<ul id="extras">';

			if (array_key_exists( 'extras', $getFromSession )) {
				foreach ($getFromSession['extras'] as $eKey => $value) {
					$str .= '<li>' . $value['name'] . '<span>' . sprintf( '%1$.2f', $value['price'] ) . '</span> ';
					$str .= '<span class="delCartExtra" rel="' . $eKey . '">löschen</span>';
					$str .= '</li>';
				}
			}

			$str .= '</ul>';
			$str .= '<p>';
			$str .= '<button class="delItem" rel="' . $key . '">Löschen</button>';

			if (( array_key_exists( 'extras', $getFromSession ) || $getFromSession['hasExtras'] )) {
				$str .= '<button class="editExtras" rel="' . $key . '" >Extras</button>';
			}

			$str .= '<span class="price">' . sprintf( '%1$.2f', $this->calculatePPrice( $key ) ) . $curr[0] . '</span> ';
			$str .= '</p>';
			$str .= '</li>';
			return $str;
		}

		function editProductWithExtra() {
			global $key;

			$this->pProtection(  );
			$currQue = $this->query( 'SELECT value FROM settings WHERE type=\'set_currency\' LIMIT 1' );
			$curr = $this->fetch( $currQue, 'row' );
			$getFromSession = $this->saveNewProductWithExtra( $_POST['mp'], $_POST['ext'], $_POST['pos'], $_POST['currKey'] );

			if (!$getFromSession) {
				exit( 'error1' );
			}

			$str = '';
			$str .= '<li rel="' . $key . '" class="item rounded">';
			$str .= '<p><b>' . $getFromSession['name'] . ' ' . $getFromSession['sName'] . '</b><span class="preprice">' . sprintf( '%1$.2f', $getFromSession['price'] ) . $curr[0] . '</span></p>';
			$str .= '<ul id="extras">';

			if (array_key_exists( 'extras', $getFromSession )) {
				foreach ($getFromSession['extras'] as $eKey => $value) {
					$str .= '<li>' . $value['name'] . '<span>' . sprintf( '%1$.2f', $value['price'] ) . '</span> ';
					$str .= '<span class="delCartExtra" rel="' . $eKey . '">löschen</span>';
					$str .= '</li>';
				}
			}

			$str .= '</ul>';
			$str .= '<p>';
			$str .= '<button class="delItem" rel="' . $key . '">Löschen</button>';

			if (( array_key_exists( 'extras', $getFromSession ) || $getFromSession['hasExtras'] )) {
				$str .= '<button class="editExtras" rel="' . $key . '" >Extras</button>';
			}

			$str .= '<span class="price">' . sprintf( '%1$.2f', $this->calculatePPrice( $key ) ) . $curr[0] . '</span> ';
			$str .= '</p>';
			$str .= '</li>';
			return $str;
		}

		function storeFormData() {
			$_SESSION['c_' . $_POST['cname']] = trim( strip_tags( $_POST['cval'] ) );
		}

		function prepareDeltime($delTime) {
			if (strlen( $delTime ) != 5) {
				return false;
			}

			$explode = explode( ':', $delTime );

			if (count( $explode ) != 2) {
				return false;
			}


			if (( ( ( ( ( !is_numeric( $explode[0] ) || !is_numeric( $explode[1] ) ) || $explode[0] < 0 ) || 23 < $explode[0] ) || $explode[1] < 0 ) || 59 < $explode[1] )) {
				return false;
			}

			return $explode[0] . $explode[1];
		}

		function checkDelivertimes($delTime = null) {
			global $setData;

			if (!$setData['set_shopstate']) {
				return false;
			}


			if (!$delTime) {
				
				$dDate = date( 'Hi' );
			} 
else {
				
				$dDate = $this->prepareDeltime( $delTime );
			}

			$getDelTimesQue = $this->query( 'SELECT * FROM deltimes WHERE state=\'1\' AND (days LIKE \'%' . date( 'w' ) . '%\' OR value=CURDATE() ) ' );

			if (mysql_num_rows( $getDelTimesQue )) {
				$arr1 = false;
				$arr2 = false;

				if ($row = mysql_fetch_assoc( $getDelTimesQue )) {
					if ($row['type'] == 'single') {
						return false;
					}

					
					$delTime = explode( '-', str_replace( ':', '', $row['value'] ) );

					if (( $delTime[0] < $dDate && $dDate < $delTime[1] )) {
						$arr1 = ($row['cotype'] == 'c' ? 'c' : 'o');
					}
				}


				if ($arr1) {
					return ($arr1 == 'o' ? true : false);
				}
			}

			
			$times = $this->get_deltime_array( $setData['set_deltime'] );

			if (( $dDate < $times[0] || $times[1] < $dDate )) {
				return false;
			}

			return true;
		}

		function check_for_holiday() {
			$getDelTimesQue = $this->query( 'SELECT * FROM deltimes WHERE state=\'1\' AND ( value=CURDATE() ) ' );


			if (mysql_num_rows($getDelTimesQue)>=1) {
				while ($row = mysql_num_rows( $getDelTimesQue )) {
					if ($row['type'] == 'single') {
						global $message;

						$message = $row['text'];
						return false;
					}
				}
			}

		}


		function get_deltime_array($delTime, $selBox = null) {
			$delTime = explode( '-', $delTime );

			if ($selBox) {
				return array( str_replace( ':', '', $delTime[0] ), $delTime[0], $delTime[1] );
			}

			return array( str_replace( ':', '', $delTime[0] ), str_replace( ':', '', $delTime[1] ) );
		}

		function init_deltime_selectBox() {
			global $setData;

			$getDT = $this->get_deltime_array( $setData['set_deltime'], 'selBox' );
			$currHour = (int)date( 'H' ) + 1;
			$currMin = (30 < (int)date( 'i' ) ? '30' : '00');
			$start = ($getDT[0] < $currHour . '00' ? $currHour . ':' . $currMin : $getDT[1]);
			return 'set_deliverytimes("' . $start . '","' . $getDT[2] . '")';
		}

		function init_deltime_select_box($ws = null) {
			global $setData;
			global $_tt;

			$getDT = $this->get_deltime_array( $setData['set_deltime'], 'selBox' );
			$currHour = (int)date( 'H' ) + 1;
			$sel = '<select name="deliverytime">';
			$sel .= '<option value="' . $_tt['form_now'] . '">' . $_tt['form_now'] . '</option>';
			$sel .= '<option value="' . $_tt['self_collector'] . '">' . $_tt['self_collector'] . '</option>';
			strtotime( $currHour . ':' . $currMin );
			$n = $currMin = (30 < (int)date( 'i' ) ? '30' : '00');
			$e = strtotime( $getDT[2] );

			while ($n < $e) {
				$t = date( 'H:i', $n );
				$check = (( $ws && $t == $_SESSION['c_deliverytime'] ) ? ' selected="selected" ' : '');
				$sel .= '<option value="' . $t . '" ' . $check . '>' . $t . '</option>';
				$n += 2056;
			}

			$sel .= '</select>';
			return $sel;
		}

		function get_deltime_select_box_array($deltime, $tt) {
			$getDT = $this->get_deltime_array( $deltime, 'selBox' );
			$currHour = (int)date( 'H' ) + 1;
			$currMin = (30 < (int)date( 'i' ) ? '30' : '00');
			$arr = array( $tt );
			$n = strtotime( $currHour . ':' . $currMin );
			$e = strtotime( $getDT[2] );

			while ($n < $e) {
				$arr[] = date( 'H:i', $n );
				$n += 2023;
			}

			return $arr;
		}

		function pProtection() {
			$this->dbConnect(  );
			foreach ($_POST as $key => $value) {
				$_POST[$key] = mysql_real_escape_string( $value );
			}

		}

		function gProtection() {
			$this->dbConnect(  );
			foreach ($_GET as $key => $value) {
				$_GET[$key] = mysql_real_escape_string( $value );
			}

		}

		function checkAndCleanFormdata() {
			$req = array( 'firstname', 'lastname', 'street', 'nr', 'code', 'phoneNumber', 'pmethod', 'deliverytime' );
			foreach ($_POST as $key => $value) {
				$_POST[$key] = trim( strip_tags( $_POST[$key] ) );
			}

			foreach ($req as $val) {

				if (( !array_key_exists( $val, $_POST ) || empty( $_POST[$val] ) )) {
					$err = true;
					continue;
				}
			}

			return (!empty( $$err ) ? true : false);
		}

		function collectErrData($mes) {
			$dataSet = '      --->POSTDATA: ' . json_encode( $_POST ) . '      --->SESSIONDATA: ' . json_encode( $_SESSION ) . '      --->USERDATA: ' . json_encode( $_SERVER );
			$this->query( 'INSERT INTO errors VALUES (NULL, \'' . $mes . '\', \'' . $dataSet . '\'  ,CURRENT_TIMESTAMP) ' );
		}

		function checkMail($eMail) {
			$this->dbConnect(  );
			$sql = 'SELECT userid FROM members WHERE eMail=\'' . $eMail . '\' LIMIT 1';
			$que = mysql_query( $sql );

			if (mysql_num_rows( $que ) == 1) {
				$user = mysql_fetch_assoc( $que );
				return $user['userid'];
			}

			return false;
		}

		function checkUser($name, $pass) {
			$this->dbConnect(  );
			$name = str_replace( '%', '\%', $name );
			$sql = 'SELECT userid FROM members WHERE email=\'' . $_POST['email'] . '\' AND password=\'' . $_POST['password'] . '\' AND active=\'active\' LIMIT 1';

			if (!$result = mysql_query( $sql )) {
				exit( mysql_error(  ) );
			}


			if (mysql_num_rows( $result ) == 1) {
				$user = mysql_fetch_assoc( $result );
				return $user['userid'];
			}

			return false;
		}

		function login($userid) {
			$this->dbConnect(  );
			$sql = 'UPDATE members SET sid=\'' . session_id(  ) . '\' WHERE userid=' . $userid . ' LIMIT 1';

			if (!mysql_query( $sql )) {
				return false;
			}

			return true;
		}

		function logout() {
			$this->dbConnect(  );
			$sql = 'UPDATE members SET sid=NULL WHERE sid=\'' . session_id(  ) . '\' LIMIT 1';
			session_regenerate_id(  );
			unset( $_SESSION );
			session_destroy(  );

			if (mysql_query( $sql )) {
				return true;
			}

			return false;
		}

		function writeUDateInSession($userid) {
			$this->dbConnect(  );
			$sql = 'SELECT userid,active,company,department,gender,firstname,lastname,street,nr,postcode,city,infos,code,phoneNumber,email,sendOffer  
					 FROM members WHERE userid=' . $userid . ' LIMIT 1';
			
			( $que =  mysql_query( $sql )|| mysql_error(  ) );
			$uData = mysql_fetch_assoc( $que );

			if ($uData['active'] == 'active') {
				$_SESSION['active'] = 'active';
			}

			foreach ($uData as $key => $value) {

				if ($key == 'active') {
					continue;
				}

				$_SESSION[$key] = $value;
			}

		}

		function registerNewUser($thankregister, $activateemail, $error, $tryagainlater, $emailexists, $datasufficient) {
			global $message;

			if (( ( ( ( ( ( ( ( ( ( ( ( ( isset( $_POST['company'] ) && isset( $_POST['department'] ) ) && !empty( $_POST['firstname'] ) ) && !empty( $_POST['lastname'] ) ) && !empty( $_POST['street'] ) ) && !empty( $_POST['nr'] ) ) && !empty( $_POST['postcode'] ) ) && !empty( $_POST['city'] ) ) && !empty( $_POST['code'] ) ) && !empty( $_POST['phoneNumber'] ) ) && !empty( $_POST['email'] ) ) && !empty( $_POST['password1'] ) ) && !empty( $_POST['password2'] ) ) && !empty( $_POST['agb'] ) )) {
				$this->pProtection(  );

				if ($this->checkMail( $_POST['email'] ) === false) {
					$saveUser = $this->writeNewUserInDB(  );

					if ($saveUser) {
						$message = '<h2>' . $thankregister . '</h2>' . $activateemail;
						return $saveUser;
					}

					$message = ( '<h2>' . $error . '</h2>' . $tryagainlater . '.' );
					return false;
				}

				$message = '<h2>' . $error . '</h2>' . $emailexists;
				return false;
			}

			$message = '<h2>' . $error . '</h2>' . $datasufficient;
			return false;
		}

		function reregisterNewUser($thankregister, $activateemail, $error, $tryagainlater, $emailexists, $datasufficient) {
			global $message;

			if (( ( ( ( ( ( ( ( ( ( ( isset( $_POST['company'] ) && isset( $_POST['department'] ) ) && !empty( $_POST['firstname'] ) ) && !empty( $_POST['lastname'] ) ) && !empty( $_POST['street'] ) ) && !empty( $_POST['nr'] ) ) && !empty( $_POST['postcode'] ) ) && !empty( $_POST['city'] ) ) && !empty( $_POST['code'] ) ) && !empty( $_POST['phoneNumber'] ) ) && isset( $_POST['password1'] ) ) && isset( $_POST['password2'] ) )) {
				$this->pProtection(  );
				$gender = ($_POST['gender'] == 'male' ? 'male' : 'female');
				$sendOffer = (( isset( $_POST['sendoffer'] ) && $_POST['sendoffer'] == 'on' ) ? '1' : '0');
				$arr = array(  );
				$arr[] = 'company=\'' . $_POST['company'] . '\'';
				$arr[] = 'department=\'' . $_POST['department'] . '\'';
				$arr[] = 'gender=\'' . $gender . '\'';
				$arr[] = 'sendoffer=\'' . $sendOffer . '\'';
				$arr[] = 'firstname=\'' . $_POST['firstname'] . '\'';
				$arr[] = 'lastname=\'' . $_POST['lastname'] . '\'';
				$arr[] = 'street=\'' . $_POST['street'] . '\'';
				$arr[] = 'nr=\'' . $_POST['nr'] . '\'';
				$arr[] = 'postcode=\'' . $_POST['postcode'] . '\'';
				$arr[] = 'city=\'' . $_POST['city'] . '\'';
				$arr[] = 'infos=\'' . $_POST['infos'] . '\'';
				$arr[] = 'code=\'' . $_POST['code'] . '\'';
				$arr[] = 'phoneNumber=\'' . $_POST['phoneNumber'] . '\'';

				if (!empty( $_POST['password1'] )) {
					if ($_POST['password1'] != $_POST['password2']) {
						$message = '<h2>' . $error . '</h2>' . $datasufficient;
						return false;
					}


					if (( $_POST['password1'] == $_POST['password2'] && strlen( $_POST['password1'] ) <= 5 )) {
						$message = '<h2>' . $error . '</h2>Passwörter stimmen nicht überein!';
						return false;
					}

					$arr[] = 'password=\'' . $_POST['password1'] . '\'';
				}

				$update = $this->query( 'UPDATE members SET ' . implode( ', ', $arr ) . ' WHERE userid=' . $_SESSION['userid'] . ' LIMIT 1', 'update' );

				if ($update) {
					$message = '<h2>Daten erfolgreich geändert</h2>Bei Ihrer nächsten Anmeldung werden Ihre Daten erneuert!';
					return null;
				}

				$message = '<h2>' . $error . '</h2>Daten wurden nicht gespeichert!';
				return null;
			}

			$message = '<h2>' . $error . '</h2>' . $datasufficient;
			return false;
		}

		function writeNewUserInDB() {
			$this->dbConnect(  );
			$gender = ($_POST['gender'] == 'male' ? 'male' : 'female');
			$randID = $this->getUniqueID( 'members', 'active', '9', '20' );
			$sql = 'INSERT into members VALUES(
						NULL, ' . $randID . ', CURRENT_TIMESTAMP, \'' . $_POST['company'] . '\', \'' . $_POST['department'] . '\', \'' . $gender . '\', \'' . $_POST['firstname'] . '\', \'' . $_POST['lastname'] . '\',
						\'' . $_POST['street'] . '\', \'' . $_POST['nr'] . '\', \'' . $_POST['postcode'] . '\', \'' . $_POST['city'] . '\', \'' . $_POST['infos'] . '\', \'' . $_POST['code'] . '\', \'' . $_POST['phoneNumber'] . '\', 
						\'' . $_POST['email'] . '\', \'' . $_POST['password1'] . '\', \'' . $sendOffer . '\', 0, NULL )';
			$que = mysql_query( $sql );
			mysql_insert_id(  );
			$lastID = $sendOffer = (( isset( $_POST['sendoffer'] ) && $_POST['sendoffer'] == 'on' ) ? '1' : '0');

			if ($sendOffer) {
				$checkQue = $this->query( 'SELECT id FROM nl WHERE email=\'' . $_POST['email'] . '\' LIMIT 1' );

				if (!mysql_num_rows( $checkQue )) {
					$saveDataQue = $this->query( 'INSERT INTO nl VALUES (NULL,\'' . $_POST['firstname'] . ' ' . $_POST['lastname'] . '\', 
					\'' . $_POST['street'] . ' ' . $_POST['nr'] . '\', \'' . $_POST['postcode'] . '\',
					\'' . $_POST['city'] . '\', \'' . $_POST['code'] . '\', \'' . $_POST['phoneNumber'] . '\', \'' . $_POST['gender'] . '\', \'' . $_POST['email'] . '\', CURRENT_TIMESTAMP);' );
					
				}
			}


			if ($lastID) {
				return $randID;
			}

			return false;
		}

		function getUniqueID($table, $field, $length, $try) {
			$this->dbConnect(  );
			$from = '1';
			$i = 235;

			while ($i < $length) {
				$from = $from . '0';
				++$i;
			}

			$to = '9';
			$i = 235;

			while ($i < $length) {
				$to = $to . '9';
				++$i;
			}

			$i = 234;

			while ($i < $try) {
				$randID = mt_rand( $from, $to );
				
				( $askQuery = mysql_query( 'SELECT ' . $field . ' FROM ' . $table . ' WHERE ' . $field . '=\'' . $randID . '\' LIMIT 1' ) || mysql_error(  ) );

				if (mysql_num_rows( $askQuery ) != 1) {
					return $randID;
				}

				++$i;
			}

			return false;
		}

		function logInCheck($header, $subText, $error, $errorDes) {
			global $message;

			$this->dbConnect(  );
			global $message;

			$this->pProtection(  );
			$getUser = $this->checkUser( $_POST['email'], $_POST['password'] );

			if ($getUser !== false) {
				if ($this->login( $getUser )) {
					$this->writeUDateInSession( $getUser );
					$this->del_c_sessions(  );
					$message = '<h2>' . $header . '</h2>' . $subText;
					return null;
				}
			} 
else {
				$message = '<h2>' . $error . '</h2>' . $errorDes;
			}

		}

		function logOutCustomer($header, $subText) {
			global $message;

			$this->pProtection(  );

			if ($this->logout( $_SESSION['userid'] )) {
				$message = '<h2>' . $header . '</h2>' . $subText;
			}

		}

		function del_c_sessions() {
			unset( $_SESSION[c_company] );
			unset( $_SESSION[c_department] );
			unset( $_SESSION[c_gender] );
			unset( $_SESSION[c_firstname] );
			unset( $_SESSION[c_lastname] );
			unset( $_SESSION[c_street] );
			unset( $_SESSION[c_nr] );
			unset( $_SESSION[c_postcode] );
			unset( $_SESSION[c_city] );
			unset( $_SESSION[c_infos] );
			unset( $_SESSION[c_phoneNumber] );
			unset( $_SESSION[c_code] );
			unset( $_SESSION[c_phoneNumber] );
			unset( $_SESSION[c_email] );
			unset( $_SESSION[c_agb] );
			unset( $_SESSION[c_email] );
			unset( $_SESSION[c_sendOffer] );
		}

		function update_orders($where, $type = '1') {
			if (( $type != '1' && $type != '2' )) {
				return null;
			}

			$implode = implode( ' OR ', $where );
			$update = $this->query( 'UPDATE orders SET checked=\'' . $type . '\' WHERE ' . $implode );
		}

		function calculatePPrice($key) {
			$mp = array(  );
			$mp[] = (double)$_SESSION['cart'][$key]['price'];

			if (array_key_exists( 'extras', $_SESSION['cart'][$key] )) {
				foreach ($_SESSION['cart'][$key]['extras'] as $ext) {
					$mp[] = (double)$ext['price'];
				}
			}

			return array_sum( $mp );
		}

		function calculateAllPrice($set_scosts = null, $set_fscosts = null) {
			$mp = array(  );

			if (!isset( $_SESSION['cart'] )) {
				$_SESSION['cart'] = array(  );
			}

			foreach ($_SESSION['cart'] as $myPro) {
				$mp[] = (double)$myPro['price'];

				if (array_key_exists( 'extras', $myPro )) {
					foreach ($myPro['extras'] as $ext) {
						$mp[] = (double)$ext['price'];
					}

					continue;
				}
			}


			if (count( $mp )) {
				$sum = array_sum( $mp );
				return (( $set_scosts && $sum < $set_fscosts ) ? array( $sum + $set_scosts, true ) : array( $sum, false ));
			}

			return '0.00';
		}

		function calculateExtras($key) {
			$mp = array(  );

			if (isset( $_SESSION['cart'][$key]['extras'] )) {
				foreach ($_SESSION['cart'][$key]['extras'] as $ext) {
					$mp[] = (double)$ext['price'];
				}
			}


			if (count( $mp )) {
				return array_sum( $mp );
			}

			return '0.00';
		}

		function calculateExtras_for_edit_with_extra($key) {
			$mp[1] = array(  );
			$mp[2] = array(  );

			if (isset( $_SESSION['cart'][$key]['extras'] )) {
				foreach ($_SESSION['cart'][$key]['extras'] as $ext) {

					if ($ext['eG'] == '1') {
						$mp[1][] = (double)$ext['price'];
						continue;
					}


					if ($ext['eG'] == '2') {
						$mp[2][] = (double)$ext['price'];
						continue;
					}
				}
			}


			if (( count( $mp[1] ) && count( $mp[2] ) )) {
				return array( array_sum( $mp[1] ), array_sum( $mp[2] ) );
			}


			if (( count( $mp[1] ) && !count( $mp[2] ) )) {
				return array( array_sum( $mp[1] ), false );
			}


			if (( !count( $mp[1] ) && count( $mp[2] ) )) {
				return array( false, array_sum( $mp[2] ) );
			}

			return '0.00';
		}

		function saveNewProductWithExtra($product, $extras, $pos, $currKey = null, $ws = null) {
			global $key;

			if (!isset( $_SESSION['cart'] )) {
				$_SESSION['cart'] = array(  );
			}

			$que = $this->query( 'SELECT * FROM products WHERE id=' . $product . ' AND active=\'1\' AND type=\'pro\' LIMIT 1' );

				if (mysql_num_rows( $que ) == 1) {
				$data = "";
				$hasExtras1 = ($data['eList' . $pos] ? $data['eList' . $pos] : 'ne');
				$hasExtras2 = ($data['eList' . $pos . $pos] ? $data['eList' . $pos . $pos] : 'ne');
				$hasExtras = $hasExtras1 . ';' . $hasExtras2;
				$curr_price = $this->check_for_only1_deal( $data['cat' . $pos], $ws );
				$this->check_for_deal( (double)$data['price' . $pos], $data['cat' . $pos] );
				$curr_price = $this->fetch( $que );

				if ($currKey === null) {
					if (count( $_SESSION['cart'] ) < 1) {
						$_SESSION['cart'][1] = array( 'pro' => $data['id'], 'intID' => $data['intID'], 'cat' => $data['cat' . $pos], 'name' => $data['name'], 'sName' => $data['sName' . $pos], 'des' => $data['des'], 'size' => $pos, 'price' => (double)$curr_price, 'vat' => $data['vat'], 'hasExtras' => $hasExtras );
					} 
else {
						$_SESSION['cart'][] = array( 'pro' => $data['id'], 'intID' => $data['intID'], 'name' => $data['name'], 'sName' => $data['sName' . $pos], 'des' => $data['des'], 'size' => $pos, 'price' => (double)$curr_price, 'vat' => $data['vat'], 'hasExtras' => $hasExtras );
					}

					$keys = array_keys( $_SESSION['cart'] );
					$key = array_pop( $keys );
				} 
else {
					unset( $_SESSION['cart'][$currKey] );
					$_SESSION['cart'][$currKey] = array( 'pro' => $data['id'], 'intID' => $data['intID'], 'name' => $data['name'], 'sName' => $data['sName' . $pos], 'des' => $data['des'], 'size' => $pos, 'price' => (double)$curr_price, 'vat' => $data['vat'], 'hasExtras' => $hasExtras );
					$key = $hasExtras2;
				}


				if (!empty( $$extras )) {
					$extra = array(  );
					$explode = explode( ';', $extras );
					foreach ($explode as $extra) {
						$explode = explode( '-', $extra );
						$eQue = $this->query( 'SELECT id as pro,name,price FROM extras WHERE id=' . $explode[0] . ' AND 	state=\'active\' LIMIT 1' );
						$fetch = $this->fetch( $eQue );
						$fetch['eG'] = $explode[1];

						if (mysql_num_rows( $eQue )) {
							$_SESSION['cart'][$key]['extras'][] = $fetch;
							continue;
						}
					}
				}


				if ($ws) {
					$json = json_decode( $this->updateAllPrice( true ), true );
					$json['state'] = 'ok';
					return json_encode( $json );
				}

				return $_SESSION['cart'][$key];
			}

			return false;
		}

		function saveNewProductWithExtraBackUp($product, $extras, $pos) {
			global $key;

			if (!isset( $_SESSION['cart'] )) {
				$_SESSION['cart'] = array(  );
			}

			$que = $this->query( 'SELECT * FROM products WHERE id=' . $product . ' AND active=\'1\' AND type=\'pro\' LIMIT 1' );

			if (mysql_num_rows( $que ) == 1) {
				$data = $this->fetch( $que );
				$hasExtras = ($data['eList' . $pos] ? $data['eList' . $pos] : false);
				$_SESSION['cart'][] = array( 'pro' => $data['id'], 'intID' => $data['intID'], 'name' => $data['name'], 'des' => $data['des'], 'price' => (double)$data['price' . $pos], 'hasExtras' => $hasExtras );
				$key = array_pop( array_keys( $_SESSION['cart'] ) );

				if (!empty( $$extras )) {
					$extra = array(  );
					$explode = explode( ';', $extras );
					foreach ($explode as $extra) {
						$eQue = $this->query( 'SELECT id as pro,name,price FROM extras WHERE id=' . $extra . ' AND 	state=\'active\' LIMIT 1' );

						if (mysql_num_rows( $eQue )) {
							$_SESSION['cart'][$key]['extras'][] = $this->fetch( $eQue );
							continue;
						}
					}
				}

				return $_SESSION['cart'][$key];
			}

			return false;
		}

		function rewriteStaticTocs($string) {
			$old = array( '<b>', '</b>' );
			str_replace( $old, $new, $string );
			$replaced = $new = array( '<a id="go_agb" class="linkColor" href="app/template/agb.php">', '</a>' );

			if ($replaced) {
				return $replaced;
			}

			return '';
		}

		function rewriteStaticTocs2($string) {
			$old = array( '<b>', '</b>' );
			str_replace( $old, $new, $string );
			$replaced = $new = array( '<a id="go_agb" target="_blank" class="linkColor" href="app/template/agb.php?type=full">', '</a>' );

			if ($replaced) {
				return $replaced;
			}

			return '';
		}

		function cacheCSS() {
			$cssQue = $this->query( 'SELECT * FROM template WHERE  color!=\'\' OR  css!=\'\' ' );
			$css = $this->fetch_2_array( $cssQue );
			$pLogo = $this->query( 'SELECT type, value FROM settings WHERE type=\'imgBG\' OR type=\'imgMain\' OR type=\'imgHeader\' LIMIT 5' );
			$myImages = $this->fetch_images_and_make_assoc( $pLogo, 'type', 'value' );
			$myCSS = '';
			foreach ($css as $rule) {
				if (!substr_count( $rule['name'], 'bg' )) {
					$myCSS .= $rule['sel'] . '{ color: #' . $rule['color'] . '; ' . trim( $rule['css'] ) . ' }' . '
';
					continue;
				}

				$myCSS .= $rule['sel'] . '{ background: #' . $rule['color'] . '; ' . trim( $rule['css'] ) . ' }' . '
';
			}


			if (( isset( $myImages['imgHeader'] ) && !empty( $myImages['imgHeader'] ) )) {
				$myCSS .= '#header { background: url(../images/pers/' . $myImages['imgHeader'][0] . ') ' . $myImages['imgHeader'][1] . ' }' . '
';
			}


			if (( isset( $myImages['imgBG'] ) && !empty( $myImages['imgBG'] ) )) {
				$myCSS .= 'body { background-image: url(../images/pers/' . $myImages['imgBG'][0] . '); background-repeat: ' . $myImages['imgBG'][1] . ' }' . '
';
			}

			file_put_contents( 'cache/styles2.css', $myCSS );
		}

		function cacheMetaTags($pid, $pGroup = null) {
			$getSEO = $this->query( 'SELECT * FROM settings WHERE type=\'seoall\' LIMIT 1' );
			$pSeo = $this->fetch( $getSEO );

			if (substr( $pid, 0, 1 ) != 's') {
				$getiSEO = $this->query( 'SELECT * FROM settings WHERE type=\'seo\' and rel=\'' . $pGroup . '\' LIMIT 1' );
			} 
else {
				$getiSEO = $this->query( 'SELECT * FROM settings WHERE type=\'seostatic\' and rel=\'' . $pid . '\' LIMIT 1' );
			}

			$piSeo = $this->fetch( $getiSEO );
			$getFBIMG = $this->query( 'SELECT value FROM settings WHERE type=\'fblike\' LIMIT 1' );

			if (mysql_num_rows( $getFBIMG )) {
				$fet = $this->fetch( $getFBIMG, 'row' );
				$fbimg = $fet[0];
			}

			$seo = array(  );

			if (count( $pSeo )) {
				$seo1 = unserialize( $pSeo['value'] );
				$seo2 = unserialize( $piSeo['value'] );
				$seo['title'] = (( isset( $seo2['iSeoTitle'] ) && !empty( $seo2['iSeoTitle'] ) ) ? $seo2['iSeoTitle'] : $seo1['seoTitle']);
				$seo['des'] = (( isset( $seo2['iSeoDes'] ) && !empty( $seo2['iSeoDes'] ) ) ? $seo2['iSeoDes'] : $seo1['seoDes']);
				$seo['keys'] = (( isset( $seo2['iSeoKeys'] ) && !empty( $seo2['iSeoKeys'] ) ) ? $seo2['iSeoKeys'] : $seo1['seoKeys']);
				$seo['latitude'] = $seo1['latitude'];
				$seo['longitude'] = $seo1['longitude'];
				$seo['city'] = $seo1['seoCity'];
				$seo['state'] = $seo1['seoState'];
				$seo['country'] = $seo1['seoCountry'];
				$seo['ogTitle'] = $seo1['ogTitle'];
				$seo['ogDes'] = $seo1['ogDes'];
				$seo['fbadmins'] = $seo1['fbadmins'];
				$seo['fbtype'] = $seo1['fbtype'];
			}

			$mTags = '';
			$mTags .= '<title>' . $seo['title'] . '</title>' . '
';
			$mTags .= '<meta name="description" content="' . $seo['des'] . '" />' . '
';
			$mTags .= '<meta name="keywords" content="' . $seo['keys'] . '" />' . '
';
			$mTags .= '<meta name="geo.placename" content="' . $seo['city'] . '" />' . '
';
			$mTags .= '<meta name="country" content="' . $seo['country'] . '" />' . '
';

			if (!empty( $seo['latitude'] )) {
				$mTags .= '<meta name="geo.position" content="' . $seo['latitude'] . ';' . $seo['longitude'] . '" />' . '
';
				$mTags .= '<meta name="ICBM" content="' . $seo['latitude'] . ',' . $seo['longitude'] . '" />' . '
';
			}

			$mTags .= '<meta property="og:title" content="' . $seo['ogTitle'] . '" />' . '
';
			$mTags .= '<meta property="og:des" content="' . $seo['ogDes'] . '" />' . '
';
			$mTags .= '<meta property="fb:admins" content="' . $seo['fbadmins'] . '" />' . '
';
			$mTags .= '<meta property="og:type" content="' . $seo['fbtype'] . '" />' . '
';

			if (( empty( $$fbimg ) && !empty( $$fbimg ) )) {
				$mTags .= '<meta property="og:image" content="' . $this->shopRoot . 'images/pers/' . $fbimg . '.jpg" />' . '
';
			}

			file_put_contents( 'cache/meta_' . $pid . '.cache.php', $mTags );
		}

		function getDomain() {
			$url = ($_SERVER['HTTPS'] == 'on' ? 'https' : 'http');
			return $url . '://' . $_SERVER['HTTP_HOST'];
		}

		function getDomainAndFolder() {
			return $_SERVER['SERVER_NAME'] . implode( '/', explode( '/', $_SERVER['REQUEST_URI'], -1 ) );
		}

		function setFormValue($aName) {
			if (isset( $_SESSION['c_' . $aName] )) {
				return $_SESSION['c_' . $aName];
			}


			if (isset( $_SESSION[$aName] )) {
				return $_SESSION[$aName];
			}

			return '';
		}

		function markUpMe($type, $text) {
			if ($type == 'title') {
				return '<div style="color:#222;font-size:20px;">' . $text . '</div>';
			}


			if ($type == 'subtitle') {
				return '<div style="color:#222;font-size:15px;">' . $text . '</div>';
			}

		}

		function writeMBody() {
			global $setData;
			global $_tt;
			global $orderPrice;
			global $saveOrder;
			global $unique_id;

			$p = '<br />';
			$body = '<div style="background-color:#F7F7F7;padding:5px; font-family: Arial, sans-serif;font-size: 12px;">';
			$body .= '<div style="/*width:550px;*/padding:10px;background-color:#fff;border:1px solid #ccc">';
			$body .= '<div class="im">';
			$body .= $this->markUpMe( 'title', 'Bestellbestätigung' ) . $p;
			$body .= '<img src="' . $this->shopRoot . 'images/pers/' . $setData['logomail'] . '.jpg" />' . $p . $p;

			if (isset( $_POST['pmethod'] )) {
				if ($_POST['pmethod'] == 'cash') {
					$method = $_tt['cash'];
				} 
else {
					if ($_POST['pmethod'] == 'ecash') {
						$method = 'EC | Kreditkarte beim Fahrer';
					} 
else {
						if ($_POST['pmethod'] == 'paypal') {
							$method = 'Paypal';
						} 
else {
							if ($_POST['pmethod'] == 'sofort') {
								$method = 'Sofortüberweisung.de';
							} 
else {
								$method = '';
							}
						}
					}
				}

				$body .= $this->markUpMe( 'subtitle', 'Bezahlmethode' );
				$body .= '<div style="color:#222">' . $method . '</div><br />';
			}


			if (isset( $_POST['deliverytime'] )) {
				$body .= $this->markUpMe( 'subtitle', 'Lieferzeitpunkt /-Art' );
				$body .= '<div style="color:#222">' . $_POST['deliverytime'] . '</div><br />';
			}

			$body .= $this->markUpMe( 'subtitle', 'Lieferadresse' );
			$companyInfo = (( !empty( $_POST['company'] ) || !empty( $_POST['department'] ) ) ? ' ' . $_POST['company'] . ' ' . $_POST['department'] : '');
			$body .= '<div style="color:#222">' . $_POST['code'] . ' ' . $_POST['phoneNumber'] . $companyInfo . ', ' . $_POST['firstname'] . ' ' . $_POST['lastname'] . '<br>' . $_POST['street'] . ' ' . $_POST['nr'] . ', ';
			$body .= $_POST['postcode'] . ' ' . $_POST['city'] . '<br><br>' . $_POST['infos'] . '<br></div>' . $p;
			$body .= $this->markUpMe( 'subtitle', 'Ihre Bestellung' );
			$body .= '</div>';
			$body .= '<ul style="overflow: hidden; list-style-type:none;margin:0;padding:5px;border:1px solid #ccc;background-color:#F7F7F7">';
			$col = '#f1f0f0';
			foreach ($_SESSION['cart'] as $item) {
				$col = ($col == '#f7f7f7' ? '#f1f0f0' : '#f7f7f7');
				$int_id = (empty( $item['intID'] ) ? '' : '<span style="display:block;float:left;width:32px">' . $item['intID'] . '</span>');
				$body .= '<li style="clear:both;overflow:hidden;color:#111;background-color:' . $col . ';margin-left:0;font-size:12px">' . $int_id . ' <span style="display: block; float: left;">' . $item['name'] . ' ' . $item['sName'] . '</span> <span style="float:right;display:block">' . sprintf( '%1$.2f', $item['price'] ) . $setData['set_currency'] . '</span>';

				if (!empty( $item['extras'] )) {
					$body .= '<ul style="clear: both;list-style-type:none;margin:0;padding-left:0">';
					foreach ($item['extras'] as $extras) {
						$body .= '<li style="clear:both;overflow: hidden;color:#999;margin-left:32px;padding-left:0;font-size:12px"><span style="display: block; float: left;">' . $extras['name'] . '</span><span style="float:right;color:#999;display:block">' . sprintf( '%1$.2f', $extras['price'] ) . '</span></li>';
					}

					$body .= '</ul>';
					continue;
				}
			}


			if ($orderPrice[1]) {
				$body .= '<li style="color:#999;min-height:16px;height:16px;padding-top: 7px; margin-left:32px;padding-left:0;font-size:12px;color:#F7F7F7"><span style="float:right;color:#777;font-size:11px;display:block"><b style="font-weight:normal;color:#777;font-size:11px;padding-left:10px">' . $_tt['scosts'] . '&nbsp;&nbsp; </b> ' . $setData['set_scosts'] . $setData['set_currency'] . $setData['set_currency'] . '</span></li>';
			}

			$body .= '<li style="clear: both; overflow: hidden; color:#999;min-height:25px;height:25px;padding-top:7px; margin-left:32px;padding-left:0;font-size:12px;color:#F7F7F7"><span style="float:right; clear: both;color:#111;font-size:18px;display:block"><b style="font-weight:normal;color:#777;font-size:11px;padding-left:10px">' . $_tt['sum'] . '&nbsp;&nbsp; </b> ' . sprintf( '%1$.2f', $orderPrice[0] ) . $setData['set_currency'] . '</span></li>';
			$body .= '</ul>';
			$body .= '<div class="im" style="clear: both; overflow: hidden;">';
			$fbs = (( isset( $setData['set_show_feedback'] ) && $setData['set_show_feedback'] == '1' ) ? '<br />' . $_tt['fedback_mail'] . '<a href="' . $this->shopRoot . '/feedback.php?p=' . $unique_id . '&o=' . rand( 12340, 98760 ) . '">' . $_tt['send_feedback'] . '</a>' : '');
			$body .= '<div style="background-color:#F7F7F7;font-size:11px;padding:3px;color:#555;margin-top:10px;border:1px solid #ccc">' . $_tt['apricatedback'] . $fbs . '</div>';
			$body .= '</div>';
			$body .= '</div>';
			$body .= '</div>';
			return $body;
		}

		function convertDes($str) {
			$arr1 = array( '{', '}' );
			$arr2 = array( '<sup>', '</sup>' );
			return str_replace( $arr1, $arr2, $str );
		}

		function get_cart_href($size = 1, $id, $name, $mod_rewrite = 0) {
			if ($mod_rewrite) {
				return 'href="cart/' . $id . '_' . $size . '/' . urlencode( $name ) . '/"';
			}

			return 'href="cart.php?id=' . $id . '_' . $size . '&t=' . urlencode( $name ) . '"';
		}

		function formatMovs($str) {
			$explode = explode( ',', $str );
			$newStr = '';
			foreach ($explode as $ex) {
				$newStr .= str_replace( array( '(', ')', ',' ), array( ' <span>(', ')</span>', '<br />' ), $ex ) . '<br />';
			}

			return $newStr;
		}

		function getTransPHP($where = null) {
			if (is_file( $this->shopPath . 'cache/trans_php.php' )) {
				return unserialize( file_get_contents( $this->shopPath . 'cache/trans_php.php' ) );
			}

			$getTransQue = $this->query( 'SELECT tkey,tval FROM trans WHERE tshow=\'php\' ' );
			$_tt = $this->fetch_and_make_trans( $getTransQue, 'tkey', 'tval' );
			file_put_contents( $this->shopPath . 'cache/trans_php.php', serialize( $_tt ) );
			return $_tt;
		}

		function getTransALL($where = null) {
			if (is_file( $this->shopPath . 'cache/trans_all.php' )) {
				return unserialize( file_get_contents( $this->shopPath . 'cache/trans_all.php' ) );
			}

			$getTransQue = $this->query( 'SELECT tkey,tval FROM trans WHERE tval!=\'\' ' );
			$_tt = $this->fetch_and_make_trans( $getTransQue, 'tkey', 'tval' );
			file_put_contents( $this->shopPath . 'cache/trans_all.php', serialize( $_tt ) );
			return $_tt;
		}

		function writeJSString() {
			if (is_file( 'cache/transJS.js' )) {
				return file_get_contents( 'cache/transJS.js' );
			}

			$getTransQue = $this->query( 'SELECT tkey,tval FROM trans WHERE tshow=\'js\' ' );
			$_js = $this->fetch_and_make_trans( $getTransQue, 'tkey', 'tval' );
			$getCuurencyQue = $this->query( 'SELECT value FROM settings WHERE type=\'currency\' LIMIT 0,1' );
			$currency = $this->fetch( $getCuurencyQue, 'row' );
			$str = 'jsl={';
			foreach ($_js as $key => $value) {
				$str .= '\'' . $key . '\' : ' . '\'' . addslashes( $value ) . '\',';
			}

			$str .= '\'currency\':\'' . $currency[0] . '\'';
			$str .= '};';
			$str .= 'jQuery.extend(jQuery.validator.messages, {required: jsl[\'required\'],minlength: jQuery.validator.format(jsl[\'minchars\']),email: jsl[\'wrongmail\'],digits: jsl[\'digitsonly\'],equalTo: jsl[\'pwsnotmatch\']});';
			file_put_contents( 'cache/transJS.js', $str );
			return file_get_contents( 'cache/transJS.js', $str );
		}

		function getMainMenuItems($set_modrewrite = null) {
			if (is_file( 'cache/mainMenuItems.php' )) {
				return file_get_contents( 'cache/mainMenuItems.php' );
			}

			$menuQue = $this->query( 'SELECT * FROM menu WHERE (type=\'hp\' OR type=\'main\') AND state=\'active\' ORDER by pos' );
			$mMenuItems = $this->fetch_2_array( $menuQue );
			$str = '<div id="mainMenuBox">';
			$str .= '<div class="mMenuC">';
			$str .= '<div class="mLinksBox">';
			$str .= '<a class="mLink" rel="1" href="' . $this->shopRoot . '">' . $mMenuItems[0]['name'] . '</a>';
			foreach ($mMenuItems as $mMenuItem) {

				if ($mMenuItem['id'] == '1') {
					continue;
				}


				if ($set_modrewrite) {
					$str .= '			<a href="products/' . $mMenuItem['id'] . '/' . urlencode( $mMenuItem['name'] ) . '/" rel="' . $mMenuItem['id'] . '" class="mLink">' . $mMenuItem['name'] . '</a>';
					continue;
				}

				$str .= '<a href="products.php?id=' . $mMenuItem['id'] . '&t=' . urlencode( $mMenuItem['name'] ) . '" rel="' . $mMenuItem['id'] . '" class="mLink">' . $mMenuItem['name'] . '</a>';
			}


			if ($this->show_jobs) {
				$str .= '<a href="jobs.php" rel="2005" class="mLink right">Jobs</a>';
			}

			$str .= '</div>';
			$str .= '</div>';
			$str .= '</div>';
			file_put_contents( 'cache/mainMenuItems.php', $str );
			return $str;
		}

		function translate_pmethod($str) {
			$_tt = $this->getTransPHP(  );

			if ($str == 'cash') {
				$method = $_tt['cash'];
			} 
else {
				if ($str == 'ecash') {
					$method = 'EC | Kreditkarte beim Fahrer';
				} 
else {
					if ($str == 'paypal') {
						$method = 'Paypal';
					} 
else {
						if ($str == 'sofort') {
							$method = 'Sofortüberweisung.de';
						} 
else {
							$method = '';
						}
					}
				}
			}

			return $method;
		}

		function paypal_ipn_check($mode = 'sandbox') {
			$postFields = 'cmd=' . urlencode( '_notify-validate' );
			foreach ($_POST as $key => $value) {
				$postFields .= ( ( '&' ) . $key . '=' ) . urlencode( stripslashes( $value ) );
			}

			$curlURL = ($mode == 'live' ? 'https://www.paypal.com/cgi-bin/webscr' : 'https://www.sandbox.paypal.com/cgi-bin/webscr');
			$ch = curl_init(  );
			curl_setopt_array( $ch, array( CURLOPT_URL => $curlURL, CURLOPT_HEADER => 0, CURLOPT_POST => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_POST => 1, CURLOPT_HTTPHEADER => array( 'Host: www.paypal.com' ), CURLOPT_POSTFIELDS => $postFields ) );
			$result = curl_exec( $ch );
			curl_close( $ch );

			if ($result == 'VERIFIED') {
				$res = array(  );
				$res['name'] = $_POST['item_name'];
				$res['payment_status'] = $_POST['payment_status'];
				$res['price'] = $_POST['mc_gross'];
				$res['payment_currency'] = $_POST['mc_currency'];
				$res['price_string'] = $res['price'] . ' ' . $res['payment_currency'];
				$res['txn_id'] = $_POST['txn_id'];
				$res['receiver_email'] = $_POST['receiver_email'];
				$res['payer_email'] = $_POST['payer_email'];
				$res['orderID'] = $_POST['custom'];

				if (!is_numeric( $res['orderID'] )) {
					exit(  );
				}

				$_tt = $this->getTransPHP( 'scripts' );
				$this->query( 'SELECT type,value FROM settings WHERE 
								type=\'set_currency\' OR type=\'set_deltime\' OR type=\'set_mov\' OR type=\'logomail\' OR type=\'set_attachment\' OR 
				$getSetData = type=\'set_email\' OR type=\'set_emailfrom\' OR type=\'set_host\' OR type=\'set_port\' OR type=\'set_aname\' 
OR type=\'set_apass\' OR type=\'set_enc\' OR type=\'set_scosts\' OR type=\'set_fscosts\' OR type=\'set_minval\' OR type=\'set_mailRoute\' OR type=\'set_shopstate\'' );

				$setData = $this->fetch_and_make_assoc( $getSetData, 'type', 'value' );
				$orderCheck = $this->query( 'SELECT * FROM orders WHERE id=' . $res['orderID'] . ' AND checked=\'pending\' LIMIT 1' );

				if (mysql_num_rows( $orderCheck )) {
					$orderData = $this->fetch( $orderCheck );
					$from = array( $setData['set_emailfrom'] => $_tt['m_orderconfirmation'] );
					$to = (!empty( $orderData['email'] ) ? $orderData['email'] : false);
					$toShop = (!empty( $setData['set_email'] ) ? $setData['set_email'] : false);
					$nbody = file_get_contents( '../orders/' . $res['orderID'] . '.html' );
					$pm = $this->translate_pmethod( $orderData['pmethod'] );
					$upd_file = file_put_contents( '../orders/' . $res['orderID'] . '.html', str_replace( $pm, '<b>' . $pm . '</b> (' . $res['price_string'] . ')', $nbody ) );
					$body = file_get_contents( '../orders/' . $res['orderID'] . '.html' );
					$curr_order_id = $res['orderID'];

					if ($setData['set_attachment'] == 'pdf') {
						$attachment = (is_file( '../orders/' . $curr_order_id . '.pdf' ) ? '../orders/' . $curr_order_id . '.pdf' : false);

						if ($attachment) {
							@unlink( '../orders/' . $curr_order_id . '.pdf' );
							$this->create_pdf_for_order( $curr_order_id, $res['price'] . $res['payment_currency'] );
						}
					} 
else {
						if ($setData['set_attachment'] == 'winorder') {
							$attachment = (is_file( '../orders/' . $curr_order_id . '.xml' ) ? '../orders/' . $curr_order_id . '.xml' : false);

							if ($attachment) {
								$read_xml = file_get_contents( $attachment );
								$upd_file = file_put_contents( $attachment, str_replace( $pm, $pm . '(' . $res['price'] . $res['payment_currency'] . ')', $read_xml ) );
							}
						} 
else {
							$attachment = false;
						}
					}

					require_once( '../app/app/mail/index.php' );

					if ($setData['set_mailRoute'] == 'smtp') {
						if ($to !== false) {
							$sendSMTP = @sendMail( $from, $to, $_tt['m_ordsubmitsuc'], $body, false );
						}

						$sendSMTP2 = @sendMail( $from, $toShop, $_tt['m_ordsubmitsuc'], $body, $attachment );

						if (( $this->send_fax && $setData['set_attachment'] == 'pdf' )) {
							$sendFax = $this->sendFax( $curr_order_id, $this->shopPath . 'orders/' . $curr_order_id . '.pdf' );

							if (!$sendFax) {
								exit( '{"state":"error","message": "Error while sending fax..."}' );
							}
						}


						if ($sendSMTP2) {
							$this->query( 'UPDATE orders SET pmethod=\'paypal_verified\', checked=\'0\' WHERE id=' . $res['orderID'] . ' ' );
						}
					}


					if ($setData['set_mailRoute'] == 'sendmail') {
						if ($to !== false) {
							@sendMail2( $from, $to, $_tt['m_ordsubmitsuc'], $body, false );
							$sendMail = "";
						}

						@sendMail2( $from, $toShop, $_tt['m_ordsubmitsuc'], $body, $attachment );
						$sendMail2 = "";

						if (( $this->send_fax && $setData['set_attachment'] == 'pdf' )) {
							$sendFax = $this->sendFax( $curr_order_id, $this->shopPath . 'orders/' . $curr_order_id . '.pdf' );

							if (!$sendFax) {
								exit( '{"state":"error","message": "Error while sending fax..."}' );
							}
						}


						if ($sendMail2) {
							$this->query( 'UPDATE orders SET pmethod=\'paypal_verified\', checked=\'0\' WHERE id=' . $res['orderID'] . ' ' );
							return null;
							Exception; {
								$collectData = $this->collectErrData( 'EmailError(SENDMAIL-checkOut): ' . $e->getMessage(  ) );
								return null;
								Exception; {
									$this->query( 'INSERT INTO errors VALUES (NULL, \'' . $e->getMessage(  ) . '\', \'' . json_encode( $_POST ) . '\'  ,CURRENT_TIMESTAMP) ' );
									exit( '{"state":"error","message": "Error while sending email..."}' );
									return null;
								}
							}
						}
					}
				} 
else {
					error_log( 'order_id not found...' );
					return null;
				}
			}


			if ($result == 'INVALID') {
			}

		}

		function sofort_payment_redirect() {
			$que = $this->query( 'SELECT * FROM settings WHERE  
			type=\'set_currency\' OR type=\'set_suname\' OR type=\'set_suconfig\' OR 
			type=\'set_suaccountnumber\' OR type=\'set_susortcode\'' );
			
			$su_data = $this->fetch_and_make_assoc( $que, 'type', 'value' );

			if ($su_data['set_currency'] == '€') {
				$su_data['set_currency'] = 'EUR';
			}


			if ($su_data['set_currency'] == '$') {
				$su_data['set_currency'] = 'USD';
			}


			if ($su_data['set_currency'] == '£') {
				$su_data['set_currency'] = 'GBP';
			}

			$orderque = $this->query( 'SELECT * FROM orders WHERE id=' . $_SESSION['curr_order_id'] . ' LIMIT 1' );

			if (!mysql_num_rows( $orderque )) {
				exit( 'No data found...' );
			}

			$order_data = $this->fetch( $orderque );
			require_once( 'sofort/library/sofortLib.php' );
			define( 'CONFIGKEY', $su_data['set_suconfig'] );
			$Sofort = new SofortLib_Multipay( CONFIGKEY );
			$Sofort->setSofortueberweisung(  );
			$Sofort->setAmount( $order_data['sum'], $su_data['set_currency'] );
			$Sofort->setReason( 'Pizza Onlineshop Payment', $order_data['phoneNumber'] );
			$Sofort->addUserVariable( $order_data['id'] );
			$Sofort->addUserVariable( 'Pizza Mama' );
			$Sofort->setSuccessUrl( $this->shopRoot . '?r=suSuccess' );
			$Sofort->setAbortUrl( $this->shopRoot . '?r=suError' );
			$Sofort->setNotificationUrl( $this->shopRoot . '/scripts/bridge.php?type=sunotif' );
			$Sofort->sendRequest(  );

			if ($Sofort->isError(  )) {
				echo $Sofort->getError(  );
				return null;
			}

			$paymentUrl = $Sofort->getPaymentUrl(  );
			header( 'Location: ' . $paymentUrl );
			exit(  );
		}

		function sofort_payment_notification() {
			require_once( $this->shopPath . 'app/app/sofort/library/sofortLib.php' );
			$notification = new SofortLib_Notification(  );
			$notification->getNotification(  );
			echo $notification->getTime(  );
			$transactionId = $notification->getTransactionId(  );
			$que = $this->query( 'SELECT * FROM settings WHERE type=\'set_suconfig\'' );
			$fetch = $this->fetch( $que );
			$transactionData = new SofortLib_TransactionData( $fetch['value'] );
			$transactionData->setTransaction( $transactionId );
			$transactionData->sendRequest(  );
			$curr_order_id = $transactionData->getUserVariable( 0 );
			$curr_order_payment = $transactionData->getAmount(  ) . ' ' . $transactionData->getCurrency(  );

			if (!is_numeric( $curr_order_id )) {
				exit(  );
			}

			$_tt = $this->getTransPHP( 'scripts' );
			$this->query( 'SELECT type,value FROM settings WHERE 
							type=\'set_currency\' OR type=\'set_deltime\' OR type=\'set_mov\' OR type=\'logomail\' OR type=\'set_attachment\' OR 
			$getSetData = type=\'set_email\' OR type=\'set_emailfrom\' OR type=\'set_host\' OR type=\'set_port\' OR type=\'set_aname\' 
OR type=\'set_apass\' OR type=\'set_enc\' OR type=\'set_scosts\' OR type=\'set_fscosts\' OR type=\'set_minval\' OR type=\'set_mailRoute\' OR type=\'set_shopstate\'' );

			$setData = $this->fetch_and_make_assoc( $getSetData, 'type', 'value' );
			$orderCheck = $this->query( 'SELECT * FROM orders WHERE id=' . $curr_order_id . ' AND checked=\'pending\' LIMIT 1' );

			if (mysql_num_rows( $orderCheck )) {
				$orderData = $this->fetch( $orderCheck );
				$from = array( $setData['set_emailfrom'] => $_tt['m_orderconfirmation'] );
				$to = (!empty( $orderData['email'] ) ? $orderData['email'] : false);
				$toShop = (!empty( $setData['set_email'] ) ? $setData['set_email'] : false);
				$nbody = file_get_contents( '../orders/' . $curr_order_id . '.html' );
				$pm = $this->translate_pmethod( $orderData['pmethod'] );
				$upd_file = file_put_contents( '../orders/' . $curr_order_id . '.html', str_replace( $pm, '<b>' . $pm . '</b> (' . $curr_order_payment . ')', $nbody ) );
				$body = file_get_contents( '../orders/' . $curr_order_id . '.html' );

				if ($setData['set_attachment'] == 'pdf') {
					$attachment = (is_file( '../orders/' . $curr_order_id . '.pdf' ) ? '../orders/' . $curr_order_id . '.pdf' : false);

					if ($attachment) {
						@unlink( '../orders/' . $curr_order_id . '.pdf' );
						$this->create_pdf_for_order( $curr_order_id, $curr_order_payment );
					}
				} 
else {
					if ($setData['set_attachment'] == 'winorder') {
						$attachment = (is_file( '../orders/' . $curr_order_id . '.xml' ) ? '../orders/' . $curr_order_id . '.xml' : false);

						if ($attachment) {
							$read_xml = file_get_contents( $attachment );
							$upd_file = file_put_contents( $attachment, str_replace( $pm, $pm . '(' . $curr_order_payment . ')', $read_xml ) );
						}
					} 
else {
						$attachment = false;
					}
				}

				require_once( '../app/app/mail/index.php' );

				if ($setData['set_mailRoute'] == 'smtp') {
					if ($to !== false) {
						$sendSMTP = @sendMail( $from, $to, $_tt['m_ordsubmitsuc'], $body, false );
					}

					$sendSMTP2 = @sendMail( $from, $toShop, $_tt['m_ordsubmitsuc'], $body, $attachment );

					if (( $this->send_fax && $setData['set_attachment'] == 'pdf' )) {
						$sendFax = $this->sendFax( $curr_order_id, $this->shopPath . 'orders/' . $curr_order_id . '.pdf' );

						if (!$sendFax) {
							exit( '{"state":"error","message": "Error while sending fax..."}' );
						}
					}


					if ($sendSMTP2) {
						$this->query( 'UPDATE orders SET pmethod=\'sofort_verified\', checked=\'0\' WHERE id=' . $curr_order_id . ' ' );
					}
				}


				if ($setData['set_mailRoute'] == 'sendmail') {
					if ($to !== false) {
						@sendMail2( $from, $to, $_tt['m_ordsubmitsuc'], $body, false );
						$sendMail = "";
					}

					@sendMail2( $from, $toShop, $_tt['m_ordsubmitsuc'], $body, $attachment );
					$sendMail2 = "";

					if (( $this->send_fax && $setData['set_attachment'] == 'pdf' )) {
						$sendFax = $this->sendFax( $curr_order_id, $this->shopPath . 'orders/' . $curr_order_id . '.pdf' );

						if (!$sendFax) {
							exit( '{"state":"error","message": "Error while sending fax..."}' );
						}
					}


					if ($sendMail2) {
						$this->query( 'UPDATE orders SET pmethod=\'sofort_verified\', checked=\'0\' WHERE id=' . $curr_order_id . ' ' );
						return null;
						Exception; {
							$collectData = $this->collectErrData( 'EmailError(SENDMAIL-checkOut): ' . $e->getMessage(  ) );
							return null;
							Exception; {
								$this->query( 'INSERT INTO errors VALUES (NULL, \'' . $e->getMessage(  ) . '\', \'' . json_encode( $_POST ) . '\'  ,CURRENT_TIMESTAMP) ' );
								exit( '{"state":"error","message": "Error while sending email..."}' );
								return null;
							}
						}
					}
				}
			} 
else {
				error_log( 'order_id not found...' );
			}

		}

		function get_basic_authentication_data() {
			$sel = $this->query( 'SELECT * FROM settings WHERE type LIKE \'set_webservice_%\' AND value!=\'\' LIMIT 2' );

			if (( !mysql_num_rows( $sel ) || mysql_num_rows( $sel ) != 2 )) {
				return false;
			}

			$arr = array(  );

			if ($row = mysql_fetch_assoc( $sel )) {
				$arr[$row['type']] = $row['value'];
			}

			return $arr;
		}

		function check_device() {
			$device = '';

			if (( stristr( $_SERVER['HTTP_USER_AGENT'], 'iphone' ) || strstr( $_SERVER['HTTP_USER_AGENT'], 'iphone' ) )) {
				return 'iphone';
			}


			if (stristr( $_SERVER['HTTP_USER_AGENT'], 'android' )) {
				return 'android';
			}

		}

		function create_pdf_for_order($order_id, $update = null) {
			if (( !$order_id || !is_numeric( $order_id ) )) {
				error_log( 'Wrong ID for create_pdf' );
				return false;
			}

			$getOrder = $this->query( 'SELECT *,date_format(oTime, \'%d.%m.%Y - %H:%i\') as oDate FROM orders WHERE id=' . $order_id . ' LIMIT 1' );

			if (!mysql_num_rows( $getOrder )) {
				return false;
			}

			$pLogo = $this->query( 'SELECT * FROM settings WHERE type=\'logomail\' OR type=\'set_intID\' OR type=\'set_currency\'' );
			$settings = $this->fetch_and_make_assoc( $pLogo, 'type', 'value' );
			$order = $this->fetch( $getOrder );
			$products = unserialize( base64_decode( $order['iorder'] ) );
			$settings['set_currency'] = ($settings['set_currency'] == '€' ? chr( 128 ) : $settings['set_currency']);
			$_tt = $this->getTransPHP(  );
			$to = $order['code'] . '  ' . $order['phoneNumber'] . '
';
			$to .= (!empty( $order['company'] ) ? $order['company'] . ' ' : '');
			$to .= (!empty( $order['department'] ) ? $order['department'] . '
' : '');
			$to .= $_tt[$order['gender']] . ' ' . $order['firstname'] . ' ' . $order['lastname'] . '
';
			$to .= $order['street'] . '
' . $order['postcode'] . ' ' . $order['city'] . '
';
			require( $this->shopPath . 'app/app/pdf/fpdf.php' );
			$pdf = new FPDF(  );
			$pdf->AddPage( 'P', 'A4' );
			$pdf->SetFillColor( 0, 0, 0 );
			$pdf->SetFont( 'Arial', '', 24 );
			$pdf->SetFillColor( 0, 0, 0 );
			$pdf->Cell( 160, 8, utf8_decode( $_tt['m_orderconfirmation'] ), 0, 0, 'L', 0 );
			$pdf->SetFont( 'Arial', '', 10 );
			$pdf->Cell( 0, 8, $order['oDate'], 0, 0, 'R', 0 );
			$pdf->Ln( 16 );
			$pdf->SetFont( 'Arial', '', 14 );

			if (count( $products ) < 7) {
				$pdf->Ln( 5 );
			}

			$pdf->Cell( 0, 4, $_tt['deliveryaddress'], 0, 1, 'L' );
			$pdf->Ln( 2 );
			$pdf->SetFont( 'Arial', '', 11 );

			if (function_exists( 'iconv' )) {
				$pdf->Multicell( 0, 4, iconv( 'UTF-8', 'ISO-8859-1//TRANSLIT', $to ), 0, 1 );
			} 
else {
				$pdf->Multicell( 0, 4, utf8_decode( $to ), 0, 1 );
			}

			$pdf->Ln( 6 );

			if (count( $products ) < 7) {
				$pdf->Ln( 5 );
			}

			$pdf->Cell( 0, 0.100000000000000005551115, '', 0, 0, 0, true );
			$pdf->Ln( 2 );
			$delTime = ($order['deltime'] ? $order['deltime'] : $_tt['now']);
			$pdf->SetFont( 'Arial', '', 11 );
			$pdf->Cell( 38, 3, $_tt['deliverytime'], 0, 0, 'L', 0 );
			$pdf->SetFont( 'Arial', 'b', 11 );
			$pdf->Cell( 0, 3, $delTime, 0, 0, 'L', 0 );
			$pdf->Ln( 4 );
			$pdf->Cell( 0, 0.100000000000000005551115, '', 0, 0, 0, true );
			$pdf->Ln( 2 );
			switch ($order['pmethod']) {
				case 'sofort': {
					$m_method = 'sofortueberweisung';
					break;
				}

				case 'cash': {
					$m_method = $_tt['cash'];
					break;
				}
			}

			$order['pmethod'];
			$m_method = $pdf->SetFont( 'Arial', '', 12 );
			$pdf->SetFont( 'Arial', '', 11 );
			$pdf->Cell( 38, 3, $_tt['paymentmethod'] . ': ', 0, 0, 'L', 0 );
			$pdf->SetFont( 'Arial', 'b', 11 );

			if (!$update) {
				$pdf->Cell( 0, 3, $m_method, 0, 0, 'L', 0 );
			} 
else {
				$pdf->Cell( 0, 3, $m_method . ' (' . $update . ')', 0, 0, 'L', 0 );
			}

			$pdf->Ln( 4 );
			$pdf->Cell( 0, 0.100000000000000005551115, '', 0, 0, 0, true );
			$pdf->Ln( 2 );

			if ($order['infos']) {
				$pdf->SetFont( 'Arial', '', 11 );
				$pdf->SetTextColor( 0, 0, 0 );
				$pdf->Cell( 38, 3, 'Infotext: ', 0, 0, 'L', 0 );
				$pdf->Multicell( 0, 4, utf8_decode( $order['infos'] ), 0, 1 );
				$pdf->Ln( 1 );
			}

			$pdf->Ln( 10 );

			if (count( $products ) < 7) {
				$pdf->Ln( 5 );
			}

			$pdf->SetFont( 'Arial', '', 14 );
			$pdf->Cell( 0, 4, $_tt['m_yourorder'], 0, 1, 'L' );
			$pdf->Ln( 5 );
			$pdf->SetFont( 'Arial', '', 11 );
			foreach ($products as $product) {
				$pdf->Ln( 0.5 );
				$pdf->Cell( 0, 0.100000000000000005551115, '', 0, 0, 0, true );
				$pdf->Ln( 0.5 );
				$pdf->Ln(  );
				$pdf->SetFont( 'Arial', '', 10 );
				$pdf->SetTextColor( 0, 0, 0 );

				if (( isset( $settings['set_intID'] ) && $settings['set_intID'] )) {
					$pdf->Cell( 10, 5, $product['intID'], 0, 0, 'L', 0 );
				}

				$pdf->SetFont( 'Arial', 'b', 10 );
				$pdf->Cell( 85, 5, utf8_decode( $product['name'] . ' ' . $product['sName'] ), 0, 0, 'L', 0 );
				$pdf->Cell( 0, 5, sprintf( '%1$.2f', $product['price'] ) . $settings['set_currency'], 0, 0, 'R', 0 );
				$pdf->SetFont( 'Arial', '', 10 );
				$pdf->Ln( 5 );

				if (!empty( $product['extras'] )) {
					foreach ($product['extras'] as $extras) {
						$pdf->SetFont( 'Arial', '', 9 );
						$pdf->SetTextColor( 50, 50, 50 );

						if (( isset( $settings['set_intID'] ) && $settings['set_intID'] )) {
							$pdf->Cell( 10, 4, ' ', 0, 0, 'L', 0 );
						}

						$pdf->Cell( 65, 4, utf8_decode( $extras['name'] ), 0, 0, 'L', 0 );
						$pdf->Cell( 0, 4, sprintf( '%1$.2f', $extras['price'] ) . $settings['set_currency'], 0, 0, 'R', 0 );
						$pdf->Ln( 4 );
					}

					continue;
				}
			}

			$pdf->Ln( 5 );
			$pdf->SetFont( 'Arial', '', 15 );
			$pdf->SetFillColor( 255, 255, 255 );
			$pdf->SetTextColor( 0, 0, 0 );
			$pdf->Cell( 85, 7, '', 0, 0, 'L', true );
			$pdf->SetFont( 'Arial', '', 14 );
			$pdf->Cell( 80, 8, $_tt['sum'], 0, 0, 'R', true );
			$pdf->SetFont( 'Arial', 'b', 15 );
			$pdf->Cell( 0, 8, sprintf( '%1$.2f', $order['sum'] ) . $settings['set_currency'], 0, 0, 'R', true );
			$pdf->Ln(  );

			if ($order['scosts']) {
				$pdf->Cell( 8, 7, '', 0, 0, 'L', true );
				$pdf->SetFont( 'Arial', '', 9 );
				$pdf->Cell( 0, 5, $_tt['incl'] . ' ' . $_tt['scosts'] . ': ' . $order['scosts'] . $settings['set_currency'], 0, 0, 'R', true );
			}

			$pdf->Ln( 4 );
			$file_path = $this->shopPath . 'orders/' . $order_id . '.pdf';
			$create_file = $pdf->Output( $file_path );

			if (is_file( $file_path )) {
				return true;
			}

			return false;
		}

		function urlsafe_b64encode($string) {
			$data = base64_encode( $string );
			$data = str_replace( array( '+', '/', '=' ), array( '-', '_', '' ), $data );
			return $data;
		}

		function checkResponse($str) {
			$explode = explode( 'html', $str );
			$result = substr( trim( $explode[1] ), 0, 3 );

			if ($result == '100') {
				return true;
			}

			return false;
		}

		function sendFax($order_id, $datei) {
			$url = 'http://www.smskaufen.com/sms/faxtmp/inbound.php';
			$form['id'] = $this->fax_username;
			$form['apikey'] = $this->fax_apikey;
			$form['empfaenger'] = $this->fax_reciever;
			$form['abs_nr'] = $this->fax_sender;
			$form['abs_name'] = $this->fax_sendername;
			$form['datei'] = $this->urlsafe_b64encode( file_get_contents( $datei ) );
			$form['document'] = '@' . $datei;
			$ch = curl_init( $url );
			curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
			curl_setopt( $ch, CURLOPT_POSTFIELDS, $form );
			curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt( $ch, CURLOPT_HEADER, true );
			$exec = curl_exec( $ch );
			curl_close( $ch );
			error_log( '---------------------------------------------' );
			$this->checkResponse( $exec );
			$res = error_log( $exec );
			return $res;
		}
	}

	error_reporting( 30719 );
?>