/*
-------------------------------------------------------------------
版權(quán)所有: 使用與轉(zhuǎn)載需注明出處 by franco
Generator: vs2008
Description: source code of "isports.Common"
Our Website:
-------------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NBear.Data;
using System.Configuration;
namespace isports.Common
{
/// <summary>
/// 對常用方法重新進行封裝,及獲取一些常用環(huán)境變量
/// </summary>
public class Fetch
{
#region Fields
private static readonly string mTablePrefix;
public static readonly string SiteMapPath;
#endregion
public static T Get<T>(string name)
{
T value = default(T);
object str = HttpContext.Current.Request.QueryString[name];
if (str != null)
{
if (value is ValueType)
{
System.Reflection.MethodInfo parse = typeof(T).GetMethod("Parse", new Type[] { typeof(string) });
if (parse != null)
{
try
{
value = (T)parse.Invoke(null, new object[] { str.ToString() });
}
catch { }
}
}
else if (typeof(T) == typeof(string))
{
value = (T)str;
}
}
return value;
}
public static T Get<T>(string name, T defaultValue)
{
T value = defaultValue;
object str = HttpContext.Current.Request.QueryString[name];
if (str != null)
{
if (value is ValueType)
{
System.Reflection.MethodInfo parse = typeof(T).GetMethod("Parse", new Type[] { typeof(string) });
if (parse != null)
{
try
{
value = (T)parse.Invoke(null, new object[] { str.ToString() });
}
catch { }
}
}
else if (typeof(T) == typeof(string))
{
value = (T)str;
}
}
return value;
}
#region 構(gòu)造函數(shù),初始化變量前綴等。
static Fetch()
{
//mTablePrefix = ApplicationSettings.Get("TablePrefix");
//if (null == mTablePrefix || string.Empty == mTablePrefix)
//{
// mTablePrefix = "meili_";
//}
//else
//{
// mTablePrefix = mTablePrefix.Replace("'", "''");
//}
//SiteMapPath = Fetch.MapPath(".");
}
#endregion
#region 獲取網(wǎng)站的GateWay對象
public static Gateway GetWay()
{
Gateway gw = null;
try
{
gw = new Gateway(DatabaseType.SqlServer, Common.Text.DecryptDES(ConfigurationManager.ConnectionStrings["WebSiteConnectionString"].ConnectionString, "mleasy09"));
}
catch
{
//gw = new Gateway(DatabaseType.SqlServer9, "server=" + Text.DecryptDES(AppCommon.SERVERIP, "mleasy09") + ";uid=" + Text.DecryptDES(AppCommon.USERID, "mleasy09") + ";pwd=" + Text.DecryptDES(AppCommon.PASSWORD, "mleasy09") + ";database=" + Text.DecryptDES(AppCommon.DATABASE, "mleasy09"));
}
return gw;
}
#endregion
#region 獲取頁面提交的參數(shù)值,相當(dāng)于 Request.Form public static string Post(string name)
/// <summary>
/// 獲取頁面提交的參數(shù)值,相當(dāng)于 Request.Form
/// </summary>
public static string Post(string name)
{
string value = HttpContext.Current.Request.Form[name];
return value == null ? string.Empty : value.Trim();
}
#endregion
#region 獲取頁面地址的參數(shù)值,相當(dāng)于 Request.QueryString public static string Get(string name)
/// <summary>
/// 獲取頁面地址的參數(shù)值,相當(dāng)于 Request.QueryString
/// </summary>
public static string Get(string name)
{
string value = HttpContext.Current.Request.QueryString[name];
return value == null ? string.Empty : value.Trim();
}
#endregion
#region 獲取頁面地址的參數(shù)值并檢查安全性,相當(dāng)于 Request.QueryString public static string Get(string name, CheckGetEnum chkType)
/// <summary>
/// 獲取頁面地址的參數(shù)值并檢查安全性,相當(dāng)于 Request.QueryString
/// chkType 有 CheckGetEnum.Int, CheckGetEnum.Safety兩種類型,
/// CheckGetEnum.Int 保證參數(shù)是數(shù)字型
/// CheckGetEnum.Safety 保證提交的參數(shù)值沒有操作數(shù)據(jù)庫的語句
/// </summary>
public static string Get(string name, CheckGetEnum chkType)
{
string value = Get(name);
bool isPass = false;
switch (chkType)
{
default:
isPass = true;
break;
case CheckGetEnum.Int:
{
try
{
int.Parse(value);
isPass = RegExp.IsNumeric(value);
}
catch
{
isPass = false;
}
break;
}
case CheckGetEnum.Safety:
isPass = RegExp.IsSafety(value);
break;
}
if (!isPass)
{
new Terminator().Throw("地址欄中參數(shù)“" + name + "”的值不符合要求或具有潛在威脅,請不要手動修改URL。");
return string.Empty;
}
return value;
}
#endregion
#region 跟蹤調(diào)試輸出一個對象 public static void Trace(object obj)
/// <summary>
/// 跟蹤調(diào)試輸出一個對象
/// </summary>
public static void Trace(object obj)
{
HttpContext.Current.Response.Write(obj.ToString());
}
#endregion
#region 將相對地址轉(zhuǎn)化為絕對地址,進一步封裝和優(yōu)化 Server.MapPath public static string MapPath(string path)
/// <summary>
/// 將相對地址轉(zhuǎn)化為絕對地址,進一步封裝和優(yōu)化 Server.MapPath
/// </summary>
public static string MapPath(string path)
{
if (RegExp.IsPhysicalPath(path))
{
return path;
}
if (null != HttpContext.Current)
{
if (!RegExp.IsRelativePath(path))
{
return HttpContext.Current.Server.MapPath(path);
}
#if DEBUG
if (null == HttpContext.Current)
{
throw new Exception("HttpContext.Current 為空引用");
}
#endif
return HttpContext.Current.Server.MapPath(PathUpSeek + "/" + path);
}
else if (SiteMapPath.Length > 0)
{
return Text.JoinString(SiteMapPath + "/" + path);
}
else
{
throw new Exception("HttpContext.Current 為空引用");
}
}
#endregion
#region IP 地址字符串形式轉(zhuǎn)換成長整型 public static long Ip2Int(string ip)
/// <summary>
/// IP 地址字符串形式轉(zhuǎn)換成長整型
/// </summary>
public static long Ip2Int(string ip)
{
if (!RegExp.IsIp(ip))
{
return -1;
}
string[] arr = ip.Split('.');
long lng = long.Parse(arr[0]) * 16777216;
lng += int.Parse(arr[1]) * 65536;
lng += int.Parse(arr[2]) * 256;
lng += int.Parse(arr[3]);
return lng;
}
#endregion
#region 獲取客戶端IP public static string UserIp
/// <summary>
/// 獲取客戶端IP
/// </summary>
public static string UserIp
{
get
{
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ip == null || ip == string.Empty)
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (!RegExp.IsIp(ip))
{
return "Unknown";
}
return ip;
}
}
#endregion
#region 獲取訪問者所使用的瀏覽器名 public static string UserBrowser
/// <summary>
/// 獲取訪問者所使用的瀏覽器名
/// </summary>
public static string UserBrowser
{
get
{
string agent = HttpContext.Current.Request.UserAgent;
if (agent == null || agent == string.Empty)
return "Unknown";
agent = agent.ToLower();
HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
if (agent.IndexOf("firefox") >= 0
|| agent.IndexOf("firebird") >= 0
|| agent.IndexOf("myie") >= 0
|| agent.IndexOf("opera") >= 0
|| agent.IndexOf("netscape") >= 0
|| agent.IndexOf("msie") >= 0
)
return bc.Browser + bc.Version;
return "Unknown";
}
}
#endregion
#region 獲得網(wǎng)站域名 public static string ServerDomain
/// <summary>
/// 獲得網(wǎng)站域名
/// </summary>
public static string ServerDomain
{
get
{
string host = HttpContext.Current.Request.Url.Host.ToLower();
string[] arr = host.Split('.');
if (arr.Length < 3 || RegExp.IsIp(host))
{
return host;
}
string domain = host.Remove(0, host.IndexOf(".") + 1);
if (domain.StartsWith("com.") || domain.StartsWith("net.") || domain.StartsWith("org.") || domain.StartsWith("gov."))
{
return host;
}
return domain;
}
}
#endregion
#region 獲得當(dāng)前路徑
/// <summary>
/// 獲得當(dāng)前路徑
/// </summary>
public static string CurrentPath
{
get
{
string path = HttpContext.Current.Request.Path;
path = path.Substring(0, path.LastIndexOf("/"));
if (path == "/")
{
return string.Empty;
}
return path;
}
}
#endregion
#region 獲得網(wǎng)站虛擬根目錄 public static string PathUpSeek
/// <summary>
/// 獲得網(wǎng)站虛擬根目錄
/// </summary>
public static string PathUpSeek
{
get
{
string currentPath = CurrentPath;
string lower_current_path = currentPath.ToLower();
#if DEBUG
string[] arr = (Text.JoinString(ApplicationSettings.Get("PathUpSeek"), "|/Members|/Auction|/Ask|/News")).ToLower().Split('|');
#else
string[] arr = (Text.JoinString(ApplicationSettings.Get("PathUpSeek"), "|/install|/upgrade")).ToLower().Split('|');
#endif
foreach (string s in arr)
{
if (null == s || 0 == s.Length)
{
continue;
}
if (s[0] != '/')
{
continue;
}
if (lower_current_path.EndsWith(s))
{
return currentPath.Remove(currentPath.Length - s.Length, s.Length);
}
}
int indexof = currentPath.IndexOf("/templates/");
if (-1 != indexof)
{
return currentPath.Substring(0, indexof);
}
return currentPath;
}
}
#endregion
#region 獲得當(dāng)前URL public static string CurrentUrl
/// <summary>
/// 獲得當(dāng)前URL
/// </summary>
public static string CurrentUrl
{
get
{
return HttpContext.Current.Request.Url.ToString();
}
}
#endregion
#region 獲取當(dāng)前請求的原始URL
/// <summary>
/// 獲取當(dāng)前請求的原始 URL
/// </summary>
public static string webCurrentUrl
{
get
{
return HttpContext.Current.Request.RawUrl.ToString();
}
}
#endregion
#region 獲得來源URL public static string Referrer
/// <summary>
/// 獲得來源URL
/// </summary>
public static string Referrer
{
get
{
Uri uri = HttpContext.Current.Request.UrlReferrer;
if (uri == null)
{
return string.Empty;
}
return Convert.ToString(uri);
}
}
#endregion
#region 是否從其他連接向本域名連接 public static bool IsPostFromAnotherDomain
/// <summary>
/// 是否從其他連接向本域名連接
/// </summary>
public static bool IsPostFromAnotherDomain
{
get
{
if (HttpContext.Current.Request.HttpMethod == "GET")
{
return false;
}
return Referrer.IndexOf(ServerDomain) == -1;
}
}
#endregion
#region 是否從其他連接向本域名以POST方式提交表單 public static bool IsGetFromAnotherDomain
/// <summary>
/// 是否從其他連接向本域名以POST方式提交表單
/// </summary>
public static bool IsGetFromAnotherDomain
{
get
{
if (HttpContext.Current.Request.HttpMethod == "POST")
{
return false;
}
return Referrer.IndexOf(ServerDomain) == -1;
}
}
#endregion
#region 是否被判斷為機器人 public static bool IsRobots
/// <summary>
/// 是否被判斷為機器人
/// </summary>
public static bool IsRobots
{
get
{
return IsWebSearch();
}
}
#endregion
#region 搜索引擎來源判斷 public static bool IsWebSearch()
/// <summary>
/// 搜索引擎來源判斷
/// </summary>
private static string[] _WebSearchList = new string[] { "google", "isaac", "surveybot", "baiduspider", "yahoo", "yisou", "3721", "qihoo", "daqi", "ia_archiver", "p.arthur", "fast-webcrawler", "java", "microsoft-atl-native", "turnitinbot", "webgather", "sleipnir", "msn" };
public static bool IsWebSearch()
{
string user_agent = HttpContext.Current.Request.UserAgent;
if (null == user_agent || string.Empty == user_agent)
{
return true;
}
else
{
user_agent = user_agent.ToLower();
}
for (int i = 0; i < _WebSearchList.Length; i++)
{
if (-1 != user_agent.IndexOf(_WebSearchList[i]))
{
return true;
}
}
return UserBrowser.Equals("Unknown");
}
#endregion
#region Url編碼,相當(dāng)于 Server.UrlEncode public static string UrlEncode(string url)
/// <summary>
/// Url編碼,相當(dāng)于 Server.UrlEncode
/// </summary>
public static string UrlEncode(string url)
{
return HttpContext.Current.Server.UrlEncode(url);
}
#endregion
#region 返回論壇的物理路徑,默認與網(wǎng)站同目錄 public static string ForumDirectory()
public static string ForumDirectory()
{
string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
string[] p = path.Split("\\".ToCharArray());
int arrlength = p.Length;
if (arrlength > 0)
{
path = "";
for (int i = 0; i < arrlength - 2; i++)
{
path += p[i] + "/";
}
}
return path + "bbs/";
}
#endregion
#region 屬性
///// <summary>
///// 變量前綴
///// </summary>
//public static string TablePrefix
//{
// get
// {
// return mTablePrefix;
// }
//}
///// <summary>
///// 獲取驗證碼文件的地址
///// </summary>
//public static string ValidateCodeFileName
//{
// get
// {
// string file_name = Config.Settings["ValidateCodeFileName"];
// if (null == file_name || 0 == file_name.Length)
// {
// file_name = "image.aspx";
// }
// return file_name;
// }
//}
#endregion
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。