<?php
function throwCsssForSelect( ){
	$aFiles = glob( DIR_TEMPLATES . '*.css' );
	if( isset( $aFiles ) ){
		$aResult = Array( );
		foreach( $aFiles as $iFile => $sFileName ){
			$sFileName = substr( $sFileName, strlen( DIR_TEMPLATES ) );
			if( ( strpos( $sFileName, 'style_editor' ) === false ) && ( strpos( $sFileName, 'style_print' ) === false ) && ( strpos( $sFileName, 'style_orders_email' ) === false ) && ( strpos( $sFileName, 'style_PaymentForm' ) === false ) ) $aResult[$sFileName] = $sFileName;
		}
		return $aResult;
	}
}
function saveVariables( $aForm, $sFile, $sVariable = 'config' ){
	$aFile = file( $sFile );
	$iCount = count( $aFile );
	$rFile = fopen( $sFile, 'w' );
	for( $i = 0;$i < $iCount;$i++ ){
		foreach( $aForm as $sKey => $sValue ){
			if( preg_match( '/' . $sVariable . "\\['" . $sKey . "'\\]\\s*=/", $aFile[$i] ) ){
				$sValue = changeSpecialChars( $sValue );
				if( $sVariable == 'lang' ) $sValue = str_replace( '\'', '&quot;', $sValue );
				else $sValue = str_replace( '"', '&quot;', $sValue );
				$sValue = stripslashes( $sValue );
				if( ( is_numeric( $sValue ) || preg_match( '/^(true|false|null)$/', $sValue ) == true ) && !preg_match( '/0[0-9]+/', $sValue ) ) $aFile[$i] = "\$" . $sVariable . "['" . $sKey . "'] = " . $sValue . ";\n";
				else{
					if( $sVariable == 'lang' ) $aFile[$i] = "\$" . $sVariable . "['" . $sKey . "'] = '" . $sValue . "';\n";
					else $aFile[$i] = "\$" . $sVariable . "['" . $sKey . "'] = \"" . $sValue . "\";\n";
				}
			}
		}
		fwrite( $rFile, $aFile[$i] );
	}
	fclose( $rFile );
}
function loginActions( $p, $sKey = 'bLogged' ){
	global $aActions;
	$oTpl = TplParser::getInstance( );
	if( !isset( $_SESSION[$sKey] ) || $_SESSION[$sKey] !== TRUE ){
		if( $p == 'login' && isset( $_POST['sLogin'] ) && isset( $_POST['sPass'] ) ){
			switch( checkLogin( $_POST['sLogin'], $_POST['sPass'], $sKey ) ){
				case 1:
					$sRedirect = !empty( $_POST['sLoginPageNext'] ) ? $_POST['sLoginPageNext'] : $_SERVER['PHP_SELF'];
					if( isset( $_POST['sPostContent'] ) ){
						$_SESSION['sPostContent'] = $_POST['sPostContent'];
					} else{
						$_SESSION['sPostContent'] = null;
					}
					header( 'Location: ' . $sRedirect );
					exit;
				break;
				default:
					$oTpl->assign( 'bIncorrect', true );
				break;
			}
		}
		unset( $aActions );
		echo $oTpl->parse( 'login.tpl' );
		exit;
	} else{
		if( $p == 'logout' ){
			unset( $_SESSION[$sKey], $_SESSION['iAdminId'] );
			$sLoginPage = $_SERVER['PHP_SELF'];
			header( 'Location: ' . $_SERVER['PHP_SELF'] );
			exit;
		}
	}
}
function checkLogin( $sLogin, $sPass, $sKey ){
	if( $GLOBALS['config']['login'] == $sLogin && $GLOBALS['config']['pass'] == md5( $sPass ) ){
		$_SESSION[$sKey] = true;
		$_SESSION['iAdminId'] = - 1;
		return 1;
	} else{
		return checkAdminLogin( $sLogin, $sPass, $sKey );
		return 0;
	}
}
function throwTrueFalseSelect( $bTrueFalse = false ){
	$aSelect = Array( null, null );
	if( $bTrueFalse != true ) $aSelect[0] = 'selected="selected"';
	else $aSelect[1] = 'selected="selected"';
	$sOption = '<option value="true" ' . $aSelect[1] . '>' . LANG_YES_SHORT . '</option>';
	$sOption.= '<option value="false" ' . $aSelect[0] . '>' . LANG_NO_SHORT . '</option>';
	return $sOption;
}
function throwTrueNullSelect( $bTrueNull = null ){
	$aSelect = Array( null, null );
	if( $bTrueNull != true ) $aSelect[0] = 'selected="selected"';
	else $aSelect[1] = 'selected="selected"';
	$sOption = '<option value="true" ' . $aSelect[1] . '>' . LANG_YES_SHORT . '</option>';
	$sOption.= '<option value="null" ' . $aSelect[0] . '>' . LANG_NO_SHORT . '</option>';
	return $sOption;
}
function changeUri( $sUrl ){
	return preg_replace( "/&iPage=[0-9]*/", '', $sUrl );
}
?>