using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web; namespace AIAHTML5.API.Models { public class IPValidator { public static Double ChangeIpToDouble(String ipVal) { String[] resultArray = new String[20]; //Array resultArray ; String n = ipVal; String[] array = n.Split('.'); for (int i = 0; i < array.Length; i++) { if (array[i].Length == 1) { array[i] = "00" + array[i]; resultArray[i] = (array[i]); } else if (array[i].Length == 2) { array[i] = "0" + array[i]; resultArray[i] = (array[i]); } else { array[i] = array[i]; resultArray[i] = (array[i]); } } //String theContent = resultArray.ToString(); String theContent = ""; for (int i = 0; i <= resultArray.Length - 1; i++) { if (!String.IsNullOrEmpty(resultArray[i])) theContent += resultArray[i]; } theContent = (theContent == "" ? "0" : theContent); return Convert.ToDouble(theContent); //return theContent; } public static bool ValidateIP(String strSiteIp, String strSiteIPTo, String strSiteMasterIPTo, String strIPToValidate, int intIsMaster) { Double dblSiteIP = 0; Double dblSiteIPTo = 0; Double dblSiteMIPTo = 0; Double dblIPToValidate = 0; bool boolReturn = false; try { // Convert all IP to double values dblSiteIP = ChangeIpToDouble(((strSiteIp == "" || IsNumericIP(strSiteIp) == false) ? "0" : strSiteIp)); dblSiteIPTo = ChangeIpToDouble(((strSiteIPTo == "" || IsNumericIP(strSiteIPTo) == false) ? "0" : strSiteIPTo)); dblSiteMIPTo = ChangeIpToDouble(((strSiteMasterIPTo == "" || IsNumericIP(strSiteMasterIPTo) == false) ? "0" : strSiteMasterIPTo)); dblIPToValidate = ChangeIpToDouble(((strIPToValidate == "" || IsNumericIP(strIPToValidate) == false) ? "0" : strIPToValidate)); if (intIsMaster > 0) { if (dblSiteIP == dblIPToValidate) boolReturn = true; if (dblSiteIPTo > 0 && dblSiteMIPTo > 0) { if (isBetween(dblIPToValidate, dblSiteIPTo, dblSiteMIPTo) == true) { boolReturn = true; } } else if (dblSiteIPTo > 0) { if (dblSiteIPTo == dblIPToValidate) boolReturn = true; } else if (dblSiteMIPTo > 0) { if (dblSiteMIPTo == dblIPToValidate) boolReturn = true; } } else { if (dblSiteIP > 0 && dblSiteIPTo > 0) { if (isBetween(dblIPToValidate, dblSiteIP, dblSiteIPTo) == true) { boolReturn = true; } } else if (dblSiteIP > 0) { if (dblSiteIP == dblIPToValidate) boolReturn = true; } else if (dblSiteIPTo > 0) { if (dblSiteIPTo == dblIPToValidate) boolReturn = true; } } return boolReturn; } catch (Exception objExp) { return false; } } public static String FormatURLToIP(String strURL) { strURL = strURL.Replace("www.", ""); strURL = strURL.Replace("http://", ""); strURL = strURL.Replace("https://", ""); strURL = strURL.Replace("/", ""); if (strURL.IndexOf(":") != -1) { char[] delimiters = new char[] { ':' }; string[] parts = strURL.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); strURL = parts[0]; } return strURL; } /// /// /// /// /// /// /// private static bool isBetween(Double iNum, Double iFrom, Double iTo) { if (iNum == 0) return false; if (iNum >= iFrom && iNum <= iTo) { return true; } return false; } /// /// /// /// /// public static bool IsNumericIP(string text) { //Regex objRegex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$"); Regex objRegex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$*\.?[0-9]+$*\.?[0-9]+$*\.?[0-9]+$"); return objRegex.IsMatch(text); } } }