index.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. <?php
  2. // MTSM - Minetest Server Manager - Server management frontend
  3. // (C) Chris Dorman, 2021
  4. // License: CC-BY-SA version 3.0
  5. // http://notabug.org/Pentium44/MTSM
  6. session_start();
  7. include "config.php";
  8. include "functions.php";
  9. // check if flatfile database location is populated
  10. if(!file_exists("mtm_db"))
  11. {
  12. mkdir("mtm_db", 0777);
  13. generateToken();
  14. }
  15. if(!file_exists("mtm_db/events"))
  16. {
  17. mkdir("mtm_db/events", 0777);
  18. }
  19. if(!file_exists("mtm_db/worlds"))
  20. {
  21. mkdir("mtm_db/worlds", 0777);
  22. }
  23. if(!file_exists("mtm_db/pids"))
  24. {
  25. mkdir("mtm_db/pids", 0777);
  26. }
  27. if(!file_exists("mtm_db/users"))
  28. {
  29. mkdir("mtm_db/users", 0777);
  30. }
  31. // Adding a usermods directory
  32. if(!file_exists("mtm_db/usermods"))
  33. {
  34. mkdir("mtm_db/usermods");
  35. }
  36. if(!file_exists("mtm_db/users/usercount"))
  37. {
  38. file_put_contents("mtm_db/users/usercount", "0");
  39. }
  40. $username = $_SESSION['mtm-user'];
  41. ?>
  42. <!DOCTYPE html>
  43. <html lang="en-us">
  44. <head>
  45. <title><?php echo $mtsmtitle; ?></title>
  46. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  47. <meta name="viewport" content="width=device-width, initial-scale=.55, shrink-to-fit=yes"><meta name="description" content="<?php echo htmlentities($mtsmtitle) . " - " . $desc; ?>">
  48. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  49. <link rel="stylesheet" type="text/css" href="ctn/style.css">
  50. </head>
  51. <body>
  52. <div class="maincontain">
  53. <div id="navcontainer">
  54. <div id="navbar"><!--
  55. <?php if(isset($_SESSION['mtm-user']) && isset($_SESSION['mtm-pass'])) {
  56. if($username == "admin") {?>
  57. --><a href="index.php">Manage</a><!--
  58. --><a href="?do=about">About</a><!--
  59. --><a href="?do=logout">Logout</a><!--
  60. <?php } else { ?>
  61. --><a href="index.php">Create</a><!--
  62. --><a href="?do=manage">Manage</a><!--
  63. --><a href="?do=about">About</a><!--
  64. --><a href="?do=logout">Logout</a><!--
  65. <?php }
  66. } else { ?>
  67. --><a href="?forms=login">Login</a><!--
  68. --><a href="?forms=register">Register</a><!--
  69. --><a href="?do=about">About</a><!--
  70. <?php } ?>
  71. --></div>
  72. </div>
  73. <div class='title'><?php echo $mtsmtitle; ?></div>
  74. <div class='contain'>
  75. <?php
  76. if(isset($_GET['forms']))
  77. {
  78. $forms = $_GET['forms'];
  79. $id = $_GET['pid'];
  80. if($forms=="register") {
  81. registerForm();
  82. }
  83. else if($forms=="login") {
  84. loginForm();
  85. }
  86. else { echo "ERROR: Unknown form-name<br>"; }
  87. }
  88. else if(isset($_GET['notify']))
  89. {
  90. $notify = $_GET['notify'];
  91. if($notify=="1") { echo "Error: User not found"; }
  92. else if($notify=="2") { echo "Error: Incorrect password provided"; }
  93. else if($notify=="3") { echo "Error: Please fill out all the text boxes"; }
  94. else if($notify=="4") { echo "Error: The provided passwords did not match"; }
  95. else if($notify=="5") { echo "Error: Special characters cannot be used in your username, and admin may not be used."; }
  96. else if($notify=="6") { echo "Error: This username is already in use"; }
  97. else { echo "Error: unknown error... this is quite unusual..."; }
  98. }
  99. else if(isset($_GET['do']))
  100. {
  101. $do = $_GET['do'];
  102. // Server admin can just delete ssb_db
  103. /*if($do=="clean")
  104. {
  105. if($_POST['password']!="" && $_POST['password']==$pw)
  106. {
  107. $db_content = glob("ssb_db/" . '*', GLOB_MARK);
  108. foreach($db_content as $file)
  109. {
  110. unlink($file);
  111. }
  112. rmdir("mtm_db");
  113. echo "Database Cleaned<br>";
  114. }
  115. else
  116. {
  117. echo "ERROR: Wrong Password<br>";
  118. }
  119. }*/
  120. // grab session values and send friend request functions.
  121. if($do=="create-multicraft") {
  122. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  123. include("mtm_db/users/$username.php");
  124. if(!is_dir("mtm_db/worlds/$userid")) {
  125. mkdir("mtm_db/worlds/$userid");
  126. // Check if usermods folder exists, create if not!
  127. if(!is_dir("mtm_db/usermods/$username")) {
  128. mkdir("mtm_db/usermods/$username");
  129. }
  130. // Lets setup a default configuration script
  131. $config_contents = "port = 30" . $userid . "\n";
  132. $config_contents .= "name = " . $username . "\n";
  133. $config_contents .= "max_users = $max_slot_count\n";
  134. $config_contents .= "default_game = minetest_game\n";
  135. $config_contents .= "default_privs = shout, interact\n";
  136. $config_contents .= "secure.trusted_mods = irc\n";
  137. $config_contents .= "server_address = $defaultip\n";
  138. $config_contents .= "irc.server = $default_irc_server\n";
  139. $config_contents .= "irc.port = $default_irc_port\n";
  140. $config_contents .= "irc.nick = MT" . $portrange . $userid . "\n";
  141. $config_contents .= "irc.channel = #$username\n";
  142. file_put_contents("mtm_db/worlds/$userid/minetest.conf", $config_contents);
  143. // Toggle server off, to the web panel
  144. file_put_contents("mtm_db/users/$userid.pid.statuscode", "false");
  145. file_put_contents("mtm_db/users/$userid.pid.status", "<div style='display:inline;color:#ff0000;'>Stopped</div>\n");
  146. // Trigger event to start server
  147. file_put_contents("mtm_db/events/$userid", "tar -xzf servers/multicraft.tgz -C mtm_db/worlds/$userid");
  148. file_put_contents("mtm_db/users/$userid.type", "multicraft");
  149. header("Location: index.php?do=manage");
  150. } else {
  151. echo "Error: World exists, please click &quot;Manage&quot; to start / stop your server, download a world backup, or delete your current world!";
  152. }
  153. }
  154. }
  155. // Create private server for testing or private party reasons.
  156. if($do=="create-minetest") {
  157. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  158. include("mtm_db/users/$username.php");
  159. if(!is_dir("mtm_db/worlds/$userid")) {
  160. mkdir("mtm_db/worlds/$userid");
  161. // Check if usermods folder exists, create if not!
  162. if(!is_dir("mtm_db/usermods/$username")) {
  163. mkdir("mtm_db/usermods/$username");
  164. }
  165. // Lets setup a default configuration script
  166. $config_contents = "port = " . $portrange . $userid . "\n";
  167. $config_contents .= "name = " . $username . "\n";
  168. $config_contents .= "max_users = $max_slot_count\n";
  169. $config_contents .= "default_game = minetest_game\n";
  170. $config_contents .= "default_privs = shout, interact\n";
  171. $config_contents .= "secure.trusted_mods = irc\n";
  172. $config_contents .= "server_address = $defaultip\n";
  173. $config_contents .= "irc.server = $default_irc_server\n";
  174. $config_contents .= "irc.port = $default_irc_port\n";
  175. $config_contents .= "irc.nick = MT$portrange$userid\n";
  176. $config_contents .= "irc.channel = #$username\n";
  177. file_put_contents("mtm_db/worlds/$userid/minetest.conf", $config_contents);
  178. // Toggle server off, to the web panel
  179. file_put_contents("mtm_db/users/$userid.pid.statuscode", "false");
  180. file_put_contents("mtm_db/users/$userid.pid.status", "<div style='display:inline;color:#ff0000;'>Stopped</div>\n");
  181. // Trigger event to start servver
  182. file_put_contents("mtm_db/events/$userid", "tar -xzf servers/minetest-current.tgz -C mtm_db/worlds/$userid");
  183. file_put_contents("mtm_db/users/$userid.type", "minetest");
  184. header("Location: index.php?do=manage");
  185. } else {
  186. echo "Error: World exists, please click &quot;Manage&quot; to start / stop your server, download a world backup, or delete your current world!";
  187. }
  188. }
  189. }
  190. if($do=="configure-game") {
  191. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  192. include("mtm_db/users/$username.php");
  193. if(is_dir("mtm_db/worlds/$userid")) {
  194. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  195. echo "Pending: Server is pending creation, this process shouldn't take longer than 30 seconds...";
  196. echo "<meta http-equiv='refresh' content='3,url=index.php?do=configure-mods' />";
  197. } else {
  198. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.statuscode");
  199. if($serverstatus == "false") {
  200. if(isset($_GET['game']) && $_GET['game']!="" && is_dir("games/" . stripslashes($_GET['game']))) {
  201. $gamename = htmlentities(stripslashes($_GET['game']));
  202. file_put_contents("mtm_db/events/$userid", "./server set-gamemode $userid $gamename");
  203. header("Location: index.php?do=manage");
  204. } else {
  205. echo "ERROR: " . $_GET['game'] . " not found as a game<br />";
  206. }
  207. } else {
  208. echo "Shut down server before game configuration...";
  209. }
  210. }
  211. } else {
  212. echo "ERROR: Server not found!";
  213. }
  214. }
  215. }
  216. if($do=="configure-server")
  217. {
  218. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  219. include("mtm_db/users/$username.php");
  220. if(is_dir("mtm_db/worlds/$userid")) {
  221. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  222. echo "Pending: Server is pending creation, this process shouldn't take longer than 30 seconds...";
  223. echo "<meta http-equiv='refresh' content='3,url=index.php?do=configure-mods' />";
  224. } else {
  225. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.statuscode");
  226. if($serverstatus == "false") {
  227. // Check if each variable is set and not empty, set in configuration file if so
  228. // server_name
  229. if(isset($_POST['servername']) && $_POST['servername']!="") {
  230. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "server_name = " . $_POST['servername'] . "\n", FILE_APPEND);
  231. }
  232. // server_description
  233. if(isset($_POST['serverdesc']) && $_POST['serverdesc']!="") {
  234. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "server_description = " . $_POST['serverdesc'] . "\n", FILE_APPEND);
  235. }
  236. // server_url
  237. if(isset($_POST['servername']) && $_POST['serverurl']!="") {
  238. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "server_url = " . $_POST['serverurl'] . "\n", FILE_APPEND);
  239. }
  240. // motd
  241. if(isset($_POST['motd']) && $_POST['motd']!="") {
  242. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "motd = " . $_POST['motd'] . "\n", FILE_APPEND);
  243. }
  244. // static_spawnpoint
  245. if(isset($_POST['staticspawnpoint']) && $_POST['staticspawnpoint']!="") {
  246. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "static_spawnpoint = " . $_POST['staticspawnpoint'] . "\n", FILE_APPEND);
  247. }
  248. // enable_pvp = true
  249. if(isset($_POST['enablepvp'])) {
  250. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "enable_pvp = true\n", FILE_APPEND);
  251. }
  252. // enable_pvp = true
  253. if(isset($_POST['creative'])) {
  254. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "creative_mode = true\n", FILE_APPEND);
  255. }
  256. // enable_damage = true
  257. if(isset($_POST['enabledamage'])) {
  258. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "enable_damage = true\n", FILE_APPEND);
  259. } else {
  260. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "enable_damage = false\n", FILE_APPEND);
  261. }
  262. // server_announce = true
  263. if(isset($_POST['serverannounce'])) {
  264. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "server_announce = true\n", FILE_APPEND);
  265. }
  266. // player_transfer_distance = "value"
  267. if(isset($_POST['playertransferdistance']) && $_POST['playertransferdistance']!="") {
  268. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "player_transfer_distance = " . $_POST['playertransferdistance'] . "\n", FILE_APPEND);
  269. }
  270. // default_privs
  271. if(isset($_POST['defaultprivs']) && $_POST['defaultprivs']!="") {
  272. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "default_privs = " . $_POST['defaultprivs'] . "\n", FILE_APPEND);
  273. }
  274. // mapgen settings
  275. if(isset($_POST['mgname']) && $_POST['mgname']!="") {
  276. file_put_contents("mtm_db/worlds/$userid/minetest.conf", "mg_name = " . $_POST['mgname'] . "\n", FILE_APPEND);
  277. }
  278. header("Location: index.php?do=manage");
  279. } else {
  280. echo "Shut down server before configuration...";
  281. }
  282. }
  283. } else {
  284. echo "ERROR: Server not found!";
  285. }
  286. }
  287. }
  288. if($do=="configure-mods")
  289. {
  290. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  291. include("mtm_db/users/$username.php");
  292. if(is_dir("mtm_db/worlds/$userid")) {
  293. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  294. echo "Pending: Server is pending creation, this process shouldn't take longer than 30 seconds...";
  295. echo "<meta http-equiv='refresh' content='3' />";
  296. } else {
  297. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.statuscode");
  298. if($serverstatus == "false") {
  299. // Let them know this might take a while
  300. echo "Please wait, this might take a few seconds depending on the amount of mods being enabled...";
  301. // Check to see which mods where selected, and go from there!
  302. chdir("mods");
  303. foreach(glob("*") as $modname) {
  304. // server_announce = true
  305. if(isset($_POST[$modname]) && !is_dir("../mtm_db/worlds/$userid/worlds/world/worldmods/$modname")) {
  306. echo "Copying $modname...";
  307. chdir("..");
  308. file_put_contents("mtm_db/events/$userid", "cp -a mods/$modname mtm_db/worlds/$userid/worlds/world/worldmods/$modname");
  309. chdir("mods");
  310. sleep(1);
  311. }
  312. }
  313. chdir("..");
  314. header("Location: index.php?do=manage");
  315. } else {
  316. echo "Shut down server before configuration...";
  317. }
  318. }
  319. } else {
  320. echo "ERROR: Server not found!";
  321. }
  322. }
  323. }
  324. if($do=="configure-user-mods")
  325. {
  326. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  327. include("mtm_db/users/$username.php");
  328. if(is_dir("mtm_db/worlds/$userid")) {
  329. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  330. echo "Pending: Server is pending creation, this process shouldn't take longer than 30 seconds...";
  331. echo "<meta http-equiv='refresh' content='3' />";
  332. } else {
  333. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.statuscode");
  334. if($serverstatus == "false") {
  335. // Let them know this might take a while
  336. echo "Please wait, this might take a few seconds depending on the amount of mods being enabled...";
  337. // Check to see which mods where selected, and go from there!
  338. chdir("mtm_db/usermods/$username");
  339. foreach(glob("*") as $modname) {
  340. // server_announce = true
  341. if(isset($_POST[$modname])) {
  342. echo "Copying $modname...";
  343. chdir("../..");
  344. file_put_contents("events/$userid", "cp -a mtm_db/usermods/$username/$modname mtm_db/worlds/$userid/worlds/world/worldmods/$modname");
  345. chdir("usermods/$username");
  346. sleep(2);
  347. }
  348. }
  349. chdir("../../../");
  350. header("Location: index.php?do=manage");
  351. } else {
  352. echo "Shut down server before configuration...";
  353. }
  354. }
  355. } else {
  356. echo "ERROR: Server not found!";
  357. }
  358. }
  359. }
  360. if($do=="start") {
  361. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  362. include("mtm_db/users/$username.php");
  363. if(is_dir("mtm_db/worlds/$userid")) {
  364. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  365. echo "Pending: Server is pending creation, this process shouldn't take longer than 30 seconds...";
  366. echo "<meta http-equiv='refresh' content='3' />";
  367. } else {
  368. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.statuscode");
  369. if($serverstatus == "false" || !file_exists("mtm_db/users/$userid.pid")) {
  370. $servertype = file_get_contents("mtm_db/users/$userid.type");
  371. if($servertype == "multicraft") {
  372. file_put_contents("mtm_db/events/$userid", "./server start-multicraft $userid");
  373. } else if($servertype == "minetest") {
  374. file_put_contents("mtm_db/events/$userid", "./server start-minetest $userid");
  375. }
  376. header("Location: index.php?do=manage");
  377. } else {
  378. echo "Server already running...";
  379. }
  380. }
  381. } else {
  382. echo "ERROR: Server not found!";
  383. }
  384. }
  385. }
  386. if($do=="stop") {
  387. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  388. include("mtm_db/users/$username.php");
  389. if(is_dir("mtm_db/worlds/$userid")) {
  390. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  391. echo "Pending: Server is pending creation, this process shouldn't take longer than 30 seconds...";
  392. echo "<meta http-equiv='refresh' content='3' />";
  393. } else {
  394. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.statuscode");
  395. if($serverstatus == "true") {
  396. file_put_contents("mtm_db/events/$userid", "./server stop $userid");
  397. header("Location: index.php?do=manage");
  398. } else {
  399. echo "Server already stopped...";
  400. }
  401. }
  402. } else {
  403. echo "ERROR: Server not found!";
  404. }
  405. }
  406. }
  407. if($do=="delete") {
  408. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  409. include("mtm_db/users/$username.php");
  410. if(is_dir("mtm_db/worlds/$userid")) {
  411. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.statuscode");
  412. if($serverstatus == "true") {
  413. file_put_contents("mtm_db/events/$userid", "./server del $userid");
  414. sleep(2);
  415. header("Location: index.php");
  416. } else {
  417. file_put_contents("mtm_db/events/$userid", "./server del $userid");
  418. sleep(2);
  419. header("Location: index.php");
  420. }
  421. } else {
  422. echo "ERROR: Server not found!";
  423. }
  424. }
  425. }
  426. if($do=="info-debug") {
  427. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  428. include("mtm_db/users/$username.php");
  429. if(is_dir("mtm_db/worlds/$userid")) {
  430. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  431. echo "Please wait: Server is being created, this process shouldn't take longer than 30 seconds...";
  432. // Refresh the page every 3 seconds for updates on the VM status.
  433. echo "<meta http-equiv='refresh' content='3' />";
  434. } else {
  435. echo $username . "'s Server<br /> Server status: ";
  436. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.status");
  437. echo $serverstatus;
  438. echo "<br /><a href='index.php?do=reset-config' class='button'>Reset minetest.conf</a> &nbsp;";
  439. echo " <a href='index.php?do=del-logfile' class='button'>Reset logfile</a>\n";
  440. echo "<h3>Server information</h3>";
  441. echo "<h4>minetest.conf</h4>";
  442. $userserverconfig = file_get_contents("mtm_db/worlds/$userid/minetest.conf");
  443. echo "<code><pre>$userserverconfig</pre></code>";
  444. echo "<h4>server log</h4>";
  445. $userserverdebug = file_get_contents("mtm_db/users/$userid.slog");
  446. echo "<code><pre>$userserverdebug</pre></code>";
  447. }
  448. } else {
  449. echo "ERROR: Server not found!";
  450. }
  451. }
  452. }
  453. if($do=="manage") {
  454. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  455. include("mtm_db/users/$username.php");
  456. if(is_dir("mtm_db/worlds/$userid")) {
  457. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  458. echo "Please wait: Server is being created, this process shouldn't take longer than 30 seconds...";
  459. // Refresh the page every 3 seconds for updates on the VM status.
  460. echo "<meta http-equiv='refresh' content='1' />";
  461. } else {
  462. // Check if usermods folder exists, create if not!
  463. // For users that created their accounts before update.
  464. if(!is_dir("mtm_db/usermods/$username")) {
  465. mkdir("mtm_db/usermods/$username");
  466. }
  467. echo $username . "'s Server<br /> Server status: ";
  468. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.status");
  469. echo $serverstatus;
  470. echo "<br /><br /><a href='index.php?do=start' class='button'>Start</a>&nbsp;";
  471. echo "<a href='index.php?do=stop' class='button'>Stop</a>&nbsp;<a href='index.php?do=delete' class='button'>Delete</a><br />";
  472. echo "<a href='index.php?do=settings' class='button'>Server Settings</a>&nbsp;<a href='index.php?do=mods' class='button'>Mod Settings</a>&nbsp;";
  473. echo "<a href='index.php?do=info-debug' class='button'>Server Information</a>";
  474. echo "<br /><br />";
  475. echo "<b>Connection information:</b><br />";
  476. echo "<table><tr><td>IP/Port:</td><td>" . $defaultip . "/" . $portrange . $userid . "</td></tr>";
  477. echo "<tr><td>Admin: </td><td>$username</td></tr>";
  478. echo "</table>\n";
  479. if(is_dir("mtm_db/worlds/$userid/worlds/world/worldmods")) {
  480. echo "<h4>mods enabled</h4>";
  481. echo "<ol>";
  482. chdir("mtm_db/worlds/$userid/worlds/world/worldmods");
  483. foreach(glob("*") as $mods) {
  484. echo "<li>$mods</li>";
  485. }
  486. echo "</ol>";
  487. }
  488. echo "<meta http-equiv='refresh' content='5' />";
  489. }
  490. } else {
  491. echo "ERROR: Server not found!";
  492. }
  493. }
  494. }
  495. if($do=="settings")
  496. {
  497. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  498. include("mtm_db/users/$username.php");
  499. if(is_dir("mtm_db/worlds/$userid")) {
  500. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  501. echo "Please wait: Server is being created, this process shouldn't take longer than 30 seconds...";
  502. // Refresh the page every 5 seconds for updates on the VM status.
  503. echo "<meta http-equiv='refresh' content='5' />";
  504. } else {
  505. echo $username . "'s Server<br /> Server status: ";
  506. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.status");
  507. echo $serverstatus;
  508. echo "<br />";
  509. echo "<h3>Configure minetest.conf</h3>";
  510. settingsForm();
  511. }
  512. } else {
  513. echo "ERROR: Server not found!";
  514. }
  515. }
  516. }
  517. if($do=="upload-modzip")
  518. {
  519. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  520. include("mtm_db/users/$username.php");
  521. if(is_dir("mtm_db/worlds/$userid")) {
  522. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  523. echo "Please wait: Server is being created, this process shouldn't take longer than 30 seconds...";
  524. // Refresh the page every 5 seconds for updates on the VM status.
  525. echo "<meta http-equiv='refresh' content='3' />";
  526. } else {
  527. // Lets checkout what's being uploaded
  528. if(isset($_POST['upload-btn'])) {
  529. $filename = basename($_FILES['file']['name']);
  530. $filetype = $_FILES['file']['type'];
  531. $filesize = $_FILES['file']['size'];
  532. if($filesize > $max_modzip_size) {
  533. echo "Error on mod upload, file too big!";
  534. } else if (strtolower(pathinfo($filename,PATHINFO_EXTENSION))!=$allowed_file_extension) {
  535. echo "Error on mod upload, ZIP archives only!";
  536. } else if (($_FILES["file"]["type"] == "application/zip")
  537. || ($_FILES["file"]["type"] == "application/octet-stream")) {
  538. // Looking good!
  539. if($_FILES['file']['error'] > 0) {
  540. echo "Unknown error code: " . $_FILES['file']['error'];
  541. } else {
  542. move_uploaded_file($_FILES['file']['tmp_name'], "mtm_db/usermods/$username/tmpmod.zip");
  543. file_put_contents("mtm_db/events/$userid", "unzip mtm_db/usermods/$username/tmpmod.zip -d mtm_db/usermods/$username/");
  544. sleep(4);
  545. // Delete the mod zip after extraction
  546. unlink("mtm_db/usermods/$username/tmpmod.zip");
  547. header("Location: index.php?do=mods");
  548. }
  549. } else {
  550. echo "Error on mod upload, archive mimetype was not of a ZIP archive.";
  551. }
  552. } else {
  553. echo "Upload button causing some issues?";
  554. }
  555. }
  556. } else {
  557. echo "ERROR: Server not found!";
  558. }
  559. }
  560. }
  561. if($do=="mods")
  562. {
  563. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  564. include("mtm_db/users/$username.php");
  565. if(is_dir("mtm_db/worlds/$userid")) {
  566. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  567. echo "Please wait: Server is being created, this process shouldn't take longer than 30 seconds...";
  568. // Refresh the page every 5 seconds for updates on the VM status.
  569. echo "<meta http-equiv='refresh' content='3' />";
  570. } else {
  571. echo $username . "'s Server<br /> Server status: ";
  572. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.status");
  573. echo $serverstatus;
  574. echo "<br /><a href='index.php?do=reset-mods' class='button'>Reset mods</a>\n";
  575. echo "<h3>Change default game</h3>";
  576. chdir("games");
  577. foreach(glob("*") as $gamemode) {
  578. echo "<a href='index.php?do=configure-game&game=$gamemode' class='button'>$gamemode</a> ";
  579. }
  580. chdir("..");
  581. echo "<h3>Enable mods from local repository</h3>";
  582. modsForm();
  583. // Disable user uploads of zip files when given to general public.
  584. if($reg_token_required == "true") {
  585. echo "<h3>Enable user available mods</h3>";
  586. // In case someone fucks there shit up
  587. echo "<a href='index.php?do=reset-user-mods' class='button'>Delete user mods</a><br />";
  588. userModsForm($username);
  589. echo "<h3>Personal mod upload</h3>";
  590. modUploadForm();
  591. }
  592. }
  593. } else {
  594. echo "ERROR: Server not found!";
  595. }
  596. }
  597. }
  598. if($do=="reset-mods")
  599. {
  600. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  601. include("mtm_db/users/$username.php");
  602. if(is_dir("mtm_db/worlds/$userid")) {
  603. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  604. echo "Please wait: Server is being created, this process shouldn't take longer than 30 seconds...";
  605. // Refresh the page every 5 seconds for updates on the VM status.
  606. echo "<meta http-equiv='refresh' content='3' />";
  607. } else {
  608. if(is_dir("mtm_db/worlds/$userid/worlds/world/worldmods")) {
  609. $serverstatus = file_get_contents("mtm_db/users/$userid.pid.statuscode");
  610. if($serverstatus == "false") {
  611. file_put_contents("mtm_db/events/$userid", "rm -r mtm_db/worlds/$userid/worlds/world/worldmods/*");
  612. sleep(1);
  613. header("Location: index.php?do=manage");
  614. } else {
  615. echo "Error: please shut down your server before deleting world mods!";
  616. }
  617. } else {
  618. echo "Error: mods directory for your world doesn't exit!";
  619. }
  620. }
  621. } else {
  622. echo "ERROR: Server not found!";
  623. }
  624. }
  625. }
  626. if($do=="reset-user-mods")
  627. {
  628. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  629. include("mtm_db/users/$username.php");
  630. if(is_dir("mtm_db/worlds/$userid")) {
  631. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  632. echo "Please wait: Server is being created, this process shouldn't take longer than 30 seconds...";
  633. // Refresh the page every 5 seconds for updates on the VM status.
  634. echo "<meta http-equiv='refresh' content='3' />";
  635. } else {
  636. if(is_dir("mtm_db/usermods/$username")) {
  637. file_put_contents("mtm_db/events/$userid", "rm -r mtm_db/usermods/$username/*");
  638. sleep(2);
  639. header("Location: index.php?do=manage");
  640. }
  641. }
  642. } else {
  643. echo "ERROR: Server not found!";
  644. }
  645. }
  646. }
  647. if($do=="reset-config")
  648. {
  649. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  650. include("mtm_db/users/$username.php");
  651. if(is_dir("mtm_db/worlds/$userid")) {
  652. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  653. echo "Please wait: Server is being created, this process shouldn't take longer than 30 seconds... If you haven't created a server yet, do so before trying to reset a configuration file.";
  654. // Refresh the page every 5 seconds for updates on the VM status.
  655. echo "<meta http-equiv='refresh' content='5' />";
  656. } else {
  657. // Lets setup a default configuration script
  658. $config_contents = "port = 30" . $userid . "\n";
  659. $config_contents .= "name = " . $username . "\n";
  660. $config_contents .= "max_users = $max_slot_count\n";
  661. $config_contents .= "default_game = minetest_game\n";
  662. $config_contents .= "default_privs = shout, interact\n";
  663. $config_contents .= "secure.trusted_mods = irc\n";
  664. $config_contents .= "server_address = $defaultip\n";
  665. $config_contents .= "irc.server = $default_irc_server\n";
  666. $config_contents .= "irc.port = $default_irc_port\n";
  667. $config_contents .= "irc.nick = MT" . $portrange . $userid . "\n";
  668. $config_contents .= "irc.channel = #$username\n";
  669. file_put_contents("mtm_db/worlds/$userid/minetest.conf", $config_contents);
  670. header("Location: index.php?do=manage");
  671. }
  672. } else {
  673. }
  674. }
  675. }
  676. if($do=="del-logfile")
  677. {
  678. if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass'])) { loginForm(); } else {
  679. include("mtm_db/users/$username.php");
  680. if(is_dir("mtm_db/worlds/$userid")) {
  681. if(!is_dir("mtm_db/worlds/$userid/bin")) {
  682. echo "Server doesn't exist... unable to remove debug.txt";
  683. // Refresh the page every 5 seconds for updates on the VM status.
  684. echo "<meta http-equiv='refresh' content='3' />";
  685. } else {
  686. // Lets setup a default configuration script
  687. if(!file_exists("mtm_db/users/$userid.slog")) {
  688. echo "ERROR: debug.txt doesn't exist...";
  689. } else {
  690. unlink("mtm_db/users/$userid.slog");
  691. header("Location: index.php?do=manage");
  692. }
  693. }
  694. } else {
  695. echo "ERROR: Somethings not right, user directory not found!";
  696. }
  697. }
  698. }
  699. if($do=="about")
  700. {
  701. echo "<h2>About</h2>";
  702. echo "<p>" . $desc;
  703. $guestabout = file_get_contents("text/about.txt");
  704. echo $guestabout;
  705. echo "<br />If you enjoy our services, please consider donating!";
  706. }
  707. if($do=="login")
  708. {
  709. $username = stripslashes(htmlentities($_POST['username']));
  710. if($username == "admin") {
  711. if($admin_passwd == stripslashes(htmlentities($_POST['password']))) {
  712. $pass = sha1(md5($admin_passwd));
  713. $user = $username;
  714. $_SESSION['mtm-user'] = $user;
  715. $_SESSION['mtm-pass'] = $pass;
  716. header("Location: index.php");
  717. } else {
  718. echo "Wrong administrator password!";
  719. }
  720. } else if(file_exists("mtm_db/users/$username.php")) {
  721. include_once("mtm_db/users/$username.php");
  722. if($user_password == sha1(md5($_POST['password']))) {
  723. $pass = $user_password;
  724. $user = $username;
  725. $_SESSION['mtm-user'] = $user;
  726. $_SESSION['mtm-pass'] = $pass;
  727. header("Location: index.php");
  728. } else {
  729. echo "Wrong password!";
  730. }
  731. } else {
  732. echo "User $username not found!";
  733. }
  734. }
  735. if($do=="admin-start-all") {
  736. if($username=="admin" && $_SESSION['mtm-pass']==sha1(md5($admin_passwd))) {
  737. // To be continued!
  738. chdir("mtm_db/users");
  739. echo "Starting servers...";
  740. foreach(glob("*.php") as $user_account_file) {
  741. include_once($user_account_file);
  742. $servertype = file_get_contents("$userid.type");
  743. $serverstatus = file_get_contents("$userid.pid.status");
  744. $statuscode = file_get_contents("$userid.pid.statuscode");
  745. if(!file_exists("$userid.pid.statuscode") || $statuscode == "false") {
  746. chdir("..");
  747. if($servertype == "multicraft" && is_dir("worlds/$userid/bin")) {
  748. file_put_contents("events/$userid", "./server start-multicraft $userid");
  749. sleep(1);
  750. echo "Starting multicraft world $userid...<br />";
  751. } else if($servertype == "minetest" && is_dir("worlds/$userid/bin")) {
  752. file_put_contents("events/$userid", "./server start-minetest $userid");
  753. sleep(1);
  754. echo "Starting minetest world $userid...<br />";
  755. } else {
  756. echo "World not created for $userid...<br />";
  757. }
  758. chdir("users");
  759. }
  760. }
  761. }
  762. }
  763. if($do=="admin-stop-all") {
  764. if($username=="admin" && $_SESSION['mtm-pass']==sha1(md5($admin_passwd))) {
  765. echo "Stopping servers...";
  766. if(!isset($userid)) { $userid = "1"; }
  767. file_put_contents("mtm_db/events/$userid", "./server stopall");
  768. }
  769. }
  770. if($do=="logout")
  771. {
  772. $_SESSION['mtm-user'] = null;
  773. $_SESSION['mtm-pass'] = null;
  774. header("Location: index.php?forms=login");
  775. }
  776. if($do=="register")
  777. {
  778. if($_POST['username']!="" && $_POST['password']!="" && $_POST['password-again']!="" && $_POST['fullname']!="" && isset($_POST['email']) && $_POST['email']!="") {
  779. if($_POST['password']==$_POST['password-again']) {
  780. if(!preg_match('/[^a-z0-9]/i', $_POST['username']) && $_POST['username']!="admin") {
  781. if(!file_exists("mtm_db/users/" . $_POST['username'] . ".php")) {
  782. $servercount = file_get_contents("mtm_db/users/usercount");
  783. if($servercount < $maxservers)
  784. {
  785. if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  786. include_once("mtm_db/token.php");
  787. // Enable registration tokens via config.php now!
  788. if($reg_token_required == "true") {
  789. if(stripslashes(htmlentities($_POST['codeword'])) == $reg_token) {
  790. $servercount = $servercount + 1;
  791. if ($servercount < 10) {
  792. $usergenid = "00" . $servercount;
  793. } else if ($servercount < 100) {
  794. $usergenid = "0" . $servercount;
  795. } else {
  796. $usergenid = $servercount;
  797. }
  798. generateToken();
  799. file_put_contents("mtm_db/users/usercount", $servercount);
  800. file_put_contents("mtm_db/users/" . stripslashes(htmlentities($_POST['username'])) . ".php", "<?php\n\$user_handle = \"" . stripslashes(htmlentities($_POST['username'])) . "\";\n\$user_password = \"" . sha1(md5($_POST['password'])) . "\";\n \$user_email = \"" . stripslashes(htmlentities($_POST['email'])) . "\"; \$user_fullname = \"" . stripslashes(htmlentities($_POST['fullname'])) . "\"; \$userid = \"" . $usergenid . "\";\n?>");
  801. header("Location: index.php");
  802. }
  803. else
  804. {
  805. echo "ERROR: Registration code incorrect, please try again!";
  806. }
  807. } else { // Else we aren't using registration codes! FREE SERVERS
  808. $servercount = $servercount + 1;
  809. if ($servercount < 10) {
  810. $usergenid = "00" . $servercount;
  811. } else if ($servercount < 100) {
  812. $usergenid = "0" . $servercount;
  813. } else {
  814. $usergenid = $servercount;
  815. }
  816. file_put_contents("mtm_db/users/usercount", $servercount);
  817. file_put_contents("mtm_db/users/" . stripslashes(htmlentities($_POST['username'])) . ".php", "<?php\n\$user_handle = \"" . stripslashes(htmlentities($_POST['username'])) . "\";\n\$user_password = \"" . sha1(md5($_POST['password'])) . "\";\n \$user_email = \"" . stripslashes(htmlentities($_POST['email'])) . "\"; \$user_fullname = \"" . stripslashes(htmlentities($_POST['fullname'])) . "\"; \$userid = \"" . $usergenid . "\";\n?>");
  818. header("Location: index.php");
  819. } // End registration tokens via config.php
  820. }
  821. else
  822. {
  823. echo "ERROR: Email is invalid!";
  824. }
  825. }
  826. else
  827. {
  828. echo "ERROR: Server cap reached!";
  829. }
  830. } else {
  831. header("Location: index.php?notify=6");
  832. }
  833. } else {
  834. header("Location: index.php?notify=5");
  835. }
  836. } else {
  837. header("Location: index.php?notify=4");
  838. }
  839. } else {
  840. header("Location: index.php?notify=3");
  841. }
  842. }
  843. }
  844. else if (!isset($_SESSION['mtm-user']) || !isset($_SESSION['mtm-pass']))
  845. {
  846. header("Location: index.php?do=about");
  847. }
  848. else
  849. {
  850. if($username!="admin") {
  851. include("mtm_db/users/$username.php");
  852. if(!is_dir("mtm_db/worlds/$userid"))
  853. {
  854. echo "<h3>Minetest server creation</h3>";
  855. echo "<p>You will have the ability to create a server, and will have a consistent uptime based on server uptime, and automated server management!";
  856. echo "There's mods and gamepacks available to users and can be toggled on and off! Over time, more will be populated into the local repository.</p>";
  857. // Get home page for non-users based on users wants
  858. $useraboutmulticraft = file_get_contents("text/multicraft.txt");
  859. $useraboutminetest = file_get_contents("text/minetest.txt");
  860. echo "<p>";
  861. echo $useraboutmulticraft . "<br />";
  862. echo "<div style='display:block;width:100%;text-align:center;'><a href='index.php?do=create-multicraft' class='button'>Go with MultiCraft</a></div>";
  863. echo "</p>";
  864. echo "<p>";
  865. echo $useraboutminetest . "<br />";
  866. echo "<div style='display:block;width:100%;text-align:center;'><a href='index.php?do=create-minetest' class='button'>Go with Minetest current</a></div>";
  867. echo "</p>";
  868. }
  869. else
  870. {
  871. header("Location: index.php?do=manage");
  872. }
  873. } else {
  874. // This is the admin panel!
  875. echo "<h3>Administration panel</h3>";
  876. if(!file_exists("mtm_db/token.php")) { generateToken(); }
  877. include_once("mtm_db/token.php");
  878. echo "<p>Current registration token: $reg_token</p>";
  879. echo "<h3>Server information</h3>";
  880. // Open system meminfo
  881. $fhmem = fopen('/proc/meminfo','r');
  882. // Open system loadavg
  883. $fhcpu = fopen('/proc/loadavg','r');
  884. // Set to 0 for population
  885. $memavail = 0;
  886. $memfree = 0;
  887. while ($line = fgets($fhmem)) {
  888. $piecesavail = array();
  889. $piecesfree = array();
  890. if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $piecesavail)) {
  891. $memavail = $piecesavail[1];
  892. }
  893. if (preg_match('/^MemAvailable:\s+(\d+)\skB$/', $line, $piecesfree)) {
  894. $memfree = $piecesfree[1];
  895. break;
  896. }
  897. }
  898. fclose($fhmem);
  899. echo round(($memavail/1024)) . " MB RAM found, " . round(($memfree/1024)) . " MB RAM free<br />\n";
  900. while ($line = fgets($fhcpu)) {
  901. echo "Load: " . $line;
  902. }
  903. fclose($fhcpu);
  904. // Admin server controls.
  905. echo "<br />";
  906. echo "<a href='index.php?do=admin-start-all' class='button'>Start All</a>";
  907. echo "<a href='index.php?do=admin-stop-all' class='button'>Stop All</a><br />";
  908. // To be continued!
  909. chdir("mtm_db/users");
  910. echo "<div id='admint'><table><tr><td>Username</td><td>Full Name</td><td>Email</td><td>Server-Type/Port</td><td>User ID</td><td>Server Stat</td></tr>";
  911. foreach(glob("*.php") as $user_account_file) {
  912. include_once($user_account_file);
  913. $servertype = file_get_contents("$userid.type");
  914. $serverstatus = file_get_contents("$userid.pid.status");
  915. echo "<tr><td>$user_handle</td><td>$user_fullname</td><td>$user_email</td><td>$servertype/$portrange$userid</td><td>$userid</td><td>$serverstatus</td></tr>";
  916. }
  917. echo "</table></div>";
  918. }
  919. }
  920. ?>
  921. <br /><br />
  922. <div style="margin: auto; width: 100%; text-align: center; background-color: #555555; padding: 3px;">Powered By <a href="https://notabug.org/Pentium44/MTSM">MTSM</a> <?php echo $version; ?> <br />
  923. </div>
  924. </div>
  925. </div> <!-- main contain -->
  926. </body>
  927. </html>