123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.UI;
- using System.Configuration;
- using System.Text.RegularExpressions;
- using System.Security.Cryptography;
- using System.IO;
- using System.Web;
- namespace Bowin.Common.Utility
- {
- public static class Function
- {
- /// <summary>
- /// 过滤sql注入
- /// </summary>
- /// <param name="htmlStr"></param>
- /// <returns></returns>
- public static string FilterSQLStr(string htmlStr)
- {
- htmlStr = htmlStr.Replace(";", "").Replace("&", "&").Replace(" having ", "").Replace(" and ", "").Replace("+and+", "").Replace("and%20", "").Replace("exec ", "").Replace("select ", "").Replace("delete ", "").Replace("update ", "").Replace("count ", "");
- htmlStr = htmlStr.Replace("chr ", "").Replace("mid ", "").Replace("master ", "").Replace("truncate ", "").Replace("char ", "").Replace("declare ", "");
- htmlStr = htmlStr.Replace("or ", "").Replace("*", "*").Replace("%", "%").Replace("$", "$");
- htmlStr = htmlStr.Replace("-", "-").Replace("+", "+").Replace(",", ",").Replace("'", "'").Replace("\\ ", "").Replace("\\' ", "");
- htmlStr = htmlStr.Replace("\\\"", "").Replace("=", "=").Replace("\"", "\").Replace("/", "/").Replace("(", "(").Replace(")", ")");
- htmlStr = htmlStr.Replace("<", "<").Replace(">", ">");
- htmlStr = htmlStr.Replace("@", "@");
- return htmlStr;
- }
- public static bool TryParse<T1, T2>(this T1 source, out T2 result)
- {
- try
- {
- result = (T2)Convert.ChangeType(source, typeof(T2));
- return true;
- }
- catch
- {
- result = default(T2);
- return false;
- }
- }
- ///<summary>
- /// 创建人:zengqz.
- /// 创建时间:2010-6-12 15:22:37
- /// 最后一次修改人:
- /// 最后一次修改时间:
- /// 返回一个 主键,包含生成主键时间的信息,改进GUID的有序性,提高索引效率
- /// </summary>
- /// <returns>32位的字符串</returns>
- private static string NewPK()
- {
- byte[] guidArray = System.Guid.NewGuid().ToByteArray();
- DateTime baseDate = new DateTime(1900, 1, 1);
- DateTime now = DateTime.Now;
- TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
- TimeSpan msecs = new TimeSpan(now.Ticks - (new DateTime(now.Year, now.Month, now.Day).Ticks));
- byte[] daysArray = BitConverter.GetBytes(days.Days);
- byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
- Array.Reverse(daysArray);
- Array.Reverse(msecsArray);
- Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
- Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
- return new System.Guid(guidArray).ToString().Replace("-", "").ToLower();
- }
- public static Guid NewPKGuid()
- {
- byte[] guidArray = System.Guid.NewGuid().ToByteArray();
- DateTime baseDate = new DateTime(1900, 1, 1);
- DateTime now = DateTime.Now;
- TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
- TimeSpan msecs = new TimeSpan(now.Ticks - (new DateTime(now.Year, now.Month, now.Day).Ticks));
- byte[] daysArray = BitConverter.GetBytes(days.Days);
- byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
- Array.Reverse(daysArray);
- Array.Reverse(msecsArray);
- Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
- Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
- return new System.Guid(guidArray);
- }
- /// <summary>
- /// 创建人:zengqz.
- /// 创建时间:2009-7-2 15:22:37
- /// 最后一次修改人:
- /// 最后一次修改时间:
- /// 说明:得到GUID字符串(32个字符)
- /// </summary>
- /// <returns>去除"-"的GUID字符串</returns>
- public static string GetGUID()
- {
- return NewPK();
- }
- public static void ReturnToPage(System.Web.UI.Page page, string msg, string url)
- {
- if (url == null) url = "";
- if ((msg != "") && (msg != null))
- {
- page.ClientScript.RegisterStartupScript(page.GetType(), "ShowMessage", "<script>addLoadEvent(function(){alert('" + msg + "')});</script>");
- }
- StringBuilder builder = new StringBuilder();
- builder.Append("<script>addLoadEvent(function(){");
- switch (url)
- {
- case "":
- builder.Append("jsClose()");
- break;
- case "parent":
- builder.Append("jsClose('parent')");
- break;
- case "parentsubmit":
- builder.Append("jsClose('parentsubmit')");
- break;
- case "this":
- string s = (page.Request.UrlReferrer == null) ? "" : page.Request.UrlReferrer.ToString();
- if (s == "")
- {
- builder.Append("jsClose()");
- }
- else
- {
- Regex rx = new Regex(@"[\?|&]ref=\d*");
- s = rx.Replace(s, "");
- s += (s.IndexOf("?") > 0) ? "&ref=" : "?ref=";
- builder.Append("jsClose('" + s + getRandom(9999) + "')");
- }
- break;
- case "hold":
- break;
- default:
- builder.Append("jsClose('" + url + "')");
- break;
- }
- builder.Append("});</script>");
- page.ClientScript.RegisterStartupScript(page.GetType(), "CloseWindow", builder.ToString());
- }
- #region 返回随机数 getRandom
- public static int getRandom(int iMax)
- {
- if (iMax < 1) iMax = 1;
- Random r = new Random(System.Environment.TickCount);
- return r.Next(iMax);
- }
- #endregion
- /// <summary>
- /// 创建人:zengqz.
- /// 创建时间:2009-7-7 9:22:00
- /// 最后一次修改人:
- /// 最后一次修改时间:
- /// 说明:显示图标
- /// </summary>
- /// <param name="sPostfix">图标类型(bmp,csv,dll,doc,fon,ini,jpg,paf,pps,ppt,rar,tf,txt,slx,xml,zip)</param>
- /// <returns>显示图标的路径</returns>
- public static string ShowIcon(string sPostfix)
- {
- string sResult = "";
- switch (sPostfix)
- {
- case "bmp":
- case "csv":
- case "dll":
- case "doc":
- case "fon":
- case "ini":
- case "jpg":
- case "pdf":
- case "pps":
- case "ppt":
- case "rar":
- case "ttf":
- case "txt":
- case "xls":
- case "xml":
- case "zip":
- sResult = "~/Images/FileTypeIcon/" + sPostfix + ".gif";
- break;
- default:
- sResult = "~/Images/FileTypeIcon/white.gif";
- break;
- }
- return sResult;
- }
- /// <summary>
- /// 设置页面权限
- /// </summary>
- /// <param name="page">当前页面</param>
- /// <param name="sControlSet">设置控件字符串</param>
- public static void SetPageRight(Page page, string sControlSet)
- {
- page.ClientScript.RegisterStartupScript(typeof(Page), "jsGetSetPageBtnDisplay", " jsSetPageBtnDisplay('" + sControlSet + "')", true);
- }
- /// <summary>
- /// 获取上传附件大小
- /// </summary>
- /// <returns></returns>
- public static int GetAttachmentSize()
- {
- string sAttachmentSize = "";
- sAttachmentSize = ConfigurationManager.AppSettings["AttachmentSizeLimit"];
- if (string.IsNullOrEmpty(sAttachmentSize))
- {
- sAttachmentSize = "50";
- }
- return Convert.ToInt32(sAttachmentSize);
- }
- /// <summary>
- /// 获取SOA权限路由
- /// </summary>
- /// <returns></returns>
- public static string GetSystemRoute()
- {
- return ConfigurationManager.AppSettings["SYSTEM_ROUTE"];
- }
- /// <summary>
- /// 获取域名
- /// </summary>
- /// <returns></returns>
- public static string GetDomail_Url()
- {
- return ConfigurationManager.AppSettings["Domail_Url"];
- }
- /// <summary>
- /// 获取虚拟目录名称
- /// </summary>
- /// <returns></returns>
- public static string GetVirtual_Directory()
- {
- return ConfigurationManager.AppSettings["Virtual_Directory_Name"];
- }
- public static string GetDownFile(string fileName, DateTime fileDate)
- {
- string downfileDoc = ConfigurationManager.AppSettings["Doc_downFile"];
- downfileDoc = Path.Combine(downfileDoc, fileDate.ToString("yyyyMM"));
- downfileDoc = Path.Combine(downfileDoc, fileName);
- downfileDoc = downfileDoc.Replace("\\", "/");
- return downfileDoc;
- }
- /// <summary>
- /// 计算文件的MD5校验
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static string GetMD5HashFromFile(string fileName)
- {
- try
- {
- FileStream file = new FileStream(fileName, FileMode.Open);
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] retVal = md5.ComputeHash(file);
- file.Close();
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < retVal.Length; i++)
- {
- sb.Append(retVal[i].ToString("x2"));
- }
- return sb.ToString();
- }
- catch (Exception ex)
- {
- throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
- }
- }
- ///// <summary>
- ///// 上传文件
- ///// </summary>
- ///// <param name="buffer">文件流</param>
- ///// <param name="fileName">文件路径(包括文件名)</param>
- ///// <returns></returns>
- //public static bool UploadFile(byte[] buffer, string filePath)
- //{
- // var flag = false;
- // try
- // {
- // string filePhysicalPath = HttpContext.Current.Server.MapPath(filePath); //存放文件的物理路径
- // var path = Path.GetDirectoryName(filePhysicalPath);
- // if (!Directory.Exists(path))
- // {
- // Directory.CreateDirectory(path);
- // }
- // //定义并实例化一个内存流,以存放提交上来的字节数组。
- // MemoryStream ms = new MemoryStream(buffer);
- // //定义实际文件对象,保存上载的文件。
- // FileStream fs = new FileStream(filePhysicalPath, FileMode.Create);
- // //把内内存里的数据写入物理文件
- // ms.WriteTo(fs);
- // ms.Close();
- // fs.Close();
- // fs = null;
- // ms = null;
- // flag = true;
- // }
- // catch
- // {
- // flag = false;
- // }
- // return flag;
- //}
- /// <summary>
- /// 上传文件(支持断点续传)
- /// </summary>
- /// <param name="buffer">文件流</param>
- /// <param name="filePath">文件路径(包括文件名)</param>
- /// <param name="offset">偏移</param>
- /// <returns>上传是否成功</returns>
- public static bool UploadFile(byte[] buffer, string filePath, long offset = 0)
- {
- var writeBuffer = buffer;
- if (writeBuffer == null) return false;
- if (offset < 0) offset = 0;
- try
- {
- string filePhysicalPath = HttpContext.Current.Server.MapPath(filePath); //存放文件的物理路径
- var path = Path.GetDirectoryName(filePhysicalPath);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- using (FileStream filesStream = new FileStream(filePhysicalPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
- {
- filesStream.Seek(offset, SeekOrigin.Begin);
- filesStream.Write(buffer, 0, buffer.Length);
- filesStream.Flush();
- }
- return true;
- }
- catch (Exception ex)
- {
- throw ex;
- //return false;
- }
- }
- /// <summary>
- /// 获取临时文件名路径
- /// </summary>
- /// <returns></returns>
- public static string GetTempFile()
- {
- return ConfigurationManager.AppSettings["Doc_downFile"];
- }
- /// <summary>
- /// 获取模板文件名路径
- /// </summary>
- /// <returns></returns>
- public static string GetTemplateFile()
- {
- return ConfigurationManager.AppSettings["Doc_TemplateFile"];
- }
- /// <summary>
- /// 是否本地登录
- /// </summary>
- /// <returns></returns>
- public static string GetIsLocalLogin()
- {
- return ConfigurationManager.AppSettings["Is_Local_Login"];
- }
- /// <summary>
- /// 是否需要单点登陆
- /// </summary>
- /// <returns></returns>
- public static string GetIsSSOLogin()
- {
- return ConfigurationManager.AppSettings["Is_SSO_Login"];
- }
- /// <summary>
- /// 获取单点登陆 登录令牌键值
- /// </summary>
- /// <returns></returns>
- public static string GetSS0CookieID()
- {
- return ConfigurationManager.AppSettings["SS0_CookieID"];
- }
- /// <summary>
- /// 获取单点登陆 省平台登录令牌键值
- /// </summary>
- /// <returns></returns>
- public static string GetSS0PortalCookieID()
- {
- return ConfigurationManager.AppSettings["SS0_Portal_CookieID"];
- }
- /// <summary>
- /// 获取单点登陆 登录令牌键值
- /// </summary>
- /// <returns></returns>
- public static string GetCookiesDomain()
- {
- return ConfigurationManager.AppSettings["Cookies_Domain"];
- }
- /// <summary>
- /// 是否使用AIO短信接口
- /// </summary>
- /// <returns></returns>
- public static string GetIsUseAIOSMS()
- {
- return ConfigurationManager.AppSettings["Is_UseAIOSMS"];
- }
- /// <summary>
- /// AIO短信接口 帐号
- /// </summary>
- /// <returns></returns>
- public static string GetAIOSMSAccount()
- {
- return ConfigurationManager.AppSettings["AIO_SMS_Account"];
- }
- /// <summary>
- /// AIO短信接口 密码
- /// </summary>
- /// <returns></returns>
- public static string GetAIOSMSPassword()
- {
- return ConfigurationManager.AppSettings["AIO_SMS_Password"];
- }
- #region 得到用户IP地址 getUserIP
- public static string GetUserIP()
- {
- HttpRequest objRequest = HttpContext.Current.Request;
- return objRequest.UserHostAddress;
- //return (objRequest.ServerVariables["HTTP_VIA"] != null) ? objRequest.ServerVariables["HTTP_X_FORWARDED_FOR"] : objRequest.ServerVariables["REMOTE_ADDR"];
- }
- #endregion
- #region AES加密和解密
- /// <summary>
- /// 执行DES解密
- /// </summary>
- /// <param name="decryptStr">解密字符串</param>
- /// <returns>返回解密后字符串</returns>
- public static string DESDecrypt(string decryptStr)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = new byte[decryptStr.Length / 2];
- for (int x = 0; x < decryptStr.Length / 2; x++)
- {
- int i = (Convert.ToInt32(decryptStr.Substring(x * 2, 2), 16));
- inputByteArray[x] = (byte)i;
- }
- byte[] desKey = new byte[] { 0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08 };
- byte[] desIV = new byte[] { 0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08 };
- des.Key = desKey;
- des.IV = desIV;
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
- try
- {
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- }
- catch (Exception)
- {
- ms.Dispose();
- return "";
- }
- StringBuilder ret = new StringBuilder();
- return System.Text.Encoding.Default.GetString(ms.ToArray());
- }
- /// <summary>
- /// xuner DES加密
- /// </summary>
- /// <param name="pToEncrypt">加密前字符串</param>
- /// <returns>加密后字符串</returns>
- public static string DESEncrypt(string pToEncrypt)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
- byte[] desKey = new byte[] { 0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08 };
- byte[] desIV = new byte[] { 0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08 };
- des.Key = desKey;
- des.IV = desIV;
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- StringBuilder ret = new StringBuilder();
- foreach (byte b in ms.ToArray())
- {
- ret.AppendFormat("{0:X2}", b);
- }
- ret.ToString();
- return ret.ToString();
- }
- #endregion
- /// <summary>
- /// 获取一个汉字的拼音首字母
- /// </summary>
- /// <param name="cnChar"></param>
- /// <returns></returns>
- public static string GetSpell(string cnChar)
- {
- //将汉字转化为ASNI码,二进制序列
- byte[] arrCN = Encoding.Default.GetBytes(cnChar);
- if (arrCN.Length > 1)
- {
- int area = (short)arrCN[0];
- int pos = (short)arrCN[1];
- int code = (area << 8) + pos;
- int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119,
- 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387,
- 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
- for (int i = 0; i < 26; i++)
- {
- int max = 55290;
- if (i != 25) max = areacode[i + 1];
- if (areacode[i] <= code && code < max)
- {
- return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
- }
- }
- return "*";
- }
- else return cnChar;
- }
- /// <summary>
- /// 验证手机号码
- /// </summary>
- /// <param name="mobileNo"></param>
- /// <returns></returns>
- public static bool IsMobileNo(string mobileNo)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(mobileNo, @"^[1]+[3,5]+\d{9}");
- }
- public static List<int> WeekList
- {
- get
- {
- return new List<int>()
- {
- 0,1,2,3,4,5,6,7
- };
- }
- }
- }
- }
|