WebUI_Function.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*******************************************************************************
  2. * iNethinkCMS - 网站内容管理系统
  3. * Copyright (C) 2012-2013 inethink.com
  4. *
  5. * @author jackyang <69991000@qq.com>
  6. * @website http://cms.inethink.com
  7. * @version 1.3.6.0 (2013-08-14)
  8. *
  9. * This is licensed under the GNU LGPL, version 3.0 or later.
  10. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
  11. *******************************************************************************/
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using System.Web;
  16. using System.Web.UI;
  17. using System.Web.UI.WebControls;
  18. using System.Collections;
  19. using System.IO;
  20. using System.Text.RegularExpressions;
  21. namespace iNethinkCMS.Web.UI
  22. {
  23. public class WebUI_Function
  24. {
  25. // 输出指定间隔的半角空格
  26. public static string SpaceLength(int i)
  27. {
  28. string space = "";
  29. for (int j = 0; j < i; j++)
  30. {
  31. space += System.Web.HttpContext.Current.Server.HtmlDecode("&nbsp;&nbsp;&nbsp;");
  32. }
  33. return space;
  34. }
  35. //获得文件扩展名
  36. public static string GetFileExt(string FullPath)
  37. {
  38. if (FullPath != "")
  39. return FullPath.Substring(FullPath.LastIndexOf('.') + 1).ToLower();
  40. else
  41. return "";
  42. }
  43. //创建文件夹
  44. public static void CreateFolder(string FolderPath)
  45. {
  46. if (!System.IO.Directory.Exists(FolderPath))
  47. {
  48. System.IO.Directory.CreateDirectory(FolderPath);
  49. }
  50. }
  51. //获取网站访问URL
  52. public static string Fun_GetHttpUrl()
  53. {
  54. string vAbsoluteUri = HttpContext.Current.Request.Url.AbsoluteUri;
  55. string vPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
  56. return vAbsoluteUri.Replace(vPathAndQuery, "");
  57. }
  58. //获取顶级域名
  59. public static string Fun_GetDomain()
  60. {
  61. string vHost = HttpContext.Current.Request.Url.Host;
  62. string[] BeReplacedStrs = new string[] { ".com.cn", ".edu.cn", ".net.cn", ".org.cn", ".co.jp", ".gov.cn", ".co.uk", "ac.cn", ".edu", ".tv", ".info", ".com", ".ac", ".ag", ".am", ".at", ".be", ".biz", ".bz", ".cc", ".cn", ".com", ".de", ".es", ".eu", ".fm", ".gs", ".hk", ".in", ".info", ".io", ".it", ".jp", ".la", ".md", ".ms", ".name", ".net", ".nl", ".nu", ".org", ".pl", ".ru", ".sc", ".se", ".sg", ".sh", ".tc", ".tk", ".tv", ".tw", ".us", ".co", ".uk", ".vc", ".vg", ".ws", ".il", ".li", ".nz" };
  63. foreach (string oneBeReplacedStr in BeReplacedStrs)
  64. {
  65. string BeReplacedStr = oneBeReplacedStr + " ";
  66. if (vHost.IndexOf(BeReplacedStr) != -1)
  67. {
  68. vHost = vHost.Replace(BeReplacedStr, string.Empty);
  69. break;
  70. }
  71. }
  72. int dotIndex = vHost.LastIndexOf(".");
  73. vHost = vHost.Substring(dotIndex + 1);
  74. return vHost;
  75. }
  76. //清空系统缓存
  77. public static void Fun_CacheDel()
  78. {
  79. string vCacheKey = Command.Command_Configuration.GetConfigString("CacheKey");
  80. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  81. System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
  82. while (CacheEnum.MoveNext())
  83. {
  84. if (Command.Command_StringPlus.Left(CacheEnum.Key.ToString(), vCacheKey.Length) == vCacheKey)
  85. {
  86. objCache.Remove(CacheEnum.Key.ToString());
  87. }
  88. }
  89. }
  90. //获取站点路径(通过栏目ID循环)
  91. public static string Fun_GetSitePath(int byCID, string subPath = "", bool hasBootrapWrap = true)
  92. {
  93. BLL.BLL_iNethinkCMS_Channel bll = new BLL.BLL_iNethinkCMS_Channel();
  94. Model.Model_iNethinkCMS_Channel model = new Model.Model_iNethinkCMS_Channel();
  95. /**
  96. * <ol class="breadcrumb">
  97. <li><a href="#">Home</a></li>
  98. <li><a href="#">Library</a></li>
  99. <li class="active">Data</li>
  100. </ol>
  101. */
  102. string vBackInfo = "";
  103. int vFatherID = byCID;
  104. do
  105. {
  106. model = bll.GetModel(vFatherID);
  107. vBackInfo = "<li><a href=\"channel.aspx?id=" + model.Cid + "\">" + model.Name + "</a></li>" + vBackInfo;
  108. vFatherID = Convert.ToInt32(model.FatherID);
  109. } while (vFatherID > 0);
  110. if (subPath.Trim().Length > 0)
  111. {
  112. vBackInfo = @"<li><a href=""/" + subPath + @""">首页</a></li>" + vBackInfo;
  113. }
  114. else
  115. {
  116. vBackInfo = "<li><a href=\"/\">首页</a></li>" + vBackInfo;
  117. }
  118. if (hasBootrapWrap) {
  119. vBackInfo = $@"<ol class=""breadcrumb"">{vBackInfo}</ol>";
  120. }
  121. return vBackInfo;
  122. }
  123. //获取站点路径(通过标题)
  124. public static string Fun_GetSitePath_FromTit(string byTit)
  125. {
  126. string vBackInfo = "<a href=\"/\">首页</a>";
  127. if (byTit.Length > 0)
  128. {
  129. vBackInfo += " > " + byTit;
  130. }
  131. return vBackInfo;
  132. }
  133. //获取缩略图
  134. public static string Fun_GetThumbnail(string byPicUrl, string byWidth, string byHeight, string byMode, string byQuality)
  135. {
  136. string vPicUrl = HttpContext.Current.Server.MapPath(byPicUrl);
  137. string rInfo = @"/upload/thumbnail/thumb_" + byMode + "_" + byQuality + "_" + byWidth + "_" + byHeight + "_" + Path.GetFileNameWithoutExtension(vPicUrl)
  138. + Path.GetExtension(vPicUrl);
  139. string vThumbPicUrl = HttpContext.Current.Server.MapPath(rInfo);
  140. int vWidth = Convert.ToInt32(byWidth);
  141. int vHeight = Convert.ToInt32(byHeight);
  142. int vQuality = Convert.ToInt32(byQuality);
  143. //判断小图是否存在
  144. if (File.Exists(vThumbPicUrl) == false)
  145. {
  146. try
  147. {
  148. CreateFolder(Path.GetDirectoryName(vThumbPicUrl));
  149. Helper.Helper_Thumbnails.CreationThumbnail(vPicUrl, vThumbPicUrl, vWidth, vHeight, byMode, vQuality);
  150. }
  151. catch
  152. {
  153. rInfo = byPicUrl;
  154. }
  155. }
  156. return rInfo;
  157. }
  158. //URL参数添加/修改
  159. public static string Fun_AddQueryToURL(string url, string key, string value)
  160. {
  161. int fragPos = url.LastIndexOf("#");
  162. string fragment = string.Empty;
  163. if (fragPos > -1)
  164. {
  165. fragment = url.Substring(fragPos);
  166. url = url.Substring(0, fragPos);
  167. }
  168. int querystart = url.IndexOf("?");
  169. if (querystart < 0)
  170. {
  171. url += "?" + key + "=" + value;
  172. }
  173. else
  174. {
  175. Regex reg = new Regex(@"(?<=[&\?])" + key + @"=[^\s&#]*", RegexOptions.Compiled);
  176. if (reg.IsMatch(url))
  177. url = reg.Replace(url, key + "=" + value);
  178. else
  179. url += "&" + key + "=" + value;
  180. }
  181. return url + fragment;
  182. }
  183. //URL参数删除
  184. public static string Fun_RemoveQueryFromURL(string url, string key)
  185. {
  186. Regex reg = new Regex(@"[&\?]" + key + @"=[^\s&#]*&?", RegexOptions.Compiled);
  187. return reg.Replace(url, new MatchEvaluator(PutAwayGarbageFromURL));
  188. }
  189. private static string PutAwayGarbageFromURL(Match match)
  190. {
  191. string value = match.Value;
  192. if (value.EndsWith("&"))
  193. {
  194. return value.Substring(0, 1);
  195. }
  196. else
  197. {
  198. return string.Empty;
  199. }
  200. }
  201. //获取字典类型
  202. public static string Fun_GetDictTypeName_FromDictType(object byDictType)
  203. {
  204. string vDictName;
  205. int vDictType = int.Parse(byDictType.ToString());
  206. switch (vDictType)
  207. {
  208. case 1:
  209. vDictName = "友情链接";
  210. break;
  211. default:
  212. vDictName = "-";
  213. break;
  214. }
  215. return vDictName;
  216. }
  217. //获取指定自定义字段信息
  218. public static string Fun_GetFieldsInfo(string byCustomFieldsKey, string byFieldsInfo)
  219. {
  220. string vFieldsInfo = "";
  221. bool isFieldsInfoText = false;//是否获取自定义字段的Text
  222. string vCustomFieldsValue = "";//自定义字段设置的值字符串
  223. if (byCustomFieldsKey.LastIndexOf("_text", StringComparison.OrdinalIgnoreCase) == byCustomFieldsKey.Length-5)
  224. {
  225. byCustomFieldsKey = byCustomFieldsKey.Substring(0, byCustomFieldsKey.Length - 5);
  226. vCustomFieldsValue = Fun_GetCustomFieldsValue(byCustomFieldsKey);
  227. if (vCustomFieldsValue.IndexOf("=") > 0)
  228. {
  229. isFieldsInfoText = true;
  230. }
  231. }
  232. //byCustomFieldsKey = byCustomFieldsKey.ToLower();
  233. int i = byFieldsInfo.IndexOf("<" + byCustomFieldsKey + ">", StringComparison.OrdinalIgnoreCase);
  234. int j = byFieldsInfo.IndexOf("</" + byCustomFieldsKey + ">", StringComparison.OrdinalIgnoreCase);
  235. if (i >= 0 && j >= 0)
  236. {
  237. i = i + byCustomFieldsKey.Length + 2;
  238. j = j - i;
  239. vFieldsInfo = Command.Command_StringPlus.Mid(byFieldsInfo, i, j);
  240. }
  241. if (isFieldsInfoText) {
  242. vFieldsInfo = Fun_GetCustomFieldsText(vCustomFieldsValue, vFieldsInfo);
  243. }
  244. return vFieldsInfo;
  245. }
  246. public static string Fun_GetCustomFieldsText(string byCustomFieldsValue, string byValue)
  247. {
  248. string retValue = "";
  249. string[] tempFieldsList = byCustomFieldsValue.Split(',');
  250. foreach (string tempFields in tempFieldsList)
  251. {
  252. try
  253. {
  254. if (tempFields.Trim().Length > 0)
  255. {
  256. string[] tempFieldsValues = tempFields.Split('=');
  257. if (tempFieldsValues.Length == 2 && byValue.Trim().ToLower() == tempFieldsValues[1].Trim().ToLower())
  258. {
  259. retValue = tempFieldsValues[0].Trim();
  260. break;
  261. }
  262. }
  263. }
  264. catch { }
  265. }
  266. return retValue;
  267. }
  268. static System.Data.DataTable customfieldsDataTable = null;
  269. public static System.Data.DataTable CustomfieldsDataTable
  270. {
  271. get { return WebUI_Function.customfieldsDataTable; }
  272. }
  273. /// <summary>
  274. ///
  275. /// </summary>
  276. /// <param name="byCustomFieldsKey"></param>
  277. /// <param name="isReload">是否为刷新加载</param>
  278. /// <returns></returns>
  279. public static string Fun_GetCustomFieldsValue(string byCustomFieldsKey, bool isReload=false)
  280. {
  281. string vCustomFieldsValue = "";
  282. if (customfieldsDataTable == null)
  283. {
  284. iNethinkCMS.BLL.BLL_iNethinkCMS_Channel_CustomFields bll_customfields = new BLL.BLL_iNethinkCMS_Channel_CustomFields();
  285. customfieldsDataTable = bll_customfields.GetList(0, "[Display]=1", "OrderNum Desc,ID Desc").Tables[0];
  286. // customfieldsDataTable.PrimaryKey = new System.Data.DataColumn[] { new System.Data.DataColumn("CustomFieldsKey") };
  287. }
  288. for (int i = 0; i < customfieldsDataTable.Rows.Count; i++)
  289. {
  290. string vCustomFieldsKey = customfieldsDataTable.Rows[i]["CustomFieldsKey"].ToString();
  291. if (vCustomFieldsKey.Trim().ToLower() == byCustomFieldsKey.Trim().ToLower())
  292. {
  293. vCustomFieldsValue = customfieldsDataTable.Rows[i]["CustomFieldsValue"].ToString().Trim();
  294. break;
  295. }
  296. else if (i + 1 == customfieldsDataTable.Rows.Count)
  297. {
  298. customfieldsDataTable.Rows.Clear();
  299. customfieldsDataTable.Dispose();
  300. customfieldsDataTable = null;
  301. if (!isReload)
  302. {
  303. vCustomFieldsValue = Fun_GetCustomFieldsValue(byCustomFieldsKey, true);
  304. }
  305. }
  306. }
  307. return vCustomFieldsValue;
  308. }
  309. //获取地址
  310. public static string Fun_UrlRewriter(string byString)
  311. {
  312. //string vTmp = byString;
  313. int vUrlMode = Command.Command_Configuration.GetConfigInt("UrlMode");
  314. if (vUrlMode == 1)
  315. {
  316. string vRewriteExtName = Command.Command_Configuration.GetConfigString("RewriteExtName");
  317. string vRewriteChannelPrefix = Command.Command_Configuration.GetConfigString("RewriteChannelPrefix");
  318. string vRewriteSpecialPrefix = Command.Command_Configuration.GetConfigString("RewriteSpecialPrefix");
  319. string vRewriteContentPrefix = Command.Command_Configuration.GetConfigString("RewriteContentPrefix");
  320. string vRewriteGuestbookPrefix = Command.Command_Configuration.GetConfigString("RewriteGuestbookPrefix");
  321. byString = Fun_ReplaceX(byString, @"(content.aspx\?id=)(\d+)(&page=)(\d+)", vRewriteContentPrefix + "_$2_$4." + vRewriteExtName);
  322. byString = Fun_ReplaceX(byString, @"(content.aspx\?id=)(\d+)", vRewriteContentPrefix + "_$2." + vRewriteExtName);
  323. byString = Fun_ReplaceX(byString, @"(channel.aspx\?id=)(\d+)(&page=)(\d+)", vRewriteChannelPrefix + "_$2_$4." + vRewriteExtName);
  324. byString = Fun_ReplaceX(byString, @"(channel.aspx\?id=)(\d+)", vRewriteChannelPrefix + "_$2." + vRewriteExtName);
  325. byString = Fun_ReplaceX(byString, @"(special.aspx\?id=)(\d+)(&page=)(\d+)", vRewriteSpecialPrefix + "_$2_$4." + vRewriteExtName);
  326. byString = Fun_ReplaceX(byString, @"(special.aspx\?id=)(\d+)", vRewriteSpecialPrefix + "_$2." + vRewriteExtName);
  327. byString = Fun_ReplaceX(byString, @"(plugs/guestbook/index.aspx\?page=)(\d+)", vRewriteGuestbookPrefix + "_$2." + vRewriteExtName);
  328. byString = Fun_ReplaceX(byString, @"(plugs/guestbook/index.aspx)", vRewriteGuestbookPrefix + vRewriteExtName);
  329. byString = Fun_ReplaceX(byString, @"(index.aspx\?page=)(\d+)", "index_$2." + vRewriteExtName);
  330. byString = Fun_ReplaceX(byString, @"(index.aspx)", "index." + vRewriteExtName);
  331. }
  332. return byString;
  333. }
  334. public static string Fun_ReplaceX(string byHtml, string byPatterns, string byReplaceval)
  335. {
  336. Regex myRegex = new Regex(byPatterns, RegexOptions.IgnoreCase);
  337. return myRegex.Replace(byHtml, byReplaceval);
  338. }
  339. }
  340. }