/*
* Box Socialâ„¢
* http://boxsocial.net/
* Copyright © 2007, David Lachlan Smith
*
* $Id:$
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see ");
input = input.Replace(" ");
input = input.Replace("
");
input = input.Replace("", string.Empty);
input = input.Replace("
", "
", "
"); //input = Regex.Replace(input, @"\
(\s)\<\/p\>", "", RegexOptions.Compiled); input = input.Replace("
", string.Empty); input = input.Replace("
\n
", string.Empty); input = input.Replace("\r\n
", string.Empty); return input; } private static bool EndTag(string input, int startIndex, string tag) { int notMine = 0; string startTag = "[" + tag + "]"; string endTag = "[/" + tag + "]"; int startTagLength = tag.Length + 2; int endTagLength = tag.Length + 3; int i = startIndex; byte nextIndexOf = 0; while (i <= (input.Length - endTagLength) && i >= startIndex) { if (nextIndexOf == 1) { notMine++; } if (nextIndexOf == 2) { if (notMine == 0) return true; notMine--; } nextIndexOf = 0; i++; int endT = input.IndexOf(startTag, i); int startT = input.IndexOf(endTag, i); if (endT <= 0) endT = startT + 1; else nextIndexOf = 1; if (startT <= 0) endT = endT + 1; else if (startT < endT) nextIndexOf = 2; i = Math.Min(endT, startT); } return false; } private static bool TagAllowed(string tag, BbcodeOptions options) { switch (tag) { case "img": if ((options & BbcodeOptions.ShowImages) != BbcodeOptions.ShowImages) { return false; } break; case "youtube": if ((options & BbcodeOptions.ShowVideo) != BbcodeOptions.ShowVideo) { return false; } break; case "flash": if ((options & BbcodeOptions.ShowFlash) != BbcodeOptions.ShowFlash) { return false; } break; case "silverlight": // we'll treat silverlight as flash for now if ((options & BbcodeOptions.ShowFlash) != BbcodeOptions.ShowFlash) { return false; } break; } return true; } private static bool ValidList(BbcodeTag tag) { BbcodeAttributes attr = new BbcodeAttributes(tag.Attributes); if (!attr.HasAttributes()) return true; if (Regex.IsMatch(attr.GetAttribute("default"), "^(circle|square)|([aAiI1]{1})$", RegexOptions.Compiled)) return true; return false; } public static string ParseSmilies(string input) { /*if (Global.SmiliesDataTable == null) { Global.SmiliesDataTable = Global.db.Query("SELECT * FROM smilies;"); } foreach (DataRow smile in Global.SmiliesDataTable.Rows) { input = input.Replace(smile["SmilieCode"].ToString(), "" + Parse(e.Attributes.GetAttribute("default")) + " wrote:
"; } else { e.PrefixText = "
" + Parse(e.Attributes.GetAttribute("default")) + " wrote:
"; } } else { if (!e.InList) { e.PrefixText = "quote:
"; } else { e.PrefixText = "
quote:
"; } } e.SuffixText = ""; } } private static void BbcodeBold(BbcodeEventArgs e) { if (e.Tag.Tag != "b") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = "*"; e.SuffixText = "*"; } else { e.PrefixText = ""; e.SuffixText = ""; } } private static void BbcodeItalic(BbcodeEventArgs e) { if (e.Tag.Tag != "i") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = "_"; e.SuffixText = "_"; } else { e.PrefixText = ""; e.SuffixText = ""; } } private static void BbcodeUnderline(BbcodeEventArgs e) { if (e.Tag.Tag != "u") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { e.PrefixText = ""; e.SuffixText = ""; } } private static void BbcodeStrikeout(BbcodeEventArgs e) { if (e.Tag.Tag != "s") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = "--"; e.SuffixText = "--"; } else { e.PrefixText = ""; e.SuffixText = ""; } } private static void BbcodeCode(BbcodeEventArgs e) { if (e.Tag.Tag != "code") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = "--- Code ---"; e.SuffixText = "------"; } else { if (!e.InList) { e.PrefixText = "Code
"; e.SuffixText = ""; } else { e.PrefixText = "Code
"; e.SuffixText = ""; } } } private static void BbcodeList(BbcodeEventArgs e) { if (e.Tag.Tag != "list") return; e.SetHandled(); if (e.StripTag) { } else { if (e.Attributes.GetAttribute("default") != null) { if (Regex.IsMatch(e.Attributes.GetAttribute("default"), "^[aA1iI]{1}$", RegexOptions.Compiled)) { if (!e.InList) { e.PrefixText = ""; e.SuffixText = "
"; } else { e.PrefixText = "
"; e.SuffixText = "
"; } } else if (Regex.IsMatch(e.Attributes.GetAttribute("default"), "^(circle|square)$", RegexOptions.Compiled)) { if (!e.InList) { e.PrefixText = ""; e.SuffixText = "
"; } else { e.PrefixText = "
"; e.SuffixText = "
"; } } else { e.AbortParse(); } } else { if (!e.InList) { e.PrefixText = ""; e.SuffixText = "
"; } else { e.PrefixText = "
"; e.SuffixText = "
"; } } } } private static void BbcodeListItem(BbcodeEventArgs e) { if (e.Tag.Tag != "*") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = " *"; e.SuffixText = string.Empty; } else { e.PrefixText = ""; e.SuffixText = " "; } } private static void BbcodeColour(BbcodeEventArgs e) { if (e.Tag.Tag != "color") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { try { System.Drawing.ColorTranslator.FromHtml(e.Attributes.GetAttribute("default")); } catch { e.AbortParse(); } e.PrefixText = ""; e.SuffixText = ""; } } private static void BbcodeSize(BbcodeEventArgs e) { if (e.Tag.Tag != "size") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { try { int fontSize = int.Parse(e.Attributes.GetAttribute("default")); if (fontSize > 300 || fontSize < 25) e.AbortParse(); } catch { e.AbortParse(); } e.PrefixText = ""; e.SuffixText = ""; } } private static void BbcodeH1(BbcodeEventArgs e) { if (e.Tag.Tag != "h1") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { e.PrefixText = ""; e.SuffixText = "
"; } } private static void BbcodeH2(BbcodeEventArgs e) { if (e.Tag.Tag != "h2") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { e.PrefixText = "
"; e.SuffixText = "
"; } } private static void BbcodeH3(BbcodeEventArgs e) { if (e.Tag.Tag != "h3") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { e.PrefixText = "
"; e.SuffixText = "
"; } } private static void BbcodeAlign(BbcodeEventArgs e) { if (e.Tag.Tag != "align") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { if (!Regex.IsMatch(e.Attributes.GetAttribute("default"), "^(left|right|center|justify)$", RegexOptions.Compiled)) e.AbortParse(); e.PrefixText = "
"; e.SuffixText = "
"; } } private static void BbcodeFloat(BbcodeEventArgs e) { if (e.Tag.Tag != "float") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { if (!e.InList) { if (!Regex.IsMatch(e.Attributes.GetAttribute("default"), "^(left|right)$", RegexOptions.Compiled)) e.AbortParse(); e.PrefixText = "
"; e.SuffixText = ""; } else { e.AbortParse(); } } } private static void BbcodeUrl(BbcodeEventArgs e) { if (e.Tag.Tag != "url") return; e.SetHandled(); if (e.StripTag) { if (e.Attributes.HasAttributes()) { e.PrefixText = string.Empty; e.SuffixText = " : (" + e.Attributes.GetAttribute("default") + ")"; } else { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } } else { if (e.Attributes.HasAttributes()) { if (Regex.IsMatch(e.Attributes.GetAttribute("default"), "^([\\w]+?://[\\w\\#$%&~/.\\-;:=,?@\\[\\]+]*?)$", RegexOptions.Compiled)) { e.PrefixText = ""; } else if (Regex.IsMatch(e.Attributes.GetAttribute("default"), "^((www|ftp)\\.[\\w\\#$%&~/.\\-;:=,?@\\[\\]+]*?)$", RegexOptions.Compiled)) { e.PrefixText = ""; } else { e.AbortParse(); } e.SuffixText = ""; } else { if (Regex.IsMatch(e.Contents, "^([\\w]+?://[\\w\\#$%&~/.\\-;:=,?@\\[\\]+]*?)$", RegexOptions.Compiled)) { e.PrefixText = ""; } else if (Regex.IsMatch(e.Contents, "^((www|ftp)\\.[\\w\\#$%&~/.\\-;:=,?@\\[\\]+]*?)$", RegexOptions.Compiled)) { e.PrefixText = ""; } else { e.AbortParse(); } e.SuffixText = ""; } } } private void BbcodeInternalUrl(BbcodeEventArgs e) { if (e.Tag.Tag != "iurl") return; e.SetHandled(); if (e.StripTag) { if (e.Attributes.HasAttributes()) { e.PrefixText = string.Empty; e.SuffixText = string.Format("(http://zinzam.com{0})", core.Uri.StripSid(e.Attributes.GetAttribute("default"))); } else { e.PrefixText = "(http://zinzam.com"; e.SuffixText = ")"; } } else { if (e.Attributes.HasAttributes()) { if (e.Attributes.HasAttribute("sid") && e.Attributes.GetAttribute("sid").ToLower() == "true") { e.PrefixText = ""; } else { e.PrefixText = ""; } e.SuffixText = ""; } else { e.PrefixText = ""; e.SuffixText = ""; } } } private static void BbcodeInline(BbcodeEventArgs e) { if (e.Tag.Tag != "inline") return; e.SetHandled(); if (e.Owner == null) { e.AbortParse(); } else { e.PrefixText = "
"; } } private static void BbcodeThumb(BbcodeEventArgs e) { if (e.Tag.Tag != "thumb") return; e.SetHandled(); if (e.Owner == null) { e.AbortParse(); } else { e.PrefixText = "
"; } } private static void BbcodeImage(BbcodeEventArgs e) { if (e.Tag.Tag != "img") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { if (!Regex.IsMatch(e.Contents, "^((http|ftp|https|ftps)://)([^ \\?&=\\#\\\"\\n\\r\\t<]*?(\\.(jpg|jpeg|gif|png)))$", RegexOptions.IgnoreCase)) e.AbortParse(); if (TagAllowed(e.Tag.Tag, e.Options)) { e.PrefixText = "
"; } else { e.PrefixText = "IMG: "; e.SuffixText = ""; } } } private static void BbcodeYouTube(BbcodeEventArgs e) { if (e.Tag.Tag != "youtube") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { string youTubeUrl = e.Contents; e.RemoveContents(); if (youTubeUrl.ToLower().StartsWith("http://")) { char[] splitChars = { '=', '?', '&' }; string[] argh = youTubeUrl.Split(splitChars); if (argh.Length <= 2) { e.AbortParse(); } for (int y = 0; y < argh.Length - 1; y++) { if (argh[y] == "v") { youTubeUrl = argh[y + 1]; } else if (y == argh.Length - 2) { e.AbortParse(); } } } if (TagAllowed(e.Tag.Tag, e.Options)) { youTubeUrl = "http://www.youtube.com/v/" + youTubeUrl; e.PrefixText = ""; e.SuffixText = string.Empty; } else { youTubeUrl = "http://www.youtube.com/watch?v=" + youTubeUrl; e.PrefixText = "YT: " + youTubeUrl; e.SuffixText = ""; } } } private static void BbcodeLaTeX(BbcodeEventArgs e) { if (e.Tag.Tag != "latex") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { string latexExpression = HttpUtility.UrlEncode(e.Contents).Replace("+", "%20"); e.RemoveContents(); e.PrefixText = "
"; e.SuffixText = string.Empty; } } private static void BbcodeFlash(BbcodeEventArgs e) { // TODO: flash bbcode if (e.Tag.Tag != "flash") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { if (TagAllowed(e.Tag.Tag, e.Options)) { } else { } } } private static void BbcodeSilverlight(BbcodeEventArgs e) { // TODO: silverlight bbcode if (e.Tag.Tag != "silverlight") return; e.SetHandled(); if (e.StripTag) { e.PrefixText = string.Empty; e.SuffixText = string.Empty; } else { if (TagAllowed(e.Tag.Tag, e.Options)) { } else { } } } private void BbcodeUser(BbcodeEventArgs e) { if (e.Tag.Tag != "user") return; e.SetHandled(); if (e.StripTag) { string key = e.Contents; e.RemoveContents(); long id = 0; if (key != "you") { try { id = long.Parse(key); } catch { e.AbortParse(); return; } } else { id = core.LoggedInMemberId; } if (id > 0) { core.LoadUserProfile(id); User userUser = core.PrimitiveCache[id]; if (e.Attributes.HasAttribute("ownership") && e.Attributes.GetAttribute("ownership") == "true") { e.PrefixText = userUser.DisplayNameOwnership; e.SuffixText = string.Empty; } else { e.PrefixText = userUser.DisplayName; e.SuffixText = string.Empty; } } else { e.PrefixText = "Anonymous"; e.SuffixText = string.Empty; } } else { if (core != null) { string key = e.Contents; e.RemoveContents(); long id = 0; if (key != "you") { try { id = long.Parse(key); } catch { e.AbortParse(); return; } } else { id = core.LoggedInMemberId; } if (id > 0) { core.LoadUserProfile(id); User userUser = core.PrimitiveCache[id]; if (e.Attributes.HasAttribute("ownership") && e.Attributes.GetAttribute("ownership") == "true") { e.PrefixText = string.Format("{0}", userUser.DisplayNameOwnership, userUser.Uri); e.SuffixText = string.Empty; } else { e.PrefixText = string.Format("{0}", userUser.DisplayName, userUser.Uri); e.SuffixText = string.Empty; } } else { e.PrefixText = "Anonymous"; e.SuffixText = string.Empty; } } } } public string Strip(string input) { return Parse(input, null, null, true); } public string Strip(string input, User viewer) { return Parse(input, viewer, null, true); } public string Strip(string input, User viewer, User postOwner) { return Parse(input, viewer, postOwner, true); } } public class BbcodeAttributes { Hashtable Attributes; public BbcodeAttributes(string input) { Attributes = new Hashtable(); bool inQuote = false; bool inValue = false; int length = input.Length; string param = string.Empty; string val = string.Empty; for (int i = 0; i < length; i++) { if (input[i].Equals('&') && i + 6 < input.Length && input.Substring(i, 6).Equals(""")) { inQuote = !inQuote; i += 5; } else { if (!inValue) { if (input[i].Equals('=')) { inValue = true; } else { param += input[i].ToString(); } } else if (!(!inQuote && input[i].Equals(' '))) { val += input[i].ToString(); } } if ((!inQuote && input[i].Equals(' ')) || i + 1 == length) { if (param.Length == 0) param = "default"; Attributes.Add(param.ToLower(), val); param = string.Empty; val = string.Empty; inValue = false; } } } public string GetAttribute(string key) { return Attributes[key] as string; } public bool HasAttributes() { return (Attributes.Count > 0); } internal bool HasAttribute(string key) { return Attributes.ContainsKey(key); } } }