📄 globals.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
// 修改说明:新增获取IP方法
// 修改人:宝玉
// 修改日期:2005-02-26
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Web;
using System.Globalization;
using CommunityServer.Configuration;
namespace CommunityServer.Components {
public class Globals {
// the HTML newline character
public const String HtmlNewLine = "<br />";
public const String _appSettingsPrefix = "AspNetForumsSettings.";
public static void ValidateSecureConnection(HttpContext context)
{
//Validate we have a secure connection as early as possible
if(CSConfiguration.GetConfig().RequireSLL && !context.Request.IsSecureConnection)
{
Uri url = context.Request.Url;
context.Response.Redirect("https://" + url.ToString().Substring(7));
context.Response.End();
}
}
/// <summary>
/// Will return false if no Url querystring value can be found
/// </summary>
/// <returns></returns>
public static bool RedirectSiteUrl()
{
CSContext cntx = CSContext.Current;
if(cntx.Url != null)
{
RedirectSiteUrl( cntx.Url, cntx.Args );
}
return false;
}
public static void RedirectSiteUrl(string path) { RedirectSiteUrl( path, null ); }
public static void RedirectSiteUrl(string path, string args)
{
CSContext cntx = CSContext.Current;
if(cntx.IsWebRequest)
{
try
{
if(args == null)
cntx.Context.Response.Redirect(Globals.GetSiteUrls().RawPath(path));
else
cntx.Context.Response.Redirect(string.Format(Globals.GetSiteUrls().RawPath(path), args.Split(',')), true);
}
catch { }
}
throw new CSException(CSExceptionType.RedirectFailure);
}
#region FormatSignature
public static string FormatSignature (string userSignature)
{
if ( userSignature != null && userSignature.Length > 0 )
return "<hr size=\"1\" align=\"left\" width=\"25%\">" + userSignature.Replace("\r\n", "<br>");
else
return String.Empty;
}
#endregion
#region Trace
public static void Trace(string category, string message)
{
if (!CSContext.Current.SiteSettings.EnableDebugMode)
return;
HttpContext context = HttpContext.Current;
Stack debugStack;
// Do we have the debug stack?
//
if (context.Items["DebugTrace"] == null)
debugStack = new Stack();
else
debugStack = (Stack) context.Items["DebugTrace"];
// Add a new item
//
debugStack.Push(new DebugTrace(category, message));
// Add item to general ASP.NET trace
//
context.Trace.Write(category, message);
}
public class DebugTrace {
DateTime time;
string category;
string message;
public DebugTrace(string category, string message) {
this.category = category;
this.message = message;
time = DateTime.Now;
}
public DateTime Time { get { return time; } }
public string Category { get { return category; } }
public string Message { get { return message; } }
}
#endregion
#region Encode/Decode
/// <summary>
/// Converts a prepared subject line back into a raw text subject line.
/// </summary>
/// <param name="textToFormat">The prepared subject line.</param>
/// <returns>A raw text subject line.</returns>
/// <remarks>This function is only needed when editing an existing message or when replying to
/// a message - it turns the HTML escaped characters back into their pre-escaped status.</remarks>
public static string HtmlDecode(String textToFormat)
{
if(IsNullorEmpty(textToFormat))
return textToFormat;
//ScottW: Removed Context dependency
return System.Web.HttpUtility.HtmlDecode(textToFormat);
// strip the HTML - i.e., turn < into <, > into >
//return HttpContext.Current.Server.HtmlDecode(FormattedMessageSubject);
}
/// <summary>
/// Converts a prepared subject line back into a raw text subject line.
/// </summary>
/// <param name="textToFormat">The prepared subject line.</param>
/// <returns>A raw text subject line.</returns>
/// <remarks>This function is only needed when editing an existing message or when replying to
/// a message - it turns the HTML escaped characters back into their pre-escaped status.</remarks>
public static string HtmlEncode(String textToFormat) {
// strip the HTML - i.e., turn < into <, > into >
if(IsNullorEmpty(textToFormat))
return textToFormat;
//ScottW: Removed Context dependency
return System.Web.HttpUtility.HtmlEncode(textToFormat);
//return HttpContext.Current.Server.HtmlEncode(FormattedMessageSubject);
}
public static string UrlEncode(string urlToEncode)
{
if(IsNullorEmpty(urlToEncode))
return urlToEncode;
return System.Web.HttpUtility.UrlEncode(urlToEncode);
}
public static string UrlDecode(string urlToDecode)
{
if(IsNullorEmpty(urlToDecode))
return urlToDecode;
return System.Web.HttpUtility.UrlEncode(urlToDecode);
}
#endregion
#region DefaultForumView
/************ PROPERTY SET/GET STATEMENTS **************/
/// <summary>
/// Returns the default view to use for viewing the forum posts, as specified in the AspNetForumsSettings
/// section of Web.config.
/// </summary>
static public int DefaultForumView
{
get {
const int _defaultForumView = 2;
const String _settingName = "defaultForumView";
String _str = CSCache.Get("webForums." + _settingName) as String;
int iValue = _defaultForumView;
if (_str == null) {
// we need to get the string and place it in the cache
String prefix = "";
NameValueCollection context = (NameValueCollection)ConfigurationSettings.GetConfig("AspNetForumsSettings");
if (context == null) {
// get the appSettings context
prefix = Globals._appSettingsPrefix;;
context = (NameValueCollection)ConfigurationSettings.GetConfig("appSettings");
}
_str = context[prefix + _settingName];
// determine what forum view to show
if (_str == null)
// choose the default
iValue = _defaultForumView;
else
switch(_str.ToUpper()) {
case "THREADED":
iValue = 2;
break;
case "MIXED":
iValue = 1;
break;
case "FLAT":
iValue = 0;
break;
default:
iValue = _defaultForumView;
break;
}
_str = iValue.ToString();
CSCache.Insert("webForums." + _settingName, _str);
}
return Convert.ToInt32(_str);
}
}
#endregion
#region Skin/App Paths
static public String GetSkinPath()
{
// TODO -- Need to get the full path if the application path is not available
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -