regi.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. function showerror($msg){
  3. header("Location: regi.php?msg=" . $msg);
  4. exit(0);
  5. }
  6. header('aoAuth: v1.0');
  7. if (session_status() == PHP_SESSION_NONE) {
  8. session_start();
  9. }
  10. $databasePath = "";
  11. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  12. $rootPath = "C:/AOB/";
  13. }else{
  14. $rootPath = "/etc/AOB/";
  15. }
  16. if (filesize("root.inf") > 0){
  17. //Use the special path instead.
  18. $rootPath = trim(file_get_contents("root.inf"));
  19. }
  20. $databasePath = $rootPath . "whitelist.config";
  21. $content = "";
  22. $regexists = false;
  23. if (file_exists($databasePath)){
  24. include_once("auth.php");
  25. //If the user is able to continues to proceed, that means the user has right to use this system
  26. $content = file_get_contents($databasePath);
  27. $regexists = true;
  28. }else{
  29. //There is no user registration yet. Create one
  30. }
  31. //See if this page is requested for command.
  32. $errormsg = "";
  33. if (isset($_POST['act']) && $_POST['act'] != ""){
  34. $action = $_POST['act'];
  35. if ($action == "newuser"){
  36. if (isset($_POST['username']) && isset($_POST['secretecode'])){
  37. $newusername = $_POST['username'];
  38. $password = $_POST['secretecode'];
  39. if ($password == ""){
  40. showerror("Password cannot be empty!");
  41. }
  42. $encodedpw = hash('sha512',$password);
  43. $content = trim($content);
  44. $users = explode(PHP_EOL,$content);
  45. $usernameexists = false;
  46. foreach ($users as $userdata){
  47. $username = explode(",",$userdata)[0];
  48. if (strtolower($username) == strtolower($newusername)){
  49. $usernameexists = true;
  50. }
  51. }
  52. if ($usernameexists){
  53. $errormsg = "Username already exists.";
  54. showerror($errormsg);
  55. }else{
  56. $encodedpw = strtoupper($encodedpw);
  57. file_put_contents($databasePath,$newusername . "," . $encodedpw . PHP_EOL,FILE_APPEND);
  58. header("Location: regi.php?msg=New user added.");
  59. exit(0);
  60. }
  61. }
  62. }else if ($action == "rmvuser"){
  63. if (isset($_POST['username'])){
  64. $targetusername = $_POST['username'];
  65. $content = trim($content);
  66. $users = explode(PHP_EOL,$content);
  67. $allowedusers = [];
  68. foreach ($users as $userdata){
  69. $username = explode(",",$userdata)[0];
  70. if (strtolower($username) == strtolower($targetusername)){
  71. }else{
  72. array_push($allowedusers,$userdata);
  73. }
  74. }
  75. $newcontent = implode(PHP_EOL,$allowedusers);
  76. $newcontent .= PHP_EOL;
  77. if (count($allowedusers) == 0){
  78. unlink($databasePath);
  79. }else{
  80. file_put_contents($databasePath,$newcontent);
  81. }
  82. die("DONE");
  83. }else{
  84. die("ERROR. username not defined for act=rmvuser");
  85. }
  86. }
  87. exit(0);
  88. }
  89. ?>
  90. <html>
  91. <!DOCTYPE HTML>
  92. <head>
  93. <meta name="viewport" content="width=device-width, initial-scale=0.7, shrink-to-fit=no">
  94. <title>ArOZ Onlineβ</title>
  95. <link rel="stylesheet" href="script/tocas/tocas.css">
  96. <script src="script/tocas/tocas.js"></script>
  97. <script src="script/jquery.min.js"></script>
  98. </head>
  99. <body>
  100. <!--
  101. <nav id="topbar" class="ts attached inverted borderless large menu">
  102. <div class="ts narrow container">
  103. <a href="" class="item">ArOZ Online β</a>
  104. </div>
  105. </nav>
  106. -->
  107. <br><br><br>
  108. <div class="ts container">
  109. <h3 class="ts header">
  110. <i class="privacy icon"></i>
  111. <div class="content">
  112. ArOZ Online Authentication Register
  113. </div>
  114. </h3>
  115. <!-- New user adding form-->
  116. <div id="newuser" class="ts container" style="display:none;">
  117. <form class="ts small form" action="regi.php" method="POST">
  118. <div class="field">
  119. <label>Username</label>
  120. <input name="username" type="text">
  121. </div>
  122. <div class="field">
  123. <label>Password</label>
  124. <input id="passwordfield" name="secretecode" type="password">
  125. </div>
  126. <input name="act" type="text" value="newuser" style="display:none;">
  127. <code>Please login to your new account after you have added the first new user.</code><br><br>
  128. <div class="ts warning button" onmousedown="showpw();" onmouseup="hidepw();"><i class="unhide icon"></i>Show Password</div>
  129. <button class="ts primary button" type="submit" value="Submit"><i class="add user icon"></i>Add user</button>
  130. </form>
  131. </div>
  132. <!-- Message Box-->
  133. <?php
  134. if (isset($_GET['msg'])){
  135. echo '<div id="returnedmsg" class="ts secondary primary message">
  136. <div class="header">Message</div>
  137. <p>'.$_GET['msg'].'</p>
  138. </div>';
  139. }
  140. ?>
  141. <!-- List of user -->
  142. <p>List of registered users for this system</p>
  143. <div class="ts divider"></div>
  144. <div class="ts segmented list">
  145. <?php
  146. $content = trim($content);
  147. if ($content != ""){
  148. $users = explode(PHP_EOL,$content);
  149. foreach ($users as $userdata){
  150. $username = explode(",",$userdata)[0];
  151. echo '<div class="item"><i class="user icon"></i>'.$username.'</div>';
  152. }
  153. }
  154. ?>
  155. </div>
  156. <div style="width:100%;" align="right">
  157. <div class="ts buttons">
  158. <button class="ts primary button" onClick='$("#newuser").show();'><i class="add user icon"></i>New User</button>
  159. <button class="ts warning button" onClick="removeUser();"><i class="remove user icon" ></i>Remove User</button>
  160. </div>
  161. </div>
  162. <a id="backBtn" href="index.php">Back to index</a>
  163. <div class="ts divider"></div>
  164. ArOZ Online Authentication System feat. IMUS Laboratory
  165. </div>
  166. <script>
  167. var selectedUser = "";
  168. setTimeout(function(){ hideMsgBox(); }, 5000);
  169. if (parent.underNaviEnv){
  170. $("#backBtn").hide();
  171. }
  172. function hideMsgBox(){
  173. if($("#returnedmsg").length == 0) {
  174. }else{
  175. $("#returnedmsg").fadeOut(1000);
  176. }
  177. }
  178. function removeUser(){
  179. if (selectedUser != ""){
  180. if (confirm("Are you sure you want to remove user: " + selectedUser) == true){
  181. $.post( "regi.php", { username: selectedUser, act: "rmvuser" })
  182. .done(function(data){
  183. window.location.href="regi.php?msg=User Removed";
  184. });
  185. }
  186. }
  187. }
  188. function showpw(){
  189. $("#passwordfield").attr("type","text");
  190. }
  191. function hidepw(){
  192. $("#passwordfield").attr("type","password");
  193. }
  194. $(".item").click(function(){
  195. $(".item").each(function(){
  196. $(this).removeClass("selected");
  197. });
  198. $(this).addClass("selected");
  199. selectedUser = $(this).text();
  200. });
  201. </script>
  202. </body>
  203. </html>