[TASK] Remove function index
[Packages/TYPO3.CMS.git] / typo3 / sysext / sv / class.tx_sv_auth.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2004-2011 René Fritz <r.fritz@colorcube.de>
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 * Service 'User authentication' for the 'sv' extension.
29 *
30 * @author René Fritz <r.fritz@colorcube.de>
31 */
32
33
34
35 /**
36 * Authentication services class
37 *
38 * @author René Fritz <r.fritz@colorcube.de>
39 * @package TYPO3
40 * @subpackage tx_sv
41 */
42 class tx_sv_auth extends tx_sv_authbase {
43
44
45 /**
46 * Find a user (eg. look up the user record in database when a login is sent)
47 *
48 * @return mixed user array or FALSE
49 */
50 function getUser() {
51 $user = FALSE;
52
53 if ($this->login['status']=='login' && $this->login['uident']) {
54
55 $user = $this->fetchUserRecord($this->login['uname']);
56
57 if(!is_array($user)) {
58 // Failed login attempt (no username found)
59 $this->writelog(255,3,3,2,
60 "Login-attempt from %s (%s), username '%s' not found!!",
61 Array($this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname'])); // Logout written to log
62 t3lib_div::sysLog(
63 sprintf( "Login-attempt from %s (%s), username '%s' not found!", $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname'] ),
64 'Core',
65 0
66 );
67 } else {
68 if ($this->writeDevLog) t3lib_div::devLog('User found: '.t3lib_div::arrayToLogString($user, array($this->db_user['userid_column'],$this->db_user['username_column'])), 'tx_sv_auth');
69 }
70 }
71 return $user;
72 }
73
74 /**
75 * Authenticate a user (Check various conditions for the user that might invalidate its authentication, eg. password match, domain, IP, etc.)
76 *
77 * @param array Data of user.
78 * @return boolean
79 */
80 function authUser($user) {
81 $OK = 100;
82
83 if ($this->login['uident'] && $this->login['uname']) {
84
85 // Checking password match for user:
86 $OK = $this->compareUident($user, $this->login);
87
88 if(!$OK) {
89 // Failed login attempt (wrong password) - write that to the log!
90 if ($this->writeAttemptLog) {
91 $this->writelog(255,3,3,1,
92 "Login-attempt from %s (%s), username '%s', password not accepted!",
93 Array($this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']));
94 t3lib_div::sysLog(
95 sprintf( "Login-attempt from %s (%s), username '%s', password not accepted!", $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname'] ),
96 'Core',
97 0
98 );
99 }
100 if ($this->writeDevLog) t3lib_div::devLog('Password not accepted: '.$this->login['uident'], 'tx_sv_auth', 2);
101 }
102
103 // Checking the domain (lockToDomain)
104 if ($OK && $user['lockToDomain'] && $user['lockToDomain']!=$this->authInfo['HTTP_HOST']) {
105 // Lock domain didn't match, so error:
106 if ($this->writeAttemptLog) {
107 $this->writelog(255,3,3,1,
108 "Login-attempt from %s (%s), username '%s', locked domain '%s' did not match '%s'!",
109 Array($this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $user[$this->db_user['username_column']], $user['lockToDomain'], $this->authInfo['HTTP_HOST']));
110 t3lib_div::sysLog(
111 sprintf( "Login-attempt from %s (%s), username '%s', locked domain '%s' did not match '%s'!", $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $user[$this->db_user['username_column']], $user['lockToDomain'], $this->authInfo['HTTP_HOST'] ),
112 'Core',
113 0
114 );
115 }
116 $OK = FALSE;
117 }
118 }
119
120 return $OK;
121 }
122
123 /**
124 * Find usergroup records, currently only for frontend
125 *
126 * @param array Data of user.
127 * @param array Group data array of already known groups. This is handy if you want select other related groups. Keys in this array are unique IDs of those groups.
128 * @return mixed Groups array, keys = uid which must be unique
129 */
130 function getGroups($user, $knownGroups) {
131 global $TYPO3_CONF_VARS;
132
133 $groupDataArr = array();
134
135 if($this->mode=='getGroupsFE') {
136
137 $groups = array();
138 if (is_array($user) && $user[$this->db_user['usergroup_column']]) {
139 $groupList = $user[$this->db_user['usergroup_column']];
140 $groups = array();
141 $this->getSubGroups($groupList,'',$groups);
142 }
143
144 // ADD group-numbers if the IPmask matches.
145 if (is_array($TYPO3_CONF_VARS['FE']['IPmaskMountGroups'])) {
146 foreach($TYPO3_CONF_VARS['FE']['IPmaskMountGroups'] as $IPel) {
147 if ($this->authInfo['REMOTE_ADDR'] && $IPel[0] && t3lib_div::cmpIP($this->authInfo['REMOTE_ADDR'],$IPel[0])) {$groups[]=intval($IPel[1]);}
148 }
149 }
150
151 $groups = array_unique($groups);
152
153 if (count($groups)) {
154 $list = implode(',',$groups);
155
156 if ($this->writeDevLog) t3lib_div::devLog('Get usergroups with id: '.$list, 'tx_sv_auth');
157
158 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\''.$this->authInfo['HTTP_HOST'].'\')';
159 if (!$this->authInfo['showHiddenRecords']) $hiddenP = 'AND hidden=0 ';
160 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->db_groups['table'], 'deleted=0 '.$hiddenP.' AND uid IN ('.$list.')'.$lockToDomain_SQL);
161 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
162 $groupDataArr[$row['uid']] = $row;
163 }
164 if ($res) $GLOBALS['TYPO3_DB']->sql_free_result($res);
165
166 } else {
167 if ($this->writeDevLog) t3lib_div::devLog('No usergroups found.', 'tx_sv_auth', 2);
168 }
169 } elseif ($this->mode=='getGroupsBE') {
170
171 # Get the BE groups here
172 # still needs to be implemented in t3lib_userauthgroup
173 }
174
175 return $groupDataArr;
176 }
177
178 /**
179 * Fetches subgroups of groups. Function is called recursively for each subgroup.
180 * Function was previously copied from t3lib_userAuthGroup->fetchGroups and has been slightly modified.
181 *
182 * @param string Commalist of fe_groups uid numbers
183 * @param string List of already processed fe_groups-uids so the function will not fall into a eternal recursion.
184 * @return array
185 * @access private
186 */
187 function getSubGroups($grList, $idList='', &$groups) {
188
189 // Fetching records of the groups in $grList (which are not blocked by lockedToDomain either):
190 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\''.$this->authInfo['HTTP_HOST'].'\')';
191 if (!$this->authInfo['showHiddenRecords']) $hiddenP = 'AND hidden=0 ';
192 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,subgroup', 'fe_groups', 'deleted=0 '.$hiddenP.' AND uid IN ('.$grList.')'.$lockToDomain_SQL);
193
194 $groupRows = array(); // Internal group record storage
195
196 // The groups array is filled
197 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
198 if(!in_array($row['uid'], $groups)) { $groups[] = $row['uid']; }
199 $groupRows[$row['uid']] = $row;
200 }
201
202 // Traversing records in the correct order
203 $include_staticArr = t3lib_div::intExplode(',', $grList);
204 foreach($include_staticArr as $uid) { // traversing list
205
206 // Get row:
207 $row=$groupRows[$uid];
208 if (is_array($row) && !t3lib_div::inList($idList,$uid)) { // Must be an array and $uid should not be in the idList, because then it is somewhere previously in the grouplist
209
210 // Include sub groups
211 if (trim($row['subgroup'])) {
212 $theList = implode(',',t3lib_div::intExplode(',',$row['subgroup'])); // Make integer list
213 $this->getSubGroups($theList, $idList.','.$uid, $groups); // Call recursively, pass along list of already processed groups so they are not recursed again.
214 }
215 }
216 }
217 }
218 }
219
220
221
222 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/sv/class.tx_sv_auth.php'])) {
223 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/sv/class.tx_sv_auth.php']);
224 }
225 ?>