auth.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /*
  3. ArOZ Online Auth Script
  4. This script is designed to provide all auth function for the whole ArOZ Online System
  5. Please do not modify this script unless you know what you are doing.
  6. CopyRight ArOZ Online Project feat. IMUS Laboratory, All right reserved.
  7. Developed by Toby Chui since 2016
  8. */
  9. //Uncomment the following line for emergency terminating all services on ArOZ Online System
  10. //header("HTTP/1.0 503 Service Unavailable"); echo "<p>ArOZ Online System on this site has been emergency shut down by system administrator.</p>"; exit(0);
  11. header('aoAuth: v1.0');
  12. if (session_status() == PHP_SESSION_NONE) {
  13. session_start();
  14. }
  15. //Auth System Settings. DO NOT TOUCH THESE VALUES
  16. $maxAuthscriptDepth = 32;
  17. $sysConfigDir = "userdata/"; //Remember to end with "/"
  18. //You can get the following variable from any script that included this auth script.
  19. /*
  20. $sysConfigDir --> Location of the ArOZ Online Storage Directory, usually C:/AOB/ on Windows or /etc/AOB/ on Linux
  21. $rootPath --> Relative directory to root, in backslash format (aka ../)
  22. */
  23. function checkIfCookieSeedsMatch($seedsbank,$cookieString){
  24. $data = explode("_",$cookieString);
  25. $timestamp = $data[0];
  26. $seedfile = $seedsbank . $timestamp . '.auth';
  27. if (time() > $timestamp){
  28. if (file_exists($seedfile)){
  29. //This session has been expired. Remove the session from server side
  30. unlink($seedfile);
  31. }
  32. return false;
  33. }
  34. $seeds = $data[1];
  35. if (file_exists($seedfile)){
  36. $seedvalue = file_get_contents($seedfile);
  37. if ($seedvalue == $seeds){
  38. return true;
  39. }else{
  40. return false;
  41. }
  42. }else{
  43. return false;
  44. }
  45. }
  46. $databasePath = "";
  47. if ($sysConfigDir == ""){
  48. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  49. $sysConfigDir = "C:/AOB/";
  50. }else{
  51. $sysConfigDir = "/etc/AOB/";
  52. }
  53. }else{
  54. //This system use a specially configured root location. Append that to system root.inf if this is launched on the root location.
  55. if(file_exists("root.inf")){
  56. file_put_contents("root.inf",$sysConfigDir);
  57. }
  58. }
  59. $databasePath = $sysConfigDir . "whitelist.config";
  60. $seedsbank = $sysConfigDir . "cookieseeds/";
  61. if (file_exists($seedsbank) == false){
  62. if (!@mkdir($seedsbank,0777,true)){
  63. //mkdir failed. Try to override it with sudo permission if on linux.
  64. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  65. die("ERRPR. Unable to write to directory: " . $seedsbank);
  66. }else{
  67. exec('sudo mkdir "' . realpath($seedsbank) . '"');
  68. exec('sudo chmod 777 "' . realpath($seedsbank) . '"');
  69. }
  70. }
  71. }
  72. if (isset($_POST['username']) && isset($_POST['apwd']) && isset($_POST['rmbm'])){
  73. $loginContent = file_get_contents($databasePath);
  74. $loginContent = explode("\n",$loginContent);
  75. $rememberMe = $_POST['rmbm'];
  76. if ($rememberMe == "on"){
  77. //There might be auto login. Check if the password field matched any seed first.
  78. if (checkIfCookieSeedsMatch($seedsbank,$_POST['apwd'])){
  79. //Update the current cookies
  80. $cookieExpireTime = time()+ 172800;
  81. setcookie("username",$_POST["username"],$cookieExpireTime );
  82. $password = $_POST["apwd"];
  83. $rndnum = rand(10000000, 90000000);
  84. $seedString = hash("sha512",$password . $rndnum);
  85. setcookie("password",$cookieExpireTime . "_" . $seedString,$cookieExpireTime);
  86. file_put_contents($seedsbank . $cookieExpireTime . '.auth',$seedString);
  87. $_SESSION['login'] = $_POST["username"];
  88. echo "DONE. Login suceed.";
  89. exit();
  90. }
  91. $rememberMe = true;
  92. }else{
  93. $rememberMe = false;
  94. }
  95. $cookieContent = "";
  96. if ($rememberMe){
  97. setcookie("username",$_POST["username"],time()+ 172800 );
  98. //Updates in 28-9-2018, removed raw password storage in cookie (who the hell think of this in the first place lol)
  99. //setcookie("password",$_POST["apwd"],time()+ 172800 );
  100. $cookieExpireTime = time()+ 172800;
  101. $password = $_POST["apwd"];
  102. $rndnum = rand(10000000, 90000000);
  103. if ($password == ""){
  104. echo "ERROR. Password cannot be empty.";
  105. exit();
  106. }
  107. $seedString = hash("sha512",$password . $rndnum);
  108. $cookieContent = $seedString;
  109. }else{
  110. setcookie("username","",time()+ 172800);
  111. setcookie("password","",time()+ 172800);
  112. }
  113. foreach ($loginContent as $registedUserData){
  114. if ($registedUserData != ""){
  115. $chunk = explode(",",$registedUserData);
  116. $username = $chunk[0];
  117. $hasedpw = $chunk[1];
  118. if ($username == $_POST['username']){
  119. $hashedInput = hash('sha512', $_POST['apwd']);
  120. if (trim(strtoupper($hasedpw)) == trim(strtoupper($hashedInput))){
  121. //Login suceed
  122. $_SESSION['login'] = $username;
  123. if ($rememberMe && $cookieContent != ""){
  124. //Store the cookie to browser as well as the server side for future access
  125. setcookie("password",$cookieExpireTime . "_" . $cookieContent,time()+ 172800);
  126. file_put_contents($seedsbank . $cookieExpireTime . '.auth',$cookieContent);
  127. }
  128. echo "DONE. Login suceed.";
  129. exit();
  130. }else{
  131. echo "ERROR. Password incorrect";
  132. exit();
  133. }
  134. }
  135. }
  136. }
  137. echo "ERROR. Username not find.";
  138. exit();
  139. }
  140. if (file_exists($databasePath)){
  141. $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  142. $rootPath = "";
  143. if (file_exists("root.inf")){
  144. //The script is running on the root folder
  145. }else{
  146. //The script is not running on the root folder, find upward and see where is the root file is placed.
  147. for ($x = 0; $x <= $maxAuthscriptDepth; $x++) {
  148. if (file_exists($rootPath . "/root.inf")){
  149. break;
  150. }else{
  151. $rootPath = $rootPath . "../";
  152. }
  153. }
  154. }
  155. if (session_id() == '' || !isset($_SESSION['login']) || $_SESSION['login'] == "") {
  156. //echo $actual_link;
  157. header('Location: ' . $rootPath .'login.php?target=' . str_replace("&","%26",$actual_link));
  158. exit();
  159. }else{
  160. //session exists. Let the user go through with updates cookie
  161. if (isset($_COOKIE['username']) && $_COOKIE['username'] != "") {
  162. setcookie("username",$_COOKIE["username"],time()+ 172800 );
  163. setcookie("password",$_COOKIE["password"],time()+ 172800 );
  164. }else{
  165. //cookie expired. Request for another update with login
  166. $_SESSION['login'] = "";
  167. header('Location: ' . $rootPath .'login.php?target=' . str_replace("&","%26",$actual_link));
  168. exit();
  169. }
  170. }
  171. }else{
  172. //Database file do not exists. As the user to create one
  173. header("Location: regi.php");
  174. exit();
  175. }
  176. ?>