⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 insert_bug.aspx

📁 Bug管理系统
💻 ASPX
字号:
<%@ Page language="C#" validateRequest="false"%>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="anmar.SharpMimeTools" %>
<%@ Assembly Name="SharpMimeTools" %>

<!--
Copyright 2002-2005 Corey Trager
Distributed under the terms of the GNU General Public License
-->
<!-- #include file = "inc.aspx" -->
<!-- #include file = "inc_insert_bug.aspx" -->
<!-- #include file = "inc_print_bug.aspx" -->

<script language="C#" runat="server">


DbUtil dbutil;
Security security;
int id;
String sql;


///////////////////////////////////////////////////////////////////////
void Page_Load(Object sender, EventArgs e)
{

	Util.do_not_cache(Response);
	dbutil = new DbUtil();

	string username = Request["username"];
	string password = Request["password"];
	string projectid_string = Request["projectid"];
	string comment = Request["comment"];
	string from = Request["from"];
	string message = Request["message"];
	
	
	// only via emails
	if (from == null)
	{
		from = "";
	}
	else if (from.Length > 60)
	{
		from = from.Substring(0,60);
	}
	
	// this could also be the email subject
	string short_desc = (Request["short_desc"]);
	if (short_desc == null)
	{
		short_desc = "";
	}
	else if (short_desc.Length > 100)
	{
		short_desc = short_desc.Substring(0,100);
	}

	SharpMimeMessage mime_message = null;

	if (message != null && message.Length > 0)
	{
		if (message != null)
		{

			// feed a stream to MIME parser
			byte[] bytes = Encoding.UTF8.GetBytes(message);
			System.IO.MemoryStream ms = new System.IO.MemoryStream (bytes);
			mime_message = new SharpMimeMessage(ms);
			
			if (mime_message.Header.ContentType.ToLower().IndexOf("text/plain") > -1)
			{
				comment = mime_message.BodyDecoded;
			}
			else
			{
				// use the first plain text message body
				foreach (SharpMimeMessage part in mime_message)
				{
					if (part.IsMultipart)
					{
						foreach (SharpMimeMessage subpart in part)
						{
							if (subpart.Header.ContentType.ToLower().IndexOf("text/plain") > -1)
							{
								comment = subpart.BodyDecoded;
								break;
							}
						}
					}
					else
					{
						if (part.Header.ContentType.ToLower().IndexOf("text/plain") > -1)
						{
							comment = part.BodyDecoded;
							break;
						}
					}
				}
			}
			
			if (comment == null)
			{
				comment = "NO MESSAGE BODY FOUND";
			}
			
		}
	}
	else
	{
		if (comment == null)
		{
			comment = "";
		}
	}
	

	string bugid_string = Request["bugid"];

	if (username == null
	|| username == "")
	{
		Response.AddHeader("BTNET","ERROR: username required");
		Response.Write("ERROR: username required");
		Response.End();
	}

	if (password == null
	|| password == "")
	{
		Response.AddHeader("BTNET","ERROR: password required");
		Response.Write("ERROR: password required");
		Response.End();
	}

	// authenticate user

	sql = @"select us_id
		from users
		where us_username = N'$us'
		and (us_password = N'$pw' or us_password = N'$en')
		and us_active = 1";
	
	sql = sql.Replace("$us",username.Replace("'","''")); 
	sql = sql.Replace("$pw",password.Replace("'","''"));
	sql = sql.Replace("$en", Util.encrypt_string_using_MD5(password));
	
	DataRow dr = dbutil.get_datarow(sql);

	if (dr == null) 
	{
		Response.AddHeader("BTNET","ERROR: invalid username or password");
		Response.Write("ERROR: invalid username or password");
		Response.End();
	}

	security = new Security();
	security.this_usid = (int) dr["us_id"];

	int projectid = 0;
	if (Util.is_int(projectid_string))
	{
		projectid = Convert.ToInt32(projectid_string);
	}

	int bugid = 0;
	if (Util.is_int(bugid_string))
	{
		bugid = Convert.ToInt32(bugid_string);
	
		// Check if the bug is still in the database
		// No comment can be added to merged or deleted bugids
		// In this case a new bug is created, this to prevent possible los of information
		
		sql = @"select count(bg_id)
			from bugs
			where bg_id = $id";

		sql = sql.Replace("$id", Convert.ToString(bugid));

		if (Convert.ToInt32(dbutil.execute_scalar(sql)) == 0)
		{
			bugid = 0;
		}		
	}


	// Either insert a new bug or append a commment to existing bug
	// based on presence, absence of bugid
	if (bugid == 0)
	{
		// insert a new bug
		
		string email_to = "";
		
		if (mime_message != null)
		{
			if (mime_message.Header.Subject != null && mime_message.Header.Subject != "")
			{
				short_desc = SharpMimeTools.parserfc2047Header(mime_message.Header.Subject);
				// in case somebody is replying to a bug that has been deleted or merged
				short_desc = short_desc.Replace("DO NOT EDIT THIS", "PREVIOUS");
			}
			else
			{
				short_desc = "[No Subject]";
			}

			string headers = "";

			if (mime_message.Header.Subject != null && mime_message.Header.Subject != "")
			{
				headers += "Subject: " + SharpMimeTools.parserfc2047Header(mime_message.Header.Subject) + "\n";
			}

			if (mime_message.Header.To != null && mime_message.Header.To != "")
			{
				headers += "To: " + mime_message.Header.To + "\n";
				email_to = mime_message.Header.To;
			}

			if (mime_message.Header.Cc != null && mime_message.Header.Cc != "")
			{
				headers += "Cc: " + mime_message.Header.Cc + "\n";
			}
			
			if (headers != "")
			{
				comment = headers + "\n" + comment;
			}
		}
		
		int categoryid = 0;
		int priorityid = 0;
		int assignedid = 0;
		int statusid = 0;
		
		Regex regex = new Regex("\r\n");
		string[] lines = regex.Split(comment);
		string adjusted_comment = "";
		
		// loop through the messages
		for (int i=0;i < lines.Length; i++) 
		{
			if (lines[i].IndexOf("$CATEGORY$:") > -1)
			{
				try
				{
					categoryid = Convert.ToInt32(lines[i].Substring(lines[i].IndexOf("$CATEGORY$:") + 11));
				}
				catch (Exception)
				{
				}
			}
			else if (lines[i].IndexOf("$PRIORITY$:") > -1)
			{
				try
				{
					priorityid = Convert.ToInt32(lines[i].Substring(lines[i].IndexOf("$PRIORITY$:") + 11));
				}
				catch (Exception)
				{
				}
			}
			else if (lines[i].IndexOf("$ASSIGNEDTO$:") > -1)
			{
				try
				{
					assignedid = Convert.ToInt32(lines[i].Substring(lines[i].IndexOf("$ASSIGNEDTO$:") + 13));
				}
				catch (Exception)
				{
				}
			}
			else if (lines[i].IndexOf("$STATUS$:") > -1)
			{
				try
				{
					statusid = Convert.ToInt32(lines[i].Substring(lines[i].IndexOf("$STATUS$:") + 9));
				}
				catch (Exception)
				{
				}
			}
			else
			{
				if (adjusted_comment != "")
				{
					adjusted_comment += "\r\n";
				}
				adjusted_comment += lines[i];
			}
		}		
		
		int int_new_id = insert_bug(
			short_desc,
			(int) dr["us_id"],
			projectid,
			categoryid,
			priorityid,
			assignedid,
			statusid,
			0, // udf
			"","","", // project specific dropdown values
			adjusted_comment,
			from,
			null);

		if (mime_message != null)
		{
			add_attachments(mime_message, int_new_id);
			
			string auto_reply_text = Util.get_setting("AutoReplyText","");
			
			if (auto_reply_text != "")
			{
				// to prevent endless loop of replies when "from" email happens to be the
				// same as the "to" email, make sure that condition isn't true
				if (from.ToUpper() != email_to.ToUpper())
				{

					// Get the project from e-mail
					string to;

					sql = @"select
						pj_pop3_email_from
						from projects
						where pj_id = $pj";

					sql = sql.Replace("$pj", Convert.ToString(projectid));

					object project_email = dbutil.execute_scalar(sql);

					if (project_email != null)
					{
						to = Convert.ToString(project_email);
					}
					else
					{
						to = email_to;
					}

					string subject = short_desc + "  (DO NOT EDIT THIS:" + int_new_id + ")";

					Util.send_email(
						from, // we are responding to the from
						to,  
						"",
						short_desc,
						auto_reply_text);

				}
			}
		}
		
		Response.AddHeader("BTNET","OK:" + Convert.ToString(int_new_id));
		Response.Write ("OK:" + Convert.ToString(int_new_id));
		Response.End();

	}
	else
	{
		if (mime_message != null)
		{

			string headers = "";

			if (mime_message.Header.Subject != null && mime_message.Header.Subject != "")
			{
				headers = "Subject: " + SharpMimeTools.parserfc2047Header(mime_message.Header.Subject) + "\n";
			}

			if (mime_message.Header.To != null && mime_message.Header.To != "")
			{
				headers += "To: " + mime_message.Header.To + "\n";
			}

			if (mime_message.Header.Cc != null && mime_message.Header.Cc != "")
			{
				headers += "Cc: " + mime_message.Header.Cc + "\n";
			}

			if (headers != "")
			{
				comment = headers + "\n" + comment;
			}
		}

		// Add a comment to existing bug.
		insert_comment(bugid,
			(int) dr["us_id"],
		 	comment,
		 	from);

		if (mime_message != null)
		{
			add_attachments(mime_message, bugid);
		}

		 	
		send_notifications(UPDATE,
			bugid,
			(int) dr["us_id"]);
		 	
		Response.AddHeader("BTNET","OK");
		Response.Write ("OK");
		Response.End();
	}

}


///////////////////////////////////////////////////////////////////////
void add_attachments(SharpMimeMessage mime_message, int id)
{

	foreach (SharpMimeMessage part in mime_message)
	{
		string filename = part.Header.ContentDispositionParameters["filename"];
		if (filename != null && filename != "")
		{
			add_attachment(filename, part, id);
		}
	}
}

///////////////////////////////////////////////////////////////////////

void add_attachment(string filename, SharpMimeMessage part, int id)
{

	string missing_attachment_msg = "";
	string upload_folder = "";
	
	int max_upload_size = Convert.ToInt32(Util.get_setting("MaxUploadSize","100000"));
	if (part.Size > max_upload_size)
	{
		missing_attachment_msg = "ERROR: email attachment exceeds size limit."; 
	}
	else
	{

		upload_folder = Util.get_setting("UploadFolder","c:\\");
		if (!System.IO.Directory.Exists(upload_folder))
		{
			missing_attachment_msg = "ERROR: Unable to find UploadFolder for email attachment.";
		}
	}
		

	sql = @"declare @comment int
			select @comment = bc_id from bug_comments where bc_bug = $bg
			insert into bug_attachments
			(ba_bug, ba_file, ba_desc, ba_size, ba_uploaded_date, ba_uploaded_user, ba_content_type, ba_comment)
			values ($bg, N'$fi', N'$de', $si, getdate(), $us, N'$ct', @comment)

			declare @ba_id int
			set @ba_id = @@IDENTITY

			select @ba_id";

	if (missing_attachment_msg == "")
	{
		sql = sql.Replace("$de", "email attachment");
	}
	else
	{
		sql = sql.Replace("$de", missing_attachment_msg);
	}

	sql = sql.Replace("$bg", Convert.ToString(id));
	sql = sql.Replace("$fi", filename.Replace("'","''"));
	sql = sql.Replace("$si", Convert.ToString(part.Size));
	sql = sql.Replace("$us", Convert.ToString(security.this_usid));

	string content_type = part.Header.TopLevelMediaType + "/" + part.Header.SubType;
	sql = sql.Replace("$ct", content_type);

	// save the attachment's identity

	DataRow dr = dbutil.get_datarow (sql);

	int ba_id = (int) dr[0];

	if (missing_attachment_msg == "")
	{
		try
		{
			part.DumpBody(upload_folder, 
				Convert.ToString(id) + "_"      // bug id
				+ ba_id + "_"   // attachment id
				+ filename);
		}
		catch (Exception e2)
		{

			// clean up
			sql = @"update bug_attachments set ba_desc = '$de' where ba_id = $ba";
			sql = sql.Replace("$de", e2.Message.Replace("'","''"));
			dbutil.execute_nonquery(sql);
		}
	}

}


///////////////////////////////////////////////////////////////////////
void Page_Unload(Object sender, EventArgs e)
{
	if (dbutil != null) {dbutil.close();}
}



</script>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -