PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/web/studio/ASC.Web.Studio/Products/Community/Modules/Forum/UserControls/Common/ForumManager.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer
C# | 272 lines | 208 code | 49 blank | 15 comment | 84 complexity | 525401d153d7db61fa4bb5c1b3e3efb7 MD5 | raw file
  1. /*
  2. *
  3. * (c) Copyright Ascensio System Limited 2010-2021
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. */
  16. using System;
  17. using System.Collections;
  18. using System.Web;
  19. using ASC.Common.Caching;
  20. using ASC.Core;
  21. using ASC.Data.Storage;
  22. using ASC.Forum;
  23. using ASC.Web.Core.Users;
  24. using ASC.Web.Core.Utility.Skins;
  25. using ASC.Web.Studio.Utility;
  26. namespace ASC.Web.UserControls.Forum.Common
  27. {
  28. public class ForumManager
  29. {
  30. private class SecurityActionValidator : ISecurityActionView
  31. {
  32. #region ISecurityActionView Members
  33. public bool IsAccessible { get; set; }
  34. public event EventHandler<SecurityAccessEventArgs> ValidateAccess;
  35. #endregion
  36. public void OnValidate(SecurityAccessEventArgs e)
  37. {
  38. if (ValidateAccess != null)
  39. ValidateAccess(this, e);
  40. }
  41. }
  42. internal static string ForumScriptKey { get { return "__forum_core_script"; } }
  43. internal static string SearchHelperScriptKey { get { return "__searchhelper_core_script"; } }
  44. private static readonly object _syncObj = new object();
  45. private static readonly Hashtable _settingsCollection;
  46. private readonly SecurityActionValidator _securityValidator;
  47. public IPresenterFactory PresenterFactory { get; private set; }
  48. public Settings Settings { get; private set; }
  49. private static readonly ICache CacheAsc = AscCache.Memory;
  50. internal ForumManager(Settings settings)
  51. {
  52. SessionKeys = new ForumSessionKeys(settings.ID);
  53. Settings = settings;
  54. PresenterFactory = new ForumPresenterFactory();
  55. _securityValidator = new SecurityActionValidator();
  56. IPresenter securityPresenter = PresenterFactory.GetPresenter<ISecurityActionView>();
  57. securityPresenter.SetView(_securityValidator);
  58. }
  59. static ForumManager()
  60. {
  61. _settingsCollection = Hashtable.Synchronized(new Hashtable());
  62. }
  63. public static ForumManager GetForumManager(Guid settingsID)
  64. {
  65. lock (_syncObj)
  66. {
  67. if (_settingsCollection.ContainsKey(settingsID))
  68. return (_settingsCollection[settingsID] as Settings).ForumManager;
  69. }
  70. return null;
  71. }
  72. public bool ValidateAccessSecurityAction(ForumAction forumAction, object targetObject)
  73. {
  74. _securityValidator.OnValidate(new SecurityAccessEventArgs(forumAction, targetObject));
  75. return _securityValidator.IsAccessible;
  76. }
  77. public string GetTopicImage(Topic topic)
  78. {
  79. var isNew = topic.IsNew();
  80. if (!topic.Sticky)
  81. {
  82. if (topic.Type == TopicType.Informational && isNew && topic.Closed)
  83. return WebImageSupplier.GetAbsoluteWebPath("top_new_closed.png", Settings.ImageItemID);
  84. else if (topic.Type == TopicType.Informational && isNew && !topic.Closed)
  85. return WebImageSupplier.GetAbsoluteWebPath("top_new.png", Settings.ImageItemID);
  86. else if (topic.Type == TopicType.Informational && !isNew && topic.Closed)
  87. return WebImageSupplier.GetAbsoluteWebPath("top_closed.png", Settings.ImageItemID);
  88. else if (topic.Type == TopicType.Informational && !isNew && !topic.Closed)
  89. return WebImageSupplier.GetAbsoluteWebPath("top.png", Settings.ImageItemID);
  90. else if (topic.Type == TopicType.Poll && isNew && topic.Closed)
  91. return WebImageSupplier.GetAbsoluteWebPath("poll_new_closed.png", Settings.ImageItemID);
  92. else if (topic.Type == TopicType.Poll && isNew && !topic.Closed)
  93. return WebImageSupplier.GetAbsoluteWebPath("poll_new.png", Settings.ImageItemID);
  94. else if (topic.Type == TopicType.Poll && !isNew && topic.Closed)
  95. return WebImageSupplier.GetAbsoluteWebPath("poll_closed.png", Settings.ImageItemID);
  96. else if (topic.Type == TopicType.Poll && !isNew && !topic.Closed)
  97. return WebImageSupplier.GetAbsoluteWebPath("poll.png", Settings.ImageItemID);
  98. }
  99. else
  100. {
  101. if (topic.Type == TopicType.Informational && isNew && topic.Closed)
  102. return WebImageSupplier.GetAbsoluteWebPath("top_new_closed_sticky.png", Settings.ImageItemID);
  103. else if (topic.Type == TopicType.Informational && isNew && !topic.Closed)
  104. return WebImageSupplier.GetAbsoluteWebPath("top_new_sticky.png", Settings.ImageItemID);
  105. else if (topic.Type == TopicType.Informational && !isNew && topic.Closed)
  106. return WebImageSupplier.GetAbsoluteWebPath("top_closed_sticky.png", Settings.ImageItemID);
  107. else if (topic.Type == TopicType.Informational && !isNew && !topic.Closed)
  108. return WebImageSupplier.GetAbsoluteWebPath("top_sticky.png", Settings.ImageItemID);
  109. else if (topic.Type == TopicType.Poll && isNew && topic.Closed)
  110. return WebImageSupplier.GetAbsoluteWebPath("poll_new_closed_sticky.png", Settings.ImageItemID);
  111. else if (topic.Type == TopicType.Poll && isNew && !topic.Closed)
  112. return WebImageSupplier.GetAbsoluteWebPath("poll_new_sticky.png", Settings.ImageItemID);
  113. else if (topic.Type == TopicType.Poll && !isNew && topic.Closed)
  114. return WebImageSupplier.GetAbsoluteWebPath("poll_closed_sticky.png", Settings.ImageItemID);
  115. else if (topic.Type == TopicType.Poll && !isNew && !topic.Closed)
  116. return WebImageSupplier.GetAbsoluteWebPath("poll_sticky.png", Settings.ImageItemID);
  117. }
  118. return "";
  119. }
  120. internal IDataStore GetStore()
  121. {
  122. return StorageFactory.GetStorage(TenantProvider.CurrentTenantID.ToString(), this.Settings.FileStoreModuleID);
  123. }
  124. #region Remove and Find Attachments
  125. public string GetAttachmentVirtualDirPath(Thread thread, Guid settingsID, Guid userID, out string offsetPhysicalPath)
  126. {
  127. offsetPhysicalPath = thread.CategoryID + "\\" + thread.ID + "\\" + userID.ToString();
  128. return thread.CategoryID + "/" + thread.ID + "/" + userID.ToString();
  129. }
  130. public string GetAttachmentWebPath(Attachment attachment)
  131. {
  132. return GetStore().GetUri(attachment.OffsetPhysicalPath).ToString();
  133. }
  134. public void RemoveAttachments(string offsetPhysicalPath)
  135. {
  136. try
  137. {
  138. var store = GetStore();
  139. store.Delete(offsetPhysicalPath);
  140. }
  141. catch { };
  142. }
  143. public void RemoveAttachments(Post post)
  144. {
  145. if (post.Attachments == null)
  146. return;
  147. foreach (var attachment in post.Attachments)
  148. {
  149. RemoveAttachments(attachment.OffsetPhysicalPath);
  150. }
  151. }
  152. public void RemoveAttachments(string[] attachmentPaths)
  153. {
  154. foreach (var offsetPath in attachmentPaths)
  155. {
  156. RemoveAttachments(offsetPath);
  157. }
  158. }
  159. public void RemoveAttachments(Thread thread)
  160. {
  161. string attachmentTopicDir = thread.CategoryID + "\\" + thread.ID;
  162. ClearDirectory(attachmentTopicDir);
  163. }
  164. public void RemoveAttachments(ThreadCategory threadCategory)
  165. {
  166. ClearDirectory(threadCategory.ID.ToString());
  167. }
  168. private void ClearDirectory(string directoryPath)
  169. {
  170. var store = GetStore();
  171. try
  172. {
  173. store.DeleteFiles(directoryPath, "*", true);
  174. }
  175. catch { };
  176. }
  177. #endregion
  178. public string GetHTMLImgUserAvatar(Guid userID)
  179. {
  180. return "<img alt=\"\" class='userPhoto' src=\"" + UserPhotoManager.GetBigPhotoURL(userID) + "\"/>";
  181. }
  182. public string PreviousPage =>
  183. CacheAsc.Get<PageLocation>(this.SessionKeys.PreviousPageLocation) != null
  184. ? CacheAsc.Get<PageLocation>(this.SessionKeys.PreviousPageLocation).Url
  185. : Settings.StartPageAbsolutePath;
  186. public void SetCurrentPage(ForumPage page)
  187. {
  188. var current = new PageLocation(ForumPage.Default, Settings.StartPageAbsolutePath);
  189. if (CacheAsc.Get<PageLocation>(this.SessionKeys.CurrentPageLocation) != null)
  190. current = CacheAsc.Get<PageLocation>(this.SessionKeys.CurrentPageLocation);
  191. var previous = (PageLocation)current.Clone();
  192. if (previous.Page != page)
  193. CacheAsc.Insert(this.SessionKeys.PreviousPageLocation, previous, TimeSpan.FromMinutes(15));
  194. current = new PageLocation(page, HttpContext.Current.Request.GetUrlRewriter().AbsoluteUri);
  195. CacheAsc.Insert(this.SessionKeys.CurrentPageLocation, current, TimeSpan.FromMinutes(15));
  196. }
  197. public ForumSessionKeys SessionKeys { get; private set; }
  198. public class ForumSessionKeys
  199. {
  200. private Guid _settingsID;
  201. public ForumSessionKeys(Guid settingsID)
  202. {
  203. _settingsID = settingsID;
  204. }
  205. public string CurrentPageLocation => "forum_current_page_location" + _settingsID.ToString() + SecurityContext.CurrentAccount.ID.ToString();
  206. public string PreviousPageLocation => "forum_previous_page_location" + _settingsID.ToString() + SecurityContext.CurrentAccount.ID.ToString();
  207. }
  208. }
  209. }