
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="net.sf.json.*"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="com.eg.IniFile"%>
<%@ page import="com.eg.EgInstallInfo"%>
<%@ page import="com.eg.EgMappingInfo"%>

<%
		try {
		String from=request.getParameter("from");
		response.setContentType("application/json");
		response.setHeader("X-Robots-Tag", "noindex, nofollow");
		JSONObject json = new JSONObject();
		JSONObject screenlabels = new JSONObject();
		EgMappingInfo mapping = new EgMappingInfo("manager");

		screenlabels.put("username", mapping.getTextDescription("admin.login.Username"));
		screenlabels.put("password", mapping.getTextDescription("admin.login.Password"));
		screenlabels.put("logon", mapping.getTextDescription("admin.login.Domain"));
		screenlabels.put("logon_idp", "Identity Provider");
		screenlabels.put("siginin", mapping.getTextDescription("admin.login.Sign In"));
		screenlabels.put("ForgotPassword", mapping.getTextDescription("admin.login.Forgot Password"));
		screenlabels.put("Help",mapping.getTextDescription("admin.login.Help"));
		
		screenlabels.put("identity", mapping.getTextDescription("admin.login.Please confirm your identity"));
		screenlabels.put("Get Password", mapping.getTextDescription("admin.login.Get Password"));
		screenlabels.put("Login Page", mapping.getTextDescription("admin.login.Login Page"));
		screenlabels.put("admin.login.Copyright", mapping.getTextDescription("admin.login.Copyright"));
		screenlabels.put("copyright",mapping.getTextDescription("admin.login.eG Innovations, Inc&#46 All Rights Reserved"));
		screenlabels.put("admin.login.Powered by eG Enterprise",mapping.getTextDescription("admin.login.Powered by eG Enterprise"));

		com.eg.EgManagersInfo mgrInfo=null;
		java.util.Hashtable ht=null;
		boolean isDomainConfigured = false;
		ArrayList egLoggingSystem = new ArrayList();
		if(EgInstallInfo.isSuperManager)
		{
			mgrInfo= com.eg.EgManagersInfo.getInstance();
			ht = mgrInfo.getReportingManagers();
			if(ht != null && !ht.isEmpty())
			{
				java.util.Iterator itr = ht.keySet().iterator();
				while(itr.hasNext())
				{
					String mgrId = (String)itr.next();
					IniFile iniFile = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/"+mgrId+"-eg_authenticate.ini",false,"yes");
					String[] domains = iniFile.getSubjects();
					if(domains != null && domains.length > 0)
					{
						isDomainConfigured=true;
						for (int i=0;i<domains.length;i++)
						{
							if(!egLoggingSystem.contains(domains[i]))
							{
								egLoggingSystem.add(domains[i]);
							}
						}
					}
				}
			}
			if(!egLoggingSystem.isEmpty())
			{
				java.util.Collections.sort(egLoggingSystem,String.CASE_INSENSITIVE_ORDER);
			}
		}
		else
		{
			IniFile iniFile = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_authenticate.ini");
			//egLoggingSystem.add("eG Enterprise");
			String[] domains = iniFile.getSubjects();
			if (domains != null && domains.length > 0)
			{
				for (int i = 0; i < domains.length; i++)
				{
					if (!egLoggingSystem.contains(domains[i]))
					{
						egLoggingSystem.add(domains[i]);
					}
				}
			}
			isDomainConfigured = (domains != null && domains.length > 0);
		}
		IniFile ssoIniObj = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_samlsso.ini");

		ArrayList egLoggingIdp = new ArrayList();
		String[] idpList = ssoIniObj.getSubjects();
		if (idpList != null && idpList.length > 0) 
		{
			for (int i = 0; i < idpList.length; i++) 
			{
				if (!com.egurkha.util.StringUtils.isNull(idpList[i]) && (idpList[i].equals("SAML_SSO") || idpList[i].startsWith("SECONDARY_")))
				{
					continue;
				}
				if (!egLoggingIdp.contains(idpList[i])) 
				{
					egLoggingIdp.add(idpList[i]);
				}
			}
		}
		
		String isLoggingFrom = request.getParameter("isLoggingFrom");
		String idpResponse = request.getParameter("idpResponse");
		String loggedInDomain = request.getParameter("loggedInDomain");
		String loggedInIdp="";
		loggedInIdp = request.getParameter("loggedInIdp");
		if (com.egurkha.util.StringUtils.isNull(isLoggingFrom))
		{
			isLoggingFrom = "eG Enterprise";
		}
		
		if (com.egurkha.util.StringUtils.isNull(idpResponse))
		{
			idpResponse = "";
		}

		JSONArray domainArray = new JSONArray();
		JSONArray idpArray = new JSONArray();
		JSONObject obj;
		
		JSONObject json_mobile=new JSONObject();
		JSONArray domainArray_mobile = new JSONArray();
		JSONArray idpArray_mobile = new JSONArray();
		JSONObject obj_mobile;

		String domainCombobox_value="";
		String idpCombobox_value="";
		for (int k = 0;k<egLoggingSystem.size();k++)
		{
			String logging = (String)egLoggingSystem.get(k);
			obj = new JSONObject();
			obj_mobile=new JSONObject();
			String dispLoggingDomain=logging;
			String   valforDomain=logging;

			//if(dispLoggingDomain!=null && dispLoggingDomain.equals("eG Enterprise"))
			//{
				//dispLoggingDomain="Local";
			//}

			if (loggedInDomain != null && logging.equalsIgnoreCase(loggedInDomain))
			{
				obj.put("displayField",dispLoggingDomain);
				obj.put("valueField",logging);
				obj_mobile.put("displayField",dispLoggingDomain);
				obj_mobile.put("valueField",logging); 
				domainCombobox_value = logging;
			}
			else
			{
				obj.put("displayField",dispLoggingDomain);
				obj.put("valueField",logging);
				obj_mobile.put("displayField",dispLoggingDomain);
				obj_mobile.put("valueField",logging); 
				//domainCombobox_value = logging;

			}
			domainArray.add(obj);
			domainArray_mobile.add(obj_mobile);
		}
		obj_mobile= new JSONObject();
		obj_mobile.put("displayField","Local");
		obj_mobile.put("valueField","Local"); 
		domainArray_mobile.add(obj_mobile);

		if (egLoggingIdp != null && egLoggingIdp.size() > 0)
		{
			for (int k = 0;k<egLoggingIdp.size();k++)
			{
				String idpName = (String)egLoggingIdp.get(k);
				JSONObject obj_idp = new JSONObject();
				JSONObject obj_mobile_idp=new JSONObject();

				obj_idp.put("displayField",idpName);
				obj_idp.put("valueField",idpName);

				obj_mobile_idp.put("displayField",idpName);
				obj_mobile_idp.put("valueField",idpName); 

				idpArray.add(obj_idp);
				idpArray_mobile.add(obj_mobile_idp);

				if(loggedInIdp!=null&&loggedInIdp.length()>0 && idpName.equalsIgnoreCase(loggedInIdp))
				{
					idpCombobox_value = idpName;
				}
			}
		}

		json.put("idpCombobox_value",idpCombobox_value); 
		json.put("configuredDomains", domainArray);
		json.put("configuredIdp", idpArray);
		json.put("loggedInDomain", domainCombobox_value);
		json_mobile.put("configuredDomains",domainArray_mobile);
		json_mobile.put("configuredIdp",idpArray_mobile);

		boolean hasMultiLoginTypes = false;
		boolean hasSsoLoginEnabled = false;
		String enableSSOLogin = ssoIniObj.getValue("SAML_SSO","SSO_ENABLED");
		if(enableSSOLogin!=null && (enableSSOLogin.equalsIgnoreCase("yes") || enableSSOLogin.equalsIgnoreCase("true")))
		{
			hasSsoLoginEnabled = true;
		}
		//boolean isDomainConfigured = (domains != null && domains.length > 0);

		hasMultiLoginTypes = (isDomainConfigured || (hasSsoLoginEnabled && (egLoggingIdp != null && egLoggingIdp.size() > 0)))? true : false;
		
		json.put("hasMultiLoginTypes",hasMultiLoginTypes);
		if (hasMultiLoginTypes)
		{
			JSONArray loginTypesArr = new JSONArray();
			JSONObject tmpJsonObj = new JSONObject();
			tmpJsonObj.put("displayField","eG Enterprise");
			tmpJsonObj.put("valueField","eG Enterprise");
			loginTypesArr.add(tmpJsonObj);

			if (isDomainConfigured)
			{
				tmpJsonObj = new JSONObject();
				tmpJsonObj.put("displayField","Active Directory");
				tmpJsonObj.put("valueField","Domain");
				loginTypesArr.add(tmpJsonObj);
			}

			if (hasSsoLoginEnabled && (egLoggingIdp != null && egLoggingIdp.size() > 0))
			{
				tmpJsonObj = new JSONObject();
				tmpJsonObj.put("displayField","SAML Identity Provider");
				tmpJsonObj.put("valueField","SAML");
				loginTypesArr.add(tmpJsonObj);
			}
			json.put("loginTypesArr",loginTypesArr);
			screenlabels.put("loginTypeLbl", mapping.getTextDescription("admin.login.Authentication"));
			json.put("isLoggingFrom", isLoggingFrom);
			json.put("idpResponse", idpResponse);
		}
		json.put("screenlabels", screenlabels);

		if(from!=null && from.equalsIgnoreCase("Mobile"))
		{
				IniFile serviceIniObj = new IniFile(EgInstallInfo.getInstallDir()+ "/manager/config/eg_services.ini");
				String restrictMobileAppAccess = serviceIniObj.getValue("MISC_ARGS","RestrictMobileAppAccess");
				serviceIniObj=null;
				if(restrictMobileAppAccess!=null && restrictMobileAppAccess.equalsIgnoreCase("yes"))
				{
					json_mobile.put("success" , new Boolean(true));
					json_mobile.put("mobileAppAccess" , new Boolean(false));
					json_mobile.put("mobileAppAccessMsg","Mobile App access is restricted in this eG Manager. Please contact your eG Administrator.");
				}
				else
				{
					json_mobile.put("success" , new Boolean(true));
					json_mobile.put("mobileAppAccess" , new Boolean(true));
					json_mobile.put("mobileAppAccessMsg","");
				}
				response.getWriter().write(json_mobile.toString());
		}
		else
		{
				response.getWriter().write(json.toString());
		}
		
	} catch (Exception e) {
		e.printStackTrace();
	}
%>



