[TASK] Change "false" into "FALSE"
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_beuserauth.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains class for TYPO3 backend user authentication
29 *
30 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 * @internal
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 76: class t3lib_beUserAuth extends t3lib_userAuthGroup
41 * 150: function trackBeUser($flag)
42 * 168: function checkLockToIP()
43 * 188: function backendCheckLogin()
44 * 216: function checkCLIuser()
45 * 240: function backendSetUC()
46 * 278: function overrideUC()
47 * 288: function resetUC()
48 * 301: function emailAtLogin()
49 * 353: function veriCode()
50 *
51 * TOTAL FUNCTIONS: 9
52 * (This index is automatically created/updated by the extension "extdeveval")
53 *
54 */
55
56
57 /**
58 * TYPO3 user authentication, backend
59 * Could technically have been the same class as t3lib_userauthgroup since these two are always used together and only together.
60 * t3lib_userauthgroup contains most of the functions used for checking permissions, authenticating users, setting up the user etc. This class is most interesting in terms of an API for user from outside.
61 * This class contains the configuration of the database fields used plus some functions for the authentication process of backend users.
62 *
63 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
64 * @package TYPO3
65 * @subpackage t3lib
66 */
67 class t3lib_beUserAuth extends t3lib_userAuthGroup {
68 var $session_table = 'be_sessions'; // Table to use for session data.
69 var $name = 'be_typo_user'; // Session/Cookie name
70
71 var $user_table = 'be_users'; // Table in database with userdata
72 var $username_column = 'username'; // Column for login-name
73 var $userident_column = 'password'; // Column for password
74 var $userid_column = 'uid'; // Column for user-id
75 var $lastLogin_column = 'lastlogin';
76
77 var $enablecolumns = Array(
78 'rootLevel' => 1,
79 'deleted' => 'deleted',
80 'disabled' => 'disable',
81 'starttime' => 'starttime',
82 'endtime' => 'endtime'
83 );
84
85 var $formfield_uname = 'username'; // formfield with login-name
86 var $formfield_uident = 'userident'; // formfield with password
87 var $formfield_chalvalue = 'challenge'; // formfield with a unique value which is used to encrypt the password and username
88 var $formfield_status = 'login_status'; // formfield with status: *'login', 'logout'
89
90 var $writeStdLog = 1; // Decides if the writelog() function is called at login and logout
91 var $writeAttemptLog = 1; // If the writelog() functions is called if a login-attempt has be tried without success
92
93 var $auth_timeout_field = 6000; // if > 0 : session-timeout in seconds. if FALSE/<0 : no timeout. if string: The string is fieldname from the usertable where the timeout can be found.
94 var $lifetime = 0; // 0 = Session-cookies. If session-cookies, the browser will stop session when the browser is closed. Else it keeps the session for $lifetime seconds.
95 var $challengeStoredInCookie = TRUE;
96
97
98 // User Config:
99 var $uc;
100
101 // User Config Default values:
102 // The array may contain other fields for configuration. For this, see "setup" extension and "TSConfig" document (User TSconfig, "setup.[xxx]....")
103 /*
104 Reserved keys for other storage of session data:
105 moduleData
106 moduleSessionID
107 */
108 var $uc_default = Array(
109 'interfaceSetup' => '', // serialized content that is used to store interface pane and menu positions. Set by the logout.php-script
110 'moduleData' => Array(), // user-data for the modules
111 'thumbnailsByDefault' => 0,
112 'emailMeAtLogin' => 0,
113 'condensedMode' => 0,
114 'noMenuMode' => 0,
115 'startModule' => 'help_aboutmodules',
116 'hideSubmoduleIcons' => 0,
117 'helpText' => 1,
118 'titleLen' => 30,
119 'edit_wideDocument' => '0',
120 'edit_showFieldHelp' => 'icon',
121 'edit_RTE' => '1',
122 'edit_docModuleUpload' => '1',
123 'enableFlashUploader' => '1',
124 'disableCMlayers' => 0,
125 'navFrameWidth' => '', // Default is 245 pixels
126 'navFrameResizable' => 0,
127 'resizeTextareas' => 1,
128 'resizeTextareas_MaxHeight' => 300,
129 'resizeTextareas_Flexible' => 1,
130 );
131
132
133 /**
134 * Sets the security level for the Backend login
135 *
136 * @return void
137 */
138 function start() {
139 $securityLevel = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['loginSecurityLevel']);
140 $this->security_level = $securityLevel ? $securityLevel : 'superchallenged';
141
142 parent::start();
143 }
144
145 /**
146 * If TYPO3_CONF_VARS['BE']['enabledBeUserIPLock'] is enabled and an IP-list is found in the User TSconfig objString "options.lockToIP", then make an IP comparison with REMOTE_ADDR and return the outcome (TRUE/FALSE)
147 *
148 * @return boolean TRUE, if IP address validates OK (or no check is done at all)
149 * @access private
150 */
151 function checkLockToIP() {
152 global $TYPO3_CONF_VARS;
153 $out = 1;
154 if ($TYPO3_CONF_VARS['BE']['enabledBeUserIPLock']) {
155 $IPList = $this->getTSConfigVal('options.lockToIP');
156 if (trim($IPList)) {
157 $baseIP = t3lib_div::getIndpEnv('REMOTE_ADDR');
158 $out = t3lib_div::cmpIP($baseIP, $IPList);
159 }
160 }
161 return $out;
162 }
163
164 /**
165 * Check if user is logged in and if so, call ->fetchGroupData() to load group information and access lists of all kind, further check IP, set the ->uc array and send login-notification email if required.
166 * If no user is logged in the default behaviour is to exit with an error message, but this will happen ONLY if the constant TYPO3_PROCEED_IF_NO_USER is set TRUE.
167 * This function is called right after ->start() in fx. init.php
168 *
169 * @return void
170 */
171 function backendCheckLogin() {
172 if (!$this->user['uid']) {
173 if (!defined('TYPO3_PROCEED_IF_NO_USER') || !TYPO3_PROCEED_IF_NO_USER) {
174 t3lib_utility_Http::redirect($GLOBALS['BACK_PATH']);
175 }
176 } else { // ...and if that's the case, call these functions
177 $this->fetchGroupData(); // The groups are fetched and ready for permission checking in this initialization. Tables.php must be read before this because stuff like the modules has impact in this
178 if ($this->checkLockToIP()) {
179 if ($this->isUserAllowedToLogin()) {
180 $this->backendSetUC(); // Setting the UC array. It's needed with fetchGroupData first, due to default/overriding of values.
181 $this->emailAtLogin(); // email at login - if option set.
182 } else {
183 throw new RuntimeException('Login Error: TYPO3 is in maintenance mode at the moment. Only administrators are allowed access.', 1294585860);
184 }
185 } else {
186 throw new RuntimeException('Login Error: IP locking prevented you from being authorized. Can\'t proceed, sorry.', 1294585861);
187 }
188 }
189 }
190
191 /**
192 * If the backend script is in CLI mode, it will try to load a backend user named by the CLI module name (in lowercase)
193 *
194 * @return boolean Returns TRUE if a CLI user was loaded, otherwise FALSE!
195 */
196 function checkCLIuser() {
197 // First, check if cliMode is enabled:
198 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
199 if (!$this->user['uid']) {
200 if (substr($GLOBALS['MCONF']['name'], 0, 5) == '_CLI_') {
201 $userName = strtolower($GLOBALS['MCONF']['name']);
202 $this->setBeUserByName($userName);
203 if ($this->user['uid']) {
204 if (!$this->isAdmin()) {
205 return TRUE;
206 } else {
207 die('ERROR: CLI backend user "' . $userName . '" was ADMIN which is not allowed!' . LF . LF);
208 }
209 } else {
210 die('ERROR: No backend user named "' . $userName . '" was found! [Database: ' . TYPO3_db . ']' . LF . LF);
211 }
212 } else {
213 die('ERROR: Module name, "' . $GLOBALS['MCONF']['name'] . '", was not prefixed with "_CLI_"' . LF . LF);
214 }
215 } else {
216 die('ERROR: Another user was already loaded which is impossible in CLI mode!' . LF . LF);
217 }
218 }
219 }
220
221 /**
222 * Initialize the internal ->uc array for the backend user
223 * Will make the overrides if necessary, and write the UC back to the be_users record if changes has happend
224 *
225 * @return void
226 * @internal
227 */
228 function backendSetUC() {
229 global $TYPO3_CONF_VARS;
230
231 // UC - user configuration is a serialized array inside the userobject
232 $temp_theSavedUC = unserialize($this->user['uc']); // if there is a saved uc we implement that instead of the default one.
233 if (is_array($temp_theSavedUC)) {
234 $this->unpack_uc($temp_theSavedUC);
235 }
236 // Setting defaults if uc is empty
237 if (!is_array($this->uc)) {
238 $this->uc = array_merge(
239 $this->uc_default,
240 (array) $TYPO3_CONF_VARS['BE']['defaultUC'],
241 t3lib_div::removeDotsFromTS((array) $this->getTSConfigProp('setup.default'))
242 );
243 $this->overrideUC();
244 $U = 1;
245 }
246 // If TSconfig is updated, update the defaultUC.
247 if ($this->userTSUpdated) {
248 $this->overrideUC();
249 $U = 1;
250 }
251 // Setting default lang from be_user record.
252 if (!isset($this->uc['lang'])) {
253 $this->uc['lang'] = $this->user['lang'];
254 $U = 1;
255 }
256 // Setting the time of the first login:
257 if (!isset($this->uc['firstLoginTimeStamp'])) {
258 $this->uc['firstLoginTimeStamp'] = $GLOBALS['EXEC_TIME'];
259 $U = TRUE;
260 }
261
262 // Saving if updated.
263 if ($U) {
264 $this->writeUC(); // Method from the t3lib_userauth class.
265 }
266 }
267
268 /**
269 * Override: Call this function every time the uc is updated.
270 * That is 1) by reverting to default values, 2) in the setup-module, 3) userTS changes (userauthgroup)
271 *
272 * @return void
273 * @internal
274 */
275 function overrideUC() {
276 $this->uc = array_merge((array) $this->uc, (array) $this->getTSConfigProp('setup.override')); // Candidate for t3lib_div::array_merge() if integer-keys will some day make trouble...
277 }
278
279 /**
280 * Clears the user[uc] and ->uc to blank strings. Then calls ->backendSetUC() to fill it again with reset contents
281 *
282 * @return void
283 * @internal
284 */
285 function resetUC() {
286 $this->user['uc'] = '';
287 $this->uc = '';
288 $this->backendSetUC();
289 }
290
291 /**
292 * Will send an email notification to warning_email_address/the login users email address when a login session is just started.
293 * Depends on various parameters whether mails are send and to whom.
294 *
295 * @return void
296 * @access private
297 */
298 function emailAtLogin() {
299 if ($this->loginSessionStarted) {
300 // Send notify-mail
301 $subject = 'At "' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '"' .
302 ' from ' . t3lib_div::getIndpEnv('REMOTE_ADDR') .
303 (t3lib_div::getIndpEnv('REMOTE_HOST') ? ' (' . t3lib_div::getIndpEnv('REMOTE_HOST') . ')' : '');
304 $msg = sprintf('User "%s" logged in from %s (%s) at "%s" (%s)',
305 $this->user['username'],
306 t3lib_div::getIndpEnv('REMOTE_ADDR'),
307 t3lib_div::getIndpEnv('REMOTE_HOST'),
308 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
309 t3lib_div::getIndpEnv('HTTP_HOST')
310 );
311
312 // Warning email address
313 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr']) {
314 $warn = 0;
315 $prefix = '';
316 if (intval($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode']) & 1) { // first bit: All logins
317 $warn = 1;
318 $prefix = $this->isAdmin() ? '[AdminLoginWarning]' : '[LoginWarning]';
319 }
320 if ($this->isAdmin() && (intval($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode']) & 2)) { // second bit: Only admin-logins
321 $warn = 1;
322 $prefix = '[AdminLoginWarning]';
323 }
324 if ($warn) {
325 $from = t3lib_utility_Mail::getSystemFrom();
326 /** @var $mail t3lib_mail_Message */
327 $mail = t3lib_div::makeInstance('t3lib_mail_Message');
328 $mail->setTo($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'])
329 ->setFrom($from)
330 ->setSubject($prefix . ' ' . $subject)
331 ->setBody($msg);
332 $mail->send();
333 }
334 }
335
336 // If An email should be sent to the current user, do that:
337 if ($this->uc['emailMeAtLogin'] && strstr($this->user['email'], '@')) {
338 $from = t3lib_utility_Mail::getSystemFrom();
339 /** @var $mail t3lib_mail_Message */
340 $mail = t3lib_div::makeInstance('t3lib_mail_Message');
341 $mail->setTo($this->user['email'])
342 ->setFrom($from)
343 ->setSubject($subject)
344 ->setBody($msg);
345 $mail->send();
346 }
347 }
348 }
349
350 /**
351 * Determines whether a backend user is allowed to access the backend.
352 *
353 * The conditions are:
354 * + backend user is a regular user and adminOnly is not defined
355 * + backend user is an admin user
356 * + backend user is used in CLI context and adminOnly is explicitely set to "2"
357 *
358 * @return boolean Whether a backend user is allowed to access the backend
359 */
360 protected function isUserAllowedToLogin() {
361 $isUserAllowedToLogin = FALSE;
362 $adminOnlyMode = $GLOBALS['TYPO3_CONF_VARS']['BE']['adminOnly'];
363
364 // Backend user is allowed if adminOnly is not set or user is an admin:
365 if (!$adminOnlyMode || $this->isAdmin()) {
366 $isUserAllowedToLogin = TRUE;
367 // Backend user is allowed if adminOnly is set to 2 (CLI) and a CLI process is running:
368 } elseif ($adminOnlyMode == 2 && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
369 $isUserAllowedToLogin = TRUE;
370 }
371
372 return $isUserAllowedToLogin;
373 }
374
375 /**
376 * Logs out the current user and clears the form protection tokens.
377 */
378 public function logoff() {
379 if (isset($GLOBALS['BE_USER'])) {
380 t3lib_formProtection_Factory::get()->clean();
381 }
382 parent::logoff();
383 }
384 }
385
386
387 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_beuserauth.php'])) {
388 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_beuserauth.php']);
389 }
390
391 ?>