SobekCM.SobekCM_Page_Globals.SobekCM_Page_Globals C# (CSharp) Method

SobekCM_Page_Globals() public method

public SobekCM_Page_Globals ( bool isPostBack, string page_name ) : System
isPostBack bool
page_name string
return System
        public SobekCM_Page_Globals(bool isPostBack, string page_name)
        {
            // Pull out the http request
            HttpRequest request = HttpContext.Current.Request;

            // Get the base url
            string base_url = request.Url.AbsoluteUri.ToLower().Replace("sobekcm.aspx", "");
            if (base_url.IndexOf("?") > 0)
                base_url = base_url.Substring(0, base_url.IndexOf("?"));

            try
            {
                tracer = new Custom_Tracer();
                tracer.Add_Trace("SobekCM_Page_Globals.Constructor", "Starting");
                SobekCM_Database.Connection_String = UI_ApplicationCache_Gateway.Settings.Database_Connection.Connection_String;

                // If this is running on localhost, and in debug, set base directory to this one
            #if DEBUG
                if (base_url.IndexOf("localhost:") > 0)
                {
                    UI_ApplicationCache_Gateway.Settings.Servers.System_Base_URL = base_url;
                    UI_ApplicationCache_Gateway.Settings.Servers.Base_URL = base_url;

                    string baseDir = System.Web.HttpContext.Current.Server.MapPath("~");
                    UI_ApplicationCache_Gateway.Settings.Servers.Base_Directory = baseDir;
                    UI_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location = Path.Combine(baseDir, "mySobek", "InProcess");
                }
            #endif

                // Ensure the settings base directory is set correctly
                if ( String.IsNullOrEmpty(UI_ApplicationCache_Gateway.Settings.Servers.Base_Directory))
                {
                    string baseDir = System.Web.HttpContext.Current.Server.MapPath("~");
                    if ((baseDir.Length > 0) && (baseDir[baseDir.Length - 1] != '\\'))
                        baseDir = baseDir + "\\";
                    UI_ApplicationCache_Gateway.Settings.Servers.Base_Directory = baseDir;

                    SobekCM_Database.Set_Setting("Application Server Network", baseDir);
                }

                // Ensure the web server IP address is set correctly
                if (String.IsNullOrEmpty(UI_ApplicationCache_Gateway.Settings.Servers.SobekCM_Web_Server_IP))
                {
                    string ip = get_local_ip();
                    if (ip.Length > 0)
                    {
                        UI_ApplicationCache_Gateway.Settings.Servers.SobekCM_Web_Server_IP = ip;

                        SobekCM_Database.Set_Setting("SobekCM Web Server IP", ip);
                    }
                }

                // (TEMPORARY FOR UF)
                if (( !String.IsNullOrEmpty(UI_ApplicationCache_Gateway.Settings.System.System_Abbreviation)) && ( UI_ApplicationCache_Gateway.Settings.System.System_Abbreviation.IndexOf("UFDC") == 0))
                {
                    string baseDir = System.Web.HttpContext.Current.Server.MapPath("~");
                    UI_ApplicationCache_Gateway.Settings.Servers.Base_Directory = baseDir;
                }

                // Check that something is saved for the original requested URL (may not exist if not forwarded)
                if (!HttpContext.Current.Items.Contains("Original_URL"))
                    HttpContext.Current.Items["Original_URL"] = request.Url.ToString();
            }
            catch (Exception ee)
            {
                tracer.Add_Trace("SobekCM_Page_Globals.Constructor", "Exception caught around line 129: " + ee.Message);
                tracer.Add_Trace("SobekCM_Page_Globals.Constructor", ee.StackTrace);

                // Send to the dashboard
                if ((HttpContext.Current.Request.UserHostAddress == "127.0.0.1") || (HttpContext.Current.Request.UserHostAddress == HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"]) || (HttpContext.Current.Request.Url.ToString().IndexOf("localhost") >= 0))
                {
                    // Create an error message
                    string errorMessage = "Error caught while validating application state";
                    if ((UI_ApplicationCache_Gateway.Settings.Database_Connection == null) || (String.IsNullOrEmpty(UI_ApplicationCache_Gateway.Settings.Database_Connection.Connection_String)))
                    {
                        errorMessage = "No database connection string found!";
                        string configFileLocation = AppDomain.CurrentDomain.BaseDirectory + "config/sobekcm.xml";
                        try
                        {
                            if (!File.Exists(configFileLocation))
                            {
                                errorMessage = "Missing config/sobekcm.xml configuration file on the web server.<br />Ensure the configuration file 'sobekcm.xml' exists in a 'config' subfolder directly under the web application.<br />Example configuration is:" +
                                               "<div style=\"background-color: #bbbbbb; margin-left: 30px; margin-top:10px; padding: 3px;\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;  ?&gt;<br /> &lt;configuration&gt;<br /> &nbsp; &nbsp &lt;connection_string type=&quot;MSSQL&quot;&gt;data source=localhost\\instance;initial catalog=SobekCM;integrated security=Yes;&lt;/connection_string&gt;<br /> &nbsp; &nbsp &lt;error_emails&gt;[email protected]&lt;/error_emails&gt;<br /> &nbsp; &nbsp &lt;error_page&gt;http://ufdc.ufl.edu/error.html&lt;/error_page&gt;<br />&lt;/configuration&gt;</div>";
                            }
                        }
                        catch
                        {
                            errorMessage = "No database connection string found.<br />Likely an error reading the configuration file due to permissions on the web server.<br />Ensure the configuration file 'sobekcm.xml' exists in a 'config' subfolder directly under the web application.<br />Example configuration is:" +
                                           "<div style=\"background-color: #bbbbbb; margin-left: 30px; margin-top:10px; padding: 3px;\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;  ?&gt;<br /> &lt;configuration&gt;<br /> &nbsp; &nbsp &lt;connection_string type=&quot;MSSQL&quot;&gt;data source=localhost\\instance;initial catalog=SobekCM;integrated security=Yes;&lt;/connection_string&gt;<br /> &nbsp; &nbsp &lt;error_emails&gt;[email protected]&lt;/error_emails&gt;<br /> &nbsp; &nbsp &lt;error_page&gt;http://ufdc.ufl.edu/error.html&lt;/error_page&gt;<br />&lt;/configuration&gt;</div>";
                        }
                    }
                    else
                    {
                        if (ee.Message.IndexOf("The EXECUTE permission") >= 0)
                        {
                            errorMessage = "Permissions error while connecting to the database and pulling necessary data.<br /><br />Confirm the following:<ul><li>IIS is configured correctly to use anonymous authentication</li><li>Anonymous user (or service account) is part of the sobek_users role in the database.</li></ul>";
                        }
                        else
                        {
                            errorMessage = "Error connecting to the database and pulling necessary data.<br /><br />Confirm the following:<ul><li>Database connection string is correct ( " + UI_ApplicationCache_Gateway.Settings.Database_Connection.Connection_String + ")</li><li>IIS is configured correctly to use anonymous authentication</li><li>Anonymous user (or service account) is part of the sobek_users role in the database.</li></ul>";
                        }
                    }
                    // Wrap this into the SobekCM Exception
                    SobekCM_Traced_Exception newException = new SobekCM_Traced_Exception(errorMessage, ee, tracer);

                    // Save this to the session state, and then forward to the dashboard
                    HttpContext.Current.Session["Last_Exception"] = newException;
                    HttpContext.Current.Response.Redirect("dashboard.aspx", false);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    return;
                }
                else
                {
                    throw ee;
                }
            }

            tracer.Add_Trace("SobekCM_Page_Globals.Constructor", "About to parse the URL for the navigation object");

            // Analyze the response and get the mode
            try
            {
                currentMode = new Navigation_Object();
                NameValueCollection queryString = request.QueryString;

                QueryString_Analyzer.Parse_Query(queryString, currentMode, base_url, request.UserLanguages, UI_ApplicationCache_Gateway.Aggregations, UI_ApplicationCache_Gateway.Collection_Aliases, UI_ApplicationCache_Gateway.Items, UI_ApplicationCache_Gateway.URL_Portals, UI_ApplicationCache_Gateway.WebContent_Hierarchy, tracer);

                currentMode.Base_URL=base_url;
                currentMode.isPostBack = isPostBack;
                currentMode.Browser_Type = request.Browser.Type.ToUpper();
                currentMode.Set_Robot_Flag(request.UserAgent, request.UserHostAddress);
            }
            catch  ( Exception ee )
            {
                tracer.Add_Trace("SobekCM_Page_Globals.Constructor", "Exception caught around line 198: " + ee.Message);
                tracer.Add_Trace("SobekCM_Page_Globals.Constructor", ee.StackTrace);

                HttpContext.Current.Response.Status = "301 Moved Permanently";
                HttpContext.Current.Response.AddHeader("Location", base_url);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                return;
            }

            tracer.Add_Trace("SobekCM_Page_Globals.Constructor", "Navigation parse completed");

            // If this was for HTML, but was at the data, just convert to XML
            if ((page_name == "SOBEKCM_DATA") && (currentMode.Writer_Type != Writer_Type_Enum.XML) && (currentMode.Writer_Type != Writer_Type_Enum.JSON) && (currentMode.Writer_Type != Writer_Type_Enum.DataSet) && (currentMode.Writer_Type != Writer_Type_Enum.Data_Provider))
                currentMode.Writer_Type = Writer_Type_Enum.XML;

            tracer.Add_Trace("SobekCM_Page_Globals.Constructor", "Navigation Object created from URI query string");

            try
            {
                // If this was an error, redirect now
                if (currentMode.Mode == Display_Mode_Enum.Error)
                {
                    return;
                }

                // All the user stuff can be skipped if this was from a robot
                if (!currentMode.Is_Robot)
                {
                    // Determine which IP Ranges this IP address belongs to, if not already determined.
                    if (HttpContext.Current.Session["IP_Range_Membership"] == null)
                    {
                        string userAddress = request.UserHostAddress;
            #if DEBUG
                        // If in DEBUG mode, and it is the loopback IP address, use a local IP
                        if (userAddress == "::1")
                        {
                            try
                            {
                                var host = Dns.GetHostEntry(Dns.GetHostName());
                                foreach (var ip in host.AddressList)
                                {
                                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                                    {
                                        userAddress = ip.ToString();
                                        break;
                                    }
                                }
                            }
                            catch { }

                        }
            #endif
                        int ip_mask = UI_ApplicationCache_Gateway.IP_Restrictions.Restrictive_Range_Membership(userAddress);
                        HttpContext.Current.Session["IP_Range_Membership"] = ip_mask;
                    }

                    // Set the Session TOC, if provided
                    if (currentMode.TOC_Display != TOC_Display_Type_Enum.Undetermined)
                    {
                        if (currentMode.TOC_Display == TOC_Display_Type_Enum.Hide)
                        {
                            HttpContext.Current.Session["Show TOC"] = false;
                        }
                        else
                        {
                            HttpContext.Current.Session["Show TOC"] = true;
                        }
                    }

                    // Only do any of the user stuff if this is from the main SobekCM page
                    if (page_name == "SOBEKCM")
                    {
                        tracer.Add_Trace("SobekCM_Page_Globals.Constructor", "Checking for logged on user by cookie or session");
                        perform_user_checks(isPostBack);
                    }

                    // If this is a system admin, they can run as a different user actually
                    if ((currentUser != null) && (currentUser.Is_System_Admin) && (request.QueryString["userid"] != null))
                    {
                        try
                        {
                            int userid = Convert.ToInt32(request.QueryString["userid"]);
                            User_Object mirroredUser = Engine_Database.Get_User(userid, tracer);
                            if (mirroredUser != null)
                            {
                                // Replace the user information in the session state
                                HttpContext.Current.Session["user"] = mirroredUser;
                                currentUser = mirroredUser;
                            }
                        }
                        catch (Exception)
                        {
                            // Nothing to do here.. shouldn't ever really be here..
                        }
                    }

                    if (currentMode.Request_Completed)
                        return;

                    // If this was a call for RESET, clear the memory
                    if ((currentMode.Mode == Display_Mode_Enum.Administrative) && (currentMode.Admin_Type == Admin_Type_Enum.Reset))
                    {
                        Reset_Memory();

                        // Since this reset, send to the admin, memory management portion
                        currentMode.Mode = Display_Mode_Enum.Internal;
                        currentMode.Internal_Type = Internal_Type_Enum.Cache;
                    }
                }
                else // THIS IS A ROBOT REQUEST
                {
                    Perform_Search_Engine_Robot_Checks(currentMode, request.QueryString);
                }

                // Always pull TOP level collection
                SobekEngineClient.Aggregations.Get_Aggregation("all", currentMode.Language, UI_ApplicationCache_Gateway.Settings.System.Default_UI_Language, tracer);

                // If this is for a public folder, get the data
                if (currentMode.Mode == Display_Mode_Enum.Public_Folder)
                {
                    Public_Folder();
                }

                // Was this a robot?
                if (currentMode.Request_Completed)
                    return;

                // Run the search if this should be done now
                if (currentMode.Mode == Display_Mode_Enum.Results)
                {
                    Search_Block();
                }

                if (currentMode.Mode == Display_Mode_Enum.My_Sobek)
                {
                    MySobekCM_Block();
                }

                // Run the simple text block if this is that mode
                if (currentMode.Mode == Display_Mode_Enum.Simple_HTML_CMS)
                {
                    Simple_Web_Content_Text_Block();
                }
            }
            catch (OutOfMemoryException ee)
            {
                if (currentMode != null)
                {
                    currentMode.Mode = Display_Mode_Enum.Error;
                    currentMode.Error_Message = "Out of memory exception caught";
                    currentMode.Caught_Exception = ee;
                }
                else
                {
                    Email_Information("Fatal Out of memory exception caught", ee);
                }
            }
            catch (Exception ee)
            {
                if (currentMode != null)
                {
                    currentMode.Mode = Display_Mode_Enum.Error;
                    currentMode.Error_Message = "Unknown error occurred";
                    currentMode.Caught_Exception = ee;
                }
                else
                {
                    Email_Information("Unknown Fatal Error Occurred", ee);
                }
            }
        }