Newer
Older
backup-commander / html / inc / secure.php
  1. <?php
  2.  
  3. /*if( isset($_POST['action']) ) {
  4. error_log("Login action:".$_POST['action'] . "," . $_POST['username'] . "," . $_POST['password'] );
  5. }*/
  6.  
  7. session_start();
  8.  
  9. if( isset($_GET['action']) && $_GET['action']=='logout' ) {
  10. unset( $_SESSION['username'] );
  11. header("Location: index.php");
  12. exit;
  13. }
  14.  
  15. if( isset($_SESSION['username']) && strlen($_SESSION['username'])>=4 ){
  16. //the user is logged in so nothing further to do in this script
  17. return;
  18. }
  19.  
  20. //is there a login attempt
  21. if( isset($_POST['username']) && isset($_POST['password']) ){
  22. //any request must have the 'action' set to btn_login
  23. if( !isset($_POST['action']) || $_POST['action']!='btn_login' ) {
  24. error_log("Login Error: Missing or incorrect value for 'action'");
  25. exit;
  26. }
  27. if( !isset($_POST['svr_nonce']) || $_POST['svr_nonce']=='' ) {
  28. error_log("Login Error: Missing or blank value for 'svr_nonce'");
  29. exit;
  30. }
  31.  
  32. //has the correct nonce been returned?
  33. if( $_POST['svr_nonce']!=$_SESSION['svr_nonce'] ){
  34. $_SESSION['svr_nonce'] = ''; // invalidate nonce
  35. error_log("Login Error: Incorrect value for 'svr_nonce'");
  36. exit;
  37. }
  38.  
  39. include_once __DIR__."/../shd/common.php";
  40. include_once "login.php";
  41. if(authenticate( $_POST['username'], $_POST['password'], $_SESSION['svr_nonce'] ) ){
  42. error_log("Login Success: ".$_POST['username']);
  43. $_SESSION['username'] = $_POST['username'];
  44. //return the backup list page
  45. include_once __DIR__ . "/bu_list_content.php";
  46. sendHtmlOk_WithData( [ 'authenticated', getbu_list_content() ] );
  47.  
  48. }else{
  49. error_log("Login Failure: ".$_POST['username']);
  50. // failed authentication
  51. // --- Send back the login page ---
  52. $_SESSION['svr_nonce'] = generateNonce();
  53. sendHtmlOk_WithData( [ 'login', getLogin_content(), $_SESSION['svr_nonce'] ] );
  54. }
  55. exit;
  56. }
  57.  
  58. // no session is set
  59. // no attempt to login was made
  60. // likely that the page was just refreshed
  61. // --- Send back the login page ---
  62.  
  63. include_once __DIR__."/../shd/common.php";
  64. $_SESSION['svr_nonce'] = generateNonce();
  65.  
  66. include_once "login.php";
  67. sendHtmlOk_WithData( [ 'login', getLogin_content(), $_SESSION['svr_nonce'] ] );
  68. exit; //end the script to stop further processing by calling files
  69.  
  70. //$svr_username = $_SESSION['username'];
  71.  
  72. ?>