/////////////////////////////////////////////////////////////////////// void on_update() { Boolean good = validate(); if (good) { sql = new SQLString(@"update bug_posts set bp_comment = @cm, bp_comment_search = @cs, bp_content_type = @cn, bp_hidden_from_external_users = @internal where bp_id = @id select bg_short_desc from bugs where bg_id = @bugid"); if (use_fckeditor) { string text = Util.strip_dangerous_tags(comment.Value); sql = sql.AddParameterWithValue("cm", text.Replace("'", "'")); sql = sql.AddParameterWithValue("cs", Util.strip_html(comment.Value).Replace("'", "''")); sql = sql.AddParameterWithValue("cn", "text/html"); } else { sql = sql.AddParameterWithValue("cm", HttpUtility.HtmlDecode(comment.Value).Replace("'", "''")); sql = sql.AddParameterWithValue("cs", comment.Value.Replace("'", "''")); sql = sql.AddParameterWithValue("cn", "text/plain"); } sql = sql.AddParameterWithValue("id", Convert.ToString(id)); sql = sql.AddParameterWithValue("bugid", Convert.ToString(bugid)); sql = sql.AddParameterWithValue("internal", Util.bool_to_string(internal_only.Checked)); DataRow dr = DbUtil.get_datarow(sql); // Don't send notifications for internal only comments. // We aren't putting them the email notifications because it that makes it // easier for them to accidently get forwarded to the "wrong" people... if (!internal_only.Checked) { Bug.send_notifications(Bug.UPDATE, bugid, User.Identity); WhatsNew.add_news(bugid, (string)dr["bg_short_desc"], "updated", User.Identity); } Response.Redirect("edit_bug.aspx?id=" + Convert.ToString(bugid)); } }
/////////////////////////////////////////////////////////////////////// public static NewIds insert_bug( string short_desc, Security security, string tags, int projectid, int orgid, int categoryid, int priorityid, int statusid, int assigned_to_userid, int udfid, string project_custom_dropdown_value1, string project_custom_dropdown_value2, string project_custom_dropdown_value3, string comment_formated, string comment_search, string from, string cc, string content_type, bool internal_only, SortedDictionary <string, string> hash_custom_cols, bool send_notifications) { if (short_desc.Trim() == "") { short_desc = "[No Description]"; } if (assigned_to_userid == 0) { assigned_to_userid = Util.get_default_user(projectid); } string sql = @"insert into bugs (bg_short_desc, bg_tags, bg_reported_user, bg_last_updated_user, bg_reported_date, bg_last_updated_date, bg_project, bg_org, bg_category, bg_priority, bg_status, bg_assigned_to_user, bg_user_defined_attribute, bg_project_custom_dropdown_value1, bg_project_custom_dropdown_value2, bg_project_custom_dropdown_value3 $custom_cols_placeholder1) values (N'$short_desc', N'$tags', $reported_user, $reported_user, getdate(), getdate(), $project, $org, $category, $priority, $status, $assigned_user, $udf, N'$pcd1',N'$pcd2',N'$pcd3' $custom_cols_placeholder2)" ; sql = sql.Replace("$short_desc", short_desc.Replace("'", "''")); sql = sql.Replace("$tags", tags.Replace("'", "''")); sql = sql.Replace("$reported_user", Convert.ToString(security.user.usid)); sql = sql.Replace("$project", Convert.ToString(projectid)); sql = sql.Replace("$org", Convert.ToString(orgid)); sql = sql.Replace("$category", Convert.ToString(categoryid)); sql = sql.Replace("$priority", Convert.ToString(priorityid)); sql = sql.Replace("$status", Convert.ToString(statusid)); sql = sql.Replace("$assigned_user", Convert.ToString(assigned_to_userid)); sql = sql.Replace("$udf", Convert.ToString(udfid)); sql = sql.Replace("$pcd1", project_custom_dropdown_value1); sql = sql.Replace("$pcd2", project_custom_dropdown_value2); sql = sql.Replace("$pcd3", project_custom_dropdown_value3); if (hash_custom_cols == null) { sql = sql.Replace("$custom_cols_placeholder1", ""); sql = sql.Replace("$custom_cols_placeholder2", ""); } else { string custom_cols_sql1 = ""; string custom_cols_sql2 = ""; DataSet ds_custom_cols = Util.get_custom_columns(); foreach (DataRow drcc in ds_custom_cols.Tables[0].Rows) { string column_name = (string)drcc["name"]; // skip if no permission to update if (security.user.dict_custom_field_permission_level[column_name] != Security.PERMISSION_ALL) { continue; } custom_cols_sql1 += ",[" + column_name + "]"; string datatype = (string)drcc["datatype"]; string custom_col_val = Util.request_to_string_for_sql( hash_custom_cols[column_name], datatype); custom_cols_sql2 += "," + custom_col_val; } sql = sql.Replace("$custom_cols_placeholder1", custom_cols_sql1); sql = sql.Replace("$custom_cols_placeholder2", custom_cols_sql2); } sql += "\nselect scope_identity()"; int bugid = Convert.ToInt32(DbUtil.execute_scalar(sql)); int postid = Bug.insert_comment( bugid, security.user.usid, comment_formated, comment_search, from, cc, content_type, internal_only); Bug.auto_subscribe(bugid); if (send_notifications) { Bug.send_notifications(Bug.INSERT, bugid, security); } return(new NewIds(bugid, postid)); }
/////////////////////////////////////////////////////////////////////// private static int insert_post_attachment_impl( Security security, int bugid, Stream content, int content_length, int copy_bpid, string file, string comment, string content_type, int parent, bool hidden_from_external_users, bool send_notifications) { // Note that this method does not perform any security check nor does // it check that content_length is less than MaxUploadSize. // These are left up to the caller. string upload_folder = Util.get_upload_folder(); string sql; bool store_attachments_in_database = (Util.get_setting("StoreAttachmentsInDatabase", "0") == "1"); string effective_file = file; int effective_content_length = content_length; string effective_content_type = content_type; Stream effective_content = null; try { // Determine the content. We may be instructed to copy an existing // attachment via copy_bpid, or a Stream may be provided as the content parameter. if (copy_bpid != -1) { BugPostAttachment bpa = get_bug_post_attachment(copy_bpid); effective_content = bpa.content; effective_file = bpa.file; effective_content_length = bpa.content_length; effective_content_type = bpa.content_type; } else { effective_content = content; effective_file = file; effective_content_length = content_length; effective_content_type = content_type; } // Insert a new post into bug_posts. sql = @" declare @now datetime set @now = getdate() update bugs set bg_last_updated_date = @now, bg_last_updated_user = $us where bg_id = $bg insert into bug_posts (bp_type, bp_bug, bp_file, bp_comment, bp_size, bp_date, bp_user, bp_content_type, bp_parent, bp_hidden_from_external_users) values ('file', $bg, N'$fi', N'$de', $si, @now, $us, N'$ct', $pa, $internal) select scope_identity()" ; sql = sql.Replace("$bg", Convert.ToString(bugid)); sql = sql.Replace("$fi", effective_file.Replace("'", "''")); sql = sql.Replace("$de", comment.Replace("'", "''")); sql = sql.Replace("$si", Convert.ToString(effective_content_length)); sql = sql.Replace("$us", Convert.ToString(security.user.usid)); // Sometimes, somehow, content type is null. Not sure how. sql = sql.Replace("$ct", effective_content_type != null ? effective_content_type.Replace("'", "''") : string.Empty); if (parent == -1) { sql = sql.Replace("$pa", "null"); } else { sql = sql.Replace("$pa", Convert.ToString(parent)); } sql = sql.Replace("$internal", Util.bool_to_string(hidden_from_external_users)); int bp_id = Convert.ToInt32(DbUtil.execute_scalar(sql)); try { // Store attachment in bug_post_attachments table. if (store_attachments_in_database) { byte[] data = new byte[effective_content_length]; int bytes_read = 0; while (bytes_read < effective_content_length) { int bytes_read_this_iteration = effective_content.Read(data, bytes_read, effective_content_length - bytes_read); if (bytes_read_this_iteration == 0) { throw new Exception("Unexpectedly reached the end of the stream before all data was read."); } bytes_read += bytes_read_this_iteration; } sql = @"insert into bug_post_attachments (bpa_post, bpa_content) values (@bp, @bc)" ; using (SqlCommand cmd = new SqlCommand(sql)) { cmd.Parameters.AddWithValue("@bp", bp_id); cmd.Parameters.Add("@bc", SqlDbType.Image).Value = data; cmd.CommandTimeout = Convert.ToInt32(Util.get_setting("SqlCommand.CommandTimeout", "30")); DbUtil.execute_nonquery(cmd); } } else { // Store attachment in UploadFolder. if (upload_folder == null) { throw new Exception("StoreAttachmentsInDatabase is false and UploadFolder is not set in web.config."); } // Copy the content Stream to a file in the upload_folder. byte[] buffer = new byte[16384]; int bytes_read = 0; using (FileStream fs = new FileStream(upload_folder + "\\" + bugid + "_" + bp_id + "_" + effective_file, FileMode.CreateNew, FileAccess.Write)) { while (bytes_read < effective_content_length) { int bytes_read_this_iteration = effective_content.Read(buffer, 0, buffer.Length); if (bytes_read_this_iteration == 0) { throw new Exception("Unexpectedly reached the end of the stream before all data was read."); } fs.Write(buffer, 0, bytes_read_this_iteration); bytes_read += bytes_read_this_iteration; } } } } catch { // clean up sql = @"delete from bug_posts where bp_id = $bp"; sql = sql.Replace("$bp", Convert.ToString(bp_id)); DbUtil.execute_nonquery(sql); throw; } if (send_notifications) { Bug.send_notifications(Bug.UPDATE, bugid, security); } return(bp_id); } finally { // If this procedure "owns" the content (instead of our caller owning it), dispose it. if (effective_content != null && effective_content != content) { effective_content.Dispose(); } } }
/////////////////////////////////////////////////////////////////////// void on_update() { Boolean good = validate(); if (good) { if (tsk_id == 0) // insert new { sql = new SQLString(@" insert into bug_tasks ( tsk_bug, tsk_created_user, tsk_created_date, tsk_last_updated_user, tsk_last_updated_date, tsk_assigned_to_user, tsk_planned_start_date, tsk_actual_start_date, tsk_planned_end_date, tsk_actual_end_date, tsk_planned_duration, tsk_actual_duration, tsk_duration_units, tsk_percent_complete, tsk_status, tsk_sort_sequence, tsk_description ) values ( @tsk_bug, @tsk_created_user, getdate(), @tsk_last_updated_user, getdate(), @tsk_assigned_to_user, @tsk_planned_start_date, @tsk_actual_start_date, @tsk_planned_end_date, @tsk_actual_end_date, @tsk_planned_duration, @tsk_actual_duration, @tsk_duration_units, @tsk_percent_complete, @tsk_status, @tsk_sort_sequence, @tsk_description ); declare @tsk_id int select @tsk_id = scope_identity() insert into bug_posts (bp_bug, bp_user, bp_date, bp_comment, bp_type) values(@tsk_bug, @tsk_last_updated_user, getdate(), N'added task ' + convert(varchar, @tsk_id), 'update')"); sql = sql.AddParameterWithValue("tsk_created_user", Convert.ToString(User.Identity.GetUserId())); } else // edit existing { sql = new SQLString(@" update bug_tasks set tsk_last_updated_user = @tsk_last_updated_user, tsk_last_updated_date = getdate(), tsk_assigned_to_user = @tsk_assigned_to_user, tsk_planned_start_date = '@tsk_planned_start_date', tsk_actual_start_date = '@tsk_actual_start_date', tsk_planned_end_date = '@tsk_planned_end_date', tsk_actual_end_date = '@tsk_actual_end_date', tsk_planned_duration = @tsk_planned_duration, tsk_actual_duration = @tsk_actual_duration, tsk_duration_units = @tsk_duration_units, tsk_percent_complete = @tsk_percent_complete, tsk_status = @tsk_status, tsk_sort_sequence = @tsk_sort_sequence, tsk_description = @tsk_description where tsk_id = @tsk_id; insert into bug_posts (bp_bug, bp_user, bp_date, bp_comment, bp_type) values(@tsk_bug, @tsk_last_updated_user, getdate(), N'updated task ' + @tsk_id, 'update')"); sql = sql.AddParameterWithValue("tsk_id", Convert.ToString(tsk_id)); } sql = sql.AddParameterWithValue("tsk_bug", Convert.ToString(bugid)); sql = sql.AddParameterWithValue("tsk_last_updated_user", Convert.ToString(User.Identity.GetUserId())); sql = sql.AddParameterWithValue("tsk_planned_start_date", format_date_hour_min( planned_start_date.Value, planned_start_hour.SelectedItem.Value, planned_start_min.SelectedItem.Value)); sql = sql.AddParameterWithValue("tsk_actual_start_date", format_date_hour_min( actual_start_date.Value, actual_start_hour.SelectedItem.Value, actual_start_min.SelectedItem.Value)); sql = sql.AddParameterWithValue("tsk_planned_end_date", format_date_hour_min( planned_end_date.Value, planned_end_hour.SelectedItem.Value, planned_end_min.SelectedItem.Value)); sql = sql.AddParameterWithValue("tsk_actual_end_date", format_date_hour_min( actual_end_date.Value, actual_end_hour.SelectedItem.Value, actual_end_min.SelectedItem.Value)); sql = sql.AddParameterWithValue("tsk_planned_duration", format_decimal_for_db(planned_duration.Value)); sql = sql.AddParameterWithValue("tsk_actual_duration", format_decimal_for_db(actual_duration.Value)); sql = sql.AddParameterWithValue("tsk_percent_complete", format_number_for_db(percent_complete.Value)); sql = sql.AddParameterWithValue("tsk_status", status.SelectedItem.Value); sql = sql.AddParameterWithValue("tsk_sort_sequence", format_number_for_db(sort_sequence.Value)); sql = sql.AddParameterWithValue("tsk_assigned_to_user", assigned_to.SelectedItem.Value); sql = sql.AddParameterWithValue("tsk_description", desc.Value); sql = sql.AddParameterWithValue("tsk_duration_units", duration_units.SelectedItem.Value); DbUtil.execute_nonquery(sql); Bug.send_notifications(Bug.UPDATE, bugid, User.Identity); Response.Redirect("tasks.aspx?bugid=" + Convert.ToString(bugid)); } else { if (tsk_id == 0) // insert new { msg.InnerText = "Task was not created."; } else // edit existing { msg.InnerText = "Task was not updated."; } } }
//************************************************************* public static bool fetch_messages(string project_user, string project_password, int projectid) { // experimental, under construction POP3Client.POP3client client = new POP3Client.POP3client(Pop3ReadInputStreamCharByChar); string[] SubjectCannotContainStrings = Util.rePipes.Split(Pop3SubjectCannotContain); string[] FromCannotContainStrings = Util.rePipes.Split(Pop3FromCannotContain); //try { System.Data.DataRow defaults = Bug.get_bug_defaults(); //int projectid = (int)defaults["pj"]; int categoryid = (int)defaults["ct"]; int priorityid = (int)defaults["pr"]; int statusid = (int)defaults["st"]; int udfid = (int)defaults["udf"]; Util.write_to_log("pop3:" + client.connect(Pop3Server, Pop3Port, Pop3UseSSL)); Util.write_to_log("pop3:sending POP3 command USER"); Util.write_to_log("pop3:" + client.USER(project_user)); Util.write_to_log("pop3:sending POP3 command PASS"); Util.write_to_log("pop3:" + client.PASS(project_password)); Util.write_to_log("pop3:sending POP3 command STAT"); Util.write_to_log("pop3:" + client.STAT()); Util.write_to_log("pop3:sending POP3 command LIST"); string list; list = client.LIST(); Util.write_to_log("pop3:list follows:"); Util.write_to_log(list); string[] messages = null; System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("\r\n"); messages = regex.Split(list); int end = messages.Length - 1; // loop through the messages for (int i = 1; i < end; i++) { int space_pos = messages[i].IndexOf(" "); int message_number = Convert.ToInt32(messages[i].Substring(0, space_pos)); string message_raw_string = client.RETR(message_number); if (Pop3WriteRawMessagesToLog) { Util.write_to_log("raw email message:"); Util.write_to_log(message_raw_string); } SharpMimeMessage mime_message = MyMime.get_sharp_mime_message(message_raw_string); string from_addr = MyMime.get_from_addr(mime_message); string subject = MyMime.get_subject(mime_message); if (Pop3SubjectMustContain != "" && subject.IndexOf(Pop3SubjectMustContain) < 0) { Util.write_to_log("skipping because subject does not contain: " + Pop3SubjectMustContain); continue; } bool bSkip = false; for (int k = 0; k < SubjectCannotContainStrings.Length; k++) { if (SubjectCannotContainStrings[k] != "") { if (subject.IndexOf(SubjectCannotContainStrings[k]) >= 0) { Util.write_to_log("skipping because subject cannot contain: " + SubjectCannotContainStrings[k]); bSkip = true; break; // done checking, skip this message } } } if (bSkip) { continue; } if (Pop3FromMustContain != "" && from_addr.IndexOf(Pop3FromMustContain) < 0) { Util.write_to_log("skipping because from does not contain: " + Pop3FromMustContain); continue; // that is, skip to next message } for (int k = 0; k < FromCannotContainStrings.Length; k++) { if (FromCannotContainStrings[k] != "") { if (from_addr.IndexOf(FromCannotContainStrings[k]) >= 0) { Util.write_to_log("skipping because from cannot contain: " + FromCannotContainStrings[k]); bSkip = true; break; // done checking, skip this message } } } if (bSkip) { continue; } int bugid = MyMime.get_bugid_from_subject(ref subject); string cc = MyMime.get_cc(mime_message); string comment = MyMime.get_comment(mime_message); string headers = MyMime.get_headers_for_comment(mime_message); if (headers != "") { comment = headers + "\n" + comment; } Security security = MyMime.get_synthesized_security(mime_message, from_addr, Pop3ServiceUsername); int orgid = security.user.org; if (bugid == 0) { if (security.user.forced_project != 0) { projectid = security.user.forced_project; } if (subject.Length > 200) { subject = subject.Substring(0, 200); } Bug.NewIds new_ids = Bug.insert_bug( subject, security, "", // tags projectid, orgid, categoryid, priorityid, statusid, 0, // assignedid, udfid, "", "", "", // project specific dropdown values comment, comment, from_addr, cc, "text/plain", false, // internal only null, // custom columns false); MyMime.add_attachments(mime_message, new_ids.bugid, new_ids.postid, security); // your customizations Bug.apply_post_insert_rules(new_ids.bugid); Bug.send_notifications(Bug.INSERT, new_ids.bugid, security); WhatsNew.add_news(new_ids.bugid, subject, "added", security); MyPop3.auto_reply(new_ids.bugid, from_addr, subject, projectid); } else // update existing { string StatusResultingFromIncomingEmail = Util.get_setting("StatusResultingFromIncomingEmail", "0"); string sql = ""; if (StatusResultingFromIncomingEmail != "0") { sql = @"update bugs set bg_status = $st where bg_id = $bg " ; sql = sql.Replace("$st", StatusResultingFromIncomingEmail); } sql += "select bg_short_desc from bugs where bg_id = $bg"; sql = sql.Replace("$bg", Convert.ToString(bugid)); DataRow dr2 = DbUtil.get_datarow(sql); // Add a comment to existing bug. int postid = Bug.insert_comment( bugid, security.user.usid, // (int) dr["us_id"], comment, comment, from_addr, cc, "text/plain", false); // internal only MyMime.add_attachments(mime_message, bugid, postid, security); Bug.send_notifications(Bug.UPDATE, bugid, security); WhatsNew.add_news(bugid, (string)dr2["bg_short_desc"], "updated", security); } if (Pop3DeleteMessagesOnServer) { Util.write_to_log("sending POP3 command DELE"); Util.write_to_log(client.DELE(message_number)); } } } //catch (Exception ex) //{ // Util.write_to_log("pop3:exception in fetch_messages: " + ex.Message); // error_count++; // if (error_count > Pop3TotalErrorsAllowed) // { // return false; // } //} Util.write_to_log("pop3:quit"); Util.write_to_log("pop3:" + client.QUIT()); return(true); }