using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
namespace PalGain.Core
{
///
/// Represents a common helper
///
public partial class CommonHelper
{
///
/// Ensures the subscriber email or throw.
///
/// The email.
///
public static string EnsureSubscriberEmailOrThrow(string email) {
string output = EnsureNotNull(email);
output = output.Trim();
output = EnsureMaximumLength(output, 255);
if(!IsValidEmail(output)) {
throw new NopException("Email is not valid.");
}
return output;
}
///
/// Verifies that a string is in valid e-mail format
///
/// Email to verify
/// true if the string is a valid e-mail address and false if it's not
public static bool IsValidEmail(string email)
{
if (String.IsNullOrEmpty(email))
return false;
email = email.Trim();
var result = Regex.IsMatch(email, "^(?:[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\`\\{\\|\\}\\~]+\\.)*[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\`\\{\\|\\}\\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-](?!\\.)){0,61}[a-zA-Z0-9]?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\\[(?:(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\.){3}(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\]))$", RegexOptions.IgnoreCase);
return result;
}
///
/// Generate random digit code
///
/// Length
/// Result string
public static string GenerateRandomDigitCode(int length)
{
var random = new Random();
string str = string.Empty;
for (int i = 0; i < length; i++)
str = String.Concat(str, random.Next(10).ToString());
return str;
}
///
/// Returns an random interger number within a specified rage
///
/// Minimum number
/// Maximum number
/// Result
public static int GenerateRandomInteger(int min = 0, int max = int.MaxValue)
{
var randomNumberBuffer = new byte[10];
new RNGCryptoServiceProvider().GetBytes(randomNumberBuffer);
return new Random(BitConverter.ToInt32(randomNumberBuffer, 0)).Next(min, max);
}
///
/// Ensure that a string doesn't exceed maximum allowed length
///
/// Input string
/// Maximum length
/// A string to add to the end if the original string was shorten
/// Input string if its lengh is OK; otherwise, truncated input string
public static string EnsureMaximumLength(string str, int maxLength, string postfix = null)
{
if (String.IsNullOrEmpty(str))
return str;
if (str.Length > maxLength)
{
var result = str.Substring(0, maxLength);
if (!String.IsNullOrEmpty(postfix))
{
result += postfix;
}
return result;
}
return str;
}
///
/// Ensures that a string only contains numeric values
///
/// Input string
/// Input string with only numeric values, empty string if input is null/empty
public static string EnsureNumericOnly(string str)
{
if (String.IsNullOrEmpty(str))
return string.Empty;
var result = new StringBuilder();
foreach (char c in str)
{
if (Char.IsDigit(c))
result.Append(c);
}
return result.ToString();
}
///
/// Ensure that a string is not null
///
/// Input string
/// Result
public static string EnsureNotNull(string str)
{
if (str == null)
return string.Empty;
return str;
}
///
/// Indicates whether the specified strings are null or empty strings
///
/// Array of strings to validate
/// Boolean
public static bool AreNullOrEmpty(params string[] stringsToValidate) {
bool result = false;
Array.ForEach(stringsToValidate, str => {
if (string.IsNullOrEmpty(str)) result = true;
});
return result;
}
///
/// Compare two arrasy
///
/// Type
/// Array 1
/// Array 2
/// Result
public static bool ArraysEqual(T[] a1, T[] a2)
{
//also see Enumerable.SequenceEqual(a1, a2);
if (ReferenceEquals(a1, a2))
return true;
if (a1 == null || a2 == null)
return false;
if (a1.Length != a2.Length)
return false;
var comparer = EqualityComparer.Default;
for (int i = 0; i < a1.Length; i++)
{
if (!comparer.Equals(a1[i], a2[i])) return false;
}
return true;
}
private static AspNetHostingPermissionLevel? _trustLevel;
///
/// Finds the trust level of the running application (http://blogs.msdn.com/dmitryr/archive/2007/01/23/finding-out-the-current-trust-level-in-asp-net.aspx)
///
/// The current trust level.
public static AspNetHostingPermissionLevel GetTrustLevel()
{
if (!_trustLevel.HasValue)
{
//set minimum
_trustLevel = AspNetHostingPermissionLevel.None;
//determine maximum
foreach (AspNetHostingPermissionLevel trustLevel in new[] {
AspNetHostingPermissionLevel.Unrestricted,
AspNetHostingPermissionLevel.High,
AspNetHostingPermissionLevel.Medium,
AspNetHostingPermissionLevel.Low,
AspNetHostingPermissionLevel.Minimal
})
{
try
{
new AspNetHostingPermission(trustLevel).Demand();
_trustLevel = trustLevel;
break; //we've set the highest permission we can
}
catch (System.Security.SecurityException)
{
continue;
}
}
}
return _trustLevel.Value;
}
///
/// Sets a property on an object to a valuae.
///
/// The object whose property to set.
/// The name of the property to set.
/// The value to set the property to.
public static void SetProperty(object instance, string propertyName, object value)
{
if (instance == null) throw new ArgumentNullException("instance");
if (propertyName == null) throw new ArgumentNullException("propertyName");
Type instanceType = instance.GetType();
PropertyInfo pi = instanceType.GetProperty(propertyName);
if (pi == null)
throw new NopException("No property '{0}' found on the instance of type '{1}'.", propertyName, instanceType);
if (!pi.CanWrite)
throw new NopException("The property '{0}' on the instance of type '{1}' does not have a setter.", propertyName, instanceType);
if (value != null && !value.GetType().IsAssignableFrom(pi.PropertyType))
value = To(value, pi.PropertyType);
pi.SetValue(instance, value, new object[0]);
}
public static TypeConverter GetNopCustomTypeConverter(Type type)
{
//we can't use the following code in order to register our custom type descriptors
//TypeDescriptor.AddAttributes(typeof(List), new TypeConverterAttribute(typeof(GenericListTypeConverter)));
//so we do it manually here
if (type == typeof(List))
return new GenericListTypeConverter();
if (type == typeof(List))
return new GenericListTypeConverter();
if (type == typeof(List))
return new GenericListTypeConverter();
return TypeDescriptor.GetConverter(type);
}
///
/// Converts a value to a destination type.
///
/// The value to convert.
/// The type to convert the value to.
/// The converted value.
public static object To(object value, Type destinationType)
{
return To(value, destinationType, CultureInfo.InvariantCulture);
}
///
/// Converts a value to a destination type.
///
/// The value to convert.
/// The type to convert the value to.
/// Culture
/// The converted value.
public static object To(object value, Type destinationType, CultureInfo culture)
{
if (value != null)
{
var sourceType = value.GetType();
TypeConverter destinationConverter = GetNopCustomTypeConverter(destinationType);
TypeConverter sourceConverter = GetNopCustomTypeConverter(sourceType);
if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))
return destinationConverter.ConvertFrom(null, culture, value);
if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))
return sourceConverter.ConvertTo(null, culture, value, destinationType);
if (destinationType.IsEnum && value is int)
return Enum.ToObject(destinationType, (int)value);
if (!destinationType.IsInstanceOfType(value))
return Convert.ChangeType(value, destinationType, culture);
}
return value;
}
///
/// Converts a value to a destination type.
///
/// The value to convert.
/// The type to convert the value to.
/// The converted value.
public static T To(object value)
{
//return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
return (T)To(value, typeof(T));
}
///
/// Convert enum for front-end
///
/// Input string
/// Converted string
public static string ConvertEnum(string str)
{
string result = string.Empty;
char[] letters = str.ToCharArray();
foreach (char c in letters)
if (c.ToString() != c.ToString().ToLower())
result += " " + c.ToString();
else
result += c.ToString();
return result;
}
///
/// Set Telerik (Kendo UI) culture
///
public static void SetTelerikCulture()
{
//little hack here
//always set culture to 'en-US' (Kendo UI has a bug related to editing decimal values in other cultures). Like currently it's done for admin area in Global.asax.cs
var culture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
}