loginwebpage.class.inc.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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=\"loginop\" 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 SecureConnectionRequired()
  76. {
  77. $oConfig = new Config(ITOP_CONFIG_FILE);
  78. return $oConfig->GetSecureConnectionRequired();
  79. }
  80. static function IsConnectionSecure()
  81. {
  82. $bSecured = false;
  83. if ( !empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!= 'off') )
  84. {
  85. $bSecured = true;
  86. }
  87. return $bSecured;
  88. }
  89. static function DoLogin()
  90. {
  91. if (self::SecureConnectionRequired() && !self::IsConnectionSecure())
  92. {
  93. // Non secured URL... redirect to a secured one
  94. $sUrl = Utils::GetAbsoluteUrl(true /* query string */, true /* force HTTPS */);
  95. header("Location: $sUrl");
  96. exit;
  97. }
  98. $operation = utils::ReadParam('loginop', '');
  99. session_start();
  100. if ($operation == 'logoff')
  101. {
  102. self::ResetSession();
  103. }
  104. if (!isset($_SESSION['auth_user']) || !isset($_SESSION['auth_pwd']))
  105. {
  106. if ($operation == 'loginurl')
  107. {
  108. $sAuthUser = utils::ReadParam('auth_user', '', 'get');
  109. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'get');
  110. }
  111. else if ($operation == 'login')
  112. {
  113. $sAuthUser = utils::ReadParam('auth_user', '', 'post');
  114. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'post');
  115. }
  116. else
  117. {
  118. $oPage = new LoginWebPage();
  119. $oPage->DisplayLoginForm();
  120. $oPage->output();
  121. exit;
  122. }
  123. }
  124. else
  125. {
  126. $sAuthUser = $_SESSION['auth_user'];
  127. $sAuthPwd = $_SESSION['auth_pwd'];
  128. }
  129. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  130. {
  131. self::ResetSession();
  132. $oPage = new LoginWebPage();
  133. $oPage->DisplayLoginForm( true /* failed attempt */);
  134. $oPage->output();
  135. exit;
  136. }
  137. else
  138. {
  139. $_SESSION['auth_user'] = $sAuthUser ;
  140. $_SESSION['auth_pwd'] = $sAuthPwd;
  141. }
  142. }
  143. } // End of class
  144. ?>