2f22216cac39610c62ce0a35212bf36fe2100d47
[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 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) {
514 $markerArray['###PERMALOGIN###'] = $this->pi_getLL('permalogin', '', 1);
515 if($GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'] == 1) {
516 $markerArray['###PERMALOGIN_HIDDENFIELD_ATTRIBUTES###'] = 'disabled="disabled"';
517 $markerArray['###PERMALOGIN_CHECKBOX_ATTRIBUTES###'] = 'checked="checked"';
518 } else {
519 $markerArray['###PERMALOGIN_HIDDENFIELD_ATTRIBUTES###'] = '';
520 $markerArray['###PERMALOGIN_CHECKBOX_ATTRIBUTES###'] = '';
521 }
522 } else {
523 $subpartArray['###PERMALOGIN_VALID###'] = '';
524 }
525 return $this->cObj->substituteMarkerArrayCached($subpart, $markerArray, $subpartArray, $linkpartArray);
526 }
527
528 /**
529 * Process redirect methods. The function searches for a redirect url using all configured methods.
530 *
531 * @return string redirect url
532 */
533 protected function processRedirect() {
534 $redirect_url = array();
535 if ($this->conf['redirectMode']) {
536 $redirectMethods = t3lib_div::trimExplode(',', $this->conf['redirectMode'], TRUE);
537 foreach ($redirectMethods as $redirMethod) {
538 if ($GLOBALS['TSFE']->loginUser && $this->logintype === 'login') {
539 // logintype is needed because the login-page wouldn't be accessible anymore after a login (would always redirect)
540 switch ($redirMethod) {
541 case 'groupLogin': // taken from dkd_redirect_at_login written by Ingmar Schlecht; database-field changed
542 $groupData = $GLOBALS['TSFE']->fe_user->groupData;
543 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
544 'felogin_redirectPid',
545 $GLOBALS['TSFE']->fe_user->usergroup_table,
546 'felogin_redirectPid!="" AND uid IN (' . implode(',', $groupData['uid']) . ')'
547 );
548 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
549 $redirect_url[] = $this->pi_getPageLink($row[0], array(), TRUE); // take the first group with a redirect page
550 }
551 break;
552 case 'userLogin':
553 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
554 'felogin_redirectPid',
555 $GLOBALS['TSFE']->fe_user->user_table,
556 $GLOBALS['TSFE']->fe_user->userid_column . '=' . $GLOBALS['TSFE']->fe_user->user['uid'] . ' AND felogin_redirectPid!=""'
557 );
558 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res)) {
559 $redirect_url[] = $this->pi_getPageLink($row[0], array(), TRUE);
560 }
561 break;
562 case 'login':
563 if ($this->conf['redirectPageLogin']) {
564 $redirect_url[] = $this->pi_getPageLink(intval($this->conf['redirectPageLogin']), array(), TRUE);
565 }
566 break;
567 case 'getpost':
568 $redirect_url[] = $this->redirectUrl;
569 break;
570 case 'referer':
571 // avoid forced logout, when trying to login immediatly after a logout
572 $redirect_url[] = preg_replace('/[&?]logintype=[a-z]+/', '', $this->referer);
573 break;
574 case 'refererDomains':
575 // Auto redirect.
576 // Feature to redirect to the page where the user came from (HTTP_REFERER).
577 // Allowed domains to redirect to, can be configured with plugin.tx_felogin_pi1.domains
578 // Thanks to plan2.net / Martin Kutschker for implementing this feature.
579 if ($this->conf['domains']) {
580 $url = $this->referer;
581 // is referring url allowed to redirect?
582 $match = array();
583 if (preg_match('/^http://([[:alnum:]._-]+)//', $url, $match)) {
584 $redirect_domain = $match[1];
585 $found = false;
586 foreach(split(',', $this->conf['domains']) as $d) {
587 if (preg_match('/(^|\.)/'.$d.'$', $redirect_domain)) {
588 $found = true;
589 break;
590 }
591 }
592 if (!$found) {
593 $url = '';
594 }
595 }
596
597 // Avoid forced logout, when trying to login immediatly after a logout
598 if ($url) {
599 $redirect_url[] = preg_replace('/[&?]logintype=[a-z]+/', '', $url);
600 }
601 }
602 break;
603 }
604 } else if ($this->logintype === 'login') { // after login-error
605 switch ($redirMethod) {
606 case 'loginError':
607 if ($this->conf['redirectPageLoginError']) {
608 $redirect_url[] = $this->pi_getPageLink(intval($this->conf['redirectPageLoginError']), array(), TRUE);
609 }
610 break;
611 }
612 } elseif (($this->logintype == '') && ($redirMethod == 'login') && $this->conf['redirectPageLogin']) {
613 // if login and page not accessible
614 $this->cObj->typolink('', array(
615 'parameter' => $this->conf['redirectPageLogin'],
616 'linkAccessRestrictedPages' => TRUE,
617 ));
618 $redirect_url[] = $this->cObj->lastTypoLinkUrl;
619
620 } elseif (($this->logintype == '') && ($redirMethod == 'logout') && $this->conf['redirectPageLogout'] && $GLOBALS['TSFE']->loginUser) {
621 // if logout and page not accessible
622 $redirect_url[] = $this->pi_getPageLink(intval($this->conf['redirectPageLogout']), array(), TRUE);
623
624 } elseif ($this->logintype === 'logout') { // after logout
625
626 // Hook for general actions after after logout has been confirmed
627 if ($this->logintype === 'logout' && $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['logout_confirmed']) {
628 $_params = array();
629 foreach($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['logout_confirmed'] as $_funcRef) {
630 if ($_funcRef) {
631 t3lib_div::callUserFunction($_funcRef, $_params, $this);
632 }
633 }
634 }
635
636 switch ($redirMethod) {
637 case 'logout':
638 if ($this->conf['redirectPageLogout']) {
639 $redirect_url[] = $this->pi_getPageLink(intval($this->conf['redirectPageLogout']), array(), TRUE);
640 }
641 break;
642 }
643 } else { // not logged in
644 // Placeholder for maybe future options
645 switch ($redirMethod) {
646 case 'getpost':
647 // preserve the get/post value
648 $redirect_url[] = $this->redirectUrl;
649 break;
650 }
651 }
652
653 }
654 }
655 // remove empty values
656 if (count($redirect_url)) {
657 return t3lib_div::trimExplode(',', implode(',', $redirect_url), TRUE);
658 } else {
659 return array();
660 }
661 }
662
663 /**
664 * Reads flexform configuration and merge it with $this->conf
665 *
666 * @return void
667 */
668 protected function mergeflexFormValuesIntoConf() {
669 $flex = array();
670 if ($this->flexFormValue('showForgotPassword', 'sDEF')) {
671 $flex['showForgotPassword'] = $this->flexFormValue('showForgotPassword','sDEF');
672 }
673
674 if ($this->flexFormValue('showPermaLogin', 'sDEF')) {
675 $flex['showPermaLogin'] = $this->flexFormValue('showPermaLogin', 'sDEF');
676 }
677
678 if ($this->flexFormValue('pages', 'sDEF')) {
679 $flex['pages'] = $this->flexFormValue('pages', 'sDEF');
680 }
681
682 if ($this->flexFormValue('recursive', 'sDEF')) {
683 $flex['recursive'] = $this->flexFormValue('recursive', 'sDEF');
684 }
685
686 if ($this->flexFormValue('templateFile', 'sDEF')) {
687 $flex['templateFile'] = $this->uploadDir . $this->flexFormValue('templateFile', 'sDEF');
688 }
689
690 if ($this->flexFormValue('redirectMode', 's_redirect')) {
691 $flex['redirectMode'] = $this->flexFormValue('redirectMode', 's_redirect');
692 }
693
694 if ($this->flexFormValue('redirectFirstMethod', 's_redirect')) {
695 $flex['redirectFirstMethod'] = $this->flexFormValue('redirectFirstMethod', 's_redirect');
696 }
697
698 if ($this->flexFormValue('redirectDisable', 's_redirect')) {
699 $flex['redirectDisable'] = $this->flexFormValue('redirectDisable', 's_redirect');
700 }
701
702 if ($this->flexFormValue('redirectPageLogin', 's_redirect')) {
703 $flex['redirectPageLogin'] = $this->flexFormValue('redirectPageLogin', 's_redirect');
704 }
705
706 if ($this->flexFormValue('redirectPageLoginError', 's_redirect')) {
707 $flex['redirectPageLoginError'] = $this->flexFormValue('redirectPageLoginError','s_redirect');
708 }
709
710 if ($this->flexFormValue('redirectPageLogout', 's_redirect')) {
711 $flex['redirectPageLogout'] = $this->flexFormValue('redirectPageLogout', 's_redirect');
712 }
713
714 $pid = $flex['pages'] ? $this->pi_getPidList($flex['pages'], $flex['recursive']) : 0;
715 if ($pid > 0) {
716 $flex['storagePid'] = $pid;
717 }
718
719 $this->conf = array_merge($this->conf, $flex);
720 }
721
722 /**
723 * Loads a variable from the flexform
724 *
725 * @param string name of variable
726 * @param string name of sheet
727 * @return string value of var
728 */
729 protected function flexFormValue($var, $sheet) {
730 return $this->pi_getFFvalue($this->cObj->data['pi_flexform'], $var,$sheet);
731 }
732
733 /**
734 * Generate link with typolink function
735 *
736 * @param string linktext
737 * @param array link vars
738 * @param boolean true: returns only url false (default) returns the link)
739 *
740 * @return string link or url
741 */
742 protected function getPageLink($label, $piVars,$returnUrl = false) {
743 $additionalParams = '';
744
745 if (count($piVars)) {
746 foreach($piVars as $key=>$val) {
747 $additionalParams .= '&' . $key . '=' . $val;
748 }
749 }
750 // should GETvars be preserved?
751 if ($this->conf['preserveGETvars']) {
752 $additionalParams .= $this->getPreserveGetVars();
753 }
754
755 $this->conf['linkConfig.']['parameter'] = $GLOBALS['TSFE']->id;
756 if ($additionalParams) {
757 $this->conf['linkConfig.']['additionalParams'] = $additionalParams;
758 }
759
760 if ($returnUrl) {
761 return htmlspecialchars($this->cObj->typolink_url($this->conf['linkConfig.']));
762 } else {
763 return $this->cObj->typolink($label,$this->conf['linkConfig.']);
764 }
765 }
766
767 /**
768 * Is used by TS-setting preserveGETvars
769 * possible values are "all" or a commaseperated list of GET-vars
770 * they are used as additionalParams for link generation
771 *
772 * @return string additionalParams-string
773 */
774 protected function getPreserveGetVars() {
775
776 $params = '';
777 $preserveVars =! ($this->conf['preserveGETvars'] || $this->conf['preserveGETvars']=='all' ? array() : implode(',', (array)$this->conf['preserveGETvars']));
778 $getVars = t3lib_div::_GET();
779
780 foreach ($getVars as $key => $val) {
781 if (stristr($key,$this->prefixId) === false) {
782 if (is_array($val)) {
783 foreach ($val as $key1 => $val1) {
784 if ($this->conf['preserveGETvars'] == 'all' || in_array($key . '[' . $key1 .']', $preserveVars)) {
785 $params .= '&' . $key . '[' . $key1 . ']=' . $val1;
786 }
787 }
788 } else {
789 if (!in_array($key, array('id','no_cache','logintype','redirect_url','cHash'))) {
790 $params .= '&' . $key . '=' . $val;
791 }
792 }
793 }
794 }
795 return $params;
796 }
797
798 /**
799 * Is used by forgot password - function with md5 option.
800 *
801 * @author Bernhard Kraft
802 *
803 * @param int length of new password
804 * @return string new password
805 */
806 protected function generatePassword($len) {
807 $pass = '';
808 while ($len--) {
809 $char = rand(0,35);
810 if ($char < 10) {
811 $pass .= '' . $char;
812 } else {
813 $pass .= chr($char - 10 + 97);
814 }
815 }
816 return $pass;
817 }
818
819 /**
820 * Returns the header / message value from flexform if present, else from locallang.xml
821 *
822 * @param string label name
823 * @param string TS stdWrap array
824 * @return string label text
825 */
826 protected function getDisplayText($label, $stdWrapArray=array()) {
827 $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);
828 $replace = $this->getUserFieldMarkers();
829 return strtr($text, $replace);
830 }
831
832 /**
833 * Returns Array of markers filled with user fields
834 *
835 * @return array marker array
836 */
837 protected function getUserFieldMarkers() {
838 $marker = array();
839 // replace markers with fe_user data
840 if ($GLOBALS['TSFE']->fe_user->user) {
841 // all fields of fe_user will be replaced, scheme is ###FEUSER_FIELDNAME###
842 foreach ($GLOBALS['TSFE']->fe_user->user as $field => $value) {
843 $marker['###FEUSER_' . t3lib_div::strtoupper($field) . '###'] = $this->cObj->stdWrap($value, $this->conf['userfields.'][$field . '.']);
844 }
845 // add ###USER### for compatibility
846 $marker['###USER###'] = $marker['###FEUSER_USERNAME###'];
847 }
848 return $marker;
849 }
850 }
851
852
853
854 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/felogin/pi1/class.tx_felogin_pi1.php']) {
855 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/felogin/pi1/class.tx_felogin_pi1.php']);
856 }
857
858 ?>