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