PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/VirtualReality/aurora-sim
C# | 277 lines | 185 code | 28 blank | 64 comment | 19 complexity | 5f0b8d8c5dca1c05c5d97fffdd9a7904 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 System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using Aurora.Framework;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Services.Interfaces;
  34. namespace Aurora.Services.DataService
  35. {
  36. public class LocalAbuseReportsConnector : IAbuseReportsConnector
  37. {
  38. private IGenericData GD;
  39. private string WebPassword = "";
  40. private string m_abuseReportsTable = "abusereports";
  41. #region IAbuseReportsConnector Members
  42. public void Initialize(IGenericData GenericData, IConfigSource source, IRegistryCore simBase, string defaultConnectionString)
  43. {
  44. GD = GenericData;
  45. if (source.Configs[Name] != null)
  46. defaultConnectionString = source.Configs[Name].GetString("ConnectionString", defaultConnectionString);
  47. GD.ConnectToDatabase(defaultConnectionString, "AbuseReports", source.Configs["AuroraConnectors"].GetBoolean("ValidateTables", true));
  48. DataManager.DataManager.RegisterPlugin(Name + "Local", this);
  49. if (source.Configs["AuroraConnectors"].GetString("AbuseReportsConnector", "LocalConnector") ==
  50. "LocalConnector")
  51. {
  52. WebPassword = Util.Md5Hash(source.Configs["Handlers"].GetString("WebUIHandlerPassword", String.Empty));
  53. //List<string> Results = GD.Query("Method", "abusereports", "passwords", "Password");
  54. //if (Results.Count == 0)
  55. //{
  56. // string newPass = MainConsole.Instance.PasswdPrompt("Password to access Abuse Reports");
  57. // GD.Insert("passwords", new object[] { "abusereports", Util.Md5Hash(Util.Md5Hash(newPass)) });
  58. //}
  59. DataManager.DataManager.RegisterPlugin(this);
  60. }
  61. }
  62. public string Name
  63. {
  64. get { return "IAbuseReportsConnector"; }
  65. }
  66. /// <summary>
  67. /// Gets the abuse report associated with the number and uses the pass to authenticate.
  68. /// </summary>
  69. /// <param name = "Number"></param>
  70. /// <param name = "Password"></param>
  71. /// <returns></returns>
  72. public AbuseReport GetAbuseReport(int Number, string Password)
  73. {
  74. return (!CheckPassword(Password)) ? null :GetAbuseReport(Number);
  75. }
  76. /// <summary>
  77. /// Gets the abuse report associated with the number without authentication
  78. /// </summary>
  79. /// <param name="Number"></param>
  80. /// <returns></returns>
  81. public AbuseReport GetAbuseReport(int Number)
  82. {
  83. QueryFilter filter = new QueryFilter();
  84. filter.andFilters["Number"] = Number;
  85. List<string> Reports = GD.Query(new string[] { "*" }, m_abuseReportsTable, filter, null, null, null);
  86. return (Reports.Count == 0) ? null : new AbuseReport
  87. {
  88. Category = Reports[0],
  89. ReporterName = Reports[1],
  90. ObjectName = Reports[2],
  91. ObjectUUID = new UUID(Reports[3]),
  92. AbuserName = Reports[4],
  93. AbuseLocation = Reports[5],
  94. AbuseDetails = Reports[6],
  95. ObjectPosition = Reports[7],
  96. RegionName = Reports[8],
  97. ScreenshotID = new UUID(Reports[9]),
  98. AbuseSummary = Reports[10],
  99. Number = int.Parse(Reports[11]),
  100. AssignedTo = Reports[12],
  101. Active = int.Parse(Reports[13]) == 1,
  102. Checked = int.Parse(Reports[14]) == 1,
  103. Notes = Reports[15]
  104. };
  105. }
  106. public List<AbuseReport> GetAbuseReports(int start, int count, bool active)
  107. {
  108. List<AbuseReport> rv = new List<AbuseReport>();
  109. QueryFilter filter = new QueryFilter();
  110. filter.andGreaterThanEqFilters["CAST(number AS UNSIGNED)"] = start;
  111. filter.andFilters["Active"] = active ? 1 : 0;
  112. List<string> query = GD.Query(new string[1] { "*" }, m_abuseReportsTable, filter, null, null, null);
  113. if (query.Count % 16 != 0)
  114. {
  115. return rv;
  116. }
  117. try
  118. {
  119. for(int i=0;i<query.Count;i+=16)
  120. {
  121. AbuseReport report = new AbuseReport
  122. {
  123. Category = query[i + 0],
  124. ReporterName = query[i + 1],
  125. ObjectName = query[i + 2],
  126. ObjectUUID = new UUID(query[i + 3]),
  127. AbuserName = query[i + 4],
  128. AbuseLocation = query[i + 5],
  129. AbuseDetails = query[i + 6],
  130. ObjectPosition = query[i + 7],
  131. RegionName = query[i + 8],
  132. ScreenshotID = new UUID(query[i + 9]),
  133. AbuseSummary = query[i + 10],
  134. Number = int.Parse(query[i + 11]),
  135. AssignedTo = query[i + 12],
  136. Active = int.Parse(query[i + 13]) == 1,
  137. Checked = int.Parse(query[i + 14]) == 1,
  138. Notes = query[i + 15]
  139. };
  140. rv.Add(report);
  141. }
  142. }
  143. catch
  144. {
  145. }
  146. return rv;
  147. }
  148. /// <summary>
  149. /// Adds a new abuse report to the database
  150. /// </summary>
  151. /// <param name = "report"></param>
  152. /// <param name = "Password"></param>
  153. public void AddAbuseReport(AbuseReport report)
  154. {
  155. List<object> InsertValues = new List<object>{
  156. report.Category.ToString(),
  157. report.ReporterName,
  158. report.ObjectName,
  159. report.ObjectUUID,
  160. report.AbuserName,
  161. report.AbuseLocation,
  162. report.AbuseDetails,
  163. report.ObjectPosition,
  164. report.RegionName,
  165. report.ScreenshotID,
  166. report.AbuseSummary
  167. };
  168. Dictionary<string, bool> sort = new Dictionary<string, bool>(1);
  169. sort["Number"] = false;
  170. //We do not trust the number sent by the region. Always find it ourselves
  171. List<string> values = GD.Query(new string[1] { "Number" }, m_abuseReportsTable, null, sort, null, null);
  172. report.Number = values.Count == 0 ? 0 : int.Parse(values[0]);
  173. report.Number++;
  174. InsertValues.Add(report.Number);
  175. InsertValues.Add(report.AssignedTo);
  176. InsertValues.Add(report.Active ? 1 : 0);
  177. InsertValues.Add(report.Checked ? 1 : 0);
  178. InsertValues.Add(report.Notes);
  179. GD.Insert(m_abuseReportsTable, InsertValues.ToArray());
  180. }
  181. /// <summary>
  182. /// Updates an abuse report and authenticates with the password.
  183. /// </summary>
  184. /// <param name = "report"></param>
  185. /// <param name = "Password"></param>
  186. public void UpdateAbuseReport(AbuseReport report, string Password)
  187. {
  188. if (!CheckPassword(Password))
  189. {
  190. return;
  191. }
  192. UpdateAbuseReport(report);
  193. }
  194. /// <summary>
  195. /// Updates an abuse reprot without authentication
  196. /// </summary>
  197. /// <param name="report"></param>
  198. public void UpdateAbuseReport(AbuseReport report)
  199. {
  200. Dictionary<string, object> row = new Dictionary<string, object>(16);
  201. //This is update, so we trust the number as it should know the number it's updating now.
  202. row["Category"] = report.Category.ToString();
  203. row["ReporterName"] = report.ReporterName;
  204. row["ObjectName"] = report.ObjectName;
  205. row["ObjectUUID"] = report.ObjectUUID;
  206. row["AbuserName"] = report.AbuserName;
  207. row["AbuseLocation"] = report.AbuseLocation;
  208. row["AbuseDetails"] = report.AbuseDetails;
  209. row["ObjectPosition"] = report.ObjectPosition;
  210. row["RegionName"] = report.RegionName;
  211. row["ScreenshotID"] = report.ScreenshotID;
  212. row["AbuseSummary"] = report.AbuseSummary;
  213. row["Number"] = report.Number;
  214. row["AssignedTo"] = report.AssignedTo;
  215. row["Active"] = report.Active ? 1 : 0;
  216. row["Checked"] = report.Checked ? 1 : 0;
  217. row["Notes"] = report.Notes;
  218. GD.Replace(m_abuseReportsTable, row);
  219. }
  220. #endregion
  221. public void Dispose()
  222. {
  223. }
  224. /// <summary>
  225. /// Check the user's password, not currently used
  226. /// </summary>
  227. /// <param name = "Password"></param>
  228. /// <returns></returns>
  229. private bool CheckPassword(string Password)
  230. {
  231. if (Password == WebPassword)
  232. {
  233. return true;
  234. }
  235. string OtherPass = Util.Md5Hash(Password);
  236. if (OtherPass == WebPassword)
  237. {
  238. return true;
  239. }
  240. QueryFilter filter = new QueryFilter();
  241. filter.andFilters["Method"] = "abusereports";
  242. List<string> TruePassword = GD.Query(new string[] { "Password" }, "passwords", filter, null, null, null);
  243. return !(
  244. TruePassword.Count == 0 ||
  245. OtherPass == TruePassword[0]
  246. );
  247. }
  248. }
  249. }