Initial revision
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_feuserauth.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 * Front End session user. Login and session data
29 * Included from index_ts.php
30 *
31 * Revised for TYPO3 3.6 June/2003 by Kasper Skårhøj
32 *
33 * @author Kasper Skårhøj <kasper@typo3.com>
34 * @package TYPO3
35 * @subpackage tslib
36 */
37 /**
38 * [CLASS/FUNCTION INDEX of SCRIPT]
39 *
40 *
41 *
42 * 76: class tslib_feUserAuth extends t3lib_userAuth
43 * 140: function fetchGroupData()
44 * 186: function getUserTSconf()
45 *
46 * SECTION: Session data management functions
47 * 231: function fetchSessionData()
48 * 257: function storeSessionData()
49 * 282: function getKey($type,$key)
50 * 307: function setKey($type,$key,$data)
51 * 332: function record_registration($recs)
52 *
53 * TOTAL FUNCTIONS: 7
54 * (This index is automatically created/updated by the extension "extdeveval")
55 *
56 */
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 /**
72 * Extension class for Front End User Authentication.
73 *
74 * @author Kasper Skårhøj <kasper@typo3.com>
75 */
76 class tslib_feUserAuth extends t3lib_userAuth {
77 var $session_table = 'fe_sessions'; // Table to use for session data.
78 var $name = 'fe_typo_user'; // Session/Cookie name
79 var $get_name = 'ftu'; // Session/GET-var name
80
81 var $user_table = 'fe_users'; // Table in database with userdata
82 var $username_column = 'username'; // Column for login-name
83 var $userident_column = 'password'; // Column for password
84 var $userid_column = 'uid'; // Column for user-id
85 var $lastLogin_column = 'lastlogin';
86
87 var $enablecolumns = Array (
88 'deleted' => 'deleted',
89 'disabled' => 'disable',
90 'starttime' => 'starttime',
91 'endtime' => 'endtime'
92 );
93 var $formfield_uname = 'user'; // formfield with login-name
94 var $formfield_uident = 'pass'; // 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 = 'logintype'; // formfield with status: *'login', 'logout'
97 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.
98
99 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.
100
101 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.
102
103 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.
104 var $sendNoCacheHeaders = 0;
105 var $getFallBack = 1; // If this is set, authentication is also accepted by the HTTP_GET_VARS. 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
106 var $hash_length = 10;
107 var $getMethodEnabled = 1; // Login may be supplied by url.
108
109 var $usergroup_column = 'usergroup';
110 var $usergroup_table = 'fe_groups';
111 var $groupData = Array(
112 'title' =>Array(),
113 'uid' =>Array(),
114 'pid' =>Array()
115 );
116 var $TSdataArray=array(); // Used to accumulate the TSconfig data of the user
117 var $userTS = array();
118 var $userTSUpdated=0;
119 var $showHiddenRecords=0;
120
121 // Session and user data:
122 /*
123 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.
124 '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)
125 'Reserved' keys are:
126 - 'recs': Array: Used to 'register' records, eg in a shopping basket. Structure: [recs][tablename][record_uid]=number
127 - sys: Reserved for TypoScript standard code.
128 */
129 var $sesData = Array();
130 var $sesData_change = 0;
131 var $userData_change = 0;
132
133
134 /**
135 * Will select all fe_groups records that the current fe_user is member of - and which groups are also allowed in the current domain.
136 * It also accumulates the TSconfig for the fe_user/fe_groups in ->TSdataArray
137 *
138 * @return integer Returns the number of usergroups for the frontend users (if the internal user record exists and the usergroup field contains a value)
139 */
140 function fetchGroupData() {
141 $this->TSdataArray=array();
142 $this->userTS = array();
143 $this->userTSUpdated=0;
144
145 // Setting default configuration:
146 $this->TSdataArray[]=$GLOBALS['TYPO3_CONF_VARS']['FE']['defaultUserTSconfig'];
147
148 if (is_array($this->user) && $this->user['usergroup']) {
149 $groups = t3lib_div::intExplode(',',$this->user['usergroup']);
150 $list = implode($groups,',');
151 $lockToDomain_SQL = ' AND (lockToDomain="" OR lockToDomain="'.t3lib_div::getIndpEnv('HTTP_HOST').'")';
152 if (!$this->showHiddenRecords) $hiddenP = 'AND NOT hidden ';
153 $query='SELECT * FROM '.$this->usergroup_table.' WHERE NOT deleted '.$hiddenP.'AND uid IN ('.$list.')'.$lockToDomain_SQL;
154 $res = mysql(TYPO3_db,$query);
155 while ($row=mysql_fetch_assoc($res)) {
156 $this->groupData['title'][$row['uid']]=$row['title'];
157 $this->groupData['uid'][$row['uid']]=$row['uid'];
158 $this->groupData['pid'][$row['uid']]=$row['pid'];
159 $this->groupData['TSconfig'][$row['uid']]=$row['TSconfig'];
160 }
161 if (mysql_num_rows($res)) {
162 // TSconfig:
163 reset($groups);
164 while(list(,$TSuid)=each($groups)) {
165 $this->TSdataArray[]=$this->groupData['TSconfig'][$TSuid];
166 }
167 $this->TSdataArray[]=$this->user['TSconfig'];
168
169 // Sort information
170 ksort($this->groupData['title']);
171 ksort($this->groupData['uid']);
172 ksort($this->groupData['pid']);
173 return count($this->groupData['uid']);
174 } else {
175 return 0;
176 }
177 }
178 }
179
180 /**
181 * Returns the parsed TSconfig for the fe_user
182 * 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
183 *
184 * @return array TSconfig array for the fe_user
185 */
186 function getUserTSconf() {
187 if (!$this->userTSUpdated) {
188 // Parsing the user TS (or getting from cache)
189 $this->TSdataArray = t3lib_TSparser::checkIncludeLines_array($this->TSdataArray);
190 $userTS = implode($this->TSdataArray,chr(10).'[GLOBAL]'.chr(10));
191 $parseObj = t3lib_div::makeInstance('t3lib_TSparser');
192 $parseObj->parse($userTS);
193 $this->userTS = $parseObj->setup;
194
195 $this->userTSUpdated=1;
196 }
197 return $this->userTS;
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 /*****************************************
217 *
218 * Session data management functions
219 *
220 ****************************************/
221
222 /**
223 * Fetches the session data for the user (from the fe_session_data table) based on the ->id of the current user-session.
224 * The session data is restored to $this->sesData
225 * 1/100 calls will also do a garbage collection.
226 *
227 * @return void
228 * @access private
229 * @see storeSessionData()
230 */
231 function fetchSessionData() {
232 // Gets SesData if any
233 if ($this->id) {
234 $query='SELECT * FROM fe_session_data WHERE hash = "'.$this->id.'"';
235 $dbres=mysql(TYPO3_db,$query);
236 echo mysql_error();
237 if ($sesDataRow = mysql_fetch_assoc($dbres)) {
238 $this->sesData = unserialize($sesDataRow['content']);
239 }
240 }
241 // delete old data:
242 if ((rand()%100) <= 1) { // a possibility of 1 % for garbage collection.
243 $query='DELETE FROM fe_session_data WHERE tstamp < "'.(time()-3600*24).'"'; // all data older than 24 hours are deleted.
244 $dbres=mysql(TYPO3_db,$query);
245 echo mysql_error();
246 }
247 }
248
249 /**
250 * Will write UC and session data.
251 * If the flag $this->userData_change has been set, the function ->writeUC is called (which will save persistent user session data)
252 * 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)
253 *
254 * @return void
255 * @see fetchSessionData(), getKey(), setKey()
256 */
257 function storeSessionData() {
258 // Saves UC and SesData if changed.
259 if ($this->userData_change) {
260 $this->writeUC('');
261 }
262 if ($this->sesData_change) {
263 if ($this->id) {
264 $query='DELETE FROM fe_session_data WHERE hash = "'.$this->id.'"';
265 $dbres=mysql(TYPO3_db,$query);
266 echo mysql_error();
267 $query='INSERT INTO fe_session_data (hash, content, tstamp) VALUES ("'.$this->id.'", "'.addslashes(serialize($this->sesData)).'", "'.time().'")';
268 $dbres=mysql(TYPO3_db,$query);
269 echo mysql_error();
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] = stripslashes($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 ?>