Added feature #10017: [felogin] New Method for "forgotPassword"
[Packages/TYPO3.CMS.git] / typo3 / sysext / felogin / pi1 / class.tx_felogin_pi1.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2009 Steffen Kamper <info@sk-typo3.de>
6 * Based on Newloginbox (c) 2002-2004 Kasper Skaarhoj <kasper@typo3.com>
7 *
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 *
26 * The code was adapted from newloginbox, see manual for detailed description
27 ***************************************************************/
28 /**
29 * Plugin 'Website User Login' for the 'felogin' extension.
30 *
31 * @author Steffen Kamper <info@sk-typo3.de>
32 * @package TYPO3
33 * @subpackage tx_felogin
34 */
35 class tx_felogin_pi1 extends tslib_pibase {
36 var $prefixId = 'tx_felogin_pi1'; // Same as class name
37 var $scriptRelPath = 'pi1/class.tx_felogin_pi1.php'; // Path to this script relative to the extension dir.
38 var $extKey = 'felogin'; // The extension key.
39 var $pi_checkCHash = true;
40 var $userIsLoggedIn; // Is user logged in?
41 var $template;
42 var $uploadDir;
43 var $redirectUrl;
44 protected $noRedirect = false;
45
46 /**
47 * The main method of the plugin
48 *
49 * @param string $content: The PlugIn content
50 * @param array $conf: The PlugIn configuration
51 *
52 * @return string The content that is displayed on the website
53 */
54 public function main($content,$conf) {
55
56 // Loading TypoScript array into object variable:
57 $this->conf = $conf;
58 $this->uploadDir = 'uploads/tx_felogin/';
59
60 // Loading default pivars
61 $this->pi_setPiVarDefaults();
62
63 // Loading language-labels
64 $this->pi_loadLL();
65
66 // Init FlexForm configuration for plugin:
67 $this->pi_initPIflexForm();
68 $this->mergeflexFormValuesIntoConf();
69
70
71 // Get storage PIDs:
72 if ($this->conf['storagePid']) {
73 if (intval($this->conf['recursive'])) {
74 $this->spid = $this->pi_getPidList($this->conf['storagePid'], intval($this->conf['recursive']));
75 } else {
76 $this->spid = $this->conf['storagePid'];
77 }
78 } else {
79 $pids = $GLOBALS['TSFE']->getStorageSiterootPids();
80 $this->spid = $pids['_STORAGE_PID'];
81 }
82
83 // GPvars:
84 $this->logintype = t3lib_div::_GP('logintype');
85 $this->redirectUrl = t3lib_div::_GP('redirect_url');
86 $this->noRedirect = ($this->piVars['noredirect'] || $this->conf['redirectDisable']);
87
88 // if config.typolinkLinkAccessRestrictedPages is set, the var is return_url
89 $returnUrl = t3lib_div::_GP('return_url');
90 if ($returnUrl) {
91 $this->redirectUrl = $returnUrl;
92 }
93
94 // Get Template
95 $templateFile = $this->conf['templateFile'] ? $this->conf['templateFile'] : 'EXT:felogin/template.html';
96 $this->template = $this->cObj->fileResource($templateFile);
97
98 // Is user logged in?
99 $this->userIsLoggedIn = $GLOBALS['TSFE']->loginUser;
100
101 // Redirect
102 if ($this->conf['redirectMode'] && !$this->conf['redirectDisable'] && !$this->noRedirect) {
103 $this->redirectUrl = $this->processRedirect();
104 }
105
106
107 // What to display
108 $content='';
109 if ($this->piVars['forgot']) {
110 $content .= $this->showForgot();
111 } elseif ($this->piVars['forgothash']) {
112 $content .= $this->changePassword();
113 } else {
114 if($this->userIsLoggedIn && !$this->logintype) {
115 $content .= $this->showLogout();
116 } else {
117 $content .= $this->showLogin();
118 }
119 }
120
121
122
123 // Process the redirect
124 if (($this->logintype === 'login' || $this->logintype === 'logout') && $this->redirectUrl && !$this->noRedirect) {
125 if (!$GLOBALS['TSFE']->fe_user->cookieId) {
126 $content .= '<p style="color:red; font-weight:bold;">' . $this->pi_getLL('cookie_warning', '', 1) . '</p>';
127 } else {
128 header('Location: '.t3lib_div::locationHeaderUrl($this->redirectUrl));
129 exit;
130 }
131 }
132 return $this->conf['wrapContentInBaseClass'] ? $this->pi_wrapInBaseClass($content) : $content;
133
134 }
135
136 /**
137 * Shows the forgot password form
138 *
139 * @return string content
140 */
141 protected function showForgot() {
142 $subpart = $this->cObj->getSubpart($this->template, '###TEMPLATE_FORGOT###');
143 $subpartArray = $linkpartArray = array();
144 $postData = t3lib_div::_POST($this->prefixId);
145
146 if ($postData['forgot_email']) {
147
148 // get hashes for compare
149 $postedHash = $postData['forgot_hash'];
150 $hashData = $GLOBALS['TSFE']->fe_user->getKey('ses', 'forgot_hash');
151
152
153 if ($postedHash === $hashData['forgot_hash']) {
154 $row = FALSE;
155
156 // look for user record
157 $data = $GLOBALS['TYPO3_DB']->fullQuoteStr($this->piVars['forgot_email'], 'fe_users');
158 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
159 'uid, username, password, email',
160 'fe_users',
161 '(email=' . $data .' OR username=' . $data . ') AND pid IN ('.$GLOBALS['TYPO3_DB']->cleanIntList($this->spid).') '.$this->cObj->enableFields('fe_users')
162 );
163
164 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
165 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
166 }
167
168 if ($row) {
169 // generate an email with the hashed link
170 $error = $this->generateAndSendHash($row);
171 }
172 // generate message
173 if ($error) {
174 $markerArray['###STATUS_MESSAGE###'] = $this->cObj->stdWrap($error, $this->conf['forgotMessage_stdWrap.']);
175 } else {
176 $markerArray['###STATUS_MESSAGE###'] = $this->cObj->stdWrap($this->pi_getLL('ll_forgot_reset_message_emailSent', '', 1), $this->conf['forgotMessage_stdWrap.']);
177 }
178 $subpartArray['###FORGOT_FORM###'] = '';
179
180
181 } else {
182 //wrong email
183 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('forgot_reset_message', $this->conf['forgotMessage_stdWrap.']);
184 $markerArray['###BACKLINK_LOGIN###'] = '';
185 }
186 } else {
187 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('forgot_reset_message', $this->conf['forgotMessage_stdWrap.']);
188 $markerArray['###BACKLINK_LOGIN###'] = '';
189 }
190
191 $markerArray['###BACKLINK_LOGIN###'] = $this->getPageLink($this->pi_getLL('ll_forgot_header_backToLogin', '', 1), array());
192 $markerArray['###STATUS_HEADER###'] = $this->getDisplayText('forgot_header', $this->conf['forgotHeader_stdWrap.']);
193
194 $markerArray['###LEGEND###'] = $this->pi_getLL('reset_password', '', 1);
195 $markerArray['###ACTION_URI###'] = $this->getPageLink('', array($this->prefixId . '[forgot]'=>1), true);
196 $markerArray['###EMAIL_LABEL###'] = $this->pi_getLL('your_email', '', 1);
197 $markerArray['###FORGOT_PASSWORD_ENTEREMAIL###'] = $this->pi_getLL('forgot_password_enterEmail', '', 1);
198 $markerArray['###FORGOT_EMAIL###'] = $this->prefixId.'[forgot_email]';
199 $markerArray['###SEND_PASSWORD###'] = $this->pi_getLL('reset_password', '', 1);
200
201 $markerArray['###DATA_LABEL###'] = $this->pi_getLL('ll_enter_your_data', '', 1);
202
203
204
205 $markerArray = array_merge($markerArray, $this->getUserFieldMarkers());
206
207 // generate hash
208 $hash = md5($this->generatePassword(3));
209 $markerArray['###FORGOTHASH###'] = $hash;
210 // set hash in feuser session
211 $GLOBALS['TSFE']->fe_user->setKey('ses', 'forgot_hash', array('forgot_hash' => $hash));
212
213
214 return $this->cObj->substituteMarkerArrayCached($subpart, $markerArray, $subpartArray, $linkpartArray);
215 }
216
217 /**
218 * This function checks the hash from link and checks the validity. If it's valid it shows the form for
219 * changing the password and process the change of password after submit, if not valid it returns the error message
220 *
221 * @return string The content.
222 */
223 protected function changePassword() {
224
225 $subpartArray = $linkpartArray = array();
226 $done = false;
227
228 $minLength = intval($this->conf['newPasswordMinLength']) ? intval($this->conf['newPasswordMinLength']) : 6;
229
230 $subpart = $this->cObj->getSubpart($this->template, '###TEMPLATE_CHANGEPASSWORD###');
231
232 $markerArray['###STATUS_HEADER###'] = $this->getDisplayText('change_password_header', $this->conf['changePasswordHeader_stdWrap.']);
233 $markerArray['###STATUS_MESSAGE###'] = sprintf($this->getDisplayText('change_password_message', $this->conf['changePasswordMessage_stdWrap.']), $minLength);
234
235 $markerArray['###BACKLINK_LOGIN###'] = '';
236 $uid = $this->piVars['user'];
237 $piHash = $this->piVars['forgothash'];
238
239 $hash = explode('|', $piHash);
240 if (intval($uid) == 0) {
241 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('change_password_notvalid_message', $this->conf['changePasswordMessage_stdWrap.']);
242 $subpartArray['###CHANGEPASSWORD_FORM###'] = '';
243 } else {
244 $user = $this->pi_getRecord('fe_users', intval($uid));
245 $userHash = $user['felogin_forgotHash'];
246 $compareHash = explode('|', $userHash);
247
248 if (!$compareHash || !$compareHash[1] || $compareHash[0] < time() || $hash[0] != $compareHash[0] || md5($hash[1]) != $compareHash[1]) {
249 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('change_password_notvalid_message',$this->conf['changePasswordMessage_stdWrap.']);
250 $subpartArray['###CHANGEPASSWORD_FORM###'] = '';
251 } else {
252 // all is fine, continue with new password
253 $postData = t3lib_div::_POST($this->prefixId);
254
255 if ($postData['changepasswordsubmit']) {
256 if (strlen($postData['password1']) < $minLength) {
257 $markerArray['###STATUS_MESSAGE###'] = sprintf($this->getDisplayText('change_password_tooshort_message', $this->conf['changePasswordMessage_stdWrap.']), $minLength);
258 } elseif ($postData['password1'] != $postData['password2']) {
259 $markerArray['###STATUS_MESSAGE###'] = sprintf($this->getDisplayText('change_password_notequal_message', $this->conf['changePasswordMessage_stdWrap.']), $minLength);
260 } else {
261 $newPass = $postData['password1'];
262
263 if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['password_changed']) {
264 $_params = array(
265 'user' => $user,
266 'newPassword' => $newPass,
267 );
268 foreach($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['password_changed'] as $_funcRef) {
269 if ($_funcRef) {
270 t3lib_div::callUserFunction($_funcRef, $_params, $this);
271 }
272 }
273 $newPass = $_params['newPassword'];
274 }
275
276 // save new password and clear DB-hash
277 $res = $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
278 'fe_users',
279 'uid=' . $user['uid'],
280 array('password' => $newPass, 'felogin_forgotHash' => '')
281 );
282 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('change_password_done_message', $this->conf['changePasswordMessage_stdWrap.']);
283 $done = true;
284 $subpartArray['###CHANGEPASSWORD_FORM###'] = '';
285 $markerArray['###BACKLINK_LOGIN###'] = $this->getPageLink($this->pi_getLL('ll_forgot_header_backToLogin', '', 1), array());
286 }
287 }
288
289 if (!$done) {
290 // Change password form
291 $markerArray['###ACTION_URI###'] = $this->pi_getPageLink($GLOBALS['TSFE']->id, '', array(
292 $this->prefixId . '[user]' => $user['uid'],
293 $this->prefixId . '[forgothash]' => $piHash
294 ));
295 $markerArray['###LEGEND###'] = $this->pi_getLL('change_password', '', 1);
296 $markerArray['###NEWPASSWORD1_LABEL###'] = $this->pi_getLL('newpassword_label1', '', 1);
297 $markerArray['###NEWPASSWORD2_LABEL###'] = $this->pi_getLL('newpassword_label2', '', 1);
298 $markerArray['###NEWPASSWORD1###'] = $this->prefixId . '[password1]';
299 $markerArray['###NEWPASSWORD2###'] = $this->prefixId . '[password2]';
300 $markerArray['###STORAGE_PID###'] = $this->spid;
301 $markerArray['###SEND_PASSWORD###'] = $this->pi_getLL('change_password', '', 1);
302 $markerArray['###FORGOTHASH###'] = $piHash;
303 }
304 }
305 }
306
307 return $this->cObj->substituteMarkerArrayCached($subpart, $markerArray, $subpartArray, $linkpartArray);
308 }
309
310 /**
311 * generates a hashed link and send it with email
312 *
313 * @param array $user contains user data
314 * @return string Empty string with success, error message with no success
315 */
316 protected function generateAndSendHash($user) {
317 $hours = intval($this->conf['forgotLinkHashValidTime']) > 0 ? intval($this->conf['forgotLinkHashValidTime']) : 24;
318 $validEnd = time() + 3600 * $hours;
319 $validEndString = date($this->conf['dateFormat'], $validEnd);
320
321 $hash = md5(rand());
322 $randHash = $validEnd . '|' . $hash;
323 $randHashDB = $validEnd . '|' . md5($hash);
324
325 //write hash to DB
326 $res = $GLOBALS['TYPO3_DB']->exec_UPDATEquery('fe_users', 'uid=' . $user['uid'], array('felogin_forgotHash' => $randHashDB));
327
328 // send hashlink to user
329 $this->conf['linkPrefix'] = -1;
330 $isAbsRelPrefix = !empty($GLOBALS['TSFE']->absRefPrefix);
331 $isBaseURL = !empty($GLOBALS['TSFE']->baseUrl);
332 $isFeloginBaseURL = !empty($this->conf['feloginBaseURL']);
333
334 if ($isFeloginBaseURL) {
335 // first priority
336 $this->conf['linkPrefix'] = $this->conf['feloginBaseURL'];
337 } else {
338 if ($isBaseURL) {
339 // 3rd priority
340 $this->conf['linkPrefix'] = $GLOBALS['TSFE']->baseUrl;
341 }
342 }
343
344 if ($this->conf['linkPrefix'] == -1 && !$isAbsRelPrefix) {
345 // no preix is set, return the error
346 return $this->pi_getLL('ll_change_password_nolinkprefix_message');
347 }
348
349 $link = ($isAbsRelPrefix ? '' : $this->conf['linkPrefix']) . $this->pi_getPageLink($GLOBALS['TSFE']->id, '', array(
350 $this->prefixId . '[user]' => $user['uid'],
351 $this->prefixId . '[forgothash]' => $randHash
352 ));
353
354 $msg = sprintf($this->pi_getLL('ll_forgot_validate_reset_password', '', 0), $user['username'], $link, $validEndString);
355
356 // no RDCT - Links for security reasons
357 $oldSetting = $GLOBALS['TSFE']->config['config']['notification_email_urlmode'];
358 $GLOBALS['TSFE']->config['config']['notification_email_urlmode'] = 0;
359 // send the email
360 $this->cObj->sendNotifyEmail($msg, $user['email'], '', $this->conf['email_from'], $this->conf['email_fromName'], $this->conf['replyTo']);
361 // restore settings
362 $GLOBALS['TSFE']->config['config']['notification_email_urlmode'] = $oldSetting;
363
364 return '';
365 }
366
367 /**
368 * Shows logout form
369 *
370 * @return string The content.
371 */
372 protected function showLogout() {
373 $subpart = $this->cObj->getSubpart($this->template, '###TEMPLATE_LOGOUT###');
374 $subpartArray = $linkpartArray = array();
375
376 $markerArray['###STATUS_HEADER###'] = $this->getDisplayText('status_header',$this->conf['logoutHeader_stdWrap.']);
377 $markerArray['###STATUS_MESSAGE###']=$this->getDisplayText('status_message',$this->conf['logoutMessage_stdWrap.']);$this->cObj->stdWrap($this->flexFormValue('message','s_status'),$this->conf['logoutMessage_stdWrap.']);
378
379 $markerArray['###LEGEND###'] = $this->pi_getLL('logout', '', 1);
380 $markerArray['###ACTION_URI###'] = $this->getPageLink('',array(),true);
381 $markerArray['###LOGOUT_LABEL###'] = $this->pi_getLL('logout', '', 1);
382 $markerArray['###NAME###'] = htmlspecialchars($GLOBALS['TSFE']->fe_user->user['name']);
383 $markerArray['###STORAGE_PID###'] = $this->spid;
384 $markerArray['###USERNAME###'] = htmlspecialchars($GLOBALS['TSFE']->fe_user->user['username']);
385 $markerArray['###USERNAME_LABEL###'] = $this->pi_getLL('username', '', 1);
386 $markerArray['###NOREDIRECT###'] = $this->noRedirect ? '1' : '0';
387 $markerArray['###PREFIXID###'] = $this->prefixId;
388 $markerArray = array_merge($markerArray, $this->getUserFieldMarkers());
389
390 if ($this->redirectUrl) {
391 // use redirectUrl for action tag because of possible access restricted pages
392 $markerArray['###ACTION_URI###'] = htmlspecialchars($this->redirectUrl);
393 $this->redirectUrl = '';
394 }
395 return $this->cObj->substituteMarkerArrayCached($subpart, $markerArray, $subpartArray, $linkpartArray);
396 }
397
398 /**
399 * Shows login form
400 *
401 * @return string content
402 */
403 protected function showLogin() {
404 $subpart = $this->cObj->getSubpart($this->template, '###TEMPLATE_LOGIN###');
405 $subpartArray = $linkpartArray = array();
406
407 $gpRedirectUrl = '';
408
409 $markerArray['###LEGEND###'] = $this->pi_getLL('oLabel_header_welcome', '', 1);
410
411 if($this->logintype === 'login') {
412 if($this->userIsLoggedIn) {
413 // login success
414 $markerArray['###STATUS_HEADER###'] = $this->getDisplayText('success_header',$this->conf['successHeader_stdWrap.']);
415 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('success_message', $this->conf['successMessage_stdWrap.']);
416 $markerArray = array_merge($markerArray, $this->getUserFieldMarkers());
417 $subpartArray['###LOGIN_FORM###'] = '';
418
419 // Hook for general actions after after login has been confirmed (by Thomas Danzl <thomas@danzl.org>)
420 if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['login_confirmed']) {
421 $_params = array();
422 foreach($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['login_confirmed'] as $_funcRef) {
423 if ($_funcRef) {
424 t3lib_div::callUserFunction($_funcRef, $_params, $this);
425 }
426 }
427 }
428
429 } else {
430 // login error
431 $markerArray['###STATUS_HEADER###'] = $this->getDisplayText('error_header',$this->conf['errorHeader_stdWrap.']);
432 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('error_message',$this->conf['errorMessage_stdWrap.']);
433 $gpRedirectUrl = t3lib_div::_GP('redirect_url');
434 }
435 } else {
436 if($this->logintype === 'logout') {
437 // login form after logout
438 $markerArray['###STATUS_HEADER###'] = $this->getDisplayText('logout_header',$this->conf['welcomeHeader_stdWrap.']);
439 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('logout_message',$this->conf['welcomeMessage_stdWrap.']);
440 } else {
441 // login form
442 $markerArray['###STATUS_HEADER###'] = $this->getDisplayText('welcome_header',$this->conf['welcomeHeader_stdWrap.']);
443 $markerArray['###STATUS_MESSAGE###'] = $this->getDisplayText('welcome_message',$this->conf['welcomeMessage_stdWrap.']);
444 }
445 }
446
447
448 // Hook (used by kb_md5fepw extension by Kraft Bernhard <kraftb@gmx.net>)
449 // This hook allows to call User JS functions.
450 // The methods should also set the required JS functions to get included
451 $onSubmit = '';
452 $extraHidden = '';
453 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['loginFormOnSubmitFuncs'])) {
454 $_params = array();
455 $onSubmitAr = array();
456 $extraHiddenAr = array();
457 foreach($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['loginFormOnSubmitFuncs'] as $funcRef) {
458 list($onSub, $hid) = t3lib_div::callUserFunction($funcRef, $_params, $this);
459 $onSubmitAr[] = $onSub;
460 $extraHiddenAr[] = $hid;
461 }
462 }
463 if (count($onSubmitAr)) {
464 $onSubmit = implode('; ', $onSubmitAr).'; return true;';
465 $extraHidden = implode(chr(10), $extraHiddenAr);
466 }
467
468 // Login form
469 $markerArray['###ACTION_URI###'] = $this->getPageLink('',array(),true);
470 $markerArray['###EXTRA_HIDDEN###'] = $extraHidden; // used by kb_md5fepw extension...
471 $markerArray['###LEGEND###'] = $this->pi_getLL('login', '', 1);
472 $markerArray['###LOGIN_LABEL###'] = $this->pi_getLL('login', '', 1);
473 $markerArray['###ON_SUBMIT###'] = $onSubmit; // used by kb_md5fepw extension...
474 $markerArray['###PASSWORD_LABEL###'] = $this->pi_getLL('password', '', 1);
475 $markerArray['###STORAGE_PID###'] = $this->spid;
476 $markerArray['###USERNAME_LABEL###'] = $this->pi_getLL('username', '', 1);
477 $markerArray['###REDIRECT_URL###'] = $gpRedirectUrl ? htmlspecialchars($gpRedirectUrl) : htmlspecialchars($this->redirectUrl);
478 $markerArray['###NOREDIRECT###'] = $this->noRedirect ? '1' : '0';
479 $markerArray['###PREFIXID###'] = $this->prefixId;
480 $markerArray = array_merge($markerArray, $this->getUserFieldMarkers());
481
482 if ($this->flexFormValue('showForgotPassword','sDEF') || $this->conf['showForgotPasswordLink']) {
483 $linkpartArray['###FORGOT_PASSWORD_LINK###'] = explode('|',$this->getPageLink('|',array($this->prefixId.'[forgot]'=>1)));
484 $markerArray['###FORGOT_PASSWORD###'] = $this->pi_getLL('ll_forgot_header', '', 1);
485 } else {
486 $subpartArray['###FORGOTP_VALID###'] = '';
487 }
488
489
490
491 if (($this->conf['showPermaLogin']) && ($GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'] == 0 || $GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'] == 1) && $GLOBALS['TYPO3_CONF_VARS']['FE']['lifetime'] > 0) {
492 $markerArray['###PERMALOGIN###'] = $this->pi_getLL('permalogin', '', 1);
493 if($GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'] == 1) {
494 $markerArray['###PERMALOGIN_HIDDENFIELD_ATTRIBUTES###'] = 'disabled="disabled"';
495 $markerArray['###PERMALOGIN_CHECKBOX_ATTRIBUTES###'] = 'checked="checked"';
496 } else {
497 $markerArray['###PERMALOGIN_HIDDENFIELD_ATTRIBUTES###'] = '';
498 $markerArray['###PERMALOGIN_CHECKBOX_ATTRIBUTES###'] = '';
499 }
500 } else {
501 $subpartArray['###PERMALOGIN_VALID###'] = '';
502 }
503 return $this->cObj->substituteMarkerArrayCached($subpart, $markerArray, $subpartArray, $linkpartArray);
504 }
505
506 /**
507 * Process redirect methods. The function searches for a redirect url using all configured methods.
508 *
509 * @return string redirect url
510 */
511 protected function processRedirect() {
512 if ($this->conf['redirectMode']) {
513 foreach (t3lib_div::trimExplode(',', $this->conf['redirectMode'],1) as $redirMethod) {
514 if ($GLOBALS['TSFE']->loginUser && $this->logintype === 'login') {
515 // logintype is needed because the login-page wouldn't be accessible anymore after a login (would always redirect)
516 switch ($redirMethod) {
517 case 'groupLogin': // taken from dkd_redirect_at_login written by Ingmar Schlecht; database-field changed
518 $groupData = $GLOBALS['TSFE']->fe_user->groupData;
519 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
520 'felogin_redirectPid',
521 $GLOBALS['TSFE']->fe_user->usergroup_table,
522 'felogin_redirectPid!="" AND uid IN (' . implode(',', $groupData['uid']) . ')'
523 );
524 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
525 $redirect_url = $this->pi_getPageLink($row[0],array(),true); // take the first group with a redirect page
526 }
527 break;
528 case 'userLogin':
529 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
530 'felogin_redirectPid',
531 $GLOBALS['TSFE']->fe_user->user_table,
532 $GLOBALS['TSFE']->fe_user->userid_column . '=' . $GLOBALS['TSFE']->fe_user->user['uid'] . ' AND felogin_redirectPid!=""'
533 );
534 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
535 $redirect_url = $this->pi_getPageLink($row[0], array(), true);
536 }
537 break;
538 case 'login':
539 if ($this->conf['redirectPageLogin']) {
540 $redirect_url = $this->pi_getPageLink(intval($this->conf['redirectPageLogin']), array(), true);
541 }
542 break;
543 case 'getpost':
544 $redirect_url = $this->redirectUrl;
545 break;
546 case 'referer':
547 $redirect_url = t3lib_div::getIndpEnv('HTTP_REFERER');
548 // avoid forced logout, when trying to login immediatly after a logout
549 $redirect_url = preg_replace('/[&?]logintype=[a-z]+/', '', $redirect_url);
550 break;
551 case 'refererDomains':
552 // Auto redirect.
553 // Feature to redirect to the page where the user came from (HTTP_REFERER).
554 // Allowed domains to redirect to, can be configured with plugin.tx_felogin_pi1.domains
555 // Thanks to plan2.net / Martin Kutschker for implementing this feature.
556 if (!$redirect_url && $this->conf['domains']) {
557 $redirect_url = t3lib_div::getIndpEnv('HTTP_REFERER');
558 // is referring url allowed to redirect?
559 $match = array();
560 if (preg_match('/^http://([[:alnum:]._-]+)//', $redirect_url, $match)) {
561 $redirect_domain = $match[1];
562 $found = false;
563 foreach(split(',', $this->conf['domains']) as $d) {
564 if (preg_match('/(^|\.)/'.$d.'$', $redirect_domain)) {
565 $found = true;
566 break;
567 }
568 }
569 if (!$found) {
570 $redirect_url = '';
571 }
572 }
573
574 // Avoid forced logout, when trying to login immediatly after a logout
575 $redirect_url = preg_replace('/[&?]logintype=[a-z]+/', '', $redirect_url);
576 }
577 break;
578 }
579 } else if ($this->logintype === 'login') { // after login-error
580 switch ($redirMethod) {
581 case 'loginError':
582 if ($this->conf['redirectPageLoginError']) {
583 $redirect_url = $this->pi_getPageLink(intval($this->conf['redirectPageLoginError']), array(), true);
584 }
585 break;
586 }
587 } elseif ($this->logintype === 'logout') { // after logout
588
589 // Hook for general actions after after logout has been confirmed
590 if ($this->logintype === 'logout' && $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['logout_confirmed']) {
591 $_params = array();
592 foreach($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['logout_confirmed'] as $_funcRef) {
593 if ($_funcRef) {
594 t3lib_div::callUserFunction($_funcRef, $_params, $this);
595 }
596 }
597 }
598
599 switch ($redirMethod) {
600 case 'logout':
601 if ($this->conf['redirectPageLogout']) {
602 $redirect_url = $this->pi_getPageLink(intval($this->conf['redirectPageLogout']), array(), true);
603 }
604 break;
605 }
606 } else { // not logged in
607 // Placeholder for maybe future options
608 switch ($redirMethod) {
609 case 'getpost':
610 // preserve the get/post value
611 $redirect_url = $this->redirectUrl;
612 break;
613 }
614 }
615
616 if ($redirect_url && $this->conf['redirectFirstMethod']) {
617 break;
618 }
619 }
620 }
621 return $redirect_url;
622 }
623
624 /**
625 * Reads flexform configuration and merge it with $this->conf
626 *
627 * @return void
628 */
629 protected function mergeflexFormValuesIntoConf() {
630 $flex = array();
631 if ($this->flexFormValue('showForgotPassword', 'sDEF')) {
632 $flex['showForgotPassword'] = $this->flexFormValue('showForgotPassword','sDEF');
633 }
634
635 if ($this->flexFormValue('showPermaLogin', 'sDEF')) {
636 $flex['showPermaLogin'] = $this->flexFormValue('showPermaLogin', 'sDEF');
637 }
638
639 if ($this->flexFormValue('pages', 'sDEF')) {
640 $flex['pages'] = $this->flexFormValue('pages', 'sDEF');
641 }
642
643 if ($this->flexFormValue('recursive', 'sDEF')) {
644 $flex['recursive'] = $this->flexFormValue('recursive', 'sDEF');
645 }
646
647 if ($this->flexFormValue('templateFile', 'sDEF')) {
648 $flex['templateFile'] = $this->uploadDir . $this->flexFormValue('templateFile', 'sDEF');
649 }
650
651 if ($this->flexFormValue('redirectMode', 's_redirect')) {
652 $flex['redirectMode'] = $this->flexFormValue('redirectMode', 's_redirect');
653 }
654
655 if ($this->flexFormValue('redirectFirstMethod', 's_redirect')) {
656 $flex['redirectFirstMethod'] = $this->flexFormValue('redirectFirstMethod', 's_redirect');
657 }
658
659 if ($this->flexFormValue('redirectDisable', 's_redirect')) {
660 $flex['redirectDisable'] = $this->flexFormValue('redirectDisable', 's_redirect');
661 }
662
663 if ($this->flexFormValue('redirectPageLogin', 's_redirect')) {
664 $flex['redirectPageLogin'] = $this->flexFormValue('redirectPageLogin', 's_redirect');
665 }
666
667 if ($this->flexFormValue('redirectPageLoginError', 's_redirect')) {
668 $flex['redirectPageLoginError'] = $this->flexFormValue('redirectPageLoginError','s_redirect');
669 }
670
671 if ($this->flexFormValue('redirectPageLogout', 's_redirect')) {
672 $flex['redirectPageLogout'] = $this->flexFormValue('redirectPageLogout', 's_redirect');
673 }
674
675 $pid = $flex['pages'] ? $this->pi_getPidList($flex['pages'], $flex['recursive']) : 0;
676 if ($pid > 0) {
677 $flex['storagePid'] = $pid;
678 }
679
680 $this->conf = array_merge($this->conf, $flex);
681 }
682
683 /**
684 * Loads a variable from the flexform
685 *
686 * @param string name of variable
687 * @param string name of sheet
688 * @return string value of var
689 */
690 protected function flexFormValue($var, $sheet) {
691 return $this->pi_getFFvalue($this->cObj->data['pi_flexform'], $var,$sheet);
692 }
693
694 /**
695 * Generate link with typolink function
696 *
697 * @param string linktext
698 * @param array link vars
699 * @param boolean true: returns only url false (default) returns the link)
700 *
701 * @return string link or url
702 */
703 protected function getPageLink($label, $piVars,$returnUrl = false) {
704 $additionalParams = '';
705
706 if (count($piVars)) {
707 foreach($piVars as $key=>$val) {
708 $additionalParams .= '&' . $key . '=' . $val;
709 }
710 }
711 // should GETvars be preserved?
712 if ($this->conf['preserveGETvars']) {
713 $additionalParams .= $this->getPreserveGetVars();
714 }
715
716 $this->conf['linkConfig.']['parameter'] = $GLOBALS['TSFE']->id;
717 if ($additionalParams) {
718 $this->conf['linkConfig.']['additionalParams'] = $additionalParams;
719 }
720
721 if ($returnUrl) {
722 return htmlspecialchars($this->cObj->typolink_url($this->conf['linkConfig.']));
723 } else {
724 return $this->cObj->typolink($label,$this->conf['linkConfig.']);
725 }
726 }
727
728 /**
729 * Is used by TS-setting preserveGETvars
730 * possible values are "all" or a commaseperated list of GET-vars
731 * they are used as additionalParams for link generation
732 *
733 * @return string additionalParams-string
734 */
735 protected function getPreserveGetVars() {
736
737 $params = '';
738 $preserveVars =! ($this->conf['preserveGETvars'] || $this->conf['preserveGETvars']=='all' ? array() : implode(',', (array)$this->conf['preserveGETvars']));
739 $getVars = t3lib_div::_GET();
740
741 foreach ($getVars as $key => $val) {
742 if (stristr($key,$this->prefixId) === false) {
743 if (is_array($val)) {
744 foreach ($val as $key1 => $val1) {
745 if ($this->conf['preserveGETvars'] == 'all' || in_array($key . '[' . $key1 .']', $preserveVars)) {
746 $params .= '&' . $key . '[' . $key1 . ']=' . $val1;
747 }
748 }
749 } else {
750 if (!in_array($key, array('id','no_cache','logintype','redirect_url','cHash'))) {
751 $params .= '&' . $key . '=' . $val;
752 }
753 }
754 }
755 }
756 return $params;
757 }
758
759 /**
760 * Is used by forgot password - function with md5 option.
761 *
762 * @author Bernhard Kraft
763 *
764 * @param int length of new password
765 * @return string new password
766 */
767 protected function generatePassword($len) {
768 $pass = '';
769 while ($len--) {
770 $char = rand(0,35);
771 if ($char < 10) {
772 $pass .= '' . $char;
773 } else {
774 $pass .= chr($char - 10 + 97);
775 }
776 }
777 return $pass;
778 }
779
780 /**
781 * Returns the header / message value from flexform if present, else from locallang.xml
782 *
783 * @param string label name
784 * @param string TS stdWrap array
785 * @return string label text
786 */
787 protected function getDisplayText($label, $stdWrapArray=array()) {
788 $text = $this->flexFormValue($label, 's_messages') ? $this->cObj->stdWrap($this->flexFormValue($label, 's_messages'), $stdWrapArray) : $this->cObj->stdWrap($this->pi_getLL('ll_'.$label, '', 1), $stdWrapArray);
789 $replace = $this->getUserFieldMarkers();
790 return strtr($text, $replace);
791 }
792
793 /**
794 * Returns Array of markers filled with user fields
795 *
796 * @return array marker array
797 */
798 protected function getUserFieldMarkers() {
799 $marker = array();
800 // replace markers with fe_user data
801 if ($GLOBALS['TSFE']->fe_user->user) {
802 // all fields of fe_user will be replaced, scheme is ###FEUSER_FIELDNAME###
803 foreach ($GLOBALS['TSFE']->fe_user->user as $field => $value) {
804 $marker['###FEUSER_' . t3lib_div::strtoupper($field) . '###'] = $this->cObj->stdWrap($value, $this->conf['userfields.'][$field . '.']);
805 }
806 // add ###USER### for compatibility
807 $marker['###USER###'] = $marker['###FEUSER_USERNAME###'];
808 }
809 return $marker;
810 }
811 }
812
813
814
815 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/felogin/pi1/class.tx_felogin_pi1.php']) {
816 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/felogin/pi1/class.tx_felogin_pi1.php']);
817 }
818
819 ?>