예제 #1
0
        public static string ParseBBCode(this string input, Color?defaultColor = null, int defaultSize = 2)
        {
            if (String.IsNullOrWhiteSpace(input))
            {
                return(input ?? String.Empty);
            }

            input = RegexLineBreak.Replace(input, "<BR>");
            input = RegexCenterText.Replace(input, "<CENTER>$1</CENTER>");
            input = RegexLeftText.Replace(input, "<LEFT>$1</LEFT>");
            input = RegexRightText.Replace(input, "<RIGHT>$1</RIGHT>");
            input = RegexSmallText.Replace(input, "<SMALL>$1</SMALL>");
            input = RegexBigText.Replace(input, "<BIG>$1</BIG>");
            input = RegexBoldText.Replace(input, "<B>$1</B>");
            input = RegexItalicText.Replace(input, "<I>$1</I>");
            input = RegexUnderlineText.Replace(input, "<U>$1</U>");
            input = RegexStrikeOutText.Replace(input, "<S>$1</S>");

            input = RegexUrl.Replace(input, "<A HREF=\"$1\">$1</A>");
            input = RegexUrlAnchored.Replace(input, "<A HREF=\"$1\">$2</A>");

            input = RegexSizeAnchored.Replace(input, "<BASEFONT SIZE=$1>$2<BASEFONT SIZE=" + defaultSize + ">");

            if (defaultColor != null)
            {
                input = RegexColorAnchored.Replace(
                    input, "<BASEFONT COLOR=$1>$2<BASEFONT COLOR=#" + defaultColor.Value.ToArgb().ToString("X6") + ">");
            }
            else
            {
                input = RegexColorAnchored.Replace(input, "<BASEFONT COLOR=$1>$2");
            }

            return(input);
        }
예제 #2
0
        public static string ParseBBCode(
            this string input,
            Color?defaultColor = null,
            int defaultSize    = 2,
            bool imgAsLink     = true,
            bool stripMisc     = false)
        {
            if (String.IsNullOrWhiteSpace(input))
            {
                return(input ?? String.Empty);
            }

            input = RegexLineBreak.Replace(input, "<br>");
            input = RegexCenterText.Replace(input, "<center>$1</center>");
            input = RegexLeftText.Replace(input, "<left>$1</left>");
            input = RegexRightText.Replace(input, "<right>$1</right>");
            input = RegexSmallText.Replace(input, "<small>$1</small>");
            input = RegexBigText.Replace(input, "<big>$1</big>");
            input = RegexBoldText.Replace(input, "<b>$1</b>");
            input = RegexItalicText.Replace(input, "<i>$1</i>");
            input = RegexUnderlineText.Replace(input, "<u>$1</u>");
            input = RegexStrikeOutText.Replace(input, "<s>$1</s>");

            input = RegexUrl.Replace(input, "<a href=\"$1\">$1</a>");
            input = RegexUrlAnchored.Replace(input, "<a href=\"$1\">$2</a>");

            if (imgAsLink)
            {
                input = RegexImage.Replace(input, "<a href=\"$1\">$1</a>");
                input = RegexImageAnchored.Replace(input, "<a href=\"$1\">$2</a>");
            }

            input = RegexSizeAnchored.Replace(input, "<basefont size=$1>$2<basefont size=" + defaultSize + ">");

            if (defaultColor != null)
            {
                input = RegexColorAnchored.Replace(
                    input,
                    "<basefont color=$1>$2<basefont color=#" + defaultColor.Value.ToRgb().ToString("X6") + ">");
            }
            else
            {
                input = RegexColorAnchored.Replace(input, "<basefont color=$1>$2");
            }

            if (stripMisc)
            {
                input = RegexStripMisc.Replace(input, "($1) $2");
            }

            return(input);
        }
예제 #3
0
        /// <summary>
        /// Validate the parameter. Error messages are added to the errors collection
        /// if there are issues with the parameter.
        /// </summary>
        public void Validate(IHttpPostParam parameter, ArrayRig <string> errors)
        {
            // is default validatation enabled?
            if (DefaultValidation)
            {
                // yes, is the parameter required?
                bool required = this._attributes.ContainsKey("required");

                if (required)
                {
                    // yes, was the parameter retrieved?
                    if (parameter == null)
                    {
                        // no, add an error
                        errors.Add("Missing a required parameter.");

                        // is the custom validation set?
                        if (OnValidate != null)
                        {
                            // yes, run the custom validation
                            OnValidate.Add(parameter, errors);
                            OnValidate.Run();
                        }

                        return;
                    }
                }
                else if (parameter == null)
                {
                    return;
                }

                string name;

                // validate the input value name
                if (parameter.Params.TryGetValue("name", out name))
                {
                    if (!name.Equals(this["name"], StringComparison.Ordinal))
                    {
                        errors.Add("Input element name mismatch.");
                        return;
                    }
                }
                else
                {
                    name = this["name"];
                    if (name == null)
                    {
                        errors.Add("Attribute 'name' missing.");
                        return;
                    }
                }

                // has the friendly name been assigned? yes, replace it in messages.
                if (!string.IsNullOrEmpty(FriendlyName))
                {
                    name = FriendlyName;
                }

                // get the post parameter as its correct type
                HttpPostParam <string>           paramString = parameter as HttpPostParam <string>;
                HttpPostParam <ArrayRig <byte> > paramBinary = null;
                if (paramString == null)
                {
                    paramBinary = parameter as HttpPostParam <ArrayRig <byte> >;
                }
                else if (string.IsNullOrEmpty(paramString.Value))
                {
                    // is the parameter required? yes, add an error
                    if (required)
                    {
                        errors.Add(name + " is required.");
                    }
                    // no, skip validation
                    else
                    {
                        return;
                    }
                }
                else if (paramString.Value.Length > MaxLength)
                {
                    errors.Add("Exceeded maximum characters (" + MaxLength + ") for " + name + ".");
                    return;
                }
                else if (paramString.Value.Length < MinLength)
                {
                    errors.Add("Entered value was shorter than the minimum length (" + MinLength + ") for " + name + ".");
                    return;
                }
                else if (Regex != null && !Regex.IsMatch(paramString.Value))
                {
                    // has the title for the input been specified?
                    errors.Add(this["title"] ?? "Invalid entry for " + name + ".");
                    return;
                }

                if ((paramString == null || paramString.Value == string.Empty) &&
                    (paramBinary == null || paramBinary.Value.Count == 0))
                {
                    if (required || _type == InputType.Hidden)
                    {
                        errors.Add(name + " is required.");
                    }
                    return;
                }

                // perform default validation based on type
                switch (_type)
                {
                case InputType.Checkbox:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (!paramString.Value.Equals(this["value"]))
                    {
                        errors.Add(name + " has an incorrect value.");
                        return;
                    }
                    break;

                case InputType.Color:
                    if (paramString == null)
                    {
                        errors.Add("Please select a color for " + name + ".");
                        return;
                    }
                    WebColor color;
                    if (!WebColor.TryParse(paramString.Value, out color))
                    {
                        errors.Add("Incorrect hexadecimal color format. Should be #XXXXXXXX or #XXXXXX or #XXX.");
                        return;
                    }
                    break;

                case InputType.Date:
                    if (paramString == null)
                    {
                        errors.Add("Please select " + name + ".");
                        return;
                    }
                    DateTime date;
                    if (!DateTime.TryParse(paramString.Value, out date))
                    {
                        errors.Add("Received an incorrect date format.");
                        return;
                    }
                    break;

                case InputType.DatetimeLocal:
                    if (paramString == null)
                    {
                        errors.Add("Please select a date-time for " + name + ".");
                        return;
                    }
                    DateTime dateTime;
                    if (!DateTime.TryParse(paramString.Value, out dateTime))
                    {
                        errors.Add("Received an incorrect date-time format.");
                        return;
                    }
                    break;

                case InputType.Email:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (!Efz.Validate.Email(paramString.Value))
                    {
                        errors.Add("Email appears malformed.");
                        return;
                    }
                    break;

                case InputType.File:
                    if (paramBinary == null)
                    {
                        errors.Add("You didn't choose a file for " + name + ".");
                        return;
                    }
                    if (paramBinary.Value.Count == 0)
                    {
                        errors.Add("Required file parameter is missing.");
                    }
                    string filename;
                    if (!paramBinary.Params.TryGetValue("filename", out filename) ||
                        !Fs.ParseFileName(ref filename))
                    {
                        errors.Add("The file name of " + name + " was invalid.");
                        return;
                    }
                    // ensure the content type matches accepted types
                    string contentType;
                    if (!paramBinary.Params.TryGetValue("content-type", out contentType))
                    {
                        errors.Add("Content type of " + name + " was not provided.");
                        return;
                    }
                    // check the accepted file types
                    string accepted;
                    if (_attributes.TryGetValue("accept", out accepted))
                    {
                        var extension = Mime.GetExtension(contentType);
                        if (extension == string.Empty)
                        {
                            errors.Add("Content type '" + contentType + "' was invalid.");
                            return;
                        }
                        if (!accepted.Contains(Chars.Stop + extension + Chars.Comma) && !accepted.EndsWith(Chars.Stop + extension, StringComparison.Ordinal))
                        {
                            errors.Add("Accepted file types include '" + accepted + "'.");
                            return;
                        }
                    }
                    break;

                case InputType.Month:
                    if (paramString == null)
                    {
                        errors.Add("You didn't choose a month for " + name + ".");
                        return;
                    }
                    if (paramString.Value.Length != 7 || !RegexMonth.IsMatch(paramString.Value))
                    {
                        errors.Add(name + " seems malformed.");
                        return;
                    }
                    if (paramString.Value[4] == Chars.Dash)
                    {
                        paramString.Value = paramString.Value.Substring(5) + Chars.Dash + paramString.Value.Substring(0, 4);
                    }
                    break;

                case InputType.Number:
                    int number;
                    if (!int.TryParse(paramString.Value, out number))
                    {
                        errors.Add("The " + name + " seems malformed.");
                        return;
                    }
                    if (_attributes.ContainsKey("min"))
                    {
                        int min = _attributes["min"].ToInt32();
                        if (number < min)
                        {
                            errors.Add(name + " was not within a valid range.");
                            return;
                        }
                    }
                    if (_attributes.ContainsKey("max"))
                    {
                        int max = _attributes["max"].ToInt32();
                        if (number > max)
                        {
                            errors.Add(name + " was not within a valid range.");
                            return;
                        }
                    }
                    if (_attributes.ContainsKey("step"))
                    {
                        int step = _attributes["step"].ToInt32();
                        if (number % step != 0)
                        {
                            errors.Add(name + " was not a valid value.");
                            return;
                        }
                    }
                    break;

                case InputType.Password:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (!RegexPassword.IsMatch(paramString.Value))
                    {
                        errors.Add("Enter a password with at least four(4) characters.");
                        return;
                    }
                    break;

                case InputType.Range:
                    if (paramString == null)
                    {
                        errors.Add("Value required for " + name + ".");
                        return;
                    }
                    int range;
                    if (!int.TryParse(paramString.Value, out range))
                    {
                        errors.Add("Invalid " + name + " value received.");
                        return;
                    }
                    if (_attributes.ContainsKey("min"))
                    {
                        int min = _attributes["min"].ToInt32();
                        if (range < min)
                        {
                            errors.Add(name + " was not within a valid range.");
                            return;
                        }
                    }
                    if (_attributes.ContainsKey("max"))
                    {
                        int max = _attributes["max"].ToInt32();
                        if (range > max)
                        {
                            errors.Add(name + " was not within a valid range.");
                            return;
                        }
                    }
                    if (_attributes.ContainsKey("step"))
                    {
                        int step = _attributes["step"].ToInt32();
                        if (range % step != 0)
                        {
                            errors.Add(name + " was not a valid value.");
                            return;
                        }
                    }
                    break;

                case InputType.Tel:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (!Efz.Validate.PhoneNumber(paramString.Value))
                    {
                        errors.Add(name + " was suspected of being invalid.");
                        return;
                    }
                    break;

                case InputType.Text:
                    if (paramString == null || paramString.Value.Length == 0)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    break;

                case InputType.Time:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (paramString.Value.Length != 5 || !RegexTime.IsMatch(paramString.Value))
                    {
                        errors.Add(name + " was found to be invalid.");
                        return;
                    }
                    break;

                case InputType.Url:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (paramString.Value.Length < 4 || paramString.Value.Length > 200 ||
                        !RegexUrl.IsMatch(paramString.Value))
                    {
                        errors.Add("Url was invalid.");
                        return;
                    }
                    break;

                case InputType.Week:
                    if (paramString == null)
                    {
                        errors.Add("Please select " + name + ".");
                        return;
                    }
                    if (paramString.Value.Length != 8 || !RegexWeek.IsMatch(paramString.Value))
                    {
                        errors.Add(name + " was malformed.");
                        return;
                    }
                    break;
                }
            }

            // is the custom validation set? no, complete
            if (OnValidate == null)
            {
                return;
            }

            // run the custom validation
            OnValidate.Add(parameter, errors);
            OnValidate.Run();
        }