login.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. if( empty( $_SERVER["HTTPS"] ) ) {
  3. http_response_code(301);
  4. header("Location: https://" . $_SERVER["SERVER_NAME"] . "/" . $_SERVER["REQUEST_URI"]);
  5. die();
  6. }
  7. $loginFailure = -1;
  8. $supported_methods = [ 'GET', 'POST' ];
  9. if( in_array( $_SERVER['REQUEST_METHOD'], $supported_methods ) ) {
  10. switch($_SERVER['REQUEST_METHOD']) {
  11. case 'GET' : $request = &$_GET; break;
  12. case 'POST': $request = &$_POST; break;
  13. }
  14. $args = [];
  15. foreach( $request as $key => $val ) {
  16. //Clean the junk
  17. $args[$key] = urldecode(filter_var( $val, FILTER_SANITIZE_ENCODED, FILTER_SANITIZE_SPECIAL_CHARS ));
  18. }
  19. $loginMsg = "Unknown Error";
  20. if( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
  21. require_once "tCMS/Auth.inc"; //Authenticate
  22. $auth = new Auth;
  23. $login = $auth->do_auth( $args['username'], $args['password'] );
  24. if(!is_array($login) || !empty($login['err']) || empty( $login['success'] ) ) {
  25. $loginFailure = 1;
  26. if( !empty( $login['msg'] ) ) $loginMsg = $login['msg'];
  27. http_response_code(401);
  28. } else { # We succeeded
  29. $loginMsg = "Login Succeded, redirecting...";
  30. $loginFailure = 0;
  31. }
  32. }
  33. }
  34. # Construct the redirection URL
  35. if( empty( $args['to'] ) ) {
  36. $redir = 'index.php';
  37. } else {
  38. $redir = 'index.php?app=' . $args["to"];
  39. }
  40. $redirection_URL = "https://" . $_SERVER["SERVER_NAME"] . "/sys/admin/$redir";
  41. ?>
  42. <!doctype html>
  43. <html dir="ltr" lang="en-US">
  44. <head>
  45. <title>tCMS 2 ~ Login</title>
  46. <style>
  47. body {
  48. background-color: gray;
  49. font-family: sans-serif;
  50. }
  51. #login {
  52. margin: 0 auto;
  53. max-width: 25rem;
  54. color: white;
  55. }
  56. #logo {
  57. display: block;
  58. max-width: 90%;
  59. margin: 0 0 0 5%;
  60. height: 2rem;
  61. }
  62. #login form {
  63. max-width: 85%;
  64. display: block;
  65. margin: 0 auto;
  66. }
  67. #maximumGo {
  68. width: calc(100% - 1rem);
  69. }
  70. #copyright {
  71. font-size: .75rem;
  72. text-align: center;
  73. }
  74. #jsalert {
  75. margin: 1rem;
  76. border-radius: .75rem;
  77. border-color: rgba(255,0,0,.75);
  78. padding: 1rem;
  79. }
  80. .alert-danger {
  81. background-color: rgba(255,0,0,.25);
  82. }
  83. .alert-success {
  84. background-color: rgba(0,255,0,.25);
  85. }
  86. input {
  87. box-sizing: border-box;
  88. border-radius: .5em;
  89. border: .25em solid black;
  90. color: white;
  91. padding: .25em;
  92. margin: .25em;
  93. }
  94. .input-group {
  95. display: table;
  96. width: 100%;
  97. }
  98. .input-group > input, .input-group > label {
  99. display: table-cell;
  100. }
  101. .input-group > input {
  102. width: calc(100% - 1rem);
  103. background-color: #333;
  104. }
  105. input[type="submit"] {
  106. box-shadow: 0 0 .5em black;
  107. background-color: #333;
  108. color: white;
  109. }
  110. </style>
  111. <script>
  112. document.addEventListener("DOMContentLoaded", function(event) {
  113. var loginFailure = <?php echo $loginFailure; ?>;
  114. if( loginFailure === -1 ) {
  115. document.querySelector('#jsalert').style.cssText = 'visibility: hidden;';
  116. } else if ( loginFailure === 1 ) {
  117. document.querySelector('#jsalert').classList.remove("alert-success");
  118. document.querySelector('#jsalert').classList.add("alert-danger");
  119. document.querySelector('#msgIcon').innerHTML = "❌";
  120. document.querySelector('#message').innerHTML = "<?php echo $loginMsg ?>";
  121. } else {
  122. document.querySelector('#jsalert').classList.remove("alert-danger");
  123. document.querySelector('#jsalert').classList.add("alert-success");
  124. document.querySelector('#msgIcon').innerHTML = "✓";
  125. document.querySelector('#message').innerHTML = "<?php echo $loginMsg ?>";
  126. window.setTimeout(function() {
  127. window.location="<?php echo $redirection_URL; ?>";
  128. }, 500);
  129. }
  130. });
  131. </script>
  132. <link rel="icon" type="image/vnd.microsoft.icon" href="../../img/icon/favicon.ico" />
  133. </head>
  134. <body>
  135. <div id="login">
  136. <div id="jsalert" class="alert-danger">
  137. <table>
  138. <tr>
  139. <td id="msgIcon">
  140. </td>
  141. <td id="message" style="padding-left: 1rem;">
  142. Please enable JavaScript on this domain.
  143. </td>
  144. </tr>
  145. </table>
  146. </div>
  147. <div>
  148. <img id="logo" src="../../themed/default/img/icon/tCMS.svg" style="float:left" /><span style="font-family:courier;font-size:2rem;">CMS Login</span>
  149. </div>
  150. <div id="spacer" style="clear: both;"><br /></div>
  151. <form method="POST" action="index.php">
  152. <input type="hidden" name="app" value="login" />
  153. <input type="hidden" name="to" value="<?php if(!empty($args['to'])) echo $args['to']; ?>" />
  154. Username<br />
  155. <div class="input-group">
  156. <label for="username">😎</span></label>
  157. <input name="username" id="username" placeholder="AzureDiamond" value="" type="text"></input>
  158. </div>
  159. <br />
  160. Password<br />
  161. <div class="input-group">
  162. <label for="password">🔑</label>
  163. <input name="password" id="password" placeholder="hunter2" value="" type="password"></input>
  164. </div>
  165. <br />
  166. <input type="submit" id="maximumGo" value="Log in"></input>
  167. </form>
  168. </div>
  169. </body>
  170. </html>