AdminWebFront.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using OpenSim.world;
  6. using OpenSim.UserServer;
  7. using OpenSim.Servers;
  8. using OpenSim.Assets;
  9. using OpenSim.Framework.Inventory;
  10. using libsecondlife;
  11. using OpenSim.RegionServer.world.scripting;
  12. using Avatar=libsecondlife.Avatar;
  13. using OpenSim.RegionServer.world.scripting.Scripts;
  14. namespace OpenSim.CAPS
  15. {
  16. public class AdminWebFront
  17. {
  18. private string AdminPage;
  19. private string NewAccountForm;
  20. private string LoginForm;
  21. private string passWord = "Admin";
  22. private World m_world;
  23. private LoginServer _userServer;
  24. private InventoryCache _inventoryCache;
  25. public AdminWebFront(string password, World world, InventoryCache inventoryCache, LoginServer userserver)
  26. {
  27. _inventoryCache = inventoryCache;
  28. _userServer = userserver;
  29. m_world = world;
  30. passWord = password;
  31. LoadAdminPage();
  32. }
  33. public void LoadMethods( BaseHttpServer server )
  34. {
  35. server.AddRestHandler("GET", "/Admin", GetAdminPage);
  36. server.AddRestHandler("GET", "/Admin/Welcome", GetWelcomePage);
  37. server.AddRestHandler("GET", "/Admin/Accounts", GetAccountsPage );
  38. server.AddRestHandler("GET", "/Admin/Clients", GetConnectedClientsPage);
  39. server.AddRestHandler("GET", "/Admin/Entities", GetEntitiesPage);
  40. server.AddRestHandler("GET", "/Admin/Scripts", GetScriptsPage);
  41. server.AddRestHandler("GET", "/Admin/AddTestScript", AddTestScript );
  42. server.AddRestHandler("GET", "/ClientInventory", GetClientsInventory);
  43. server.AddRestHandler("POST", "/Admin/NewAccount", PostNewAccount );
  44. server.AddRestHandler("POST", "/Admin/Login", PostLogin );
  45. }
  46. private string GetWelcomePage(string request, string path, string param)
  47. {
  48. string responseString;
  49. responseString = "Welcome to the OpenSim Admin Page";
  50. responseString += "<br><br><br> " + LoginForm;
  51. return responseString;
  52. }
  53. private string PostLogin(string requestBody, string path, string param)
  54. {
  55. string responseString;
  56. // Console.WriteLine(requestBody);
  57. if (requestBody == passWord)
  58. {
  59. responseString = "<p> Login Successful </p>";
  60. }
  61. else
  62. {
  63. responseString = "<p> Password Error </p>";
  64. responseString += "<p> Please Login with the correct password </p>";
  65. responseString += "<br><br> " + LoginForm;
  66. }
  67. return responseString;
  68. }
  69. private string PostNewAccount(string requestBody, string path, string param)
  70. {
  71. string responseString;
  72. string firstName = "";
  73. string secondName = "";
  74. string userPasswd = "";
  75. string[] comp;
  76. string[] passw;
  77. string[] line;
  78. string delimStr = "&";
  79. char[] delimiter = delimStr.ToCharArray();
  80. string delimStr2 = "=";
  81. char[] delimiter2 = delimStr2.ToCharArray();
  82. //Console.WriteLine(requestBody);
  83. comp = requestBody.Split(delimiter);
  84. passw = comp[3].Split(delimiter2);
  85. if (passw[1] == passWord) // check admin password is correct
  86. {
  87. line = comp[0].Split(delimiter2); //split firstname
  88. if (line.Length > 1)
  89. {
  90. firstName = line[1];
  91. }
  92. line = comp[1].Split(delimiter2); //split secondname
  93. if (line.Length > 1)
  94. {
  95. secondName = line[1];
  96. }
  97. line = comp[2].Split(delimiter2); //split user password
  98. if (line.Length > 1)
  99. {
  100. userPasswd = line[1];
  101. }
  102. if (this._userServer != null)
  103. {
  104. this._userServer.CreateUserAccount(firstName, secondName, userPasswd);
  105. }
  106. responseString = "<p> New Account created </p>";
  107. }
  108. else
  109. {
  110. responseString = "<p> Admin password is incorrect, please login with the correct password</p>";
  111. responseString += "<br><br>" + LoginForm;
  112. }
  113. return responseString;
  114. }
  115. private string GetConnectedClientsPage(string request, string path, string param)
  116. {
  117. string responseString;
  118. responseString = " <p> Listing connected Clients </p>";
  119. OpenSim.world.Avatar TempAv;
  120. foreach (libsecondlife.LLUUID UUID in m_world.Entities.Keys)
  121. {
  122. if (m_world.Entities[UUID].ToString() == "OpenSim.world.Avatar")
  123. {
  124. TempAv = (OpenSim.world.Avatar)m_world.Entities[UUID];
  125. responseString += "<p> Client: ";
  126. responseString += TempAv.firstname + " , " + TempAv.lastname + " , <A HREF=\"javascript:loadXMLDoc('ClientInventory/" + UUID.ToString() + "')\">" + UUID + "</A> , " + TempAv.ControllingClient.SessionID + " , " + TempAv.ControllingClient.CircuitCode + " , " + TempAv.ControllingClient.userEP.ToString();
  127. responseString += "</p>";
  128. }
  129. }
  130. return responseString;
  131. }
  132. private string AddTestScript(string request, string path, string param)
  133. {
  134. int index = path.LastIndexOf('/');
  135. string lluidStr = path.Substring(index+1);
  136. LLUUID id;
  137. if( LLUUID.TryParse( lluidStr, out id ) )
  138. {
  139. // This is just here for concept purposes... Remove!
  140. m_world.AddScript( m_world.Entities[id], new FollowRandomAvatar());
  141. return String.Format("Added new script to object [{0}]", id);
  142. }
  143. else
  144. {
  145. return String.Format("Couldn't parse [{0}]", lluidStr );
  146. }
  147. }
  148. private string GetScriptsPage(string request, string path, string param)
  149. {
  150. return String.Empty;
  151. }
  152. private string GetEntitiesPage(string request, string path, string param)
  153. {
  154. string responseString;
  155. responseString = " <p> Listing current entities</p><ul>";
  156. foreach (Entity entity in m_world.Entities.Values)
  157. {
  158. string testScriptLink = "javascript:loadXMLDoc('Admin/AddTestScript/" + entity.uuid.ToString() + "');";
  159. responseString += String.Format( "<li>[{0}] \"{1}\" @ {2} <a href=\"{3}\">add test script</a></li>", entity.uuid, entity.Name, entity.Pos, testScriptLink );
  160. }
  161. responseString += "</ul>";
  162. return responseString;
  163. }
  164. private string GetClientsInventory(string request, string path, string param)
  165. {
  166. string[] line;
  167. string delimStr = "/";
  168. char[] delimiter = delimStr.ToCharArray();
  169. string responseString;
  170. responseString = " <p> Listing Inventory </p>";
  171. line = path.Split(delimiter);
  172. if (line.Length > 2)
  173. {
  174. if (line[1] == "ClientInventory")
  175. {
  176. AgentInventory inven = this._inventoryCache.GetAgentsInventory(new libsecondlife.LLUUID(line[2]));
  177. responseString += " <p> Client: " + inven.AgentID.ToStringHyphenated() +" </p>";
  178. if (inven != null)
  179. {
  180. foreach (InventoryItem item in inven.InventoryItems.Values)
  181. {
  182. responseString += "<p> InventoryItem: ";
  183. responseString += item.Name +" , "+ item.ItemID +" , "+ item.Type +" , "+ item.FolderID +" , "+ item.AssetID +" , "+ item.Description ;
  184. responseString += "</p>";
  185. }
  186. }
  187. }
  188. }
  189. return responseString;
  190. }
  191. private string GetCachedAssets(string request, string path, string param)
  192. {
  193. return "";
  194. }
  195. private string GetAccountsPage(string request, string path, string param)
  196. {
  197. string responseString;
  198. responseString = "<p> Account management </p>";
  199. responseString += "<br> ";
  200. responseString += "<p> Create New Account </p>";
  201. responseString += NewAccountForm;
  202. return responseString;
  203. }
  204. private string GetAdminPage(string request, string path, string param)
  205. {
  206. return AdminPage;
  207. }
  208. private void LoadAdminPage()
  209. {
  210. try
  211. {
  212. StreamReader SR;
  213. SR = File.OpenText("testadmin.htm");
  214. AdminPage = SR.ReadToEnd();
  215. SR.Close();
  216. SR = File.OpenText("newaccountform.htm");
  217. NewAccountForm = SR.ReadToEnd();
  218. SR.Close();
  219. SR = File.OpenText("login.htm");
  220. LoginForm = SR.ReadToEnd();
  221. SR.Close();
  222. }
  223. catch (Exception e)
  224. {
  225. Console.WriteLine(e.ToString());
  226. }
  227. }
  228. }
  229. }