loginwebpage.class.inc.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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: 300px;
  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: 300px;
  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. public function DisplayChangePwdForm($bFailedLogin = false)
  110. {
  111. $sAuthUser = utils::ReadParam('auth_user', '');
  112. $sAuthPwd = utils::ReadParam('suggest_pwd', '');
  113. $sVersionShort = Dict::Format('UI:iTopVersion:Short', ITOP_VERSION);
  114. $this->add("<div id=\"login-logo\"><a href=\"http://www.combodo.com/itop\"><img title=\"$sVersionShort\" src=\"../images/itop-logo.png\"></a></div>\n");
  115. $this->add("<div id=\"login\">\n");
  116. $this->add("<h1>".Dict::S('UI:Login:ChangeYourPassword')."</h1>\n");
  117. if ($bFailedLogin)
  118. {
  119. $this->add("<p class=\"hilite\">".Dict::S('UI:Login:IncorrectOldPassword')."</p>\n");
  120. }
  121. $this->add("<form method=\"post\">\n");
  122. $this->add("<table>\n");
  123. $this->add("<tr><td><label for=\"old_pwd\">".Dict::S('UI:Login:OldPasswordPrompt').":</label></td><td><input type=\"password\" id=\"old_pwd\" name=\"old_pwd\" value=\"\" /></td></tr>\n");
  124. $this->add("<tr><td><label for=\"new_pwd\">".Dict::S('UI:Login:NewPasswordPrompt').":</label></td><td><input type=\"password\" id=\"new_pwd\" name=\"new_pwd\" value=\"\" /></td></tr>\n");
  125. $this->add("<tr><td><label for=\"retype_new_pwd\">".Dict::S('UI:Login:RetypeNewPasswordPrompt').":</label></td><td><input type=\"password\" id=\"retype_new_pwd\" name=\"retype_new_pwd\" value=\"\" /></td></tr>\n");
  126. $this->add("<tr><td colspan=\"2\" class=\"center v-spacer\"> <input type=\"button\" onClick=\"GoBack();\" value=\"".Dict::S('UI:Button:Cancel')."\" />&nbsp;&nbsp;<input type=\"submit\" value=\"".Dict::S('UI:Button:ChangePassword')."\" /></td></tr>\n");
  127. $this->add("</table>\n");
  128. $this->add("<input type=\"hidden\" name=\"loginop\" value=\"do_change_pwd\" />\n");
  129. $this->add("</form>\n");
  130. $this->add("</div>\n");
  131. }
  132. static protected function ResetSession()
  133. {
  134. // Unset all of the session variables.
  135. $_SESSION = array();
  136. // If it's desired to kill the session, also delete the session cookie.
  137. // Note: This will destroy the session, and not just the session data!
  138. if (isset($_COOKIE[session_name()]))
  139. {
  140. setcookie(session_name(), '', time()-3600, '/');
  141. }
  142. // Finally, destroy the session.
  143. session_destroy();
  144. }
  145. static function SecureConnectionRequired()
  146. {
  147. $oConfig = new Config(ITOP_CONFIG_FILE);
  148. return $oConfig->GetSecureConnectionRequired();
  149. }
  150. static function IsConnectionSecure()
  151. {
  152. $bSecured = false;
  153. if ( !empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!= 'off') )
  154. {
  155. $bSecured = true;
  156. }
  157. return $bSecured;
  158. }
  159. static function DoLogin()
  160. {
  161. if (self::SecureConnectionRequired() && !self::IsConnectionSecure())
  162. {
  163. // Non secured URL... redirect to a secured one
  164. $sUrl = Utils::GetAbsoluteUrl(true /* query string */, true /* force HTTPS */);
  165. header("Location: $sUrl");
  166. exit;
  167. }
  168. $bHTTPBasicAuthentication = (utils::ReadParam('auth', '', 'get') == 'http_basic');
  169. if ($bHTTPBasicAuthentication)
  170. {
  171. // Basic HTTP/PHP authentication mecanism
  172. //
  173. // meme avec ca c'est pourri - return;
  174. if (!isset($_SERVER['PHP_AUTH_USER']))
  175. {
  176. header('WWW-Authenticate: Basic realm="iTop access is restricted"');
  177. header('HTTP/1.0 401 Unauthorized');
  178. // Note: accessed when the user will click on Cancel
  179. echo '<p><strong>'.Dict::S('UI:Login:Error:AccessRestricted').'</strong></p>';
  180. exit;
  181. }
  182. else
  183. {
  184. $sAuthUser = $_SERVER['PHP_AUTH_USER'];
  185. $sAuthPwd = $_SERVER['PHP_AUTH_PW'];
  186. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  187. {
  188. header('WWW-Authenticate: Basic realm="Unknown user \''.$sAuthUser.'\'"');
  189. header('HTTP/1.0 401 Unauthorized');
  190. // Note: accessed when the user will click on Cancel
  191. // Todo: count the attempts
  192. echo '<p><strong>'.Dict::S('UI:Login:Error:AccessRestricted').'</strong></p>';
  193. exit;
  194. }
  195. }
  196. return;
  197. }
  198. // Home-made authentication mecanism
  199. //
  200. $operation = utils::ReadParam('loginop', '');
  201. session_start();
  202. if ($operation == 'logoff')
  203. {
  204. self::ResetSession();
  205. }
  206. if ($operation == 'change_pwd')
  207. {
  208. $oPage = new LoginWebPage();
  209. $oPage->DisplayChangePwdForm();
  210. $oPage->output();
  211. exit;
  212. }
  213. if ($operation == 'do_change_pwd')
  214. {
  215. $sAuthUser = $_SESSION['auth_user'];
  216. $sOldPwd = utils::ReadPostedParam('old_pwd');
  217. $sNewPwd = utils::ReadPostedParam('new_pwd');
  218. if (UserRights::CanChangePassword() && ((!UserRights::Login($sAuthUser, $sOldPwd)) || (!UserRights::ChangePassword($sOldPwd, $sNewPwd))))
  219. {
  220. $oPage = new LoginWebPage();
  221. $oPage->DisplayChangePwdForm(true); // old pwd was wrong
  222. $oPage->output();
  223. exit;
  224. }
  225. else
  226. {
  227. // Remember the changed password
  228. $_SESSION['auth_pwd'] = $sNewPwd;
  229. return;
  230. }
  231. }
  232. if (!isset($_SESSION['auth_user']) || !isset($_SESSION['auth_pwd']))
  233. {
  234. if ($operation == 'loginurl')
  235. {
  236. $sAuthUser = utils::ReadParam('auth_user', '', 'get');
  237. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'get');
  238. }
  239. else if ($operation == 'login')
  240. {
  241. $sAuthUser = utils::ReadParam('auth_user', '', 'post');
  242. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'post');
  243. }
  244. else
  245. {
  246. $oPage = new LoginWebPage();
  247. $oPage->DisplayLoginForm();
  248. $oPage->output();
  249. exit;
  250. }
  251. }
  252. else
  253. {
  254. $sAuthUser = $_SESSION['auth_user'];
  255. $sAuthPwd = $_SESSION['auth_pwd'];
  256. }
  257. if (!UserRights::Login($sAuthUser, $sAuthPwd))
  258. {
  259. self::ResetSession();
  260. $oPage = new LoginWebPage();
  261. $oPage->DisplayLoginForm( true /* failed attempt */);
  262. $oPage->output();
  263. exit;
  264. }
  265. else
  266. {
  267. $_SESSION['auth_user'] = $sAuthUser ;
  268. $_SESSION['auth_pwd'] = $sAuthPwd;
  269. }
  270. }
  271. } // End of class
  272. ?>