// OnClick for Google Geo Coords
    protected void btnSubmitGeoCoords_Click(Object sender, EventArgs e)
    {
        // Connect to service.
        AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");

        string lat, lon;

        // Catch any errors.
        try
        {
            lat = myProxy.GetLatitude(txtInputGeoCoords.Text);
            lon = myProxy.GetLongitude(txtInputGeoCoords.Text);
        }
        catch (Exception ex)
        {
            lat = lon = ex.ToString();
        }
        // Make sure to close connection regardless of whether errors were thrown.
        finally
        {
            myProxy.Close();
        }

        // Update label.
        if (lat != "" && lon != "")
        {
            lblOutputGeoCoords.Text =
                "Latitude:  " + lat + "<br/>" +
                "Longitude: " + lon;
        }
        else
        {
            lblOutputGeoCoords.Text = "No data for this location. Please try another location.";
        }
    }
 protected void btnEncrypt_Click(object sender, EventArgs e)
 {
     string input = txtEncryptDecrypt.Text;
     if (input != "")
     {
         AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");
         string result = myProxy.Encrypt(input);
         myProxy.Close();
         txtEncryptDecrypt.Text = result;
     }
     else
     {
         txtEncryptDecrypt.Text = "Error: Invalid Input";
     }
 }
    // OnClick for Weather Service
    protected void btnSubmitWeather_Click(Object sender, EventArgs e)
    {
        // Connect to service.
        AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");

        string[] result = new string[6];

        // Catch any errors.
        try
        {
            result = myProxy.Weather5Day(txtInputWeather.Text);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            result[0] = ex.ToString();
            for (int i = 1; i < result.Count(); i++)
            {
                result[i] = "";
            }
        }
        // Make sure to close connection regardless of whether errors were thrown.
        finally
        {
            myProxy.Close();
        }

        // Build the data string.
        string concat = "";
        for (int i = 0; i < result.Count(); i++)
        {
            // Do some HTML/ASP formatting.
            result[i] = result[i].Replace("\n", "<br/>");
            concat += result[i] + "<br/><br/>";
        }

        // Update label.
        lblOutputWeather.Text = concat;
    }
    // OnClick for Solar Service
    protected void btnSubmitSolar_Click(Object sender, EventArgs e)
    {
        // Connect to service.
        AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");

        double result;

        // Catch any errors.
        try
        {
            result = myProxy.SolarIntensity(txtInputSolar.Text);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            result = -1;
        }
        // Make sure to close connection regardless of whether errors were thrown.
        finally
        {
            myProxy.Close();
        }

        // Update label.
        if (result != -1)
        {
            lblOutputSolar.Text = result + " kWh/m^2/day";
        }
        else
        {
            lblOutputSolar.Text = "No data for this location. Please try another location.";
        }
    }
    // OnClick for Guardian News Search Service
    protected void btnSubmitQuery_Click(Object sender, EventArgs e)
    {
        // Connect to service.
        AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");

        string[] result;

        // Catch any errors.
        try
        {
            result = myProxy.SearchNews(txtInputQuery.Text);
        }
        catch (Exception ex)
        {
            result = new string[5];
            result[0] = ex.ToString();
        }
        // Make sure to close connection regardless of whether errors were thrown.
        finally
        {
            myProxy.Close();
        }

        // Build the data string.

        string concat = "";

        for (int i = 0; i < result.Count(); i++)
        {
            string cur = result[i];
            string[] fields = new string[5];

            for (int j = 0; j < 5; j++)
            {
                fields[j] = cur.Substring(0, cur.IndexOf('\n'));
                cur = cur.Substring(cur.IndexOf("\n") + 1);
            }
            concat = concat +
                fields[2] + " - " + fields[3] + "<br/>" +
                "<a href=\"" + fields[1] + "\">" + fields[0] + "</a>";

            // Do some HTML/ASP formatting.
            concat += "<br/><br/>";
        }

        // Update label.
        lblOutputQueryResults.Text = concat;
    }
    // OnClick for Google Points Of Interest
    protected void btnSubmitPOI_Click(Object sender, EventArgs e)
    {
        string[] results;

        // Connect to service.
        AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");

        // Catch any errors.
        try
        {
            results = myProxy.PointsOfInterest(txtInputPOILoc.Text, txtInputPOIKey.Text);
        }
        catch (Exception ex)
        {
            results = new string[1];
            results[0] = ex.ToString();
            lblOutputPOI.Text = ex.ToString();
            return;
        }
        // Make sure to close connection regardless of whether errors were thrown.
        finally
        {
            myProxy.Close();
        }

        string concat = "";

        for (int i = 0; i < results.Count(); i++)
        {
            string cur = results[i];
            string[] fields = new string[5];

            for (int j = 0; j < 5; j++)
            {
                fields[j] = cur.Substring(0, cur.IndexOf('\n'));
                cur = cur.Substring(cur.IndexOf('\n') + 1);
            }
            concat = concat +
                fields[0] + "<br />" +
                fields[1] + "<br />" +
                "<a href=\"https://www.google.com/maps/place/" + fields[0].Replace(" ", "+") + "/@" + fields[2] + "," + fields[3] + ",15z" + "\">" + "Google Maps" + "</a>" + "<br />" +
                fields[4].ToUpper();

            // Do some HTML/ASP formatting.
            concat += "<br/><br/>";
        }

        lblOutputPOI.Text = concat;
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        // Invalidate any current login.
        Session["name"] = null;
        Session["username"] = null;
        Session["admin"] = null;
        Session["staff"] = null;

        string membersPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\Members.xml");

        XmlDocument membersDoc = new XmlDocument();

        try
        {
            membersDoc.Load(membersPath);
        }
        catch (Exception ex)
        {
            return;
        }

        string username = txtUsername.Text;
        string password = txtPassword.Text;

        AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");
        string encryptedUsername = myProxy.Encrypt(username);
        string encryptedPassword = myProxy.Encrypt(password);
        myProxy.Close();

        XmlNodeList members = membersDoc.SelectNodes("Members/Member");

        // Check if member exists and password is valid.

        Boolean exists = false;
        for (int i = 0; i < members.Count; i++)
        {
            string user = members[i].ChildNodes.Item(1).InnerText;
            if (user.Equals(encryptedUsername))
            {
                exists = true;

                if (members[i].ChildNodes.Item(2).InnerText == encryptedPassword)
                {
                    /* PERFORM LOGIN STUFF HERE. USERNAME AND PASSWORD MATCHES. */
                    myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");
                    string name = myProxy.Decrypt(members[i].ChildNodes.Item(0).InnerText);

                    Session["name"] = (object) name;
                    Session["username"] = (object) username;

                    // Load staff XML and see if user is staff member.

                    string staffPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\Staff.xml");

                    XmlDocument staffDoc = new XmlDocument();

                    try
                    {
                        staffDoc.Load(staffPath);
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    XmlNodeList staff = staffDoc.SelectNodes("Staff/Member");

                    for (int j = 0; j < staff.Count; j++)
                    {
                        if (staff[j].ChildNodes.Item(0).InnerText.Equals(encryptedUsername))
                        {
                            if (staff[j].ChildNodes.Item(1).InnerText == "true")
                            {
                                Session["admin"] = (object) true;
                            }
                            if (staff[j].ChildNodes.Item(2).InnerText == "true")
                            {
                                Session["staff"] = (object) true;
                            }
                            break;
                        }
                    }

                    Update();

                    statusMessage.Text = "Successful Login!";

                    Server.Transfer("~/Members/Members.aspx");
                }
                else
                {
                    statusMessage.Text = "Error: Password incorrect.";
                }

                break;
            }
        }

        if (!exists)
        {
            statusMessage.Text = "Error: Username does not exist.";
        }
    }
    protected void btnCreateAccount_Click(object sender, EventArgs e)
    {
        string path = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\Members.xml");

        XmlDocument doc = new XmlDocument();

        try
        {
            doc.Load(path);
        }
        catch (Exception ex)
        {
            return;
        }

        string name = txtFullName.Text;
        string username = txtUsername.Text;
        string password = txtPassword.Text;

        if (username.Length < 6 || password.Length < 6 || name.Length == 0)
        {
            statusMessage.Text = "Error: Username/Password must be at least 6 characters, and name must not be empty.";
            return;
        }

        AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");
        string encryptedName = myProxy.Encrypt(name);
        string encryptedUsername = myProxy.Encrypt(username);
        string encryptedPassword = myProxy.Encrypt(password);
        myProxy.Close();

        XmlNodeList members = doc.SelectNodes("Members/Member");

        Boolean exists = false;
        for (int i = 0; i < members.Count; i++)
        {
            string user = members[i].ChildNodes.Item(1).InnerText;
            if (user.Equals(encryptedUsername))
            {
                exists = true;
                break;
            }
        }

        if (!exists)
        {
            XmlNode root = doc.SelectSingleNode("Members");

            XmlElement newMember = doc.CreateElement("Member");

            XmlElement newName = doc.CreateElement("Name");
                XmlText nameText = doc.CreateTextNode(encryptedName);
                newName.AppendChild(nameText);

            XmlElement newUsername = doc.CreateElement("Username");
                XmlText usernameText = doc.CreateTextNode(encryptedUsername);
                newUsername.AppendChild(usernameText);

            XmlElement newPassword = doc.CreateElement("Password");
                XmlText passwordText = doc.CreateTextNode(encryptedPassword);
                newPassword.AppendChild(passwordText);

            newMember.AppendChild(newName);
            newMember.AppendChild(newUsername);
            newMember.AppendChild(newPassword);

            root.AppendChild(newMember);

            doc.Save(path);

            statusMessage.Text = "Success. Now go Home and login.";
            Server.Transfer("./Login.aspx");
        }

        else
        {
            statusMessage.Text = "Username already exists. Please choose another.";
        }
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        string username = txtUsername.Text;
        if (username == "admin")
        {
            lblResult.Text = "Error: Cannot edit Admin permissions.";
        }
        else if (username == (string)Session["username"])
        {
            lblResult.Text = "Error: Cannot edit your own permissions.";
        }
        else
        {

            string membersPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\Members.xml");
            string staffPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\Staff.xml");

            XmlDocument membersDoc = new XmlDocument();
            XmlDocument staffDoc = new XmlDocument();

            try
            {
                membersDoc.Load(membersPath);
                staffDoc.Load(staffPath);
            }
            catch (Exception ex)
            {
                lblResult.Text = "Error: Could not load members/staff XML files.";
                return;
            }

            AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");
            string encryptedUsername = myProxy.Encrypt(username);
            myProxy.Close();

            XmlNodeList members = membersDoc.SelectNodes("Members/Member");

            bool memberExists = false;
            bool isStaff = false;

            for (int i = 0; i < members.Count; i++)
            {
                string user = members[i].ChildNodes.Item(1).InnerText;
                if (user.Equals(encryptedUsername))
                {
                    memberExists = true;

                    XmlNodeList staff = staffDoc.SelectNodes("Staff/Member");

                    for (int j = 0; j < staff.Count; j++)
                    {
                        if (staff[j].ChildNodes.Item(0).InnerText.Equals(encryptedUsername))
                        {
                            isStaff = true;

                            // Staff member should be deleted.
                            if (!checkAdmin.Checked && !checkStaff.Checked)
                            {
                                XmlNode node = staffDoc.SelectSingleNode("Staff/Member[Username='******']");
                                node.ParentNode.RemoveChild(node);
                                staffDoc.Save(staffPath);
                                lblResult.Text = "Staff member removed.";
                            }
                            // Staff member should be updated.
                            else
                            {
                                if (checkAdmin.Checked)
                                {
                                    staff[j].ChildNodes.Item(1).InnerText = "true";
                                }
                                else
                                {
                                    staff[j].ChildNodes.Item(1).InnerText = "false";
                                }
                                if (checkStaff.Checked)
                                {
                                    staff[j].ChildNodes.Item(2).InnerText = "true";
                                }
                                else
                                {
                                    staff[j].ChildNodes.Item(2).InnerText = "false";
                                }
                                staffDoc.Save(staffPath);
                                lblResult.Text = "Staff member permissions updated.";
                            }

                            break;
                        } // end if (staff member found)
                    } // End staff for loop
                }// end if (member was found)
            } // End members for loop

            if (!memberExists)
            {
                lblResult.Text = "Error: Username does not exist and cannot be modified.";
            }

            if (memberExists && !isStaff)
            {

                if (checkAdmin.Checked || checkStaff.Checked)
                {
                    XmlNode root = staffDoc.SelectSingleNode("Staff");

                    XmlElement newStaff = staffDoc.CreateElement("Member");

                    XmlElement newUsername = staffDoc.CreateElement("Username");
                    XmlText usernameText = staffDoc.CreateTextNode(encryptedUsername);
                    newUsername.AppendChild(usernameText);

                    XmlElement adminAuthorized = staffDoc.CreateElement("AdminAuthorized");
                    XmlText adminText = staffDoc.CreateTextNode(checkAdmin.Checked.ToString().ToLower());
                    adminAuthorized.AppendChild(adminText);

                    XmlElement staffAuthorized = staffDoc.CreateElement("StaffAuthorized");
                    XmlText staffText = staffDoc.CreateTextNode(checkStaff.Checked.ToString().ToLower());
                    staffAuthorized.AppendChild(staffText);

                    newStaff.AppendChild(newUsername);
                    newStaff.AppendChild(adminAuthorized);
                    newStaff.AppendChild(staffAuthorized);

                    root.AppendChild(newStaff);

                    staffDoc.Save(staffPath);

                    lblResult.Text = "Member promoted to staff and permissions set.";
                }
                else
                {
                    lblResult.Text = "No permissions selected.";
                }
            }
        }
        Update();
    }
Ejemplo n.º 10
0
    protected void remove_Click(object sender, EventArgs e)
    {
        bool memberRemoved = false;
        string username = txtUsername.Text;

        if (username == "admin")
        {
            lblResult.Text = "ERROR: Cannot remove Admin account.";
            return;
        }
        else if (username == (string)Session["username"])
        {
            lblResult.Text = "ERROR: Cannot remove your own account.";
            return;
        }
        else
        {
            string membersPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\Members.xml");
            string staffPath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, @"App_Data\Staff.xml");

            XmlDocument membersDoc = new XmlDocument();
            XmlDocument staffDoc = new XmlDocument();

            try
            {
                membersDoc.Load(membersPath);
                staffDoc.Load(staffPath);
            }
            catch (Exception ex)
            {
                lblResult.Text = "Error: Could not load members/staff XML files.";
                return;
            }

            AllServices.ServiceSOAPClient myProxy = new AllServices.ServiceSOAPClient("SOAPEndpoint");
            string encryptedUsername = myProxy.Encrypt(username);
            myProxy.Close();

            XmlNodeList staff = staffDoc.SelectNodes("Staff/Member");
            XmlNodeList members = membersDoc.SelectNodes("Members/Member");

            for (int i = 0; i < staff.Count; i++)
            {
                if (staff[i].ChildNodes.Item(0).InnerText == encryptedUsername)
                {
                    lblResult.Text = "Error: Cannot remove another Staff account. Edit permissions (Staff page 1) then remove.";
                    return;
                }
            }

            for (int i = 0; i < members.Count; i++)
            {
                if (members[i].ChildNodes.Item(1).InnerText == encryptedUsername)
                {
                    XmlNode node = membersDoc.SelectSingleNode("Members/Member[Username='******']");
                    node.ParentNode.RemoveChild(node);
                    membersDoc.Save(membersPath);
                    memberRemoved = true;
                }
            }
        }
        if (memberRemoved)
        {
            lblResult.Text = "Member Removed.";
        }
        else
        {
            lblResult.Text = "Member not found.";
        }
        Update();
    }