loginwebpage.class.inc.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $bHTTPBasicAuthentication = (utils::ReadParam('auth', '', 'get') == 'http_basic');
  99. if ($bHTTPBasicAuthentication)
  100. {
  101. // Basic HTTP/PHP authentication mecanism
  102. //
  103. // meme avec ca c'est pourri - return;
  104. if (!isset($_SERVER['PHP_AUTH_USER']))
  105. {
  106. header('WWW-Authenticate: Basic realm="iTop access is restricted"');
  107. header('HTTP/1.0 401 Unauthorized');
  108. // Note: accessed when the user will click on Cancel
  109. echo '<p><strong>iTop access is restricted</strong>. Please, contact an iTop administrator.</p>';
  110. exit;
  111. }
  112. else
  113. {
  114. $sAuthUser = $_SERVER['PHP_AUTH_USER'];
  115. $sAuthPwd = $_SERVER['PHP_AUTH_PW'];
  116. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  117. {
  118. header('WWW-Authenticate: Basic realm="Unknown user \''.$sAuthUser.'\'"');
  119. header('HTTP/1.0 401 Unauthorized');
  120. // Note: accessed when the user will click on Cancel
  121. // Todo: count the attempts
  122. echo '<p><strong>iTop access is restricted</strong>. Please, contact an iTop administrator.</p>';
  123. exit;
  124. }
  125. }
  126. return;
  127. }
  128. // Home-made authentication mecanism
  129. //
  130. $operation = utils::ReadParam('loginop', '');
  131. session_start();
  132. if ($operation == 'logoff')
  133. {
  134. self::ResetSession();
  135. }
  136. if (!isset($_SESSION['auth_user']) || !isset($_SESSION['auth_pwd']))
  137. {
  138. if ($operation == 'loginurl')
  139. {
  140. $sAuthUser = utils::ReadParam('auth_user', '', 'get');
  141. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'get');
  142. }
  143. else if ($operation == 'login')
  144. {
  145. $sAuthUser = utils::ReadParam('auth_user', '', 'post');
  146. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'post');
  147. }
  148. else
  149. {
  150. $oPage = new LoginWebPage();
  151. $oPage->DisplayLoginForm();
  152. $oPage->output();
  153. exit;
  154. }
  155. }
  156. else
  157. {
  158. $sAuthUser = $_SESSION['auth_user'];
  159. $sAuthPwd = $_SESSION['auth_pwd'];
  160. }
  161. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  162. {
  163. self::ResetSession();
  164. $oPage = new LoginWebPage();
  165. $oPage->DisplayLoginForm( true /* failed attempt */);
  166. $oPage->output();
  167. exit;
  168. }
  169. else
  170. {
  171. $_SESSION['auth_user'] = $sAuthUser ;
  172. $_SESSION['auth_pwd'] = $sAuthPwd;
  173. }
  174. }
  175. } // End of class
  176. ?>