regi.php 5.9 KB

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