Hooks in tslib_fe, Frontend login mode feature for pages; various other things. see...
[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 $this->groupData = Array(
146 'title' => Array(),
147 'uid' => Array(),
148 'pid' => Array()
149 );
150
151 // Setting default configuration:
152 $this->TSdataArray[]=$GLOBALS['TYPO3_CONF_VARS']['FE']['defaultUserTSconfig'];
153
154 if (is_array($this->user) && $this->user['usergroup']) {
155 $groups = t3lib_div::intExplode(',',$this->user['usergroup']);
156 $list = implode(',',$groups);
157 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain=\''.t3lib_div::getIndpEnv('HTTP_HOST').'\')';
158 if (!$this->showHiddenRecords) $hiddenP = 'AND hidden=0 ';
159
160 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->usergroup_table, 'deleted=0 '.$hiddenP.'AND uid IN ('.$list.')'.$lockToDomain_SQL);
161 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
162 $this->groupData['title'][$row['uid']] = $row['title'];
163 $this->groupData['uid'][$row['uid']] = $row['uid'];
164 $this->groupData['pid'][$row['uid']] = $row['pid'];
165 $this->groupData['TSconfig'][$row['uid']] = $row['TSconfig'];
166 }
167
168 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
169 $GLOBALS['TYPO3_DB']->sql_free_result($res);
170 // TSconfig:
171 reset($groups);
172 while(list(,$TSuid)=each($groups)) {
173 $this->TSdataArray[]=$this->groupData['TSconfig'][$TSuid];
174 }
175 $this->TSdataArray[]=$this->user['TSconfig'];
176
177 // Sort information
178 ksort($this->groupData['title']);
179 ksort($this->groupData['uid']);
180 ksort($this->groupData['pid']);
181 return count($this->groupData['uid']);
182 } else {
183 return 0;
184 }
185 }
186 }
187
188 /**
189 * Returns the parsed TSconfig for the fe_user
190 * 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
191 *
192 * @return array TSconfig array for the fe_user
193 */
194 function getUserTSconf() {
195 if (!$this->userTSUpdated) {
196 // Parsing the user TS (or getting from cache)
197 $this->TSdataArray = t3lib_TSparser::checkIncludeLines_array($this->TSdataArray);
198 $userTS = implode(chr(10).'[GLOBAL]'.chr(10),$this->TSdataArray);
199 $parseObj = t3lib_div::makeInstance('t3lib_TSparser');
200 $parseObj->parse($userTS);
201 $this->userTS = $parseObj->setup;
202
203 $this->userTSUpdated=1;
204 }
205 return $this->userTS;
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 /*****************************************
225 *
226 * Session data management functions
227 *
228 ****************************************/
229
230 /**
231 * Fetches the session data for the user (from the fe_session_data table) based on the ->id of the current user-session.
232 * The session data is restored to $this->sesData
233 * 1/100 calls will also do a garbage collection.
234 *
235 * @return void
236 * @access private
237 * @see storeSessionData()
238 */
239 function fetchSessionData() {
240 // Gets SesData if any
241 if ($this->id) {
242 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'fe_session_data', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, 'fe_session_data'));
243 if ($sesDataRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres)) {
244 $this->sesData = unserialize($sesDataRow['content']);
245 }
246 }
247 // delete old data:
248 if ((rand()%100) <= 1) { // a possibility of 1 % for garbage collection.
249 $GLOBALS['TYPO3_DB']->exec_DELETEquery('fe_session_data', 'tstamp < '.intval(time()-3600*24)); // all data older than 24 hours are deleted.
250 }
251 }
252
253 /**
254 * Will write UC and session data.
255 * If the flag $this->userData_change has been set, the function ->writeUC is called (which will save persistent user session data)
256 * 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)
257 *
258 * @return void
259 * @see fetchSessionData(), getKey(), setKey()
260 */
261 function storeSessionData() {
262 // Saves UC and SesData if changed.
263 if ($this->userData_change) {
264 $this->writeUC('');
265 }
266 if ($this->sesData_change) {
267 if ($this->id) {
268 $insertFields = array (
269 'hash' => $this->id,
270 'content' => serialize($this->sesData),
271 'tstamp' => time()
272 );
273 $GLOBALS['TYPO3_DB']->exec_DELETEquery('fe_session_data', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, 'fe_session_data'));
274 $GLOBALS['TYPO3_DB']->exec_INSERTquery('fe_session_data', $insertFields);
275 }
276 }
277 }
278
279 /**
280 * 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)
281 *
282 * @param string Session data type; Either "user" (persistent, bound to fe_users profile) or "ses" (temporary, bound to current session cookie)
283 * @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.
284 * @return mixed Returns whatever value there was in the array for the key, $key
285 * @see setKey()
286 */
287 function getKey($type,$key) {
288 if ($key) {
289 switch($type) {
290 case 'user':
291 return $this->uc[$key];
292 break;
293 case 'ses':
294 return $this->sesData[$key];
295 break;
296 }
297 }
298 }
299
300 /**
301 * Saves session data, either persistent or bound to current session cookie. Please see getKey() for more details.
302 * 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.
303 * 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.
304 * 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.
305 *
306 * @param string Session data type; Either "user" (persistent, bound to fe_users profile) or "ses" (temporary, bound to current session cookie)
307 * @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.
308 * @param mixed The data value to store in $key
309 * @return void
310 * @see setKey(), storeSessionData(), record_registration()
311 */
312 function setKey($type,$key,$data) {
313 if ($key) {
314 switch($type) {
315 case 'user':
316 if ($this->user['uid']) {
317 $this->uc[$key]=$data;
318 $this->userData_change=1;
319 }
320 break;
321 case 'ses':
322 $this->sesData[$key]=$data;
323 $this->sesData_change=1;
324 break;
325 }
326 }
327 }
328
329 /**
330 * Registration of records/"shopping basket" in session data
331 * This will take the input array, $recs, and merge into the current "recs" array found in the session data.
332 * If a change in the recs storage happens (which it probably does) the function setKey() is called in order to store the array again.
333 *
334 * @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).
335 * @return void
336 */
337 function record_registration($recs) {
338 if ($recs['clear_all']) {
339 $this->setKey('ses','recs','');
340 }
341 $change=0;
342 $recs_array=$this->getKey('ses','recs');
343 reset($recs);
344 while(list($table,$data)=each($recs)) {
345 if (is_array($data)) {
346 reset($data);
347 while(list($rec_id,$value)=each($data)) {
348 if ($value != $recs_array[$table][$rec_id]) {
349 $recs_array[$table][$rec_id] = $value;
350 $change=1;
351 }
352 }
353 }
354 }
355 if ($change) {
356 $this->setKey('ses','recs',$recs_array);
357 }
358 }
359 }
360
361
362 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_feuserauth.php']) {
363 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_feuserauth.php']);
364 }
365 ?>