WebHelper.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.Hosting;
  10. namespace GreenTree.Nachtragsmanagement.Core
  11. {
  12. public class WebHelper
  13. {
  14. #region Fields
  15. private readonly HttpContextBase _httpContext;
  16. #endregion
  17. #region Utilities
  18. protected virtual Boolean IsRequestAvailable(HttpContextBase httpContext)
  19. {
  20. if (httpContext == null)
  21. return false;
  22. try
  23. {
  24. if (httpContext.Request == null)
  25. return false;
  26. }
  27. catch (HttpException)
  28. {
  29. return false;
  30. }
  31. return true;
  32. }
  33. protected virtual bool TryWriteWebConfig()
  34. {
  35. try
  36. {
  37. // In medium trust, "UnloadAppDomain" is not supported. Touch web.config
  38. // to force an AppDomain restart.
  39. File.SetLastWriteTimeUtc(MapPath("~/web.config"), DateTime.UtcNow);
  40. return true;
  41. }
  42. catch
  43. {
  44. return false;
  45. }
  46. }
  47. protected virtual bool TryWriteGlobalAsax()
  48. {
  49. try
  50. {
  51. //When a new plugin is dropped in the Plugins folder and is installed into nopCommerce,
  52. //even if the plugin has registered routes for its controllers,
  53. //these routes will not be working as the MVC framework couldn't
  54. //find the new controller types and couldn't instantiate the requested controller.
  55. //That's why you get these nasty errors
  56. //i.e "Controller does not implement IController".
  57. //The issue is described here: http://www.nopcommerce.com/boards/t/10969/nop-20-plugin.aspx?p=4#51318
  58. //The solution is to touch global.asax file
  59. File.SetLastWriteTimeUtc(MapPath("~/global.asax"), DateTime.UtcNow);
  60. return true;
  61. }
  62. catch
  63. {
  64. return false;
  65. }
  66. }
  67. #endregion
  68. #region Methods
  69. /// <summary>
  70. /// Ctor
  71. /// </summary>
  72. /// <param name="httpContext">HTTP context</param>
  73. public WebHelper(HttpContextBase httpContext)
  74. {
  75. _httpContext = httpContext;
  76. }
  77. /// <summary>
  78. /// Get URL referrer
  79. /// </summary>
  80. /// <returns>URL referrer</returns>
  81. public virtual string GetUrlReferrer()
  82. {
  83. string referrerUrl = string.Empty;
  84. //URL referrer is null in some case (for example, in IE 8)
  85. if (IsRequestAvailable(_httpContext) && _httpContext.Request.UrlReferrer != null)
  86. referrerUrl = _httpContext.Request.UrlReferrer.PathAndQuery;
  87. return referrerUrl;
  88. }
  89. /// <summary>
  90. /// Get context IP address
  91. /// </summary>
  92. /// <returns>URL referrer</returns>
  93. public virtual string GetCurrentIpAddress()
  94. {
  95. if (!IsRequestAvailable(_httpContext))
  96. return string.Empty;
  97. var result = "";
  98. if (_httpContext.Request.Headers != null)
  99. {
  100. //The X-Forwarded-For (XFF) HTTP header field is a de facto standard
  101. //for identifying the originating IP address of a client
  102. //connecting to a web server through an HTTP proxy or load balancer.
  103. var forwardedHttpHeader = "X-FORWARDED-FOR";
  104. if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["ForwardedHTTPheader"]))
  105. {
  106. //but in some cases server use other HTTP header
  107. //in these cases an administrator can specify a custom Forwarded HTTP header
  108. forwardedHttpHeader = ConfigurationManager.AppSettings["ForwardedHTTPheader"];
  109. }
  110. //it's used for identifying the originating IP address of a client connecting to a web server
  111. //through an HTTP proxy or load balancer.
  112. string xff = _httpContext.Request.Headers.AllKeys
  113. .Where(x => forwardedHttpHeader.Equals(x, StringComparison.InvariantCultureIgnoreCase))
  114. .Select(k => _httpContext.Request.Headers[k])
  115. .FirstOrDefault();
  116. //if you want to exclude private IP addresses, then see http://stackoverflow.com/questions/2577496/how-can-i-get-the-clients-ip-address-in-asp-net-mvc
  117. if (!String.IsNullOrEmpty(xff))
  118. {
  119. string lastIp = xff.Split(new[] { ',' }).FirstOrDefault();
  120. result = lastIp;
  121. }
  122. }
  123. if (String.IsNullOrEmpty(result) && _httpContext.Request.UserHostAddress != null)
  124. {
  125. result = _httpContext.Request.UserHostAddress;
  126. }
  127. //some validation
  128. if (result == "::1")
  129. result = "127.0.0.1";
  130. //remove port
  131. if (!String.IsNullOrEmpty(result))
  132. {
  133. int index = result.IndexOf(":", StringComparison.InvariantCultureIgnoreCase);
  134. if (index > 0)
  135. result = result.Substring(0, index);
  136. }
  137. return result;
  138. }
  139. /// <summary>
  140. /// Gets a value indicating whether current connection is secured
  141. /// </summary>
  142. /// <returns>true - secured, false - not secured</returns>
  143. public virtual bool IsCurrentConnectionSecured()
  144. {
  145. bool useSsl = false;
  146. if (IsRequestAvailable(_httpContext))
  147. {
  148. useSsl = _httpContext.Request.IsSecureConnection;
  149. //when your hosting uses a load balancer on their server then the Request.IsSecureConnection is never got set to true, use the statement below
  150. //just uncomment it
  151. //useSSL = _httpContext.Request.ServerVariables["HTTP_CLUSTER_HTTPS"] == "on" ? true : false;
  152. }
  153. return useSsl;
  154. }
  155. /// <summary>
  156. /// Gets server variable by name
  157. /// </summary>
  158. /// <param name="name">Name</param>
  159. /// <returns>Server variable</returns>
  160. public virtual string ServerVariables(string name)
  161. {
  162. string result = string.Empty;
  163. try
  164. {
  165. if (!IsRequestAvailable(_httpContext))
  166. return result;
  167. //put this method is try-catch
  168. //as described here http://www.nopcommerce.com/boards/t/21356/multi-store-roadmap-lets-discuss-update-done.aspx?p=6#90196
  169. if (_httpContext.Request.ServerVariables[name] != null)
  170. {
  171. result = _httpContext.Request.ServerVariables[name];
  172. }
  173. }
  174. catch
  175. {
  176. result = string.Empty;
  177. }
  178. return result;
  179. }
  180. /// <summary>
  181. /// Returns true if the requested resource is one of the typical resources that needn't be processed by the cms engine.
  182. /// </summary>
  183. /// <param name="request">HTTP Request</param>
  184. /// <returns>True if the request targets a static resource file.</returns>
  185. /// <remarks>
  186. /// These are the file extensions considered to be static resources:
  187. /// .css
  188. /// .gif
  189. /// .png
  190. /// .jpg
  191. /// .jpeg
  192. /// .js
  193. /// .axd
  194. /// .ashx
  195. /// </remarks>
  196. public virtual bool IsStaticResource(HttpRequest request)
  197. {
  198. if (request == null)
  199. throw new ArgumentNullException("request");
  200. string path = request.Path;
  201. string extension = VirtualPathUtility.GetExtension(path);
  202. if (extension == null) return false;
  203. switch (extension.ToLower())
  204. {
  205. case ".axd":
  206. case ".ashx":
  207. case ".bmp":
  208. case ".css":
  209. case ".gif":
  210. case ".htm":
  211. case ".html":
  212. case ".ico":
  213. case ".jpeg":
  214. case ".jpg":
  215. case ".js":
  216. case ".png":
  217. case ".rar":
  218. case ".zip":
  219. return true;
  220. }
  221. return false;
  222. }
  223. /// <summary>
  224. /// Maps a virtual path to a physical disk path.
  225. /// </summary>
  226. /// <param name="path">The path to map. E.g. "~/bin"</param>
  227. /// <returns>The physical path. E.g. "c:\inetpub\wwwroot\bin"</returns>
  228. public virtual string MapPath(string path)
  229. {
  230. if (HostingEnvironment.IsHosted)
  231. {
  232. //hosted
  233. return HostingEnvironment.MapPath(path);
  234. }
  235. //not hosted. For example, run in unit tests
  236. string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
  237. path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
  238. return Path.Combine(baseDirectory, path);
  239. }
  240. /// <summary>
  241. /// Modifies query string
  242. /// </summary>
  243. /// <param name="url">Url to modify</param>
  244. /// <param name="queryStringModification">Query string modification</param>
  245. /// <param name="anchor">Anchor</param>
  246. /// <returns>New url</returns>
  247. public virtual string ModifyQueryString(string url, string queryStringModification, string anchor)
  248. {
  249. if (url == null)
  250. url = string.Empty;
  251. url = url.ToLowerInvariant();
  252. if (queryStringModification == null)
  253. queryStringModification = string.Empty;
  254. queryStringModification = queryStringModification.ToLowerInvariant();
  255. if (anchor == null)
  256. anchor = string.Empty;
  257. anchor = anchor.ToLowerInvariant();
  258. string str = string.Empty;
  259. string str2 = string.Empty;
  260. if (url.Contains("#"))
  261. {
  262. str2 = url.Substring(url.IndexOf("#") + 1);
  263. url = url.Substring(0, url.IndexOf("#"));
  264. }
  265. if (url.Contains("?"))
  266. {
  267. str = url.Substring(url.IndexOf("?") + 1);
  268. url = url.Substring(0, url.IndexOf("?"));
  269. }
  270. if (!string.IsNullOrEmpty(queryStringModification))
  271. {
  272. if (!string.IsNullOrEmpty(str))
  273. {
  274. var dictionary = new Dictionary<string, string>();
  275. foreach (string str3 in str.Split(new[] { '&' }))
  276. {
  277. if (!string.IsNullOrEmpty(str3))
  278. {
  279. string[] strArray = str3.Split(new[] { '=' });
  280. if (strArray.Length == 2)
  281. {
  282. if (!dictionary.ContainsKey(strArray[0]))
  283. {
  284. //do not add value if it already exists
  285. //two the same query parameters? theoretically it's not possible.
  286. //but MVC has some ugly implementation for checkboxes and we can have two values
  287. //find more info here: http://www.mindstorminteractive.com/topics/jquery-fix-asp-net-mvc-checkbox-truefalse-value/
  288. //we do this validation just to ensure that the first one is not overridden
  289. dictionary[strArray[0]] = strArray[1];
  290. }
  291. }
  292. else
  293. {
  294. dictionary[str3] = null;
  295. }
  296. }
  297. }
  298. foreach (string str4 in queryStringModification.Split(new[] { '&' }))
  299. {
  300. if (!string.IsNullOrEmpty(str4))
  301. {
  302. string[] strArray2 = str4.Split(new[] { '=' });
  303. if (strArray2.Length == 2)
  304. {
  305. dictionary[strArray2[0]] = strArray2[1];
  306. }
  307. else
  308. {
  309. dictionary[str4] = null;
  310. }
  311. }
  312. }
  313. var builder = new StringBuilder();
  314. foreach (string str5 in dictionary.Keys)
  315. {
  316. if (builder.Length > 0)
  317. {
  318. builder.Append("&");
  319. }
  320. builder.Append(str5);
  321. if (dictionary[str5] != null)
  322. {
  323. builder.Append("=");
  324. builder.Append(dictionary[str5]);
  325. }
  326. }
  327. str = builder.ToString();
  328. }
  329. else
  330. {
  331. str = queryStringModification;
  332. }
  333. }
  334. if (!string.IsNullOrEmpty(anchor))
  335. {
  336. str2 = anchor;
  337. }
  338. return (url + (string.IsNullOrEmpty(str) ? "" : ("?" + str)) + (string.IsNullOrEmpty(str2) ? "" : ("#" + str2))).ToLowerInvariant();
  339. }
  340. /// <summary>
  341. /// Remove query string from url
  342. /// </summary>
  343. /// <param name="url">Url to modify</param>
  344. /// <param name="queryString">Query string to remove</param>
  345. /// <returns>New url</returns>
  346. public virtual string RemoveQueryString(string url, string queryString)
  347. {
  348. if (url == null)
  349. url = string.Empty;
  350. url = url.ToLowerInvariant();
  351. if (queryString == null)
  352. queryString = string.Empty;
  353. queryString = queryString.ToLowerInvariant();
  354. string str = string.Empty;
  355. if (url.Contains("?"))
  356. {
  357. str = url.Substring(url.IndexOf("?") + 1);
  358. url = url.Substring(0, url.IndexOf("?"));
  359. }
  360. if (!string.IsNullOrEmpty(queryString))
  361. {
  362. if (!string.IsNullOrEmpty(str))
  363. {
  364. var dictionary = new Dictionary<string, string>();
  365. foreach (string str3 in str.Split(new[] { '&' }))
  366. {
  367. if (!string.IsNullOrEmpty(str3))
  368. {
  369. string[] strArray = str3.Split(new[] { '=' });
  370. if (strArray.Length == 2)
  371. {
  372. dictionary[strArray[0]] = strArray[1];
  373. }
  374. else
  375. {
  376. dictionary[str3] = null;
  377. }
  378. }
  379. }
  380. dictionary.Remove(queryString);
  381. var builder = new StringBuilder();
  382. foreach (string str5 in dictionary.Keys)
  383. {
  384. if (builder.Length > 0)
  385. {
  386. builder.Append("&");
  387. }
  388. builder.Append(str5);
  389. if (dictionary[str5] != null)
  390. {
  391. builder.Append("=");
  392. builder.Append(dictionary[str5]);
  393. }
  394. }
  395. str = builder.ToString();
  396. }
  397. }
  398. return (url + (string.IsNullOrEmpty(str) ? "" : ("?" + str)));
  399. }
  400. /// <summary>
  401. /// Gets query string value by name
  402. /// </summary>
  403. /// <typeparam name="T"></typeparam>
  404. /// <param name="name">Parameter name</param>
  405. /// <returns>Query string value</returns>
  406. public virtual T QueryString<T>(string name)
  407. {
  408. string queryParam = null;
  409. if (IsRequestAvailable(_httpContext) && _httpContext.Request.QueryString[name] != null)
  410. queryParam = _httpContext.Request.QueryString[name];
  411. if (!String.IsNullOrEmpty(queryParam))
  412. return CommonHelper.To<T>(queryParam);
  413. return default(T);
  414. }
  415. /// <summary>
  416. /// Restart application domain
  417. /// </summary>
  418. /// <param name="makeRedirect">A value indicating whether we should made redirection after restart</param>
  419. /// <param name="redirectUrl">Redirect URL; empty string if you want to redirect to the current page URL</param>
  420. public virtual void RestartAppDomain(bool makeRedirect = false, string redirectUrl = "")
  421. {
  422. if (CommonHelper.GetTrustLevel() > AspNetHostingPermissionLevel.Medium)
  423. {
  424. //full trust
  425. HttpRuntime.UnloadAppDomain();
  426. TryWriteGlobalAsax();
  427. }
  428. else
  429. {
  430. //medium trust
  431. bool success = TryWriteWebConfig();
  432. if (!success)
  433. {
  434. throw new Exception("The application needs to be restarted due to a configuration change, but was unable to do so." + Environment.NewLine +
  435. "To prevent this issue in the future, a change to the web server configuration is required:" + Environment.NewLine +
  436. "- run the application in a full trust environment, or" + Environment.NewLine +
  437. "- give the application write access to the 'web.config' file.");
  438. }
  439. success = TryWriteGlobalAsax();
  440. if (!success)
  441. {
  442. throw new Exception("The application needs to be restarted due to a configuration change, but was unable to do so." + Environment.NewLine +
  443. "To prevent this issue in the future, a change to the web server configuration is required:" + Environment.NewLine +
  444. "- run the application in a full trust environment, or" + Environment.NewLine +
  445. "- give the application write access to the 'Global.asax' file.");
  446. }
  447. }
  448. // If setting up extensions/modules requires an AppDomain restart, it's very unlikely the
  449. // current request can be processed correctly. So, we redirect to the same URL, so that the
  450. // new request will come to the newly started AppDomain.
  451. if (_httpContext != null && makeRedirect)
  452. {
  453. if (String.IsNullOrEmpty(redirectUrl))
  454. redirectUrl = "~/";
  455. _httpContext.Response.Redirect(redirectUrl, true /*endResponse*/);
  456. }
  457. }
  458. /// <summary>
  459. /// Gets a value that indicates whether the client is being redirected to a new location
  460. /// </summary>
  461. public virtual bool IsRequestBeingRedirected
  462. {
  463. get
  464. {
  465. var response = _httpContext.Response;
  466. return response.IsRequestBeingRedirected;
  467. }
  468. }
  469. /// <summary>
  470. /// Gets or sets a value that indicates whether the client is being redirected to a new location using POST
  471. /// </summary>
  472. public virtual bool IsPostBeingDone
  473. {
  474. get
  475. {
  476. if (_httpContext.Items["nop.IsPOSTBeingDone"] == null)
  477. return false;
  478. return Convert.ToBoolean(_httpContext.Items["nop.IsPOSTBeingDone"]);
  479. }
  480. set
  481. {
  482. _httpContext.Items["nop.IsPOSTBeingDone"] = value;
  483. }
  484. }
  485. #endregion
  486. }
  487. }