loginwebpage.class.inc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. require_once("../application/nicewebpage.class.inc.php");
  3. /**
  4. * Web page used for displaying the login form
  5. */
  6. class LoginWebPage extends NiceWebPage
  7. {
  8. public function __construct()
  9. {
  10. parent::__construct("iTop Login");
  11. $this->add_style("
  12. body {
  13. background-color: #eee;
  14. margin: 0;
  15. padding: 0;
  16. }
  17. #login {
  18. width: 230px;
  19. margin-left: auto;
  20. margin-right: auto;
  21. margin-top: 150px;
  22. padding: 20px;
  23. background-color: #fff;
  24. border: 1px solid #000;
  25. }
  26. .center {
  27. text-align: center;
  28. }
  29. h1 {
  30. color: #83b217;
  31. font-size: 16pt;
  32. }
  33. .v-spacer {
  34. padding-top: 1em;
  35. }
  36. ");
  37. }
  38. public function DisplayLoginForm($bFailedLogin = false)
  39. {
  40. $sAuthUser = utils::ReadParam('auth_user', '');
  41. $sAuthPwd = utils::ReadParam('suggest_pwd', '');
  42. $this->add("<div id=\"login\">\n");
  43. $this->add("<h1>Welcome to iTop!</h1>\n");
  44. if ($bFailedLogin)
  45. {
  46. $this->add("<p class=\"hilite\">Incorrect login/password, please try again.</p>\n");
  47. }
  48. else
  49. {
  50. $this->add("<p>Please identify yourself before continuing.</p>\n");
  51. }
  52. $this->add("<form method=\"post\">\n");
  53. $this->add("<table>\n");
  54. $this->add("<tr><td><label for=\"user\">User Name:</label></td><td><input id=\"user\" type=\"text\" name=\"auth_user\" value=\"$sAuthUser\" /></td></tr>\n");
  55. $this->add("<tr><td><label for=\"pwd\">Password:</label></td><td><input id=\"pwd\" type=\"password\" name=\"auth_pwd\" value=\"$sAuthPwd\" /></td></tr>\n");
  56. $this->add("<tr><td colspan=\"2\" class=\"center v-spacer\"> <input type=\"submit\" value=\"Enter iTop\" /></td></tr>\n");
  57. $this->add("</table>\n");
  58. $this->add("<input type=\"hidden\" name=\"operation\" value=\"login\" />\n");
  59. $this->add("</form>\n");
  60. $this->add("</div>\n");
  61. }
  62. static protected function ResetSession()
  63. {
  64. // Unset all of the session variables.
  65. $_SESSION = array();
  66. // If it's desired to kill the session, also delete the session cookie.
  67. // Note: This will destroy the session, and not just the session data!
  68. if (isset($_COOKIE[session_name()]))
  69. {
  70. setcookie(session_name(), '', time()-3600, '/');
  71. }
  72. // Finally, destroy the session.
  73. session_destroy();
  74. }
  75. static function DoLogin()
  76. {
  77. $operation = utils::ReadParam('operation', '');
  78. session_start();
  79. if ($operation == 'logoff')
  80. {
  81. self::ResetSession();
  82. }
  83. if (!isset($_SESSION['auth_user']) || !isset($_SESSION['auth_pwd']))
  84. {
  85. if ($operation == 'login')
  86. {
  87. $sAuthUser = utils::ReadParam('auth_user', '', 'post');
  88. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'post');
  89. }
  90. else
  91. {
  92. $oPage = new LoginWebPage();
  93. $oPage->DisplayLoginForm();
  94. $oPage->output();
  95. exit;
  96. }
  97. }
  98. else
  99. {
  100. $sAuthUser = $_SESSION['auth_user'];
  101. $sAuthPwd = $_SESSION['auth_pwd'];
  102. }
  103. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  104. {
  105. self::ResetSession();
  106. $oPage = new LoginWebPage();
  107. $oPage->DisplayLoginForm( true /* failed attempt */);
  108. $oPage->output();
  109. exit;
  110. }
  111. else
  112. {
  113. $_SESSION['auth_user'] = $sAuthUser ;
  114. $_SESSION['auth_pwd'] = $sAuthPwd;
  115. }
  116. }
  117. } // End of class
  118. ?>