/// <summary> /// Send Private Message /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void Save_Click([NotNull] object sender, [NotNull] EventArgs e) { var replyTo = this.Get <HttpRequestBase>().QueryString.GetFirstOrDefault("p").IsSet() ? this.Get <HttpRequestBase>().QueryString.GetFirstOrDefault("p").ToType <int>() : -1; // recipient was set in dropdown if (this.ToList.Visible) { this.To.Text = this.ToList.SelectedItem.Text; } if (this.To.Text.Length <= 0) { // recipient is required field YafContext.Current.AddLoadMessage(this.GetText("need_to"), MessageTypes.Warning); return; } // subject is required if (this.PmSubjectTextBox.Text.Trim().Length <= 0) { YafContext.Current.AddLoadMessage(this.GetText("need_subject"), MessageTypes.Warning); return; } // message is required if (this._editor.Text.Trim().Length <= 0) { YafContext.Current.AddLoadMessage(this.GetText("need_message"), MessageTypes.Warning); return; } if (this.ToList.SelectedItem != null && this.ToList.SelectedItem.Value == "0") { // administrator is sending PMs tp all users string body = this._editor.Text; var messageFlags = new MessageFlags { IsHtml = this._editor.UsesHTML, IsBBCode = this._editor.UsesBBCode }; // test user's PM count if (!this.VerifyMessageAllowed(1)) { return; } var receivingPMInfo = LegacyDb.user_pmcount(replyTo).Rows[0]; // test receiving user's PM count if (!YafContext.Current.IsAdmin || !(bool) Convert.ChangeType(UserMembershipHelper.GetUserRowForID(replyTo, true)["IsAdmin"], typeof(bool))) { if (receivingPMInfo["NumberTotal"].ToType <int>() + 1 <= receivingPMInfo["NumberAllowed"].ToType <int>()) { return; } // recipient has full PM box YafContext.Current.AddLoadMessage( this.GetTextFormatted("RECIPIENTS_PMBOX_FULL", this.To.Text), MessageTypes.Error); return; } LegacyDb.pmessage_save( YafContext.Current.PageUserID, 0, this.PmSubjectTextBox.Text, body, messageFlags.BitValue, replyTo); // redirect to outbox (sent items), not control panel YafBuildLink.Redirect(ForumPages.cp_pm, "v={0}", "out"); } else { // remove all abundant whitespaces and separators var rx = new Regex(@";(\s|;)*;"); this.To.Text = rx.Replace(this.To.Text, ";"); if (this.To.Text.StartsWith(";")) { this.To.Text = this.To.Text.Substring(1); } if (this.To.Text.EndsWith(";")) { this.To.Text = this.To.Text.Substring(0, this.To.Text.Length - 1); } rx = new Regex(@"\s*;\s*"); this.To.Text = rx.Replace(this.To.Text, ";"); // list of recipients var recipients = new List <string>(this.To.Text.Trim().Split(';')); if (recipients.Count > this.Get <YafBoardSettings>().PrivateMessageMaxRecipients && !YafContext.Current.IsAdmin && this.Get <YafBoardSettings>().PrivateMessageMaxRecipients != 0) { // to many recipients YafContext.Current.AddLoadMessage( this.GetTextFormatted( "TOO_MANY_RECIPIENTS", this.Get <YafBoardSettings>().PrivateMessageMaxRecipients), MessageTypes.Warning); return; } if (!this.VerifyMessageAllowed(recipients.Count)) { return; } // list of recipient's ids var recipientIds = new List <int>(); // get recipients' IDs foreach (string recipient in recipients) { int?userId = this.Get <IUserDisplayName>().GetId(recipient); if (!userId.HasValue) { YafContext.Current.AddLoadMessage( this.GetTextFormatted("NO_SUCH_USER", recipient), MessageTypes.Warning); return; } if (UserMembershipHelper.IsGuestUser(userId.Value)) { YafContext.Current.AddLoadMessage(this.GetText("NOT_GUEST"), MessageTypes.Error); return; } // get recipient's ID from the database if (!recipientIds.Contains(userId.Value)) { recipientIds.Add(userId.Value); } var receivingPMInfo = LegacyDb.user_pmcount(userId.Value).Rows[0]; // test receiving user's PM count if ((receivingPMInfo["NumberTotal"].ToType <int>() + 1 < receivingPMInfo["NumberAllowed"].ToType <int>()) || YafContext.Current.IsAdmin || (bool) Convert.ChangeType( UserMembershipHelper.GetUserRowForID(userId.Value, true)["IsAdmin"], typeof(bool))) { continue; } // recipient has full PM box YafContext.Current.AddLoadMessage( this.GetTextFormatted("RECIPIENTS_PMBOX_FULL", recipient), MessageTypes.Error); return; } // send PM to all recipients foreach (var userId in recipientIds) { string body = this._editor.Text; var messageFlags = new MessageFlags { IsHtml = this._editor.UsesHTML, IsBBCode = this._editor.UsesBBCode }; LegacyDb.pmessage_save( YafContext.Current.PageUserID, userId, this.PmSubjectTextBox.Text, body, messageFlags.BitValue, replyTo); // reset reciever's lazy data as he should be informed at once this.Get <IDataCache>().Remove(Constants.Cache.ActiveUserLazyData.FormatWith(userId)); if (this.Get <YafBoardSettings>().AllowPMEmailNotification) { this.Get <ISendNotification>() .ToPrivateMessageRecipient(userId, this.PmSubjectTextBox.Text.Trim()); } } // redirect to outbox (sent items), not control panel YafBuildLink.Redirect(ForumPages.cp_pm, "v={0}", "out"); } }
/// <summary> /// Handles save button click event. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> protected void Save_Click(object sender, EventArgs e) { // recipient was set in dropdown if (this.ToList.Visible) { this.To.Text = this.ToList.SelectedItem.Text; } // Simon: Disable for admins DisablePMs = RoleMembershipHelper.IsUserInRole(this.To.Text, "Administrators"); if (DisablePMs) { return; } if (this.To.Text.Length <= 0) { // recipient is required field YafContext.Current.AddLoadMessage(GetText("need_to")); return; } // subject is required if (this.PmSubjectTextBox.Text.Trim().Length <= 0) { YafContext.Current.AddLoadMessage(GetText("need_subject")); return; } // message is required if (this._editor.Text.Trim().Length <= 0) { YafContext.Current.AddLoadMessage(GetText("need_message")); return; } if (this.ToList.SelectedItem != null && this.ToList.SelectedItem.Value == "0") { // administrator is sending PMs tp all users string body = this._editor.Text; var messageFlags = new MessageFlags(); messageFlags.IsHtml = this._editor.UsesHTML; messageFlags.IsBBCode = this._editor.UsesBBCode; DB.pmessage_save(YafContext.Current.PageUserID, 0, this.PmSubjectTextBox.Text, body, messageFlags.BitValue); // redirect to outbox (sent items), not control panel YafBuildLink.Redirect(ForumPages.cp_pm, "v={0}", "out"); } else { // remove all abundant whitespaces and separators this.To.Text.Trim(); var rx = new Regex(@";(\s|;)*;"); this.To.Text = rx.Replace(this.To.Text, ";"); if (this.To.Text.StartsWith(";")) { this.To.Text = this.To.Text.Substring(1); } if (this.To.Text.EndsWith(";")) { this.To.Text = this.To.Text.Substring(0, this.To.Text.Length - 1); } rx = new Regex(@"\s*;\s*"); this.To.Text = rx.Replace(this.To.Text, ";"); // list of recipients var recipients = new List <string>(this.To.Text.Trim().Split(';')); if (recipients.Count > YafContext.Current.BoardSettings.PrivateMessageMaxRecipients && !YafContext.Current.IsAdmin && YafContext.Current.BoardSettings.PrivateMessageMaxRecipients != 0) { // to many recipients YafContext.Current.AddLoadMessage(GetTextFormatted("TOO_MANY_RECIPIENTS", YafContext.Current.BoardSettings.PrivateMessageMaxRecipients)); return; } // test sending user's PM count // get user's name DataRow drPMInfo = DB.user_pmcount(YafContext.Current.PageUserID).Rows[0]; if ((Convert.ToInt32(drPMInfo["NumberTotal"]) > Convert.ToInt32(drPMInfo["NumberAllowed"]) + recipients.Count) && !YafContext.Current.IsAdmin) { // user has full PM box YafContext.Current.AddLoadMessage(GetTextFormatted("OWN_PMBOX_FULL", drPMInfo["NumberAllowed"])); return; } // list of recipient's ids var recipientIds = new List <int>(); // get recipients' IDs foreach (string recipient in recipients) { int?userId = PageContext.UserDisplayName.GetId(recipient); if (!userId.HasValue) { YafContext.Current.AddLoadMessage(GetTextFormatted("NO_SUCH_USER", recipient)); return; } else if (UserMembershipHelper.IsGuestUser(userId.Value)) { YafContext.Current.AddLoadMessage(GetText("NOT_GUEST")); return; } // get recipient's ID from the database if (!recipientIds.Contains(userId.Value)) { recipientIds.Add(userId.Value); } // test receiving user's PM count if ((DB.user_pmcount(userId.Value).Rows[0]["NumberTotal"].ToType <int>() >= DB.user_pmcount(userId.Value).Rows[0]["NumberAllowed"].ToType <int>()) && !YafContext.Current.IsAdmin && !(bool)Convert.ChangeType(UserMembershipHelper.GetUserRowForID(userId.Value, true)["IsAdmin"], typeof(bool))) { // recipient has full PM box YafContext.Current.AddLoadMessage(GetTextFormatted("RECIPIENTS_PMBOX_FULL", recipient)); return; } } // send PM to all recipients foreach (var userId in recipientIds) { string body = this._editor.Text; var messageFlags = new MessageFlags(); messageFlags.IsHtml = this._editor.UsesHTML; messageFlags.IsBBCode = this._editor.UsesBBCode; DB.pmessage_save(YafContext.Current.PageUserID, userId, this.PmSubjectTextBox.Text, body, messageFlags.BitValue); // reset reciever's lazy data as he should be informed at once PageContext.Cache.Remove(YafCache.GetBoardCacheKey(Constants.Cache.ActiveUserLazyData.FormatWith(userId))); if (YafContext.Current.BoardSettings.AllowPMEmailNotification) { this.Get <YafSendNotification>().ToPrivateMessageRecipient(userId, this.PmSubjectTextBox.Text.Trim()); } } // redirect to outbox (sent items), not control panel YafBuildLink.Redirect(ForumPages.cp_pm, "v={0}", "out"); } }