using System; using System.Collections; using System.Collections.Generic; using System.Text; using Utils.IO; using Utils; namespace SiteCore.Framework { /// /// 注册类型 /// public class RegisterType { /// /// 开放注册 /// public static readonly int Open; /// /// 关闭注册 /// public static readonly int Close = 1; /// /// 关闭注册,但受邀请的除外 /// public static readonly int CloseUnlessInvite = 2; } /// /// 网站配置 /// public class SiteSetting { /// /// 保留用户名(不可以注册的用户名) /// public String[] ReservedUserName { get; set; } /// /// 保留的用户个性网址 /// public String[] ReservedUserUrl { get; set; } /// /// 保留的关键词(不可以在用户名和个性网址中使用) /// public String[] ReservedKey { get; set; } public Boolean IsReservedKeyContains( String inputName ) { string[] arr = this.ReservedKey; foreach (String key in arr) { if (strUtil.EqualsIgnoreCase( inputName, key ) || strUtil.EqualsIgnoreCase( inputName, key + "s" )) { return true; } } return false; } //------------------------------filter---------------------------------- /// /// 禁用的关键词 /// public String[] BadWords { get; set; } /// /// 禁用词汇的替换词 /// public String BadWordsReplacement { get; set; } /// /// 所有被禁的ip地址 /// public String[] BannedIp { get; set; } private String _bannedIpInfo; /// /// 给被屏蔽访客的警告信息 /// public String BannedIpInfo { get { if (_bannedIpInfo == null) return "对不起,你的 ip 地址已被屏蔽"; return _bannedIpInfo; } set { _bannedIpInfo = value; } } //邮箱 public String SmtpUrl { get; set; } public String SmtpUser { get; set; } public String SmtpPwd { get; set; } //蜘蛛 public String SpiderIp { get; set; } //天气Url public String WeatherUrl { get; set; } //短信 public String SmsUid { get; set; } public String SmsPwd { get; set; } public String SmsUrl { get; set; } public String SmsLoginUrl { get; set; } public String SmsVerifyMobileInfo { get; set; } public String SmsGroupPayMobileInfo { get; set; } public String SmsGroupCodMobileInfo { get; set; } public String SmsGoodsCodMobileInfo { get; set; } public String SmsGoodsPayMobileInfo { get; set; } public String SmsTakeMoenyMobileInfo { get; set; } public String SmsExitQueueMobileInfo { get; set; } public String SmsConsumptionInfo { get; set; } //充值 public String Partner_99 { get; set; } public String Key_99 { get; set; } public String Host_99 { get; set; } // 扩展的值 //------------------------------------------------------------------------------------- public int GetValueInt( String key ) { return cvt.ToInt( GetValue( key ) ); } public Boolean GetValueBool( String key ) { return cvt.ToBool( GetValue( key ) ); } public decimal GetValueDecimal( String key ) { return cvt.ToDecimal( GetValue( key ) ); } public DateTime GetValueTime( String key ) { return cvt.ToTime( GetValue( key ) ); } public String GetValue( String key ) { if (strUtil.IsNullOrEmpty( key )) return null; if (_valueAll == null) return null; if (_valueAll.ContainsKey( key ) == false) return null; return _valueAll[key]; } //------------------------- public String[] GetArrayValue( String key ) { return getArrayValue( _valueAll, key ); } internal String[] getArrayValue( Dictionary dic, String key ) { if (dic == null) return new String[] { }; if (dic.ContainsKey( key ) && strUtil.HasText( dic[key] )) { return GetArrayValueByString( dic[key] ); } return new String[] { }; } public static String[] GetArrayValueByString( String valOne ) { if (strUtil.IsNullOrEmpty( valOne )) return new String[] { }; String[] arrValue = valOne.Split( new[] { ',', '/', '|', ',', '、' } ); // 剔除掉无效的字符串 ArrayList results = new ArrayList(); foreach (String val in arrValue) { if (strUtil.HasText( val )) results.Add( val.Trim() ); } return (String[])results.ToArray( typeof( String ) ); } private Dictionary _valueAll; internal void setValueAll( Dictionary valueAll ) { _valueAll = valueAll; } //---------------------------------------------------------------- /// /// 将更新保存到磁盘 /// /// /// public void Update( String item, Object val ) { String itemValue = ""; if (val != null) itemValue = val.ToString(); itemValue = strUtil.Text2Html( itemValue ); saveConfig( item, itemValue ); } public void UpdateHtml( String item, Object val ) { String itemValue = ""; if (val != null) itemValue = val.ToString(); itemValue = itemValue.Replace( "\n", "" ).Replace( "\r", "" ); saveConfig( item, itemValue ); } private void saveConfig( String item, String itemValue ) { String[] configString = File.ReadAllLines( config.siteconfigAbsPath ); List results = new List(); checkSingleLine( item, itemValue, configString, results ); StringBuilder sb = new StringBuilder(); foreach (String line in results) { sb.Append( line ); sb.Append( Environment.NewLine ); } lock (objLock) { File.Write( config.siteconfigAbsPath, sb.ToString() ); } } private void checkSingleLine( String item, String itemValue, String[] configString, List results ) { Boolean hasItem = false; foreach (String line in configString) { if (startsWith( line, item )) { hasItem = true; results.Add( item + " : " + itemValue ); _valueAll[item] = itemValue; } else { results.Add( line ); } } if (hasItem == false) { results.Add( item + " : " + itemValue ); _valueAll[item] = itemValue; } } public void UpdateItems(string[] keys,string[] values) { String[] configString = File.ReadAllLines(config.siteconfigAbsPath); List results = new List(); foreach (String line in configString) { Boolean hasItem = false; for (int i = 0; i < keys.Length; i++) { if (startsWith(line, keys[i])) { hasItem = true; results.Add(keys[i] + " : " + values[i]); _valueAll[keys[i]] = values[i]; break; } } if (!hasItem) results.Add(line); } StringBuilder sb = new StringBuilder(); foreach (String line in results) { sb.Append(line); sb.Append(Environment.NewLine); } lock (objLock) { File.Write(config.siteconfigAbsPath, sb.ToString()); } } private static Boolean startsWith( String line, String item ) { if (line.StartsWith( item )) { String noPrefixLine = strUtil.TrimStart( line, item ).Trim(); if (noPrefixLine.StartsWith( ":" )) { return true; } } return false; } private static readonly Object objLock = new object(); } }