3358d4a73e99a69d706f632a2054d6f764e81b27
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_feuserauth.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 Kasper Skaarhoj (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 * Front End session user. Login and session data
29 * Included from index_ts.php
30 *
31 * $Id$
32 * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
33 *
34 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 77: class tslib_feUserAuth extends t3lib_userAuth
42 * 141: function fetchGroupData()
43 * 189: function getUserTSconf()
44 *
45 * SECTION: Session data management functions
46 * 234: function fetchSessionData()
47 * 256: function storeSessionData()
48 * 282: function getKey($type,$key)
49 * 307: function setKey($type,$key,$data)
50 * 332: function record_registration($recs)
51 *
52 * TOTAL FUNCTIONS: 7
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
68
69
70 /**
71 * Extension class for Front End User Authentication.
72 *
73 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
74 * @package TYPO3
75 * @subpackage tslib
76 */
77 class tslib_feUserAuth extends t3lib_userAuth {
78 var $session_table = 'fe_sessions'; // Table to use for session data.
79 var $name = 'fe_typo_user'; // Session/Cookie name
80 var $get_name = 'ftu'; // Session/GET-var name
81
82 var $user_table = 'fe_users'; // Table in database with userdata
83 var $username_column = 'username'; // Column for login-name
84 var $userident_column = 'password'; // Column for password
85 var $userid_column = 'uid'; // Column for user-id
86 var $lastLogin_column = 'lastlogin';
87
88 var $enablecolumns = Array (
89 'deleted' => 'deleted',
90 'disabled' => 'disable',
91 'starttime' => 'starttime',
92 'endtime' => 'endtime'
93 );
94 var $formfield_uname = 'user'; // formfield with login-name
95 var $formfield_uident = 'pass'; // formfield with password
96 var $formfield_chalvalue = 'challenge'; // formfield with a unique value which is used to encrypt the password and username
97 var $formfield_status = 'logintype'; // formfield with status: *'login', 'logout'
98 var $security_level = ''; // sets the level of security. *'normal' = clear-text. 'challenged' = hashed password/username from form in $formfield_uident. 'superchallenged' = hashed password hashed again with username.
99
100 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.
101
102 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.
103
104 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.
105 var $sendNoCacheHeaders = 0;
106 var $getFallBack = 1; // If this is set, authentication is also accepted by the _GET. Notice that the identification is NOT 128bit MD5 hash but reduced. This is done in order to minimize the size for mobile-devices, such as WAP-phones
107 var $hash_length = 10;
108 var $getMethodEnabled = 1; // Login may be supplied by url.
109
110 var $usergroup_column = 'usergroup';
111 var $usergroup_table = 'fe_groups';
112 var $groupData = Array(
113 'title' =>Array(),
114 'uid' =>Array(),
115 'pid' =>Array()
116 );
117 var $TSdataArray=array(); // Used to accumulate the TSconfig data of the user
118 var $userTS = array();
119 var $userTSUpdated=0;
120 var $showHiddenRecords=0;
121
122 // Session and user data:
123 /*
124 There are two types of data that can be stored: UserData and Session-Data. Userdata is for the login-user, and session-data for anyone viewing the pages.
125 'Keys' are keys in the internal dataarray of the data. When you get or set a key in one of the data-spaces (user or session) you decide the type of the variable (not object though)
126 'Reserved' keys are:
127 - 'recs': Array: Used to 'register' records, eg in a shopping basket. Structure: [recs][tablename][record_uid]=number
128 - sys: Reserved for TypoScript standard code.
129 */
130 var $sesData = Array();
131 var $sesData_change = 0;
132 var $userData_change = 0;
133
134
135 /**
136 * Will select all fe_groups records that the current fe_user is member of - and which groups are also allowed in the current domain.
137 * It also accumulates the TSconfig for the fe_user/fe_groups in ->TSdataArray
138 *
139 * @return integer Returns the number of usergroups for the frontend users (if the internal user record exists and the usergroup field contains a value)
140 */
141 function fetchGroupData() {
142 $this->TSdataArray = array();
143 $this->userTS = array();
144 $this->userTSUpdated = 0;
145
146 // Setting default configuration:
147 $this->TSdataArray[]=$GLOBALS['TYPO3_CONF_VARS']['FE']['defaultUserTSconfig'];
148
149 if (is_array($this->user) && $this->user['usergroup']) {
150 $groups = t3lib_div::intExplode(',',$this->user['usergroup']);
151 $list = implode(',',$groups);
152 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain=\''.t3lib_div::getIndpEnv('HTTP_HOST').'\')';
153 if (!$this->showHiddenRecords) $hiddenP = 'AND hidden=0 ';
154
155 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->usergroup_table, 'deleted=0 '.$hiddenP.'AND uid IN ('.$list.')'.$lockToDomain_SQL);
156 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
157 $this->groupData['title'][$row['uid']] = $row['title'];
158 $this->groupData['uid'][$row['uid']] = $row['uid'];
159 $this->groupData['pid'][$row['uid']] = $row['pid'];
160 $this->groupData['TSconfig'][$row['uid']] = $row['TSconfig'];
161 }
162
163 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
164 $GLOBALS['TYPO3_DB']->sql_free_result($res);
165 // TSconfig:
166 reset($groups);
167 while(list(,$TSuid)=each($groups)) {
168 $this->TSdataArray[]=$this->groupData['TSconfig'][$TSuid];
169 }
170 $this->TSdataArray[]=$this->user['TSconfig'];
171
172 // Sort information
173 ksort($this->groupData['title']);
174 ksort($this->groupData['uid']);
175 ksort($this->groupData['pid']);
176 return count($this->groupData['uid']);
177 } else {
178 return 0;
179 }
180 }
181 }
182
183 /**
184 * Returns the parsed TSconfig for the fe_user
185 * First time this function is called it will parse the TSconfig and store it in $this->userTS. Subsequent requests will not re-parse the TSconfig but simply return what is already in $this->userTS
186 *
187 * @return array TSconfig array for the fe_user
188 */
189 function getUserTSconf() {
190 if (!$this->userTSUpdated) {
191 // Parsing the user TS (or getting from cache)
192 $this->TSdataArray = t3lib_TSparser::checkIncludeLines_array($this->TSdataArray);
193 $userTS = implode(chr(10).'[GLOBAL]'.chr(10),$this->TSdataArray);
194 $parseObj = t3lib_div::makeInstance('t3lib_TSparser');
195 $parseObj->parse($userTS);
196 $this->userTS = $parseObj->setup;
197
198 $this->userTSUpdated=1;
199 }
200 return $this->userTS;
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 /*****************************************
220 *
221 * Session data management functions
222 *
223 ****************************************/
224
225 /**
226 * Fetches the session data for the user (from the fe_session_data table) based on the ->id of the current user-session.
227 * The session data is restored to $this->sesData
228 * 1/100 calls will also do a garbage collection.
229 *
230 * @return void
231 * @access private
232 * @see storeSessionData()
233 */
234 function fetchSessionData() {
235 // Gets SesData if any
236 if ($this->id) {
237 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'fe_session_data', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, 'fe_session_data'));
238 if ($sesDataRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres)) {
239 $this->sesData = unserialize($sesDataRow['content']);
240 }
241 }
242 // delete old data:
243 if ((rand()%100) <= 1) { // a possibility of 1 % for garbage collection.
244 $GLOBALS['TYPO3_DB']->exec_DELETEquery('fe_session_data', 'tstamp < '.intval(time()-3600*24)); // all data older than 24 hours are deleted.
245 }
246 }
247
248 /**
249 * Will write UC and session data.
250 * If the flag $this->userData_change has been set, the function ->writeUC is called (which will save persistent user session data)
251 * If the flag $this->sesData_change has been set, the fe_session_data table is updated with the content of $this->sesData (deleting any old record, inserting new)
252 *
253 * @return void
254 * @see fetchSessionData(), getKey(), setKey()
255 */
256 function storeSessionData() {
257 // Saves UC and SesData if changed.
258 if ($this->userData_change) {
259 $this->writeUC('');
260 }
261 if ($this->sesData_change) {
262 if ($this->id) {
263 $insertFields = array (
264 'hash' => $this->id,
265 'content' => serialize($this->sesData),
266 'tstamp' => time()
267 );
268 $GLOBALS['TYPO3_DB']->exec_DELETEquery('fe_session_data', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, 'fe_session_data'));
269 $GLOBALS['TYPO3_DB']->exec_INSERTquery('fe_session_data', $insertFields);
270 }
271 }
272 }
273
274 /**
275 * Returns session data for the fe_user; Either persistent data following the fe_users uid/profile (requires login) or current-session based (not available when browse is closed, but does not require login)
276 *
277 * @param string Session data type; Either "user" (persistent, bound to fe_users profile) or "ses" (temporary, bound to current session cookie)
278 * @param string Key from the data array to return; The session data (in either case) is an array ($this->uc / $this->sesData) and this value determines which key to return the value for.
279 * @return mixed Returns whatever value there was in the array for the key, $key
280 * @see setKey()
281 */
282 function getKey($type,$key) {
283 if ($key) {
284 switch($type) {
285 case 'user':
286 return $this->uc[$key];
287 break;
288 case 'ses':
289 return $this->sesData[$key];
290 break;
291 }
292 }
293 }
294
295 /**
296 * Saves session data, either persistent or bound to current session cookie. Please see getKey() for more details.
297 * When a value is set the flags $this->userData_change or $this->sesData_change will be set so that the final call to ->storeSessionData() will know if a change has occurred and needs to be saved to the database.
298 * Notice: The key "recs" is already used by the function record_registration() which stores table/uid=value pairs in that key. This is used for the shopping basket among other things.
299 * Notice: Simply calling this function will not save the data to the database! The actual saving is done in storeSessionData() which is called as some of the last things in index_ts.php. So if you exit before this point, nothing gets saved of course! And the solution is to call $GLOBALS['TSFE']->storeSessionData(); before you exit.
300 *
301 * @param string Session data type; Either "user" (persistent, bound to fe_users profile) or "ses" (temporary, bound to current session cookie)
302 * @param string Key from the data array to store incoming data in; The session data (in either case) is an array ($this->uc / $this->sesData) and this value determines in which key the $data value will be stored.
303 * @param mixed The data value to store in $key
304 * @return void
305 * @see setKey(), storeSessionData(), record_registration()
306 */
307 function setKey($type,$key,$data) {
308 if ($key) {
309 switch($type) {
310 case 'user':
311 if ($this->user['uid']) {
312 $this->uc[$key]=$data;
313 $this->userData_change=1;
314 }
315 break;
316 case 'ses':
317 $this->sesData[$key]=$data;
318 $this->sesData_change=1;
319 break;
320 }
321 }
322 }
323
324 /**
325 * Registration of records/"shopping basket" in session data
326 * This will take the input array, $recs, and merge into the current "recs" array found in the session data.
327 * If a change in the recs storage happens (which it probably does) the function setKey() is called in order to store the array again.
328 *
329 * @param array The data array to merge into/override the current recs values. The $recs array is constructed as [table]][uid] = scalar-value (eg. string/integer).
330 * @return void
331 */
332 function record_registration($recs) {
333 if ($recs['clear_all']) {
334 $this->setKey('ses','recs','');
335 }
336 $change=0;
337 $recs_array=$this->getKey('ses','recs');
338 reset($recs);
339 while(list($table,$data)=each($recs)) {
340 if (is_array($data)) {
341 reset($data);
342 while(list($rec_id,$value)=each($data)) {
343 if ($value != $recs_array[$table][$rec_id]) {
344 $recs_array[$table][$rec_id] = $value;
345 $change=1;
346 }
347 }
348 }
349 }
350 if ($change) {
351 $this->setKey('ses','recs',$recs_array);
352 }
353 }
354 }
355
356
357 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_feuserauth.php']) {
358 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_feuserauth.php']);
359 }
360 ?>