<?php
function checkMenuElementForUser( $aPage ){
	$oDB = DataBase::getInstance( );
	if( !isset( $aPage['iAuthorized'] ) || $aPage['iAuthorized'] != 1 ) return true;
	if( isset( $_SESSION['iUserClient'] ) && is_numeric( $_SESSION['iUserClient'] ) ){
		$sql = 'SELECT COUNT(`users_pages`.`iElement`) AS `count`
							FROM `' . $oDB->throwTableName( DB_USERS_TO_GROUPS ) . '` AS `users_to_groups`,
									 `' . $oDB->throwTableName( DB_USERS_GROUPS ) . '` AS `users_groups`,
									 `' . $oDB->throwTableName( DB_USERS_PAGES ) . '` AS `users_pages`
							WHERE (`users_to_groups`.`iUser` = \'' . $_SESSION['iUserClient'] . '\')
								AND (`users_groups`.`iGroup` = `users_to_groups`.`iGroup`)
								AND (`users_pages`.`iGroup` = `users_groups`.`iGroup`)
								AND (\'' . $aPage['iPage'] . '\' = `users_pages`.`iPage`)
						';
		$aResult = $oDB->executeSQL( $sql, true );
		if( $aResult[0]['count'] > 0 ) return true;
	}
	return null;
}
function saveUser( $aForm, $iUser = null, $bNoPassword = false ){
//	$bAutoGroup = false;
	$oDB = DataBase::getInstance( );
	if( isset( $iUser ) ) $aForm['iUser'] = $iUser;
	if( isset( $aForm['iUser'] ) && is_numeric( $aForm['iUser'] ) ){
		$sParam = 'iUser';
		$aData = $oDB->throwData( DB_USERS, $aForm['iUser'], 'iUser' );
		if( !empty( $aForm['sPass'] ) ){
			sendPasswordEmail( $aForm['sEmail'], $aForm['sLogin'], $aForm['sPass'], 0 );
		}
		if( !isset( $aForm['sLogin'] ) || empty( $aForm['sLogin'] ) ){
			$aForm['sLogin'] = $aData['sLogin'];
			if( !isset( $aForm['sPass'] ) || empty( $aForm['sPass'] ) ) $aForm['sPass'] = $aData['sPass'];
			else $aForm['sPass'] = md5( $aForm['sPass'] );
		} else{
			if( !isset( $aForm['sPass'] ) || empty( $aForm['sPass'] ) ) $aForm['sPass'] = $aData['sPass'];
			else $aForm['sPass'] = md5( $aForm['sPass'] );
		}
	} else{
		$sParam = null;
		if( !$bNoPassword ){ //nie wysyłamy emaila do użytkowników anonimowych
			require_once( $GLOBALS['config']['dir_libraries'] . 'emogrifier.php' );
			$oTpl = TplParser::getInstance( );
			$aSend = Array( 'sTopic' => $GLOBALS['config']['website_name'] . ' - ' . $GLOBALS['lang']['Register'], 'sSender' => $GLOBALS['config']['server_email'] );
			$aUrl = parse_url( 'http' . ( $_SERVER['HTTPS'] ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
			$aUrl['path'] = preg_replace( '|/[^/]*$|', '/', $aUrl['path'] );
			$oTpl->assign( 'aUrl', $aUrl );
			$oTpl->assign( 'aUser', $aForm );
			$aSend['sMailContent'] = $oTpl->parse( 'registration_email.tpl' );
			$sHtml = $oTpl->parse( 'registration_email_html.tpl' );
			$sCss = file_get_contents( $GLOBALS['config']['dir_templates'] . 'style_orders_email.css' );
			$oEm = new Emogrifier( $sHtml, $sCss );
			$aSend['sHtmlContent'] = $oEm->emogrify( );
			sendEmail( $aSend, $aForm['sEmail'] );
//			$bAutoGroup = true;
		}
		if( $bNoPassword ){
			$aForm['sPass'] = null;
		} else{
			$aForm['sPass'] = md5( $aForm['sPass'] );
		}
	}
	$aForm = changeMassTxt( $aForm );
	$aForm['iGTExported'] = 0; //Aktualizacja danych przy najbliższej synchronizacji z oscGT
	$aForm['iUser'] = $oDB->save( DB_USERS, $aForm, $sParam, 'sort' );
//	if( $bAutoGroup && isset( $GLOBALS['config']['auto_group'] ) && is_numeric( $GLOBALS['config']['auto_group'] ) && !empty( $GLOBALS['config']['auto_group'] ) ) $oDB->save( DB_USERS_TO_GROUPS, Array( 'iUser' => $aForm['iUser'], 'iGroup' => $GLOBALS['config']['auto_group'] ) );
	return $aForm['iUser'];
}
function throwUser( $iUser ){
	$oDB = DataBase::getInstance( );
	$aData = $oDB->throwData( DB_USERS, $iUser, 'iUser' );
	$aData['fDiscount'] = $oDB->executeSQL( 'SELECT MAX( `fDiscount` ) FROM `' . $oDB->throwTableName( DB_USERS_GROUPS ) . '` INNER JOIN `' . $oDB->throwTableName( DB_USERS_TO_GROUPS ) . '` USING( `iGroup` ) WHERE `iUser` = ' . intval( $iUser ), true, DataBase::SINGLE_VALUE );
	if( isset( $aData ) && is_array( $aData ) ){
		return changeMassTxt( $aData );
	}
}
function checkUserLogin( $sLogin, $sPass ){
	$sLogin = changeTxt( $sLogin );
	$sPass = md5( changeTxt( $sPass ) );
	$oDB = & DataBase::getInstance( );
	$sql = 'SELECT `iUser` FROM `' . $oDB->throwTableName( DB_USERS ) . '` WHERE ( `sEmail` = \'' . $oDB->escape( $sLogin ) . '\' OR `sLogin` = \'' . $oDB->escape( $sLogin ) . '\' ) AND `sPass` = \'' . $sPass . '\' AND `iStatus` >= 1 LIMIT 1;';
	return $oDB->executeSQL( $sql, true, DataBase::SINGLE_VALUE );
}
function checkUserLoginNotExist( $sLogin, $iUser = null ){
	$oDB = DataBase::getInstance( );
	$sLogin = changeTxt( $sLogin );
	if( preg_match( '/^anonymous[0-9]*$/', $sLogin ) ) return false; //Nie pozwalamy zakładać kont anonimowych, są zarezerwowane dla klientów niezarejestrowanych składających zamówienie
	if( preg_match( '/^fb\\-[0-9]*$/', $sLogin ) ) return false; //Nie pozwalamy zakładać kont facebookowych, są zarezerwowane dla klientów logujących się przez facebooka
	$sql = 'SELECT `iUser` FROM `' . $oDB->throwTableName( DB_USERS ) . '` WHERE ( `sLogin` = \'' . $oDB->escape( $sLogin ) . '\' OR `sEmail` = \'' . $oDB->escape( $sLogin ) . '\' ) AND `sPass` IS NOT NULL';
	if( isset( $iUser ) && intval( $iUser ) > 0 ){
		$sql .= ' AND `iUser` != ' . intval( $iUser );
	}
	if( $oDB->executeSQL( $sql, true, DataBase::SINGLE_VALUE ) ) return false;
	else return true;
}
function listUserOrders( $iUser ){
	$oTpl = & TplParser::getInstance( );
	$oFile = Files::getInstance( );
	$oDB = DataBase::getInstance( );
	$sql = 'SELECT * FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `iUser` = \'' . intval( $iUser ) . '\';';
	$aStats = $oDB->executeSQL( $sql, true );
	$sLink = $GLOBALS['oPage']->aPages[$GLOBALS['config']['user_orders_page']]['sLinkName'] . '?iOrder=';
	foreach( $aStats as &$aData ){
		$aData['sDate'] = date( 'Y-m-d H:i', $aData['iTime'] );
		$aData['sStatus'] = $GLOBALS['oOrder']->throwStatus( $aData['iStatus'] );
		if( $aData['iStatus'] == 1 ) $aData['sStatus'] = '<b>' . $aData['sStatus'] . '</b>';
		$aData['sLink'] = $sLink . $aData['iOrder'];
	}
	$oTpl->assign( 'aStats', $aStats );
	return $oTpl->parse( 'user_orders_list.tpl' );
}
function SendRemindPassword( $sLogin, $sEmail ){
	$sLogin = changeTxt( $sLogin );
	$sEmail = changeTxt( $sEmail );
	$oDB = DataBase::getInstance( );
	$aData = $oDB->throwData( DB_USERS, $sLogin, 'sLogin' );
	if( isset( $aData ) && is_array( $aData ) ){
		if( $aData['sEmail'] == $sEmail ){
			$sOld = $aData['sPass'] = uniqid( );
			saveUser( $aData );
			return sendEmail( Array( 'sTopic' => $GLOBALS['lang']['Password_remind'], 'sMailContent' => $GLOBALS['lang']['Login'] . ': ' . $sLogin . "\n" . 'Nowe haslo: ' . $sOld, 'sSender' => $GLOBALS['config']['server_email'] ), $sEmail );
		} else return false;
	}
}
function informUserOnGroup( $iUser, $bDiscount, $bAdditionalPages ){
	$aUser = throwUser( $iUser );
	include( 'languages/' . LANGUAGE . '.php' );
	if( checkEmail( $aUser['sEmail'] ) ){
		$content = $lang['user_group_email'];
		$content = changeTxt( $content, 'nlNds' );
		$content = str_replace( '%sLogin%', $aUser['sLogin'], $content );
		$content = str_replace( '%sDiscount%', $bDiscount ? $lang['user_group_email_discount'] : null, $content );
		$content = str_replace( '%sPages%', $bAdditionalPages ? $lang['user_group_email_pages'] : null, $content );
		$aMail['sMailContent'] = str_replace( '|n|', "\n", $content );
		$aMail['sTopic'] = $lang['user_group_email_topic'];
		$aMail['sSender'] = $GLOBALS['config']['email'];
		return sendEmail( $aMail, $aUser['sEmail'] );
	}
}
?>