using System;
using System.Text.RegularExpressions;
using System.Net;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Mail;
namespace JobMorning
{
/// <summary>
/// Summary description for JMTools.
/// </summary>
public class JMTools
{
public JMTools()
{
//
// TODO: Add constructor logic here
//
}
public static bool CheckLocalIP(string strLicIP)
{
bool isLocalIPValid = false;
// This function checks if the passed IP is in the list
// of IP owned by the computer.
string strHostName = "";
string strIP = "";
strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
strIP = addr[i].ToString();
if (strIP.Equals(strLicIP))
{
isLocalIPValid = true;
}
// In any case, validate all local addresses
// starting by either 192.168 or 127
if (strIP.Substring(0,7)=="192.168")
{
isLocalIPValid = true;
}
if (strIP.Substring(0,3)=="127")
{
isLocalIPValid = true;
}
}
return isLocalIPValid;
}
public class EmailValidator
{
private bool syntaxvalid = false;
private string account, domain, inputemail;
/// <summary>
/// Determines if an email has valid syntax
/// </summary>
/// <param name="email">the email to test</param>
/// <param name="domainExtensionRequired">indicates whether or not the
/// email must end with a known DomainExtension to be considered valid</param>
/// <returns>boolean indicating if the email has valid syntax</returns>
/// <remarks>
/// Validates an email address specifying whether or not
/// the email is required to have a DomainExtension that is valid.
/// </remarks>
public static bool Valid(string email, bool domainExtensionRequired)
{
EmailValidator v;
bool valid;
//call syntax validator
v = new EmailValidator(email, domainExtensionRequired);
//determine validity
valid = v.IsValid;
//cleanup
v = null;
//return indication of validity
return valid;
}
/// <summary>
/// Initializes a new instance of the EmailSyntaxValidator
/// </summary>
/// <param name="email">the email to test</param>
/// <param name="domainExtensionRequired">indicates whether or not the
/// email must end with a known DomainExtension to be considered valid</param>
/// <remarks>
/// The initializer creates an instance of the EmailSyntaxValidator
/// class to validate a single email. You can specify whether or not
/// the DomainExtension is required and should be validated.
/// </remarks>
public EmailValidator(string email, bool domainExtensionRequired)
{
string tmpmail;
//save email as validated
this.inputemail = email;
//remove <brackets> if found
tmpmail = this.RemoveBrackets(email);
//then trim
tmpmail = this.Trim(tmpmail);
//separate account from domain, quit if unable to separate
if (! this.CrackEmail(tmpmail))
{
return;
}
//validate the domain, quit if domain is bad
if (! this.DomainValid(domainExtensionRequired))
{
return;
}
//if the DomainExtension is required, validate the domain extension,
//quit if the domain extension is bad
if (domainExtensionRequired && ! this.DomainExtensionValid())
{
return;
}
//email syntax is valid
this.syntaxvalid = true;
}
/// <summary>
/// separates email account from domain
/// </summary>
/// <param name="email">the email to parse</param>
/// <returns>boolean indicating success of separation</returns>
private bool CrackEmail(string email)
{
Regex re;
Match m;
bool ok = false;
//pattern to separate email into account and domain.
//note that we parse from right to left, thereby forcing
//this pattern to match the last @ symbol in the email.
re = new Regex(
"^(.+?)\\@(.+?)$",
RegexOptions.Singleline | RegexOptions.RightToLeft
);
//determine if email matches pattern (email contains one
//or more @'s)
if (re.IsMatch(email))
{
//if matched, separate account and domain as noted
//in our pattern with the () sections.
m = re.Match(email);
//first group is the account
this.account = m.Groups[1].Value;
//second group is the domain
this.domain = m.Groups[2].Value;
//cleanup
m = null;
//indicate success
ok = true;
} // if (re.IsMatch(email))
//cleanup
re = null;
//return the indication of parse success or failure
return ok;
}
/// <summary>
/// removes outer brackets from an email address
/// </summary>
/// <param name="input">the email to parse</param>
/// <returns>the email without brackets</returns>
private string RemoveBrackets(string input)
{
string output = null;
Regex re;
//pattern to match brackets or no brackets
re = new Regex(
"^\\<*|\\>*$",
RegexOptions.IgnoreCase | RegexOptions.Singleline
);
//if email matches (it will always match this pattern)
if (re.IsMatch(input))
{
//replace them with nothing
output = re.Replace(input, "");
}
//cleanup
re = null;
//return the email without brackets
return output;
}
/// <summary>
/// trims any leading and trailing white space from the email
/// </summary>
/// <param name="input">the email to parse</param>
/// <returns>the email with no leading or trailing white space</returns>
private string Trim(string input)
{
string output = null;
Regex re;
//pattern to trim leading and trailing white space from string
re = new Regex(
"^\\s*|\\s*$",
RegexOptions.IgnoreCase | RegexOptions.Singleline
);
//if matches
if (re.IsMatch(input))
{
//remove whitespace
output = re.Replace(input, "");
}
//cleanup
re = null;
//return the email with no leading or trailing white space
return output;
}
private bool DomainValid(bool domainExtensionRequired)
{
bool valid;
Regex re;
string pattern, emaildomain;
if (domainExtensionRequired)
{
//if the DomainExtension is required, the pattern contains
//a basic DomainExtension length check at the end
pattern = "^((([a-z0-9-]+)\\.)+)[a-z]{2,6}$";
emaildomain=this.domain;
}
else
{
//when the DomainExtension is not required, the pattern is
//the same except for the DomainExtension length check.
//note the pattern ends with a . in the loop.
//This means that we must append a . to the domain
//temporarily to test the email.
pattern = "^((([a-z0-9-]+)\\.)+)$";
emaildomain=this.domain+".";
}
re = new Regex(
pattern,
RegexOptions.IgnoreCase | RegexOptions.Singleline
);
//if the email matches, it's valid
valid = re.IsMatch(emaildomain);
//cleanup
re = null;
//return indication of validity
return valid;
}
private bool DomainExtensionValid()
{
bool valid;
Regex re;
string domainvalidatorpattern = "";
//pattern to validate all known DomainExtension's
domainvalidatorpattern+="\\.(";
domainvalidatorpattern+="a[c-gil-oq-uwz]|"; //ac,ad,ae,af,ag,ai,al,am,an,ao,aq,ar,as,at,au,aw,az
domainvalidatorpattern+="b[a-bd-jm-or-tvwyz]|"; //ba,bb,bd,be,bf,bg,bh,bi,bj,bm,bn,bo,br,bs,bt,bv,bw,by,bz
domainvalidatorpattern+="c[acdf-ik-orsuvx-z]|"; //ca,cc,cd,cf,cg,ch,ci,ck,cl,cm,cn,co,cr,cs,cu,cv,cz,cy,cz
domainvalidatorpattern+="d[ejkmoz]|"; //de,dj,dk,dm,do,dz
domainvalidatorpattern+="e[ceghr-u]|"; //ec,ee,eg,eh,er,es,et,eu
domainvalidatorpattern+="f[i-kmorx]|"; //fi,fj,fk,fm,fo,fr,fx
domainvalidatorpattern+="g[abd-ilmnp-uwy]|"; //ga,gb,gd,ge,gf,gg,gh,gi,gl,gm,gn,gp,gq,gr,gs,gt,gu,gw,gy
domainvalidatorpattern+="h[kmnrtu]|"; //hk,hm,hn,hr,ht,hu
domainvalidatorpattern+="i[delm-oq-t]|"; //id,ie,il,im,in,io,iq,ir,is,it
domainvalidatorpattern+="j[emop]|"; //je,jm,jo,jp
domainvalidatorpattern+="k[eg-imnprwyz]|"; //ke,kg,kh,ki,km,kn,kp,kr,kw,ky,kz
domainvalidatorpattern+="l[a-cikr-vy]|"; //la,lb,lc,li,lk,lr,ls,lt,lu,lv,ly
domainvalidatorpattern+="m[acdghk-z]|"; //ma,mc,md,mg,mh,mk,ml,mm,mn,mo,mp,mq,mr,ms,mt,mu,mv,mw,mx,my,mz
domainvalidatorpattern+="n[ace-giloprtuz]|"; //na,nc,ne,nf,ng,ni,nl,no,np,nr,nt,nu,nz
domainvalidatorpattern+="om|"; //om
domainvalidatorpattern+="p[ae-hk-nrtwy]|"; //pa,pe,pf,pg,ph,pk,pl,pm,pn,pr,pt,pw,py
domainvalidatorpattern+="qa|"; //qa
domainvalidatorpattern+="r[eouw]|"; //re,ro,ru,rw
domainvalidatorpattern+="s[a-eg-ort-vyz]|"; //sa,sb,sc,sd,se,sg,sh,si,sj,sk,sl,sm,sn,so,sr,st,su,sv,sy,sz
domainvalidatorpattern+="t[cdf-hjkm-prtvwz]|"; //tc,td,tf,tg,th,tj,tk,tm,tn,to,tp,tr,tt,tv,tx,tz
domainvalidatorpattern+="u[agkmsyz]|"; //ua,ug,uk,um,us,uy,uz
domainvalidatorpattern+="v[aceginu]|"; //va,vc,ve,vg,vy,vn,vu
domainvalidatorpattern+="w[fs]|"; //wf,ws
domainvalidatorpattern+="y[etu]|"; //ye,yt,yu
domainvalidatorpattern+="z[admrw]|"; //za,zd,zm,zr,zw
domainvalidatorpattern+="com|"; //com
domainvalidatorpattern+="edu|"; //edu
domainvalidatorpattern+="net|"; //net
domainvalidatorpattern+="org|"; //org
domainvalidatorpattern+="mil|"; //mil
domainvalidatorpattern+="gov|"; //gov
domainvalidatorpattern+="biz|"; //biz
domainvalidatorpattern+="pro|"; //pro
domainvalidatorpattern+="aero|"; //aero
domainvalidatorpattern+="coop|"; //coop
domainvalidatorpattern+="info|"; //info
domainvalidatorpattern+="name|"; //name
domainvalidatorpattern+="int|"; //int
domainvalidatorpattern+="museum"; //museum
domainvalidatorpattern+=")$";
re = new Regex(
domainvalidatorpattern,
RegexOptions.IgnoreCase | RegexOptions.Singleline
);
//if domain matches pattern, it has a valid DomainExtension
valid = re.IsMatch(this.domain);
//cleanup
re = null;
//return an indication of DomainExtension validity
return valid;
}
/// <summary>
/// Gets a value indicating whether or not the email address
/// has valid syntax
/// </summary>
/// <remarks>
/// This property returns a boolean indicating whether or not
/// the email address has valid syntax as determined by the
/// class.
/// </remarks>
/// <value>boolean indicating the validity of the email</value>
public bool IsValid
{
get
{
return this.syntaxvalid;
}
}
/// <summary>
/// Get the domain part of the email address.
/// </summary>
/// <remarks>
/// This property returns the domain part of the email
/// address if and only if the email is considered valid
/// by the class. Otherwise null is returned.
/// </remarks>
/// <value>string representing the domain of the email</value>
public string Domain
{
get
{
return this.domain;
}
}
/// <summary>
/// Get the account part of the email address.
/// </summary>
/// <remarks>
/// This property returns the account part of the email
/// address if and only if the email is considered valid
/// by the class. Otherwise null is returned.
/// </remarks>
/// <value>string representing the account of the email</value>
public string Account
{
get
{
return this.account;
}
}
/// <summary>
/// Gets the email address as entered.
/// </summary>
/// <remarks>
/// This property is filled regardless of the validity of the email.
/// It contains the email as it was entered into the class.
/// </remarks>
/// <value>string representing the email address as entered</value>
public string Address
{
get
{
return this.inputemail;
}
}
}
public class LicenceElements
{
private string strIP;
private string NBPages;
private string NBSessions;
private string NBStoredProc;
private string NBSavePoints;
public string[] allDomains;
private string strThisLicence;
public string GetStrIP()
{
return strIP;
}
public string GetNBPages()
{
return NBPages;
}
public string GetNBSessions()
{
return NBSessions;
}
public string GetNBStoredProc()
{
return NBStoredProc;
}
public string GetNBSavePoints()
{
return NBSavePoints;
}
public LicenceElements(string strLicence)
{
strThisLicence = strLicence;
}
public string ExtractLicenceInfo()
{
string strReturn="";
string strDecrypted = CryptoDecrypt.Decrypt(strThisLicence);
try
{
string[] allLicenceItems = strDecrypted.Split(';');
strIP = allLicenceItems[0];
if (!JMTools.CheckLocalIP(strIP))
{
strReturn="<result>Licence is invalid</result>";
// Set the licence to the minimal licence, because the IP is not valid.
NBPages = "10";
NBStoredProc = "2";
NBSavePoints = "2";
NBSessions = "2";
} // if (!JMTools.CheckLocalIp(strIP))
else
{
NBPages = allLicenceItems[1];
NBStoredProc = allLicenceItems[2];
NBSavePoints = allLicenceItems[3];
NBSessions = allLicenceItems[4];
}
allDomains = new string[(allLicenceItems.Length-5)];
for (int k=5; k<allLicenceItems.Length;k++)
{
allDomains[(k-3)]= allLicenceItems[k];
}
strReturn="<result>Licence is valid</result>";
}
catch (System.IndexOutOfRangeException e)
{
strReturn="<result>Licence does not contain all the necessary elements</result>";
}
return strReturn;
}
}
}
}