login.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /* $Id: login.php 3066 2007-05-29 21:11:04Z danez $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. // main.external
  16. require_once('inc/main.external.php');
  17. // init template-instance
  18. tmplInitializeInstance($cfg["default_theme"], "page.login.tmpl");
  19. // start session
  20. @session_start();
  21. // unregister globals
  22. if (@ini_get('register_globals')) {
  23. require_once('inc/functions/functions.compat.php');
  24. unregister_GLOBALS();
  25. }
  26. // already got a session ?
  27. if (isset($_SESSION['user'])) {
  28. @header("location: index.php?iid=index");
  29. exit();
  30. }
  31. // start ob
  32. @ob_start();
  33. // authentication
  34. $isLoginRequest = false;
  35. switch ($cfg['auth_type']) {
  36. case 3: /* Basic-Passthru */
  37. case 2: /* Basic-Auth */
  38. if ((isset($_SERVER['PHP_AUTH_USER'])) && (isset($_SERVER['PHP_AUTH_PW']))) {
  39. $user = strtolower($_SERVER['PHP_AUTH_USER']);
  40. $iamhim = addslashes($_SERVER['PHP_AUTH_PW']);
  41. $md5password = "";
  42. if ((!empty($user)) && (isset($iamhim)))
  43. $isLoginRequest = true;
  44. } else {
  45. @header('WWW-Authenticate: Basic realm="'. $cfg["auth_basic_realm"] .'"');
  46. @header('HTTP/1.0 401 Unauthorized');
  47. @ob_end_clean();
  48. exit();
  49. }
  50. break;
  51. case 1: /* Form-Auth + Cookie */
  52. $cookieDelim = '|';
  53. // check if login-request
  54. $isCookieLoginRequest = tfb_getRequestVar('docookielogin');
  55. if ($isCookieLoginRequest == "true") {
  56. $isLoginRequest = true;
  57. $user = strtolower(tfb_getRequestVar('username'));
  58. $iamhim = "";
  59. $md5password = tfb_getRequestVar('md5pass');
  60. // set new cookie
  61. setcookie("autologin", $user.$cookieDelim.$md5password, time() + 60 * 60 * 24 * 30);
  62. } else {
  63. // is a form-login-request ?
  64. $docookieloginnew = tfb_getRequestVar('docookieloginnew');
  65. if ($docookieloginnew == "true") {
  66. $isLoginRequest = true;
  67. $user = strtolower(tfb_getRequestVar('username'));
  68. $requestPW = tfb_getRequestVar('iamhim');
  69. $iamhim = addslashes($requestPW);
  70. $md5password = "";
  71. $setcookie = tfb_getRequestVar('setcookie');
  72. // set cookie if wanted
  73. if ($setcookie == "true")
  74. setcookie("autologin", $user.$cookieDelim.md5($requestPW), time() + 60 * 60 * 24 * 30);
  75. } else {
  76. // check if cookie-set
  77. if (isset($_COOKIE["autologin"])) {
  78. // cookie is set
  79. $tmpl->setvar('cookie_set', 1);
  80. $creds = explode($cookieDelim, $_COOKIE["autologin"]);
  81. $tmpl->setvar('cookieuser', $creds[0]);
  82. $tmpl->setvar('cookiepass', $creds[1]);
  83. }
  84. }
  85. }
  86. break;
  87. case 4: /* Form-Auth + Image-Validation */
  88. // Image class
  89. require_once('inc/classes/Image.php');
  90. $user = strtolower(tfb_getRequestVar('username'));
  91. $iamhim = addslashes(tfb_getRequestVar('iamhim'));
  92. $md5password = "";
  93. $isImageSupported = Image::isSupported();
  94. if (!empty($user)) {
  95. $isLoginRequest = true;
  96. // image-validation
  97. if ($isImageSupported) {
  98. $secCode = tfb_getRequestVar('security');
  99. $rndChk = tfb_getRequestVar('rnd_chk');
  100. if ($secCode !== loginImageCode($cfg["db_user"], $rndChk)) {
  101. // log this
  102. AuditAction($cfg["constants"]["access_denied"], "FAILED IMAGE-VALIDATION: ".$user);
  103. // flush credentials if sec-code-validation fails (-> login-failure)
  104. $user = "";
  105. $iamhim = "";
  106. }
  107. }
  108. }
  109. if ($isImageSupported) {
  110. $tmpl->setvar('imageSupported', 1);
  111. // rand
  112. mt_srand((double)microtime() * 1000000);
  113. $rnd = mt_rand(0, 1000000);
  114. $tmpl->setvar('rnd', $rnd);
  115. } else {
  116. $tmpl->setvar('imageSupported', 0);
  117. }
  118. break;
  119. case 0: /* Form-Based Auth Standard */
  120. default:
  121. $user = strtolower(tfb_getRequestVar('username'));
  122. $iamhim = addslashes(tfb_getRequestVar('iamhim'));
  123. $md5password = "";
  124. if (!empty($user))
  125. $isLoginRequest = true;
  126. break;
  127. }
  128. // process login if this is a login-request
  129. if ($isLoginRequest) {
  130. // First User check
  131. $next_loc = "index.php?iid=index";
  132. $sql = "SELECT count(*) FROM tf_users";
  133. $user_count = $db->GetOne($sql);
  134. if ($user_count == 0) {
  135. firstLogin($user, $iamhim);
  136. $next_loc = "admin.php?op=serverSettings";
  137. }
  138. // perform auth
  139. if (performAuthentication($user, $iamhim, $md5password) == 1) {
  140. @header("location: ".$next_loc);
  141. exit();
  142. } else {
  143. $tmpl->setvar('login_failed', 1);
  144. }
  145. }
  146. // defines
  147. $tmpl->setvar('auth_type', $cfg["auth_type"]);
  148. tmplSetTitleBar($cfg["pagetitle"], false);
  149. tmplSetFoot(false);
  150. tmplSetIidVars();
  151. $tmpl->setvar('iid', 'login');
  152. // parse template
  153. $tmpl->pparse();
  154. ?>