<%
/********************************************************************************************************
*	Name			:	EgLicenseReport.jsp
*	Purpose			:	To decrypt license file
*	CreationDate	:	Nov 12 2009
*	Author			:	G.Arivarasu
*******************************************************************************************************/
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.io.PrintWriter"%>
<%@ page import="java.io.FileWriter"%>
<%@ page import="java.io.IOException"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.eg.EgInstallInfo"%>
<%@ page import="com.eg.EgConMonHelper"%>
<%@ page import="com.eg.EgDecryptLicense"%>
<%@ page import="com.eg.*"%>
<%@ page import="java.io.File"%>
<%@ page import=" java.io.*"%>
<%@ page import = "com.eg.EgTestInfo" %>
<%@ page import="com.egurkha.util.StringUtils"%>
<%@ taglib prefix = "egui" uri = "../WEB-INF/eg-taglib.tld"%>
<html>
<head>
<link rel="stylesheet" href="eg_styles/adminstyle_blue.css"/>
<script language="javascript" src="/final/admin/eg_scripts/timeout.js"></script>
<script language="javascript">
function formreset()
{
	document.license.info.value ='';
	document.license.submit();
}
document.onscroll = function()
{
	clearTimeout(time);
	time = setTimeout(logout,timeoutTime); 
}
</script>
<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	
	ArrayList list=new ArrayList();
	ArrayList zoneList=new ArrayList();
	String mgrMiscPath = EgInstallInfo.getInstallDir() + "/manager/config/eg_mgrmisc.ini";
	IniFile mgrMiscObj = new IniFile(mgrMiscPath);
	String licenseUsageValue = mgrMiscObj.getValue("EgCert_Usage","CertUsageMail");
	String zoneUsageValue = mgrMiscObj.getValue("EgCert_Usage","ZoneUsageMail");
	String detailTypeValue = mgrMiscObj.getValue("EgCert_Usage","DetailType");
	EgCertUsage certUsage = new EgCertUsage();
	String[] zoneName = certUsage.getAllZoneNames();
	int zoneLen = zoneName.length;
	String tool = "decrypt";

	String sessionInUse = request.getParameter("sessionInUse");
	String pwd = request.getParameter("pwd");
	String authenticated = (String)session.getAttribute("authenticated");

	if (authenticated == null)
	{
		// Direct access from the browser address bar or from inside the toolkit via the tool
		if (pwd == null)
		{
			if (sessionInUse == null)
			{
				response.sendRedirect(request.getContextPath()+"/toolkitlogin?comeFrom="+tool+"&tools="+tool);
			}
			else
			{
%>
				<script>
					window.parent.location.href ="/final/toolkitlogin";
				</script>
<%
			}
		}
		else
		{
			// Direct access from the browser address bar
			if (!pwd.equals(EgConMonHelper.executePwd))
			{
				response.sendRedirect(request.getContextPath()+"/toolkitlogin?comeFrom="+tool+"&tools="+tool);
			}
			else
			{
				session.setAttribute("reqFromForTk","decrypt");
				session.setAttribute("validate_tools","true");
				session.setAttribute("tools",tool);
				String redirectURL = request.getContextPath()+"/toolkit";
				response.sendRedirect(redirectURL);
			}
		}
	}
	else
	{ 
		// Direct access from the browser address bar and session in use
		if (sessionInUse == null)
		{
			session.setAttribute("validate_tools","true");
			session.setAttribute("tools",tool);
			response.sendRedirect(request.getContextPath()+"/toolkit");
		}
	}
	session.setAttribute("reqFromForTk","decrypt");

	String comeFrom = request.getParameter("comeFrom");
	if (!StringUtils.isNull(comeFrom) && comeFrom.equals("DecryptUtil"))
	{
		session.setAttribute("treeViewSrc","decrypt");
	}
	try
	{
		String 	content = request.getParameter("info");
		String empty = "";
		String peakUsgDetails = "";
%>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>

<body>

<table cellpadding="4" cellspacing="0" border="0" align="center" width="100%">
	<tr>
		<td height="10px">&nbsp;</td>
	</tr>
	<tr>
		<td align="center" style="width:900px">
			<form name="license" method="POST" action="EgLicenseReport.jsp?&sessionInUse=true">
			<table width="900px" cellpadding="4" cellspacing="0" border="0" class="table_border">
				<tr>
					<td  class="eg_8bold table_header" height="20"> Please paste the content of license file from your mail </td>
				</tr>
				<tr>
					<td align="center"><textarea name="info" rows="20" id="info" style="width:880px"><%=(content==null)?empty:content%></textarea>
					</td>
				</tr>
				<tr>
					<td align="center"> <input type="submit" name="b1" value="Decrypt" class="eg_darkbutton"/>&nbsp;&nbsp;&nbsp; <input type="reset" name="b2" value="Clear" onclick="formreset();" class="eg_darkbutton" /> </td>
				</tr>
<%
		File file=null;
		com.eg.EgDecryptLicense license=new com.eg.EgDecryptLicense();
		EgTestInfo tokInfo = new EgTestInfo("manager");
		FileWriter fw =new FileWriter (EgInstallInfo.getInstallDir()+"/tmp/EgLicense.txt");
	
		if (content !=null && content.length()>0)
		{
			fw.write(content);
			file = new File(EgInstallInfo.getInstallDir()+"/tmp/EgLicense.txt");
			StringTokenizer line = new StringTokenizer(content, "\n");
			//String[] line= content.split("\n");
			while(line.hasMoreTokens())
			{
				String st=((String)line.nextToken()).trim();
				if(st.equals(""))
				{
					out.println("Remove extra empty space");  
					return;
				}
				String output=license.decrypt(st);
				if (output.startsWith("peakUsgMsg#"))
				{
					peakUsgDetails = output;
				}
				else if(output.startsWith("<tr>"))
				{
					zoneList.add(output);
				}
				else
				{
					list.add(output);
				}
			}
%>
			</table>
			</form>
		</td>
	</tr>
	
	<tr>
		<td align="center" style="width:900px">
			<table style="width:900px" cellpadding="4" cellspacing="0" border="0" class="table_border">
				<tr>
					<td colspan="2" class="eg_8bold table_header" height="20">eG Manager License Information </td>
				</tr>
<%
	if(list.size() > 0)
	{
		String data="";
		int locmark=0;
		for(int i=0;i<=30;i++)
		{
			data=((String)list.get(i)).trim();
			if(data.indexOf(":")!=-1 && !data.startsWith("<tr>"))
			{
				//String[] fist_tokens=data.split(":");
				//StringTokenizer  first_tokens= new StringTokenizer(data, ":");
				int index = data.indexOf(":");
				if(index > 0)
				{
					String info=(data.substring(0,index)).trim();
					String value=data.substring(index+1);
%>
			<tr>
				<td align="left" class="cell_border1"><%=info%></td>
				<td align="left" class="cell_border1"><%=value%></td>
			</tr>
<% 
				continue;
				}
			}
			if(data.startsWith("-----"))
			{
				locmark=i;
				break;
			}
		}
%>
						
	</table>
	</td>
	</tr>
	
	<tr>
		<td align="center" style="width:900px">
			<table style="width:900px" cellpadding="4" cellspacing="0" border="0" class="table_border">
			<tr>
				<td width="100%" align="center"  class="eg_8bold table_header" colspan="8"><egui:intnl text="eG Manager License Usage"/></td>
			</tr>
<%
			data=((String)list.get(locmark+1)).trim(); 
			//String[] second_tokens=data.split("\\s+");
			//out.println(data+":");
			StringTokenizer st_first = new StringTokenizer(data, " ");
			
			//out.println("size of : "+st_first.countTokens());
			StringTokenizer st = new StringTokenizer(data, " ");

			if(st_first.countTokens()>5)
			{
%>
				<tr>
<% 
				while (st.hasMoreTokens())
				{
					String key1 = ((String)st.nextToken()).trim();
					if(key1.equals("Not"))
					{
						String key2 = st.nextToken();
%>
						<td  class="eg_8bold table_header" height="20">
						<%=key1+" "+key2%>
						</td>
<%
					}
					else
					{
%>
						<td  class="eg_8bold table_header" height="20">
						<%=key1 %>
						</td>
<%
					}
				}
%>
				</tr>
<%
				for(int i=locmark+3;i<list.size()-1;i++)
				{
					data=((String)list.get(i)).trim();
					if(!data.startsWith("------"))
					{
						//String[] third_tokens=data.split("\\s+ ");
						
						ArrayList emptyColumnsList = tokInfo.egTokenizer(data, "  ");
						for(int j=0;j<emptyColumnsList.size();j++)
						{
							if(((String)emptyColumnsList.get(j)).equals(""))
								continue;
							if(((String)emptyColumnsList.get(j)).indexOf("Agentless")!=-1 ||((String)emptyColumnsList.get(j)).indexOf("Agents")!=-1)
							{
%>
								<td align="left" class="cell_border1"><p1>&nbsp;&nbsp;<%=emptyColumnsList.get(j)%></p1></td>
<%
								continue;
							}
							String egUI=EgInstallInfo.getInstallDir() + "/manager/config/eg_ui.ini";
							IniFile iniFile = new IniFile(egUI);
							String subject="";
							Vector compList=new Vector();
							compList.add("Citrix Users");
							compList.add("Terminal Users");
							compList.add("VDI Users");
							String temp=(String)emptyColumnsList.get(j);
							if(compList!=null && compList.contains(temp))
							{
%>
								<td class="cell_border1 cell_border2" style="padding: 4px 0px 4px 40px;" valign="top"><p1><egui:intnl type = "<%=temp%>" /> </p1></td>
<%
							}
							else
							{
								String val1 = (String)emptyColumnsList.get(j);
								String val ="";
								if(val1!= null&& val1.indexOf("$")>-1)
								{
									val=val1.substring(0,val1.indexOf("$"));
									String strDecimal="";
									if(val!=null && val.trim().indexOf(".")>-1)
									{
										strDecimal = val.substring(val.indexOf("."));
										val = val.substring(0,val.indexOf("."));
										%>
										 <td align='left' class='cell_border1 cell_border2'><span style='float:left;text-align:right;width:50%'><%=val%></span><span style='float:right;text-align:left;width:50%'><%=strDecimal%></span></td>
										 <%
									}
									else if(!val.trim().equalsIgnoreCase("-"))
									{
										String dec = ".0";
										%>
										<td align='left' class='cell_border1 cell_border2'><span style='float:left;text-align:right;width:50%'><%=val%></span><span style='float:right;text-align:left;width:50%'><%=dec%></span></td>
										<%
									}
									else
									{
										%>
										<td align='left' class='cell_border1 cell_border2'><span style='float:left;text-align:right;width:50%'><%=val%></span><span style='float:right;text-align:left;width:50%'></span></td>
										<%
									}
								}
								else
								{
%>
								<td align="left" class="cell_border1 cell_border2"><p1><%=emptyColumnsList.get(j)%></p1></td>
<%
								}
							}
						}
					}
%>
				</tr>
<%
						
					
				}
			}
			else if(st_first.countTokens()==4)
			{
 %>
				<tr>
					<td  class="eg_8bold table_header" height="20"> Details </td>
					<td  class="eg_8bold table_header" height="20"> Allowed </td>
					<td  class="eg_8bold table_header" height="20"> Used </td>
					<td  class="eg_8bold table_header" height="20"> Usage(%) </td>
				</tr>
<% 
				for(int i=locmark+3;i<list.size();i++)
				{
 					data=((String)list.get(i)).trim();   
 					if(!data.startsWith("------"))
					{
 						//String[] third_tokens=data.split("\\s+");
						ArrayList emptyColumnsList = tokInfo.egTokenizer(data, "\t");
						for(int j=0;j<emptyColumnsList.size();j++)
						{
							if(((String)emptyColumnsList.get(j)).equals(""))
								continue;
%>
							<td align="left" class="cell_border1"><p1><%=emptyColumnsList.get(j)%></p1></td>
<%
							
						}
%>
				</tr>
<%
					}
				}
			}
%>
			</table>
<%
			if (!peakUsgDetails.equals(""))
			{
				int hashIdx = peakUsgDetails.indexOf("#");
				if (hashIdx > -1)
				{
					peakUsgDetails = peakUsgDetails.substring(hashIdx+1);
				}
%>
				
				<table style="width:900px" cellpadding="4" cellspacing="0" border="0" class="table_border1">
					<tr>
						<td><%=peakUsgDetails%></td>
					</tr>
				</table>
<% 
			}
		}
	}
%>
		</td>
	</tr>
	<%
	if(zoneList.size()>0 )
	{
	%>
	<tr>
	<td align="center" style="width:900px">
	<table align="center" border="0" width="100%" class="table_border" cellpadding="0" cellspacing="0">
	<col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%">
	<%
		String zoneVal = (String)zoneList.get(0);
		if(zoneVal.contains("Segments"))
		{
	%>
		<td width="100%" align="center"  class="eg_8bold table_header" colspan="23"><egui:intnl text="License Usage for Zones"/></td>
		</tr>
	<%
		}
		else
		{
	%>
		<td width="100%" align="center"  class="eg_8bold table_header" colspan="11"><egui:intnl text="License Usage for Zones"/></td>
	<%
		}
		String zonedata="";
		for(int i=0;i<=(zoneLen+2);i++)
		{
			zonedata =((String)zoneList.get(i)).trim();
			if (zonedata.startsWith("<tr>") && zonedata.indexOf("-----") == -1)
			{
	%>
				<%=zonedata%>
	<%
			}
		}
%>
	<table  width="100%" cellpadding="4" cellspacing="0" border="0" class="table_border1">
	<tr>
	<td width="100%" align='left'>Note:'%' are based on total allowed monitors</td>
	</tr>
	</table>
	</table>
	</td>
	</tr>
	<%
	}
	%>

	<tr>
		<td height="10px">&nbsp;</td>
	</tr>
</table>
<%

	}
	catch(Exception e)
	{
		System.out.println("Decryption failed due to : Input data error {"+e.toString()+"}");
	}
	
%>

</body>
</html>