Initial revision
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_beuserauth.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skårhøj (kasper@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 <kasper@typo3.com>
33 * @package TYPO3
34 * @subpackage t3lib
35 * @internal
36 */
37 /**
38 * [CLASS/FUNCTION INDEX of SCRIPT]
39 *
40 *
41 *
42 * 74: class t3lib_beUserAuth extends t3lib_userAuthGroup
43 * 148: function trackBeUser($flag)
44 * 161: function checkLockToIP()
45 * 181: function backendCheckLogin()
46 * 206: function backendSetUC()
47 * 241: function overrideUC()
48 * 251: function resetUC()
49 * 264: function emailAtLogin()
50 * 316: function veriCode()
51 *
52 * TOTAL FUNCTIONS: 8
53 * (This index is automatically created/updated by the extension "extdeveval")
54 *
55 */
56
57
58
59
60
61
62
63
64
65
66 /**
67 * TYPO3 user authentication, backend
68 * Could technically have been the same class as t3lib_userauthgroup since these two are always used together and only together.
69 * 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.
70 * This class contains the configuration of the database fields used plus some functions for the authentication process of backend users.
71 *
72 * @author Kasper Skårhøj <kasper@typo3.com>
73 */
74 class t3lib_beUserAuth extends t3lib_userAuthGroup {
75 var $session_table = 'be_sessions'; // Table to use for session data.
76 var $name = 'be_typo_user'; // Session/Cookie name
77
78 var $user_table = 'be_users'; // Table in database with userdata
79 var $username_column = 'username'; // Column for login-name
80 var $userident_column = 'password'; // Column for password
81 var $userid_column = 'uid'; // Column for user-id
82 var $lastLogin_column = 'lastlogin';
83 var $notifyHeader = 'From: TYPO3 Login notify <no_reply@no_reply.no_reply>';
84
85 var $enablecolumns = Array (
86 'rootLevel' => 1,
87 'deleted' => 'deleted',
88 'disabled' => 'disable',
89 'starttime' => 'starttime',
90 'endtime' => 'endtime'
91 );
92
93 var $formfield_uname = 'username'; // formfield with login-name
94 var $formfield_uident = 'userident'; // formfield with password
95 var $formfield_chalvalue = 'challenge'; // formfield with a unique value which is used to encrypt the password and username
96 var $formfield_status = 'login_status'; // formfield with status: *'login', 'logout'
97 var $security_level = 'challenged'; // sets the level of security. *'normal' = clear-text. 'challenged' = hashed password/username from form in $formfield_uident. 'superchallenged' = hashed password hashed again with username.
98
99 var $writeStdLog = 1; // Decides if the writelog() function is called at login and logout
100 var $writeAttemptLog = 1; // If the writelog() functions is called if a login-attempt has be tried without success
101
102 var $auth_include = ''; // this is the name of the include-file containing the login form. If not set, login CAN be anonymous. If set login IS needed.
103
104 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.
105 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.
106
107
108
109 // User Config:
110 var $uc;
111
112 // User Config Default values:
113 /*
114 Reserved keys for other storage of session data:
115 moduleData
116 moduleSessionID
117 */
118 var $uc_default = Array (
119 // 'lang' => 'dk', // this value will be transferred from $BE_USER->user[lang] if not set...
120 'interfaceSetup' => '', // serialized content that is used to store interface pane and menu positions. Set by the logout.php-script
121 'moduleData' => Array(), // user-data for the modules
122 'thumbnailsByDefault' => 0,
123 'emailMeAtLogin' => 0,
124 'condensedMode' => 0,
125 'noMenuMode' => 0,
126 'startInTaskCenter' => 0,
127 'localFrameEdit' => 0,
128 'dontEditInPageModule' => 0,
129 'hideSubmoduleIcons' => 0,
130 'helpText' => 1,
131 'titleLen' => 30,
132 'edit_wideDocument' => '0',
133 'edit_showFieldHelp' => 'icon',
134 'edit_RTE' => '1',
135 'edit_docModuleUpload' => '1',
136 'disableCMlayers' => 0,
137 'navFrameWidth' => '', // Default is 245 pixels
138 );
139
140
141 /**
142 * If flag is set and the extensions 'beuser_tracking' is loaded, this will insert a table row with the REQUEST_URI of current script - thus tracking the scripts the backend users uses...
143 *
144 * @param boolean
145 * @return void
146 * @access private
147 */
148 function trackBeUser($flag) {
149 if ($flag && t3lib_extMgm::isLoaded('beuser_tracking')) {
150 $query = 'INSERT INTO sys_trackbeuser (userid,tstamp,script) VALUES ('.$this->user['uid'].','.time().',"'.addslashes(t3lib_div::getIndpEnv('REQUEST_URI')).'")';
151 $res = mysql(TYPO3_db,$query);
152 }
153 }
154
155 /**
156 * 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)
157 *
158 * @return boolean True, if IP address validates OK (or no check is done at all)
159 * @access private
160 */
161 function checkLockToIP() {
162 global $TYPO3_CONF_VARS;
163 $out=1;
164 if ($TYPO3_CONF_VARS['BE']['enabledBeUserIPLock']) {
165 $IPList = $this->getTSConfigVal('options.lockToIP');
166 if (trim($IPList)) {
167 $baseIP = t3lib_div::getIndpEnv('REMOTE_ADDR');
168 $out=t3lib_div::cmpIP($baseIP, $IPList);
169 }
170 }
171 return $out;
172 }
173
174 /**
175 * 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.
176 * 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.
177 * This function is called right after ->start() in fx. init.php
178 *
179 * @return void
180 */
181 function backendCheckLogin() {
182 if (!$this->user['uid']) {
183 if (!defined('TYPO3_PROCEED_IF_NO_USER') || !TYPO3_PROCEED_IF_NO_USER) {
184 t3lib_BEfunc::typo3PrintError ('Login-error','No user logged in! Sorry, I can\'t proceed then!<br /><br />(You must have cookies enabled!)',0);
185 exit;
186 }
187 } else { // ...and if that's the case, call these functions
188 $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
189 if ($this->checkLockToIP()) {
190 $this->backendSetUC(); // Setting the UC array. It's needed with fetchGroupData first, due to default/overriding of values.
191 $this->emailAtLogin(); // email at login - if option set.
192 } else {
193 t3lib_BEfunc::typo3PrintError ('Login-error','IP locking prevented you from being authorized. Can\'t proceed, sorry.',0);
194 exit;
195 }
196 }
197 }
198
199 /**
200 * Initialize the internal ->uc array for the backend user
201 * Will make the overrides if necessary, and write the UC back to the be_users record if changes has happend
202 *
203 * @return void
204 * @internal
205 */
206 function backendSetUC() {
207 // UC - user configuration is a serialized array inside the userobject
208 $temp_theSavedUC=unserialize($this->user['uc']); // if there is a saved uc we implement that instead of the default one.
209 if (is_array($temp_theSavedUC)) {
210 $this->unpack_uc($temp_theSavedUC);
211 }
212 // Setting defaults if uc is empty
213 if (!is_array($this->uc)) {
214 $this->uc = array_merge($this->uc_default,$TYPO3_CONF_VARS['BE']['defaultUC'],$this->getTSConfigProp('setup.default')); // Candidate for t3lib_div::array_merge() if integer-keys will some day make trouble...
215 $this->overrideUC();
216 $U=1;
217 }
218 // If TSconfig is updated, update the defaultUC.
219 if ($this->userTSUpdated) {
220 $this->overrideUC();
221 $U=1;
222 }
223 // Setting default lang from be_user record.
224 if (!isset($this->uc['lang'])) {
225 $this->uc['lang']=$this->user['lang'];
226 $U=1;
227 }
228 // Saving if updated.
229 if ($U) {
230 $this->writeUC(); // Method from the t3lib_userauth class.
231 }
232 }
233
234 /**
235 * Override: Call this function every time the uc is updated.
236 * That is 1) by reverting to default values, 2) in the setup-module, 3) userTS changes (userauthgroup)
237 *
238 * @return void
239 * @internal
240 */
241 function overrideUC() {
242 $this->uc = array_merge($this->uc,$this->getTSConfigProp('setup.override')); // Candidate for t3lib_div::array_merge() if integer-keys will some day make trouble...
243 }
244
245 /**
246 * Clears the user[uc] and ->uc to blank strings. Then calls ->backendSetUC() to fill it again with reset contents
247 *
248 * @return void
249 * @internal
250 */
251 function resetUC() {
252 $this->user['uc']='';
253 $this->uc='';
254 $this->backendSetUC();
255 }
256
257 /**
258 * Will send an email notification to warning_email_address/the login users email address when a login session is just started.
259 * Depends on various parameters whether mails are send and to whom.
260 *
261 * @return void
262 * @access private
263 */
264 function emailAtLogin() {
265 if ($this->loginSessionStarted) {
266 // Send notify-mail
267 $subject = 'At "'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'].'"'.
268 ' from '.t3lib_div::getIndpEnv('REMOTE_ADDR').
269 (t3lib_div::getIndpEnv('REMOTE_HOST') ? ' ('.t3lib_div::getIndpEnv('REMOTE_HOST').')' : '');
270 $msg = sprintf ('User "%s" logged in from %s (%s) at "%s" (%s)',
271 $this->user['username'],
272 t3lib_div::getIndpEnv('REMOTE_ADDR'),
273 t3lib_div::getIndpEnv('REMOTE_HOST'),
274 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
275 t3lib_div::getIndpEnv('HTTP_HOST')
276 );
277
278 // Warning email address
279 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr']) {
280 $warn=0;
281 $prefix='';
282 if (intval($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode']) & 1) { // first bit: All logins
283 $warn=1;
284 $prefix= $this->isAdmin() ? '[AdminLoginWarning]' : '[LoginWarning]';
285 }
286 if ($this->isAdmin() && (intval($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode']) & 2)) { // second bit: Only admin-logins
287 $warn=1;
288 $prefix='[AdminLoginWarning]';
289 }
290 if ($warn) {
291 mail($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'],
292 $prefix.' '.$subject,
293 $msg,
294 $this->notifyHeader
295 );
296 }
297 }
298
299 // If An email should be sent to the current user, do that:
300 if ($this->uc['emailMeAtLogin'] && strstr($this->user['email'],'@')) {
301 mail($this->user['email'],
302 $subject,
303 $msg,
304 $this->notifyHeader
305 );
306 }
307 }
308 }
309
310 /**
311 * VeriCode returns 10 first chars of a md5 hash of the session cookie AND the encryptionKey from TYPO3_CONF_VARS.
312 * This code is used as an alternative verification when the JavaScript interface executes cmd's to tce_db.php from eg. MSIE 5.0 because the proper referer is not passed with this browser...
313 *
314 * @return string
315 */
316 function veriCode() {
317 return substr(md5($this->id.$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']),0,10);
318 }
319 }
320
321
322
323
324 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_beuserauth.php']) {
325 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_beuserauth.php']);
326 }
327 ?>