Example #1
0
        protected void Page_Command(Object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Save")
            {
                try
                {
                    // 01/16/2006 Paul.  Enable validator before validating page.
                    this.ValidateEditViewFields(m_sMODULE + ".EditView");
                    this.ValidateEditViewFields(m_sMODULE + ".EditOptions");

                    if (Page.IsValid)
                    {
                        DropDownList SERVICE = FindControl("SERVICE") as DropDownList;
                        if (SERVICE != null)
                        {
                            if (SERVICE.SelectedValue == "imap")
                            {
                                ctlEditButtons.ErrorText += "POP3 is the only supported service at this time. ";
                                return;
                            }
                        }
                        DropDownList MAILBOX_TYPE = FindControl("MAILBOX_TYPE") as DropDownList;
                        if (MAILBOX_TYPE != null)
                        {
                            if (MAILBOX_TYPE.SelectedValue != "bounce")
                            {
                                ctlEditButtons.ErrorText += "Bounce handling is the only supported action at this time. ";
                                return;
                            }
                        }
                    }
                    if (Page.IsValid)
                    {
                        // 01/08/2008 Paul.  If the encryption key does not exist, then we must create it and we must save it back to the database.
                        // 01/08/2008 Paul.  SugarCRM uses blowfish for the inbound email encryption, but we will not since .NET 2.0 does not support blowfish natively.
                        Guid gINBOUND_EMAIL_KEY = Sql.ToGuid(Application["CONFIG.InboundEmailKey"]);
                        if (Sql.IsEmptyGuid(gINBOUND_EMAIL_KEY))
                        {
                            gINBOUND_EMAIL_KEY = Guid.NewGuid();
                            SqlProcs.spCONFIG_Update("mail", "InboundEmailKey", gINBOUND_EMAIL_KEY.ToString());
                            Application["CONFIG.InboundEmailKey"] = gINBOUND_EMAIL_KEY;
                        }
                        Guid gINBOUND_EMAIL_IV = Sql.ToGuid(Application["CONFIG.InboundEmailIV"]);
                        if (Sql.IsEmptyGuid(gINBOUND_EMAIL_IV))
                        {
                            gINBOUND_EMAIL_IV = Guid.NewGuid();
                            SqlProcs.spCONFIG_Update("mail", "InboundEmailIV", gINBOUND_EMAIL_IV.ToString());
                            Application["CONFIG.InboundEmailIV"] = gINBOUND_EMAIL_IV;
                        }

                        string            sCUSTOM_MODULE = "INBOUND_EMAIL";
                        DataTable         dtCustomFields = SplendidCache.FieldsMetaData_Validated(sCUSTOM_MODULE);
                        DbProviderFactory dbf            = DbProviderFactories.GetFactory();
                        using (IDbConnection con = dbf.CreateConnection())
                        {
                            con.Open();
                            // 11/18/2007 Paul.  Use the current values for any that are not defined in the edit view.
                            DataRow   rowCurrent = null;
                            DataTable dtCurrent  = new DataTable();
                            if (!Sql.IsEmptyGuid(gID))
                            {
                                string sSQL;
                                sSQL = "select *                    " + ControlChars.CrLf
                                       + "  from vwINBOUND_EMAILS_Edit" + ControlChars.CrLf
                                       + " where ID = @ID             " + ControlChars.CrLf;
                                using (IDbCommand cmd = con.CreateCommand())
                                {
                                    cmd.CommandText = sSQL;
                                    Sql.AddParameter(cmd, "@ID", gID);
                                    using (DbDataAdapter da = dbf.CreateDataAdapter())
                                    {
                                        ((IDbDataAdapter)da).SelectCommand = cmd;
                                        da.Fill(dtCurrent);
                                        if (dtCurrent.Rows.Count > 0)
                                        {
                                            rowCurrent = dtCurrent.Rows[0];
                                        }
                                        else
                                        {
                                            // 11/19/2007 Paul.  If the record is not found, clear the ID so that the record cannot be updated.
                                            // It is possible that the record exists, but that ACL rules prevent it from being selected.
                                            gID = Guid.Empty;
                                        }
                                    }
                                }
                            }
                            using (IDbTransaction trn = con.BeginTransaction())
                            {
                                try
                                {
                                    string sEMAIL_PASSWORD = new DynamicControl(this, "EMAIL_PASSWORD").Text;
                                    if (sEMAIL_PASSWORD == "**********")
                                    {
                                        if (rowCurrent != null)
                                        {
                                            sEMAIL_PASSWORD = Sql.ToString(rowCurrent["EMAIL_PASSWORD"]);
                                        }
                                        else
                                        {
                                            sEMAIL_PASSWORD = "";
                                        }
                                    }
                                    else
                                    {
                                        string sENCRYPTED_EMAIL_PASSWORD = Security.EncryptPassword(sEMAIL_PASSWORD, gINBOUND_EMAIL_KEY, gINBOUND_EMAIL_IV);
                                        if (Security.DecryptPassword(sENCRYPTED_EMAIL_PASSWORD, gINBOUND_EMAIL_KEY, gINBOUND_EMAIL_IV) != sEMAIL_PASSWORD)
                                        {
                                            throw(new Exception("Decryption failed"));
                                        }
                                        sEMAIL_PASSWORD = sENCRYPTED_EMAIL_PASSWORD;
                                    }
                                    SqlProcs.spINBOUND_EMAILS_Update
                                        (ref gID
                                        , new DynamicControl(this, rowCurrent, "NAME").Text
                                        , new DynamicControl(this, rowCurrent, "STATUS").SelectedValue
                                        , new DynamicControl(this, rowCurrent, "SERVER_URL").Text
                                        , new DynamicControl(this, rowCurrent, "EMAIL_USER").Text
                                        , sEMAIL_PASSWORD
                                        , Sql.ToInteger(new DynamicControl(this, rowCurrent, "PORT").Text)
                                        , new DynamicControl(this, rowCurrent, "MAILBOX_SSL").Checked
                                        , new DynamicControl(this, rowCurrent, "SERVICE").SelectedValue
                                        , "INBOX"
                                        , new DynamicControl(this, rowCurrent, "MARK_READ").Checked
                                        , new DynamicControl(this, rowCurrent, "ONLY_SINCE").Checked
                                        , new DynamicControl(this, rowCurrent, "MAILBOX_TYPE").SelectedValue
                                        , new DynamicControl(this, rowCurrent, "TEMPLATE_ID").ID
                                        , new DynamicControl(this, rowCurrent, "GROUP_ID").ID
                                        , new DynamicControl(this, rowCurrent, "FROM_NAME").Text
                                        , new DynamicControl(this, rowCurrent, "FROM_ADDR").Text
                                        , new DynamicControl(this, rowCurrent, "FILTER_DOMAIN").Text
                                        , trn
                                        );
                                    SplendidDynamic.UpdateCustomFields(this, trn, gID, sCUSTOM_MODULE, dtCustomFields);
                                    trn.Commit();
                                }
                                catch (Exception ex)
                                {
                                    trn.Rollback();
                                    SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex);
                                    ctlEditButtons.ErrorText = ex.Message;
                                    return;
                                }
                            }
                        }
                        SplendidCache.ClearEmailGroups();
                        SplendidCache.ClearInboundEmails();
                        Response.Redirect("view.aspx?ID=" + gID.ToString());
                    }
                }
                catch (Exception ex)
                {
                    SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex);
                    ctlEditButtons.ErrorText = ex.Message;
                }
            }
            else if (e.CommandName == "Cancel")
            {
                if (Sql.IsEmptyGuid(gID))
                {
                    Response.Redirect("default.aspx");
                }
                else
                {
                    Response.Redirect("view.aspx?ID=" + gID.ToString());
                }
            }
        }