loginwebpage.class.inc.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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(<<<EOF
  34. body {
  35. background: #eee;
  36. margin: 0;
  37. padding: 0;
  38. }
  39. #login-logo {
  40. margin-top: 150px;
  41. width: 250px;
  42. padding-left: 20px;
  43. padding-right: 20px;
  44. padding-top: 10px;
  45. padding-bottom: 10px;
  46. margin-left: auto;
  47. margin-right: auto;
  48. background: #f6f6f1;
  49. height: 54px;
  50. border-top: 1px solid #000;
  51. border-left: 1px solid #000;
  52. border-right: 1px solid #000;
  53. border-bottom: 0;
  54. text-align: center;
  55. }
  56. #login-logo img {
  57. border: 0;
  58. }
  59. #login {
  60. width: 250px;
  61. margin-left: auto;
  62. margin-right: auto;
  63. padding: 20px;
  64. background-color: #fff;
  65. border-bottom: 1px solid #000;
  66. border-left: 1px solid #000;
  67. border-right: 1px solid #000;
  68. border-top: 0;
  69. }
  70. .center {
  71. text-align: center;
  72. }
  73. h1 {
  74. color: #1C94C4;
  75. font-size: 16pt;
  76. }
  77. .v-spacer {
  78. padding-top: 1em;
  79. }
  80. EOF
  81. );
  82. }
  83. public function DisplayLoginForm($bFailedLogin = false)
  84. {
  85. $sAuthUser = utils::ReadParam('auth_user', '');
  86. $sAuthPwd = utils::ReadParam('suggest_pwd', '');
  87. $sVersionShort = Dict::Format('UI:iTopVersion:Short', ITOP_VERSION);
  88. $this->add("<div id=\"login-logo\"><a href=\"http://www.combodo.com/itop\"><img title=\"$sVersionShort\" src=\"../images/itop-logo.png\"></a></div>\n");
  89. $this->add("<div id=\"login\">\n");
  90. $this->add("<h1>".Dict::S('UI:Login:Welcome')."</h1>\n");
  91. if ($bFailedLogin)
  92. {
  93. $this->add("<p class=\"hilite\">".Dict::S('UI:Login:IncorrectLoginPassword')."</p>\n");
  94. }
  95. else
  96. {
  97. $this->add("<p>".Dict::S('UI:Login:IdentifyYourself')."</p>\n");
  98. }
  99. $this->add("<form method=\"post\">\n");
  100. $this->add("<table>\n");
  101. $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");
  102. $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");
  103. $this->add("<tr><td colspan=\"2\" class=\"center v-spacer\"> <input type=\"submit\" value=\"".Dict::S('UI:Button:Login')."\" /></td></tr>\n");
  104. $this->add("</table>\n");
  105. $this->add("<input type=\"hidden\" name=\"loginop\" value=\"login\" />\n");
  106. $this->add("</form>\n");
  107. $this->add("</div>\n");
  108. }
  109. static protected function ResetSession()
  110. {
  111. // Unset all of the session variables.
  112. $_SESSION = array();
  113. // If it's desired to kill the session, also delete the session cookie.
  114. // Note: This will destroy the session, and not just the session data!
  115. if (isset($_COOKIE[session_name()]))
  116. {
  117. setcookie(session_name(), '', time()-3600, '/');
  118. }
  119. // Finally, destroy the session.
  120. session_destroy();
  121. }
  122. static function SecureConnectionRequired()
  123. {
  124. $oConfig = new Config(ITOP_CONFIG_FILE);
  125. return $oConfig->GetSecureConnectionRequired();
  126. }
  127. static function IsConnectionSecure()
  128. {
  129. $bSecured = false;
  130. if ( !empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!= 'off') )
  131. {
  132. $bSecured = true;
  133. }
  134. return $bSecured;
  135. }
  136. static function DoLogin()
  137. {
  138. if (self::SecureConnectionRequired() && !self::IsConnectionSecure())
  139. {
  140. // Non secured URL... redirect to a secured one
  141. $sUrl = Utils::GetAbsoluteUrl(true /* query string */, true /* force HTTPS */);
  142. header("Location: $sUrl");
  143. exit;
  144. }
  145. $bHTTPBasicAuthentication = (utils::ReadParam('auth', '', 'get') == 'http_basic');
  146. if ($bHTTPBasicAuthentication)
  147. {
  148. // Basic HTTP/PHP authentication mecanism
  149. //
  150. // meme avec ca c'est pourri - return;
  151. if (!isset($_SERVER['PHP_AUTH_USER']))
  152. {
  153. header('WWW-Authenticate: Basic realm="iTop access is restricted"');
  154. header('HTTP/1.0 401 Unauthorized');
  155. // Note: accessed when the user will click on Cancel
  156. echo '<p><strong>'.Dict::S('UI:Login:Error:AccessRestricted').'</strong></p>';
  157. exit;
  158. }
  159. else
  160. {
  161. $sAuthUser = $_SERVER['PHP_AUTH_USER'];
  162. $sAuthPwd = $_SERVER['PHP_AUTH_PW'];
  163. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  164. {
  165. header('WWW-Authenticate: Basic realm="Unknown user \''.$sAuthUser.'\'"');
  166. header('HTTP/1.0 401 Unauthorized');
  167. // Note: accessed when the user will click on Cancel
  168. // Todo: count the attempts
  169. echo '<p><strong>'.Dict::S('UI:Login:Error:AccessRestricted').'</strong></p>';
  170. exit;
  171. }
  172. }
  173. return;
  174. }
  175. // Home-made authentication mecanism
  176. //
  177. $operation = utils::ReadParam('loginop', '');
  178. session_start();
  179. if ($operation == 'logoff')
  180. {
  181. self::ResetSession();
  182. }
  183. if (!isset($_SESSION['auth_user']) || !isset($_SESSION['auth_pwd']))
  184. {
  185. if ($operation == 'loginurl')
  186. {
  187. $sAuthUser = utils::ReadParam('auth_user', '', 'get');
  188. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'get');
  189. }
  190. else if ($operation == 'login')
  191. {
  192. $sAuthUser = utils::ReadParam('auth_user', '', 'post');
  193. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'post');
  194. }
  195. else
  196. {
  197. $oPage = new LoginWebPage();
  198. $oPage->DisplayLoginForm();
  199. $oPage->output();
  200. exit;
  201. }
  202. }
  203. else
  204. {
  205. $sAuthUser = $_SESSION['auth_user'];
  206. $sAuthPwd = $_SESSION['auth_pwd'];
  207. }
  208. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  209. {
  210. self::ResetSession();
  211. $oPage = new LoginWebPage();
  212. $oPage->DisplayLoginForm( true /* failed attempt */);
  213. $oPage->output();
  214. exit;
  215. }
  216. else
  217. {
  218. $_SESSION['auth_user'] = $sAuthUser ;
  219. $_SESSION['auth_pwd'] = $sAuthPwd;
  220. }
  221. }
  222. } // End of class
  223. ?>