MySQLUserProfilesData.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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.Data;
  29. using System.Reflection;
  30. using OpenSim.Data;
  31. using OpenSim.Framework;
  32. using MySql.Data.MySqlClient;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using log4net;
  36. namespace OpenSim.Data.MySQL
  37. {
  38. public class UserProfilesData: IProfilesData
  39. {
  40. static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. #region Properites
  42. string ConnectionString
  43. {
  44. get; set;
  45. }
  46. protected virtual Assembly Assembly
  47. {
  48. get { return GetType().Assembly; }
  49. }
  50. #endregion Properties
  51. #region class Member Functions
  52. public UserProfilesData(string connectionString)
  53. {
  54. ConnectionString = connectionString;
  55. Init();
  56. }
  57. void Init()
  58. {
  59. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  60. {
  61. dbcon.Open();
  62. Migration m = new Migration(dbcon, Assembly, "UserProfiles");
  63. m.Update();
  64. }
  65. }
  66. #endregion Member Functions
  67. #region Classifieds Queries
  68. /// <summary>
  69. /// Gets the classified records.
  70. /// </summary>
  71. /// <returns>
  72. /// Array of classified records
  73. /// </returns>
  74. /// <param name='creatorId'>
  75. /// Creator identifier.
  76. /// </param>
  77. public OSDArray GetClassifiedRecords(UUID creatorId)
  78. {
  79. OSDArray data = new OSDArray();
  80. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  81. {
  82. string query = "SELECT classifieduuid, name FROM classifieds WHERE creatoruuid = ?Id";
  83. dbcon.Open();
  84. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  85. {
  86. cmd.Parameters.AddWithValue("?Id", creatorId);
  87. using( MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default))
  88. {
  89. if(reader.HasRows)
  90. {
  91. while (reader.Read())
  92. {
  93. OSDMap n = new OSDMap();
  94. UUID Id = UUID.Zero;
  95. string Name = null;
  96. try
  97. {
  98. UUID.TryParse(Convert.ToString( reader["classifieduuid"]), out Id);
  99. Name = Convert.ToString(reader["name"]);
  100. }
  101. catch (Exception e)
  102. {
  103. m_log.ErrorFormat("[PROFILES_DATA]" +
  104. ": UserAccount exception {0}", e.Message);
  105. }
  106. n.Add("classifieduuid", OSD.FromUUID(Id));
  107. n.Add("name", OSD.FromString(Name));
  108. data.Add(n);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. return data;
  115. }
  116. public bool UpdateClassifiedRecord(UserClassifiedAdd ad, ref string result)
  117. {
  118. string query = string.Empty;
  119. query += "INSERT INTO classifieds (";
  120. query += "`classifieduuid`,";
  121. query += "`creatoruuid`,";
  122. query += "`creationdate`,";
  123. query += "`expirationdate`,";
  124. query += "`category`,";
  125. query += "`name`,";
  126. query += "`description`,";
  127. query += "`parceluuid`,";
  128. query += "`parentestate`,";
  129. query += "`snapshotuuid`,";
  130. query += "`simname`,";
  131. query += "`posglobal`,";
  132. query += "`parcelname`,";
  133. query += "`classifiedflags`,";
  134. query += "`priceforlisting`) ";
  135. query += "VALUES (";
  136. query += "?ClassifiedId,";
  137. query += "?CreatorId,";
  138. query += "?CreatedDate,";
  139. query += "?ExpirationDate,";
  140. query += "?Category,";
  141. query += "?Name,";
  142. query += "?Description,";
  143. query += "?ParcelId,";
  144. query += "?ParentEstate,";
  145. query += "?SnapshotId,";
  146. query += "?SimName,";
  147. query += "?GlobalPos,";
  148. query += "?ParcelName,";
  149. query += "?Flags,";
  150. query += "?ListingPrice ) ";
  151. query += "ON DUPLICATE KEY UPDATE ";
  152. query += "category=?Category, ";
  153. query += "expirationdate=?ExpirationDate, ";
  154. query += "name=?Name, ";
  155. query += "description=?Description, ";
  156. query += "parentestate=?ParentEstate, ";
  157. query += "posglobal=?GlobalPos, ";
  158. query += "parcelname=?ParcelName, ";
  159. query += "classifiedflags=?Flags, ";
  160. query += "priceforlisting=?ListingPrice, ";
  161. query += "snapshotuuid=?SnapshotId";
  162. if(string.IsNullOrEmpty(ad.ParcelName))
  163. ad.ParcelName = "Unknown";
  164. if(ad.ParcelId == null)
  165. ad.ParcelId = UUID.Zero;
  166. if(string.IsNullOrEmpty(ad.Description))
  167. ad.Description = "No Description";
  168. DateTime epoch = new DateTime(1970, 1, 1);
  169. DateTime now = DateTime.Now;
  170. TimeSpan epochnow = now - epoch;
  171. TimeSpan duration;
  172. DateTime expiration;
  173. TimeSpan epochexp;
  174. if(ad.Flags == 2)
  175. {
  176. duration = new TimeSpan(7,0,0,0);
  177. expiration = now.Add(duration);
  178. epochexp = expiration - epoch;
  179. }
  180. else
  181. {
  182. duration = new TimeSpan(365,0,0,0);
  183. expiration = now.Add(duration);
  184. epochexp = expiration - epoch;
  185. }
  186. ad.CreationDate = (int)epochnow.TotalSeconds;
  187. ad.ExpirationDate = (int)epochexp.TotalSeconds;
  188. try
  189. {
  190. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  191. {
  192. dbcon.Open();
  193. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  194. {
  195. cmd.Parameters.AddWithValue("?ClassifiedId", ad.ClassifiedId.ToString());
  196. cmd.Parameters.AddWithValue("?CreatorId", ad.CreatorId.ToString());
  197. cmd.Parameters.AddWithValue("?CreatedDate", ad.CreationDate.ToString());
  198. cmd.Parameters.AddWithValue("?ExpirationDate", ad.ExpirationDate.ToString());
  199. cmd.Parameters.AddWithValue("?Category", ad.Category.ToString());
  200. cmd.Parameters.AddWithValue("?Name", ad.Name.ToString());
  201. cmd.Parameters.AddWithValue("?Description", ad.Description.ToString());
  202. cmd.Parameters.AddWithValue("?ParcelId", ad.ParcelId.ToString());
  203. cmd.Parameters.AddWithValue("?ParentEstate", ad.ParentEstate.ToString());
  204. cmd.Parameters.AddWithValue("?SnapshotId", ad.SnapshotId.ToString ());
  205. cmd.Parameters.AddWithValue("?SimName", ad.SimName.ToString());
  206. cmd.Parameters.AddWithValue("?GlobalPos", ad.GlobalPos.ToString());
  207. cmd.Parameters.AddWithValue("?ParcelName", ad.ParcelName.ToString());
  208. cmd.Parameters.AddWithValue("?Flags", ad.Flags.ToString());
  209. cmd.Parameters.AddWithValue("?ListingPrice", ad.Price.ToString ());
  210. cmd.ExecuteNonQuery();
  211. }
  212. }
  213. }
  214. catch (Exception e)
  215. {
  216. m_log.ErrorFormat("[PROFILES_DATA]" +
  217. ": ClassifiedesUpdate exception {0}", e.Message);
  218. result = e.Message;
  219. return false;
  220. }
  221. return true;
  222. }
  223. public bool DeleteClassifiedRecord(UUID recordId)
  224. {
  225. string query = string.Empty;
  226. query += "DELETE FROM classifieds WHERE ";
  227. query += "classifieduuid = ?recordId";
  228. try
  229. {
  230. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  231. {
  232. dbcon.Open();
  233. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  234. {
  235. cmd.Parameters.AddWithValue("?recordId", recordId.ToString());
  236. cmd.ExecuteNonQuery();
  237. }
  238. }
  239. }
  240. catch (Exception e)
  241. {
  242. m_log.ErrorFormat("[PROFILES_DATA]" +
  243. ": DeleteClassifiedRecord exception {0}", e.Message);
  244. return false;
  245. }
  246. return true;
  247. }
  248. public bool GetClassifiedInfo(ref UserClassifiedAdd ad, ref string result)
  249. {
  250. string query = string.Empty;
  251. query += "SELECT * FROM classifieds WHERE ";
  252. query += "classifieduuid = ?AdId";
  253. try
  254. {
  255. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  256. {
  257. dbcon.Open();
  258. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  259. {
  260. cmd.Parameters.AddWithValue("?AdId", ad.ClassifiedId.ToString());
  261. using (MySqlDataReader reader = cmd.ExecuteReader())
  262. {
  263. if(reader.Read ())
  264. {
  265. ad.CreatorId = new UUID(reader.GetGuid("creatoruuid"));
  266. ad.ParcelId = new UUID(reader.GetGuid("parceluuid"));
  267. ad.SnapshotId = new UUID(reader.GetGuid("snapshotuuid"));
  268. ad.CreationDate = Convert.ToInt32(reader["creationdate"]);
  269. ad.ExpirationDate = Convert.ToInt32(reader["expirationdate"]);
  270. ad.ParentEstate = Convert.ToInt32(reader["parentestate"]);
  271. ad.Flags = (byte)reader.GetUInt32("classifiedflags");
  272. ad.Category = reader.GetInt32("category");
  273. ad.Price = reader.GetInt16("priceforlisting");
  274. ad.Name = reader.GetString("name");
  275. ad.Description = reader.GetString("description");
  276. ad.SimName = reader.GetString("simname");
  277. ad.GlobalPos = reader.GetString("posglobal");
  278. ad.ParcelName = reader.GetString("parcelname");
  279. }
  280. }
  281. }
  282. dbcon.Close();
  283. }
  284. }
  285. catch (Exception e)
  286. {
  287. m_log.ErrorFormat("[PROFILES_DATA]" +
  288. ": GetPickInfo exception {0}", e.Message);
  289. }
  290. return true;
  291. }
  292. #endregion Classifieds Queries
  293. #region Picks Queries
  294. public OSDArray GetAvatarPicks(UUID avatarId)
  295. {
  296. string query = string.Empty;
  297. query += "SELECT `pickuuid`,`name` FROM userpicks WHERE ";
  298. query += "creatoruuid = ?Id";
  299. OSDArray data = new OSDArray();
  300. try
  301. {
  302. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  303. {
  304. dbcon.Open();
  305. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  306. {
  307. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  308. using (MySqlDataReader reader = cmd.ExecuteReader())
  309. {
  310. if(reader.HasRows)
  311. {
  312. while (reader.Read())
  313. {
  314. OSDMap record = new OSDMap();
  315. record.Add("pickuuid",OSD.FromString((string)reader["pickuuid"]));
  316. record.Add("name",OSD.FromString((string)reader["name"]));
  317. data.Add(record);
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. catch (Exception e)
  325. {
  326. m_log.ErrorFormat("[PROFILES_DATA]" +
  327. ": GetAvatarPicks exception {0}", e.Message);
  328. }
  329. return data;
  330. }
  331. public UserProfilePick GetPickInfo(UUID avatarId, UUID pickId)
  332. {
  333. string query = string.Empty;
  334. UserProfilePick pick = new UserProfilePick();
  335. query += "SELECT * FROM userpicks WHERE ";
  336. query += "creatoruuid = ?CreatorId AND ";
  337. query += "pickuuid = ?PickId";
  338. try
  339. {
  340. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  341. {
  342. dbcon.Open();
  343. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  344. {
  345. cmd.Parameters.AddWithValue("?CreatorId", avatarId.ToString());
  346. cmd.Parameters.AddWithValue("?PickId", pickId.ToString());
  347. using (MySqlDataReader reader = cmd.ExecuteReader())
  348. {
  349. if(reader.HasRows)
  350. {
  351. reader.Read();
  352. string description = (string)reader["description"];
  353. if (string.IsNullOrEmpty(description))
  354. description = "No description given.";
  355. UUID.TryParse((string)reader["pickuuid"], out pick.PickId);
  356. UUID.TryParse((string)reader["creatoruuid"], out pick.CreatorId);
  357. UUID.TryParse((string)reader["parceluuid"], out pick.ParcelId);
  358. UUID.TryParse((string)reader["snapshotuuid"], out pick.SnapshotId);
  359. pick.GlobalPos = (string)reader["posglobal"];
  360. pick.Gatekeeper = (string)reader["gatekeeper"];
  361. bool.TryParse((string)reader["toppick"], out pick.TopPick);
  362. bool.TryParse((string)reader["enabled"], out pick.Enabled);
  363. pick.Name = (string)reader["name"];
  364. pick.Desc = description;
  365. pick.ParcelName = (string)reader["user"];
  366. pick.OriginalName = (string)reader["originalname"];
  367. pick.SimName = (string)reader["simname"];
  368. pick.SortOrder = (int)reader["sortorder"];
  369. }
  370. }
  371. }
  372. dbcon.Close();
  373. }
  374. }
  375. catch (Exception e)
  376. {
  377. m_log.ErrorFormat("[PROFILES_DATA]" +
  378. ": GetPickInfo exception {0}", e.Message);
  379. }
  380. return pick;
  381. }
  382. public bool UpdatePicksRecord(UserProfilePick pick)
  383. {
  384. string query = string.Empty;
  385. query += "INSERT INTO userpicks VALUES (";
  386. query += "?PickId,";
  387. query += "?CreatorId,";
  388. query += "?TopPick,";
  389. query += "?ParcelId,";
  390. query += "?Name,";
  391. query += "?Desc,";
  392. query += "?SnapshotId,";
  393. query += "?User,";
  394. query += "?Original,";
  395. query += "?SimName,";
  396. query += "?GlobalPos,";
  397. query += "?SortOrder,";
  398. query += "?Enabled,";
  399. query += "?Gatekeeper)";
  400. query += "ON DUPLICATE KEY UPDATE ";
  401. query += "parceluuid=?ParcelId,";
  402. query += "name=?Name,";
  403. query += "description=?Desc,";
  404. query += "user=?User,";
  405. query += "simname=?SimName,";
  406. query += "snapshotuuid=?SnapshotId,";
  407. query += "pickuuid=?PickId,";
  408. query += "posglobal=?GlobalPos,";
  409. query += "gatekeeper=?Gatekeeper";
  410. try
  411. {
  412. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  413. {
  414. dbcon.Open();
  415. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  416. {
  417. cmd.Parameters.AddWithValue("?PickId", pick.PickId.ToString());
  418. cmd.Parameters.AddWithValue("?CreatorId", pick.CreatorId.ToString());
  419. cmd.Parameters.AddWithValue("?TopPick", pick.TopPick.ToString());
  420. cmd.Parameters.AddWithValue("?ParcelId", pick.ParcelId.ToString());
  421. cmd.Parameters.AddWithValue("?Name", pick.Name.ToString());
  422. cmd.Parameters.AddWithValue("?Desc", pick.Desc.ToString());
  423. cmd.Parameters.AddWithValue("?SnapshotId", pick.SnapshotId.ToString());
  424. cmd.Parameters.AddWithValue("?User", pick.ParcelName.ToString());
  425. cmd.Parameters.AddWithValue("?Original", pick.OriginalName.ToString());
  426. cmd.Parameters.AddWithValue("?SimName",pick.SimName.ToString());
  427. cmd.Parameters.AddWithValue("?GlobalPos", pick.GlobalPos);
  428. cmd.Parameters.AddWithValue("?Gatekeeper",pick.Gatekeeper);
  429. cmd.Parameters.AddWithValue("?SortOrder", pick.SortOrder.ToString ());
  430. cmd.Parameters.AddWithValue("?Enabled", pick.Enabled.ToString());
  431. cmd.ExecuteNonQuery();
  432. }
  433. }
  434. }
  435. catch (Exception e)
  436. {
  437. m_log.ErrorFormat("[PROFILES_DATA]" +
  438. ": UpdateAvatarNotes exception {0}", e.Message);
  439. return false;
  440. }
  441. return true;
  442. }
  443. public bool DeletePicksRecord(UUID pickId)
  444. {
  445. string query = string.Empty;
  446. query += "DELETE FROM userpicks WHERE ";
  447. query += "pickuuid = ?PickId";
  448. try
  449. {
  450. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  451. {
  452. dbcon.Open();
  453. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  454. {
  455. cmd.Parameters.AddWithValue("?PickId", pickId.ToString());
  456. cmd.ExecuteNonQuery();
  457. }
  458. }
  459. }
  460. catch (Exception e)
  461. {
  462. m_log.ErrorFormat("[PROFILES_DATA]" +
  463. ": DeleteUserPickRecord exception {0}", e.Message);
  464. return false;
  465. }
  466. return true;
  467. }
  468. #endregion Picks Queries
  469. #region Avatar Notes Queries
  470. public bool GetAvatarNotes(ref UserProfileNotes notes)
  471. { // WIP
  472. string query = string.Empty;
  473. query += "SELECT `notes` FROM usernotes WHERE ";
  474. query += "useruuid = ?Id AND ";
  475. query += "targetuuid = ?TargetId";
  476. OSDArray data = new OSDArray();
  477. try
  478. {
  479. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  480. {
  481. dbcon.Open();
  482. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  483. {
  484. cmd.Parameters.AddWithValue("?Id", notes.UserId.ToString());
  485. cmd.Parameters.AddWithValue("?TargetId", notes.TargetId.ToString());
  486. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  487. {
  488. if(reader.HasRows)
  489. {
  490. reader.Read();
  491. notes.Notes = OSD.FromString((string)reader["notes"]);
  492. }
  493. else
  494. {
  495. notes.Notes = OSD.FromString("");
  496. }
  497. }
  498. }
  499. }
  500. }
  501. catch (Exception e)
  502. {
  503. m_log.ErrorFormat("[PROFILES_DATA]" +
  504. ": GetAvatarNotes exception {0}", e.Message);
  505. }
  506. return true;
  507. }
  508. public bool UpdateAvatarNotes(ref UserProfileNotes note, ref string result)
  509. {
  510. string query = string.Empty;
  511. bool remove;
  512. if(string.IsNullOrEmpty(note.Notes))
  513. {
  514. remove = true;
  515. query += "DELETE FROM usernotes WHERE ";
  516. query += "useruuid=?UserId AND ";
  517. query += "targetuuid=?TargetId";
  518. }
  519. else
  520. {
  521. remove = false;
  522. query += "INSERT INTO usernotes VALUES ( ";
  523. query += "?UserId,";
  524. query += "?TargetId,";
  525. query += "?Notes )";
  526. query += "ON DUPLICATE KEY ";
  527. query += "UPDATE ";
  528. query += "notes=?Notes";
  529. }
  530. try
  531. {
  532. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  533. {
  534. dbcon.Open();
  535. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  536. {
  537. if(!remove)
  538. cmd.Parameters.AddWithValue("?Notes", note.Notes);
  539. cmd.Parameters.AddWithValue("?TargetId", note.TargetId.ToString ());
  540. cmd.Parameters.AddWithValue("?UserId", note.UserId.ToString());
  541. cmd.ExecuteNonQuery();
  542. }
  543. }
  544. }
  545. catch (Exception e)
  546. {
  547. m_log.ErrorFormat("[PROFILES_DATA]" +
  548. ": UpdateAvatarNotes exception {0}", e.Message);
  549. return false;
  550. }
  551. return true;
  552. }
  553. #endregion Avatar Notes Queries
  554. #region Avatar Properties
  555. public bool GetAvatarProperties(ref UserProfileProperties props, ref string result)
  556. {
  557. string query = string.Empty;
  558. query += "SELECT * FROM userprofile WHERE ";
  559. query += "useruuid = ?Id";
  560. try
  561. {
  562. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  563. {
  564. dbcon.Open();
  565. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  566. {
  567. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  568. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  569. {
  570. if(reader.HasRows)
  571. {
  572. reader.Read();
  573. props.WebUrl = (string)reader["profileURL"];
  574. UUID.TryParse((string)reader["profileImage"], out props.ImageId);
  575. props.AboutText = (string)reader["profileAboutText"];
  576. UUID.TryParse((string)reader["profileFirstImage"], out props.FirstLifeImageId);
  577. props.FirstLifeText = (string)reader["profileFirstText"];
  578. UUID.TryParse((string)reader["profilePartner"], out props.PartnerId);
  579. props.WantToMask = (int)reader["profileWantToMask"];
  580. props.WantToText = (string)reader["profileWantToText"];
  581. props.SkillsMask = (int)reader["profileSkillsMask"];
  582. props.SkillsText = (string)reader["profileSkillsText"];
  583. props.Language = (string)reader["profileLanguages"];
  584. }
  585. else
  586. {
  587. props.WebUrl = string.Empty;
  588. props.ImageId = UUID.Zero;
  589. props.AboutText = string.Empty;
  590. props.FirstLifeImageId = UUID.Zero;
  591. props.FirstLifeText = string.Empty;
  592. props.PartnerId = UUID.Zero;
  593. props.WantToMask = 0;
  594. props.WantToText = string.Empty;
  595. props.SkillsMask = 0;
  596. props.SkillsText = string.Empty;
  597. props.Language = string.Empty;
  598. props.PublishProfile = false;
  599. props.PublishMature = false;
  600. query = "INSERT INTO userprofile (";
  601. query += "useruuid, ";
  602. query += "profilePartner, ";
  603. query += "profileAllowPublish, ";
  604. query += "profileMaturePublish, ";
  605. query += "profileURL, ";
  606. query += "profileWantToMask, ";
  607. query += "profileWantToText, ";
  608. query += "profileSkillsMask, ";
  609. query += "profileSkillsText, ";
  610. query += "profileLanguages, ";
  611. query += "profileImage, ";
  612. query += "profileAboutText, ";
  613. query += "profileFirstImage, ";
  614. query += "profileFirstText) VALUES (";
  615. query += "?userId, ";
  616. query += "?profilePartner, ";
  617. query += "?profileAllowPublish, ";
  618. query += "?profileMaturePublish, ";
  619. query += "?profileURL, ";
  620. query += "?profileWantToMask, ";
  621. query += "?profileWantToText, ";
  622. query += "?profileSkillsMask, ";
  623. query += "?profileSkillsText, ";
  624. query += "?profileLanguages, ";
  625. query += "?profileImage, ";
  626. query += "?profileAboutText, ";
  627. query += "?profileFirstImage, ";
  628. query += "?profileFirstText)";
  629. dbcon.Close();
  630. dbcon.Open();
  631. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  632. {
  633. put.Parameters.AddWithValue("?userId", props.UserId.ToString());
  634. put.Parameters.AddWithValue("?profilePartner", props.PartnerId.ToString());
  635. put.Parameters.AddWithValue("?profileAllowPublish", props.PublishProfile);
  636. put.Parameters.AddWithValue("?profileMaturePublish", props.PublishMature);
  637. put.Parameters.AddWithValue("?profileURL", props.WebUrl);
  638. put.Parameters.AddWithValue("?profileWantToMask", props.WantToMask);
  639. put.Parameters.AddWithValue("?profileWantToText", props.WantToText);
  640. put.Parameters.AddWithValue("?profileSkillsMask", props.SkillsMask);
  641. put.Parameters.AddWithValue("?profileSkillsText", props.SkillsText);
  642. put.Parameters.AddWithValue("?profileLanguages", props.Language);
  643. put.Parameters.AddWithValue("?profileImage", props.ImageId.ToString());
  644. put.Parameters.AddWithValue("?profileAboutText", props.AboutText);
  645. put.Parameters.AddWithValue("?profileFirstImage", props.FirstLifeImageId.ToString());
  646. put.Parameters.AddWithValue("?profileFirstText", props.FirstLifeText);
  647. put.ExecuteNonQuery();
  648. }
  649. }
  650. }
  651. }
  652. }
  653. }
  654. catch (Exception e)
  655. {
  656. m_log.ErrorFormat("[PROFILES_DATA]" +
  657. ": Requst properties exception {0}", e.Message);
  658. result = e.Message;
  659. return false;
  660. }
  661. return true;
  662. }
  663. public bool UpdateAvatarProperties(ref UserProfileProperties props, ref string result)
  664. {
  665. string query = string.Empty;
  666. query += "UPDATE userprofile SET ";
  667. query += "profileURL=?profileURL, ";
  668. query += "profileImage=?image, ";
  669. query += "profileAboutText=?abouttext,";
  670. query += "profileFirstImage=?firstlifeimage,";
  671. query += "profileFirstText=?firstlifetext ";
  672. query += "WHERE useruuid=?uuid";
  673. try
  674. {
  675. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  676. {
  677. dbcon.Open();
  678. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  679. {
  680. cmd.Parameters.AddWithValue("?profileURL", props.WebUrl);
  681. cmd.Parameters.AddWithValue("?image", props.ImageId.ToString());
  682. cmd.Parameters.AddWithValue("?abouttext", props.AboutText);
  683. cmd.Parameters.AddWithValue("?firstlifeimage", props.FirstLifeImageId.ToString());
  684. cmd.Parameters.AddWithValue("?firstlifetext", props.FirstLifeText);
  685. cmd.Parameters.AddWithValue("?uuid", props.UserId.ToString());
  686. cmd.ExecuteNonQuery();
  687. }
  688. }
  689. }
  690. catch (Exception e)
  691. {
  692. m_log.ErrorFormat("[PROFILES_DATA]" +
  693. ": AgentPropertiesUpdate exception {0}", e.Message);
  694. return false;
  695. }
  696. return true;
  697. }
  698. #endregion Avatar Properties
  699. #region Avatar Interests
  700. public bool UpdateAvatarInterests(UserProfileProperties up, ref string result)
  701. {
  702. string query = string.Empty;
  703. query += "UPDATE userprofile SET ";
  704. query += "profileWantToMask=?WantMask, ";
  705. query += "profileWantToText=?WantText,";
  706. query += "profileSkillsMask=?SkillsMask,";
  707. query += "profileSkillsText=?SkillsText, ";
  708. query += "profileLanguages=?Languages ";
  709. query += "WHERE useruuid=?uuid";
  710. try
  711. {
  712. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  713. {
  714. dbcon.Open();
  715. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  716. {
  717. cmd.Parameters.AddWithValue("?WantMask", up.WantToMask);
  718. cmd.Parameters.AddWithValue("?WantText", up.WantToText);
  719. cmd.Parameters.AddWithValue("?SkillsMask", up.SkillsMask);
  720. cmd.Parameters.AddWithValue("?SkillsText", up.SkillsText);
  721. cmd.Parameters.AddWithValue("?Languages", up.Language);
  722. cmd.Parameters.AddWithValue("?uuid", up.UserId.ToString());
  723. cmd.ExecuteNonQuery();
  724. }
  725. }
  726. }
  727. catch (Exception e)
  728. {
  729. m_log.ErrorFormat("[PROFILES_DATA]" +
  730. ": AgentInterestsUpdate exception {0}", e.Message);
  731. result = e.Message;
  732. return false;
  733. }
  734. return true;
  735. }
  736. #endregion Avatar Interests
  737. public OSDArray GetUserImageAssets(UUID avatarId)
  738. {
  739. OSDArray data = new OSDArray();
  740. string query = "SELECT `snapshotuuid` FROM {0} WHERE `creatoruuid` = ?Id";
  741. // Get classified image assets
  742. try
  743. {
  744. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  745. {
  746. dbcon.Open();
  747. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`classifieds`"), dbcon))
  748. {
  749. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  750. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  751. {
  752. if(reader.HasRows)
  753. {
  754. while (reader.Read())
  755. {
  756. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  757. }
  758. }
  759. }
  760. }
  761. dbcon.Close();
  762. dbcon.Open();
  763. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  764. {
  765. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  766. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  767. {
  768. if(reader.HasRows)
  769. {
  770. while (reader.Read())
  771. {
  772. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  773. }
  774. }
  775. }
  776. }
  777. dbcon.Close();
  778. dbcon.Open();
  779. query = "SELECT `profileImage`, `profileFirstImage` FROM `userprofile` WHERE `useruuid` = ?Id";
  780. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  781. {
  782. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  783. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  784. {
  785. if(reader.HasRows)
  786. {
  787. while (reader.Read())
  788. {
  789. data.Add(new OSDString((string)reader["profileImage"].ToString ()));
  790. data.Add(new OSDString((string)reader["profileFirstImage"].ToString ()));
  791. }
  792. }
  793. }
  794. }
  795. }
  796. }
  797. catch (Exception e)
  798. {
  799. m_log.ErrorFormat("[PROFILES_DATA]" +
  800. ": GetAvatarNotes exception {0}", e.Message);
  801. }
  802. return data;
  803. }
  804. #region User Preferences
  805. public bool GetUserPreferences(ref UserPreferences pref, ref string result)
  806. {
  807. string query = string.Empty;
  808. query += "SELECT imviaemail,visible,email FROM ";
  809. query += "usersettings WHERE ";
  810. query += "useruuid = ?Id";
  811. OSDArray data = new OSDArray();
  812. try
  813. {
  814. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  815. {
  816. dbcon.Open();
  817. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  818. {
  819. cmd.Parameters.AddWithValue("?Id", pref.UserId.ToString());
  820. using (MySqlDataReader reader = cmd.ExecuteReader())
  821. {
  822. if(reader.HasRows)
  823. {
  824. reader.Read();
  825. bool.TryParse((string)reader["imviaemail"], out pref.IMViaEmail);
  826. bool.TryParse((string)reader["visible"], out pref.Visible);
  827. pref.EMail = (string)reader["email"];
  828. }
  829. else
  830. {
  831. dbcon.Close();
  832. dbcon.Open();
  833. query = "INSERT INTO usersettings VALUES ";
  834. query += "(?uuid,'false','false', ?Email)";
  835. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  836. {
  837. put.Parameters.AddWithValue("?Email", pref.EMail);
  838. put.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  839. put.ExecuteNonQuery();
  840. }
  841. }
  842. }
  843. }
  844. }
  845. }
  846. catch (Exception e)
  847. {
  848. m_log.ErrorFormat("[PROFILES_DATA]" +
  849. ": Get preferences exception {0}", e.Message);
  850. result = e.Message;
  851. return false;
  852. }
  853. return true;
  854. }
  855. public bool UpdateUserPreferences(ref UserPreferences pref, ref string result)
  856. {
  857. string query = string.Empty;
  858. query += "UPDATE usersettings SET ";
  859. query += "imviaemail=?ImViaEmail, ";
  860. query += "visible=?Visible, ";
  861. query += "email=?EMail ";
  862. query += "WHERE useruuid=?uuid";
  863. try
  864. {
  865. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  866. {
  867. dbcon.Open();
  868. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  869. {
  870. cmd.Parameters.AddWithValue("?ImViaEmail", pref.IMViaEmail.ToString().ToLower());
  871. cmd.Parameters.AddWithValue("?Visible", pref.Visible.ToString().ToLower());
  872. cmd.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  873. cmd.Parameters.AddWithValue("?EMail", pref.EMail.ToString().ToLower());
  874. cmd.ExecuteNonQuery();
  875. }
  876. }
  877. }
  878. catch (Exception e)
  879. {
  880. m_log.ErrorFormat("[PROFILES_DATA]" +
  881. ": UserPreferencesUpdate exception {0} {1}", e.Message, e.InnerException);
  882. result = e.Message;
  883. return false;
  884. }
  885. return true;
  886. }
  887. #endregion User Preferences
  888. #region Integration
  889. public bool GetUserAppData(ref UserAppData props, ref string result)
  890. {
  891. string query = string.Empty;
  892. query += "SELECT * FROM `userdata` WHERE ";
  893. query += "UserId = ?Id AND ";
  894. query += "TagId = ?TagId";
  895. try
  896. {
  897. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  898. {
  899. dbcon.Open();
  900. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  901. {
  902. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  903. cmd.Parameters.AddWithValue ("?TagId", props.TagId.ToString());
  904. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  905. {
  906. if(reader.HasRows)
  907. {
  908. reader.Read();
  909. props.DataKey = (string)reader["DataKey"];
  910. props.DataVal = (string)reader["DataVal"];
  911. }
  912. else
  913. {
  914. query += "INSERT INTO userdata VALUES ( ";
  915. query += "?UserId,";
  916. query += "?TagId,";
  917. query += "?DataKey,";
  918. query += "?DataVal) ";
  919. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  920. {
  921. put.Parameters.AddWithValue("?UserId", props.UserId.ToString());
  922. put.Parameters.AddWithValue("?TagId", props.TagId.ToString());
  923. put.Parameters.AddWithValue("?DataKey", props.DataKey.ToString());
  924. put.Parameters.AddWithValue("?DataVal", props.DataVal.ToString());
  925. put.ExecuteNonQuery();
  926. }
  927. }
  928. }
  929. }
  930. }
  931. }
  932. catch (Exception e)
  933. {
  934. m_log.ErrorFormat("[PROFILES_DATA]" +
  935. ": Requst application data exception {0}", e.Message);
  936. result = e.Message;
  937. return false;
  938. }
  939. return true;
  940. }
  941. public bool SetUserAppData(UserAppData props, ref string result)
  942. {
  943. string query = string.Empty;
  944. query += "UPDATE userdata SET ";
  945. query += "TagId = ?TagId, ";
  946. query += "DataKey = ?DataKey, ";
  947. query += "DataVal = ?DataVal WHERE ";
  948. query += "UserId = ?UserId AND ";
  949. query += "TagId = ?TagId";
  950. try
  951. {
  952. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  953. {
  954. dbcon.Open();
  955. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  956. {
  957. cmd.Parameters.AddWithValue("?UserId", props.UserId.ToString());
  958. cmd.Parameters.AddWithValue("?TagId", props.TagId.ToString());
  959. cmd.Parameters.AddWithValue("?DataKey", props.DataKey.ToString());
  960. cmd.Parameters.AddWithValue("?DataVal", props.DataKey.ToString());
  961. cmd.ExecuteNonQuery();
  962. }
  963. }
  964. }
  965. catch (Exception e)
  966. {
  967. m_log.ErrorFormat("[PROFILES_DATA]" +
  968. ": SetUserData exception {0}", e.Message);
  969. return false;
  970. }
  971. return true;
  972. }
  973. #endregion Integration
  974. }
  975. }