DataHandler.php 339 KB
Newer Older
1
<?php
2
namespace TYPO3\CMS\Core\DataHandling;
3

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
Christian Kuhn's avatar
Christian Kuhn committed
16

17
use TYPO3\CMS\Backend\Utility\BackendUtility;
18
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
19
20
21
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
22
use TYPO3\CMS\Core\Database\DatabaseConnection;
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
23
24
use TYPO3\CMS\Core\Database\ReferenceIndex;
use TYPO3\CMS\Core\Database\RelationHandler;
Christian Kuhn's avatar
Christian Kuhn committed
25
use TYPO3\CMS\Core\Html\RteHtmlParser;
26
use TYPO3\CMS\Core\Messaging\FlashMessage;
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
27
28
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Resource\ResourceFactory;
29
use TYPO3\CMS\Core\Type\Bitmask\Permission;
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
30
31
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\File\BasicFileUtility;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
33
34
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
35
use TYPO3\CMS\Core\Utility\StringUtility;
36
use TYPO3\CMS\Core\Versioning\VersionState;
37

38
/**
39
40
 * The main data handler class which takes care of correctly updating and inserting records.
 * This class was formerly known as TCEmain.
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 *
 * This is the TYPO3 Core Engine class for manipulation of the database
 * This class is used by eg. the tce_db.php script which provides an the interface for POST forms to this class.
 *
 * Dependencies:
 * - $GLOBALS['TCA'] must exist
 * - $GLOBALS['LANG'] must exist
 *
 * tce_db.php for further comments and SYNTAX! Also see document 'TYPO3 Core API' for details.
 */
class DataHandler {

	// *********************
	// Public variables you can configure before using the class:
	// *********************
	/**
57
58
59
	 * If TRUE, the default log-messages will be stored. This should not be necessary if the locallang-file for the
	 * log-display is properly configured. So disabling this will just save some database-space as the default messages are not saved.
	 *
60
	 * @var bool
61
62
63
64
	 */
	public $storeLogMessages = TRUE;

	/**
65
66
	 * If TRUE, actions are logged to sys_log.
	 *
67
	 * @var bool
68
69
70
71
	 */
	public $enableLogging = TRUE;

	/**
72
73
74
	 * If TRUE, the datamap array is reversed in the order, which is a nice thing if you're creating a whole new
	 * bunch of records.
	 *
75
	 * @var bool
76
77
78
79
	 */
	public $reverseOrder = FALSE;

	/**
80
81
82
	 * If TRUE, only fields which are different from the database values are saved! In fact, if a whole input array
	 * is similar, it's not saved then.
	 *
83
	 * @var bool
84
85
86
87
	 */
	public $checkSimilar = TRUE;

	/**
88
89
90
	 * If TRUE, incoming values in the data-array have their slashes stripped. ALWAYS SET THIS TO ZERO and supply an
	 * unescaped data array instead. This switch may totally disappear in future versions of this class!
	 *
91
	 * @var bool
92
	 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
93
94
95
96
	 */
	public $stripslashes_values = TRUE;

	/**
97
98
99
	 * This will read the record after having updated or inserted it. If anything is not properly submitted an error
	 * is written to the log. This feature consumes extra time by selecting records
	 *
100
	 * @var bool
101
102
103
104
	 */
	public $checkStoredRecords = TRUE;

	/**
105
106
	 * If set, values '' and 0 will equal each other when the stored records are checked.
	 *
107
	 * @var bool
108
109
110
111
	 */
	public $checkStoredRecords_loose = TRUE;

	/**
112
113
114
	 * If this is set, then a page is deleted by deleting the whole branch under it (user must have
	 * delete permissions to it all). If not set, then the page is deleted ONLY if it has no branch.
	 *
115
	 * @var bool
116
117
118
119
	 */
	public $deleteTree = FALSE;

	/**
120
121
	 * If set, then the 'hideAtCopy' flag for tables will be ignored.
	 *
122
	 * @var bool
123
124
125
	 */
	public $neverHideAtCopy = FALSE;

126
127
128
	/**
	 * If set, then the TCE class has been instantiated during an import action of a T3D
	 *
129
	 * @var bool
130
131
132
	 */
	public $isImporting = FALSE;

133
	/**
134
135
	 * If set, then transformations are NOT performed on the input.
	 *
136
	 * @var bool
137
138
139
140
	 */
	public $dontProcessTransformations = FALSE;

	/**
141
142
	 * If set, .vDEFbase values are unset in flexforms.
	 *
143
	 * @var bool
144
	 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
145
146
147
148
	 */
	public $clear_flexFormData_vDEFbase = FALSE;

	/**
149
150
151
152
	 * TRUE: (traditional) Updates when record is saved. For flexforms, updates if change is made to the localized value.
	 * FALSE: Will not update anything.
	 * "FORCE_FFUPD" (string): Like TRUE, but will force update to the FlexForm Field
	 *
153
	 * @var bool|string
154
155
156
157
	 */
	public $updateModeL10NdiffData = TRUE;

	/**
158
159
160
	 * If TRUE, the translation diff. fields will in fact be reset so that they indicate that all needs to change again!
	 * It's meant as the opposite of declaring the record translated.
	 *
161
	 * @var bool
162
163
164
165
	 */
	public $updateModeL10NdiffDataClear = FALSE;

	/**
166
167
168
	 * If TRUE, workspace restrictions are bypassed on edit an create actions (process_datamap()).
	 * YOU MUST KNOW what you do if you use this feature!
	 *
169
	 * @var bool
170
171
172
173
	 */
	public $bypassWorkspaceRestrictions = FALSE;

	/**
174
175
176
	 * If TRUE, file handling of attached files (addition, deletion etc) is bypassed - the value is saved straight away.
	 * YOU MUST KNOW what you are doing with this feature!
	 *
177
	 * @var bool
178
179
180
181
	 */
	public $bypassFileHandling = FALSE;

	/**
182
183
184
	 * If TRUE, access check, check for deleted etc. for records is bypassed.
	 * YOU MUST KNOW what you are doing if you use this feature!
	 *
185
	 * @var bool
186
187
188
189
	 */
	public $bypassAccessCheckForRecords = FALSE;

	/**
190
191
192
193
	 * Comma-separated list. This list of tables decides which tables will be copied. If empty then none will.
	 * If '*' then all will (that the user has permission to of course)
	 *
	 * @var string
194
195
196
197
	 */
	public $copyWhichTables = '*';

	/**
198
199
200
201
	 * If 0 then branch is NOT copied.
	 * If 1 then pages on the 1st level is copied.
	 * If 2 then pages on the second level is copied ... and so on
	 *
202
	 * @var int
203
204
205
206
	 */
	public $copyTree = 0;

	/**
207
208
209
210
211
212
	 * [table][fields]=value: New records are created with default values and you can set this array on the
	 * form $defaultValues[$table][$field] = $value to override the default values fetched from TCA.
	 * If ->setDefaultsFromUserTS is called UserTSconfig default values will overrule existing values in this array
	 * (thus UserTSconfig overrules externally set defaults which overrules TCA defaults)
	 *
	 * @var array
213
214
215
216
	 */
	public $defaultValues = array();

	/**
217
218
219
220
221
	 * [table][fields]=value: You can set this array on the form $overrideValues[$table][$field] = $value to
	 * override the incoming data. You must set this externally. You must make sure the fields in this array are also
	 * found in the table, because it's not checked. All columns can be set by this array!
	 *
	 * @var array
222
223
224
225
	 */
	public $overrideValues = array();

	/**
226
227
228
229
230
	 * [filename]=alternative_filename: Use this array to force another name onto a file.
	 * Eg. if you set ['/tmp/blablabal'] = 'my_file.txt' and '/tmp/blablabal' is set for a certain file-field,
	 * then 'my_file.txt' will be used as the name instead.
	 *
	 * @var array
231
232
233
234
	 */
	public $alternativeFileName = array();

	/**
235
236
237
	 * Array [filename]=alternative_filepath: Same as alternativeFileName but with relative path to the file
	 *
	 * @var array
238
239
240
241
	 */
	public $alternativeFilePath = array();

	/**
242
243
244
245
246
	 * If entries are set in this array corresponding to fields for update, they are ignored and thus NOT updated.
	 * You could set this array from a series of checkboxes with value=0 and hidden fields before the checkbox with 1.
	 * Then an empty checkbox will disable the field.
	 *
	 * @var array
247
248
249
250
	 */
	public $data_disableFields = array();

	/**
251
252
253
254
255
256
257
	 * Use this array to validate suggested uids for tables by setting [table]:[uid]. This is a dangerous option
	 * since it will force the inserted record to have a certain UID. The value just have to be TRUE, but if you set
	 * it to "DELETE" it will make sure any record with that UID will be deleted first (raw delete).
	 * The option is used for import of T3D files when synchronizing between two mirrored servers.
	 * As a security measure this feature is available only for Admin Users (for now)
	 *
	 * @var array
258
259
260
261
	 */
	public $suggestedInsertUids = array();

	/**
262
263
264
265
	 * Object. Call back object for FlexForm traversal. Useful when external classes wants to use the
	 * iteration functions inside DataHandler for traversing a FlexForm structure.
	 *
	 * @var object
266
267
268
269
270
271
272
	 */
	public $callBackObj;

	// *********************
	// Internal variables (mapping arrays) which can be used (read-only) from outside
	// *********************
	/**
273
274
275
	 * Contains mapping of auto-versionized records.
	 *
	 * @var array
276
277
278
279
	 */
	public $autoVersionIdMap = array();

	/**
280
281
282
	 * When new elements are created, this array contains a map between their "NEW..." string IDs and the eventual UID they got when stored in database
	 *
	 * @var array
283
284
285
286
	 */
	public $substNEWwithIDs = array();

	/**
287
288
289
	 * Like $substNEWwithIDs, but where each old "NEW..." id is mapped to the table it was from.
	 *
	 * @var array
290
291
292
293
	 */
	public $substNEWwithIDs_table = array();

	/**
294
295
296
	 * Holds the tables and there the ids of newly created child records from IRRE
	 *
	 * @var array
297
298
299
300
	 */
	public $newRelatedIDs = array();

	/**
301
302
303
	 * This array is the sum of all copying operations in this class. May be READ from outside, thus partly public.
	 *
	 * @var array
304
305
306
307
	 */
	public $copyMappingArray_merged = array();

	/**
308
309
310
	 * A map between input file name and final destination for files being attached to records.
	 *
	 * @var array
311
312
313
314
	 */
	public $copiedFileMap = array();

	/**
315
316
317
	 * Contains [table][id][field] of fiels where RTEmagic images was copied. Holds old filename as key and new filename as value.
	 *
	 * @var array
318
319
320
321
	 */
	public $RTEmagic_copyIndex = array();

	/**
322
323
324
	 * Errors are collected in this variable.
	 *
	 * @var array
325
326
327
	 */
	public $errorLog = array();

328
329
330
331
332
	/**
	 * Fields from the pages-table for which changes will trigger a pagetree refresh
	 *
	 * @var array
	 */
333
	public $pagetreeRefreshFieldsFromPages = array('pid', 'sorting', 'deleted', 'hidden', 'title', 'doktype', 'is_siteroot', 'fe_group', 'nav_hide', 'nav_title', 'module', 'starttime', 'endtime', 'content_from_pid');
334
335
336
337
338
339
340
341

	/**
	 * Indicates whether the pagetree needs a refresh because of important changes
	 *
	 * @var bool
	 */
	public $pagetreeNeedsRefresh = FALSE;

342

343
344
345
	// *********************
	// Internal Variables, do not touch.
	// *********************
346

347
	// Variables set in init() function:
348

349
350
351
	/**
	 * The user-object the script uses. If not set from outside, this is set to the current global $BE_USER.
	 *
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
352
	 * @var BackendUserAuthentication
353
354
355
356
	 */
	public $BE_USER;

	/**
357
358
	 * Will be set to uid of be_user executing this script
	 *
359
	 * @var int
360
361
362
363
	 */
	public $userid;

	/**
364
365
366
	 * Will be set to username of be_user executing this script
	 *
	 * @var string
367
368
369
370
	 */
	public $username;

	/**
371
372
	 * Will be set if user is admin
	 *
373
	 * @var bool
374
375
376
377
	 */
	public $admin;

	/**
378
379
380
	 * Can be overridden from $GLOBALS['TYPO3_CONF_VARS']
	 *
	 * @var array
381
382
383
384
385
386
387
388
	 */
	public $defaultPermissions = array(
		'user' => 'show,edit,delete,new,editcontent',
		'group' => 'show,edit,new,editcontent',
		'everybody' => ''
	);

	/**
389
	 * The list of <table>-<fields> that cannot be edited by user. This is compiled from TCA/exclude-flag combined with non_exclude_fields for the user.
390
391
	 *
	 * @var array
392
	 */
393
	protected $excludedTablesAndFields = array();
394

Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
395
396
397
398
399
400
	/**
	 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
	 * @var int
	 */
	public $include_filefunctions;

401
402
	/**
	 * Data submitted from the form view, used to control behaviours,
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
403
	 * e.g. this is used to activate/deactivate fields and thus store NULL values
404
405
406
407
408
	 *
	 * @var array
	 */
	protected $control = array();

409
	/**
410
411
412
	 * Set with incoming data array
	 *
	 * @var array
413
414
415
416
	 */
	public $datamap = array();

	/**
417
418
419
	 * Set with incoming cmd array
	 *
	 * @var array
420
421
422
	 */
	public $cmdmap = array();

423
424
425
426
427
428
429
430
431
432
433
434
435
436
	/**
	 * List of changed old record ids to new records ids
	 *
	 * @var array
	 */
	protected $mmHistoryRecords = array();

	/**
	 * List of changed old record ids to new records ids
	 *
	 * @var array
	 */
	protected $historyRecords = array();

437
438
	// Internal static:
	/**
439
440
441
	 * Permission mapping
	 *
	 * @var array
442
443
444
445
446
447
448
449
450
451
452
453
454
455
	 */
	public $pMap = array(
		'show' => 1,
		// 1st bit
		'edit' => 2,
		// 2nd bit
		'delete' => 4,
		// 3rd bit
		'new' => 8,
		// 4th bit
		'editcontent' => 16
	);

	/**
456
457
	 * Integer: The interval between sorting numbers used with tables with a 'sorting' field defined. Min 1
	 *
458
	 * @var int
459
460
461
462
463
	 */
	public $sortIntervals = 256;

	// Internal caching arrays
	/**
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
464
	 * Used by function checkRecordUpdateAccess() to store whether a record is updatable or not.
465
466
	 *
	 * @var array
467
468
469
470
	 */
	public $recUpdateAccessCache = array();

	/**
471
472
473
	 * User by function checkRecordInsertAccess() to store whether a record can be inserted on a page id
	 *
	 * @var array
474
475
476
477
	 */
	public $recInsertAccessCache = array();

	/**
478
479
480
	 * Caching array for check of whether records are in a webmount
	 *
	 * @var array
481
482
483
484
	 */
	public $isRecordInWebMount_Cache = array();

	/**
485
486
487
	 * Caching array for page ids in webmounts
	 *
	 * @var array
488
489
490
491
	 */
	public $isInWebMount_Cache = array();

	/**
492
493
494
	 * Caching for collecting TSconfig for page ids
	 *
	 * @var array
495
496
497
498
	 */
	public $cachedTSconfig = array();

	/**
499
500
501
	 * Used for caching page records in pageInfo()
	 *
	 * @var array
502
503
504
505
	 */
	public $pageCache = array();

	/**
506
507
508
	 * Array caching workspace access for BE_USER
	 *
	 * @var array
509
510
511
512
513
	 */
	public $checkWorkspaceCache = array();

	// Other arrays:
	/**
514
515
516
	 * For accumulation of MM relations that must be written after new records are created.
	 *
	 * @var array
517
518
519
520
	 */
	public $dbAnalysisStore = array();

	/**
521
522
523
	 * For accumulation of files which must be deleted after processing of all input content
	 *
	 * @var array
524
525
526
527
	 */
	public $removeFilesStore = array();

	/**
528
529
530
	 * Uploaded files, set by process_uploads()
	 *
	 * @var array
531
532
533
534
	 */
	public $uploadedFileArray = array();

	/**
535
536
537
	 * Used for tracking references that might need correction after operations
	 *
	 * @var array
538
539
540
541
	 */
	public $registerDBList = array();

	/**
542
543
544
	 * Used for tracking references that might need correction in pid field after operations (e.g. IRRE)
	 *
	 * @var array
545
546
547
548
	 */
	public $registerDBPids = array();

	/**
549
550
551
552
553
554
555
556
	 * Used by the copy action to track the ids of new pages so subpages are correctly inserted!
	 * THIS is internally cleared for each executed copy operation! DO NOT USE THIS FROM OUTSIDE!
	 * Read from copyMappingArray_merged instead which is accumulating this information.
	 *
	 * NOTE: This is used by some outside scripts (e.g. hooks), as the results in $copyMappingArray_merged
	 * are only available after an action has been completed.
	 *
	 * @var array
557
558
559
560
	 */
	public $copyMappingArray = array();

	/**
561
562
563
	 * Array used for remapping uids and values at the end of process_datamap
	 *
	 * @var array
564
565
566
567
	 */
	public $remapStack = array();

	/**
568
569
570
571
	 * Array used for remapping uids and values at the end of process_datamap
	 * (e.g. $remapStackRecords[<table>][<uid>] = <index in $remapStack>)
	 *
	 * @var array
572
573
574
	 */
	public $remapStackRecords = array();

575
576
577
578
579
	/**
	 * Array used for checking whether new children need to be remapped
	 *
	 * @var array
	 */
580
581
	protected $remapStackChildIds = array();

582
583
584
585
586
	/**
	 * Array used for executing addition actions after remapping happened (set processRemapStack())
	 *
	 * @var array
	 */
587
588
	protected $remapStackActions = array();

589
590
591
592
593
	/**
	 * Array used for executing post-processing on the reference index
	 *
	 * @var array
	 */
594
595
596
	protected $remapStackRefIndex = array();

	/**
597
598
599
	 * Array used for additional calls to $this->updateRefIndex
	 *
	 * @var array
600
601
602
603
	 */
	public $updateRefIndexStack = array();

	/**
604
605
606
607
	 * Tells, that this DataHandler instance was called from \TYPO3\CMS\Impext\ImportExport.
	 * This variable is set by \TYPO3\CMS\Impext\ImportExport
	 *
	 * @var array
608
609
610
611
	 */
	public $callFromImpExp = FALSE;

	/**
612
613
614
	 * Array for new flexform index mapping
	 *
	 * @var array
615
616
617
618
619
620
	 */
	public $newIndexMap = array();

	// Various
	/**
	 * basicFileFunctions object
621
	 * For "singleton" file-manipulation object
622
	 *
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
623
	 * @var BasicFileUtility
624
625
626
627
	 */
	public $fileFunc;

	/**
628
629
630
	 * Set to "currentRecord" during checking of values.
	 *
	 * @var array
631
632
633
634
	 */
	public $checkValue_currentRecord = array();

	/**
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
635
	 * A signal flag used to tell file processing that auto versioning has happened and hence certain action should be applied.
636
	 *
637
	 * @var bool
638
639
640
	 */
	public $autoVersioningUpdate = FALSE;

641
642
643
	/**
	 * Disable delete clause
	 *
644
	 * @var bool
645
	 */
646
647
	protected $disableDeleteClause = FALSE;

648
649
650
	/**
	 * @var array
	 */
651
652
	protected $checkModifyAccessListHookObjects;

653
654
655
	/**
	 * @var array
	 */
656
657
658
	protected $version_remapMMForVersionSwap_reg;

	/**
Christian Kuhn's avatar
Christian Kuhn committed
659
660
	 * The outer most instance of \TYPO3\CMS\Core\DataHandling\DataHandler:
	 * This object instantiates itself on versioning and localization ...
661
	 *
662
	 * @var \TYPO3\CMS\Core\DataHandling\DataHandler
663
664
665
	 */
	protected $outerMostInstance = NULL;

666
667
668
669
670
	/**
	 * Internal cache for collecting records that should trigger cache clearing
	 *
	 * @var array
	 */
671
	static protected $recordsToClearCacheFor = array();
672

673
674
675
676
677
678
679
	/**
	 * Database layer. Identical to $GLOBALS['TYPO3_DB']
	 *
	 * @var DatabaseConnection
	 */
	protected $databaseConnection;

680
681
682
683
684
685
686
687
688
689
690
691
692
693
	/**
	 * Runtime Cache to store and retrieve data computed for a single request
	 *
	 * @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
	 */
	protected $runtimeCache = NULL;

	/**
	 * Prefix for the cache entries of nested element calls since the runtimeCache has a global scope.
	 *
	 * @var string
	 */
	protected $cachePrefixNestedElementCalls = 'core-datahandler-nestedElementCalls-';

694
	/**
695
	 *
696
	 */
697
698
	public function __construct() {
		$this->databaseConnection = $GLOBALS['TYPO3_DB'];
699
		$this->runtimeCache = $this->getRuntimeCache();
700
	}
701

702
703
704
705
706
707
708
	/**
	 * @param array $control
	 */
	public function setControl(array $control) {
		$this->control = $control;
	}

709
710
711
712
713
714
715
	/**
	 * Initializing.
	 * For details, see 'TYPO3 Core API' document.
	 * This function does not start the processing of data, but merely initializes the object
	 *
	 * @param array $data Data to be modified or inserted in the database
	 * @param array $cmd Commands to copy, move, delete, localize, versionize records.
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
716
	 * @param BackendUserAuthentication|NULL $altUserObject An alternative userobject you can set instead of the default, which is $GLOBALS['BE_USER']
717
718
	 * @return void
	 */
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
719
	public function start($data, $cmd, $altUserObject = NULL) {
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
		// Initializing BE_USER
		$this->BE_USER = is_object($altUserObject) ? $altUserObject : $GLOBALS['BE_USER'];
		$this->userid = $this->BE_USER->user['uid'];
		$this->username = $this->BE_USER->user['username'];
		$this->admin = $this->BE_USER->user['admin'];
		if ($this->BE_USER->uc['recursiveDelete']) {
			$this->deleteTree = 1;
		}
		if ($GLOBALS['TYPO3_CONF_VARS']['BE']['explicitConfirmationOfTranslation'] && $this->updateModeL10NdiffData === TRUE) {
			$this->updateModeL10NdiffData = FALSE;
		}
		// Initializing default permissions for pages
		$defaultPermissions = $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultPermissions'];
		if (isset($defaultPermissions['user'])) {
			$this->defaultPermissions['user'] = $defaultPermissions['user'];
		}
		if (isset($defaultPermissions['group'])) {
			$this->defaultPermissions['group'] = $defaultPermissions['group'];
		}
		if (isset($defaultPermissions['everybody'])) {
			$this->defaultPermissions['everybody'] = $defaultPermissions['everybody'];
		}
		// generates the excludelist, based on TCA/exclude-flag and non_exclude_fields for the user:
743
744
745
		if (!$this->admin) {
			$this->excludedTablesAndFields = array_flip($this->getExcludeListArray());
		}
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
		// Setting the data and cmd arrays
		if (is_array($data)) {
			reset($data);
			$this->datamap = $data;
		}
		if (is_array($cmd)) {
			reset($cmd);
			$this->cmdmap = $cmd;
		}
	}

	/**
	 * Function that can mirror input values in datamap-array to other uid numbers.
	 * Example: $mirror[table][11] = '22,33' will look for content in $this->datamap[table][11] and copy it to $this->datamap[table][22] and $this->datamap[table][33]
	 *
	 * @param array $mirror This array has the syntax $mirror[table_name][uid] = [list of uids to copy data-value TO!]
	 * @return void
	 */
	public function setMirror($mirror) {
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
		if (!is_array($mirror)) {
			return;
		}

		foreach ($mirror as $table => $uid_array) {
			if (!isset($this->datamap[$table])) {
				continue;
			}

			foreach ($uid_array as $id => $uidList) {
				if (!isset($this->datamap[$table][$id])) {
					continue;
				}

				$theIdsInArray = GeneralUtility::trimExplode(',', $uidList, TRUE);
				foreach ($theIdsInArray as $copyToUid) {
					$this->datamap[$table][$copyToUid] = $this->datamap[$table][$id];
782
783
784
785
786
787
788
789
790
791
792
793
				}
			}
		}
	}

	/**
	 * Initializes default values coming from User TSconfig
	 *
	 * @param array $userTS User TSconfig array
	 * @return void
	 */
	public function setDefaultsFromUserTS($userTS) {
794
795
796
797
798
799
800
801
802
803
804
805
806
807
		if (!is_array($userTS)) {
			return;
		}

		foreach ($userTS as $k => $v) {
			$k = substr($k, 0, -1);
			if (!$k || !is_array($v) || !isset($GLOBALS['TCA'][$k])) {
				continue;
			}

			if (is_array($this->defaultValues[$k])) {
				$this->defaultValues[$k] = array_merge($this->defaultValues[$k], $v);
			} else {
				$this->defaultValues[$k] = $v;
808
809
810
811
812
813
814
815
816
817
818
819
			}
		}
	}

	/**
	 * Processing of uploaded files.
	 * It turns out that some versions of PHP arranges submitted data for files different if sent in an array. This function will unify this so the internal array $this->uploadedFileArray will always contain files arranged in the same structure.
	 *
	 * @param array $postFiles $_FILES array
	 * @return void
	 */
	public function process_uploads($postFiles) {
820
821
822
823
824
825
826
827
		if (!is_array($postFiles)) {
			return;
		}

		// Editing frozen:
		if ($this->BE_USER->workspace !== 0 && $this->BE_USER->workspaceRec['freeze']) {
			if ($this->enableLogging) {
				$this->newlog('All editing in this workspace has been frozen!', 1);
828
			}
829
			return;
830
831
832
833
834
835
836
837
838
		}
		$subA = reset($postFiles);
		if (is_array($subA)) {
			if (is_array($subA['name']) && is_array($subA['type']) && is_array($subA['tmp_name']) && is_array($subA['size'])) {
				// Initialize the uploadedFilesArray:
				$this->uploadedFileArray = array();
				// For each entry:
				foreach ($subA as $key => $values) {
					$this->process_uploads_traverseArray($this->uploadedFileArray, $values, $key);
839
				}
840
841
			} else {
				$this->uploadedFileArray = $subA;
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
			}
		}
	}

	/**
	 * Traverse the upload array if needed to rearrange values.
	 *
	 * @param array $outputArr $this->uploadedFileArray passed by reference
	 * @param array $inputArr Input array  ($_FILES parts)
	 * @param string $keyToSet The current $_FILES array key to set on the outermost level.
	 * @return void
	 * @access private
	 * @see process_uploads()
	 */
	public function process_uploads_traverseArray(&$outputArr, $inputArr, $keyToSet) {
		if (is_array($inputArr)) {
			foreach ($inputArr as $key => $value) {
				$this->process_uploads_traverseArray($outputArr[$key], $inputArr[$key], $keyToSet);
			}
		} else {
			$outputArr[$keyToSet] = $inputArr;
		}
	}

	/*********************************************
	 *
	 * HOOKS
	 *
	 *********************************************/
	/**
	 * Hook: processDatamap_afterDatabaseOperations
	 * (calls $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);)
	 *
	 * Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
	 * but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
	 *
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
878
	 * @param array $hookObjectsArr (reference) Array with hook objects
879
	 * @param string $status (reference) Status of the current operation, 'new' or 'update
880
	 * @param string $table (reference) The table currently processing data for
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
	 * @param string $id (reference) The record uid currently processing data for, [integer] or [string] (like 'NEW...')
	 * @param array $fieldArray (reference) The field array of a record
	 * @return void
	 */
	public function hook_processDatamap_afterDatabaseOperations(&$hookObjectsArr, &$status, &$table, &$id, &$fieldArray) {
		// Process hook directly:
		if (!isset($this->remapStackRecords[$table][$id])) {
			foreach ($hookObjectsArr as $hookObj) {
				if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
					$hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);
				}
			}
		} else {
			$this->remapStackRecords[$table][$id]['processDatamap_afterDatabaseOperations'] = array(
				'status' => $status,
				'fieldArray' => $fieldArray,
				'hookObjectsArr' => $hookObjectsArr
			);
		}
	}

	/**
	 * Gets the 'checkModifyAccessList' hook objects.
	 * The first call initializes the accordant objects.
	 *
	 * @return array The 'checkModifyAccessList' hook objects (if any)
907
	 * @throws \UnexpectedValueException
908
909
910
911
912
913
	 */
	protected function getCheckModifyAccessListHookObjects() {
		if (!isset($this->checkModifyAccessListHookObjects)) {
			$this->checkModifyAccessListHookObjects = array();
			if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'])) {
				foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'] as $classData) {
914
					$hookObject = GeneralUtility::getUserObj($classData);
Sebastian Michaelsen's avatar
Sebastian Michaelsen committed
915
					if (!$hookObject instanceof DataHandlerCheckModifyAccessListHookInterface) {
Christian Kuhn's avatar
Christian Kuhn committed
916
						throw new \UnexpectedValueException('$hookObject must implement interface \\TYPO3\\CMS\\Core\\DataHandling\\DataHandlerCheckModifyAccessListHookInterface', 1251892472);
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
					}
					$this->checkModifyAccessListHookObjects[] = $hookObject;
				}
			}
		}
		return $this->checkModifyAccessListHookObjects;
	}

	/*********************************************
	 *
	 * PROCESSING DATA
	 *
	 *********************************************/
	/**
	 * Processing the data-array
	 * Call this function to process the data-array set by start()
	 *
934
	 * @return void|FALSE
935
936
	 */
	public function process_datamap() {
937
938
		$this->controlActiveElements();

939
940
941
942
943
944
		// Keep versionized(!) relations here locally:
		$registerDBList = array();
		$this->registerElementsToBeDeleted();
		$this->datamap = $this->unsetElementsToBeDeleted($this->datamap);
		// Editing frozen:
		if ($this->BE_USER->workspace !== 0 && $this->BE_USER->workspaceRec['freeze']) {
945
946
947
			if ($this->enableLogging) {
				$this->newlog('All editing in this workspace has been frozen!', 1);
			}
948
949
950
951
952
953
			return FALSE;
		}
		// First prepare user defined objects (if any) for hooks which extend this function:
		$hookObjectsArr = array();
		if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'])) {
			foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'] as $classRef) {
954
				$hookObject = GeneralUtility::getUserObj($classRef);
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
				if (method_exists($hookObject, 'processDatamap_beforeStart')) {
					$hookObject->processDatamap_beforeStart($this);
				}
				$hookObjectsArr[] = $hookObject;
			}
		}
		// Organize tables so that the pages-table is always processed first. This is required if you want to make sure that content pointing to a new page will be created.
		$orderOfTables = array();
		// Set pages first.
		if (isset($this->datamap['pages'])) {
			$orderOfTables[] = 'pages';
		}
		$orderOfTables = array_unique(array_merge($orderOfTables, array_keys($this->datamap)));
		// Process the tables...
		foreach ($orderOfTables as $table) {
			// Check if
			//	   - table is set in $GLOBALS['TCA'],
			//	   - table is NOT readOnly
			//	   - the table is set with content in the data-array (if not, there's nothing to process...)
			//	   - permissions for tableaccess OK
			$modifyAccessList = $this->checkModifyAccessList($table);
976
977
			if ($this->enableLogging && !$modifyAccessList) {
				$this->log($table, 0, 2, 0, 1, 'Attempt to modify table \'%s\' without permission', 1, array($table));
978
			}
979
980
981
982
983
984
985
986
987
988
989
990
991
			if (!isset($GLOBALS['TCA'][$table]) || $this->tableReadOnly($table) || !is_array($this->datamap[$table]) || !$modifyAccessList) {
				continue;
			}

			if ($this->reverseOrder) {
				$this->datamap[$table] = array_reverse($this->datamap[$table], 1);
			}
			// For each record from the table, do:
			// $id is the record uid, may be a string if new records...
			// $incomingFieldArray is the array of fields
			foreach ($this->datamap[$table] as $id => $incomingFieldArray) {
				if (!is_array($incomingFieldArray)) {
					continue;
992
				}
993
				$theRealPid = NULL;
994
995
996
997
998
999
1000

				// Handle native date/time fields
				$dateTimeFormats = $this->databaseConnection->getDateTimeFormats($table);
				foreach ($GLOBALS['TCA'][$table]['columns'] as $column => $config) {
					if (isset($incomingFieldArray[$column])) {
						if (isset($config['config']['dbType']) && ($config['config']['dbType'] === 'date' || $config['config']['dbType'] === 'datetime')) {
							$emptyValue = $dateTimeFormats[$config['config']['dbType']]['empty'];