[CLEANUP] Remove deprecated filemount functions in BE_USER
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Authentication / BackendUserAuthentication.php
1 <?php
2 namespace TYPO3\CMS\Core\Authentication;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
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 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31 use TYPO3\CMS\Backend\Utility\BackendUtility;
32
33 /**
34 * TYPO3 backend user authentication
35 * Contains most of the functions used for checking permissions, authenticating users,
36 * setting up the user, and API for user from outside.
37 * This class contains the configuration of the database fields used plus some
38 * functions for the authentication process of backend users.
39 *
40 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
41 * @internal
42 */
43 class BackendUserAuthentication extends \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication {
44
45 /**
46 * Should be set to the usergroup-column (id-list) in the user-record
47 * @var string
48 * @todo Define visibility
49 */
50 public $usergroup_column = 'usergroup';
51
52 /**
53 * The name of the group-table
54 * @var string
55 * @todo Define visibility
56 */
57 public $usergroup_table = 'be_groups';
58
59 /**
60 * holds lists of eg. tables, fields and other values related to the permission-system. See fetchGroupData
61 * @var array
62 * @todo Define visibility
63 * @internal
64 */
65 public $groupData = array(
66 'filemounts' => array()
67 );
68
69 /**
70 * This array will hold the groups that the user is a member of
71 * @var array
72 * @todo Define visibility
73 */
74 public $userGroups = array();
75
76 /**
77 * This array holds the uid's of the groups in the listed order
78 * @var array
79 * @todo Define visibility
80 */
81 public $userGroupsUID = array();
82
83 /**
84 * This is $this->userGroupsUID imploded to a comma list... Will correspond to the 'usergroup_cached_list'
85 * @var string
86 * @todo Define visibility
87 */
88 public $groupList = '';
89
90 /**
91 * User workspace.
92 * -99 is ERROR (none available)
93 * -1 is offline
94 * 0 is online
95 * >0 is custom workspaces
96 * @var int
97 * @todo Define visibility
98 */
99 public $workspace = -99;
100
101 /**
102 * Custom workspace record if any
103 * @var array
104 * @todo Define visibility
105 */
106 public $workspaceRec = array();
107
108 /**
109 * Used to accumulate data for the user-group.
110 * DON NOT USE THIS EXTERNALLY!
111 * Use $this->groupData instead
112 * @var array
113 * @todo Define visibility
114 * @internal
115 */
116 public $dataLists = array(
117 'webmount_list' => '',
118 'filemount_list' => '',
119 'file_permissions' => '',
120 'modList' => '',
121 'tables_select' => '',
122 'tables_modify' => '',
123 'pagetypes_select' => '',
124 'non_exclude_fields' => '',
125 'explicit_allowdeny' => '',
126 'allowed_languages' => '',
127 'workspace_perms' => '',
128 'custom_options' => ''
129 );
130
131 /**
132 * For debugging/display of order in which subgroups are included.
133 * @var array
134 * @todo Define visibility
135 */
136 public $includeHierarchy = array();
137
138 /**
139 * List of group_id's in the order they are processed.
140 * @var array
141 * @todo Define visibility
142 */
143 public $includeGroupArray = array();
144
145 /**
146 * Set to 'WIN', if windows
147 * @var string
148 * @todo Define visibility
149 */
150 public $OS = '';
151
152 /**
153 * Used to accumulate the TSconfig data of the user
154 * @var array
155 * @todo Define visibility
156 */
157 public $TSdataArray = array();
158
159 /**
160 * Contains the non-parsed user TSconfig
161 * @var string
162 * @todo Define visibility
163 */
164 public $userTS_text = '';
165
166 /**
167 * Contains the parsed user TSconfig
168 * @var array
169 * @todo Define visibility
170 */
171 public $userTS = array();
172
173 /**
174 * Set internally if the user TSconfig was parsed and needs to be cached.
175 * @var bool
176 * @todo Define visibility
177 */
178 public $userTSUpdated = FALSE;
179
180 /**
181 * Set this from outside if you want the user TSconfig to ALWAYS be parsed and not fetched from cache.
182 * @var bool
183 * @todo Define visibility
184 */
185 public $userTS_dontGetCached = FALSE;
186
187 /**
188 * RTE availability errors collected.
189 * @var array
190 * @todo Define visibility
191 */
192 public $RTE_errors = array();
193
194 /**
195 * Contains last error message
196 * @var string
197 * @todo Define visibility
198 */
199 public $errorMsg = '';
200
201 /**
202 * Cache for checkWorkspaceCurrent()
203 * @var array|NULL
204 * @todo Define visibility
205 */
206 public $checkWorkspaceCurrent_cache = NULL;
207
208 /**
209 * @var \TYPO3\CMS\Core\Resource\ResourceStorage[]
210 */
211 protected $fileStorages;
212
213 /**
214 * @var array
215 */
216 protected $filePermissions;
217
218 /**
219 * Table to use for session data
220 * @var string
221 * @todo Define visibility
222 */
223 public $session_table = 'be_sessions';
224
225 /**
226 * Table in database with user data
227 * @var string
228 * @todo Define visibility
229 */
230 public $user_table = 'be_users';
231
232 /**
233 * Column for login-name
234 * @var string
235 * @todo Define visibility
236 */
237 public $username_column = 'username';
238
239 /**
240 * Column for password
241 * @var string
242 * @todo Define visibility
243 */
244 public $userident_column = 'password';
245
246 /**
247 * Column for user-id
248 * @var string
249 * @todo Define visibility
250 */
251 public $userid_column = 'uid';
252
253 /**
254 * @var string
255 * @todo Define visibility
256 */
257 public $lastLogin_column = 'lastlogin';
258
259 /**
260 * @var array
261 * @todo Define visibility
262 */
263 public $enablecolumns = array(
264 'rootLevel' => 1,
265 'deleted' => 'deleted',
266 'disabled' => 'disable',
267 'starttime' => 'starttime',
268 'endtime' => 'endtime'
269 );
270
271 /**
272 * Form field with login-name
273 * @var string
274 * @todo Define visibility
275 */
276 public $formfield_uname = 'username';
277
278 /**
279 * Form field with password
280 * @var string
281 * @todo Define visibility
282 */
283 public $formfield_uident = 'userident';
284
285 /**
286 * Form field with a unique value which is used to encrypt the password and username
287 * @var string
288 * @todo Define visibility
289 */
290 public $formfield_chalvalue = 'challenge';
291
292 /**
293 * Form field with status: *'login', 'logout'
294 * @var string
295 * @todo Define visibility
296 */
297 public $formfield_status = 'login_status';
298
299 /**
300 * Decides if the writelog() function is called at login and logout
301 * @var bool
302 * @todo Define visibility
303 */
304 public $writeStdLog = TRUE;
305
306 /**
307 * If the writelog() functions is called if a login-attempt has be tried without success
308 * @var bool
309 * @todo Define visibility
310 */
311 public $writeAttemptLog = TRUE;
312
313 /**
314 * if > 0 : session-timeout in seconds.
315 * if FALSE/<0 : no timeout.
316 * if string: The string is field name from the user table where the timeout can be found.
317 * @var string|int
318 * @todo Define visibility
319 */
320 public $auth_timeout_field = 6000;
321
322 /**
323 * @var bool
324 * @todo Define visibility
325 */
326 public $challengeStoredInCookie = TRUE;
327
328 /**
329 * @var int
330 */
331 public $firstMainGroup = 0;
332
333 /**
334 * User Config
335 * @var array
336 * @todo Define visibility
337 */
338 public $uc;
339
340 /**
341 * User Config Default values:
342 * The array may contain other fields for configuration.
343 * For this, see "setup" extension and "TSConfig" document (User TSconfig, "setup.[xxx]....")
344 * Reserved keys for other storage of session data:
345 * moduleData
346 * moduleSessionID
347 * @var array
348 * @todo Define visibility
349 */
350 public $uc_default = array(
351 'interfaceSetup' => '',
352 // serialized content that is used to store interface pane and menu positions. Set by the logout.php-script
353 'moduleData' => array(),
354 // user-data for the modules
355 'thumbnailsByDefault' => 1,
356 'emailMeAtLogin' => 0,
357 'noMenuMode' => 0,
358 'startModule' => 'help_aboutmodules',
359 'hideSubmoduleIcons' => 0,
360 'helpText' => 1,
361 'titleLen' => 50,
362 'edit_showFieldHelp' => 'icon',
363 'edit_RTE' => '1',
364 'edit_docModuleUpload' => '1',
365 'navFrameWidth' => '',
366 // Default is 245 pixels
367 'navFrameResizable' => 0,
368 'resizeTextareas' => 1,
369 'resizeTextareas_MaxHeight' => 500,
370 'resizeTextareas_Flexible' => 0
371 );
372
373 /**
374 * Constructor
375 */
376 public function __construct() {
377 parent::__construct();
378 $this->name = self::getCookieName();
379 $this->loginType = 'BE';
380 }
381
382 /**
383 * Returns TRUE if user is admin
384 * Basically this function evaluates if the ->user[admin] field has bit 0 set. If so, user is admin.
385 *
386 * @return boolean
387 * @todo Define visibility
388 */
389 public function isAdmin() {
390 return is_array($this->user) && ($this->user['admin'] & 1) == 1;
391 }
392
393 /**
394 * Returns TRUE if the current user is a member of group $groupId
395 * $groupId must be set. $this->groupList must contain groups
396 * Will return TRUE also if the user is a member of a group through subgroups.
397 *
398 * @param integer $groupId Group ID to look for in $this->groupList
399 * @return boolean
400 * @todo Define visibility
401 */
402 public function isMemberOfGroup($groupId) {
403 $groupId = (int)$groupId;
404 if ($this->groupList && $groupId) {
405 return GeneralUtility::inList($this->groupList, $groupId);
406 }
407 return FALSE;
408 }
409
410 /**
411 * Checks if the permissions is granted based on a page-record ($row) and $perms (binary and'ed)
412 *
413 * Bits for permissions, see $perms variable:
414 *
415 * 1 - Show: See/Copy page and the pagecontent.
416 * 16- Edit pagecontent: Change/Add/Delete/Move pagecontent.
417 * 2- Edit page: Change/Move the page, eg. change title, startdate, hidden.
418 * 4- Delete page: Delete the page and pagecontent.
419 * 8- New pages: Create new pages under the page.
420 *
421 * @param array $row Is the pagerow for which the permissions is checked
422 * @param integer $perms Is the binary representation of the permission we are going to check. Every bit in this number represents a permission that must be set. See function explanation.
423 * @return boolean
424 * @todo Define visibility
425 */
426 public function doesUserHaveAccess($row, $perms) {
427 $userPerms = $this->calcPerms($row);
428 return ($userPerms & $perms) == $perms;
429 }
430
431 /**
432 * Checks if the page id, $id, is found within the webmounts set up for the user.
433 * This should ALWAYS be checked for any page id a user works with, whether it's about reading, writing or whatever.
434 * The point is that this will add the security that a user can NEVER touch parts outside his mounted
435 * pages in the page tree. This is otherwise possible if the raw page permissions allows for it.
436 * So this security check just makes it easier to make safe user configurations.
437 * If the user is admin OR if this feature is disabled
438 * (fx. by setting TYPO3_CONF_VARS['BE']['lockBeUserToDBmounts']=0) then it returns "1" right away
439 * Otherwise the function will return the uid of the webmount which was first found in the rootline of the input page $id
440 *
441 * @param integer $id Page ID to check
442 * @param string $readPerms Content of "->getPagePermsClause(1)" (read-permissions). If not set, they will be internally calculated (but if you have the correct value right away you can save that database lookup!)
443 * @param bool|int $exitOnError If set, then the function will exit with an error message.
444 * @throws \RuntimeException
445 * @return int|NULL The page UID of a page in the rootline that matched a mount point
446 * @todo Define visibility
447 */
448 public function isInWebMount($id, $readPerms = '', $exitOnError = 0) {
449 if (!$GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts'] || $this->isAdmin()) {
450 return 1;
451 }
452 $id = (int)$id;
453 // Check if input id is an offline version page in which case we will map id to the online version:
454 $checkRec = BackendUtility::getRecord('pages', $id, 'pid,t3ver_oid');
455 if ($checkRec['pid'] == -1) {
456 $id = (int)$checkRec['t3ver_oid'];
457 }
458 if (!$readPerms) {
459 $readPerms = $this->getPagePermsClause(1);
460 }
461 if ($id > 0) {
462 $wM = $this->returnWebmounts();
463 $rL = BackendUtility::BEgetRootLine($id, ' AND ' . $readPerms);
464 foreach ($rL as $v) {
465 if ($v['uid'] && in_array($v['uid'], $wM)) {
466 return $v['uid'];
467 }
468 }
469 }
470 if ($exitOnError) {
471 throw new \RuntimeException('Access Error: This page is not within your DB-mounts', 1294586445);
472 }
473 return NULL;
474 }
475
476 /**
477 * Checks access to a backend module with the $MCONF passed as first argument
478 *
479 * @param array $conf $MCONF array of a backend module!
480 * @param boolean $exitOnError If set, an array will issue an error message and exit.
481 * @throws \RuntimeException
482 * @return boolean Will return TRUE if $MCONF['access'] is not set at all, if the BE_USER is admin or if the module is enabled in the be_users/be_groups records of the user (specifically enabled). Will return FALSE if the module name is not even found in $TBE_MODULES
483 * @todo Define visibility
484 */
485 public function modAccess($conf, $exitOnError) {
486 if (!BackendUtility::isModuleSetInTBE_MODULES($conf['name'])) {
487 if ($exitOnError) {
488 throw new \RuntimeException('Fatal Error: This module "' . $conf['name'] . '" is not enabled in TBE_MODULES', 1294586446);
489 }
490 return FALSE;
491 }
492 // Workspaces check:
493 if (
494 !empty($conf['workspaces'])
495 && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')
496 && ($this->workspace !== 0 || !GeneralUtility::inList($conf['workspaces'], 'online'))
497 && ($this->workspace !== -1 || !GeneralUtility::inList($conf['workspaces'], 'offline'))
498 && ($this->workspace <= 0 || !GeneralUtility::inList($conf['workspaces'], 'custom'))
499 ) {
500 if ($exitOnError) {
501 throw new \RuntimeException('Workspace Error: This module "' . $conf['name'] . '" is not available under the current workspace', 1294586447);
502 }
503 return FALSE;
504 }
505 // Returns TRUE if conf[access] is not set at all or if the user is admin
506 if (!$conf['access'] || $this->isAdmin()) {
507 return TRUE;
508 }
509 // If $conf['access'] is set but not with 'admin' then we return TRUE, if the module is found in the modList
510 $acs = FALSE;
511 if (!strstr($conf['access'], 'admin') && $conf['name']) {
512 $acs = $this->check('modules', $conf['name']);
513 }
514 if (!$acs && $exitOnError) {
515 throw new \RuntimeException('Access Error: You don\'t have access to this module.', 1294586448);
516 } else {
517 return $acs;
518 }
519 }
520
521 /**
522 * Returns a WHERE-clause for the pages-table where user permissions according to input argument, $perms, is validated.
523 * $perms is the "mask" used to select. Fx. if $perms is 1 then you'll get all pages that a user can actually see!
524 * 2^0 = show (1)
525 * 2^1 = edit (2)
526 * 2^2 = delete (4)
527 * 2^3 = new (8)
528 * If the user is 'admin' " 1=1" is returned (no effect)
529 * If the user is not set at all (->user is not an array), then " 1=0" is returned (will cause no selection results at all)
530 * The 95% use of this function is "->getPagePermsClause(1)" which will
531 * return WHERE clauses for *selecting* pages in backend listings - in other words this will check read permissions.
532 *
533 * @param integer $perms Permission mask to use, see function description
534 * @return string Part of where clause. Prefix " AND " to this.
535 * @todo Define visibility
536 */
537 public function getPagePermsClause($perms) {
538 if (is_array($this->user)) {
539 if ($this->isAdmin()) {
540 return ' 1=1';
541 }
542 $perms = (int)$perms;
543 // Make sure it's integer.
544 $str = ' (' . '(pages.perms_everybody & ' . $perms . ' = ' . $perms . ')' . ' OR (pages.perms_userid = '
545 . $this->user['uid'] . ' AND pages.perms_user & ' . $perms . ' = ' . $perms . ')';
546 // User
547 if ($this->groupList) {
548 // Group (if any is set)
549 $str .= ' OR (pages.perms_groupid in (' . $this->groupList . ') AND pages.perms_group & '
550 . $perms . ' = ' . $perms . ')';
551 }
552 $str .= ')';
553 // ****************
554 // getPagePermsClause-HOOK
555 // ****************
556 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['getPagePermsClause'])) {
557 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['getPagePermsClause'] as $_funcRef) {
558 $_params = array('currentClause' => $str, 'perms' => $perms);
559 $str = GeneralUtility::callUserFunction($_funcRef, $_params, $this);
560 }
561 }
562 return $str;
563 } else {
564 return ' 1=0';
565 }
566 }
567
568 /**
569 * Returns a combined binary representation of the current users permissions for the page-record, $row.
570 * The perms for user, group and everybody is OR'ed together (provided that the page-owner is the user
571 * and for the groups that the user is a member of the group.
572 * If the user is admin, 31 is returned (full permissions for all five flags)
573 *
574 * @param array $row Input page row with all perms_* fields available.
575 * @return integer Bitwise representation of the users permissions in relation to input page row, $row
576 * @todo Define visibility
577 */
578 public function calcPerms($row) {
579 // Return 31 for admin users.
580 if ($this->isAdmin()) {
581 return 31;
582 }
583 // Return 0 if page is not within the allowed web mount
584 if (!$this->isInWebMount($row['uid'])) {
585 return 0;
586 }
587 $out = 0;
588 if (
589 isset($row['perms_userid']) && isset($row['perms_user']) && isset($row['perms_groupid'])
590 && isset($row['perms_group']) && isset($row['perms_everybody']) && isset($this->groupList)
591 ) {
592 if ($this->user['uid'] == $row['perms_userid']) {
593 $out |= $row['perms_user'];
594 }
595 if ($this->isMemberOfGroup($row['perms_groupid'])) {
596 $out |= $row['perms_group'];
597 }
598 $out |= $row['perms_everybody'];
599 }
600 // ****************
601 // CALCPERMS hook
602 // ****************
603 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['calcPerms'])) {
604 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['calcPerms'] as $_funcRef) {
605 $_params = array(
606 'row' => $row,
607 'outputPermissions' => $out
608 );
609 $out = GeneralUtility::callUserFunction($_funcRef, $_params, $this);
610 }
611 }
612 return $out;
613 }
614
615 /**
616 * Returns TRUE if the RTE (Rich Text Editor) can be enabled for the user
617 * Strictly this is not permissions being checked but rather a series of settings like
618 * a loaded extension, browser/client type and a configuration option in ->uc[edit_RTE]
619 * The reasons for a FALSE return can be found in $this->RTE_errors
620 *
621 * @return boolean
622 * @todo Define visibility
623 */
624 public function isRTE() {
625 // Start:
626 $this->RTE_errors = array();
627 if (!$this->uc['edit_RTE']) {
628 $this->RTE_errors[] = 'RTE is not enabled for user!';
629 }
630 if (!$GLOBALS['TYPO3_CONF_VARS']['BE']['RTEenabled']) {
631 $this->RTE_errors[] = 'RTE is not enabled in $TYPO3_CONF_VARS["BE"]["RTEenabled"]';
632 }
633 // Acquire RTE object:
634 $RTE = BackendUtility::RTEgetObj();
635 if (!is_object($RTE)) {
636 $this->RTE_errors = array_merge($this->RTE_errors, $RTE);
637 }
638 if (!count($this->RTE_errors)) {
639 return TRUE;
640 } else {
641 return FALSE;
642 }
643 }
644
645 /**
646 * Returns TRUE if the $value is found in the list in a $this->groupData[] index pointed to by $type (array key).
647 * Can thus be users to check for modules, exclude-fields, select/modify permissions for tables etc.
648 * If user is admin TRUE is also returned
649 * Please see the document Inside TYPO3 for examples.
650 *
651 * @param string $type The type value; "webmounts", "filemounts", "pagetypes_select", "tables_select", "tables_modify", "non_exclude_fields", "modules
652 * @param string $value String to search for in the groupData-list
653 * @return boolean TRUE if permission is granted (that is, the value was found in the groupData list - or the BE_USER is "admin")
654 * @todo Define visibility
655 */
656 public function check($type, $value) {
657 if (isset($this->groupData[$type])) {
658 if ($this->isAdmin() || GeneralUtility::inList($this->groupData[$type], $value)) {
659 return TRUE;
660 }
661 }
662 return FALSE;
663 }
664
665 /**
666 * Checking the authMode of a select field with authMode set
667 *
668 * @param string $table Table name
669 * @param string $field Field name (must be configured in TCA and of type "select" with authMode set!)
670 * @param string $value Value to evaluation (single value, must not contain any of the chars ":,|")
671 * @param string $authMode Auth mode keyword (explicitAllow, explicitDeny, individual)
672 * @return boolean Whether access is granted or not
673 * @todo Define visibility
674 */
675 public function checkAuthMode($table, $field, $value, $authMode) {
676 // Admin users can do anything:
677 if ($this->isAdmin()) {
678 return TRUE;
679 }
680 // Allow all blank values:
681 if ((string)$value === '') {
682 return TRUE;
683 }
684 // Certain characters are not allowed in the value
685 if (preg_match('/[:|,]/', $value)) {
686 return FALSE;
687 }
688 // Initialize:
689 $testValue = $table . ':' . $field . ':' . $value;
690 $out = TRUE;
691 // Checking value:
692 switch ((string) $authMode) {
693 case 'explicitAllow':
694 if (!GeneralUtility::inList($this->groupData['explicit_allowdeny'], ($testValue . ':ALLOW'))) {
695 $out = FALSE;
696 }
697 break;
698 case 'explicitDeny':
699 if (GeneralUtility::inList($this->groupData['explicit_allowdeny'], $testValue . ':DENY')) {
700 $out = FALSE;
701 }
702 break;
703 case 'individual':
704 if (is_array($GLOBALS['TCA'][$table]) && is_array($GLOBALS['TCA'][$table]['columns'][$field])) {
705 $items = $GLOBALS['TCA'][$table]['columns'][$field]['config']['items'];
706 if (is_array($items)) {
707 foreach ($items as $iCfg) {
708 if ((string)$iCfg[1] === (string)$value && $iCfg[4]) {
709 switch ((string)$iCfg[4]) {
710 case 'EXPL_ALLOW':
711 if (!GeneralUtility::inList($this->groupData['explicit_allowdeny'], ($testValue . ':ALLOW'))) {
712 $out = FALSE;
713 }
714 break;
715 case 'EXPL_DENY':
716 if (GeneralUtility::inList($this->groupData['explicit_allowdeny'], $testValue . ':DENY')) {
717 $out = FALSE;
718 }
719 break;
720 }
721 break;
722 }
723 }
724 }
725 }
726 break;
727 }
728 return $out;
729 }
730
731 /**
732 * Checking if a language value (-1, 0 and >0 for sys_language records) is allowed to be edited by the user.
733 *
734 * @param integer $langValue Language value to evaluate
735 * @return boolean Returns TRUE if the language value is allowed, otherwise FALSE.
736 * @todo Define visibility
737 */
738 public function checkLanguageAccess($langValue) {
739 // The users language list must be non-blank - otherwise all languages are allowed.
740 if (trim($this->groupData['allowed_languages']) !== '') {
741 $langValue = (int)$langValue;
742 // Language must either be explicitly allowed OR the lang Value be "-1" (all languages)
743 if ($langValue != -1 && !$this->check('allowed_languages', $langValue)) {
744 return FALSE;
745 }
746 }
747 return TRUE;
748 }
749
750 /**
751 * Check if user has access to all existing localizations for a certain record
752 *
753 * @param string $table The table
754 * @param array $record The current record
755 * @return boolean
756 * @todo Define visibility
757 */
758 public function checkFullLanguagesAccess($table, $record) {
759 $recordLocalizationAccess = $this->checkLanguageAccess(0);
760 if ($recordLocalizationAccess && (BackendUtility::isTableLocalizable($table) || isset($GLOBALS['TCA'][$table]['ctrl']['transForeignTable']))) {
761 if (isset($GLOBALS['TCA'][$table]['ctrl']['transForeignTable'])) {
762 $l10nTable = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'];
763 $pointerField = $GLOBALS['TCA'][$l10nTable]['ctrl']['transOrigPointerField'];
764 $pointerValue = $record['uid'];
765 } else {
766 $l10nTable = $table;
767 $pointerField = $GLOBALS['TCA'][$l10nTable]['ctrl']['transOrigPointerField'];
768 $pointerValue = $record[$pointerField] > 0 ? $record[$pointerField] : $record['uid'];
769 }
770 $recordLocalizations = BackendUtility::getRecordsByField($l10nTable, $pointerField, $pointerValue, '', '', '', '1');
771 if (is_array($recordLocalizations)) {
772 foreach ($recordLocalizations as $localization) {
773 $recordLocalizationAccess = $recordLocalizationAccess
774 && $this->checkLanguageAccess($localization[$GLOBALS['TCA'][$l10nTable]['ctrl']['languageField']]);
775 if (!$recordLocalizationAccess) {
776 break;
777 }
778 }
779 }
780 }
781 return $recordLocalizationAccess;
782 }
783
784 /**
785 * Checking if a user has editing access to a record from a $GLOBALS['TCA'] table.
786 * The checks does not take page permissions and other "environmental" things into account.
787 * It only deal with record internals; If any values in the record fields disallows it.
788 * For instance languages settings, authMode selector boxes are evaluated (and maybe more in the future).
789 * It will check for workspace dependent access.
790 * The function takes an ID (integer) or row (array) as second argument.
791 *
792 * @param string $table Table name
793 * @param mixed $idOrRow If integer, then this is the ID of the record. If Array this just represents fields in the record.
794 * @param boolean $newRecord Set, if testing a new (non-existing) record array. Will disable certain checks that doesn't make much sense in that context.
795 * @param boolean $deletedRecord Set, if testing a deleted record array.
796 * @param boolean $checkFullLanguageAccess Set, whenever access to all translations of the record is required
797 * @return boolean TRUE if OK, otherwise FALSE
798 * @todo Define visibility
799 */
800 public function recordEditAccessInternals($table, $idOrRow, $newRecord = FALSE, $deletedRecord = FALSE, $checkFullLanguageAccess = FALSE) {
801 if (!isset($GLOBALS['TCA'][$table])) {
802 return FALSE;
803 }
804 // Always return TRUE for Admin users.
805 if ($this->isAdmin()) {
806 return TRUE;
807 }
808 // Fetching the record if the $idOrRow variable was not an array on input:
809 if (!is_array($idOrRow)) {
810 if ($deletedRecord) {
811 $idOrRow = BackendUtility::getRecord($table, $idOrRow, '*', '', FALSE);
812 } else {
813 $idOrRow = BackendUtility::getRecord($table, $idOrRow);
814 }
815 if (!is_array($idOrRow)) {
816 $this->errorMsg = 'ERROR: Record could not be fetched.';
817 return FALSE;
818 }
819 }
820 // Checking languages:
821 if ($GLOBALS['TCA'][$table]['ctrl']['languageField']) {
822 // Language field must be found in input row - otherwise it does not make sense.
823 if (isset($idOrRow[$GLOBALS['TCA'][$table]['ctrl']['languageField']])) {
824 if (!$this->checkLanguageAccess($idOrRow[$GLOBALS['TCA'][$table]['ctrl']['languageField']])) {
825 $this->errorMsg = 'ERROR: Language was not allowed.';
826 return FALSE;
827 } elseif (
828 $checkFullLanguageAccess && $idOrRow[$GLOBALS['TCA'][$table]['ctrl']['languageField']] == 0
829 && !$this->checkFullLanguagesAccess($table, $idOrRow)
830 ) {
831 $this->errorMsg = 'ERROR: Related/affected language was not allowed.';
832 return FALSE;
833 }
834 } else {
835 $this->errorMsg = 'ERROR: The "languageField" field named "'
836 . $GLOBALS['TCA'][$table]['ctrl']['languageField'] . '" was not found in testing record!';
837 return FALSE;
838 }
839 } elseif (
840 isset($GLOBALS['TCA'][$table]['ctrl']['transForeignTable']) && $checkFullLanguageAccess &&
841 !$this->checkFullLanguagesAccess($table, $idOrRow)
842 ) {
843 return FALSE;
844 }
845 // Checking authMode fields:
846 if (is_array($GLOBALS['TCA'][$table]['columns'])) {
847 foreach ($GLOBALS['TCA'][$table]['columns'] as $fieldName => $fieldValue) {
848 if (isset($idOrRow[$fieldName])) {
849 if (
850 $fieldValue['config']['type'] === 'select' && $fieldValue['config']['authMode']
851 && $fieldValue['config']['authMode_enforce'] === 'strict'
852 ) {
853 if (!$this->checkAuthMode($table, $fieldName, $idOrRow[$fieldName], $fieldValue['config']['authMode'])) {
854 $this->errorMsg = 'ERROR: authMode "' . $fieldValue['config']['authMode']
855 . '" failed for field "' . $fieldName . '" with value "'
856 . $idOrRow[$fieldName] . '" evaluated';
857 return FALSE;
858 }
859 }
860 }
861 }
862 }
863 // Checking "editlock" feature (doesn't apply to new records)
864 if (!$newRecord && $GLOBALS['TCA'][$table]['ctrl']['editlock']) {
865 if (isset($idOrRow[$GLOBALS['TCA'][$table]['ctrl']['editlock']])) {
866 if ($idOrRow[$GLOBALS['TCA'][$table]['ctrl']['editlock']]) {
867 $this->errorMsg = 'ERROR: Record was locked for editing. Only admin users can change this state.';
868 return FALSE;
869 }
870 } else {
871 $this->errorMsg = 'ERROR: The "editLock" field named "' . $GLOBALS['TCA'][$table]['ctrl']['editlock']
872 . '" was not found in testing record!';
873 return FALSE;
874 }
875 }
876 // Checking record permissions
877 // THIS is where we can include a check for "perms_" fields for other records than pages...
878 // Process any hooks
879 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['recordEditAccessInternals'])) {
880 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['recordEditAccessInternals'] as $funcRef) {
881 $params = array(
882 'table' => $table,
883 'idOrRow' => $idOrRow,
884 'newRecord' => $newRecord
885 );
886 if (!GeneralUtility::callUserFunction($funcRef, $params, $this)) {
887 return FALSE;
888 }
889 }
890 }
891 // Finally, return TRUE if all is well.
892 return TRUE;
893 }
894
895 /**
896 * Checks a type of permission against the compiled permission integer,
897 * $compiledPermissions, and in relation to table, $tableName
898 *
899 * @param integer $compiledPermissions Could typically be the "compiled permissions" integer returned by ->calcPerms
900 * @param string $tableName Is the tablename to check: If "pages" table then edit,new,delete and editcontent permissions can be checked. Other tables will be checked for "editcontent" only (and $type will be ignored)
901 * @param string $actionType For $tableName='pages' this can be 'edit' (2), 'new' (8 or 16), 'delete' (4), 'editcontent' (16). For all other tables this is ignored. (16 is used)
902 * @return boolean
903 * @access public (used by typo3/alt_clickmenu.php)
904 */
905 public function isPSet($compiledPermissions, $tableName, $actionType = '') {
906 if ($this->isAdmin()) {
907 $result = TRUE;
908 } elseif ($tableName == 'pages') {
909 switch ($actionType) {
910 case 'edit':
911 $result = ($compiledPermissions & 2) !== 0;
912 break;
913 case 'new':
914 // Create new page OR page content
915 $result = ($compiledPermissions & 8 + 16) !== 0;
916 break;
917 case 'delete':
918 $result = ($compiledPermissions & 4) !== 0;
919 break;
920 case 'editcontent':
921 $result = ($compiledPermissions & 16) !== 0;
922 break;
923 default:
924 $result = FALSE;
925 }
926 } else {
927 $result = ($compiledPermissions & 16) !== 0;
928 }
929 return $result;
930 }
931
932 /**
933 * Returns TRUE if the BE_USER is allowed to *create* shortcuts in the backend modules
934 *
935 * @return boolean
936 * @todo Define visibility
937 */
938 public function mayMakeShortcut() {
939 return $this->getTSConfigVal('options.enableBookmarks')
940 && !$this->getTSConfigVal('options.mayNotCreateEditBookmarks');
941 }
942
943 /**
944 * Checking if editing of an existing record is allowed in current workspace if that is offline.
945 * Rules for editing in offline mode:
946 * - record supports versioning and is an offline version from workspace and has the corrent stage
947 * - or record (any) is in a branch where there is a page which is a version from the workspace
948 * and where the stage is not preventing records
949 *
950 * @param string $table Table of record
951 * @param array $recData Integer (record uid) or array where fields are at least: pid, t3ver_wsid, t3ver_stage (if versioningWS is set)
952 * @return string String error code, telling the failure state. FALSE=All ok
953 * @todo Define visibility
954 */
955 public function workspaceCannotEditRecord($table, $recData) {
956 // Only test offline spaces:
957 if ($this->workspace !== 0) {
958 if (!is_array($recData)) {
959 $recData = BackendUtility::getRecord(
960 $table,
961 $recData,
962 'pid' . ($GLOBALS['TCA'][$table]['ctrl']['versioningWS'] ? ',t3ver_wsid,t3ver_stage' : '')
963 );
964 }
965 if (is_array($recData)) {
966 // We are testing a "version" (identified by a pid of -1): it can be edited provided
967 // that workspace matches and versioning is enabled for the table.
968 if ((int)$recData['pid'] === -1) {
969 // No versioning, basic error, inconsistency even! Such records should not have a pid of -1!
970 if (!$GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
971 return 'Versioning disabled for table';
972 } elseif ((int)$recData['t3ver_wsid'] !== $this->workspace) {
973 // So does workspace match?
974 return 'Workspace ID of record didn\'t match current workspace';
975 } else {
976 // So is the user allowed to "use" the edit stage within the workspace?
977 return $this->workspaceCheckStageForCurrent(0)
978 ? FALSE
979 : 'User\'s access level did not allow for editing';
980 }
981 } else {
982 // We are testing a "live" record:
983 // For "Live" records, check that PID for table allows editing
984 if ($res = $this->workspaceAllowLiveRecordsInPID($recData['pid'], $table)) {
985 // Live records are OK in this branch, but what about the stage of branch point, if any:
986 // OK
987 return $res > 0
988 ? FALSE
989 : 'Stage for versioning root point and users access level did not allow for editing';
990 } else {
991 // If not offline and not in versionized branch, output error:
992 return 'Online record was not in versionized branch!';
993 }
994 }
995 } else {
996 return 'No record';
997 }
998 } else {
999 // OK because workspace is 0
1000 return FALSE;
1001 }
1002 }
1003
1004 /**
1005 * Evaluates if a user is allowed to edit the offline version
1006 *
1007 * @param string $table Table of record
1008 * @param array $recData Integer (record uid) or array where fields are at least: pid, t3ver_wsid, t3ver_stage (if versioningWS is set)
1009 * @return string String error code, telling the failure state. FALSE=All ok
1010 * @see workspaceCannotEditRecord()
1011 * @todo Define visibility
1012 */
1013 public function workspaceCannotEditOfflineVersion($table, $recData) {
1014 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
1015 if (!is_array($recData)) {
1016 $recData = BackendUtility::getRecord($table, $recData, 'uid,pid,t3ver_wsid,t3ver_stage');
1017 }
1018 if (is_array($recData)) {
1019 if ((int)$recData['pid'] === -1) {
1020 return $this->workspaceCannotEditRecord($table, $recData);
1021 } else {
1022 return 'Not an offline version';
1023 }
1024 } else {
1025 return 'No record';
1026 }
1027 } else {
1028 return 'Table does not support versioning.';
1029 }
1030 }
1031
1032 /**
1033 * Check if "live" records from $table may be created or edited in this PID.
1034 * If the answer is FALSE it means the only valid way to create or edit records in the PID is by versioning
1035 * If the answer is 1 or 2 it means it is OK to create a record, if -1 it means that it is OK in terms
1036 * of versioning because the element was within a versionized branch
1037 * but NOT ok in terms of the state the root point had!
1038 *
1039 * @param integer $pid PID value to check for. OBSOLETE!
1040 * @param string $table Table name
1041 * @return mixed Returns FALSE if a live record cannot be created and must be versionized in order to do so. 2 means a) Workspace is "Live" or workspace allows "live edit" of records from non-versionized tables (and the $table is not versionizable). 1 and -1 means the pid is inside a versionized branch where -1 means that the branch-point did NOT allow a new record according to its state.
1042 * @todo Define visibility
1043 */
1044 public function workspaceAllowLiveRecordsInPID($pid, $table) {
1045 // Always for Live workspace AND if live-edit is enabled
1046 // and tables are completely without versioning it is ok as well.
1047 if (
1048 $this->workspace === 0
1049 || $this->workspaceRec['live_edit'] && !$GLOBALS['TCA'][$table]['ctrl']['versioningWS']
1050 || $GLOBALS['TCA'][$table]['ctrl']['versioningWS_alwaysAllowLiveEdit']
1051 ) {
1052 // OK to create for this table.
1053 return 2;
1054 } else {
1055 // If the answer is FALSE it means the only valid way to create or edit records in the PID is by versioning
1056 return FALSE;
1057 }
1058 }
1059
1060 /**
1061 * Evaluates if a record from $table can be created in $pid
1062 *
1063 * @param integer $pid Page id. This value must be the _ORIG_uid if available: So when you have pages versionized as "page" or "element" you must supply the id of the page version in the workspace!
1064 * @param string $table Table name
1065 * @return boolean TRUE if OK.
1066 * @todo Define visibility
1067 */
1068 public function workspaceCreateNewRecord($pid, $table) {
1069 if ($res = $this->workspaceAllowLiveRecordsInPID($pid, $table)) {
1070 // If LIVE records cannot be created in the current PID due to workspace restrictions, prepare creation of placeholder-record
1071 if ($res < 0) {
1072 // Stage for versioning root point and users access level did not allow for editing
1073 return FALSE;
1074 }
1075 } elseif (!$GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
1076 // So, if no live records were allowed, we have to create a new version of this record:
1077 return FALSE;
1078 }
1079 return TRUE;
1080 }
1081
1082 /**
1083 * Evaluates if auto creation of a version of a record is allowed.
1084 *
1085 * @param string $table Table of the record
1086 * @param integer $id UID of record
1087 * @param integer $recpid PID of record
1088 * @return boolean TRUE if ok.
1089 * @todo Define visibility
1090 */
1091 public function workspaceAllowAutoCreation($table, $id, $recpid) {
1092 // Auto-creation of version: In offline workspace, test if versioning is
1093 // enabled and look for workspace version of input record.
1094 // If there is no versionized record found we will create one and save to that.
1095 if (
1096 $this->workspace !== 0 && !$this->workspaceRec['disable_autocreate']
1097 && $GLOBALS['TCA'][$table]['ctrl']['versioningWS'] && $recpid >= 0
1098 && !BackendUtility::getWorkspaceVersionOfRecord($this->workspace, $table, $id, 'uid')
1099 ) {
1100 // There must be no existing version of this record in workspace.
1101 return TRUE;
1102 } elseif ($this->workspaceRec['disable_autocreate']) {
1103 GeneralUtility::deprecationLog('Usage of disable_autocreate feature is deprecated since 4.5.');
1104 }
1105 return FALSE;
1106 }
1107
1108 /**
1109 * Checks if an element stage allows access for the user in the current workspace
1110 * In live workspace (= 0) access is always granted for any stage.
1111 * Admins are always allowed.
1112 * An option for custom workspaces allows members to also edit when the stage is "Review"
1113 *
1114 * @param integer $stage Stage id from an element: -1,0 = editing, 1 = reviewer, >1 = owner
1115 * @return boolean TRUE if user is allowed access
1116 * @todo Define visibility
1117 */
1118 public function workspaceCheckStageForCurrent($stage) {
1119 // Always allow for admins
1120 if ($this->isAdmin()) {
1121 return TRUE;
1122 }
1123 if ($this->workspace !== 0 && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
1124 $stage = (int)$stage;
1125 $stat = $this->checkWorkspaceCurrent();
1126 // Check if custom staging is activated
1127 $workspaceRec = BackendUtility::getRecord('sys_workspace', $stat['uid']);
1128 if ($workspaceRec['custom_stages'] > 0 && $stage !== 0 && $stage !== -10) {
1129 // Get custom stage record
1130 $workspaceStageRec = BackendUtility::getRecord('sys_workspace_stage', $stage);
1131 // Check if the user is responsible for the current stage
1132 if (
1133 $stat['_ACCESS'] === 'owner'
1134 || $stat['_ACCESS'] === 'member'
1135 && GeneralUtility::inList($workspaceStageRec['responsible_persons'], 'be_users_' . $this->user['uid'])
1136 ) {
1137 return TRUE;
1138 }
1139 // Check if the user is in a group which is responsible for the current stage
1140 foreach ($this->userGroupsUID as $groupUid) {
1141 if (
1142 $stat['_ACCESS'] === 'owner'
1143 || $stat['_ACCESS'] === 'member'
1144 && GeneralUtility::inList($workspaceStageRec['responsible_persons'], 'be_groups_' . $groupUid)
1145 ) {
1146 return TRUE;
1147 }
1148 }
1149 } elseif ($stage == -10 || $stage == -20) {
1150 if ($stat['_ACCESS'] === 'owner') {
1151 return TRUE;
1152 } else {
1153 return FALSE;
1154 }
1155 } else {
1156 $memberStageLimit = $this->workspaceRec['review_stage_edit'] ? 1 : 0;
1157 if (
1158 $stat['_ACCESS'] === 'owner'
1159 || $stat['_ACCESS'] === 'reviewer' && $stage <= 1
1160 || $stat['_ACCESS'] === 'member' && $stage <= $memberStageLimit
1161 ) {
1162 return TRUE;
1163 }
1164 }
1165 } else {
1166 // Always OK for live workspace.
1167 return TRUE;
1168 }
1169 return FALSE;
1170 }
1171
1172 /**
1173 * Returns TRUE if the user has access to publish content from the workspace ID given.
1174 * Admin-users are always granted access to do this
1175 * If the workspace ID is 0 (live) all users have access also
1176 * For custom workspaces it depends on whether the user is owner OR like with
1177 * draft workspace if the user has access to Live workspace.
1178 *
1179 * @param integer $wsid Workspace UID; 0,1+
1180 * @return boolean Returns TRUE if the user has access to publish content from the workspace ID given.
1181 * @todo Define visibility
1182 */
1183 public function workspacePublishAccess($wsid) {
1184 if ($this->isAdmin()) {
1185 return TRUE;
1186 }
1187 // If no access to workspace, of course you cannot publish!
1188 $retVal = FALSE;
1189 $wsAccess = $this->checkWorkspace($wsid);
1190 if ($wsAccess) {
1191 switch ($wsAccess['uid']) {
1192 case 0:
1193 // Live workspace
1194 // If access to Live workspace, no problem.
1195 $retVal = TRUE;
1196 break;
1197 default:
1198 // Custom workspace
1199 $retVal = $wsAccess['_ACCESS'] === 'owner' || $this->checkWorkspace(0) && !($wsAccess['publish_access'] & 2);
1200 // Either be an adminuser OR have access to online
1201 // workspace which is OK as well as long as publishing
1202 // access is not limited by workspace option.
1203 }
1204 }
1205 return $retVal;
1206 }
1207
1208 /**
1209 * Workspace swap-mode access?
1210 *
1211 * @return boolean Returns TRUE if records can be swapped in the current workspace, otherwise FALSE
1212 * @todo Define visibility
1213 */
1214 public function workspaceSwapAccess() {
1215 if ($this->workspace > 0 && (int)$this->workspaceRec['swap_modes'] === 2) {
1216 return FALSE;
1217 } else {
1218 return TRUE;
1219 }
1220 }
1221
1222 /**
1223 * Returns the value/properties of a TS-object as given by $objectString, eg. 'options.dontMountAdminMounts'
1224 * Nice (general!) function for returning a part of a TypoScript array!
1225 *
1226 * @param string $objectString Pointer to an "object" in the TypoScript array, fx. 'options.dontMountAdminMounts'
1227 * @param array|string $config Optional TSconfig array: If array, then this is used and not $this->userTS. If not array, $this->userTS is used.
1228 * @return array An array with two keys, "value" and "properties" where "value" is a string with the value of the object string and "properties" is an array with the properties of the object string.
1229 * @todo Define visibility
1230 */
1231 public function getTSConfig($objectString, $config = '') {
1232 if (!is_array($config)) {
1233 // Getting Root-ts if not sent
1234 $config = $this->userTS;
1235 }
1236 $TSConf = array('value' => NULL, 'properties' => NULL);
1237 $parts = GeneralUtility::trimExplode('.', $objectString, TRUE, 2);
1238 $key = $parts[0];
1239 if (strlen($key) > 0) {
1240 if (count($parts) > 1 && strlen($parts[1]) > 0) {
1241 // Go on, get the next level
1242 if (is_array($config[$key . '.'])) {
1243 $TSConf = $this->getTSConfig($parts[1], $config[$key . '.']);
1244 }
1245 } else {
1246 $TSConf['value'] = $config[$key];
1247 $TSConf['properties'] = $config[$key . '.'];
1248 }
1249 }
1250 return $TSConf;
1251 }
1252
1253 /**
1254 * Returns the "value" of the $objectString from the BE_USERS "User TSconfig" array
1255 *
1256 * @param string $objectString Object string, eg. "somestring.someproperty.somesubproperty
1257 * @return string The value for that object string (object path)
1258 * @see getTSConfig()
1259 * @todo Define visibility
1260 */
1261 public function getTSConfigVal($objectString) {
1262 $TSConf = $this->getTSConfig($objectString);
1263 return $TSConf['value'];
1264 }
1265
1266 /**
1267 * Returns the "properties" of the $objectString from the BE_USERS "User TSconfig" array
1268 *
1269 * @param string $objectString Object string, eg. "somestring.someproperty.somesubproperty
1270 * @return array The properties for that object string (object path) - if any
1271 * @see getTSConfig()
1272 * @todo Define visibility
1273 */
1274 public function getTSConfigProp($objectString) {
1275 $TSConf = $this->getTSConfig($objectString);
1276 return $TSConf['properties'];
1277 }
1278
1279 /**
1280 * Returns an array with the webmounts.
1281 * If no webmounts, and empty array is returned.
1282 * NOTICE: Deleted pages WILL NOT be filtered out! So if a mounted page has been deleted
1283 * it is STILL coming out as a webmount. This is not checked due to performance.
1284 *
1285 * @return array
1286 * @todo Define visibility
1287 */
1288 public function returnWebmounts() {
1289 return (string) $this->groupData['webmounts'] != '' ? explode(',', $this->groupData['webmounts']) : array();
1290 }
1291
1292 /**
1293 * Returns TRUE or FALSE, depending if an alert popup (a javascript confirmation) should be shown
1294 * call like $GLOBALS['BE_USER']->jsConfirmation($BITMASK)
1295 *
1296 * 1 - typeChange
1297 * 2 - copy/move/paste
1298 * 4 - delete
1299 * 8 - frontend editing
1300 * 128 - other (not used yet)
1301 *
1302 * @param integer $bitmask Bitmask
1303 * @return boolean TRUE if the confirmation should be shown
1304 * @todo Define visibility
1305 */
1306 public function jsConfirmation($bitmask) {
1307 $alertPopup = $this->getTSConfig('options.alertPopups');
1308 if (empty($alertPopup['value'])) {
1309 // Default: show all warnings
1310 $alertPopup = 255;
1311 } else {
1312 $alertPopup = (int)$alertPopup['value'];
1313 }
1314 // Show confirmation
1315 return ($alertPopup & $bitmask) == $bitmask;
1316 }
1317
1318 /**
1319 * Initializes a lot of stuff like the access-lists, database-mountpoints and filemountpoints
1320 * This method is called by ->backendCheckLogin() (from extending BackendUserAuthentication)
1321 * if the backend user login has verified OK.
1322 * Generally this is required initialization of a backend user.
1323 *
1324 * @return void
1325 * @access private
1326 * @see \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser
1327 * @todo Define visibility
1328 */
1329 public function fetchGroupData() {
1330 if ($this->user['uid']) {
1331 // Get lists for the be_user record and set them as default/primary values.
1332 // Enabled Backend Modules
1333 $this->dataLists['modList'] = $this->user['userMods'];
1334 // Add Allowed Languages
1335 $this->dataLists['allowed_languages'] = $this->user['allowed_languages'];
1336 // Set user value for workspace permissions.
1337 $this->dataLists['workspace_perms'] = $this->user['workspace_perms'];
1338 // Database mountpoints
1339 $this->dataLists['webmount_list'] = $this->user['db_mountpoints'];
1340 // File mountpoints
1341 $this->dataLists['filemount_list'] = $this->user['file_mountpoints'];
1342 // Fileoperation permissions
1343 $this->dataLists['file_permissions'] = $this->user['file_permissions'];
1344 // Setting default User TSconfig:
1345 $this->TSdataArray[] = $this->addTScomment('From $GLOBALS["TYPO3_CONF_VARS"]["BE"]["defaultUserTSconfig"]:')
1346 . $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultUserTSconfig'];
1347 // Default TSconfig for admin-users
1348 if ($this->isAdmin()) {
1349 $this->TSdataArray[] = $this->addTScomment('"admin" user presets:') . '
1350 admPanel.enable.all = 1
1351 ';
1352 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('sys_note')) {
1353 $this->TSdataArray[] = '
1354 // Setting defaults for sys_note author / email...
1355 TCAdefaults.sys_note.author = ' . $this->user['realName'] . '
1356 TCAdefaults.sys_note.email = ' . $this->user['email'] . '
1357 ';
1358 }
1359 }
1360 // BE_GROUPS:
1361 // Get the groups...
1362 // 240203: Since the group-field never contains any references to groups with a prepended table name
1363 // we think it's safe to just intExplode and re-implode - which should be much faster than the other function call.
1364 $grList = $this->db->cleanIntList($this->user[$this->usergroup_column]);
1365 if ($grList) {
1366 // Fetch groups will add a lot of information to the internal arrays: modules, accesslists, TSconfig etc.
1367 // Refer to fetchGroups() function.
1368 $this->fetchGroups($grList);
1369 }
1370 // Add the TSconfig for this specific user:
1371 $this->TSdataArray[] = $this->addTScomment('USER TSconfig field') . $this->user['TSconfig'];
1372 // Check include lines.
1373 $this->TSdataArray = \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::checkIncludeLines_array($this->TSdataArray);
1374 // Imploding with "[global]" will make sure that non-ended confinements with braces are ignored.
1375 $this->userTS_text = implode(LF . '[GLOBAL]' . LF, $this->TSdataArray);
1376 if (!$this->userTS_dontGetCached) {
1377 // Perform TS-Config parsing with condition matching
1378 $parseObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Configuration\\TsConfigParser');
1379 $res = $parseObj->parseTSconfig($this->userTS_text, 'userTS');
1380 if ($res) {
1381 $this->userTS = $res['TSconfig'];
1382 $this->userTSUpdated = (bool)$res['cached'];
1383 }
1384 } else {
1385 // Parsing the user TSconfig (or getting from cache)
1386 $hash = md5('userTS:' . $this->userTS_text);
1387 $cachedContent = BackendUtility::getHash($hash);
1388 if (is_array($cachedContent) && !$this->userTS_dontGetCached) {
1389 $this->userTS = $cachedContent;
1390 } else {
1391 $parseObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
1392 $parseObj->parse($this->userTS_text);
1393 $this->userTS = $parseObj->setup;
1394 BackendUtility::storeHash($hash, $this->userTS, 'BE_USER_TSconfig');
1395 // Update UC:
1396 $this->userTSUpdated = TRUE;
1397 }
1398 }
1399 // Processing webmounts
1400 // Admin's always have the root mounted
1401 if ($this->isAdmin() && !$this->getTSConfigVal('options.dontMountAdminMounts')) {
1402 $this->dataLists['webmount_list'] = '0,' . $this->dataLists['webmount_list'];
1403 }
1404 // The lists are cleaned for duplicates
1405 $this->groupData['webmounts'] = GeneralUtility::uniqueList($this->dataLists['webmount_list']);
1406 $this->groupData['pagetypes_select'] = GeneralUtility::uniqueList($this->dataLists['pagetypes_select']);
1407 $this->groupData['tables_select'] = GeneralUtility::uniqueList($this->dataLists['tables_modify'] . ',' . $this->dataLists['tables_select']);
1408 $this->groupData['tables_modify'] = GeneralUtility::uniqueList($this->dataLists['tables_modify']);
1409 $this->groupData['non_exclude_fields'] = GeneralUtility::uniqueList($this->dataLists['non_exclude_fields']);
1410 $this->groupData['explicit_allowdeny'] = GeneralUtility::uniqueList($this->dataLists['explicit_allowdeny']);
1411 $this->groupData['allowed_languages'] = GeneralUtility::uniqueList($this->dataLists['allowed_languages']);
1412 $this->groupData['custom_options'] = GeneralUtility::uniqueList($this->dataLists['custom_options']);
1413 $this->groupData['modules'] = GeneralUtility::uniqueList($this->dataLists['modList']);
1414 $this->groupData['file_permissions'] = GeneralUtility::uniqueList($this->dataLists['file_permissions']);
1415 $this->groupData['workspace_perms'] = $this->dataLists['workspace_perms'];
1416 // Populating the $this->userGroupsUID -array with the groups in the order in which they were LAST included.!!
1417 $this->userGroupsUID = array_reverse(array_unique(array_reverse($this->includeGroupArray)));
1418 // Finally this is the list of group_uid's in the order they are parsed (including subgroups!)
1419 // and without duplicates (duplicates are presented with their last entrance in the list,
1420 // which thus reflects the order of the TypoScript in TSconfig)
1421 $this->groupList = implode(',', $this->userGroupsUID);
1422 $this->setCachedList($this->groupList);
1423 // Checking read access to webmounts:
1424 if (trim($this->groupData['webmounts']) !== '') {
1425 $webmounts = explode(',', $this->groupData['webmounts']);
1426 // Explode mounts
1427 // Selecting all webmounts with permission clause for reading
1428 $where = 'deleted=0 AND uid IN (' . $this->groupData['webmounts'] . ') AND ' . $this->getPagePermsClause(1);
1429 $MProws = $this->db->exec_SELECTgetRows('uid', 'pages', $where, '', '', '', 'uid');
1430 foreach ($webmounts as $idx => $mountPointUid) {
1431 // If the mount ID is NOT found among selected pages, unset it:
1432 if ($mountPointUid > 0 && !isset($MProws[$mountPointUid])) {
1433 unset($webmounts[$idx]);
1434 }
1435 }
1436 // Implode mounts in the end.
1437 $this->groupData['webmounts'] = implode(',', $webmounts);
1438 }
1439 // Setting up workspace situation (after webmounts are processed!):
1440 $this->workspaceInit();
1441 }
1442 }
1443
1444 /**
1445 * Fetches the group records, subgroups and fills internal arrays.
1446 * Function is called recursively to fetch subgroups
1447 *
1448 * @param string $grList Commalist of be_groups uid numbers
1449 * @param string $idList List of already processed be_groups-uids so the function will not fall into a eternal recursion.
1450 * @return void
1451 * @access private
1452 * @todo Define visibility
1453 */
1454 public function fetchGroups($grList, $idList = '') {
1455 // Fetching records of the groups in $grList (which are not blocked by lockedToDomain either):
1456 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . GeneralUtility::getIndpEnv('HTTP_HOST') . '\')';
1457 $whereSQL = 'deleted=0 AND hidden=0 AND pid=0 AND uid IN (' . $grList . ')' . $lockToDomain_SQL;
1458 // Hook for manipulation of the WHERE sql sentence which controls which BE-groups are included
1459 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['fetchGroupQuery'])) {
1460 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['fetchGroupQuery'] as $classRef) {
1461 $hookObj = GeneralUtility::getUserObj($classRef);
1462 if (method_exists($hookObj, 'fetchGroupQuery_processQuery')) {
1463 $whereSQL = $hookObj->fetchGroupQuery_processQuery($this, $grList, $idList, $whereSQL);
1464 }
1465 }
1466 }
1467 $res = $this->db->exec_SELECTquery('*', $this->usergroup_table, $whereSQL);
1468 // The userGroups array is filled
1469 while ($row = $this->db->sql_fetch_assoc($res)) {
1470 $this->userGroups[$row['uid']] = $row;
1471 }
1472 $this->db->sql_free_result($res);
1473 // Traversing records in the correct order
1474 $include_staticArr = GeneralUtility::intExplode(',', $grList);
1475 // Traversing list
1476 foreach ($include_staticArr as $uid) {
1477 // Get row:
1478 $row = $this->userGroups[$uid];
1479 // Must be an array and $uid should not be in the idList, because then it is somewhere previously in the grouplist
1480 if (is_array($row) && !GeneralUtility::inList($idList, $uid)) {
1481 // Include sub groups
1482 if (trim($row['subgroup'])) {
1483 // Make integer list
1484 $theList = implode(',', GeneralUtility::intExplode(',', $row['subgroup']));
1485 // Call recursively, pass along list of already processed groups so they are not recursed again.
1486 $this->fetchGroups($theList, $idList . ',' . $uid);
1487 }
1488 // Add the group uid, current list, TSconfig to the internal arrays.
1489 $this->includeGroupArray[] = $uid;
1490 $this->includeHierarchy[] = $idList;
1491 $this->TSdataArray[] = $this->addTScomment('Group "' . $row['title'] . '" [' . $row['uid'] . '] TSconfig field:') . $row['TSconfig'];
1492 // Mount group database-mounts
1493 if (($this->user['options'] & 1) == 1) {
1494 $this->dataLists['webmount_list'] .= ',' . $row['db_mountpoints'];
1495 }
1496 // Mount group file-mounts
1497 if (($this->user['options'] & 2) == 2) {
1498 $this->dataLists['filemount_list'] .= ',' . $row['file_mountpoints'];
1499 }
1500 // The lists are made: groupMods, tables_select, tables_modify, pagetypes_select, non_exclude_fields, explicit_allowdeny, allowed_languages, custom_options
1501 $this->dataLists['modList'] .= ',' . $row['groupMods'];
1502 $this->dataLists['tables_select'] .= ',' . $row['tables_select'];
1503 $this->dataLists['tables_modify'] .= ',' . $row['tables_modify'];
1504 $this->dataLists['pagetypes_select'] .= ',' . $row['pagetypes_select'];
1505 $this->dataLists['non_exclude_fields'] .= ',' . $row['non_exclude_fields'];
1506 $this->dataLists['explicit_allowdeny'] .= ',' . $row['explicit_allowdeny'];
1507 $this->dataLists['allowed_languages'] .= ',' . $row['allowed_languages'];
1508 $this->dataLists['custom_options'] .= ',' . $row['custom_options'];
1509 $this->dataLists['file_permissions'] .= ',' . $row['file_permissions'];
1510 // Setting workspace permissions:
1511 $this->dataLists['workspace_perms'] |= $row['workspace_perms'];
1512 // If this function is processing the users OWN group-list (not subgroups) AND if the ->firstMainGroup is not set, then the ->firstMainGroup will be set.
1513 if ((string)$idList !== '' && !$this->firstMainGroup) {
1514 $this->firstMainGroup = $uid;
1515 }
1516 }
1517 }
1518 // HOOK: fetchGroups_postProcessing
1519 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['fetchGroups_postProcessing'])) {
1520 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['fetchGroups_postProcessing'] as $_funcRef) {
1521 $_params = array();
1522 GeneralUtility::callUserFunction($_funcRef, $_params, $this);
1523 }
1524 }
1525 }
1526
1527 /**
1528 * Updates the field be_users.usergroup_cached_list if the groupList of the user
1529 * has changed/is different from the current list.
1530 * The field "usergroup_cached_list" contains the list of groups which the user is a member of.
1531 * After authentication (where these functions are called...) one can depend on this list being
1532 * a representation of the exact groups/subgroups which the BE_USER has membership with.
1533 *
1534 * @param string $cList The newly compiled group-list which must be compared with the current list in the user record and possibly stored if a difference is detected.
1535 * @return void
1536 * @access private
1537 * @todo Define visibility
1538 */
1539 public function setCachedList($cList) {
1540 if ((string) $cList != (string) $this->user['usergroup_cached_list']) {
1541 $this->db->exec_UPDATEquery('be_users', 'uid=' . (int)$this->user['uid'], array('usergroup_cached_list' => $cList));
1542 }
1543 }
1544
1545 /**
1546 * Sets up all file storages for a user.
1547 * Needs to be called AFTER the groups have been loaded.
1548 *
1549 * @return void
1550 */
1551 protected function initializeFileStorages() {
1552 $this->fileStorages = array();
1553 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
1554 $storageRepository = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
1555 // Admin users have all file storages visible, without any filters
1556 if ($this->isAdmin()) {
1557 $storageObjects = $storageRepository->findAll();
1558 foreach ($storageObjects as $storageObject) {
1559 $this->fileStorages[$storageObject->getUid()] = $storageObject;
1560 }
1561 } else {
1562 // Regular users only have storages that are defined in their filemounts
1563 // Permissions and file mounts for the storage are added in StoragePermissionAspect
1564 foreach ($this->getFileMountRecords() as $row) {
1565 if (!array_key_exists((int)$row['base'], $this->fileStorages)) {
1566 $storageObject = $storageRepository->findByUid($row['base']);
1567 $this->fileStorages[$storageObject->getUid()] = $storageObject;
1568 }
1569 }
1570 }
1571
1572 // This has to be called always in order to set certain filters
1573 $this->evaluateUserSpecificFileFilterSettings();
1574 }
1575
1576 /**
1577 * Returns an array of category mount points. The category permissions from BE Groups
1578 * are also taken into consideration and are merged into User permissions.
1579 *
1580 * @return array
1581 */
1582 public function getCategoryMountPoints() {
1583 $categoryMountPoints = '';
1584
1585 // Category mounts of the groups
1586 if (is_array($this->userGroups)) {
1587 foreach ($this->userGroups as $group) {
1588 if ($group['category_perms']) {
1589 $categoryMountPoints .= ',' . $group['category_perms'];
1590 }
1591 }
1592 }
1593
1594 // Category mounts of the user record
1595 if ($this->user['category_perms']) {
1596 $categoryMountPoints .= ',' . $this->user['category_perms'];
1597 }
1598
1599 // Make the ids unique
1600 $categoryMountPoints = GeneralUtility::trimExplode(',', $categoryMountPoints);
1601 $categoryMountPoints = array_filter($categoryMountPoints); // remove empty value
1602 $categoryMountPoints = array_unique($categoryMountPoints); // remove unique value
1603
1604 return $categoryMountPoints;
1605 }
1606
1607 /**
1608 * Returns an array of file mount records, taking workspaces and user home and group home directories into account
1609 * Needs to be called AFTER the groups have been loaded.
1610 *
1611 * @return array
1612 * @internal
1613 */
1614 public function getFileMountRecords() {
1615 static $fileMountRecords = array();
1616
1617 if (empty($fileMountRecords)) {
1618 // Processing filemounts (both from the user and the groups)
1619 $fileMounts = array_unique(GeneralUtility::intExplode(',', $this->dataLists['filemount_list'], TRUE));
1620
1621 // Limit filemounts if set in workspace record
1622 if ($this->workspace > 0 && !empty($this->workspaceRec['file_mountpoints'])) {
1623 $workspaceFileMounts = GeneralUtility::intExplode(',', $this->workspaceRec['file_mountpoints'], TRUE);
1624 $fileMounts = array_intersect($fileMounts, $workspaceFileMounts);
1625 }
1626
1627 if (!empty($fileMounts)) {
1628 $orderBy = isset($GLOBALS['TCA']['sys_filemounts']['ctrl']['default_sortby'])
1629 ? $this->db->stripOrderBy($GLOBALS['TCA']['sys_filemounts']['ctrl']['default_sortby'])
1630 : 'sorting';
1631 $fileMountRecords = $this->db->exec_SELECTgetRows(
1632 '*',
1633 'sys_filemounts',
1634 'deleted=0 AND hidden=0 AND pid=0 AND uid IN (' . implode(',', $fileMounts) . ')',
1635 '',
1636 $orderBy
1637 );
1638 }
1639
1640 // Personal or Group filemounts are not accessible if file mount list is set in workspace record
1641 if ($this->workspace <= 0 || empty($this->workspaceRec['file_mountpoints'])) {
1642 // If userHomePath is set, we attempt to mount it
1643 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['userHomePath']) {
1644 list($userHomeStorageUid, $userHomeFilter) = explode(':', $GLOBALS['TYPO3_CONF_VARS']['BE']['userHomePath'], 2);
1645 $userHomeStorageUid = (int)$userHomeStorageUid;
1646 $userHomeFilter = '/' . ltrim($userHomeFilter, '/');
1647 if ($userHomeStorageUid > 0) {
1648 // Try and mount with [uid]_[username]
1649 $fileMountRecords[] = array(
1650 'base' => $userHomeStorageUid,
1651 'title' => $this->user['username'],
1652 'path' => $userHomeFilter . $this->user['uid'] . '_' . $this->user['username'] . $GLOBALS['TYPO3_CONF_VARS']['BE']['userUploadDir']
1653 );
1654 // Try and mount with only [uid]
1655 $fileMountRecords[] = array(
1656 'base' => $userHomeStorageUid,
1657 'title' => $this->user['username'],
1658 'path' => $userHomeFilter . $this->user['uid'] . $GLOBALS['TYPO3_CONF_VARS']['BE']['userUploadDir']
1659 );
1660 }
1661 }
1662
1663 // Mount group home-dirs
1664 if ((is_array($this->user) && $this->user['options'] & 2) == 2 && $GLOBALS['TYPO3_CONF_VARS']['BE']['groupHomePath'] != '') {
1665 // If groupHomePath is set, we attempt to mount it
1666 list($groupHomeStorageUid, $groupHomeFilter) = explode(':', $GLOBALS['TYPO3_CONF_VARS']['BE']['groupHomePath'], 2);
1667 $groupHomeStorageUid = (int)$groupHomeStorageUid;
1668 $groupHomeFilter = '/' . ltrim($groupHomeFilter, '/');
1669 if ($groupHomeStorageUid > 0) {
1670 foreach ($this->userGroups as $groupData) {
1671 $fileMountRecords[] = array(
1672 'base' => $groupHomeStorageUid,
1673 'title' => $groupData['title'],
1674 'path' => $groupHomeFilter . $groupData['uid']
1675 );
1676 }
1677 }
1678 }
1679 }
1680 }
1681
1682 return $fileMountRecords;
1683 }
1684
1685 /**
1686 * Returns an array with the filemounts for the user.
1687 * Each filemount is represented with an array of a "name", "path" and "type".
1688 * If no filemounts an empty array is returned.
1689 *
1690 * @api
1691 * @return \TYPO3\CMS\Core\Resource\ResourceStorage[]
1692 */
1693 public function getFileStorages() {
1694 // Initializing file mounts after the groups are fetched
1695 if ($this->fileStorages === NULL) {
1696 $this->initializeFileStorages();
1697 }
1698 return $this->fileStorages;
1699 }
1700
1701 /**
1702 * Adds filters based on what the user has set
1703 * this should be done in this place, and called whenever needed,
1704 * but only when needed
1705 *
1706 * @return void
1707 */
1708 public function evaluateUserSpecificFileFilterSettings() {
1709 // Add the option for also displaying the non-hidden files
1710 if ($this->uc['showHiddenFilesAndFolders']) {
1711 \TYPO3\CMS\Core\Resource\Filter\FileNameFilter::setShowHiddenFilesAndFolders(TRUE);
1712 }
1713 }
1714
1715 /**
1716 * Returns the information about file permissions.
1717 * Previously, this was stored in the DB field fileoper_perms now it is file_permissions.
1718 * Besides it can be handled via userTSconfig
1719 *
1720 * permissions.file.default {
1721 * addFile = 1
1722 * readFile = 1
1723 * writeFile = 1
1724 * copyFile = 1
1725 * moveFile = 1
1726 * renameFile = 1
1727 * unzipFile = 1
1728 * deleteFile = 1
1729 *
1730 * addFolder = 1
1731 * readFolder = 1
1732 * writeFolder = 1
1733 * copyFolder = 1
1734 * moveFolder = 1
1735 * renameFolder = 1
1736 * deleteFolder = 1
1737 * recursivedeleteFolder = 1
1738 * }
1739 *
1740 * # overwrite settings for a specific storageObject
1741 * permissions.file.storage.StorageUid {
1742 * readFile = 1
1743 * recursivedeleteFolder = 0
1744 * }
1745 *
1746 * Please note that these permissions only apply, if the storage has the
1747 * capabilities (browseable, writable), and if the driver allows for writing etc
1748 *
1749 * @api
1750 * @return array
1751 */
1752 public function getFilePermissions() {
1753 if (!isset($this->filePermissions)) {
1754 $filePermissions = array(
1755 // File permissions
1756 'addFile' => FALSE,
1757 'readFile' => FALSE,
1758 'writeFile' => FALSE,
1759 'copyFile' => FALSE,
1760 'moveFile' => FALSE,
1761 'renameFile' => FALSE,
1762 'unzipFile' => FALSE,
1763 'deleteFile' => FALSE,
1764 // Folder permissions
1765 'addFolder' => FALSE,
1766 'readFolder' => FALSE,
1767 'writeFolder' => FALSE,
1768 'copyFolder' => FALSE,
1769 'moveFolder' => FALSE,
1770 'renameFolder' => FALSE,
1771 'deleteFolder' => FALSE,
1772 'recursivedeleteFolder' => FALSE
1773 );
1774 if ($this->isAdmin()) {
1775 $filePermissions = array_map('is_bool', $filePermissions);
1776 } else {
1777 $userGroupRecordPermissions = GeneralUtility::trimExplode(',', $this->groupData['file_permissions'], TRUE);
1778 array_walk(
1779 $userGroupRecordPermissions,
1780 function($permission) use (&$filePermissions) {
1781 $filePermissions[$permission] = TRUE;
1782 }
1783 );
1784
1785 // Finally overlay any userTSconfig
1786 $permissionsTsConfig = $this->getTSConfigProp('permissions.file.default');
1787 if (!empty($permissionsTsConfig)) {
1788 array_walk(
1789 $permissionsTsConfig,
1790 function($value, $permission) use (&$filePermissions) {
1791 $filePermissions[$permission] = (bool) $value;
1792 }
1793 );
1794 }
1795 }
1796 $this->filePermissions = $filePermissions;
1797 }
1798 return $this->filePermissions;
1799 }
1800
1801 /**
1802 * Gets the file permissions for a storage
1803 * by merging any storage-specific permissions for a
1804 * storage with the default settings.
1805 * Admin users will always get the default settings.
1806 *
1807 * @api
1808 * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject
1809 * @return array
1810 */
1811 public function getFilePermissionsForStorage(\TYPO3\CMS\Core\Resource\ResourceStorage $storageObject) {
1812 $finalUserPermissions = $this->getFilePermissions();
1813 if (!$this->isAdmin()) {
1814 $storageFilePermissions = $this->getTSConfigProp('permissions.file.storage.' . $storageObject->getUid());
1815 if (!empty($storageFilePermissions)) {
1816 array_walk(
1817 $storageFilePermissions,
1818 function($value, $permission) use (&$finalUserPermissions) {
1819 $finalUserPermissions[$permission] = (bool) $value;
1820 }
1821 );
1822 }
1823 }
1824 return $finalUserPermissions;
1825 }
1826
1827 /**
1828 * Returns a \TYPO3\CMS\Core\Resource\Folder object that is used for uploading
1829 * files by default.
1830 * This is used for RTE and its magic images, as well as uploads
1831 * in the TCEforms fields, unless otherwise configured (will be added in the future)
1832 *
1833 * the default upload folder for a user is the defaultFolder on the first
1834 * filestorage/filemount that the user can access
1835 * however, you can set the users' upload folder like this:
1836 *
1837 * options.defaultUploadFolder = 3:myfolder/yourfolder/
1838 *
1839 * @return \TYPO3\CMS\Core\Resource\Folder|boolean The default upload folder for this user
1840 */
1841 public function getDefaultUploadFolder() {
1842 $uploadFolder = $this->getTSConfigVal('options.defaultUploadFolder');
1843 if ($uploadFolder) {
1844 $uploadFolder = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($uploadFolder);
1845 } else {
1846 foreach($this->getFileStorages() as $storage) {
1847 try {
1848 $uploadFolder = $storage->getDefaultFolder();
1849 break;
1850 } catch (\TYPO3\CMS\Core\Resource\Exception $folderAccessException) {
1851 // If the folder is not accessible (no permissions / does not exist) try the next one.
1852 }
1853 }
1854 }
1855 if ($uploadFolder instanceof \TYPO3\CMS\Core\Resource\Folder) {
1856 return $uploadFolder;
1857 } else {
1858 return FALSE;
1859 }
1860 }
1861
1862 /**
1863 * Creates a TypoScript comment with the string text inside.
1864 *
1865 * @param string $str The text to wrap in comment prefixes and delimiters.
1866 * @return string TypoScript comment with the string text inside.
1867 * @todo Define visibility
1868 */
1869 public function addTScomment($str) {
1870 $delimiter = '# ***********************************************';
1871 $out = $delimiter . LF;
1872 $lines = GeneralUtility::trimExplode(LF, $str);
1873 foreach ($lines as $v) {
1874 $out .= '# ' . $v . LF;
1875 }
1876 $out .= $delimiter . LF;
1877 return $out;
1878 }
1879
1880 /**
1881 * Initializing workspace.
1882 * Called from within this function, see fetchGroupData()
1883 *
1884 * @return void
1885 * @see fetchGroupData()
1886 * @todo Define visibility
1887 */
1888 public function workspaceInit() {
1889 // Initializing workspace by evaluating and setting the workspace, possibly updating it in the user record!
1890 $this->setWorkspace($this->user['workspace_id']);
1891 // Limiting the DB mountpoints if there any selected in the workspace record
1892 $this->initializeDbMountpointsInWorkspace();
1893 if ($allowed_languages = $this->getTSConfigVal('options.workspaces.allowed_languages.' . $this->workspace)) {
1894 $this->groupData['allowed_languages'] = $allowed_languages;
1895 $this->groupData['allowed_languages'] = GeneralUtility::uniqueList($this->groupData['allowed_languages']);
1896 }
1897 }
1898
1899 /**
1900 * Limiting the DB mountpoints if there any selected in the workspace record
1901 *
1902 * @return void
1903 */
1904 protected function initializeDbMountpointsInWorkspace() {
1905 $dbMountpoints = trim($this->workspaceRec['db_mountpoints']);
1906 if ($this->workspace > 0 && $dbMountpoints != '') {
1907 $filteredDbMountpoints = array();
1908 // Notice: We cannot call $this->getPagePermsClause(1);
1909 // as usual because the group-list is not available at this point.
1910 // But bypassing is fine because all we want here is check if the
1911 // workspace mounts are inside the current webmounts rootline.
1912 // The actual permission checking on page level is done elsewhere
1913 // as usual anyway before the page tree is rendered.
1914 $readPerms = '1=1';
1915 // Traverse mount points of the
1916 $dbMountpoints = GeneralUtility::intExplode(',', $dbMountpoints);
1917 foreach ($dbMountpoints as $mpId) {
1918 if ($this->isInWebMount($mpId, $readPerms)) {
1919 $filteredDbMountpoints[] = $mpId;
1920 }
1921 }
1922 // Re-insert webmounts:
1923 $filteredDbMountpoints = array_unique($filteredDbMountpoints);
1924 $this->groupData['webmounts'] = implode(',', $filteredDbMountpoints);
1925 }
1926 }
1927
1928 /**
1929 * Checking if a workspace is allowed for backend user
1930 *
1931 * @param mixed $wsRec If integer, workspace record is looked up, if array it is seen as a Workspace record with at least uid, title, members and adminusers columns. Can be faked for workspaces uid 0 and -1 (online and offline)
1932 * @param string $fields List of fields to select. Default fields are: uid,title,adminusers,members,reviewers,publish_access,stagechg_notification
1933 * @return array Output will also show how access was granted. Admin users will have a true output regardless of input.
1934 * @todo Define visibility
1935 */
1936 public function checkWorkspace($wsRec, $fields = 'uid,title,adminusers,members,reviewers,publish_access,stagechg_notification') {
1937 $retVal = FALSE;
1938 // If not array, look up workspace record:
1939 if (!is_array($wsRec)) {
1940 switch ((string) $wsRec) {
1941 case '0':
1942 $wsRec = array('uid' => $wsRec);
1943 break;
1944 default:
1945 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
1946 $wsRec = $this->db->exec_SELECTgetSingleRow($fields,
1947 'sys_workspace',
1948 'pid=0 AND uid=' . (int)$wsRec . BackendUtility::deleteClause('sys_workspace'),
1949 '',
1950 'title'
1951 );
1952 }
1953 }
1954 }
1955 // If wsRec is set to an array, evaluate it:
1956 if (is_array($wsRec)) {
1957 if ($this->isAdmin()) {
1958 return array_merge($wsRec, array('_ACCESS' => 'admin'));
1959 } else {
1960 switch ((string) $wsRec['uid']) {
1961 case '0':
1962 $retVal = $this->groupData['workspace_perms'] & 1
1963 ? array_merge($wsRec, array('_ACCESS' => 'online'))
1964 : FALSE;
1965 break;
1966 default:
1967 // Checking if the guy is admin:
1968 if (GeneralUtility::inList($wsRec['adminusers'], 'be_users_' . $this->user['uid'])) {
1969 return array_merge($wsRec, array('_ACCESS' => 'owner'));
1970 }
1971 // Checking if he is owner through a user group of his:
1972 foreach ($this->userGroupsUID as $groupUid) {
1973 if (GeneralUtility::inList($wsRec['adminusers'], 'be_groups_' . $groupUid)) {
1974 return array_merge($wsRec, array('_ACCESS' => 'owner'));
1975 }
1976 }
1977 // Checking if he is reviewer user:
1978 if (GeneralUtility::inList($wsRec['reviewers'], 'be_users_' . $this->user['uid'])) {
1979 return array_merge($wsRec, array('_ACCESS' => 'reviewer'));
1980 }
1981 // Checking if he is reviewer through a user group of his:
1982 foreach ($this->userGroupsUID as $groupUid) {
1983 if (GeneralUtility::inList($wsRec['reviewers'], 'be_groups_' . $groupUid)) {
1984 return array_merge($wsRec, array('_ACCESS' => 'reviewer'));
1985 }
1986 }
1987 // Checking if he is member as user:
1988 if (GeneralUtility::inList($wsRec['members'], 'be_users_' . $this->user['uid'])) {
1989 return array_merge($wsRec, array('_ACCESS' => 'member'));
1990 }
1991 // Checking if he is member through a user group of his:
1992 foreach ($this->userGroupsUID as $groupUid) {
1993 if (GeneralUtility::inList($wsRec['members'], 'be_groups_' . $groupUid)) {
1994 return array_merge($wsRec, array('_ACCESS' => 'member'));
1995 }
1996 }
1997 }
1998 }
1999 }
2000 return $retVal;
2001 }
2002
2003 /**
2004 * Uses checkWorkspace() to check if current workspace is available for user.
2005 * This function caches the result and so can be called many times with no performance loss.
2006 *
2007 * @return array See checkWorkspace()
2008 * @see checkWorkspace()
2009 * @todo Define visibility
2010 */
2011 public function checkWorkspaceCurrent() {
2012 if (!isset($this->checkWorkspaceCurrent_cache)) {
2013 $this->checkWorkspaceCurrent_cache = $this->checkWorkspace($this->workspace);
2014 }
2015 return $this->checkWorkspaceCurrent_cache;
2016 }
2017
2018 /**
2019 * Setting workspace ID
2020 *
2021 * @param integer $workspaceId ID of workspace to set for backend user. If not valid the default workspace for BE user is found and set.
2022 * @return void
2023 * @todo Define visibility
2024 */
2025 public function setWorkspace($workspaceId) {
2026 // Check workspace validity and if not found, revert to default workspace.
2027 if (!$this->setTemporaryWorkspace($workspaceId)) {
2028 $this->setDefaultWorkspace();
2029 }
2030 // Unset access cache:
2031 $this->checkWorkspaceCurrent_cache = NULL;
2032 // If ID is different from the stored one, change it:
2033 if ((int)$this->workspace !== (int)$this->user['workspace_id']) {
2034 $this->user['workspace_id'] = $this->workspace;
2035 $this->db->exec_UPDATEquery('be_users', 'uid=' . (int)$this->user['uid'], array('workspace_id' => $this->user['workspace_id']));
2036 $this->simplelog('User changed workspace to "' . $this->workspace . '"');
2037 }
2038 }
2039
2040 /**
2041 * Sets a temporary workspace in the context of the current backend user.
2042 *
2043 * @param integer $workspaceId
2044 * @return boolean
2045 */
2046 public function setTemporaryWorkspace($workspaceId) {
2047 $result = FALSE;
2048 $workspaceRecord = $this->checkWorkspace($workspaceId, '*');
2049
2050 if ($workspaceRecord) {
2051 $this->workspaceRec = $workspaceRecord;
2052 $this->workspace = (int)$workspaceId;
2053 $result = TRUE;
2054 }
2055
2056 return $result;
2057 }
2058
2059 /**
2060 * Sets the default workspace in the context of the current backend user.
2061 *
2062 * @return void
2063 */
2064 public function setDefaultWorkspace() {
2065 $this->workspace = (int)$this->getDefaultWorkspace();
2066 $this->workspaceRec = $this->checkWorkspace($this->workspace, '*');
2067 }
2068
2069 /**
2070 * Setting workspace preview state for user:
2071 *
2072 * @param boolean $previewState State of user preview.
2073 * @return void
2074 * @todo Define visibility
2075 */
2076 public function setWorkspacePreview($previewState) {
2077 $this->user['workspace_preview'] = $previewState;
2078 $this->db->exec_UPDATEquery('be_users', 'uid=' . (int)$this->user['uid'], array('workspace_preview' => $this->user['workspace_preview']));
2079 }
2080
2081 /**
2082 * Return default workspace ID for user,
2083 * If EXT:workspaces is not installed the user will be pushed the the
2084 * Live workspace
2085 *
2086 * @return integer Default workspace id. If no workspace is available it will be "-99
2087 * @todo Define visibility
2088 */
2089 public function getDefaultWorkspace() {
2090 $defaultWorkspace = -99;
2091 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces') || $this->checkWorkspace(0)) {
2092 // Check online
2093 $defaultWorkspace = 0;
2094 } elseif ($this->checkWorkspace(-1)) {
2095 // Check offline
2096 $defaultWorkspace = -1;
2097 } elseif (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
2098 // Traverse custom workspaces:
2099 $workspaces = $this->db->exec_SELECTgetRows('uid,title,adminusers,members,reviewers', 'sys_workspace', 'pid=0' . BackendUtility::deleteClause('sys_workspace'), '', 'title');
2100 foreach ($workspaces as $rec) {
2101 if ($this->checkWorkspace($rec)) {
2102 $defaultWorkspace = $rec['uid'];
2103 break;
2104 }
2105 }
2106 }
2107 return $defaultWorkspace;
2108 }
2109
2110 /**
2111 * Writes an entry in the logfile/table
2112 * Documentation in "TYPO3 Core API"
2113 *
2114 * @param integer $type Denotes which module that has submitted the entry. See "TYPO3 Core API". Use "4" for extensions.
2115 * @param integer $action Denotes which specific operation that wrote the entry. Use "0" when no sub-categorizing applies
2116 * @param integer $error Flag. 0 = message, 1 = error (user problem), 2 = System Error (which should not happen), 3 = security notice (admin)
2117 * @param integer $details_nr The message number. Specific for each $type and $action. This will make it possible to translate errormessages to other languages
2118 * @param string $details Default text that follows the message (in english!). Possibly translated by identification through type/action/details_nr
2119 * @param array $data Data that follows the log. Might be used to carry special information. If an array the first 5 entries (0-4) will be sprintf'ed with the details-text
2120 * @param string $tablename Table name. Special field used by tce_main.php.
2121 * @param int|string $recuid Record UID. Special field used by tce_main.php.
2122 * @param int|string $recpid Record PID. Special field used by tce_main.php. OBSOLETE
2123 * @param integer $event_pid The page_uid (pid) where the event occurred. Used to select log-content for specific pages.
2124 * @param string $NEWid Special field used by tce_main.php. NEWid string of newly created records.
2125 * @param integer $userId Alternative Backend User ID (used for logging login actions where this is not yet known).
2126 * @return integer Log entry ID.
2127 * @todo Define visibility
2128 */
2129 public function writelog($type, $action, $error, $details_nr, $details, $data, $tablename = '', $recuid = '', $recpid = '', $event_pid = -1, $NEWid = '', $userId = 0) {
2130 if (!$userId && isset($this->user['uid'])) {
2131 $userId = $this->user['uid'];
2132 }
2133
2134 $fields_values = array(
2135 'userid' => (int)$userId,
2136 'type' => (int)$type,
2137 'action' => (int)$action,
2138 'error' => (int)$error,
2139 'details_nr' => (int)$details_nr,
2140 'details' => $details,
2141 'log_data' => serialize($data),
2142 'tablename' => $tablename,
2143 'recuid' => (int)$recuid,
2144 'IP' => (string) GeneralUtility::getIndpEnv('REMOTE_ADDR'),
2145 'tstamp' => $GLOBALS['EXEC_TIME'],
2146 'event_pid' => (int)$event_pid,
2147 'NEWid' => $NEWid,
2148 'workspace' => $this->workspace
2149 );
2150 $this->db->exec_INSERTquery('sys_log', $fields_values);
2151 return $this->db->sql_insert_id();
2152 }
2153
2154 /**
2155 * Simple logging function
2156 *
2157 * @param string $message Log message
2158 * @param string $extKey Option extension key / module name
2159 * @param integer $error Error level. 0 = message, 1 = error (user problem), 2 = System Error (which should not happen), 3 = security notice (admin)
2160 * @return integer Log entry UID
2161 * @todo Define visibility
2162 */
2163 public function simplelog($message, $extKey = '', $error = 0) {
2164 return $this->writelog(4, 0, $error, 0, ($extKey ? '[' . $extKey . '] ' : '') . $message, array());
2165 }
2166
2167 /**
2168 * Sends a warning to $email if there has been a certain amount of failed logins during a period.
2169 * If a login fails, this function is called. It will look up the sys_log to see if there
2170 * have been more than $max failed logins the last $secondsBack seconds (default 3600).
2171 * If so, an email with a warning is sent to $email.
2172 *
2173 * @param string $email Email address
2174 * @param integer $secondsBack Number of sections back in time to check. This is a kind of limit for how many failures an hour for instance.
2175 * @param integer $max Max allowed failures before a warning mail is sent
2176 * @return void
2177 * @access private
2178 * @todo Define visibility
2179 */
2180 public function checkLogFailures($email, $secondsBack = 3600, $max = 3) {
2181 if ($email) {
2182 // Get last flag set in the log for sending
2183 $theTimeBack = $GLOBALS['EXEC_TIME'] - $secondsBack;
2184 $res = $this->db->exec_SELECTquery('tstamp', 'sys_log', 'type=255 AND action=4 AND tstamp>' . (int)$theTimeBack, '', 'tstamp DESC', '1');
2185 if ($testRow = $this->db->sql_fetch_assoc($res)) {
2186 $theTimeBack = $testRow['tstamp'];
2187 }
2188 $this->db->sql_free_result($res);
2189 // Check for more than $max number of error failures with the last period.
2190 $res = $this->db->exec_SELECTquery('*', 'sys_log', 'type=255 AND action=3 AND error<>0 AND tstamp>' . (int)$theTimeBack, '', 'tstamp');
2191 if ($this->db->sql_num_rows($res) > $max) {
2192 // OK, so there were more than the max allowed number of login failures - so we will send an email then.
2193 $subject = 'TYPO3 Login Failure Warning (at ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . ')';
2194 $email_body = 'There have been some attempts (' . $this->db->sql_num_rows($res) . ') to login at the TYPO3
2195 site "' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '" (' . GeneralUtility::getIndpEnv('HTTP_HOST') . ').
2196
2197 This is a dump of the failures:
2198
2199 ';
2200 while ($testRows = $this->db->sql_fetch_assoc($res)) {
2201 $theData = unserialize($testRows['log_data']);
2202 $email_body .= date(
2203 $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
2204 $testRows['tstamp']
2205 ) . ': ' . @sprintf($testRows['details'], (string)$theData[0], (string)$theData[1], (string)$theData[2]);
2206 $email_body .= LF;
2207 }
2208 $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
2209 /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
2210 $mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
2211 $mail->setTo($email)->setFrom($from)->setSubject($subject)->setBody($email_body);
2212 $mail->send();
2213 // Logout written to log
2214 $this->writelog(255, 4, 0, 3, 'Failure warning (%s failures within %s seconds) sent by email to %s', array($this->db->sql_num_rows($res), $secondsBack, $email));
2215 $this->db->sql_free_result($res);
2216 }
2217 }
2218 }
2219
2220 /**
2221 * Getter for the cookie name
2222 *
2223 * @static
2224 * @return string returns the configured cookie name
2225 */
2226 static public function getCookieName() {
2227 $configuredCookieName = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']);
2228 if (empty($configuredCookieName)) {
2229 $configuredCookieName = 'be_typo_user';
2230 }
2231 return $configuredCookieName;
2232 }
2233
2234 /**
2235 * If TYPO3_CONF_VARS['BE']['enabledBeUserIPLock'] is enabled and
2236 * an IP-list is found in the User TSconfig objString "options.lockToIP",
2237 * then make an IP comparison with REMOTE_ADDR and return the outcome (TRUE/FALSE)
2238 *
2239 * @return boolean TRUE, if IP address validates OK (or no check is done at all)
2240 * @todo Define visibility
2241 */
2242 public function checkLockToIP() {
2243 $out = 1;
2244 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['enabledBeUserIPLock']) {
2245 $IPList = $this->getTSConfigVal('options.lockToIP');
2246 if (trim($IPList)) {
2247 $baseIP = GeneralUtility::getIndpEnv('REMOTE_ADDR');
2248 $out = GeneralUtility::cmpIP($baseIP, $IPList);
2249 }
2250 }
2251 return $out;
2252 }
2253
2254 /**
2255 * Check if user is logged in and if so, call ->fetchGroupData() to load group information and
2256 * access lists of all kind, further check IP, set the ->uc array and send login-notification email if required.
2257 * If no user is logged in the default behaviour is to exit with an error message,
2258 * but this will happen ONLY if the constant TYPO3_PROCEED_IF_NO_USER is set TRUE.
2259 * This function is called right after ->start() in fx. init.php
2260 *
2261 * @throws \RuntimeException
2262 * @return void
2263 * @todo Define visibility
2264 */
2265 public function backendCheckLogin() {
2266 if (empty($this->user['uid'])) {
2267 if (!defined('TYPO3_PROCEED_IF_NO_USER') || !TYPO3_PROCEED_IF_NO_USER) {
2268 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($GLOBALS['BACK_PATH']);
2269 }
2270 } else {
2271 // ...and if that's the case, call these functions
2272 $this->fetchGroupData();
2273 // The groups are fetched and ready for permission checking in this initialization.
2274 // Tables.php must be read before this because stuff like the modules has impact in this
2275 if ($this->checkLockToIP()) {
2276 if ($this->isUserAllowedToLogin()) {
2277 // Setting the UC array. It's needed with fetchGroupData first, due to default/overriding of values.
2278 $this->backendSetUC();
2279 // Email at login - if option set.
2280 $this->emailAtLogin();
2281 } else {
2282 throw new \RuntimeException('Login Error: TYPO3 is in maintenance mode at the moment. Only administrators are allowed access.', 1294585860);
2283 }
2284 } else {
2285 throw new \RuntimeException('Login Error: IP locking prevented you from being authorized. Can\'t proceed, sorry.', 1294585861);
2286 }
2287 }
2288 }
2289
2290 /**
2291 * If the backend script is in CLI mode, it will try to load a backend user named by the CLI module name (in lowercase)
2292 *
2293 * @return boolean Returns TRUE if a CLI user was loaded, otherwise FALSE!
2294 * @todo Define visibility
2295 */
2296 public function checkCLIuser() {
2297 // First, check if cliMode is enabled:
2298 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
2299 if (!$this->user['uid']) {
2300 if (substr($GLOBALS['MCONF']['name'], 0, 5) == '_CLI_') {
2301 $userName = strtolower($GLOBALS['MCONF']['name']);
2302 $this->setBeUserByName($userName);
2303 if ($this->user['uid']) {
2304 if (!$this->isAdmin()) {
2305 return TRUE;
2306 } else {
2307 fwrite(STDERR, 'ERROR: CLI backend user "' . $userName . '" was ADMIN which is not allowed!' . LF . LF);
2308 die(3);
2309 }
2310 } else {
2311 fwrite(STDERR, 'ERROR: No backend user named "' . $userName . '" was found!' . LF . LF);
2312 die(3);
2313 }
2314 } else {
2315 fwrite(STDERR, 'ERROR: Module name, "' . $GLOBALS['MCONF']['name'] . '", was not prefixed with "_CLI_"' . LF . LF);
2316 die(3);
2317 }
2318 } else {
2319 fwrite(STDERR, 'ERROR: Another user was already loaded which is impossible in CLI mode!' . LF . LF);
2320 die(3);
2321 }
2322 }
2323 return FALSE;
2324 }
2325
2326 /**
2327 * Initialize the internal ->uc array for the backend user
2328 * Will make the overrides if necessary, and write the UC back to the be_users record if changes has happened
2329 *
2330 * @return void
2331 * @internal
2332 * @todo Define visibility
2333 */
2334 public function backendSetUC() {
2335 // UC - user configuration is a serialized array inside the user object
2336 // If there is a saved uc we implement that instead of the default one.
2337 $temp_theSavedUC = unserialize($this->user['uc']);
2338 if (is_array($temp_theSavedUC)) {
2339 $this->unpack_uc($temp_theSavedUC);
2340 }
2341 // Setting defaults if uc is empty
2342 $updated = FALSE;
2343 if (!is_array($this->uc)) {
2344 $this->uc = array_merge(
2345 $this->uc_default,
2346 (array)$GLOBALS['TYPO3_CONF_VARS']['BE']['defaultUC'],
2347 GeneralUtility::removeDotsFromTS((array)$this->getTSConfigProp('setup.default'))
2348 );
2349 $this->overrideUC();
2350 $updated = TRUE;
2351 }
2352 // If TSconfig is updated, update the defaultUC.
2353 if ($this->userTSUpdated) {
2354 $this->overrideUC();
2355 $updated = TRUE;
2356 }
2357 // Setting default lang from be_user record.
2358 if (!isset($this->uc['lang'])) {
2359 $this->uc['lang'] = $this->user['lang'];
2360 $updated = TRUE;
2361 }
2362 // Setting the time of the first login:
2363 if (!isset($this->uc['firstLoginTimeStamp'])) {
2364 $this->uc['firstLoginTimeStamp'] = $GLOBALS['EXEC_TIME'];
2365 $updated = TRUE;
2366 }
2367 // Saving if updated.
2368 if ($updated) {
2369 $this->writeUC();
2370 }
2371 }
2372
2373 /**
2374 * Override: Call this function every time the uc is updated.
2375 * That is 1) by reverting to default values, 2) in the setup-module, 3) userTS changes (userauthgroup)
2376 *
2377 * @return void
2378 * @internal
2379 * @todo Define visibility
2380 */
2381 public function overrideUC() {
2382 $this->uc = array_merge((array) $this->uc, (array) $this->getTSConfigProp('setup.override'));
2383 }
2384
2385 /**
2386 * Clears the user[uc] and ->uc to blank strings. Then calls ->backendSetUC() to fill it again with reset contents
2387 *
2388 * @return void
2389 * @internal
2390 * @todo Define visibility
2391 */
2392 public function resetUC() {
2393 $this->user['uc'] = '';
2394 $this->uc = '';
2395 $this->backendSetUC();
2396 }
2397
2398 /**
2399 * Will send an email notification to warning_email_address/the login users email address when a login session is just started.
2400 * Depends on various parameters whether mails are send and to whom.
2401 *
2402 * @return void
2403 * @access private
2404 */
2405 private function emailAtLogin() {
2406 if ($this->loginSessionStarted) {
2407 // Send notify-mail
2408 $subject = 'At "' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '"' . ' from '
2409 . GeneralUtility::getIndpEnv('REMOTE_ADDR')
2410 . (GeneralUtility::getIndpEnv('REMOTE_HOST') ? ' (' . GeneralUtility::getIndpEnv('REMOTE_HOST') . ')' : '');
2411 $msg = sprintf(
2412 'User "%s" logged in from %s (%s) at "%s" (%s)',
2413 $this->user['username'],
2414 GeneralUtility::getIndpEnv('REMOTE_ADDR'),
2415 GeneralUtility::getIndpEnv('REMOTE_HOST'),
2416 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
2417 GeneralUtility::getIndpEnv('HTTP_HOST')
2418 );
2419 // Warning email address
2420 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr']) {
2421 $warn = 0;
2422 $prefix = '';
2423 if ((int)$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] & 1) {
2424 // first bit: All logins
2425 $warn = 1;
2426 $prefix = $this->isAdmin() ? '[AdminLoginWarning]' : '[LoginWarning]';
2427 }
2428 if ($this->isAdmin() && (int)$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] & 2) {
2429 // second bit: Only admin-logins
2430 $warn = 1;
2431 $prefix = '[AdminLoginWarning]';
2432 }
2433 if ($warn) {
2434 $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
2435 /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
2436 $mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
2437 $mail->setTo($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'])->setFrom($from)->setSubject($prefix . ' ' . $subject)->setBody($msg);
2438 $mail->send();
2439 }
2440 }
2441 // If An email should be sent to the current user, do that:
2442 if ($this->uc['emailMeAtLogin'] && strstr($this->user['email'], '@')) {
2443 $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
2444 /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
2445 $mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
2446 $mail->setTo($this->user['email'])->setFrom($from)->setSubject($subject)->setBody($msg);
2447 $mail->send();
2448 }
2449 }
2450 }
2451
2452 /**
2453 * Determines whether a backend user is allowed to access the backend.
2454 *
2455 * The conditions are:
2456 * + backend user is a regular user and adminOnly is not defined
2457 * + backend user is an admin user
2458 * + backend user is used in CLI context and adminOnly is explicitly set to "2"
2459 * + backend user is being controlled by an admin user
2460 *
2461 * @return boolean Whether a backend user is allowed to access the backend
2462 */
2463 protected function isUserAllowedToLogin() {
2464 $isUserAllowedToLogin = FALSE;
2465 $adminOnlyMode = $GLOBALS['TYPO3_CONF_VARS']['BE']['adminOnly'];
2466 // Backend user is allowed if adminOnly is not set or user is an admin:
2467 if (!$adminOnlyMode || $this->isAdmin()) {
2468 $isUserAllowedToLogin = TRUE;
2469 } elseif ($adminOnlyMode == 2 && TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
2470 $isUserAllowedToLogin = TRUE;
2471 } elseif ($this->user['ses_backuserid']) {
2472 $backendUserId = (int)$this->user['ses_backuserid'];
2473 $whereAdmin = 'uid=' . $backendUserId . ' AND admin=1' . BackendUtility::BEenableFields('be_users');
2474 if ($this->db->exec_SELECTcountRows('uid', 'be_users', $whereAdmin) > 0) {
2475 $isUserAllowedToLogin = TRUE;
2476 }
2477 }
2478 return $isUserAllowedToLogin;
2479 }
2480
2481 /**
2482 * Logs out the current user and clears the form protection tokens.
2483 */
2484 public function logoff() {
2485 if (isset($GLOBALS['BE_USER'])) {
2486 \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get()->clean();
2487 }
2488 parent::logoff();
2489 }
2490
2491 }