WebHelper.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 : IWebHelper
  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 this page name
  141. /// </summary>
  142. /// <param name="includeQueryString">Value indicating whether to include query strings</param>
  143. /// <returns>Page name</returns>
  144. public virtual string GetThisPageUrl(bool includeQueryString)
  145. {
  146. var useSsl = IsCurrentConnectionSecured();
  147. return GetThisPageUrl(includeQueryString, useSsl);
  148. }
  149. /// <summary>
  150. /// Gets this page name
  151. /// </summary>
  152. /// <param name="includeQueryString">Value indicating whether to include query strings</param>
  153. /// <param name="useSsl">Value indicating whether to get SSL protected page</param>
  154. /// <returns>Page name</returns>
  155. public virtual string GetThisPageUrl(bool includeQueryString, bool useSsl)
  156. {
  157. var url = String.Empty;
  158. if (!IsRequestAvailable(_httpContext))
  159. return url;
  160. if (includeQueryString)
  161. {
  162. var storeHost = GetStoreHost(useSsl);
  163. if (storeHost.EndsWith("/"))
  164. storeHost = storeHost.Substring(0, storeHost.Length - 1);
  165. url = storeHost + _httpContext.Request.RawUrl;
  166. }
  167. else
  168. {
  169. if (_httpContext.Request.Url != null)
  170. url = _httpContext.Request.Url.GetLeftPart(UriPartial.Path);
  171. }
  172. url = url.ToLowerInvariant();
  173. return url;
  174. }
  175. /// <summary>
  176. /// Gets store host location
  177. /// </summary>
  178. /// <param name="useSsl">Use SSL</param>
  179. /// <returns>Store host location</returns>
  180. public virtual string GetStoreHost(bool useSsl)
  181. {
  182. var result = "";
  183. var httpHost = ServerVariables("HTTP_HOST");
  184. if (!String.IsNullOrEmpty(httpHost))
  185. {
  186. result = "http://" + httpHost;
  187. if (!result.EndsWith("/"))
  188. result += "/";
  189. }
  190. if (!result.EndsWith("/"))
  191. result += "/";
  192. return result.ToLowerInvariant();
  193. }
  194. /// <summary>
  195. /// Gets a value indicating whether current connection is secured
  196. /// </summary>
  197. /// <returns>true - secured, false - not secured</returns>
  198. public virtual bool IsCurrentConnectionSecured()
  199. {
  200. bool useSsl = false;
  201. if (IsRequestAvailable(_httpContext))
  202. {
  203. useSsl = _httpContext.Request.IsSecureConnection;
  204. //when your hosting uses a load balancer on their server then the Request.IsSecureConnection is never got set to true, use the statement below
  205. //just uncomment it
  206. //useSSL = _httpContext.Request.ServerVariables["HTTP_CLUSTER_HTTPS"] == "on" ? true : false;
  207. }
  208. return useSsl;
  209. }
  210. /// <summary>
  211. /// Gets server variable by name
  212. /// </summary>
  213. /// <param name="name">Name</param>
  214. /// <returns>Server variable</returns>
  215. public virtual string ServerVariables(string name)
  216. {
  217. string result = string.Empty;
  218. try
  219. {
  220. if (!IsRequestAvailable(_httpContext))
  221. return result;
  222. //put this method is try-catch
  223. //as described here http://www.nopcommerce.com/boards/t/21356/multi-store-roadmap-lets-discuss-update-done.aspx?p=6#90196
  224. if (_httpContext.Request.ServerVariables[name] != null)
  225. {
  226. result = _httpContext.Request.ServerVariables[name];
  227. }
  228. }
  229. catch
  230. {
  231. result = string.Empty;
  232. }
  233. return result;
  234. }
  235. /// <summary>
  236. /// Returns true if the requested resource is one of the typical resources that needn't be processed by the cms engine.
  237. /// </summary>
  238. /// <param name="request">HTTP Request</param>
  239. /// <returns>True if the request targets a static resource file.</returns>
  240. /// <remarks>
  241. /// These are the file extensions considered to be static resources:
  242. /// .css
  243. /// .gif
  244. /// .png
  245. /// .jpg
  246. /// .jpeg
  247. /// .js
  248. /// .axd
  249. /// .ashx
  250. /// </remarks>
  251. public virtual bool IsStaticResource(HttpRequest request)
  252. {
  253. if (request == null)
  254. throw new ArgumentNullException("request");
  255. string path = request.Path;
  256. string extension = VirtualPathUtility.GetExtension(path);
  257. if (extension == null) return false;
  258. switch (extension.ToLower())
  259. {
  260. case ".axd":
  261. case ".ashx":
  262. case ".bmp":
  263. case ".css":
  264. case ".gif":
  265. case ".htm":
  266. case ".html":
  267. case ".ico":
  268. case ".jpeg":
  269. case ".jpg":
  270. case ".js":
  271. case ".png":
  272. case ".rar":
  273. case ".zip":
  274. return true;
  275. }
  276. return false;
  277. }
  278. /// <summary>
  279. /// Maps a virtual path to a physical disk path.
  280. /// </summary>
  281. /// <param name="path">The path to map. E.g. "~/bin"</param>
  282. /// <returns>The physical path. E.g. "c:\inetpub\wwwroot\bin"</returns>
  283. public virtual string MapPath(string path)
  284. {
  285. if (HostingEnvironment.IsHosted)
  286. {
  287. //hosted
  288. return HostingEnvironment.MapPath(path);
  289. }
  290. //not hosted. For example, run in unit tests
  291. string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
  292. path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
  293. return Path.Combine(baseDirectory, path);
  294. }
  295. /// <summary>
  296. /// Modifies query string
  297. /// </summary>
  298. /// <param name="url">Url to modify</param>
  299. /// <param name="queryStringModification">Query string modification</param>
  300. /// <param name="anchor">Anchor</param>
  301. /// <returns>New url</returns>
  302. public virtual string ModifyQueryString(string url, string queryStringModification, string anchor)
  303. {
  304. if (url == null)
  305. url = string.Empty;
  306. url = url.ToLowerInvariant();
  307. if (queryStringModification == null)
  308. queryStringModification = string.Empty;
  309. queryStringModification = queryStringModification.ToLowerInvariant();
  310. if (anchor == null)
  311. anchor = string.Empty;
  312. anchor = anchor.ToLowerInvariant();
  313. string str = string.Empty;
  314. string str2 = string.Empty;
  315. if (url.Contains("#"))
  316. {
  317. str2 = url.Substring(url.IndexOf("#") + 1);
  318. url = url.Substring(0, url.IndexOf("#"));
  319. }
  320. if (url.Contains("?"))
  321. {
  322. str = url.Substring(url.IndexOf("?") + 1);
  323. url = url.Substring(0, url.IndexOf("?"));
  324. }
  325. if (!string.IsNullOrEmpty(queryStringModification))
  326. {
  327. if (!string.IsNullOrEmpty(str))
  328. {
  329. var dictionary = new Dictionary<string, string>();
  330. foreach (string str3 in str.Split(new[] { '&' }))
  331. {
  332. if (!string.IsNullOrEmpty(str3))
  333. {
  334. string[] strArray = str3.Split(new[] { '=' });
  335. if (strArray.Length == 2)
  336. {
  337. if (!dictionary.ContainsKey(strArray[0]))
  338. {
  339. //do not add value if it already exists
  340. //two the same query parameters? theoretically it's not possible.
  341. //but MVC has some ugly implementation for checkboxes and we can have two values
  342. //find more info here: http://www.mindstorminteractive.com/topics/jquery-fix-asp-net-mvc-checkbox-truefalse-value/
  343. //we do this validation just to ensure that the first one is not overridden
  344. dictionary[strArray[0]] = strArray[1];
  345. }
  346. }
  347. else
  348. {
  349. dictionary[str3] = null;
  350. }
  351. }
  352. }
  353. foreach (string str4 in queryStringModification.Split(new[] { '&' }))
  354. {
  355. if (!string.IsNullOrEmpty(str4))
  356. {
  357. string[] strArray2 = str4.Split(new[] { '=' });
  358. if (strArray2.Length == 2)
  359. {
  360. dictionary[strArray2[0]] = strArray2[1];
  361. }
  362. else
  363. {
  364. dictionary[str4] = null;
  365. }
  366. }
  367. }
  368. var builder = new StringBuilder();
  369. foreach (string str5 in dictionary.Keys)
  370. {
  371. if (builder.Length > 0)
  372. {
  373. builder.Append("&");
  374. }
  375. builder.Append(str5);
  376. if (dictionary[str5] != null)
  377. {
  378. builder.Append("=");
  379. builder.Append(dictionary[str5]);
  380. }
  381. }
  382. str = builder.ToString();
  383. }
  384. else
  385. {
  386. str = queryStringModification;
  387. }
  388. }
  389. if (!string.IsNullOrEmpty(anchor))
  390. {
  391. str2 = anchor;
  392. }
  393. return (url + (string.IsNullOrEmpty(str) ? "" : ("?" + str)) + (string.IsNullOrEmpty(str2) ? "" : ("#" + str2))).ToLowerInvariant();
  394. }
  395. /// <summary>
  396. /// Remove query string from url
  397. /// </summary>
  398. /// <param name="url">Url to modify</param>
  399. /// <param name="queryString">Query string to remove</param>
  400. /// <returns>New url</returns>
  401. public virtual string RemoveQueryString(string url, string queryString)
  402. {
  403. if (url == null)
  404. url = string.Empty;
  405. url = url.ToLowerInvariant();
  406. if (queryString == null)
  407. queryString = string.Empty;
  408. queryString = queryString.ToLowerInvariant();
  409. string str = string.Empty;
  410. if (url.Contains("?"))
  411. {
  412. str = url.Substring(url.IndexOf("?") + 1);
  413. url = url.Substring(0, url.IndexOf("?"));
  414. }
  415. if (!string.IsNullOrEmpty(queryString))
  416. {
  417. if (!string.IsNullOrEmpty(str))
  418. {
  419. var dictionary = new Dictionary<string, string>();
  420. foreach (string str3 in str.Split(new[] { '&' }))
  421. {
  422. if (!string.IsNullOrEmpty(str3))
  423. {
  424. string[] strArray = str3.Split(new[] { '=' });
  425. if (strArray.Length == 2)
  426. {
  427. dictionary[strArray[0]] = strArray[1];
  428. }
  429. else
  430. {
  431. dictionary[str3] = null;
  432. }
  433. }
  434. }
  435. dictionary.Remove(queryString);
  436. var builder = new StringBuilder();
  437. foreach (string str5 in dictionary.Keys)
  438. {
  439. if (builder.Length > 0)
  440. {
  441. builder.Append("&");
  442. }
  443. builder.Append(str5);
  444. if (dictionary[str5] != null)
  445. {
  446. builder.Append("=");
  447. builder.Append(dictionary[str5]);
  448. }
  449. }
  450. str = builder.ToString();
  451. }
  452. }
  453. return (url + (string.IsNullOrEmpty(str) ? "" : ("?" + str)));
  454. }
  455. /// <summary>
  456. /// Gets query string value by name
  457. /// </summary>
  458. /// <typeparam name="T"></typeparam>
  459. /// <param name="name">Parameter name</param>
  460. /// <returns>Query string value</returns>
  461. public virtual T QueryString<T>(string name)
  462. {
  463. string queryParam = null;
  464. if (IsRequestAvailable(_httpContext) && _httpContext.Request.QueryString[name] != null)
  465. queryParam = _httpContext.Request.QueryString[name];
  466. if (!String.IsNullOrEmpty(queryParam))
  467. return CommonHelper.To<T>(queryParam);
  468. return default(T);
  469. }
  470. /// <summary>
  471. /// Restart application domain
  472. /// </summary>
  473. /// <param name="makeRedirect">A value indicating whether we should made redirection after restart</param>
  474. /// <param name="redirectUrl">Redirect URL; empty string if you want to redirect to the current page URL</param>
  475. public virtual void RestartAppDomain(bool makeRedirect = false, string redirectUrl = "")
  476. {
  477. if (CommonHelper.GetTrustLevel() > AspNetHostingPermissionLevel.Medium)
  478. {
  479. //full trust
  480. HttpRuntime.UnloadAppDomain();
  481. TryWriteGlobalAsax();
  482. }
  483. else
  484. {
  485. //medium trust
  486. bool success = TryWriteWebConfig();
  487. if (!success)
  488. {
  489. throw new Exception("The application needs to be restarted due to a configuration change, but was unable to do so." + Environment.NewLine +
  490. "To prevent this issue in the future, a change to the web server configuration is required:" + Environment.NewLine +
  491. "- run the application in a full trust environment, or" + Environment.NewLine +
  492. "- give the application write access to the 'web.config' file.");
  493. }
  494. success = TryWriteGlobalAsax();
  495. if (!success)
  496. {
  497. throw new Exception("The application needs to be restarted due to a configuration change, but was unable to do so." + Environment.NewLine +
  498. "To prevent this issue in the future, a change to the web server configuration is required:" + Environment.NewLine +
  499. "- run the application in a full trust environment, or" + Environment.NewLine +
  500. "- give the application write access to the 'Global.asax' file.");
  501. }
  502. }
  503. // If setting up extensions/modules requires an AppDomain restart, it's very unlikely the
  504. // current request can be processed correctly. So, we redirect to the same URL, so that the
  505. // new request will come to the newly started AppDomain.
  506. if (_httpContext != null && makeRedirect)
  507. {
  508. if (String.IsNullOrEmpty(redirectUrl))
  509. redirectUrl = "~/";
  510. _httpContext.Response.Redirect(redirectUrl, true /*endResponse*/);
  511. }
  512. }
  513. /// <summary>
  514. /// Gets a value that indicates whether the client is being redirected to a new location
  515. /// </summary>
  516. public virtual bool IsRequestBeingRedirected
  517. {
  518. get
  519. {
  520. var response = _httpContext.Response;
  521. return response.IsRequestBeingRedirected;
  522. }
  523. }
  524. /// <summary>
  525. /// Gets or sets a value that indicates whether the client is being redirected to a new location using POST
  526. /// </summary>
  527. public virtual bool IsPostBeingDone
  528. {
  529. get
  530. {
  531. if (_httpContext.Items["nop.IsPOSTBeingDone"] == null)
  532. return false;
  533. return Convert.ToBoolean(_httpContext.Items["nop.IsPOSTBeingDone"]);
  534. }
  535. set
  536. {
  537. _httpContext.Items["nop.IsPOSTBeingDone"] = value;
  538. }
  539. }
  540. #endregion
  541. }
  542. }