<%@ WebHandler language="C#" class="SLXWebReportingServer" %> using System; using System.Web; using System.Web.SessionState; using Interop.SLXWR; using System.Runtime.InteropServices; using log4net; using Sage.SalesLogix.Reporting.Server; using System.Data.OleDb; using System.Data; using System.Xml; using System.IO; using Sage.Platform; using Sage.SalesLogix; public class SLXWebReportingServer: IHttpHandler, IRequiresSessionState { private static readonly ILog LOG = LogManager.GetLogger(typeof(SLXWebReportingServer)); // Methods private static bool GetConnectionString(string userName, string password, string timeZoneKey, out string connectionString, out string dataSource, out string errorMsg) { LOG.Info("GetConnectionString - 1"); object obj2; connectionString = null; dataSource = null; errorMsg = null; string physicalApplicationPath = HttpContext.Current.Request.PhysicalApplicationPath; if (string.IsNullOrEmpty(physicalApplicationPath)) { errorMsg = "ERR_APPLICATIONPATH"; return false; } LOG.Info("GetConnectionString - 2"); string path = Path.Combine(physicalApplicationPath, "connection.config"); if (!File.Exists(path)) { errorMsg = "ERR_CONNECTIONCONFIG_NOTFOUND"; return false; } LOG.Info("GetConnectionString - 3"); XmlDocument document = new XmlDocument(); try { document.Load(path); } catch (Exception exception) { errorMsg = string.Format("ERR_CONNECTIONCONFIG_LOAD", exception.Message); return false; } LOG.Info("GetConnectionString - 4"); XmlNodeList elementsByTagName = document.GetElementsByTagName("ConString"); if (!elementsByTagName.Count.Equals(1)) { errorMsg = "ERR_CONNECTIONCONFIG_COUNT"; return false; } OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(elementsByTagName[0].InnerText); builder["User Id"] = userName; builder["Password"] = password; if (builder.TryGetValue("Data Source", out obj2)) { dataSource = obj2.ToString(); } else { errorMsg = "ERR_DATASOURCE"; return false; } LOG.Info("GetConnectionString - 5"); string str3 = builder["Extended Properties"].ToString(); string rWPassword = GetRWPassword(builder.ConnectionString); LOG.Info("GetConnectionString - Got RW Pass: " + rWPassword ); if (!string.IsNullOrEmpty(rWPassword)) { str3 = str3 + ";RWPass=" + rWPassword; builder["Extended Properties"] = str3; } if (str3.IndexOf("IncludeCalcFields", StringComparison.OrdinalIgnoreCase) <= 0) { str3 = str3 + ";IncludeCalcFields=ON"; builder["Extended Properties"] = str3; } if (str3.IndexOf("AUTOINCBATCHSIZE", StringComparison.OrdinalIgnoreCase) <= 0) { str3 = str3 + ";AUTOINCBATCHSIZE=1"; builder["Extended Properties"] = str3; } if (str3.IndexOf("CASEINSENSITIVEFIND", StringComparison.OrdinalIgnoreCase) <= 0) { str3 = str3 + ";CASEINSENSITIVEFIND=ON"; builder["Extended Properties"] = str3; } connectionString = builder.ConnectionString; LOG.Info("GetConnectionString - Got connection string: " + connectionString); if (!string.IsNullOrEmpty(timeZoneKey) && connectionString.Contains("TIMEZONE=NONE")) { try { Sage.Platform.TimeZone zone = new TimeZones().FindTimeZone(timeZoneKey, TimeZones.TZFindType.ftKeyName); if (zone != null) { int timeZoneIndex = zone.TimeZoneIndex; if (timeZoneIndex >= 0) { connectionString = connectionString.Replace("TIMEZONE=NONE", string.Format("TIMEZONE={0}", timeZoneIndex)); } } } catch (Exception exception2) { errorMsg = string.Format("ERR_TIMEZONE", exception2.Message); return false; } } LOG.Info("GetConnectionString - Returning - " + connectionString); return true; } private static bool GetDatabaseType(string connectionString, out DatabaseServer databaseType, out string errorMsg) { databaseType = DatabaseServer.dsMSSQL; errorMsg = null; if (!string.IsNullOrEmpty(connectionString)) { string str = null; using (OleDbConnection connection = new OleDbConnection(connectionString)) { using (OleDbCommand command = new OleDbCommand("call slx_GetNativeConnInfo", connection)) { connection.Open(); try { OleDbDataReader reader = command.ExecuteReader(); try { if (reader.HasRows && reader.Read()) { str = reader.GetString(0); } } finally { reader.Close(); reader.Dispose(); } } finally { connection.Close(); } } } if (!string.IsNullOrEmpty(str)) { object obj2; OleDbConnectionStringBuilder builder2 = new OleDbConnectionStringBuilder(); builder2.ConnectionString = str; OleDbConnectionStringBuilder builder = builder2; if (builder.TryGetValue("PROVIDER", out obj2) && (obj2 != null)) { string str2 = obj2.ToString().Trim(); if (!string.IsNullOrEmpty(str2)) { if (str2.StartsWith("ORAOLEDB", StringComparison.OrdinalIgnoreCase) || str2.StartsWith("MSDAORA", StringComparison.OrdinalIgnoreCase)) { databaseType = DatabaseServer.dsOracle; } return true; } } } } return false; } private static string GetRWPassword(string connectionString) { string str = null; LOG.Info("GetRWPassword - " + connectionString); using (OleDbConnection connection = new OleDbConnection(connectionString)) { using (OleDbCommand command = new OleDbCommand("SELECT RWPASS FROM SYSTEMINFO WHERE SYSTEMINFOID = 'PRIMARY'", connection)) { connection.Open(); try { OleDbDataReader reader = command.ExecuteReader(); try { if (reader.HasRows && reader.Read()) { str = reader["RWPASS"].ToString(); if (!string.IsNullOrEmpty(str)) { str = SLXEncryption.Decrypt(str); } } return str; } finally { reader.Close(); reader.Dispose(); } } finally { connection.Close(); } return str; } } } private static string GetUserId(string connectionString, string userCode) { string str = null; using (OleDbConnection connection = new OleDbConnection(connectionString)) { using (OleDbCommand command = new OleDbCommand(string.Format("SELECT USERID FROM USERSECURITY WHERE USERCODE = '{0}'", userCode), connection)) { connection.Open(); try { OleDbDataReader reader = command.ExecuteReader(); try { if (reader.HasRows && reader.Read()) { str = reader["USERID"].ToString().Trim(); } return str; } finally { reader.Close(); reader.Dispose(); } } finally { connection.Close(); } return str; } } } private static void HandleResponse(HttpContext context, WebReportingMethod method, bool wasError, string userName, string pwdEncrypted, bool ssl, string responseData) { if (string.IsNullOrEmpty(responseData) || !context.Response.IsClientConnected) { context.Response.StatusCode = 500; context.Response.StatusDescription = string.Format("ERR_WEBREPORTINGMETHOD", method); } else { switch (method) { case WebReportingMethod.GenerateReport: { string str = ssl.Equals(false) ? "http" : "https"; Uri uri = new Uri(context.Request.Url.AbsoluteUri); string str2 = uri.Port.Equals(-1) ? uri.Host : (uri.Host + ":" + uri.Port); string str3 = wasError.Equals(false) ? "SLXWebReporting.aspx" : "ErrorPage.aspx"; string url = string.Format("{0}://{1}/slxwebrpt/{2}?", str, str2, str3); if (wasError) { url = url + string.Format("msg={0}", responseData); context.Response.Redirect(url, false); return; } url = url + string.Format("rpt={0}&User={1}&Pwd={2}", responseData, userName, pwdEncrypted); context.Response.Redirect(url, false); return; } case WebReportingMethod.GetReportTables: context.Response.Clear(); context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetNoStore(); context.Response.Cache.SetExpires(DateTime.MinValue); context.Response.ContentType = "text/xml"; context.Response.AppendHeader("Content-Length", responseData.Length.ToString()); context.Response.Write(responseData); return; } } } public void ProcessRequest(HttpContext context) { try { LOG.Info("Entering ProcessRequest"); string str16; string str17; string str18; DatabaseServer server; if (!context.Request.HttpMethod.ToUpper().Equals("POST")) { goto Label_053F; } string str = context.Request.Params["method"]; if (string.IsNullOrEmpty(str)) { context.Response.StatusCode = 400; context.Response.StatusDescription = "ERR_METHODUNDEFINED"; return; } LOG.Info("ProcessRequest - 1"); if (Enum.IsDefined(typeof(WebReportingMethod), str).Equals(false)) { context.Response.StatusCode = 400; return; } LOG.Info("ProcessRequest - 2"); WebReportingMethod method = (WebReportingMethod) Enum.Parse(typeof(WebReportingMethod), str); string keyField = string.Empty; string recordSelection = string.Empty; string sortDirections = string.Empty; string sortFields = string.Empty; string sQLSelect = string.Empty; string sQLWhere = string.Empty; bool sSL = false; bool forceSQL = false; string password = string.Empty; string str9 = context.Request.Params["pwd"]; if (!string.IsNullOrEmpty(str9) && (str9.Length > 2)) { password = SLXEncryption.Decrypt(str9); } LOG.Info("ProcessRequest - 3"); string str10 = context.Request.Params["pluginid"]; if (string.IsNullOrEmpty(str10)) { context.Response.StatusCode = 400; context.Response.StatusDescription = "ERR_PLUGINID"; LOG.Warn("Return - PluginID"); return; } string str11 = context.Request.Params["username"]; if (string.IsNullOrEmpty(str11)) { context.Response.StatusCode = 400; context.Response.StatusDescription = "ERR_USERNAME"; LOG.Warn("Return - UserName"); return; } string timeZoneKey = context.Request.Params["timezone"]; if (method.Equals(WebReportingMethod.GenerateReport)) { string[] strArray = new string[] { "sqlqry", "keyfield", "wsql", "rsf", "forcesql", "sortfields", "sortdirections", "ss" }; foreach (string str13 in strArray) { bool flag3 = false; foreach (object obj2 in context.Request.Params) { if (str13.Equals(obj2.ToString(), StringComparison.OrdinalIgnoreCase)) { flag3 = true; break; } } if (flag3.Equals(false)) { context.Response.StatusCode = 400; context.Response.StatusDescription = string.Format("ERR_EXPECTED_PARAMETER", str13); return; } } keyField = context.Request.Params["keyfield"]; recordSelection = context.Request.Params["rsf"]; sQLWhere = context.Request.Params["wsql"]; sQLSelect = context.Request.Params["sqlqry"]; sortFields = context.Request.Params["sortfields"]; sortDirections = context.Request.Params["sortdirections"]; string str14 = context.Request.Params["ss"]; sSL = !string.IsNullOrEmpty(str14) ? str14.Equals("1") : false; string str15 = context.Request.Params["forcesql"]; forceSQL = !string.IsNullOrEmpty(str15) ? str15.Equals("1") : false; } LOG.Info("ProcessRequest - 4"); if (!GetConnectionString(str11, password, timeZoneKey, out str16, out str17, out str18)) { LOG.Info("ProcessRequest - 4a"); context.Response.StatusCode = 500; if (string.IsNullOrEmpty(str18)) { str18 = "ERR_UNKNOWN"; } context.Response.StatusDescription = string.Format("ERR_CONNECTIONSTRING", str18); LOG.Warn("Return - Connection String"); return; } LOG.Info("ProcessRequest - 5"); if (!GetDatabaseType(str16, out server, out str18)) { context.Response.StatusCode = 500; if (string.IsNullOrEmpty(str18)) { str18 = "ERR_UNKNOWN"; } context.Response.StatusDescription = string.Format("ERR_DATABASETYPE", str18); LOG.Warn("Return - Unknown"); return; } LOG.Info("ProcessRequest - 6"); LOG.Info("GEtting report.."); string userId = GetUserId(str16, str11); string message = string.Empty; bool wasError = false; WebReportingClass o = new WebReportingClass(); try { string physicalApplicationPath = HttpContext.Current.Request.PhysicalApplicationPath; switch (method) { case WebReportingMethod.GenerateReport: try { message = o.GenerateReport(str16, str17, server, physicalApplicationPath, forceSQL, sSL, keyField, password, str10, recordSelection, sortDirections, sortFields, sQLSelect, sQLWhere, userId, str11); } catch (Exception exception) { wasError = true; message = exception.Message; } goto Label_04FB; case WebReportingMethod.GetReportTables: break; default: goto Label_04FB; } try { message = o.GetReportTables(str16, str17, server, physicalApplicationPath, password, str10, userId, str11); } catch (Exception e) { wasError = true; message = "0"; } } finally { Marshal.ReleaseComObject(o); } Label_04FB: LOG.Info("Label_04FB"); if (!string.IsNullOrEmpty(message)) { HandleResponse(context, method, wasError, str11, str9, sSL, message); } else { context.Response.StatusCode = 500; context.Response.StatusDescription = string.Format("ERR_WEBREPORTINGMETHOD", str); } return; Label_053F: context.Response.StatusCode = 0x195; } catch (Exception exception2) { LOG.Warn("Error in top level try/catch", exception2); context.Response.StatusCode = 500; context.Response.StatusDescription = exception2.Message; } } // Properties public bool IsReusable { get { return true; } } }