PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Mindfor.WebMP/Modules/System/Controllers/SystemController.cs

http://webmp.codeplex.com
C# | 216 lines | 149 code | 22 blank | 45 comment | 17 complexity | 5f2afa4173c39e4b3e3ca83e5e4ffb7a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Web.Mvc;
  10. using System.Web.Routing;
  11. using System.Xml.Linq;
  12. using Mindfor.Web.Data;
  13. using Mindfor.Web.Models;
  14. using Mindfor.Web.Routing;
  15. using NHibernate.Linq;
  16. using NHibernate;
  17. namespace Mindfor.Web.Modules.System.Controllers
  18. {
  19. /// <summary>
  20. /// Represents controller for displaying standart text pages from database.
  21. /// </summary>
  22. public class SystemController : CmsController
  23. {
  24. static Dictionary<string, XDocument> s_siteMaps = new Dictionary<string, XDocument>();
  25. /// <summary>
  26. /// Shows standart text page view.
  27. /// Model is PageText.
  28. /// </summary>
  29. /// <returns>ViewResult.</returns>
  30. [UrlRouteSkip]
  31. public ActionResult TextPage(int pageId)
  32. {
  33. // find page text
  34. if (CurrentPageText == null)
  35. return HttpNotFound();
  36. // if url set => redirect
  37. if (!String.IsNullOrEmpty(CurrentPageText.Url))
  38. return Redirect(CurrentPageText.Url);
  39. // show page
  40. ViewData.Model = CurrentPageText;
  41. return View();
  42. }
  43. /// <summary>
  44. /// Returns NotFound action result which renders "NotFound" view.
  45. /// </summary>
  46. [UrlRouteSkip]
  47. public ActionResult NotFound()
  48. {
  49. return HttpNotFound();
  50. }
  51. /// <summary>
  52. /// Restarts application and redirects user to returnUrl.
  53. /// </summary>
  54. /// <param name="returnUrl">Url to redirect user to. If null then user is redirected to main page.</param>
  55. [UrlRoute(Path="Admin")]
  56. [CmsAuthorize(CmsRoles.Administrator)]
  57. [AdminLink("????????????? ????", "?????????")]
  58. public ActionResult Restart(string returnUrl)
  59. {
  60. if (String.IsNullOrEmpty(returnUrl))
  61. {
  62. if (Request.UrlReferrer != null)
  63. returnUrl = Request.UrlReferrer.ToString();
  64. else
  65. returnUrl = "/";
  66. }
  67. CmsApplication.Restart();
  68. return Redirect(returnUrl);
  69. }
  70. [AdminLink]
  71. [CmsAuthorize]
  72. [UrlRoute("", Path="Admin")]
  73. public ActionResult Dashboard()
  74. {
  75. return View();
  76. }
  77. /// <summary>
  78. /// Returns result which renders Routes view.
  79. /// Model is RouteCollection.
  80. /// </summary>
  81. [UrlRoute(Path="Admin")]
  82. [CmsAuthorize(CmsRoles.Administrator)]
  83. [AdminLink("????????? ????????", "?????????")]
  84. public ActionResult Routes(string route)
  85. {
  86. RoutesModel model = new RoutesModel(RouteTable.Routes);
  87. ViewData.Model = model;
  88. if (!String.IsNullOrEmpty(route))
  89. {
  90. // rewrite path to find match routes
  91. if (route[0] != '/')
  92. route = "/" + route;
  93. this.HttpContext.RewritePath(route);
  94. // find match routes
  95. List<RouteData> matches = new List<RouteData>();
  96. foreach (RouteBase r in model.Routes)
  97. {
  98. RouteData rd = r.GetRouteData(this.HttpContext);
  99. if (rd != null)
  100. matches.Add(rd);
  101. }
  102. model.MatchRoutes = matches;
  103. }
  104. return View();
  105. }
  106. /// <summary>
  107. /// Returns xml file that contains site map for request domain.
  108. /// </summary>
  109. [UrlRoute("", Name="SiteMap", Path="sitemap.xml")]
  110. public ActionResult SiteMap()
  111. {
  112. XDocument map;
  113. string siteUrl = SiteUrl.ToString().ToLower();
  114. // load map from memory
  115. if (s_siteMaps.ContainsKey(siteUrl))
  116. map = s_siteMaps[siteUrl];
  117. // generate map
  118. else
  119. {
  120. map = new XDocument();
  121. s_siteMaps[siteUrl] = map;
  122. XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
  123. XNamespace nsXsi = "http://www.w3.org/2001/XMLSchema-instance";
  124. XElement root = new XElement(ns + "urlset",
  125. new XAttribute("xmlns", ns.NamespaceName),
  126. new XAttribute(XNamespace.Xmlns + "xsi", nsXsi.NamespaceName),
  127. new XAttribute(nsXsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd")
  128. );
  129. map.Add(root);
  130. root.Add(SiteMapLocationsForPages(ns));
  131. }
  132. // return xml
  133. return Xml(map);
  134. }
  135. /// <summary>
  136. /// Generates site map locations for required pages. If pages null then generates for root pages.
  137. /// </summary>
  138. /// <param name="pages">Pages to generate locations for.</param>
  139. private IEnumerable<XElement> SiteMapLocationsForPages(XNamespace ns, IEnumerable<Page> pages = null)
  140. {
  141. // load root pages
  142. if (pages == null)
  143. {
  144. pages = Data.Query<Page>().GetRootPages()
  145. .OrderBy(r => r.Sort)
  146. .ToList()
  147. .Where(r => r.HasAccess(null))
  148. .ToList();
  149. }
  150. // add each page
  151. List<XElement> list = new List<XElement>();
  152. foreach (Page page in pages)
  153. {
  154. // add page
  155. Uri url = new Uri(SiteUrl, Url.Page(page.Id));
  156. list.Add(
  157. new XElement(ns + "url",
  158. new XElement(ns + "loc", url))
  159. );
  160. // add children
  161. List<Page> childPages = page.ChildPages
  162. .OrderBy(r => r.Sort)
  163. .ToList()
  164. .Where(r => r.HasAccess(null))
  165. .ToList();
  166. if (childPages.Count > 0)
  167. list.AddRange(SiteMapLocationsForPages(ns, childPages));
  168. }
  169. // return
  170. return list;
  171. }
  172. /// <summary>
  173. /// Returns captcha image by captcha key.
  174. /// </summary>
  175. /// <param name="key">Captcha key.</param>
  176. [UrlRoute("Captcha/{key}", Name="Captcha")]
  177. public FileResult Captcha(string key)
  178. {
  179. // get text
  180. string text = null;
  181. try
  182. {
  183. text = CaptchaManager.DecryptTextFromKey(key);
  184. }
  185. catch { }
  186. if (text == null)
  187. return null;
  188. // generate captcha image
  189. Bitmap img = CaptchaManager.GenerateImage(150, 30, 12, text);
  190. byte[] bytes = img.SaveImage(ImageFormat.Jpeg, 50);
  191. return new FileContentResult(bytes, "image/jpeg");
  192. }
  193. }
  194. }