static public string RepairHtml(yaf.pages.ForumPage basePage, string html, bool bAllowHtml) { if (!bAllowHtml) { html = BBCode.EncodeHTML(html); } else { // get allowable html tags string tStr = basePage.BoardSettings.AcceptedHTML; string[] AllowedTags = tStr.Split(','); RegexOptions options = RegexOptions.IgnoreCase; MatchCollection m = Regex.Matches(html, "<.*?>", options); for (int i = m.Count - 1; i >= 0; i--) { string tag = html.Substring(m[i].Index + 1, m[i].Length - 1).Trim().ToLower(); if (!IsValidTag(tag, AllowedTags)) { html = html.Remove(m[i].Index, m[i].Length); // just don't show this tag for now //string tmp = System.Web.HttpContext.Current.Server.HtmlEncode(html.Substring(m[i].Index,m[i].Length)); //html = html.Insert(m[i].Index,tmp); } } } return(html); }
static public string FormatMessage(yaf.pages.ForumPage basePage, string Message, MessageFlags mFlags) { // do html damage control Message = RepairHtml(basePage, Message, mFlags.IsHTML); // convert spaces if bbcode (causes too many problems) /*if (mFlags.IsBBCode) * { * Message = Message.Replace(" "," "); * }*/ // do BBCode and Smilies... Message = BBCode.MakeHtml(basePage, Message, mFlags.IsBBCode); RegexOptions options = RegexOptions.IgnoreCase /*| RegexOptions.Singleline | RegexOptions.Multiline*/; //Email -- RegEx VS.NET Message = Regex.Replace(Message, @"(?<before>^|[ ]|<br/>)(?<email>\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)", "${before}<a href=\"mailto:${email}\">${email}</a>", options); //URL (http://) -- RegEx http://www.dotnet247.com/247reference/msgs/2/10022.aspx Message = Regex.Replace(Message, "(?<before>^|[ ]|<br/>)(?<!href=\")(?<!src=\")(?<url>(http://|https://|ftp://)(?:[\\w-]+\\.)+[\\w-]+(?:/[\\w-./?%&=;,]*)?)", "${before}<a href=\"${url}\">${url}</a>", options); //URL (www) -- RegEx http://www.dotnet247.com/247reference/msgs/2/10022.aspx Message = Regex.Replace(Message, @"(?<before>^|[ ]|<br/>)(?<!http://)(?<url>www\.(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&=;,]*)?)", "${before}<a href=\"http://${url}\">${url}</a>", options); // jaben : moved word replace to reusable function in class utils Message = Utils.BadWordReplace(Message); return(Message); }
static public DataTable GetSmilies(yaf.pages.ForumPage basePage) { DataTable dt = (DataTable)System.Web.HttpContext.Current.Cache["Smilies"]; if (dt == null) { dt = DB.smiley_list(basePage.PageBoardID, null); System.Web.HttpContext.Current.Cache["Smilies"] = dt; } return(dt); }
/// <summary> /// Formats a message to HTML by: /// 1. Converting "Forum Code" to HTML /// 2. Converting carriage returns to <br/> /// 3. Converting smiles code to img tags /// </summary> /// <param name="basePage">Forum Page</param> /// <param name="Message">Message to Format</param> /// <returns>Formatted Message</returns> static public string ForumCodeToHtml(yaf.pages.ForumPage basePage, string Message) { #if true return(Message); #else string strReturn = iConvertForumCode(basePage, Message); strReturn = strReturn.Replace("\n", "<br />"); strReturn = strReturn.Replace("\r", ""); strReturn = iAddSmiles(basePage, strReturn); return(strReturn); #endif }
/// <summary> /// Adds smiles into the code. /// </summary> /// <param name="basePagee">Forum base page</param> /// <param name="Message">Text to add smiles to.</param> /// <returns>Processed text with smiles added.</returns> static public string iAddSmiles(yaf.pages.ForumPage basePage, string Message) { DataTable dtSmileys = GetSmilies(basePage); string strTemp = Message; foreach (DataRow row in dtSmileys.Rows) { string code = row["Code"].ToString(); code = code.Replace("&", "&"); code = code.Replace("\"", """); strTemp = strTemp.Replace(code.ToLower(), String.Format("<img src=\"{0}\" alt=\"{1}\">", basePage.Smiley(Convert.ToString(row["Icon"])), basePage.Server.HtmlEncode(row["Emoticon"].ToString()))); strTemp = strTemp.Replace(code.ToUpper(), String.Format("<img src=\"{0}\" alt=\"{1}\">", basePage.Smiley(Convert.ToString(row["Icon"])), basePage.Server.HtmlEncode(row["Emoticon"].ToString()))); } return(strTemp); }
static public DataTable TopicTimes(yaf.pages.ForumPage basePage) { using (DataTable dt = new DataTable("TopicTimes")) { dt.Columns.Add("TopicText", typeof(string)); dt.Columns.Add("TopicValue", typeof(int)); string[] tTextArray = { "all", "last_day", "last_two_days", "last_week", "last_two_weeks", "last_month", "last_two_months", "last_six_months", "last_year" }; string[] tTextArrayProp = { "All", "Last Day", "Last Two Days", "Last Week", "Last Two Weeks", "Last Month", "Last Two Months", "Last Six Months", "Last Year" }; for (int i = 0; i < 8; i++) { DataRow dr = dt.NewRow(); dr["TopicText"] = (basePage == null) ? tTextArrayProp[i] : basePage.GetText(tTextArray[i]); dr["TopicValue"] = i; dt.Rows.Add(dr); } return(dt); } }
static public string MakeHtml(yaf.pages.ForumPage basePage, string bbcode, bool DoFormatting) { System.Collections.ArrayList codes = new System.Collections.ArrayList(); const string codeFormat = ".code@{0}."; Match m = r_code2.Match(bbcode); int nCodes = 0; while (m.Success) { string before_replace = m.Groups[0].Value; string after_replace = m.Groups["inner"].Value; try { HighLighter hl = new HighLighter(); hl.ReplaceEnter = true; after_replace = hl.colorText(after_replace, System.Web.HttpContext.Current.Server.MapPath(Data.ForumRoot + "defs/"), m.Groups["language"].Value); } catch (Exception x) { if (basePage.IsAdmin) { basePage.AddLoadMessage(x.Message); } after_replace = FixCode(after_replace); } bbcode = bbcode.Replace(before_replace, string.Format(codeFormat, nCodes++)); codes.Add(string.Format("<div class='code'><b>Code:</b><div class='innercode'>{0}</div></div>", after_replace)); m = r_code2.Match(bbcode); } m = r_code1.Match(bbcode); while (m.Success) { string before_replace = m.Groups[0].Value; string after_replace = FixCode(m.Groups["inner"].Value); bbcode = bbcode.Replace(before_replace, string.Format(codeFormat, nCodes++)); codes.Add(string.Format("<div class='code'><b>Code:</b><div class='innercode'>{0}</div></div>", after_replace)); m = r_code1.Match(bbcode); } m = r_size.Match(bbcode); while (m.Success) { ///Console.WriteLine("{0}",m.Groups["size"]); int i = GetNumber(m.Groups["size"].Value); string tmp; if (i < 1) { tmp = m.Groups["inner"].Value; } else if (i > 9) { tmp = string.Format("<span style=\"font-size:{1}\">{0}</span>", m.Groups["inner"].Value, GetFontSize(9)); } else { tmp = string.Format("<span style=\"font-size:{1}\">{0}</span>", m.Groups["inner"].Value, GetFontSize(i)); } bbcode = bbcode.Substring(0, m.Groups[0].Index) + tmp + bbcode.Substring(m.Groups[0].Index + m.Groups[0].Length); m = r_size.Match(bbcode); } bbcode = FormatMsg.iAddSmiles(basePage, bbcode); if (DoFormatting) { NestedReplace(ref bbcode, r_bold, "<b>${inner}</b>"); NestedReplace(ref bbcode, r_strike, "<s>${inner}</s>"); NestedReplace(ref bbcode, r_italic, "<i>${inner}</i>"); NestedReplace(ref bbcode, r_underline, "<u>${inner}</u>"); // e-mails NestedReplace(ref bbcode, r_email2, "<a href=\"mailto:${email}\">${inner}</a>", new string[] { "email" }); NestedReplace(ref bbcode, r_email1, "<a href=\"mailto:${inner}\">${inner}</a>"); // urls if (basePage.BoardSettings.BlankLinks) { NestedReplace(ref bbcode, r_url2, "<a target=\"_blank\" href=\"${http}${url}\">${inner}</a>", new string[] { "url", "http" }, new string[] { "", "http://" }); NestedReplace(ref bbcode, r_url1, "<a target=\"_blank\" href=\"${http}${inner}\">${http}${inner}</a>", new string[] { "http" }, new string[] { "http://" }); } else { NestedReplace(ref bbcode, r_url2, "<a href=\"${http}${url}\">${inner}</a>", new string[] { "url", "http" }, new string[] { "", "http://" }); NestedReplace(ref bbcode, r_url1, "<a href=\"${http}${inner}\">${http}${inner}</a>", new string[] { "http" }, new string[] { "http://" }); } // font NestedReplace(ref bbcode, r_font, "<span style=\"font-family:${font}\">${inner}</span>", new string[] { "font" }); NestedReplace(ref bbcode, r_color, "<span style=\"color:${color}\">${inner}</span>", new string[] { "color" }); // bullets bbcode = r_bullet.Replace(bbcode, "<li>"); NestedReplace(ref bbcode, r_list4, "<ol type=\"i\">${inner}</ol>"); NestedReplace(ref bbcode, r_list3, "<ol type=\"a\">${inner}</ol>"); NestedReplace(ref bbcode, r_list2, "<ol>${inner}</ol>"); NestedReplace(ref bbcode, r_list2, "<ul>${inner}</ul>"); // alignment NestedReplace(ref bbcode, r_center, "<div align=\"center\">${inner}</div>"); NestedReplace(ref bbcode, r_left, "<div align=\"left\">${inner}</div>"); NestedReplace(ref bbcode, r_right, "<div align=\"right\">${inner}</div>"); // image NestedReplace(ref bbcode, r_img, "<img src=\"${inner}\"/>"); bbcode = r_hr.Replace(bbcode, "<hr noshade/>"); bbcode = r_br.Replace(bbcode, "<br/>"); } while (r_quote2.IsMatch(bbcode)) { bbcode = r_quote2.Replace(bbcode, "<div class='quote'><b>${quote} wrote:</b><div class='innerquote'>${inner}</div></div>"); } while (r_quote1.IsMatch(bbcode)) { bbcode = r_quote1.Replace(bbcode, "<div class='quote'><b>Quote:</b><div class='innerquote'>${inner}</div></div>"); } m = r_post.Match(bbcode); while (m.Success) { string link = Forum.GetLink(Pages.posts, "m={0}#{0}", m.Groups["post"]); if (basePage.BoardSettings.BlankLinks) { bbcode = bbcode.Replace(m.Groups[0].ToString(), string.Format("<a target=\"_blank\" href=\"{0}\">{1}</a>", link, m.Groups["inner"])); } else { bbcode = bbcode.Replace(m.Groups[0].ToString(), string.Format("<a href=\"{0}\">{1}</a>", link, m.Groups["inner"])); } m = r_post.Match(bbcode); } m = r_topic.Match(bbcode); while (m.Success) { string link = Forum.GetLink(Pages.posts, "t={0}", m.Groups["topic"]); if (basePage.BoardSettings.BlankLinks) { bbcode = bbcode.Replace(m.Groups[0].ToString(), string.Format("<a target=\"_blank\" href=\"{0}\">{1}</a>", link, m.Groups["inner"])); } else { bbcode = bbcode.Replace(m.Groups[0].ToString(), string.Format("<a href=\"{0}\">{1}</a>", link, m.Groups["inner"])); } m = r_topic.Match(bbcode); } while (nCodes > 0) { bbcode = bbcode.Replace(string.Format(codeFormat, --nCodes), codes[nCodes].ToString()); } return(bbcode); }
/// <summary> /// Formats message by converting "Forum Code" to HTML. /// </summary> /// <param name="basePage">Forum Page</param> /// <param name="Message">Message to Convert</param> /// <returns>Converted Message Text</returns> static protected string iConvertForumCode(yaf.pages.ForumPage basePage, string Message) { string tmp = ""; bool bInCode = false; for (int i = 0; i < Message.Length; i++) { if (Message[i] == '[') { int e1 = Message.IndexOf(']', i); int e2 = Message.IndexOf('=', i); if (e1 > 0) { bool bNone = false; string cmd, arg = null; if (e2 < 0 || e2 > e1) { cmd = Message.Substring(i + 1, e1 - i - 1); arg = null; } else { cmd = Message.Substring(i + 1, e2 - i - 1); arg = Message.Substring(e2 + 1, e1 - e2 - 1); arg = arg.Trim(); arg = basePage.Server.HtmlDecode(arg); if (arg.Length > 2 && arg[0] == '"' && arg[arg.Length - 1] == '"') { arg = arg.Substring(1, arg.Length - 2); } } cmd = cmd.ToLower(); if (!bInCode || cmd == "/code") { switch (cmd) { case "b": tmp += "<b>"; break; case "/b": tmp += "</b>"; break; case "i": tmp += "<em>"; break; case "/i": tmp += "</em>"; break; case "u": tmp += "<u>"; break; case "/u": tmp += "</u>"; break; case "url": if (arg != null) { if (basePage.BoardSettings.BlankLinks) { tmp += String.Format("<a target=\"_blank\" href=\"{0}\">", arg); } else { tmp += String.Format("<a target=\"_top\" href=\"{0}\">", arg); } } else { tmp += "<a>"; } break; case "/url": tmp += "</a>"; break; case "img": tmp += "<img src=\""; break; case "/img": tmp += "\"/>"; break; case "color": if (arg != null) { tmp += String.Format("<span style=\"color:{0}\">", arg); } else { tmp += "<span>"; } break; case "/color": tmp += "</span>"; break; case "code": tmp += "<pre>"; bInCode = true; break; case "/code": tmp += "</pre>"; bInCode = false; break; default: bNone = true; break; } } else { bNone = true; } if (!bNone) { i = e1; continue; } } } tmp += Message[i]; } return(tmp); }