loginwebpage.class.inc.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Class LoginWebPage
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once("../application/nicewebpage.class.inc.php");
  25. /**
  26. * Web page used for displaying the login form
  27. */
  28. class LoginWebPage extends NiceWebPage
  29. {
  30. public function __construct()
  31. {
  32. parent::__construct("iTop Login");
  33. $this->add_style("
  34. body {
  35. background-color: #eee;
  36. margin: 0;
  37. padding: 0;
  38. }
  39. #login {
  40. width: 230px;
  41. margin-left: auto;
  42. margin-right: auto;
  43. margin-top: 150px;
  44. padding: 20px;
  45. background-color: #fff;
  46. border: 1px solid #000;
  47. }
  48. .center {
  49. text-align: center;
  50. }
  51. h1 {
  52. color: #83b217;
  53. font-size: 16pt;
  54. }
  55. .v-spacer {
  56. padding-top: 1em;
  57. }
  58. ");
  59. }
  60. public function DisplayLoginForm($bFailedLogin = false)
  61. {
  62. $sAuthUser = utils::ReadParam('auth_user', '');
  63. $sAuthPwd = utils::ReadParam('suggest_pwd', '');
  64. $this->add("<div id=\"login\">\n");
  65. $this->add("<h1>".Dict::S('UI:Login:Welcome')."</h1>\n");
  66. if ($bFailedLogin)
  67. {
  68. $this->add("<p class=\"hilite\">".Dict::S('UI:Login:IncorrectLoginPassword')."</p>\n");
  69. }
  70. else
  71. {
  72. $this->add("<p>".Dict::S('UI:Login:IdentifyYourself')."</p>\n");
  73. }
  74. $this->add("<form method=\"post\">\n");
  75. $this->add("<table>\n");
  76. $this->add("<tr><td><label for=\"user\">".Dict::S('UI:Login:UserNamePrompt').":</label></td><td><input id=\"user\" type=\"text\" name=\"auth_user\" value=\"$sAuthUser\" /></td></tr>\n");
  77. $this->add("<tr><td><label for=\"pwd\">".Dict::S('UI:Login:PasswordPrompt').":</label></td><td><input id=\"pwd\" type=\"password\" name=\"auth_pwd\" value=\"$sAuthPwd\" /></td></tr>\n");
  78. $this->add("<tr><td colspan=\"2\" class=\"center v-spacer\"> <input type=\"submit\" value=\"".Dict::S('UI:Button:Login')."\" /></td></tr>\n");
  79. $this->add("</table>\n");
  80. $this->add("<input type=\"hidden\" name=\"loginop\" value=\"login\" />\n");
  81. $this->add("</form>\n");
  82. $this->add("</div>\n");
  83. }
  84. static protected function ResetSession()
  85. {
  86. // Unset all of the session variables.
  87. $_SESSION = array();
  88. // If it's desired to kill the session, also delete the session cookie.
  89. // Note: This will destroy the session, and not just the session data!
  90. if (isset($_COOKIE[session_name()]))
  91. {
  92. setcookie(session_name(), '', time()-3600, '/');
  93. }
  94. // Finally, destroy the session.
  95. session_destroy();
  96. }
  97. static function SecureConnectionRequired()
  98. {
  99. $oConfig = new Config(ITOP_CONFIG_FILE);
  100. return $oConfig->GetSecureConnectionRequired();
  101. }
  102. static function IsConnectionSecure()
  103. {
  104. $bSecured = false;
  105. if ( !empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!= 'off') )
  106. {
  107. $bSecured = true;
  108. }
  109. return $bSecured;
  110. }
  111. static function DoLogin()
  112. {
  113. if (self::SecureConnectionRequired() && !self::IsConnectionSecure())
  114. {
  115. // Non secured URL... redirect to a secured one
  116. $sUrl = Utils::GetAbsoluteUrl(true /* query string */, true /* force HTTPS */);
  117. header("Location: $sUrl");
  118. exit;
  119. }
  120. $bHTTPBasicAuthentication = (utils::ReadParam('auth', '', 'get') == 'http_basic');
  121. if ($bHTTPBasicAuthentication)
  122. {
  123. // Basic HTTP/PHP authentication mecanism
  124. //
  125. // meme avec ca c'est pourri - return;
  126. if (!isset($_SERVER['PHP_AUTH_USER']))
  127. {
  128. header('WWW-Authenticate: Basic realm="iTop access is restricted"');
  129. header('HTTP/1.0 401 Unauthorized');
  130. // Note: accessed when the user will click on Cancel
  131. echo '<p><strong>'.Dict::S('UI:Login:Error:AccessRestricted').'</strong></p>';
  132. exit;
  133. }
  134. else
  135. {
  136. $sAuthUser = $_SERVER['PHP_AUTH_USER'];
  137. $sAuthPwd = $_SERVER['PHP_AUTH_PW'];
  138. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  139. {
  140. header('WWW-Authenticate: Basic realm="Unknown user \''.$sAuthUser.'\'"');
  141. header('HTTP/1.0 401 Unauthorized');
  142. // Note: accessed when the user will click on Cancel
  143. // Todo: count the attempts
  144. echo '<p><strong>'.Dict::S('UI:Login:Error:AccessRestricted').'</strong></p>';
  145. exit;
  146. }
  147. }
  148. return;
  149. }
  150. // Home-made authentication mecanism
  151. //
  152. $operation = utils::ReadParam('loginop', '');
  153. session_start();
  154. if ($operation == 'logoff')
  155. {
  156. self::ResetSession();
  157. }
  158. if (!isset($_SESSION['auth_user']) || !isset($_SESSION['auth_pwd']))
  159. {
  160. if ($operation == 'loginurl')
  161. {
  162. $sAuthUser = utils::ReadParam('auth_user', '', 'get');
  163. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'get');
  164. }
  165. else if ($operation == 'login')
  166. {
  167. $sAuthUser = utils::ReadParam('auth_user', '', 'post');
  168. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'post');
  169. }
  170. else
  171. {
  172. $oPage = new LoginWebPage();
  173. $oPage->DisplayLoginForm();
  174. $oPage->output();
  175. exit;
  176. }
  177. }
  178. else
  179. {
  180. $sAuthUser = $_SESSION['auth_user'];
  181. $sAuthPwd = $_SESSION['auth_pwd'];
  182. }
  183. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  184. {
  185. self::ResetSession();
  186. $oPage = new LoginWebPage();
  187. $oPage->DisplayLoginForm( true /* failed attempt */);
  188. $oPage->output();
  189. exit;
  190. }
  191. else
  192. {
  193. $_SESSION['auth_user'] = $sAuthUser ;
  194. $_SESSION['auth_pwd'] = $sAuthPwd;
  195. }
  196. }
  197. } // End of class
  198. ?>