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

📄 util.cs

📁 Bug管理系统
💻 CS
📖 第 1 页 / 共 3 页
字号:
		{
			// Get the matched string.
			return String.Format("<a href='mailto:{0}'>{0}</a>", m.ToString());
		}

		///////////////////////////////////////////////////////////////////////
		static string convert_bug_link(Match m) 
		{

			string link_marker = Util.get_setting("BugLinkMarker","bugid#");
			string just_number = m.ToString().Replace(link_marker,"");

			return "<a href=" 
				+ get_setting("AbsoluteUrlPrefix","http://127.0.0.1/") 
				+ "edit_bug.aspx?id=" 
				+ just_number
				+ ">" 
				+ m.ToString() 
				+ "</a>";

		}


		///////////////////////////////////////////////////////////////////////
		public static string format_comment(string s1)
		{

			string s2 = HttpUtility.HtmlEncode(s1);

			// convert urls to links
			s2 = reHyperlinks.Replace(
				s2, 
				new MatchEvaluator(Util.convert_to_hyperlink));			

			// convert email addresses to mailto links
			s2 = reEmail.Replace(
				s2, 
				new MatchEvaluator(Util.convert_to_email));	
				
			s2 = s2.Replace("\n","<br>");
			s2 = s2.Replace("  ","&nbsp; ");
			s2 = s2.Replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;");

			// convert references to other bugs to links
			string link_marker = Util.get_setting("BugLinkMarker","bugid#");
			Regex reLinkMarker = new Regex(link_marker + "[0-9]+");
			s2 = reLinkMarker.Replace(
				s2, 
				new MatchEvaluator(Util.convert_bug_link));	

			// wrap it up with the proper style
			return "<span class=cmt_text>" + s2 + "</span>";
		}

		///////////////////////////////////////////////////////////////////////
		public static string format_email_username(
			int bugid, 
			string email, 
			string username,
			string fullname)
		{
			if (email != null && email != "")
			{
				return "<a href="
				+ Util.get_setting("AbsoluteUrlPrefix","http://127.0.0.1/")
				+ "send_email.aspx?bg_id=" + Convert.ToString(bugid)
				+ "&to=" + email + ">"
				+ format_username(username, fullname)
				+ "</a>";
			}
			else
			{
				return format_username(username, fullname);
			}
		
		}
		
		
		///////////////////////////////////////////////////////////////////////
		public static string format_email_to(int bugid, string email)
		{
			return "<a href="
			+ Util.get_setting("AbsoluteUrlPrefix","http://127.0.0.1/")
			+ "send_email.aspx?bg_id=" + Convert.ToString(bugid)
			+ "&to=" + HttpUtility.UrlEncode(HttpUtility.HtmlDecode(email)) + ">"
			+ email
			+ "</a>";
		}


		///////////////////////////////////////////////////////////////////////
		public static string format_email_from(int bugid, string from)
		{
		
			string display_part = "";
			string email_part = "";
			int pos = from.IndexOf("<"); // "

			if (pos > 0)
			{
				display_part = from.Substring(0,pos);
				email_part = from.Substring(pos + 1,(from.Length -  pos) - 2);
			}
			else
			{
				email_part = from;
			}


			return display_part
				+ "<a href="
				+ Util.get_setting("AbsoluteUrlPrefix","http://127.0.0.1/")
				+ "send_email.aspx?bc_id=" 
				+ Convert.ToString(bugid)
				+ ">"
				+ email_part 
				+ "</a>";
		
		}

		///////////////////////////////////////////////////////////////////////
		public static string send_email(string to, string from, string cc, string subject, string body)
		{
			return send_email(to, from, cc, subject, body, System.Web.Mail.MailFormat.Text, "");
		}
		

		///////////////////////////////////////////////////////////////////////
		public static string send_email(string to, string from, string cc, string subject, string body, string attachment)
		{
			return send_email(to, from, cc, subject, body, System.Web.Mail.MailFormat.Text, attachment);
		}


		///////////////////////////////////////////////////////////////////////
		public static string send_email(string to, string from, string cc, string subject, string body, System.Web.Mail.MailFormat body_format)
		{
			return send_email(to, from, cc, subject, body, body_format, "");
		}


		///////////////////////////////////////////////////////////////////////
		public static string send_email(string to, string from, string cc, string subject, string body, 
			System.Web.Mail.MailFormat body_format, string attachment)
		{
			System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
			msg.To = to;
			msg.From = from;
			msg.Cc = cc;
			msg.Subject = subject;
			
			// workaround for a bug I don't understand...
			if (get_setting("SmtpForceReplaceOfBareLineFeeds","0") == "1")
			{
				body = body.Replace("\n", "\r\n");
			}
			
			msg.Body = body;
			msg.BodyFormat = body_format;
			
			
			string smtp_server = get_setting("SmtpServer","");
			if (smtp_server != "")
			{
				System.Web.Mail.SmtpMail.SmtpServer = smtp_server;
			}

			string smtp_password = get_setting("SmtpServerAuthenticatePassword","");
			
			if (smtp_password != "")
			{
				msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]= smtp_password;
				msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]=1;
				msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] =
					Util.get_setting("SmtpServerAuthenticateUser","");
			}

			string smtp_pickup = get_setting("SmtpServerPickupDirectory","");			
			if (smtp_pickup != "")
			{
				msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory"] = smtp_pickup;
			}
			

			string send_using = get_setting("SmtpSendUsing","");			
			if (send_using != "")
			{
				msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = send_using;
			}
			
			
			string email_path = "";
			if (attachment != "")
			{
				// remove the bug and attachment prefixes from the filename
				string attachment_file_file = System.IO.Path.GetFileName(attachment);
				
				int pos = attachment_file_file.IndexOf("_");
				if (pos > -1)
				{
					pos = attachment_file_file.IndexOf("_",pos+1);
					if (pos > -1)
					{
						email_path = System.IO.Path.GetDirectoryName(attachment)
							+ "\\" + attachment_file_file.Substring(pos+1);						
					}
				}
				
				
				System.Web.Mail.MailAttachment mail_attachment;
				
				// use the file without the attachment prefixes
				if (email_path != "")
				{
					
					System.IO.File.Copy(attachment,email_path);

					mail_attachment = new System.Web.Mail.MailAttachment(
						email_path,
						System.Web.Mail.MailEncoding.Base64);
					msg.Attachments.Add(mail_attachment);				
					// intentionally not deleting file here - see below	
				}
				else
				{
					mail_attachment = new System.Web.Mail.MailAttachment(
						attachment,
						System.Web.Mail.MailEncoding.Base64);
					msg.Attachments.Add(mail_attachment);				
				}
					
			}
			
			
			try
			{
				System.Web.Mail.SmtpMail.Send(msg);
				
				// We delete late here because testing showed that SmtpMail class
				// got confused when we deleted too soon. 
				if (email_path != "")
				{
					System.IO.File.Delete(email_path);
				}

				return "";
			}
			catch (Exception e)
			{
				write_to_log ("There was a problem sending email.   Check settings in Web.config.");
				write_to_log ("TO:" + to);
				write_to_log ("FROM:" + from);
				write_to_log ("SUBJECT:" + subject);
				write_to_log (e.GetBaseException().Message.ToString());
				return(e.GetBaseException().Message);
			}		


		}
		
		///////////////////////////////////////////////////////////////////////
		public static string alter_sql_per_project_permissions(string sql, int usid)
		{

			string project_permissions_sql;
			
			string dpl = Util.get_setting("DefaultPermissionLevel","2");
			
			if (dpl == "0")
			{
				project_permissions_sql = @" (bg_project in (
					select pu_project
					from project_user_xref
					where pu_user = $us
					and pu_permission_level > 0)) ";
			}
			else
			{
				project_permissions_sql = @" (bg_project not in (
					select pu_project
					from project_user_xref
					where pu_user = $us
					and pu_permission_level = 0)) ";
			}

			project_permissions_sql 
				= project_permissions_sql.Replace("$us",Convert.ToString(usid));


			// figure out where to alter sql for project permissions

			string bug_sql = sql;
	
			int pos = sql.ToUpper().IndexOf("WHERE");
			if (pos != -1)
			{
				bug_sql = bug_sql.Substring(0,pos+5) + 
						project_permissions_sql + 
						"AND " + bug_sql.Substring(pos+5);
			}
			else
			{
				pos = sql.ToUpper().IndexOf("ORDER");
				if (pos != -1)
				{
					bug_sql = bug_sql.Substring(0,pos) + 
						"WHERE" + project_permissions_sql + 
						bug_sql.Substring(pos);
				}
				else
				{
					bug_sql += " WHERE " + project_permissions_sql;
				}
			}

			return bug_sql;

		}
		
		///////////////////////////////////////////////////////////////////////
		public static DataSet get_custom_columns(DbUtil dbutil)
		{

			return dbutil.get_dataset(
				@"select sc.name, st.[name] [datatype], sc.length, sc.xprec, sc.xscale, sc.isnullable, sc.colorder	
				from syscolumns sc
				inner join systypes st on st.xusertype = sc.xusertype
				inner join sysobjects so on sc.id = so.id
				where so.name = 'bugs'
				and st.[name] <> 'sysname'
				and sc.name not in ('rowguid',
				'bg_id',
				'bg_short_desc',
				'bg_reported_user',
				'bg_reported_date',
				'bg_status',
				'bg_priority',
				'bg_category',
				'bg_project',
				'bg_assigned_to_user',
				'bg_last_updated_user',
				'bg_last_updated_date',
				'bg_user_defined_attribute',
				'bg_project_custom_dropdown_value1',
				'bg_project_custom_dropdown_value2',
				'bg_project_custom_dropdown_value3')
				order by sc.id, sc.colorder");				
	
		}


		///////////////////////////////////////////////////////////////////////
		public static void delete_bug(DbUtil dbutil, int bugid)
		{

			// delete attachements
			
			string id = Convert.ToString(bugid);

			string upload_folder = Util.get_setting("UploadFolder","c:\\");
			string sql = @"select ba_id, ba_file from bug_attachments where ba_bug = $bg";
			sql = sql.Replace("$bg", id);
			DataSet ds = dbutil.get_dataset(sql);
			foreach (DataRow dr in ds.Tables[0].Rows)
			{

				// create path
				StringBuilder path = new StringBuilder(upload_folder);
				path.Append("\\");
				path.Append(id);
				path.Append("_");
				path.Append(Convert.ToString(dr["ba_id"]));
				path.Append("_");
				path.Append(Convert.ToString(dr["ba_file"]));
				if (System.IO.File.Exists(path.ToString()))
				{
					System.IO.File.Delete(path.ToString());
				}

			}

			// delete the database entries

			sql = @"delete from bug_comments where bc_bug = $bg
				delete from bug_attachments where ba_bug = $bg
				delete from bug_subscriptions where bs_bug = $bg
				delete from bugs where bg_id = $bg";

			sql = sql.Replace("$bg", id);
			dbutil.execute_nonquery(sql);
		
		
		}
		
		///////////////////////////////////////////////////////////////////////
		public static string encrypt_string_using_MD5(string s)
		{

			byte[] byte_array = System.Text.Encoding.Default.GetBytes(s);

			System.Security.Cryptography.HashAlgorithm alg = 
				System.Security.Cryptography.HashAlgorithm.Create("MD5");

			byte[] byte_array2 = alg.ComputeHash(byte_array);

			System.Text.StringBuilder sb 
				= new System.Text.StringBuilder(byte_array2.Length);

			foreach(byte b in byte_array2)
			{
				sb.AppendFormat("{0:X2}", b);
			}

			return sb.ToString();
		}		


		///////////////////////////////////////////////////////////////////////
		public static DataRow get_bug_datarow(
			int bugid, 
			DataSet ds_custom_cols, 
			Security security, 
			DbUtil dbutil)
		{
			string custom_cols_placeholder = "";
			
			string sql = @"select
					bg_id [id],
					bg_short_desc [short_desc],
					isnull(ru.us_username,'[deleted user]') [reporter],
					isnull(ru.us_lastname + ', ' + ru.us_firstname,'') [reporter_fullname],
					bg_reported_date [reported_date],
					isnull(lu.us_username,'') [last_updated_user],
					isnull(lu.us_lastname + ', ' + lu.us_firstname,'') [last_updated_fullname],
					
					
					bg_last_updated_date [last_updated_date],
					isnull(bg_project,0) [project],
					isnull(pj_name,'[no project]') [current_project],

					isnull(bg_category,0) [category],
					isnull(ct_name,'') [category_name],

					isnull(bg_priority,0) [priority],
					isnull(pr_name,'') [priority_name],

					isnull(bg_status,0) [status],
					isnull(st_name,'') [status_name],

					isnull(bg_user_defined_attribute,0) [udf],
					isnull(udf_name,'') [udf_name],

					isnull(bg_assigned_to_user,0) [assigned_to_user],
					isnull(asg.us_username,'[not assigned]') [assigned_to_username],
					isnull(asg.us_lastname + ', ' + asg.us_firstname,'[not assigned]') [assigned_to_fullname],

					isnull(bs_id,0) [subscribed],
					isnull(pu_permission_level,$dpl) [pu_permission_level],
					
					isnull(bg_project_custom_dropdown_value1,'') [bg_project_custom_dropdown_value1],
					isnull(bg_project_custom_dropdown_value2,'') [bg_project_custom_dropdown_value2],
					isnull(bg_project_custom_dropdown_value3,'') [bg_project_custom_dropdown_value3]
					
					$custom_cols_placeholder
					from bugs
					left outer join user_defined_attribute on bg_user_defined_attribute = udf_id
					left outer join statuses on bg_status = st_id
					left outer join priorities on bg_priority = pr_id
					left outer join categories on bg_category = ct_id
					left outer join projects on bg_project = pj_id
					left outer join users asg on bg_assigned_to_user = asg.us_id
					left outer join users ru on bg_reported_user = ru.us_id
					left outer join users lu on bg_last_updated_user = lu.us_id
					left outer join bug_subscriptions on bs_bug = bg_id and bs_user = $us

⌨️ 快捷键说明

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