loginwebpage.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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($sLoginType, $bFailedLogin = false)
  84. {
  85. switch($sLoginType)
  86. {
  87. case 'basic':
  88. case 'url':
  89. $this->add_header('WWW-Authenticate: Basic realm="'.Dict::Format('UI:iTopVersion:Short', ITOP_VERSION));
  90. $this->add_header('HTTP/1.0 401 Unauthorized');
  91. // Note: displayed when the user will click on Cancel
  92. $this->add('<p><strong>'.Dict::S('UI:Login:Error:AccessRestricted').'</strong></p>');
  93. break;
  94. case 'external':
  95. case 'form':
  96. default: // In case the settings get messed up...
  97. $sAuthUser = utils::ReadParam('auth_user', '');
  98. $sAuthPwd = utils::ReadParam('suggest_pwd', '');
  99. $sVersionShort = Dict::Format('UI:iTopVersion:Short', ITOP_VERSION);
  100. $this->add("<div id=\"login-logo\"><a href=\"http://www.combodo.com/itop\"><img title=\"$sVersionShort\" src=\"../images/itop-logo.png\"></a></div>\n");
  101. $this->add("<div id=\"login\">\n");
  102. $this->add("<h1>".Dict::S('UI:Login:Welcome')."</h1>\n");
  103. if ($bFailedLogin)
  104. {
  105. $this->add("<p class=\"hilite\">".Dict::S('UI:Login:IncorrectLoginPassword')."</p>\n");
  106. }
  107. else
  108. {
  109. $this->add("<p>".Dict::S('UI:Login:IdentifyYourself')."</p>\n");
  110. }
  111. $this->add("<form method=\"post\">\n");
  112. $this->add("<table>\n");
  113. $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");
  114. $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");
  115. $this->add("<tr><td colspan=\"2\" class=\"center v-spacer\"> <input type=\"submit\" value=\"".Dict::S('UI:Button:Login')."\" /></td></tr>\n");
  116. $this->add("</table>\n");
  117. $this->add("<input type=\"hidden\" name=\"loginop\" value=\"login\" />\n");
  118. $this->add("</form>\n");
  119. $this->add("</div>\n");
  120. break;
  121. }
  122. }
  123. public function DisplayChangePwdForm($bFailedLogin = false)
  124. {
  125. $sAuthUser = utils::ReadParam('auth_user', '');
  126. $sAuthPwd = utils::ReadParam('suggest_pwd', '');
  127. $sVersionShort = Dict::Format('UI:iTopVersion:Short', ITOP_VERSION);
  128. $sInconsistenPwdMsg = Dict::S('UI:Login:RetypePwdDoesNotMatch');
  129. $this->add_script(<<<EOF
  130. function GoBack()
  131. {
  132. window.history.back();
  133. }
  134. function DoCheckPwd()
  135. {
  136. if ($('#new_pwd').val() != $('#retype_new_pwd').val())
  137. {
  138. alert('$sInconsistenPwdMsg');
  139. return false;
  140. }
  141. return true;
  142. }
  143. EOF
  144. );
  145. $this->add("<div id=\"login-logo\"><a href=\"http://www.combodo.com/itop\"><img title=\"$sVersionShort\" src=\"../images/itop-logo.png\"></a></div>\n");
  146. $this->add("<div id=\"login\">\n");
  147. $this->add("<h1>".Dict::S('UI:Login:ChangeYourPassword')."</h1>\n");
  148. if ($bFailedLogin)
  149. {
  150. $this->add("<p class=\"hilite\">".Dict::S('UI:Login:IncorrectOldPassword')."</p>\n");
  151. }
  152. $this->add("<form method=\"post\">\n");
  153. $this->add("<table>\n");
  154. $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");
  155. $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");
  156. $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");
  157. $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\" onClick=\"return DoCheckPwd();\" value=\"".Dict::S('UI:Button:ChangePassword')."\" /></td></tr>\n");
  158. $this->add("</table>\n");
  159. $this->add("<input type=\"hidden\" name=\"loginop\" value=\"do_change_pwd\" />\n");
  160. $this->add("</form>\n");
  161. $this->add("</div>\n");
  162. }
  163. static function ResetSession()
  164. {
  165. if (isset($_SESSION['login_mode']))
  166. {
  167. $sPreviousLoginMode = $_SESSION['login_mode'];
  168. }
  169. else
  170. {
  171. $sPreviousLoginMode = '';
  172. }
  173. // Unset all of the session variables.
  174. $_SESSION = array();
  175. // If it's desired to kill the session, also delete the session cookie.
  176. // Note: This will destroy the session, and not just the session data!
  177. if (isset($_COOKIE[session_name()]))
  178. {
  179. setcookie(session_name(), '', time()-3600, '/');
  180. }
  181. // Finally, destroy the session.
  182. session_destroy();
  183. }
  184. static function SecureConnectionRequired()
  185. {
  186. return utils::GetConfig()->GetSecureConnectionRequired();
  187. }
  188. static function IsConnectionSecure()
  189. {
  190. $bSecured = false;
  191. if ( !empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!= 'off') )
  192. {
  193. $bSecured = true;
  194. }
  195. return $bSecured;
  196. }
  197. protected static function Login()
  198. {
  199. if (self::SecureConnectionRequired() && !self::IsConnectionSecure())
  200. {
  201. // Non secured URL... redirect to a secured one
  202. $sUrl = Utils::GetAbsoluteUrl(true /* query string */, true /* force HTTPS */);
  203. header("Location: $sUrl");
  204. exit;
  205. }
  206. $aAllowedLoginTypes = utils::GetConfig()->GetAllowedLoginTypes();
  207. if (isset($_SESSION['auth_user']))
  208. {
  209. //echo "User: ".$_SESSION['auth_user']."\n";
  210. // Already authentified
  211. UserRights::Login($_SESSION['auth_user']); // Login & set the user's language
  212. return true;
  213. }
  214. else
  215. {
  216. $index = 0;
  217. $sLoginMode = '';
  218. $sAuthentication = 'internal';
  219. while(($sLoginMode == '') && ($index < count($aAllowedLoginTypes)))
  220. {
  221. $sLoginType = $aAllowedLoginTypes[$index];
  222. switch($sLoginType)
  223. {
  224. case 'form':
  225. // iTop standard mode: form based authentication
  226. $sAuthUser = utils::ReadParam('auth_user', '', 'post');
  227. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'post');
  228. if ($sAuthUser != '')
  229. {
  230. $sLoginMode = 'form';
  231. }
  232. break;
  233. case 'basic':
  234. // Standard PHP authentication method, works with Apache...
  235. // Case 1) Apache running in CGI mode + rewrite rules in .htaccess
  236. if (isset($_SERVER['HTTP_AUTHORIZATION']) && !empty($_SERVER['HTTP_AUTHORIZATION']))
  237. {
  238. list($sAuthUser, $sAuthPwd) = explode(':' , base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
  239. $sLoginMode = 'basic';
  240. }
  241. else if (isset($_SERVER['PHP_AUTH_USER']))
  242. {
  243. $sAuthUser = $_SERVER['PHP_AUTH_USER'];
  244. $sAuthPwd = $_SERVER['PHP_AUTH_PW'];
  245. $sLoginMode = 'basic';
  246. }
  247. break;
  248. case 'external':
  249. // Web server supplied authentication
  250. $bExternalAuth = false;
  251. $sExtAuthVar = utils::GetConfig()->GetExternalAuthenticationVariable(); // In which variable is the info passed ?
  252. $sEval = '$bExternalAuth = isset('.$sExtAuthVar.');';
  253. eval($sEval);
  254. if ($bExternalAuth)
  255. {
  256. eval('$sAuthUser = '.$sExtAuthVar.';'); // Retrieve the value
  257. $sAuthPwd = ''; // No password in this case the web server already authentified the user...
  258. $sLoginMode = 'external';
  259. $sAuthentication = 'external';
  260. }
  261. break;
  262. case 'url':
  263. // Credentials passed directly in the url
  264. $sAuthUser = utils::ReadParam('auth_user', '', 'get');
  265. if ($sAuthUser != '')
  266. {
  267. $sAuthPwd = utils::ReadParam('auth_pwd', '', 'post');
  268. $sLoginMode = 'url';
  269. }
  270. break;
  271. }
  272. $index++;
  273. }
  274. //echo "\nsLoginMode: $sLoginMode (user: $sAuthUser / pwd: $sAuthPwd\n)";
  275. if ($sLoginMode == '')
  276. {
  277. // First connection
  278. $sDesiredLoginMode = utils::ReadParam('login_mode');
  279. if (in_array($sDesiredLoginMode, $aAllowedLoginTypes))
  280. {
  281. $sLoginMode = $sDesiredLoginMode;
  282. }
  283. else
  284. {
  285. $sLoginMode = $aAllowedLoginTypes[0]; // First in the list...
  286. }
  287. $oPage = new LoginWebPage();
  288. $oPage->DisplayLoginForm( $sLoginMode, false /* no previous failed attempt */);
  289. $oPage->output();
  290. exit;
  291. }
  292. else
  293. {
  294. if (!UserRights::CheckCredentials($sAuthUser, $sAuthPwd, $sAuthentication))
  295. {
  296. self::ResetSession();
  297. $oPage = new LoginWebPage();
  298. $oPage->DisplayLoginForm( $sLoginMode, true /* failed attempt */);
  299. $oPage->output();
  300. exit;
  301. }
  302. else
  303. {
  304. // User is Ok, let's save it in the session and proceed with normal login
  305. UserRights::Login($sAuthUser, $sAuthentication); // Login & set the user's language
  306. $_SESSION['auth_user'] = $sAuthUser;
  307. $_SESSION['login_mode'] = $sLoginMode;
  308. }
  309. }
  310. }
  311. }
  312. /**
  313. * Check if the user is already authentified, if yes, then performs some additional validations:
  314. * - if $bMustBeAdmin is true, then the user must be an administrator, otherwise an error is displayed
  315. * - if $bIsAllowedToPortalUsers is false and the user has only access to the portal, then the user is redirected to the portal
  316. * @param bool $bMustBeAdmin Whether or not the user must be an admin to access the current page
  317. * @param bool $bIsAllowedToPortalUsers Whether or not the current page is considered as part of the portal
  318. */
  319. static function DoLogin($bMustBeAdmin = false, $bIsAllowedToPortalUsers = false)
  320. {
  321. $operation = utils::ReadParam('loginop', '');
  322. session_start();
  323. if ($operation == 'logoff')
  324. {
  325. if (isset($_SESSION['login_mode']))
  326. {
  327. $sLoginMode = $_SESSION['login_mode'];
  328. }
  329. else
  330. {
  331. $aAllowedLoginTypes = utils::GetConfig()->GetAllowedLoginTypes();
  332. if (count($aAllowedLoginTypes) > 0)
  333. {
  334. $sLoginMode = $aAllowedLoginTypes[0];
  335. }
  336. else
  337. {
  338. $sLoginMode = 'form';
  339. }
  340. }
  341. self::ResetSession();
  342. $oPage = new LoginWebPage();
  343. $oPage->DisplayLoginForm( $sLoginMode, false /* not a failed attempt */);
  344. $oPage->output();
  345. exit;
  346. }
  347. else if ($operation == 'change_pwd')
  348. {
  349. $sAuthUser = $_SESSION['auth_user'];
  350. UserRights::Login($sAuthUser); // Set the user's language
  351. $oPage = new LoginWebPage();
  352. $oPage->DisplayChangePwdForm();
  353. $oPage->output();
  354. exit;
  355. }
  356. if ($operation == 'do_change_pwd')
  357. {
  358. $sAuthUser = $_SESSION['auth_user'];
  359. UserRights::Login($sAuthUser); // Set the user's language
  360. $sOldPwd = utils::ReadPostedParam('old_pwd');
  361. $sNewPwd = utils::ReadPostedParam('new_pwd');
  362. if (UserRights::CanChangePassword() && ((!UserRights::CheckCredentials($sAuthUser, $sOldPwd)) || (!UserRights::ChangePassword($sOldPwd, $sNewPwd))))
  363. {
  364. $oPage = new LoginWebPage();
  365. $oPage->DisplayChangePwdForm(true); // old pwd was wrong
  366. $oPage->output();
  367. }
  368. }
  369. self::Login();
  370. if ($bMustBeAdmin && !UserRights::IsAdministrator())
  371. {
  372. require_once('../setup/setuppage.class.inc.php');
  373. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  374. $oP->add("<h1>".Dict::S('UI:Login:Error:AccessAdmin')."</h1>\n");
  375. $oP->p("<a href=\"../pages/logoff.php\">".Dict::S('UI:LogOffMenu')."</a>");
  376. $oP->output();
  377. exit;
  378. }
  379. elseif ( (!$bIsAllowedToPortalUsers) && (UserRights::IsPortalUser()))
  380. {
  381. // No rights to be here, redirect to the portal
  382. header('Location: ../portal/index.php');
  383. }
  384. }
  385. } // End of class
  386. ?>