SiteSetting.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Utils.IO;
  6. using Utils;
  7. namespace SiteCore.Framework
  8. {
  9. /// <summary>
  10. /// 注册类型
  11. /// </summary>
  12. public class RegisterType {
  13. /// <summary>
  14. /// 开放注册
  15. /// </summary>
  16. public static readonly int Open;
  17. /// <summary>
  18. /// 关闭注册
  19. /// </summary>
  20. public static readonly int Close = 1;
  21. /// <summary>
  22. /// 关闭注册,但受邀请的除外
  23. /// </summary>
  24. public static readonly int CloseUnlessInvite = 2;
  25. }
  26. /// <summary>
  27. /// 网站配置
  28. /// </summary>
  29. public class SiteSetting {
  30. /// <summary>
  31. /// 保留用户名(不可以注册的用户名)
  32. /// </summary>
  33. public String[] ReservedUserName { get; set; }
  34. /// <summary>
  35. /// 保留的用户个性网址
  36. /// </summary>
  37. public String[] ReservedUserUrl { get; set; }
  38. /// <summary>
  39. /// 保留的关键词(不可以在用户名和个性网址中使用)
  40. /// </summary>
  41. public String[] ReservedKey { get; set; }
  42. public Boolean IsReservedKeyContains( String inputName ) {
  43. string[] arr = this.ReservedKey;
  44. foreach (String key in arr) {
  45. if (strUtil.EqualsIgnoreCase( inputName, key ) || strUtil.EqualsIgnoreCase( inputName, key + "s" )) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. //------------------------------filter----------------------------------
  52. /// <summary>
  53. /// 禁用的关键词
  54. /// </summary>
  55. public String[] BadWords { get; set; }
  56. /// <summary>
  57. /// 禁用词汇的替换词
  58. /// </summary>
  59. public String BadWordsReplacement { get; set; }
  60. /// <summary>
  61. /// 所有被禁的ip地址
  62. /// </summary>
  63. public String[] BannedIp { get; set; }
  64. private String _bannedIpInfo;
  65. /// <summary>
  66. /// 给被屏蔽访客的警告信息
  67. /// </summary>
  68. public String BannedIpInfo {
  69. get {
  70. if (_bannedIpInfo == null) return "对不起,你的 ip 地址已被屏蔽";
  71. return _bannedIpInfo;
  72. }
  73. set { _bannedIpInfo = value; }
  74. }
  75. //邮箱
  76. public String SmtpUrl { get; set; }
  77. public String SmtpUser { get; set; }
  78. public String SmtpPwd { get; set; }
  79. //蜘蛛
  80. public String SpiderIp { get; set; }
  81. //天气Url
  82. public String WeatherUrl { get; set; }
  83. //短信
  84. public String SmsUid { get; set; }
  85. public String SmsPwd { get; set; }
  86. public String SmsUrl { get; set; }
  87. public String SmsLoginUrl { get; set; }
  88. public String SmsVerifyMobileInfo { get; set; }
  89. public String SmsGroupPayMobileInfo { get; set; }
  90. public String SmsGroupCodMobileInfo { get; set; }
  91. public String SmsGoodsCodMobileInfo { get; set; }
  92. public String SmsGoodsPayMobileInfo { get; set; }
  93. public String SmsTakeMoenyMobileInfo { get; set; }
  94. public String SmsExitQueueMobileInfo { get; set; }
  95. public String SmsConsumptionInfo { get; set; }
  96. //充值
  97. public String Partner_99 { get; set; }
  98. public String Key_99 { get; set; }
  99. public String Host_99 { get; set; }
  100. // 扩展的值
  101. //-------------------------------------------------------------------------------------
  102. public int GetValueInt( String key ) {
  103. return cvt.ToInt( GetValue( key ) );
  104. }
  105. public Boolean GetValueBool( String key ) {
  106. return cvt.ToBool( GetValue( key ) );
  107. }
  108. public decimal GetValueDecimal( String key ) {
  109. return cvt.ToDecimal( GetValue( key ) );
  110. }
  111. public DateTime GetValueTime( String key ) {
  112. return cvt.ToTime( GetValue( key ) );
  113. }
  114. public String GetValue( String key ) {
  115. if (strUtil.IsNullOrEmpty( key )) return null;
  116. if (_valueAll == null) return null;
  117. if (_valueAll.ContainsKey( key ) == false) return null;
  118. return _valueAll[key];
  119. }
  120. //-------------------------
  121. public String[] GetArrayValue( String key ) {
  122. return getArrayValue( _valueAll, key );
  123. }
  124. internal String[] getArrayValue( Dictionary<String, String> dic, String key ) {
  125. if (dic == null) return new String[] { };
  126. if (dic.ContainsKey( key ) && strUtil.HasText( dic[key] )) {
  127. return GetArrayValueByString( dic[key] );
  128. }
  129. return new String[] { };
  130. }
  131. public static String[] GetArrayValueByString( String valOne ) {
  132. if (strUtil.IsNullOrEmpty( valOne )) return new String[] { };
  133. String[] arrValue = valOne.Split( new[] { ',', '/', '|', ',', '、' } );
  134. // 剔除掉无效的字符串
  135. ArrayList results = new ArrayList();
  136. foreach (String val in arrValue) {
  137. if (strUtil.HasText( val )) results.Add( val.Trim() );
  138. }
  139. return (String[])results.ToArray( typeof( String ) );
  140. }
  141. private Dictionary<String, String> _valueAll;
  142. internal void setValueAll( Dictionary<String, String> valueAll ) {
  143. _valueAll = valueAll;
  144. }
  145. //----------------------------------------------------------------
  146. /// <summary>
  147. /// 将更新保存到磁盘
  148. /// </summary>
  149. /// <param name="item"></param>
  150. /// <param name="val"></param>
  151. public void Update( String item, Object val ) {
  152. String itemValue = "";
  153. if (val != null) itemValue = val.ToString();
  154. itemValue = strUtil.Text2Html( itemValue );
  155. saveConfig( item, itemValue );
  156. }
  157. public void UpdateHtml( String item, Object val ) {
  158. String itemValue = "";
  159. if (val != null) itemValue = val.ToString();
  160. itemValue = itemValue.Replace( "\n", "" ).Replace( "\r", "" );
  161. saveConfig( item, itemValue );
  162. }
  163. private void saveConfig( String item, String itemValue ) {
  164. String[] configString = File.ReadAllLines( config.siteconfigAbsPath );
  165. List<String> results = new List<string>();
  166. checkSingleLine( item, itemValue, configString, results );
  167. StringBuilder sb = new StringBuilder();
  168. foreach (String line in results) {
  169. sb.Append( line );
  170. sb.Append( Environment.NewLine );
  171. }
  172. lock (objLock) {
  173. File.Write( config.siteconfigAbsPath, sb.ToString() );
  174. }
  175. }
  176. private void checkSingleLine( String item, String itemValue, String[] configString, List<String> results ) {
  177. Boolean hasItem = false;
  178. foreach (String line in configString) {
  179. if (startsWith( line, item )) {
  180. hasItem = true;
  181. results.Add( item + " : " + itemValue );
  182. _valueAll[item] = itemValue;
  183. }
  184. else {
  185. results.Add( line );
  186. }
  187. }
  188. if (hasItem == false) {
  189. results.Add( item + " : " + itemValue );
  190. _valueAll[item] = itemValue;
  191. }
  192. }
  193. public void UpdateItems(string[] keys,string[] values)
  194. {
  195. String[] configString = File.ReadAllLines(config.siteconfigAbsPath);
  196. List<String> results = new List<string>();
  197. foreach (String line in configString)
  198. {
  199. Boolean hasItem = false;
  200. for (int i = 0; i < keys.Length; i++)
  201. {
  202. if (startsWith(line, keys[i]))
  203. {
  204. hasItem = true;
  205. results.Add(keys[i] + " : " + values[i]);
  206. _valueAll[keys[i]] = values[i];
  207. break;
  208. }
  209. }
  210. if (!hasItem) results.Add(line);
  211. }
  212. StringBuilder sb = new StringBuilder();
  213. foreach (String line in results)
  214. {
  215. sb.Append(line);
  216. sb.Append(Environment.NewLine);
  217. }
  218. lock (objLock)
  219. {
  220. File.Write(config.siteconfigAbsPath, sb.ToString());
  221. }
  222. }
  223. private static Boolean startsWith( String line, String item ) {
  224. if (line.StartsWith( item )) {
  225. String noPrefixLine = strUtil.TrimStart( line, item ).Trim();
  226. if (noPrefixLine.StartsWith( ":" )) {
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. private static readonly Object objLock = new object();
  233. }
  234. }