PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Aurora/Services/DataService/Connectors/Local/LocalAbuseReportsConnector.cs

https://bitbucket.org/VirtualReality/software-testing
C# | 284 lines | 193 code | 28 blank | 63 comment | 21 complexity | 3320875c346bd36c92967f769c73abea MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Aurora-Sim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using Aurora.Framework;
  28. using Aurora.Framework.DatabaseInterfaces;
  29. using Aurora.Framework.Modules;
  30. using Aurora.Framework.Services;
  31. using Aurora.Framework.Utilities;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using System;
  35. using System.Collections.Generic;
  36. namespace Aurora.Services.DataService
  37. {
  38. public class LocalAbuseReportsConnector : IAbuseReportsConnector
  39. {
  40. private IGenericData GD;
  41. private string WebPassword = "";
  42. private string m_abuseReportsTable = "abusereports";
  43. #region IAbuseReportsConnector Members
  44. public void Initialize(IGenericData GenericData, IConfigSource source, IRegistryCore simBase,
  45. string defaultConnectionString)
  46. {
  47. GD = GenericData;
  48. if (source.Configs[Name] != null)
  49. defaultConnectionString = source.Configs[Name].GetString("ConnectionString", defaultConnectionString);
  50. if (GD != null)
  51. GD.ConnectToDatabase(defaultConnectionString, "AbuseReports",
  52. source.Configs["AuroraConnectors"].GetBoolean("ValidateTables", true));
  53. Framework.Utilities.DataManager.RegisterPlugin(Name + "Local", this);
  54. if (source.Configs["AuroraConnectors"].GetString("AbuseReportsConnector", "LocalConnector") ==
  55. "LocalConnector")
  56. {
  57. WebPassword = Util.Md5Hash(source.Configs["Handlers"].GetString("WebUIHandlerPassword", String.Empty));
  58. //List<string> Results = GD.Query("Method", "abusereports", "passwords", "Password");
  59. //if (Results.Count == 0)
  60. //{
  61. // string newPass = MainConsole.Instance.PasswdPrompt("Password to access Abuse Reports");
  62. // GD.Insert("passwords", new object[] { "abusereports", Util.Md5Hash(Util.Md5Hash(newPass)) });
  63. //}
  64. Framework.Utilities.DataManager.RegisterPlugin(this);
  65. }
  66. }
  67. public string Name
  68. {
  69. get { return "IAbuseReportsConnector"; }
  70. }
  71. /// <summary>
  72. /// Gets the abuse report associated with the number and uses the pass to authenticate.
  73. /// </summary>
  74. /// <param name="Number"></param>
  75. /// <param name="Password"></param>
  76. /// <returns></returns>
  77. public AbuseReport GetAbuseReport(int Number, string Password)
  78. {
  79. return (!CheckPassword(Password)) ? null : GetAbuseReport(Number);
  80. }
  81. /// <summary>
  82. /// Gets the abuse report associated with the number without authentication
  83. /// </summary>
  84. /// <param name="Number"></param>
  85. /// <returns></returns>
  86. public AbuseReport GetAbuseReport(int Number)
  87. {
  88. QueryFilter filter = new QueryFilter();
  89. filter.andFilters["Number"] = Number;
  90. List<string> Reports = GD.Query(new string[] {"*"}, m_abuseReportsTable, filter, null, null, null);
  91. return (Reports.Count == 0)
  92. ? null
  93. : new AbuseReport
  94. {
  95. Category = Reports[0],
  96. ReporterName = Reports[1],
  97. ObjectName = Reports[2],
  98. ObjectUUID = new UUID(Reports[3]),
  99. AbuserName = Reports[4],
  100. AbuseLocation = Reports[5],
  101. AbuseDetails = Reports[6],
  102. ObjectPosition = Reports[7],
  103. RegionName = Reports[8],
  104. ScreenshotID = new UUID(Reports[9]),
  105. AbuseSummary = Reports[10],
  106. Number = int.Parse(Reports[11]),
  107. AssignedTo = Reports[12],
  108. Active = int.Parse(Reports[13]) == 1,
  109. Checked = int.Parse(Reports[14]) == 1,
  110. Notes = Reports[15]
  111. };
  112. }
  113. public List<AbuseReport> GetAbuseReports(int start, int count, bool active)
  114. {
  115. List<AbuseReport> rv = new List<AbuseReport>();
  116. QueryFilter filter = new QueryFilter();
  117. filter.andGreaterThanEqFilters["CAST(number AS UNSIGNED)"] = start;
  118. filter.andFilters["Active"] = active ? 1 : 0;
  119. List<string> query = GD.Query(new string[1] {"*"}, m_abuseReportsTable, filter, null, null, null);
  120. if (query.Count%16 != 0)
  121. {
  122. return rv;
  123. }
  124. try
  125. {
  126. for (int i = 0; i < query.Count; i += 16)
  127. {
  128. AbuseReport report = new AbuseReport
  129. {
  130. Category = query[i + 0],
  131. ReporterName = query[i + 1],
  132. ObjectName = query[i + 2],
  133. ObjectUUID = new UUID(query[i + 3]),
  134. AbuserName = query[i + 4],
  135. AbuseLocation = query[i + 5],
  136. AbuseDetails = query[i + 6],
  137. ObjectPosition = query[i + 7],
  138. RegionName = query[i + 8],
  139. ScreenshotID = new UUID(query[i + 9]),
  140. AbuseSummary = query[i + 10],
  141. Number = int.Parse(query[i + 11]),
  142. AssignedTo = query[i + 12],
  143. Active = int.Parse(query[i + 13]) == 1,
  144. Checked = int.Parse(query[i + 14]) == 1,
  145. Notes = query[i + 15]
  146. };
  147. rv.Add(report);
  148. }
  149. }
  150. catch
  151. {
  152. }
  153. return rv;
  154. }
  155. /// <summary>
  156. /// Adds a new abuse report to the database
  157. /// </summary>
  158. /// <param name="report"></param>
  159. public void AddAbuseReport(AbuseReport report)
  160. {
  161. List<object> InsertValues = new List<object>
  162. {
  163. report.Category.ToString(),
  164. report.ReporterName,
  165. report.ObjectName,
  166. report.ObjectUUID,
  167. report.AbuserName,
  168. report.AbuseLocation,
  169. report.AbuseDetails,
  170. report.ObjectPosition,
  171. report.RegionName,
  172. report.ScreenshotID,
  173. report.AbuseSummary
  174. };
  175. Dictionary<string, bool> sort = new Dictionary<string, bool>(1);
  176. sort["Number"] = false;
  177. //We do not trust the number sent by the region. Always find it ourselves
  178. List<string> values = GD.Query(new string[1] {"Number"}, m_abuseReportsTable, null, sort, null, null);
  179. report.Number = values.Count == 0 ? 0 : int.Parse(values[0]);
  180. report.Number++;
  181. InsertValues.Add(report.Number);
  182. InsertValues.Add(report.AssignedTo);
  183. InsertValues.Add(report.Active ? 1 : 0);
  184. InsertValues.Add(report.Checked ? 1 : 0);
  185. InsertValues.Add(report.Notes);
  186. GD.Insert(m_abuseReportsTable, InsertValues.ToArray());
  187. }
  188. /// <summary>
  189. /// Updates an abuse report and authenticates with the password.
  190. /// </summary>
  191. /// <param name="report"></param>
  192. /// <param name="Password"></param>
  193. public void UpdateAbuseReport(AbuseReport report, string Password)
  194. {
  195. if (!CheckPassword(Password))
  196. {
  197. return;
  198. }
  199. UpdateAbuseReport(report);
  200. }
  201. /// <summary>
  202. /// Updates an abuse reprot without authentication
  203. /// </summary>
  204. /// <param name="report"></param>
  205. public void UpdateAbuseReport(AbuseReport report)
  206. {
  207. Dictionary<string, object> row = new Dictionary<string, object>(16);
  208. //This is update, so we trust the number as it should know the number it's updating now.
  209. row["Category"] = report.Category.ToString();
  210. row["ReporterName"] = report.ReporterName;
  211. row["ObjectName"] = report.ObjectName;
  212. row["ObjectUUID"] = report.ObjectUUID;
  213. row["AbuserName"] = report.AbuserName;
  214. row["AbuseLocation"] = report.AbuseLocation;
  215. row["AbuseDetails"] = report.AbuseDetails;
  216. row["ObjectPosition"] = report.ObjectPosition;
  217. row["RegionName"] = report.RegionName;
  218. row["ScreenshotID"] = report.ScreenshotID;
  219. row["AbuseSummary"] = report.AbuseSummary;
  220. row["Number"] = report.Number;
  221. row["AssignedTo"] = report.AssignedTo;
  222. row["Active"] = report.Active ? 1 : 0;
  223. row["Checked"] = report.Checked ? 1 : 0;
  224. row["Notes"] = report.Notes;
  225. GD.Replace(m_abuseReportsTable, row);
  226. }
  227. #endregion
  228. public void Dispose()
  229. {
  230. }
  231. /// <summary>
  232. /// Check the user's password, not currently used
  233. /// </summary>
  234. /// <param name="Password"></param>
  235. /// <returns></returns>
  236. private bool CheckPassword(string Password)
  237. {
  238. if (Password == WebPassword)
  239. {
  240. return true;
  241. }
  242. string OtherPass = Util.Md5Hash(Password);
  243. if (OtherPass == WebPassword)
  244. {
  245. return true;
  246. }
  247. QueryFilter filter = new QueryFilter();
  248. filter.andFilters["Method"] = "abusereports";
  249. List<string> TruePassword = GD.Query(new string[] {"Password"}, "passwords", filter, null, null, null);
  250. return !(
  251. TruePassword.Count == 0 ||
  252. OtherPass == TruePassword[0]
  253. );
  254. }
  255. }
  256. }