[TASK] Rename $sortRow to $sortColumn in DataHandler
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / DataHandling / DataHandler.php
1 <?php
2 namespace TYPO3\CMS\Core\DataHandling;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Doctrine\DBAL\DBALException;
18 use Doctrine\DBAL\Driver\Statement;
19 use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
20 use Doctrine\DBAL\Platforms\SqlitePlatform;
21 use Doctrine\DBAL\Platforms\SQLServerPlatform;
22 use Doctrine\DBAL\Types\IntegerType;
23 use Psr\Log\LoggerAwareInterface;
24 use Psr\Log\LoggerAwareTrait;
25 use TYPO3\CMS\Backend\Utility\BackendUtility;
26 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
27 use TYPO3\CMS\Core\Cache\CacheManager;
28 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
29 use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
30 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidIdentifierException;
31 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowException;
32 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowLoopException;
33 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowRootException;
34 use TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidPointerFieldValueException;
35 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
36 use TYPO3\CMS\Core\Configuration\Richtext;
37 use TYPO3\CMS\Core\Database\Connection;
38 use TYPO3\CMS\Core\Database\ConnectionPool;
39 use TYPO3\CMS\Core\Database\Query\QueryHelper;
40 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
41 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
42 use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
43 use TYPO3\CMS\Core\Database\ReferenceIndex;
44 use TYPO3\CMS\Core\Database\RelationHandler;
45 use TYPO3\CMS\Core\DataHandling\Localization\DataMapProcessor;
46 use TYPO3\CMS\Core\History\RecordHistoryStore;
47 use TYPO3\CMS\Core\Html\RteHtmlParser;
48 use TYPO3\CMS\Core\Localization\LanguageService;
49 use TYPO3\CMS\Core\Messaging\FlashMessage;
50 use TYPO3\CMS\Core\Messaging\FlashMessageService;
51 use TYPO3\CMS\Core\Resource\ResourceFactory;
52 use TYPO3\CMS\Core\Service\OpcodeCacheService;
53 use TYPO3\CMS\Core\Type\Bitmask\Permission;
54 use TYPO3\CMS\Core\Utility\ArrayUtility;
55 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
56 use TYPO3\CMS\Core\Utility\File\BasicFileUtility;
57 use TYPO3\CMS\Core\Utility\GeneralUtility;
58 use TYPO3\CMS\Core\Utility\MathUtility;
59 use TYPO3\CMS\Core\Utility\PathUtility;
60 use TYPO3\CMS\Core\Utility\StringUtility;
61 use TYPO3\CMS\Core\Versioning\VersionState;
62
63 /**
64 * The main data handler class which takes care of correctly updating and inserting records.
65 * This class was formerly known as TCEmain.
66 *
67 * This is the TYPO3 Core Engine class for manipulation of the database
68 * This class is used by eg. the tce_db.php script which provides an the interface for POST forms to this class.
69 *
70 * Dependencies:
71 * - $GLOBALS['TCA'] must exist
72 * - $GLOBALS['LANG'] must exist
73 *
74 * tce_db.php for further comments and SYNTAX! Also see document 'TYPO3 Core API' for details.
75 */
76 class DataHandler implements LoggerAwareInterface
77 {
78 use LoggerAwareTrait;
79 use PublicPropertyDeprecationTrait;
80
81 /**
82 * @var array
83 */
84 protected $deprecatedPublicProperties = [
85 'updateModeL10NdiffData' => 'Using updateModeL10NdiffData is deprecated and will not be possible anymore in TYPO3 v10.',
86 'updateModeL10NdiffDataClear' => 'Using updateModeL10NdiffDataClear is deprecated and will not be possible anymore in TYPO3 v10.',
87 ];
88
89 // *********************
90 // Public variables you can configure before using the class:
91 // *********************
92 /**
93 * If TRUE, the default log-messages will be stored. This should not be necessary if the locallang-file for the
94 * log-display is properly configured. So disabling this will just save some database-space as the default messages are not saved.
95 *
96 * @var bool
97 */
98 public $storeLogMessages = true;
99
100 /**
101 * If TRUE, actions are logged to sys_log.
102 *
103 * @var bool
104 */
105 public $enableLogging = true;
106
107 /**
108 * If TRUE, the datamap array is reversed in the order, which is a nice thing if you're creating a whole new
109 * bunch of records.
110 *
111 * @var bool
112 */
113 public $reverseOrder = false;
114
115 /**
116 * If TRUE, only fields which are different from the database values are saved! In fact, if a whole input array
117 * is similar, it's not saved then.
118 *
119 * @var bool
120 */
121 public $checkSimilar = true;
122
123 /**
124 * This will read the record after having updated or inserted it. If anything is not properly submitted an error
125 * is written to the log. This feature consumes extra time by selecting records
126 *
127 * @var bool
128 */
129 public $checkStoredRecords = true;
130
131 /**
132 * If set, values '' and 0 will equal each other when the stored records are checked.
133 *
134 * @var bool
135 */
136 public $checkStoredRecords_loose = true;
137
138 /**
139 * If this is set, then a page is deleted by deleting the whole branch under it (user must have
140 * delete permissions to it all). If not set, then the page is deleted ONLY if it has no branch.
141 *
142 * @var bool
143 */
144 public $deleteTree = false;
145
146 /**
147 * If set, then the 'hideAtCopy' flag for tables will be ignored.
148 *
149 * @var bool
150 */
151 public $neverHideAtCopy = false;
152
153 /**
154 * If set, then the TCE class has been instantiated during an import action of a T3D
155 *
156 * @var bool
157 */
158 public $isImporting = false;
159
160 /**
161 * If set, then transformations are NOT performed on the input.
162 *
163 * @var bool
164 */
165 public $dontProcessTransformations = false;
166
167 /**
168 * Will distinguish between translations (with parent) and localizations (without parent) while still using the same methods to copy the records
169 * TRUE: translation of a record connected to the default language
170 * FALSE: localization of a record without connection to the default language
171 *
172 * @var bool
173 */
174 protected $useTransOrigPointerField = true;
175
176 /**
177 * TRUE: (traditional) Updates when record is saved. For flexforms, updates if change is made to the localized value.
178 * FALSE: Will not update anything.
179 * "FORCE_FFUPD" (string): Like TRUE, but will force update to the FlexForm Field
180 *
181 * @var bool|string
182 */
183 protected $updateModeL10NdiffData = true;
184
185 /**
186 * If TRUE, the translation diff. fields will in fact be reset so that they indicate that all needs to change again!
187 * It's meant as the opposite of declaring the record translated.
188 *
189 * @var bool
190 */
191 protected $updateModeL10NdiffDataClear = false;
192
193 /**
194 * If TRUE, workspace restrictions are bypassed on edit an create actions (process_datamap()).
195 * YOU MUST KNOW what you do if you use this feature!
196 *
197 * @var bool
198 */
199 public $bypassWorkspaceRestrictions = false;
200
201 /**
202 * If TRUE, file handling of attached files (addition, deletion etc) is bypassed - the value is saved straight away.
203 * YOU MUST KNOW what you are doing with this feature!
204 *
205 * @var bool
206 */
207 public $bypassFileHandling = false;
208
209 /**
210 * If TRUE, access check, check for deleted etc. for records is bypassed.
211 * YOU MUST KNOW what you are doing if you use this feature!
212 *
213 * @var bool
214 */
215 public $bypassAccessCheckForRecords = false;
216
217 /**
218 * Comma-separated list. This list of tables decides which tables will be copied. If empty then none will.
219 * If '*' then all will (that the user has permission to of course)
220 *
221 * @var string
222 */
223 public $copyWhichTables = '*';
224
225 /**
226 * If 0 then branch is NOT copied.
227 * If 1 then pages on the 1st level is copied.
228 * If 2 then pages on the second level is copied ... and so on
229 *
230 * @var int
231 */
232 public $copyTree = 0;
233
234 /**
235 * [table][fields]=value: New records are created with default values and you can set this array on the
236 * form $defaultValues[$table][$field] = $value to override the default values fetched from TCA.
237 * If ->setDefaultsFromUserTS is called UserTSconfig default values will overrule existing values in this array
238 * (thus UserTSconfig overrules externally set defaults which overrules TCA defaults)
239 *
240 * @var array
241 */
242 public $defaultValues = [];
243
244 /**
245 * [table][fields]=value: You can set this array on the form $overrideValues[$table][$field] = $value to
246 * override the incoming data. You must set this externally. You must make sure the fields in this array are also
247 * found in the table, because it's not checked. All columns can be set by this array!
248 *
249 * @var array
250 */
251 public $overrideValues = [];
252
253 /**
254 * [filename]=alternative_filename: Use this array to force another name onto a file.
255 * Eg. if you set ['/tmp/blablabal'] = 'my_file.txt' and '/tmp/blablabal' is set for a certain file-field,
256 * then 'my_file.txt' will be used as the name instead.
257 *
258 * @var array
259 */
260 public $alternativeFileName = [];
261
262 /**
263 * Array [filename]=alternative_filepath: Same as alternativeFileName but with relative path to the file
264 *
265 * @var array
266 */
267 public $alternativeFilePath = [];
268
269 /**
270 * If entries are set in this array corresponding to fields for update, they are ignored and thus NOT updated.
271 * You could set this array from a series of checkboxes with value=0 and hidden fields before the checkbox with 1.
272 * Then an empty checkbox will disable the field.
273 *
274 * @var array
275 */
276 public $data_disableFields = [];
277
278 /**
279 * Use this array to validate suggested uids for tables by setting [table]:[uid]. This is a dangerous option
280 * since it will force the inserted record to have a certain UID. The value just have to be TRUE, but if you set
281 * it to "DELETE" it will make sure any record with that UID will be deleted first (raw delete).
282 * The option is used for import of T3D files when synchronizing between two mirrored servers.
283 * As a security measure this feature is available only for Admin Users (for now)
284 *
285 * @var array
286 */
287 public $suggestedInsertUids = [];
288
289 /**
290 * Object. Call back object for FlexForm traversal. Useful when external classes wants to use the
291 * iteration functions inside DataHandler for traversing a FlexForm structure.
292 *
293 * @var object
294 */
295 public $callBackObj;
296
297 // *********************
298 // Internal variables (mapping arrays) which can be used (read-only) from outside
299 // *********************
300 /**
301 * Contains mapping of auto-versionized records.
302 *
303 * @var array
304 */
305 public $autoVersionIdMap = [];
306
307 /**
308 * 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
309 *
310 * @var array
311 */
312 public $substNEWwithIDs = [];
313
314 /**
315 * Like $substNEWwithIDs, but where each old "NEW..." id is mapped to the table it was from.
316 *
317 * @var array
318 */
319 public $substNEWwithIDs_table = [];
320
321 /**
322 * Holds the tables and there the ids of newly created child records from IRRE
323 *
324 * @var array
325 */
326 public $newRelatedIDs = [];
327
328 /**
329 * This array is the sum of all copying operations in this class. May be READ from outside, thus partly public.
330 *
331 * @var array
332 */
333 public $copyMappingArray_merged = [];
334
335 /**
336 * Per-table array with UIDs that have been deleted.
337 *
338 * @var array
339 */
340 protected $deletedRecords = [];
341
342 /**
343 * A map between input file name and final destination for files being attached to records.
344 *
345 * @var array
346 */
347 public $copiedFileMap = [];
348
349 /**
350 * Contains [table][id][field] of fiels where RTEmagic images was copied. Holds old filename as key and new filename as value.
351 *
352 * @var array
353 */
354 public $RTEmagic_copyIndex = [];
355
356 /**
357 * Errors are collected in this variable.
358 *
359 * @var array
360 */
361 public $errorLog = [];
362
363 /**
364 * Fields from the pages-table for which changes will trigger a pagetree refresh
365 *
366 * @var array
367 */
368 public $pagetreeRefreshFieldsFromPages = ['pid', 'sorting', 'deleted', 'hidden', 'title', 'doktype', 'is_siteroot', 'fe_group', 'nav_hide', 'nav_title', 'module', 'starttime', 'endtime', 'content_from_pid', 'extendToSubpages'];
369
370 /**
371 * Indicates whether the pagetree needs a refresh because of important changes
372 *
373 * @var bool
374 */
375 public $pagetreeNeedsRefresh = false;
376
377 // *********************
378 // Internal Variables, do not touch.
379 // *********************
380
381 // Variables set in init() function:
382
383 /**
384 * The user-object the script uses. If not set from outside, this is set to the current global $BE_USER.
385 *
386 * @var BackendUserAuthentication
387 */
388 public $BE_USER;
389
390 /**
391 * Will be set to uid of be_user executing this script
392 *
393 * @var int
394 */
395 public $userid;
396
397 /**
398 * Will be set to username of be_user executing this script
399 *
400 * @var string
401 */
402 public $username;
403
404 /**
405 * Will be set if user is admin
406 *
407 * @var bool
408 */
409 public $admin;
410
411 /**
412 * Can be overridden from $GLOBALS['TYPO3_CONF_VARS']
413 *
414 * @var array
415 */
416 public $defaultPermissions = [
417 'user' => 'show,edit,delete,new,editcontent',
418 'group' => 'show,edit,new,editcontent',
419 'everybody' => ''
420 ];
421
422 /**
423 * 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.
424 *
425 * @var array
426 */
427 protected $excludedTablesAndFields = [];
428
429 /**
430 * Data submitted from the form view, used to control behaviours,
431 * e.g. this is used to activate/deactivate fields and thus store NULL values
432 *
433 * @var array
434 */
435 protected $control = [];
436
437 /**
438 * Set with incoming data array
439 *
440 * @var array
441 */
442 public $datamap = [];
443
444 /**
445 * Set with incoming cmd array
446 *
447 * @var array
448 */
449 public $cmdmap = [];
450
451 /**
452 * List of changed old record ids to new records ids
453 *
454 * @var array
455 */
456 protected $mmHistoryRecords = [];
457
458 /**
459 * List of changed old record ids to new records ids
460 *
461 * @var array
462 */
463 protected $historyRecords = [];
464
465 // Internal static:
466 /**
467 * Permission mapping
468 *
469 * @var array
470 */
471 public $pMap = [
472 'show' => 1,
473 // 1st bit
474 'edit' => 2,
475 // 2nd bit
476 'delete' => 4,
477 // 3rd bit
478 'new' => 8,
479 // 4th bit
480 'editcontent' => 16
481 ];
482
483 /**
484 * Integer: The interval between sorting numbers used with tables with a 'sorting' field defined. Min 1
485 *
486 * @var int
487 */
488 public $sortIntervals = 256;
489
490 // Internal caching arrays
491 /**
492 * Used by function checkRecordUpdateAccess() to store whether a record is updatable or not.
493 *
494 * @var array
495 */
496 protected $recUpdateAccessCache = [];
497
498 /**
499 * User by function checkRecordInsertAccess() to store whether a record can be inserted on a page id
500 *
501 * @var array
502 */
503 protected $recInsertAccessCache = [];
504
505 /**
506 * Caching array for check of whether records are in a webmount
507 *
508 * @var array
509 */
510 protected $isRecordInWebMount_Cache = [];
511
512 /**
513 * Caching array for page ids in webmounts
514 *
515 * @var array
516 */
517 protected $isInWebMount_Cache = [];
518
519 /**
520 * Used for caching page records in pageInfo()
521 *
522 * @var array
523 */
524 protected $pageCache = [];
525
526 // Other arrays:
527 /**
528 * For accumulation of MM relations that must be written after new records are created.
529 *
530 * @var array
531 */
532 public $dbAnalysisStore = [];
533
534 /**
535 * For accumulation of files which must be deleted after processing of all input content
536 *
537 * @var array
538 */
539 public $removeFilesStore = [];
540
541 /**
542 * Uploaded files, set by process_uploads()
543 *
544 * @var array
545 */
546 public $uploadedFileArray = [];
547
548 /**
549 * Used for tracking references that might need correction after operations
550 *
551 * @var array
552 */
553 public $registerDBList = [];
554
555 /**
556 * Used for tracking references that might need correction in pid field after operations (e.g. IRRE)
557 *
558 * @var array
559 */
560 public $registerDBPids = [];
561
562 /**
563 * Used by the copy action to track the ids of new pages so subpages are correctly inserted!
564 * THIS is internally cleared for each executed copy operation! DO NOT USE THIS FROM OUTSIDE!
565 * Read from copyMappingArray_merged instead which is accumulating this information.
566 *
567 * NOTE: This is used by some outside scripts (e.g. hooks), as the results in $copyMappingArray_merged
568 * are only available after an action has been completed.
569 *
570 * @var array
571 */
572 public $copyMappingArray = [];
573
574 /**
575 * Array used for remapping uids and values at the end of process_datamap
576 *
577 * @var array
578 */
579 public $remapStack = [];
580
581 /**
582 * Array used for remapping uids and values at the end of process_datamap
583 * (e.g. $remapStackRecords[<table>][<uid>] = <index in $remapStack>)
584 *
585 * @var array
586 */
587 public $remapStackRecords = [];
588
589 /**
590 * Array used for checking whether new children need to be remapped
591 *
592 * @var array
593 */
594 protected $remapStackChildIds = [];
595
596 /**
597 * Array used for executing addition actions after remapping happened (set processRemapStack())
598 *
599 * @var array
600 */
601 protected $remapStackActions = [];
602
603 /**
604 * Array used for executing post-processing on the reference index
605 *
606 * @var array
607 */
608 protected $remapStackRefIndex = [];
609
610 /**
611 * Array used for additional calls to $this->updateRefIndex
612 *
613 * @var array
614 */
615 public $updateRefIndexStack = [];
616
617 /**
618 * Tells, that this DataHandler instance was called from \TYPO3\CMS\Impext\ImportExport.
619 * This variable is set by \TYPO3\CMS\Impext\ImportExport
620 *
621 * @var array
622 */
623 public $callFromImpExp = false;
624
625 // Various
626 /**
627 * basicFileFunctions object
628 * For "singleton" file-manipulation object
629 *
630 * @var BasicFileUtility
631 */
632 public $fileFunc;
633
634 /**
635 * Set to "currentRecord" during checking of values.
636 *
637 * @var array
638 */
639 public $checkValue_currentRecord = [];
640
641 /**
642 * A signal flag used to tell file processing that auto versioning has happened and hence certain action should be applied.
643 *
644 * @var bool
645 */
646 public $autoVersioningUpdate = false;
647
648 /**
649 * Disable delete clause
650 *
651 * @var bool
652 */
653 protected $disableDeleteClause = false;
654
655 /**
656 * @var array
657 */
658 protected $checkModifyAccessListHookObjects;
659
660 /**
661 * @var array
662 */
663 protected $version_remapMMForVersionSwap_reg;
664
665 /**
666 * The outer most instance of \TYPO3\CMS\Core\DataHandling\DataHandler:
667 * This object instantiates itself on versioning and localization ...
668 *
669 * @var \TYPO3\CMS\Core\DataHandling\DataHandler
670 */
671 protected $outerMostInstance = null;
672
673 /**
674 * Internal cache for collecting records that should trigger cache clearing
675 *
676 * @var array
677 */
678 protected static $recordsToClearCacheFor = [];
679
680 /**
681 * Internal cache for pids of records which were deleted. It's not possible
682 * to retrieve the parent folder/page at a later stage
683 *
684 * @var array
685 */
686 protected static $recordPidsForDeletedRecords = [];
687
688 /**
689 * Runtime Cache to store and retrieve data computed for a single request
690 *
691 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
692 */
693 protected $runtimeCache = null;
694
695 /**
696 * Prefix for the cache entries of nested element calls since the runtimeCache has a global scope.
697 *
698 * @var string
699 */
700 protected $cachePrefixNestedElementCalls = 'core-datahandler-nestedElementCalls-';
701
702 /**
703 * Sets up the data handler cache and some additional options, the main logic is done in the start() method.
704 */
705 public function __construct()
706 {
707 $this->checkStoredRecords = (bool)$GLOBALS['TYPO3_CONF_VARS']['BE']['checkStoredRecords'];
708 $this->checkStoredRecords_loose = (bool)$GLOBALS['TYPO3_CONF_VARS']['BE']['checkStoredRecordsLoose'];
709 $this->runtimeCache = $this->getRuntimeCache();
710 }
711
712 /**
713 * @param array $control
714 */
715 public function setControl(array $control)
716 {
717 $this->control = $control;
718 }
719
720 /**
721 * Initializing.
722 * For details, see 'TYPO3 Core API' document.
723 * This function does not start the processing of data, but merely initializes the object
724 *
725 * @param array $data Data to be modified or inserted in the database
726 * @param array $cmd Commands to copy, move, delete, localize, versionize records.
727 * @param BackendUserAuthentication|null $altUserObject An alternative userobject you can set instead of the default, which is $GLOBALS['BE_USER']
728 */
729 public function start($data, $cmd, $altUserObject = null)
730 {
731 // Initializing BE_USER
732 $this->BE_USER = is_object($altUserObject) ? $altUserObject : $GLOBALS['BE_USER'];
733 $this->userid = $this->BE_USER->user['uid'];
734 $this->username = $this->BE_USER->user['username'];
735 $this->admin = $this->BE_USER->user['admin'];
736 if ($this->BE_USER->uc['recursiveDelete']) {
737 $this->deleteTree = 1;
738 }
739 // Initializing default permissions for pages
740 $defaultPermissions = $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultPermissions'];
741 if (isset($defaultPermissions['user'])) {
742 $this->defaultPermissions['user'] = $defaultPermissions['user'];
743 }
744 if (isset($defaultPermissions['group'])) {
745 $this->defaultPermissions['group'] = $defaultPermissions['group'];
746 }
747 if (isset($defaultPermissions['everybody'])) {
748 $this->defaultPermissions['everybody'] = $defaultPermissions['everybody'];
749 }
750 // generates the excludelist, based on TCA/exclude-flag and non_exclude_fields for the user:
751 if (!$this->admin) {
752 $this->excludedTablesAndFields = array_flip($this->getExcludeListArray());
753 }
754 // Setting the data and cmd arrays
755 if (is_array($data)) {
756 reset($data);
757 $this->datamap = $data;
758 }
759 if (is_array($cmd)) {
760 reset($cmd);
761 $this->cmdmap = $cmd;
762 }
763 }
764
765 /**
766 * Function that can mirror input values in datamap-array to other uid numbers.
767 * 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]
768 *
769 * @param array $mirror This array has the syntax $mirror[table_name][uid] = [list of uids to copy data-value TO!]
770 */
771 public function setMirror($mirror)
772 {
773 if (!is_array($mirror)) {
774 return;
775 }
776
777 foreach ($mirror as $table => $uid_array) {
778 if (!isset($this->datamap[$table])) {
779 continue;
780 }
781
782 foreach ($uid_array as $id => $uidList) {
783 if (!isset($this->datamap[$table][$id])) {
784 continue;
785 }
786
787 $theIdsInArray = GeneralUtility::trimExplode(',', $uidList, true);
788 foreach ($theIdsInArray as $copyToUid) {
789 $this->datamap[$table][$copyToUid] = $this->datamap[$table][$id];
790 }
791 }
792 }
793 }
794
795 /**
796 * Initializes default values coming from User TSconfig
797 *
798 * @param array $userTS User TSconfig array
799 */
800 public function setDefaultsFromUserTS($userTS)
801 {
802 if (!is_array($userTS)) {
803 return;
804 }
805
806 foreach ($userTS as $k => $v) {
807 $k = mb_substr($k, 0, -1);
808 if (!$k || !is_array($v) || !isset($GLOBALS['TCA'][$k])) {
809 continue;
810 }
811
812 if (is_array($this->defaultValues[$k])) {
813 $this->defaultValues[$k] = array_merge($this->defaultValues[$k], $v);
814 } else {
815 $this->defaultValues[$k] = $v;
816 }
817 }
818 }
819
820 /**
821 * Processing of uploaded files.
822 * 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.
823 *
824 * @param array $postFiles $_FILES array
825 */
826 public function process_uploads($postFiles)
827 {
828 if (!is_array($postFiles)) {
829 return;
830 }
831
832 // Editing frozen:
833 if ($this->BE_USER->workspace !== 0 && $this->BE_USER->workspaceRec['freeze']) {
834 $this->newlog('All editing in this workspace has been frozen!', 1);
835 return;
836 }
837 $subA = reset($postFiles);
838 if (is_array($subA)) {
839 if (is_array($subA['name']) && is_array($subA['type']) && is_array($subA['tmp_name']) && is_array($subA['size'])) {
840 // Initialize the uploadedFilesArray:
841 $this->uploadedFileArray = [];
842 // For each entry:
843 foreach ($subA as $key => $values) {
844 $this->process_uploads_traverseArray($this->uploadedFileArray, $values, $key);
845 }
846 } else {
847 $this->uploadedFileArray = $subA;
848 }
849 }
850 }
851
852 /**
853 * Traverse the upload array if needed to rearrange values.
854 *
855 * @param array $outputArr $this->uploadedFileArray passed by reference
856 * @param array $inputArr Input array ($_FILES parts)
857 * @param string $keyToSet The current $_FILES array key to set on the outermost level.
858 * @access private
859 * @see process_uploads()
860 */
861 public function process_uploads_traverseArray(&$outputArr, $inputArr, $keyToSet)
862 {
863 if (is_array($inputArr)) {
864 foreach ($inputArr as $key => $value) {
865 $this->process_uploads_traverseArray($outputArr[$key], $inputArr[$key], $keyToSet);
866 }
867 } else {
868 $outputArr[$keyToSet] = $inputArr;
869 }
870 }
871
872 /*********************************************
873 *
874 * HOOKS
875 *
876 *********************************************/
877 /**
878 * Hook: processDatamap_afterDatabaseOperations
879 * (calls $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);)
880 *
881 * Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
882 * but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
883 *
884 * @param array $hookObjectsArr (reference) Array with hook objects
885 * @param string $status (reference) Status of the current operation, 'new' or 'update
886 * @param string $table (reference) The table currently processing data for
887 * @param string $id (reference) The record uid currently processing data for, [integer] or [string] (like 'NEW...')
888 * @param array $fieldArray (reference) The field array of a record
889 */
890 public function hook_processDatamap_afterDatabaseOperations(&$hookObjectsArr, &$status, &$table, &$id, &$fieldArray)
891 {
892 // Process hook directly:
893 if (!isset($this->remapStackRecords[$table][$id])) {
894 foreach ($hookObjectsArr as $hookObj) {
895 if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
896 $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);
897 }
898 }
899 } else {
900 $this->remapStackRecords[$table][$id]['processDatamap_afterDatabaseOperations'] = [
901 'status' => $status,
902 'fieldArray' => $fieldArray,
903 'hookObjectsArr' => $hookObjectsArr
904 ];
905 }
906 }
907
908 /**
909 * Gets the 'checkModifyAccessList' hook objects.
910 * The first call initializes the accordant objects.
911 *
912 * @return array The 'checkModifyAccessList' hook objects (if any)
913 * @throws \UnexpectedValueException
914 */
915 protected function getCheckModifyAccessListHookObjects()
916 {
917 if (!isset($this->checkModifyAccessListHookObjects)) {
918 $this->checkModifyAccessListHookObjects = [];
919 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'] ?? [] as $className) {
920 $hookObject = GeneralUtility::makeInstance($className);
921 if (!$hookObject instanceof DataHandlerCheckModifyAccessListHookInterface) {
922 throw new \UnexpectedValueException($className . ' must implement interface ' . DataHandlerCheckModifyAccessListHookInterface::class, 1251892472);
923 }
924 $this->checkModifyAccessListHookObjects[] = $hookObject;
925 }
926 }
927 return $this->checkModifyAccessListHookObjects;
928 }
929
930 /*********************************************
931 *
932 * PROCESSING DATA
933 *
934 *********************************************/
935 /**
936 * Processing the data-array
937 * Call this function to process the data-array set by start()
938 *
939 * @return bool|void
940 */
941 public function process_datamap()
942 {
943 $this->controlActiveElements();
944
945 // Keep versionized(!) relations here locally:
946 $registerDBList = [];
947 $this->registerElementsToBeDeleted();
948 $this->datamap = $this->unsetElementsToBeDeleted($this->datamap);
949 // Editing frozen:
950 if ($this->BE_USER->workspace !== 0 && $this->BE_USER->workspaceRec['freeze']) {
951 $this->newlog('All editing in this workspace has been frozen!', 1);
952 return false;
953 }
954 // First prepare user defined objects (if any) for hooks which extend this function:
955 $hookObjectsArr = [];
956 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'] ?? [] as $className) {
957 $hookObject = GeneralUtility::makeInstance($className);
958 if (method_exists($hookObject, 'processDatamap_beforeStart')) {
959 $hookObject->processDatamap_beforeStart($this);
960 }
961 $hookObjectsArr[] = $hookObject;
962 }
963 // Pre-process data-map and synchronize localization states
964 $this->datamap = DataMapProcessor::instance($this->datamap, $this->BE_USER)->process();
965 // 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.
966 $orderOfTables = [];
967 // Set pages first.
968 if (isset($this->datamap['pages'])) {
969 $orderOfTables[] = 'pages';
970 }
971 $orderOfTables = array_unique(array_merge($orderOfTables, array_keys($this->datamap)));
972 // Process the tables...
973 foreach ($orderOfTables as $table) {
974 // Check if
975 // - table is set in $GLOBALS['TCA'],
976 // - table is NOT readOnly
977 // - the table is set with content in the data-array (if not, there's nothing to process...)
978 // - permissions for tableaccess OK
979 $modifyAccessList = $this->checkModifyAccessList($table);
980 if (!$modifyAccessList) {
981 $this->log($table, 0, 2, 0, 1, 'Attempt to modify table \'%s\' without permission', 1, [$table]);
982 }
983 if (!isset($GLOBALS['TCA'][$table]) || $this->tableReadOnly($table) || !is_array($this->datamap[$table]) || !$modifyAccessList) {
984 continue;
985 }
986
987 if ($this->reverseOrder) {
988 $this->datamap[$table] = array_reverse($this->datamap[$table], 1);
989 }
990 // For each record from the table, do:
991 // $id is the record uid, may be a string if new records...
992 // $incomingFieldArray is the array of fields
993 foreach ($this->datamap[$table] as $id => $incomingFieldArray) {
994 if (!is_array($incomingFieldArray)) {
995 continue;
996 }
997 $theRealPid = null;
998
999 // Hook: processDatamap_preProcessFieldArray
1000 foreach ($hookObjectsArr as $hookObj) {
1001 if (method_exists($hookObj, 'processDatamap_preProcessFieldArray')) {
1002 $hookObj->processDatamap_preProcessFieldArray($incomingFieldArray, $table, $id, $this);
1003 }
1004 }
1005 // ******************************
1006 // Checking access to the record
1007 // ******************************
1008 $createNewVersion = false;
1009 $recordAccess = false;
1010 $old_pid_value = '';
1011 $this->autoVersioningUpdate = false;
1012 // Is it a new record? (Then Id is a string)
1013 if (!MathUtility::canBeInterpretedAsInteger($id)) {
1014 // Get a fieldArray with default values
1015 $fieldArray = $this->newFieldArray($table);
1016 // A pid must be set for new records.
1017 if (isset($incomingFieldArray['pid'])) {
1018 $pid_value = $incomingFieldArray['pid'];
1019 // Checking and finding numerical pid, it may be a string-reference to another value
1020 $canProceed = true;
1021 // If a NEW... id
1022 if (strpos($pid_value, 'NEW') !== false) {
1023 if ($pid_value[0] === '-') {
1024 $negFlag = -1;
1025 $pid_value = substr($pid_value, 1);
1026 } else {
1027 $negFlag = 1;
1028 }
1029 // Trying to find the correct numerical value as it should be mapped by earlier processing of another new record.
1030 if (isset($this->substNEWwithIDs[$pid_value])) {
1031 if ($negFlag === 1) {
1032 $old_pid_value = $this->substNEWwithIDs[$pid_value];
1033 }
1034 $pid_value = (int)($negFlag * $this->substNEWwithIDs[$pid_value]);
1035 } else {
1036 $canProceed = false;
1037 }
1038 }
1039 $pid_value = (int)$pid_value;
1040 if ($canProceed) {
1041 $fieldArray = $this->resolveSortingAndPidForNewRecord($table, $pid_value, $fieldArray);
1042 }
1043 }
1044 $theRealPid = $fieldArray['pid'];
1045 // Now, check if we may insert records on this pid.
1046 if ($theRealPid >= 0) {
1047 // Checks if records can be inserted on this $pid.
1048 // If this is a page translation, the check needs to be done for the l10n_parent record
1049 if ($table === 'pages' && $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0 && $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] > 0) {
1050 $recordAccess = $this->checkRecordInsertAccess($table, $incomingFieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']]);
1051 } else {
1052 $recordAccess = $this->checkRecordInsertAccess($table, $theRealPid);
1053 }
1054 if ($recordAccess) {
1055 $this->addDefaultPermittedLanguageIfNotSet($table, $incomingFieldArray);
1056 $recordAccess = $this->BE_USER->recordEditAccessInternals($table, $incomingFieldArray, true);
1057 if (!$recordAccess) {
1058 $this->newlog('recordEditAccessInternals() check failed. [' . $this->BE_USER->errorMsg . ']', 1);
1059 } elseif (!$this->bypassWorkspaceRestrictions) {
1060 // Workspace related processing:
1061 // If LIVE records cannot be created in the current PID due to workspace restrictions, prepare creation of placeholder-record
1062 if ($res = $this->BE_USER->workspaceAllowLiveRecordsInPID($theRealPid, $table)) {
1063 if ($res < 0) {
1064 $recordAccess = false;
1065 $this->newlog('Stage for versioning root point and users access level did not allow for editing', 1);
1066 }
1067 } else {
1068 // So, if no live records were allowed, we have to create a new version of this record:
1069 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
1070 $createNewVersion = true;
1071 } else {
1072 $recordAccess = false;
1073 $this->newlog('Record could not be created in this workspace in this branch', 1);
1074 }
1075 }
1076 }
1077 }
1078 } else {
1079 $this->logger->debug('Internal ERROR: pid should not be less than zero!');
1080 }
1081 // Yes new record, change $record_status to 'insert'
1082 $status = 'new';
1083 } else {
1084 // Nope... $id is a number
1085 $fieldArray = [];
1086 $recordAccess = $this->checkRecordUpdateAccess($table, $id, $incomingFieldArray, $hookObjectsArr);
1087 if (!$recordAccess) {
1088 if ($this->enableLogging) {
1089 $propArr = $this->getRecordProperties($table, $id);
1090 $this->log($table, $id, 2, 0, 1, 'Attempt to modify record \'%s\' (%s) without permission. Or non-existing page.', 2, [$propArr['header'], $table . ':' . $id], $propArr['event_pid']);
1091 }
1092 continue;
1093 }
1094 // Next check of the record permissions (internals)
1095 $recordAccess = $this->BE_USER->recordEditAccessInternals($table, $id);
1096 if (!$recordAccess) {
1097 $this->newlog('recordEditAccessInternals() check failed. [' . $this->BE_USER->errorMsg . ']', 1);
1098 } else {
1099 // Here we fetch the PID of the record that we point to...
1100 $tempdata = $this->recordInfo($table, $id, 'pid' . ($GLOBALS['TCA'][$table]['ctrl']['versioningWS'] ? ',t3ver_wsid,t3ver_stage' : ''));
1101 $theRealPid = $tempdata['pid'];
1102 // Use the new id of the versionized record we're trying to write to:
1103 // (This record is a child record of a parent and has already been versionized.)
1104 if ($this->autoVersionIdMap[$table][$id]) {
1105 // For the reason that creating a new version of this record, automatically
1106 // created related child records (e.g. "IRRE"), update the accordant field:
1107 $this->getVersionizedIncomingFieldArray($table, $id, $incomingFieldArray, $registerDBList);
1108 // Use the new id of the copied/versionized record:
1109 $id = $this->autoVersionIdMap[$table][$id];
1110 $recordAccess = true;
1111 $this->autoVersioningUpdate = true;
1112 } elseif (!$this->bypassWorkspaceRestrictions && ($errorCode = $this->BE_USER->workspaceCannotEditRecord($table, $tempdata))) {
1113 $recordAccess = false;
1114 // Versioning is required and it must be offline version!
1115 // Check if there already is a workspace version
1116 $WSversion = BackendUtility::getWorkspaceVersionOfRecord($this->BE_USER->workspace, $table, $id, 'uid,t3ver_oid');
1117 if ($WSversion) {
1118 $id = $WSversion['uid'];
1119 $recordAccess = true;
1120 } elseif ($this->BE_USER->workspaceAllowAutoCreation($table, $id, $theRealPid)) {
1121 // new version of a record created in a workspace - so always refresh pagetree to indicate there is a change in the workspace
1122 $this->pagetreeNeedsRefresh = true;
1123
1124 /** @var $tce DataHandler */
1125 $tce = GeneralUtility::makeInstance(__CLASS__);
1126 $tce->enableLogging = $this->enableLogging;
1127 // Setting up command for creating a new version of the record:
1128 $cmd = [];
1129 $cmd[$table][$id]['version'] = [
1130 'action' => 'new',
1131 // Default is to create a version of the individual records... element versioning that is.
1132 'label' => 'Auto-created for WS #' . $this->BE_USER->workspace
1133 ];
1134 $tce->start([], $cmd);
1135 $tce->process_cmdmap();
1136 $this->errorLog = array_merge($this->errorLog, $tce->errorLog);
1137 // If copying was successful, share the new uids (also of related children):
1138 if ($tce->copyMappingArray[$table][$id]) {
1139 foreach ($tce->copyMappingArray as $origTable => $origIdArray) {
1140 foreach ($origIdArray as $origId => $newId) {
1141 $this->uploadedFileArray[$origTable][$newId] = $this->uploadedFileArray[$origTable][$origId];
1142 $this->autoVersionIdMap[$origTable][$origId] = $newId;
1143 }
1144 }
1145 ArrayUtility::mergeRecursiveWithOverrule($this->RTEmagic_copyIndex, $tce->RTEmagic_copyIndex);
1146 // See where RTEmagic_copyIndex is used inside fillInFieldArray() for more information...
1147 // Update registerDBList, that holds the copied relations to child records:
1148 $registerDBList = array_merge($registerDBList, $tce->registerDBList);
1149 // For the reason that creating a new version of this record, automatically
1150 // created related child records (e.g. "IRRE"), update the accordant field:
1151 $this->getVersionizedIncomingFieldArray($table, $id, $incomingFieldArray, $registerDBList);
1152 // Use the new id of the copied/versionized record:
1153 $id = $this->autoVersionIdMap[$table][$id];
1154 $recordAccess = true;
1155 $this->autoVersioningUpdate = true;
1156 } else {
1157 $this->newlog('Could not be edited in offline workspace in the branch where found (failure state: \'' . $errorCode . '\'). Auto-creation of version failed!', 1);
1158 }
1159 } else {
1160 $this->newlog('Could not be edited in offline workspace in the branch where found (failure state: \'' . $errorCode . '\'). Auto-creation of version not allowed in workspace!', 1);
1161 }
1162 }
1163 }
1164 // The default is 'update'
1165 $status = 'update';
1166 }
1167 // If access was granted above, proceed to create or update record:
1168 if (!$recordAccess) {
1169 continue;
1170 }
1171
1172 // Here the "pid" is set IF NOT the old pid was a string pointing to a place in the subst-id array.
1173 list($tscPID) = BackendUtility::getTSCpid($table, $id, $old_pid_value ? $old_pid_value : $fieldArray['pid']);
1174 if ($status === 'new' && $table === 'pages') {
1175 $TSConfig = BackendUtility::getPagesTSconfig($tscPID)['TCEMAIN.'] ?? [];
1176 if (isset($TSConfig['permissions.']) && is_array($TSConfig['permissions.'])) {
1177 $fieldArray = $this->setTSconfigPermissions($fieldArray, $TSConfig['permissions.']);
1178 }
1179 }
1180 // Processing of all fields in incomingFieldArray and setting them in $fieldArray
1181 $fieldArray = $this->fillInFieldArray($table, $id, $fieldArray, $incomingFieldArray, $theRealPid, $status, $tscPID);
1182 $newVersion_placeholderFieldArray = [];
1183 if ($createNewVersion) {
1184 // create a placeholder array with already processed field content
1185 $newVersion_placeholderFieldArray = $fieldArray;
1186 }
1187 // NOTICE! All manipulation beyond this point bypasses both "excludeFields" AND possible "MM" relations / file uploads to field!
1188 // Forcing some values unto field array:
1189 // NOTICE: This overriding is potentially dangerous; permissions per field is not checked!!!
1190 $fieldArray = $this->overrideFieldArray($table, $fieldArray);
1191 if ($createNewVersion) {
1192 $newVersion_placeholderFieldArray = $this->overrideFieldArray($table, $newVersion_placeholderFieldArray);
1193 }
1194 // Setting system fields
1195 if ($status === 'new') {
1196 if ($GLOBALS['TCA'][$table]['ctrl']['crdate']) {
1197 $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['crdate']] = $GLOBALS['EXEC_TIME'];
1198 if ($createNewVersion) {
1199 $newVersion_placeholderFieldArray[$GLOBALS['TCA'][$table]['ctrl']['crdate']] = $GLOBALS['EXEC_TIME'];
1200 }
1201 }
1202 if ($GLOBALS['TCA'][$table]['ctrl']['cruser_id']) {
1203 $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['cruser_id']] = $this->userid;
1204 if ($createNewVersion) {
1205 $newVersion_placeholderFieldArray[$GLOBALS['TCA'][$table]['ctrl']['cruser_id']] = $this->userid;
1206 }
1207 }
1208 } elseif ($this->checkSimilar) {
1209 // Removing fields which are equal to the current value:
1210 $fieldArray = $this->compareFieldArrayWithCurrentAndUnset($table, $id, $fieldArray);
1211 }
1212 if ($GLOBALS['TCA'][$table]['ctrl']['tstamp'] && !empty($fieldArray)) {
1213 $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
1214 if ($createNewVersion) {
1215 $newVersion_placeholderFieldArray[$GLOBALS['TCA'][$table]['ctrl']['tstamp']] = $GLOBALS['EXEC_TIME'];
1216 }
1217 }
1218 // Set stage to "Editing" to make sure we restart the workflow
1219 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
1220 $fieldArray['t3ver_stage'] = 0;
1221 }
1222 // Hook: processDatamap_postProcessFieldArray
1223 foreach ($hookObjectsArr as $hookObj) {
1224 if (method_exists($hookObj, 'processDatamap_postProcessFieldArray')) {
1225 $hookObj->processDatamap_postProcessFieldArray($status, $table, $id, $fieldArray, $this);
1226 }
1227 }
1228 // Performing insert/update. If fieldArray has been unset by some userfunction (see hook above), don't do anything
1229 // Kasper: Unsetting the fieldArray is dangerous; MM relations might be saved already and files could have been uploaded that are now "lost"
1230 if (is_array($fieldArray)) {
1231 if ($status === 'new') {
1232 if ($table === 'pages') {
1233 // for new pages always a refresh is needed
1234 $this->pagetreeNeedsRefresh = true;
1235 }
1236
1237 // This creates a new version of the record with online placeholder and offline version
1238 if ($createNewVersion) {
1239 // new record created in a workspace - so always refresh pagetree to indicate there is a change in the workspace
1240 $this->pagetreeNeedsRefresh = true;
1241
1242 $newVersion_placeholderFieldArray['t3ver_label'] = 'INITIAL PLACEHOLDER';
1243 // Setting placeholder state value for temporary record
1244 $newVersion_placeholderFieldArray['t3ver_state'] = (string)new VersionState(VersionState::NEW_PLACEHOLDER);
1245 // Setting workspace - only so display of place holders can filter out those from other workspaces.
1246 $newVersion_placeholderFieldArray['t3ver_wsid'] = $this->BE_USER->workspace;
1247 $newVersion_placeholderFieldArray[$GLOBALS['TCA'][$table]['ctrl']['label']] = $this->getPlaceholderTitleForTableLabel($table);
1248 // Saving placeholder as 'original'
1249 $this->insertDB($table, $id, $newVersion_placeholderFieldArray, false);
1250 // For the actual new offline version, set versioning values to point to placeholder:
1251 $fieldArray['pid'] = -1;
1252 $fieldArray['t3ver_oid'] = $this->substNEWwithIDs[$id];
1253 $fieldArray['t3ver_id'] = 1;
1254 // Setting placeholder state value for version (so it can know it is currently a new version...)
1255 $fieldArray['t3ver_state'] = (string)new VersionState(VersionState::NEW_PLACEHOLDER_VERSION);
1256 $fieldArray['t3ver_label'] = 'First draft version';
1257 $fieldArray['t3ver_wsid'] = $this->BE_USER->workspace;
1258 // When inserted, $this->substNEWwithIDs[$id] will be changed to the uid of THIS version and so the interface will pick it up just nice!
1259 $phShadowId = $this->insertDB($table, $id, $fieldArray, true, 0, true);
1260 if ($phShadowId) {
1261 // Processes fields of the placeholder record:
1262 $this->triggerRemapAction($table, $id, [$this, 'placeholderShadowing'], [$table, $phShadowId]);
1263 // Hold auto-versionized ids of placeholders:
1264 $this->autoVersionIdMap[$table][$this->substNEWwithIDs[$id]] = $phShadowId;
1265 }
1266 } else {
1267 $this->insertDB($table, $id, $fieldArray, false, $incomingFieldArray['uid']);
1268 }
1269 } else {
1270 if ($table === 'pages') {
1271 // only a certain number of fields needs to be checked for updates
1272 // if $this->checkSimilar is TRUE, fields with unchanged values are already removed here
1273 $fieldsToCheck = array_intersect($this->pagetreeRefreshFieldsFromPages, array_keys($fieldArray));
1274 if (!empty($fieldsToCheck)) {
1275 $this->pagetreeNeedsRefresh = true;
1276 }
1277 }
1278 $this->updateDB($table, $id, $fieldArray);
1279 $this->placeholderShadowing($table, $id);
1280 }
1281 }
1282 // Hook: processDatamap_afterDatabaseOperations
1283 // Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
1284 // but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
1285 $this->hook_processDatamap_afterDatabaseOperations($hookObjectsArr, $status, $table, $id, $fieldArray);
1286 }
1287 }
1288 // Process the stack of relations to remap/correct
1289 $this->processRemapStack();
1290 $this->dbAnalysisStoreExec();
1291 $this->removeRegisteredFiles();
1292 // Hook: processDatamap_afterAllOperations
1293 // Note: When this hook gets called, all operations on the submitted data have been finished.
1294 foreach ($hookObjectsArr as $hookObj) {
1295 if (method_exists($hookObj, 'processDatamap_afterAllOperations')) {
1296 $hookObj->processDatamap_afterAllOperations($this);
1297 }
1298 }
1299 if ($this->isOuterMostInstance()) {
1300 $this->processClearCacheQueue();
1301 $this->resetElementsToBeDeleted();
1302 }
1303 }
1304
1305 /**
1306 * @param string $table
1307 * @param string $row
1308 * @param string $key
1309 *
1310 * @return string
1311 */
1312 protected function normalizeTimeFormat(string $table, string $value, string $dbType): string
1313 {
1314 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
1315 $platform = $connection->getDatabasePlatform();
1316 if ($platform instanceof SQLServerPlatform) {
1317 $defaultLength = QueryHelper::getDateTimeFormats()[$dbType]['empty'];
1318 $value = substr(
1319 $value,
1320 0,
1321 strlen($defaultLength)
1322 );
1323 }
1324 return $value;
1325 }
1326
1327 /**
1328 * Sets the "sorting" DB field and the "pid" field of an incoming record that should be added (NEW1234)
1329 * depending on the record that should be added or where it should be added.
1330 *
1331 * This method is called from process_datamap()
1332 *
1333 * @param string $table the table name of the record to insert
1334 * @param int $pid the real PID (numeric) where the record should be
1335 * @param array $fieldArray field+value pairs to add
1336 * @return array the modified field array
1337 */
1338 protected function resolveSortingAndPidForNewRecord(string $table, int $pid, array $fieldArray): array
1339 {
1340 $sortColumn = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?? '';
1341 // Points to a page on which to insert the element, possibly in the top of the page
1342 if ($pid >= 0) {
1343 // Ensure that the "pid" is not a translated page ID, but the default page ID
1344 $pid = $this->getDefaultLanguagePageId($pid);
1345 // The numerical pid is inserted in the data array
1346 $fieldArray['pid'] = $pid;
1347 // If this table is sorted we better find the top sorting number
1348 if ($sortColumn) {
1349 $fieldArray[$sortColumn] = $this->getSortNumber($table, 0, $pid);
1350 }
1351 } elseif ($sortColumn) {
1352 // Points to another record before itself
1353 // If this table is sorted we better find the top sorting number
1354 // Because $pid is < 0, getSortNumber() returns an array
1355 $sortingInfo = $this->getSortNumber($table, 0, $pid);
1356 $fieldArray['pid'] = $sortingInfo['pid'];
1357 $fieldArray[$sortColumn] = $sortingInfo['sortNumber'];
1358 } else {
1359 // Here we fetch the PID of the record that we point to
1360 $record = $this->recordInfo($table, abs($pid), 'pid');
1361 // Ensure that the "pid" is not a translated page ID, but the default page ID
1362 $fieldArray['pid'] = $this->getDefaultLanguagePageId($record['pid']);
1363 }
1364 return $fieldArray;
1365 }
1366
1367 /**
1368 * Fix shadowing of data in case we are editing an offline version of a live "New" placeholder record:
1369 *
1370 * @param string $table Table name
1371 * @param int $id Record uid
1372 */
1373 public function placeholderShadowing($table, $id)
1374 {
1375 if ($liveRec = BackendUtility::getLiveVersionOfRecord($table, $id, '*')) {
1376 if (VersionState::cast($liveRec['t3ver_state'])->indicatesPlaceholder()) {
1377 $justStoredRecord = BackendUtility::getRecord($table, $id);
1378 $newRecord = [];
1379 $shadowCols = $GLOBALS['TCA'][$table]['ctrl']['shadowColumnsForNewPlaceholders'];
1380 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['languageField'];
1381 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'];
1382 if (isset($GLOBALS['TCA'][$table]['ctrl']['translationSource'])) {
1383 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['translationSource'];
1384 }
1385 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['type'];
1386 $shadowCols .= ',' . $GLOBALS['TCA'][$table]['ctrl']['label'];
1387 $shadowColumns = array_unique(GeneralUtility::trimExplode(',', $shadowCols, true));
1388 foreach ($shadowColumns as $fieldName) {
1389 if ((string)$justStoredRecord[$fieldName] !== (string)$liveRec[$fieldName] && isset($GLOBALS['TCA'][$table]['columns'][$fieldName]) && $fieldName !== 'uid' && $fieldName !== 'pid') {
1390 $newRecord[$fieldName] = $justStoredRecord[$fieldName];
1391 }
1392 }
1393 if (!empty($newRecord)) {
1394 if ($this->enableLogging) {
1395 $this->log($table, $liveRec['uid'], 0, 0, 0, 'Shadowing done on fields <i>' . implode(',', array_keys($newRecord)) . '</i> in placeholder record ' . $table . ':' . $liveRec['uid'] . ' (offline version UID=' . $id . ')', -1, [], $this->eventPid($table, $liveRec['uid'], $liveRec['pid']));
1396 }
1397 $this->updateDB($table, $liveRec['uid'], $newRecord);
1398 }
1399 }
1400 }
1401 }
1402
1403 /**
1404 * Create a placeholder title for the label field that does match the field requirements
1405 *
1406 * @param string $table The table name
1407 * @param string $placeholderContent Placeholder content to be used
1408 * @return string placeholder value
1409 */
1410 public function getPlaceholderTitleForTableLabel($table, $placeholderContent = null)
1411 {
1412 if ($placeholderContent === null) {
1413 $placeholderContent = 'PLACEHOLDER';
1414 }
1415
1416 $labelPlaceholder = '[' . $placeholderContent . ', WS#' . $this->BE_USER->workspace . ']';
1417 $labelField = $GLOBALS['TCA'][$table]['ctrl']['label'];
1418 if (!isset($GLOBALS['TCA'][$table]['columns'][$labelField]['config']['eval'])) {
1419 return $labelPlaceholder;
1420 }
1421 $evalCodesArray = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$table]['columns'][$labelField]['config']['eval'], true);
1422 $transformedLabel = $this->checkValue_input_Eval($labelPlaceholder, $evalCodesArray, '');
1423 return $transformedLabel['value'] ?? $labelPlaceholder;
1424 }
1425
1426 /**
1427 * Filling in the field array
1428 * $this->excludedTablesAndFields is used to filter fields if needed.
1429 *
1430 * @param string $table Table name
1431 * @param int $id Record ID
1432 * @param array $fieldArray Default values, Preset $fieldArray with 'pid' maybe (pid and uid will be not be overridden anyway)
1433 * @param array $incomingFieldArray Is which fields/values you want to set. There are processed and put into $fieldArray if OK
1434 * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
1435 * @param string $status Is 'new' or 'update'
1436 * @param int $tscPID TSconfig PID
1437 * @return array Field Array
1438 */
1439 public function fillInFieldArray($table, $id, $fieldArray, $incomingFieldArray, $realPid, $status, $tscPID)
1440 {
1441 // Initialize:
1442 $originalLanguageRecord = null;
1443 $originalLanguage_diffStorage = null;
1444 $diffStorageFlag = false;
1445 // Setting 'currentRecord' and 'checkValueRecord':
1446 if (strpos($id, 'NEW') !== false) {
1447 // Must have the 'current' array - not the values after processing below...
1448 $checkValueRecord = $fieldArray;
1449 // IF $incomingFieldArray is an array, overlay it.
1450 // The point is that when new records are created as copies with flex type fields there might be a field containing information about which DataStructure to use and without that information the flexforms cannot be correctly processed.... This should be OK since the $checkValueRecord is used by the flexform evaluation only anyways...
1451 if (is_array($incomingFieldArray) && is_array($checkValueRecord)) {
1452 ArrayUtility::mergeRecursiveWithOverrule($checkValueRecord, $incomingFieldArray);
1453 }
1454 $currentRecord = $checkValueRecord;
1455 } else {
1456 // We must use the current values as basis for this!
1457 $currentRecord = ($checkValueRecord = $this->recordInfo($table, $id, '*'));
1458 // This is done to make the pid positive for offline versions; Necessary to have diff-view for page translations in workspaces.
1459 BackendUtility::fixVersioningPid($table, $currentRecord);
1460 }
1461
1462 // Get original language record if available:
1463 if (is_array($currentRecord)
1464 && $GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']
1465 && $GLOBALS['TCA'][$table]['ctrl']['languageField']
1466 && $currentRecord[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0
1467 && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']
1468 && (int)$currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] > 0
1469 ) {
1470 $originalLanguageRecord = $this->recordInfo($table, $currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']], '*');
1471 BackendUtility::workspaceOL($table, $originalLanguageRecord);
1472 $originalLanguage_diffStorage = unserialize($currentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']]);
1473 }
1474
1475 $this->checkValue_currentRecord = $checkValueRecord;
1476 // In the following all incoming value-fields are tested:
1477 // - Are the user allowed to change the field?
1478 // - Is the field uid/pid (which are already set)
1479 // - perms-fields for pages-table, then do special things...
1480 // - If the field is nothing of the above and the field is configured in TCA, the fieldvalues are evaluated by ->checkValue
1481 // If everything is OK, the field is entered into $fieldArray[]
1482 foreach ($incomingFieldArray as $field => $fieldValue) {
1483 if (isset($this->excludedTablesAndFields[$table . '-' . $field]) || $this->data_disableFields[$table][$id][$field]) {
1484 continue;
1485 }
1486
1487 // The field must be editable.
1488 // Checking if a value for language can be changed:
1489 $languageDeny = $GLOBALS['TCA'][$table]['ctrl']['languageField'] && (string)$GLOBALS['TCA'][$table]['ctrl']['languageField'] === (string)$field && !$this->BE_USER->checkLanguageAccess($fieldValue);
1490 if ($languageDeny) {
1491 continue;
1492 }
1493
1494 switch ($field) {
1495 case 'uid':
1496 case 'pid':
1497 // Nothing happens, already set
1498 break;
1499 case 'perms_userid':
1500 case 'perms_groupid':
1501 case 'perms_user':
1502 case 'perms_group':
1503 case 'perms_everybody':
1504 // Permissions can be edited by the owner or the administrator
1505 if ($table === 'pages' && ($this->admin || $status === 'new' || $this->pageInfo($id, 'perms_userid') == $this->userid)) {
1506 $value = (int)$fieldValue;
1507 switch ($field) {
1508 case 'perms_userid':
1509 case 'perms_groupid':
1510 $fieldArray[$field] = $value;
1511 break;
1512 default:
1513 if ($value >= 0 && $value < (2 ** 5)) {
1514 $fieldArray[$field] = $value;
1515 }
1516 }
1517 }
1518 break;
1519 case 't3ver_oid':
1520 case 't3ver_id':
1521 case 't3ver_wsid':
1522 case 't3ver_state':
1523 case 't3ver_count':
1524 case 't3ver_stage':
1525 case 't3ver_tstamp':
1526 // t3ver_label is not here because it CAN be edited as a regular field!
1527 break;
1528 case 'l10n_state':
1529 $fieldArray[$field] = $fieldValue;
1530 break;
1531 default:
1532 if (isset($GLOBALS['TCA'][$table]['columns'][$field])) {
1533 // Evaluating the value
1534 $res = $this->checkValue($table, $field, $fieldValue, $id, $status, $realPid, $tscPID);
1535 if (array_key_exists('value', $res)) {
1536 $fieldArray[$field] = $res['value'];
1537 }
1538 // Add the value of the original record to the diff-storage content:
1539 if ($GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']) {
1540 $originalLanguage_diffStorage[$field] = $this->updateModeL10NdiffDataClear ? '' : $originalLanguageRecord[$field];
1541 $diffStorageFlag = true;
1542 }
1543 // If autoversioning is happening we need to perform a nasty hack. The case is parallel to a similar hack inside checkValue_group_select_file().
1544 // When a copy or version is made of a record, a search is made for any RTEmagic* images in fields having the "images" soft reference parser applied.
1545 // That should be TRUE for RTE fields. If any are found they are duplicated to new names and the file reference in the bodytext is updated accordingly.
1546 // However, with auto-versioning the submitted content of the field will just overwrite the corrected values. This leaves a) lost RTEmagic files and b) creates a double reference to the old files.
1547 // The only solution I can come up with is detecting when auto versioning happens, then see if any RTEmagic images was copied and if so make a stupid string-replace of the content !
1548 if ($this->autoVersioningUpdate === true) {
1549 if (is_array($this->RTEmagic_copyIndex[$table][$id][$field])) {
1550 foreach ($this->RTEmagic_copyIndex[$table][$id][$field] as $oldRTEmagicName => $newRTEmagicName) {
1551 $fieldArray[$field] = str_replace(' src="' . $oldRTEmagicName . '"', ' src="' . $newRTEmagicName . '"', $fieldArray[$field]);
1552 }
1553 }
1554 }
1555 } elseif ($GLOBALS['TCA'][$table]['ctrl']['origUid'] === $field) {
1556 // Allow value for original UID to pass by...
1557 $fieldArray[$field] = $fieldValue;
1558 }
1559 }
1560 }
1561
1562 // Dealing with a page translation, setting "sorting", "pid", "perms_*" to the same values as the original record
1563 if ($table === 'pages' && is_array($originalLanguageRecord)) {
1564 $fieldArray['sorting'] = $originalLanguageRecord['sorting'];
1565 $fieldArray['perms_userid'] = $originalLanguageRecord['perms_userid'];
1566 $fieldArray['perms_groupid'] = $originalLanguageRecord['perms_groupid'];
1567 $fieldArray['perms_user'] = $originalLanguageRecord['perms_user'];
1568 $fieldArray['perms_group'] = $originalLanguageRecord['perms_group'];
1569 $fieldArray['perms_everybody'] = $originalLanguageRecord['perms_everybody'];
1570 }
1571
1572 // Add diff-storage information:
1573 if ($diffStorageFlag
1574 && !array_key_exists($GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField'], $fieldArray)
1575 ) {
1576 // If the field is set it would probably be because of an undo-operation - in which case we should not update the field of course...
1577 $fieldArray[$GLOBALS['TCA'][$table]['ctrl']['transOrigDiffSourceField']] = serialize($originalLanguage_diffStorage);
1578 }
1579 // Return fieldArray
1580 return $fieldArray;
1581 }
1582
1583 /*********************************************
1584 *
1585 * Evaluation of input values
1586 *
1587 ********************************************/
1588 /**
1589 * Evaluates a value according to $table/$field settings.
1590 * This function is for real database fields - NOT FlexForm "pseudo" fields.
1591 * NOTICE: Calling this function expects this: 1) That the data is saved! (files are copied and so on) 2) That files registered for deletion IS deleted at the end (with ->removeRegisteredFiles() )
1592 *
1593 * @param string $table Table name
1594 * @param string $field Field name
1595 * @param string $value Value to be evaluated. Notice, this is the INPUT value from the form. The original value (from any existing record) must be manually looked up inside the function if needed - or taken from $currentRecord array.
1596 * @param string $id The record-uid, mainly - but not exclusively - used for logging
1597 * @param string $status 'update' or 'new' flag
1598 * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted. If $realPid is -1 it means that a new version of the record is being inserted.
1599 * @param int $tscPID TSconfig PID
1600 * @return array Returns the evaluated $value as key "value" in this array. Can be checked with isset($res['value']) ...
1601 */
1602 public function checkValue($table, $field, $value, $id, $status, $realPid, $tscPID)
1603 {
1604 // Result array
1605 $res = [];
1606
1607 // Processing special case of field pages.doktype
1608 if ($table === 'pages' && $field === 'doktype') {
1609 // If the user may not use this specific doktype, we issue a warning
1610 if (!($this->admin || GeneralUtility::inList($this->BE_USER->groupData['pagetypes_select'], $value))) {
1611 if ($this->enableLogging) {
1612 $propArr = $this->getRecordProperties($table, $id);
1613 $this->log($table, $id, 5, 0, 1, 'You cannot change the \'doktype\' of page \'%s\' to the desired value.', 1, [$propArr['header']], $propArr['event_pid']);
1614 }
1615 return $res;
1616 }
1617 if ($status === 'update') {
1618 // This checks 1) if we should check for disallowed tables and 2) if there are records from disallowed tables on the current page
1619 $onlyAllowedTables = $GLOBALS['PAGES_TYPES'][$value]['onlyAllowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['onlyAllowedTables'];
1620 if ($onlyAllowedTables) {
1621 // use the real page id (default language)
1622 $recordId = $this->getDefaultLanguagePageId($id);
1623 $theWrongTables = $this->doesPageHaveUnallowedTables($recordId, $value);
1624 if ($theWrongTables) {
1625 if ($this->enableLogging) {
1626 $propArr = $this->getRecordProperties($table, $id);
1627 $this->log($table, $id, 5, 0, 1, '\'doktype\' of page \'%s\' could not be changed because the page contains records from disallowed tables; %s', 2, [$propArr['header'], $theWrongTables], $propArr['event_pid']);
1628 }
1629 return $res;
1630 }
1631 }
1632 }
1633 }
1634
1635 $curValue = null;
1636 if ((int)$id !== 0) {
1637 // Get current value:
1638 $curValueRec = $this->recordInfo($table, $id, $field);
1639 // isset() won't work here, since values can be NULL
1640 if ($curValueRec !== null && array_key_exists($field, $curValueRec)) {
1641 $curValue = $curValueRec[$field];
1642 }
1643 }
1644
1645 // Getting config for the field
1646 $tcaFieldConf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
1647
1648 // Create $recFID only for those types that need it
1649 if (
1650 $tcaFieldConf['type'] === 'flex'
1651 || $tcaFieldConf['type'] === 'group' && ($tcaFieldConf['internal_type'] === 'file' || $tcaFieldConf['internal_type'] === 'file_reference')
1652 ) {
1653 $recFID = $table . ':' . $id . ':' . $field;
1654 } else {
1655 $recFID = null;
1656 }
1657
1658 // Perform processing:
1659 $res = $this->checkValue_SW($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $field, $this->uploadedFileArray[$table][$id][$field], $tscPID);
1660 return $res;
1661 }
1662
1663 /**
1664 * Branches out evaluation of a field value based on its type as configured in $GLOBALS['TCA']
1665 * Can be called for FlexForm pseudo fields as well, BUT must not have $field set if so.
1666 *
1667 * @param array $res The result array. The processed value (if any!) is set in the "value" key.
1668 * @param string $value The value to set.
1669 * @param array $tcaFieldConf Field configuration from $GLOBALS['TCA']
1670 * @param string $table Table name
1671 * @param int $id UID of record
1672 * @param mixed $curValue Current value of the field
1673 * @param string $status 'update' or 'new' flag
1674 * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted. If $realPid is -1 it means that a new version of the record is being inserted.
1675 * @param string $recFID Field identifier [table:uid:field] for flexforms
1676 * @param string $field Field name. Must NOT be set if the call is for a flexform field (since flexforms are not allowed within flexforms).
1677 * @param array $uploadedFiles
1678 * @param int $tscPID TSconfig PID
1679 * @param array $additionalData Additional data to be forwarded to sub-processors
1680 * @return array Returns the evaluated $value as key "value" in this array.
1681 */
1682 public function checkValue_SW($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $field, $uploadedFiles, $tscPID, array $additionalData = null)
1683 {
1684 // Convert to NULL value if defined in TCA
1685 if ($value === null && !empty($tcaFieldConf['eval']) && GeneralUtility::inList($tcaFieldConf['eval'], 'null')) {
1686 $res = ['value' => null];
1687 return $res;
1688 }
1689
1690 switch ($tcaFieldConf['type']) {
1691 case 'text':
1692 $res = $this->checkValueForText($value, $tcaFieldConf, $table, $id, $realPid, $field);
1693 break;
1694 case 'passthrough':
1695 case 'imageManipulation':
1696 case 'user':
1697 $res['value'] = $value;
1698 break;
1699 case 'input':
1700 $res = $this->checkValueForInput($value, $tcaFieldConf, $table, $id, $realPid, $field);
1701 break;
1702 case 'check':
1703 $res = $this->checkValueForCheck($res, $value, $tcaFieldConf, $table, $id, $realPid, $field);
1704 break;
1705 case 'radio':
1706 $res = $this->checkValueForRadio($res, $value, $tcaFieldConf, $table, $id, $realPid, $field);
1707 break;
1708 case 'group':
1709 case 'select':
1710 $res = $this->checkValueForGroupSelect($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $recFID, $uploadedFiles, $field);
1711 break;
1712 case 'inline':
1713 $res = $this->checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, $additionalData);
1714 break;
1715 case 'flex':
1716 // FlexForms are only allowed for real fields.
1717 if ($field) {
1718 $res = $this->checkValueForFlex($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $tscPID, $uploadedFiles, $field);
1719 }
1720 break;
1721 default:
1722 // Do nothing
1723 }
1724 $res = $this->checkValueForInternalReferences($res, $value, $tcaFieldConf, $table, $id, $field);
1725 return $res;
1726 }
1727
1728 /**
1729 * Checks values that are used for internal references. If the provided $value
1730 * is a NEW-identifier, the direct processing is stopped. Instead, the value is
1731 * forwarded to the remap-stack to be post-processed and resolved into a proper
1732 * UID after all data has been resolved.
1733 *
1734 * This method considers TCA types that cannot handle and resolve these internal
1735 * values directly, like 'passthrough', 'none' or 'user'. Values are only modified
1736 * here if the $field is used as 'transOrigPointerField' or 'translationSource'.
1737 *
1738 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1739 * @param string $value The value to set.
1740 * @param array $tcaFieldConf Field configuration from TCA
1741 * @param string $table Table name
1742 * @param int $id UID of record
1743 * @param string $field The field name
1744 * @return array The result array. The processed value (if any!) is set in the "value" key.
1745 */
1746 protected function checkValueForInternalReferences(array $res, $value, $tcaFieldConf, $table, $id, $field)
1747 {
1748 $relevantFieldNames = [
1749 $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] ?? null,
1750 $GLOBALS['TCA'][$table]['ctrl']['translationSource'] ?? null,
1751 ];
1752
1753 if (
1754 // in case field is empty
1755 empty($field)
1756 // in case the field is not relevant
1757 || !in_array($field, $relevantFieldNames)
1758 // in case the 'value' index has been unset already
1759 || !array_key_exists('value', $res)
1760 // in case it's not a NEW-identifier
1761 || strpos($value, 'NEW') === false
1762 ) {
1763 return $res;
1764 }
1765
1766 $valueArray = [$value];
1767 $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
1768 $this->addNewValuesToRemapStackChildIds($valueArray);
1769 $this->remapStack[] = [
1770 'args' => [$valueArray, $tcaFieldConf, $id, $table, $field],
1771 'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 3],
1772 'field' => $field
1773 ];
1774 unset($res['value']);
1775
1776 return $res;
1777 }
1778
1779 /**
1780 * Evaluate "text" type values.
1781 *
1782 * @param string $value The value to set.
1783 * @param array $tcaFieldConf Field configuration from TCA
1784 * @param string $table Table name
1785 * @param int $id UID of record
1786 * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted. If $realPid is -1 it means that a new version of the record is being inserted.
1787 * @param string $field Field name
1788 * @return array $res The result array. The processed value (if any!) is set in the "value" key.
1789 */
1790 protected function checkValueForText($value, $tcaFieldConf, $table, $id, $realPid, $field)
1791 {
1792 if (isset($tcaFieldConf['eval']) && $tcaFieldConf['eval'] !== '') {
1793 $cacheId = $this->getFieldEvalCacheIdentifier($tcaFieldConf['eval']);
1794 $evalCodesArray = $this->runtimeCache->get($cacheId);
1795 if (!is_array($evalCodesArray)) {
1796 $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1797 $this->runtimeCache->set($cacheId, $evalCodesArray);
1798 }
1799 $valueArray = $this->checkValue_text_Eval($value, $evalCodesArray, $tcaFieldConf['is_in']);
1800 } else {
1801 $valueArray = ['value' => $value];
1802 }
1803
1804 // Handle richtext transformations
1805 if ($this->dontProcessTransformations) {
1806 return $valueArray;
1807 }
1808 $recordType = BackendUtility::getTCAtypeValue($table, $this->checkValue_currentRecord);
1809 $columnsOverridesConfigOfField = $GLOBALS['TCA'][$table]['types'][$recordType]['columnsOverrides'][$field]['config'] ?? null;
1810 if ($columnsOverridesConfigOfField) {
1811 ArrayUtility::mergeRecursiveWithOverrule($tcaFieldConf, $columnsOverridesConfigOfField);
1812 }
1813 if (isset($tcaFieldConf['enableRichtext']) && (bool)$tcaFieldConf['enableRichtext'] === true) {
1814 $richtextConfigurationProvider = GeneralUtility::makeInstance(Richtext::class);
1815 $richtextConfiguration = $richtextConfigurationProvider->getConfiguration($table, $field, $realPid, $recordType, $tcaFieldConf);
1816 $parseHTML = GeneralUtility::makeInstance(RteHtmlParser::class);
1817 $parseHTML->init($table . ':' . $field, $realPid);
1818 $valueArray['value'] = $parseHTML->RTE_transform($value, [], 'db', $richtextConfiguration);
1819 }
1820
1821 return $valueArray;
1822 }
1823
1824 /**
1825 * Evaluate "input" type values.
1826 *
1827 * @param string $value The value to set.
1828 * @param array $tcaFieldConf Field configuration from TCA
1829 * @param string $table Table name
1830 * @param int $id UID of record
1831 * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted. If $realPid is -1 it means that a new version of the record is being inserted.
1832 * @param string $field Field name
1833 * @return array $res The result array. The processed value (if any!) is set in the "value" key.
1834 */
1835 protected function checkValueForInput($value, $tcaFieldConf, $table, $id, $realPid, $field)
1836 {
1837 // Handle native date/time fields
1838 $isDateOrDateTimeField = false;
1839 $format = '';
1840 $emptyValue = '';
1841 $dateTimeTypes = QueryHelper::getDateTimeTypes();
1842 // normal integer "date" fields (timestamps) are handled in checkValue_input_Eval
1843 if (isset($tcaFieldConf['dbType']) && in_array($tcaFieldConf['dbType'], $dateTimeTypes, true)) {
1844 if (empty($value)) {
1845 $value = null;
1846 } else {
1847 $isDateOrDateTimeField = true;
1848 $dateTimeFormats = QueryHelper::getDateTimeFormats();
1849 $format = $dateTimeFormats[$tcaFieldConf['dbType']]['format'];
1850
1851 // Convert the date/time into a timestamp for the sake of the checks
1852 $emptyValue = $dateTimeFormats[$tcaFieldConf['dbType']]['empty'];
1853 // We store UTC timestamps in the database, which is what getTimestamp() returns.
1854 $dateTime = new \DateTime($value);
1855 $value = $value === $emptyValue ? null : $dateTime->getTimestamp();
1856 }
1857 }
1858 // Secures the string-length to be less than max.
1859 if ((int)$tcaFieldConf['max'] > 0) {
1860 $value = mb_substr((string)$value, 0, (int)$tcaFieldConf['max'], 'utf-8');
1861 }
1862
1863 if (empty($tcaFieldConf['eval'])) {
1864 $res = ['value' => $value];
1865 } else {
1866 // Process evaluation settings:
1867 $cacheId = $this->getFieldEvalCacheIdentifier($tcaFieldConf['eval']);
1868 $evalCodesArray = $this->runtimeCache->get($cacheId);
1869 if (!is_array($evalCodesArray)) {
1870 $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1871 $this->runtimeCache->set($cacheId, $evalCodesArray);
1872 }
1873
1874 $res = $this->checkValue_input_Eval($value, $evalCodesArray, $tcaFieldConf['is_in']);
1875 if (isset($tcaFieldConf['dbType']) && isset($res['value']) && !$res['value']) {
1876 // set the value to null if we have an empty value for a native field
1877 $res['value'] = null;
1878 }
1879
1880 // Process UNIQUE settings:
1881 // Field is NOT set for flexForms - which also means that uniqueInPid and unique is NOT available for flexForm fields! Also getUnique should not be done for versioning and if PID is -1 ($realPid<0) then versioning is happening...
1882 if ($field && $realPid >= 0 && !empty($res['value'])) {
1883 if (in_array('uniqueInPid', $evalCodesArray, true)) {
1884 $res['value'] = $this->getUnique($table, $field, $res['value'], $id, $realPid);
1885 }
1886 if ($res['value'] && in_array('unique', $evalCodesArray, true)) {
1887 $res['value'] = $this->getUnique($table, $field, $res['value'], $id);
1888 }
1889 }
1890 }
1891
1892 // Checking range of value:
1893 // @todo: The "checkbox" option was removed for type=input, this check could be probably relaxed?
1894 if ($tcaFieldConf['range'] && $res['value'] != $tcaFieldConf['checkbox'] && (int)$res['value'] !== (int)$tcaFieldConf['default']) {
1895 if (isset($tcaFieldConf['range']['upper']) && (int)$res['value'] > (int)$tcaFieldConf['range']['upper']) {
1896 $res['value'] = (int)$tcaFieldConf['range']['upper'];
1897 }
1898 if (isset($tcaFieldConf['range']['lower']) && (int)$res['value'] < (int)$tcaFieldConf['range']['lower']) {
1899 $res['value'] = (int)$tcaFieldConf['range']['lower'];
1900 }
1901 }
1902
1903 // Handle native date/time fields
1904 if ($isDateOrDateTimeField) {
1905 // Convert the timestamp back to a date/time
1906 $res['value'] = $res['value'] ? gmdate($format, $res['value']) : $emptyValue;
1907 }
1908 return $res;
1909 }
1910
1911 /**
1912 * Evaluates 'check' type values.
1913 *
1914 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1915 * @param string $value The value to set.
1916 * @param array $tcaFieldConf Field configuration from TCA
1917 * @param string $table Table name
1918 * @param int $id UID of record
1919 * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted. If $realPid is -1 it means that a new version of the record is being inserted.
1920 * @param string $field Field name
1921 * @return array Modified $res array
1922 */
1923 protected function checkValueForCheck($res, $value, $tcaFieldConf, $table, $id, $realPid, $field)
1924 {
1925 $items = $tcaFieldConf['items'];
1926 if ($tcaFieldConf['itemsProcFunc']) {
1927 /** @var ItemProcessingService $processingService */
1928 $processingService = GeneralUtility::makeInstance(ItemProcessingService::class);
1929 $items = $processingService->getProcessingItems(
1930 $table,
1931 $realPid,
1932 $field,
1933 $this->checkValue_currentRecord,
1934 $tcaFieldConf,
1935 $tcaFieldConf['items']
1936 );
1937 }
1938
1939 $itemC = 0;
1940 if ($items !== null) {
1941 $itemC = count($items);
1942 }
1943 if (!$itemC) {
1944 $itemC = 1;
1945 }
1946 $maxV = (2 ** $itemC) - 1;
1947 if ($value < 0) {
1948 // @todo: throw LogicException here? Negative values for checkbox items do not make sense and indicate a coding error.
1949 $value = 0;
1950 }
1951 if ($value > $maxV) {
1952 // @todo: This case is pretty ugly: If there is an itemsProcFunc registered, and if it returns a dynamic,
1953 // @todo: changing list of items, then it may happen that a value is transformed and vanished checkboxes
1954 // @todo: are permanently removed from the value.
1955 // @todo: Suggestion: Throw an exception instead? Maybe a specific, catchable exception that generates a
1956 // @todo: error message to the user - dynamic item sets via itemProcFunc on check would be a bad idea anyway.
1957 $value = $value & $maxV;
1958 }
1959 if ($field && $realPid >= 0 && $value > 0 && !empty($tcaFieldConf['eval'])) {
1960 $evalCodesArray = GeneralUtility::trimExplode(',', $tcaFieldConf['eval'], true);
1961 $otherRecordsWithSameValue = [];
1962 $maxCheckedRecords = 0;
1963 if (in_array('maximumRecordsCheckedInPid', $evalCodesArray, true)) {
1964 $otherRecordsWithSameValue = $this->getRecordsWithSameValue($table, $id, $field, $value, $realPid);
1965 $maxCheckedRecords = (int)$tcaFieldConf['validation']['maximumRecordsCheckedInPid'];
1966 }
1967 if (in_array('maximumRecordsChecked', $evalCodesArray, true)) {
1968 $otherRecordsWithSameValue = $this->getRecordsWithSameValue($table, $id, $field, $value);
1969 $maxCheckedRecords = (int)$tcaFieldConf['validation']['maximumRecordsChecked'];
1970 }
1971
1972 // there are more than enough records with value "1" in the DB
1973 // if so, set this value to "0" again
1974 if ($maxCheckedRecords && count($otherRecordsWithSameValue) >= $maxCheckedRecords) {
1975 $value = 0;
1976 $this->log($table, $id, 5, 0, 1, 'Could not activate checkbox for field "%s". A total of %s record(s) can have this checkbox activated. Uncheck other records first in order to activate the checkbox of this record.', -1, [$this->getLanguageService()->sL(BackendUtility::getItemLabel($table, $field)), $maxCheckedRecords]);
1977 }
1978 }
1979 $res['value'] = $value;
1980 return $res;
1981 }
1982
1983 /**
1984 * Evaluates 'radio' type values.
1985 *
1986 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
1987 * @param string $value The value to set.
1988 * @param array $tcaFieldConf Field configuration from TCA
1989 * @param string $table The table of the record
1990 * @param int $id The id of the record
1991 * @param int $pid The pid of the record
1992 * @param string $field The field to check
1993 * @return array Modified $res array
1994 */
1995 protected function checkValueForRadio($res, $value, $tcaFieldConf, $table, $id, $pid, $field)
1996 {
1997 if (is_array($tcaFieldConf['items'])) {
1998 foreach ($tcaFieldConf['items'] as $set) {
1999 if ((string)$set[1] === (string)$value) {
2000 $res['value'] = $value;
2001 break;
2002 }
2003 }
2004 }
2005
2006 // if no value was found and an itemsProcFunc is defined, check that for the value
2007 if ($tcaFieldConf['itemsProcFunc'] && empty($res['value'])) {
2008 $processingService = GeneralUtility::makeInstance(ItemProcessingService::class);
2009 $processedItems = $processingService->getProcessingItems(
2010 $table,
2011 $pid,
2012 $field,
2013 $this->checkValue_currentRecord,
2014 $tcaFieldConf,
2015 $tcaFieldConf['items']
2016 );
2017
2018 foreach ($processedItems as $set) {
2019 if ((string)$set[1] === (string)$value) {
2020 $res['value'] = $value;
2021 break;
2022 }
2023 }
2024 }
2025
2026 return $res;
2027 }
2028
2029 /**
2030 * Evaluates 'group' or 'select' type values.
2031 *
2032 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2033 * @param string|array $value The value to set.
2034 * @param array $tcaFieldConf Field configuration from TCA
2035 * @param string $table Table name
2036 * @param int $id UID of record
2037 * @param mixed $curValue Current value of the field
2038 * @param string $status 'update' or 'new' flag
2039 * @param string $recFID Field identifier [table:uid:field] for flexforms
2040 * @param array $uploadedFiles
2041 * @param string $field Field name
2042 * @return array Modified $res array
2043 */
2044 protected function checkValueForGroupSelect($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $recFID, $uploadedFiles, $field)
2045 {
2046 // Detecting if value sent is an array and if so, implode it around a comma:
2047 if (is_array($value)) {
2048 $value = implode(',', $value);
2049 }
2050 // This converts all occurrences of '&#123;' to the byte 123 in the string - this is needed in very rare cases where file names with special characters (e.g. ???, umlaut) gets sent to the server as HTML entities instead of bytes. The error is done only by MSIE, not Mozilla and Opera.
2051 // Anyway, this should NOT disturb anything else:
2052 $value = $this->convNumEntityToByteValue($value);
2053 // When values are sent as group or select they come as comma-separated values which are exploded by this function:
2054 $valueArray = $this->checkValue_group_select_explodeSelectGroupValue($value);
2055 // If multiple is not set, remove duplicates:
2056 if (!$tcaFieldConf['multiple']) {
2057 $valueArray = array_unique($valueArray);
2058 }
2059 // If an exclusive key is found, discard all others:
2060 if ($tcaFieldConf['type'] === 'select' && $tcaFieldConf['exclusiveKeys']) {
2061 $exclusiveKeys = GeneralUtility::trimExplode(',', $tcaFieldConf['exclusiveKeys']);
2062 foreach ($valueArray as $index => $key) {
2063 if (in_array($key, $exclusiveKeys, true)) {
2064 $valueArray = [$index => $key];
2065 break;
2066 }
2067 }
2068 }
2069 // This could be a good spot for parsing the array through a validation-function which checks if the values are correct (except that database references are not in their final form - but that is the point, isn't it?)
2070 // NOTE!!! Must check max-items of files before the later check because that check would just leave out file names if there are too many!!
2071 $valueArray = $this->applyFiltersToValues($tcaFieldConf, $valueArray);
2072 // Checking for select / authMode, removing elements from $valueArray if any of them is not allowed!
2073 if ($tcaFieldConf['type'] === 'select' && $tcaFieldConf['authMode']) {
2074 $preCount = count($valueArray);
2075 foreach ($valueArray as $index => $key) {
2076 if (!$this->BE_USER->checkAuthMode($table, $field, $key, $tcaFieldConf['authMode'])) {
2077 unset($valueArray[$index]);
2078 }
2079 }
2080 // During the check it turns out that the value / all values were removed - we respond by simply returning an empty array so nothing is written to DB for this field.
2081 if ($preCount && empty($valueArray)) {
2082 return [];
2083 }
2084 }
2085 // For group types:
2086 if ($tcaFieldConf['type'] === 'group'
2087 && in_array($tcaFieldConf['internal_type'], ['file', 'file_reference'], true)) {
2088 $valueArray = $this->checkValue_group_select_file($valueArray, $tcaFieldConf, $curValue, $uploadedFiles, $status, $table, $id, $recFID);
2089 }
2090 // For select types which has a foreign table attached:
2091 $unsetResult = false;
2092 if (
2093 $tcaFieldConf['type'] === 'group' && $tcaFieldConf['internal_type'] === 'db'
2094 || $tcaFieldConf['type'] === 'select' && ($tcaFieldConf['foreign_table'] || isset($tcaFieldConf['special']) && $tcaFieldConf['special'] === 'languages')
2095 ) {
2096 // check, if there is a NEW... id in the value, that should be substituted later
2097 if (strpos($value, 'NEW') !== false) {
2098 $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
2099 $this->addNewValuesToRemapStackChildIds($valueArray);
2100 $this->remapStack[] = [
2101 'func' => 'checkValue_group_select_processDBdata',
2102 'args' => [$valueArray, $tcaFieldConf, $id, $status, $tcaFieldConf['type'], $table, $field],
2103 'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 5],
2104 'field' => $field
2105 ];
2106 $unsetResult = true;
2107 } else {
2108 $valueArray = $this->checkValue_group_select_processDBdata($valueArray, $tcaFieldConf, $id, $status, $tcaFieldConf['type'], $table, $field);
2109 }
2110 }
2111 if (!$unsetResult) {
2112 $newVal = $this->checkValue_checkMax($tcaFieldConf, $valueArray);
2113 $res['value'] = $this->castReferenceValue(implode(',', $newVal), $tcaFieldConf);
2114 } else {
2115 unset($res['value']);
2116 }
2117 return $res;
2118 }
2119
2120 /**
2121 * Applies the filter methods from a column's TCA configuration to a value array.
2122 *
2123 * @param array $tcaFieldConfiguration
2124 * @param array $values
2125 * @return array|mixed
2126 * @throws \RuntimeException
2127 */
2128 protected function applyFiltersToValues(array $tcaFieldConfiguration, array $values)
2129 {
2130 if (empty($tcaFieldConfiguration['filter']) || !is_array($tcaFieldConfiguration['filter'])) {
2131 return $values;
2132 }
2133 foreach ($tcaFieldConfiguration['filter'] as $filter) {
2134 if (empty($filter['userFunc'])) {
2135 continue;
2136 }
2137 $parameters = $filter['parameters'] ?: [];
2138 $parameters['values'] = $values;
2139 $parameters['tcaFieldConfig'] = $tcaFieldConfiguration;
2140 $values = GeneralUtility::callUserFunction($filter['userFunc'], $parameters, $this);
2141 if (!is_array($values)) {
2142 throw new \RuntimeException('Failed calling filter userFunc.', 1336051942);
2143 }
2144 }
2145 return $values;
2146 }
2147
2148 /**
2149 * Handling files for group/select function
2150 *
2151 * @param array $valueArray Array of incoming file references. Keys are numeric, values are files (basically, this is the exploded list of incoming files)
2152 * @param array $tcaFieldConf Configuration array from TCA of the field
2153 * @param string $curValue Current value of the field
2154 * @param array $uploadedFileArray Array of uploaded files, if any
2155 * @param string $status 'update' or 'new' flag
2156 * @param string $table tablename of record
2157 * @param int $id UID of record
2158 * @param string $recFID Field identifier [table:uid:field] for flexforms
2159 * @return array Modified value array
2160 *
2161 * @throws \RuntimeException
2162 */
2163 public function checkValue_group_select_file($valueArray, $tcaFieldConf, $curValue, $uploadedFileArray, $status, $table, $id, $recFID)
2164 {
2165 // If file handling should NOT be bypassed, do processing:
2166 if (!$this->bypassFileHandling) {
2167 // If any files are uploaded, add them to value array
2168 // Numeric index means that there are multiple files
2169 if (isset($uploadedFileArray[0])) {
2170 $uploadedFiles = $uploadedFileArray;
2171 } else {
2172 // There is only one file
2173 $uploadedFiles = [$uploadedFileArray];
2174 }
2175 foreach ($uploadedFiles as $uploadedFileArray) {
2176 if (!empty($uploadedFileArray['name']) && $uploadedFileArray['tmp_name'] !== 'none') {
2177 $valueArray[] = $uploadedFileArray['tmp_name'];
2178 $this->alternativeFileName[$uploadedFileArray['tmp_name']] = $uploadedFileArray['name'];
2179 }
2180 }
2181 // Creating fileFunc object.
2182 if (!$this->fileFunc) {
2183 $this->fileFunc = GeneralUtility::makeInstance(BasicFileUtility::class);
2184 }
2185 // Setting permitted extensions.
2186 $this->fileFunc->setFileExtensionPermissions($tcaFieldConf['allowed'], $tcaFieldConf['disallowed'] ?: '*');
2187 }
2188 // If there is an upload folder defined:
2189 if ($tcaFieldConf['uploadfolder'] && $tcaFieldConf['internal_type'] === 'file') {
2190 $currentFilesForHistory = null;
2191 // If filehandling should NOT be bypassed, do processing:
2192 if (!$this->bypassFileHandling) {
2193 // For logging..
2194 $propArr = $this->getRecordProperties($table, $id);
2195 // Get destination path:
2196 $dest = PATH_site . $tcaFieldConf['uploadfolder'];
2197 // If we are updating:
2198 if ($status === 'update') {
2199 // Traverse the input values and convert to absolute filenames in case the update happens to an autoVersionized record.
2200 // Background: This is a horrible workaround! The problem is that when a record is auto-versionized the files of the record get copied and therefore get new names which is overridden with the names from the original record in the incoming data meaning both lost files and double-references!
2201 // The only solution I could come up with (except removing support for managing files when autoversioning) was to convert all relative files to absolute names so they are copied again (and existing files deleted). This should keep references intact but means that some files are copied, then deleted after being copied _again_.
2202 // Actually, the same problem applies to database references in case auto-versioning would include sub-records since in such a case references are remapped - and they would be overridden due to the same principle then.
2203 // Illustration of the problem comes here:
2204 // We have a record 123 with a file logo.gif. We open and edit the files header in a workspace. So a new version is automatically made.
2205 // The versions uid is 456 and the file is copied to "logo_01.gif". But the form data that we sent was based on uid 123 and hence contains the filename "logo.gif" from the original.
2206 // The file management code below will do two things: First it will blindly accept "logo.gif" as a file attached to the record (thus creating a double reference) and secondly it will find that "logo_01.gif" was not in the incoming filelist and therefore should be deleted.
2207 // If we prefix the incoming file "logo.gif" with its absolute path it will be seen as a new file added. Thus it will be copied to "logo_02.gif". "logo_01.gif" will still be deleted but since the files are the same the difference is zero - only more processing and file copying for no reason. But it will work.
2208 if ($this->autoVersioningUpdate === true) {
2209 foreach ($valueArray as $key => $theFile) {
2210 // If it is an already attached file...
2211 if ($theFile === PathUtility::basename($theFile)) {
2212 $valueArray[$key] = PATH_site . $tcaFieldConf['uploadfolder'] . '/' . $theFile;
2213 }
2214 }
2215 }
2216 // Finding the CURRENT files listed, either from MM or from the current record.
2217 $theFileValues = [];
2218 // If MM relations for the files also!
2219 if ($tcaFieldConf['MM']) {
2220 $dbAnalysis = $this->createRelationHandlerInstance();
2221 /** @var $dbAnalysis RelationHandler */
2222 $dbAnalysis->start('', 'files', $tcaFieldConf['MM'], $id);
2223 foreach ($dbAnalysis->itemArray as $item) {
2224 if ($item['id']) {
2225 $theFileValues[] = $item['id'];
2226 }
2227 }
2228 } else {
2229 $theFileValues = GeneralUtility::trimExplode(',', $curValue, true);
2230 }
2231 $currentFilesForHistory = implode(',', $theFileValues);
2232 // DELETE files: If existing files were found, traverse those and register files for deletion which has been removed:
2233 if (!empty($theFileValues)) {
2234 // Traverse the input values and for all input values which match an EXISTING value, remove the existing from $theFileValues array (this will result in an array of all the existing files which should be deleted!)
2235 foreach ($valueArray as $key => $theFile) {
2236 if ($theFile && strpos(GeneralUtility::fixWindowsFilePath($theFile), '/') === false) {
2237 $theFileValues = ArrayUtility::removeArrayEntryByValue($theFileValues, $theFile);
2238 }
2239 }
2240 // This array contains the filenames in the uploadfolder that should be deleted:
2241 foreach ($theFileValues as $key => $theFile) {
2242 $theFile = trim($theFile);
2243 if (@is_file($dest . '/' . $theFile)) {
2244 $this->removeFilesStore[] = $dest . '/' . $theFile;
2245 } elseif ($theFile) {
2246 $this->log($table, $id, 5, 0, 1, 'Could not delete file \'%s\' (does not exist). (%s)', 10, [$dest . '/' . $theFile, $recFID], $propArr['event_pid']);
2247 }
2248 }
2249 }
2250 }
2251 // Traverse the submitted values:
2252 foreach ($valueArray as $key => $theFile) {
2253 // Init:
2254 $maxSize = (int)$tcaFieldConf['max_size'];
2255 // Must be cleared. Else a faulty fileref may be inserted if the below code returns an error!
2256 $theDestFile = '';
2257 // a FAL file was added, now resolve the file object and get the absolute path
2258 // @todo in future versions this needs to be modified to handle FAL objects natively
2259 if (!empty($theFile) && MathUtility::canBeInterpretedAsInteger($theFile)) {
2260 $fileObject = ResourceFactory::getInstance()->getFileObject($theFile);
2261 $theFile = $fileObject->getForLocalProcessing(false);
2262 }
2263 // NEW FILES? If the value contains '/' it indicates, that the file
2264 // is new and should be added to the uploadsdir (whether its absolute or relative does not matter here)
2265 if (strpos(GeneralUtility::fixWindowsFilePath($theFile), '/') !== false) {
2266 // Check various things before copying file:
2267 // File and destination must exist
2268 if (@is_dir($dest) && (@is_file($theFile) || @is_uploaded_file($theFile))) {
2269 // Finding size.
2270 if (is_uploaded_file($theFile) && $theFile == $uploadedFileArray['tmp_name']) {
2271 $fileSize = $uploadedFileArray['size'];
2272 } else {
2273 $fileSize = filesize($theFile);
2274 }
2275 // Check file size:
2276 if (!$maxSize || $fileSize <= $maxSize * 1024) {
2277 // Prepare filename:
2278 $theEndFileName = $this->alternativeFileName[$theFile] ?? $theFile;
2279 $fI = GeneralUtility::split_fileref($theEndFileName);
2280 // Check for allowed extension:
2281 if ($this->fileFunc->checkIfAllowed($fI['fileext'], $dest, $theEndFileName)) {
2282 $theDestFile = $this->fileFunc->getUniqueName($this->fileFunc->cleanFileName($fI['file']), $dest);
2283 // If we have a unique destination filename, then write the file:
2284 if ($theDestFile) {
2285 GeneralUtility::upload_copy_move($theFile, $theDestFile);
2286 // Hook for post-processing the upload action
2287 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processUpload'] ?? [] as $className) {
2288 $hookObject = GeneralUtility::makeInstance($className);
2289 if (!$hookObject instanceof DataHandlerProcessUploadHookInterface) {
2290 throw new \UnexpectedValueException($className . ' must implement interface ' . DataHandlerProcessUploadHookInterface::class, 1279962349);
2291 }
2292 $hookObject->processUpload_postProcessAction($theDestFile, $this);
2293 }
2294 $this->copiedFileMap[$theFile] = $theDestFile;
2295 clearstatcache();
2296 if (!@is_file($theDestFile)) {
2297 $this->log($table, $id, 5, 0, 1, 'Copying file \'%s\' failed!: The destination path (%s) may be write protected. Please make it write enabled!. (%s)', 16, [$theFile, PathUtility::dirname($theDestFile), $recFID], $propArr['event_pid']);
2298 }
2299 } else {
2300 $this->log($table, $id, 5, 0, 1, 'Copying file \'%s\' failed!: No destination file (%s) possible!. (%s)', 11, [$theFile, $theDestFile, $recFID], $propArr['event_pid']);
2301 }
2302 } else {
2303 $this->log($table, $id, 5, 0, 1, 'File extension \'%s\' not allowed. (%s)', 12, [$fI['fileext'], $recFID], $propArr['event_pid']);
2304 }
2305 } else {
2306 $this->log($table, $id, 5, 0, 1, 'Filesize (%s) of file \'%s\' exceeds limit (%s). (%s)', 13, [GeneralUtility::formatSize($fileSize), $theFile, GeneralUtility::formatSize($maxSize * 1024), $recFID], $propArr['event_pid']);
2307 }
2308 } else {
2309 $this->log($table, $id, 5, 0, 1, 'The destination (%s) or the source file (%s) does not exist. (%s)', 14, [$dest, $theFile, $recFID], $propArr['event_pid']);
2310 }
2311 // If the destination file was created, we will set the new filename in the value array, otherwise unset the entry in the value array!
2312 if (@is_file($theDestFile)) {
2313 $info = GeneralUtility::split_fileref($theDestFile);
2314 // The value is set to the new filename
2315 $valueArray[$key] = $info['file'];
2316 } else {
2317 // The value is set to the new filename
2318 unset($valueArray[$key]);
2319 }
2320 }
2321 }
2322 }
2323 // If MM relations for the files, we will set the relations as MM records and change the valuearray to contain a single entry with a count of the number of files!
2324 if ($tcaFieldConf['MM']) {
2325 /** @var $dbAnalysis RelationHandler */
2326 $dbAnalysis = $this->createRelationHandlerInstance();
2327 // Dummy
2328 $dbAnalysis->tableArray['files'] = [];
2329 foreach ($valueArray as $key => $theFile) {
2330 // Explode files
2331 $dbAnalysis->itemArray[]['id'] = $theFile;
2332 }
2333 if ($status === 'update') {
2334 $dbAnalysis->writeMM($tcaFieldConf['MM'], $id, 0);
2335 $newFiles = implode(',', $dbAnalysis->getValueArray());
2336 list(, , $recFieldName) = explode(':', $recFID);
2337 if ($currentFilesForHistory != $newFiles) {
2338 $this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$recFieldName] = $currentFilesForHistory;
2339 $this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$recFieldName] = $newFiles;
2340 } else {
2341 $this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$recFieldName] = '';
2342 $this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$recFieldName] = '';
2343 }
2344 } else {
2345 $this->dbAnalysisStore[] = [$dbAnalysis, $tcaFieldConf['MM'], $id, 0];
2346 }
2347 $valueArray = $dbAnalysis->countItems();
2348 }
2349 } else {
2350 if (!empty($valueArray)) {
2351 // If filehandling should NOT be bypassed, do processing:
2352 if (!$this->bypassFileHandling) {
2353 // For logging..
2354 $propArr = $this->getRecordProperties($table, $id);
2355 foreach ($valueArray as &$theFile) {
2356 // FAL handling: it's a UID, thus it is resolved to the absolute path
2357 if (!empty($theFile) && MathUtility::canBeInterpretedAsInteger($theFile)) {
2358 $fileObject = ResourceFactory::getInstance()->getFileObject($theFile);
2359 $theFile = $fileObject->getForLocalProcessing(false);
2360 }
2361 if ($this->alternativeFilePath[$theFile]) {
2362 // If alternative File Path is set for the file, then it was an import
2363 // don't import the file if it already exists
2364 if (@is_file(PATH_site . $this->alternativeFilePath[$theFile])) {
2365 $theFile = PATH_site . $this->alternativeFilePath[$theFile];
2366 } elseif (@is_file($theFile)) {
2367 $dest = PathUtility::dirname(PATH_site . $this->alternativeFilePath[$theFile]);
2368 if (!@is_dir($dest)) {
2369 GeneralUtility::mkdir_deep($dest);
2370 }
2371 // Init:
2372 $maxSize = (int)$tcaFieldConf['max_size'];
2373 // Must be cleared. Else a faulty fileref may be inserted if the below code returns an error!
2374 $theDestFile = '';
2375 $fileSize = filesize($theFile);
2376 // Check file size:
2377 if (!$maxSize || $fileSize <= $maxSize * 1024) {
2378 // Prepare filename:
2379 $theEndFileName = $this->alternativeFileName[$theFile] ?? $theFile;
2380 $fI = GeneralUtility::split_fileref($theEndFileName);
2381 // Check for allowed extension:
2382 if ($this->fileFunc->checkIfAllowed($fI['fileext'], $dest, $theEndFileName)) {
2383 $theDestFile = PATH_site . $this->alternativeFilePath[$theFile];
2384 // Write the file:
2385 if ($theDestFile) {
2386 GeneralUtility::upload_copy_move($theFile, $theDestFile);
2387 $this->copiedFileMap[$theFile] = $theDestFile;
2388 clearstatcache();
2389 if (!@is_file($theDestFile)) {
2390 $this->log($table, $id, 5, 0, 1, 'Copying file \'%s\' failed!: The destination path (%s) may be write protected. Please make it write enabled!. (%s)', 16, [$theFile, PathUtility::dirname($theDestFile), $recFID], $propArr['event_pid']);
2391 }
2392 } else {
2393 $this->log($table, $id, 5, 0, 1, 'Copying file \'%s\' failed!: No destination file (%s) possible!. (%s)', 11, [$theFile, $theDestFile, $recFID], $propArr['event_pid']);
2394 }
2395 } else {
2396 $this->log($table, $id, 5, 0, 1, 'File extension \'%s\' not allowed. (%s)', 12, [$fI['fileext'], $recFID], $propArr['event_pid']);
2397 }
2398 } else {
2399 $this->log($table, $id, 5, 0, 1, 'Filesize (%s) of file \'%s\' exceeds limit (%s). (%s)', 13, [GeneralUtility::formatSize($fileSize), $theFile, GeneralUtility::formatSize($maxSize * 1024), $recFID], $propArr['event_pid']);
2400 }
2401 // If the destination file was created, we will set the new filename in the value array, otherwise unset the entry in the value array!
2402 if (@is_file($theDestFile)) {
2403 // The value is set to the new filename
2404 $theFile = $theDestFile;
2405 } else {
2406 // The value is set to the new filename
2407 unset($theFile);
2408 }
2409 }
2410 }
2411 if (!empty($theFile)) {
2412 $theFile = GeneralUtility::fixWindowsFilePath($theFile);
2413 if (GeneralUtility::isFirstPartOfStr($theFile, PATH_site)) {
2414 $theFile = PathUtility::stripPathSitePrefix($theFile);
2415 }
2416 }
2417 }
2418 unset($theFile);
2419 }
2420 }
2421 }
2422 return $valueArray;
2423 }
2424
2425 /**
2426 * Evaluates 'flex' type values.
2427 *
2428 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2429 * @param string|array $value The value to set.
2430 * @param array $tcaFieldConf Field configuration from TCA
2431 * @param string $table Table name
2432 * @param int $id UID of record
2433 * @param mixed $curValue Current value of the field
2434 * @param string $status 'update' or 'new' flag
2435 * @param int $realPid The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted. If $realPid is -1 it means that a new version of the record is being inserted.
2436 * @param string $recFID Field identifier [table:uid:field] for flexforms
2437 * @param int $tscPID TSconfig PID
2438 * @param array $uploadedFiles Uploaded files for the field
2439 * @param string $field Field name
2440 * @return array Modified $res array
2441 */
2442 protected function checkValueForFlex($res, $value, $tcaFieldConf, $table, $id, $curValue, $status, $realPid, $recFID, $tscPID, $uploadedFiles, $field)
2443 {
2444 if (is_array($value)) {
2445 // This value is necessary for flex form processing to happen on flexform fields in page records when they are copied.
2446 // Problem: when copying a page, flexform XML comes along in the array for the new record - but since $this->checkValue_currentRecord
2447 // does not have a uid or pid for that sake, the FlexFormTools->getDataStructureIdentifier() function returns no good DS. For new
2448 // records we do know the expected PID so therefore we send that with this special parameter. Only active when larger than zero.
2449 $row = $this->checkValue_currentRecord;
2450 if ($status === 'new') {
2451 $row['pid'] = $realPid;
2452 }
2453
2454 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
2455
2456 // Get data structure. The methods may throw various exceptions, with some of them being
2457 // ok in certain scenarios, for instance on new record rows. Those are ok to "eat" here
2458 // and substitute with a dummy DS.
2459 $dataStructureArray = ['sheets' => ['sDEF' => []]];
2460 try {
2461 $dataStructureIdentifier = $flexFormTools->getDataStructureIdentifier(
2462 ['config' => $tcaFieldConf],
2463 $table,
2464 $field,
2465 $row
2466 );
2467
2468 $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
2469 } catch (InvalidParentRowException $e) {
2470 } catch (InvalidParentRowLoopException $e) {
2471 } catch (InvalidParentRowRootException $e) {
2472 } catch (InvalidPointerFieldValueException $e) {
2473 } catch (InvalidIdentifierException $e) {
2474 }
2475
2476 // Get current value array:
2477 $currentValueArray = (string)$curValue !== '' ? GeneralUtility::xml2array($curValue) : [];
2478 if (!is_array($currentValueArray)) {
2479 $currentValueArray = [];
2480 }
2481 // Remove all old meta for languages...
2482 // Evaluation of input values:
2483 $value['data'] = $this->checkValue_flex_procInData($value['data'], $currentValueArray['data'], $uploadedFiles['data'], $dataStructureArray, [$table, $id, $curValue, $status, $realPid, $recFID, $tscPID]);
2484 // Create XML from input value:
2485 $xmlValue = $this->checkValue_flexArray2Xml($value, true);
2486
2487 // Here we convert the currently submitted values BACK to an array, then merge the two and then BACK to XML again. This is needed to ensure the charsets are the same
2488 // (provided that the current value was already stored IN the charset that the new value is converted to).
2489 $arrValue = GeneralUtility::xml2array($xmlValue);
2490
2491 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkFlexFormValue'] ?? [] as $className) {
2492 $hookObject = GeneralUtility::makeInstance($className);
2493 if (method_exists($hookObject, 'checkFlexFormValue_beforeMerge')) {
2494 $hookObject->checkFlexFormValue_beforeMerge($this, $currentValueArray, $arrValue);
2495 }
2496 }
2497
2498 ArrayUtility::mergeRecursiveWithOverrule($currentValueArray, $arrValue);
2499 $xmlValue = $this->checkValue_flexArray2Xml($currentValueArray, true);
2500
2501 // Action commands (sorting order and removals of elements) for flexform sections,
2502 // see FormEngine for the use of this GP parameter
2503 $actionCMDs = GeneralUtility::_GP('_ACTION_FLEX_FORMdata');
2504 if (is_array($actionCMDs[$table][$id][$field]['data'])) {
2505 $arrValue = GeneralUtility::xml2array($xmlValue);
2506 $this->_ACTION_FLEX_FORMdata($arrValue['data'], $actionCMDs[$table][$id][$field]['data']);
2507 $xmlValue = $this->checkValue_flexArray2Xml($arrValue, true);
2508 }
2509 // Create the value XML:
2510 $res['value'] = '';
2511 $res['value'] .= $xmlValue;
2512 } else {
2513 // Passthrough...:
2514 $res['value'] = $value;
2515 }
2516
2517 return $res;
2518 }
2519
2520 /**
2521 * Converts an array to FlexForm XML
2522 *
2523 * @param array $array Array with FlexForm data
2524 * @param bool $addPrologue If set, the XML prologue is returned as well.
2525 * @return string Input array converted to XML
2526 */
2527 public function checkValue_flexArray2Xml($array, $addPrologue = false)
2528 {
2529 /** @var $flexObj FlexFormTools */
2530 $flexObj = GeneralUtility::makeInstance(FlexFormTools::class);
2531 return $flexObj->flexArray2Xml($array, $addPrologue);
2532 }
2533
2534 /**
2535 * Actions for flex form element (move, delete)
2536 * allows to remove and move flexform sections
2537 *
2538 * @param array $valueArray by reference
2539 * @param array $actionCMDs
2540 */
2541 protected function _ACTION_FLEX_FORMdata(&$valueArray, $actionCMDs)
2542 {
2543 if (!is_array($valueArray) || !is_array($actionCMDs)) {
2544 return;
2545 }
2546
2547 foreach ($actionCMDs as $key => $value) {
2548 if ($key === '_ACTION') {
2549 // First, check if there are "commands":
2550 if (current($actionCMDs[$key]) === '') {
2551 continue;
2552 }
2553
2554 asort($actionCMDs[$key]);
2555 $newValueArray = [];
2556 foreach ($actionCMDs[$key] as $idx => $order) {
2557 // Just one reflection here: It is clear that when removing elements from a flexform, then we will get lost
2558 // files unless we act on this delete operation by traversing and deleting files that were referred to.
2559 if ($order !== 'DELETE') {
2560 $newValueArray[$idx] = $valueArray[$idx];
2561 }
2562 unset($valueArray[$idx]);
2563 }
2564 $valueArray += $newValueArray;
2565 } elseif (is_array($actionCMDs[$key]) && isset($valueArray[$key])) {
2566 $this->_ACTION_FLEX_FORMdata($valueArray[$key], $actionCMDs[$key]);
2567 }
2568 }
2569 }
2570
2571 /**
2572 * Evaluates 'inline' type values.
2573 * (partly copied from the select_group function on this issue)
2574 *
2575 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2576 * @param string $value The value to set.
2577 * @param array $tcaFieldConf Field configuration from TCA
2578 * @param array $PP Additional parameters in a numeric array: $table,$id,$curValue,$status,$realPid,$recFID
2579 * @param string $field Field name
2580 * @param array $additionalData Additional data to be forwarded to sub-processors
2581 * @return array Modified $res array
2582 */
2583 public function checkValue_inline($res, $value, $tcaFieldConf, $PP, $field, array $additionalData = null)
2584 {
2585 list($table, $id, , $status) = $PP;
2586 $this->checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, $additionalData);
2587 }
2588
2589 /**
2590 * Evaluates 'inline' type values.
2591 * (partly copied from the select_group function on this issue)
2592 *
2593 * @param array $res The result array. The processed value (if any!) is set in the 'value' key.
2594 * @param string $value The value to set.
2595 * @param array $tcaFieldConf Field configuration from TCA
2596 * @param string $table Table name
2597 * @param int $id UID of record
2598 * @param string $status 'update' or 'new' flag
2599 * @param string $field Field name
2600 * @param array $additionalData Additional data to be forwarded to sub-processors
2601 * @return array|bool Modified $res array
2602 */
2603 public function checkValueForInline($res, $value, $tcaFieldConf, $table, $id, $status, $field, array $additionalData = null)
2604 {
2605 if (!$tcaFieldConf['foreign_table']) {
2606 // Fatal error, inline fields should always have a foreign_table defined
2607 return false;
2608 }
2609 // When values are sent they come as comma-separated values which are exploded by this function:
2610 $valueArray = GeneralUtility::trimExplode(',', $value);
2611 // Remove duplicates: (should not be needed)
2612 $valueArray = array_unique($valueArray);
2613 // Example for received data:
2614 // $value = 45,NEW4555fdf59d154,12,123
2615 // We need to decide whether we use the stack or can save the relation directly.
2616 if (!empty($value) && (strpos($value, 'NEW') !== false || !MathUtility::canBeInterpretedAsInteger($id))) {
2617 $this->remapStackRecords[$table][$id] = ['remapStackIndex' => count($this->remapStack)];
2618 $this->addNewValuesToRemapStackChildIds($valueArray);
2619 $this->remapStack[] = [
2620 'func' => 'checkValue_inline_processDBdata',
2621 'args' => [$valueArray, $tcaFieldConf, $id, $status, $table, $field, $additionalData],
2622 'pos' => ['valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 4],
2623 'additionalData' => $additionalData,
2624 'field' => $field,
2625 ];
2626 unset($res['value']);
2627 } elseif ($value || MathUtility::canBeInterpretedAsInteger($id)) {
2628 $res['value'] = $this->checkValue_inline_processDBdata($valueArray, $tcaFieldConf, $id, $status, $table, $field, $additionalData);
2629 }
2630 return $res;
2631 }
2632
2633 /**
2634 * Checks if a fields has more items than defined via TCA in maxitems.
2635 * If there are more items than allowed, the item list is truncated to the defined number.
2636 *
2637 * @param array $tcaFieldConf Field configuration from TCA
2638 * @param array $valueArray Current value array of items
2639 * @return array The truncated value array of items
2640 */
2641 public function checkValue_checkMax($tcaFieldConf, $valueArray)
2642 {
2643 // BTW, checking for min and max items here does NOT make any sense when MM is used because the above function
2644 // calls will just return an array with a single item (the count) if MM is used... Why didn't I perform the check
2645 // before? Probably because we could not evaluate the validity of record uids etc... Hmm...
2646 // NOTE to the comment: It's not really possible to check for too few items, because you must then determine first,
2647 // if the field is actual used regarding the CType.
2648 $maxitems = isset($tcaFieldConf['maxitems']) ? (int)$tcaFieldConf['maxitems'] : 99999;
2649 return array_slice($valueArray, 0, $maxitems);
2650 }
2651
2652 /*********************************************
2653 *
2654 * Helper functions for evaluation functions.
2655 *
2656 ********************************************/
2657 /**
2658 * Gets a unique value for $table/$id/$field based on $value
2659 *
2660 * @param string $table Table name
2661 * @param string $field Field name for which $value must be unique
2662 * @param string $value Value string.
2663 * @param int $id UID to filter out in the lookup (the record itself...)
2664 * @param int $newPid If set, the value will be unique for this PID
2665 * @return string Modified value (if not-unique). Will be the value appended with a number (until 100, then the function just breaks).
2666 */
2667 public function getUnique($table, $field, $value, $id, $newPid = 0)
2668 {
2669 // If the field is configured in TCA, proceed:
2670 if (is_array($GLOBALS['TCA'][$table]) && is_array($GLOBALS['TCA'][$table]['columns'][$field])) {
2671 $newValue = $value;
2672 $statement = $this->getUniqueCountStatement($newValue, $table, $field, (int)$id, (int)$newPid);
2673 // For as long as records with the test-value existing, try again (with incremented numbers appended)
2674 if ($statement->fetchColumn()) {
2675 for ($counter = 0; $counter <= 100; $counter++) {
2676 $newValue = $value . $counter;
2677 $statement->bindValue(1, $newValue);
2678 $statement->execute();
2679 if (!$statement->fetchColumn()) {
2680 break;
2681 }
2682 }
2683 }
2684 $value = $newValue;
2685 }
2686 return $value;
2687 }
2688
2689 /**
2690 * Gets the count of records for a unique field
2691 *
2692 * @param string $value The string value which should be unique
2693 * @param string $table Table name
2694 * @param string $field Field name for which $value must be unique
2695 * @param int $uid UID to filter out in the lookup (the record itself...)
2696 * @param int $pid If set, the value will be unique for this PID
2697 * @return \Doctrine\DBAL\Driver\Statement|int Return the prepared statement to check uniqueness
2698 */
2699 protected function getUniqueCountStatement(
2700 string $value,
2701 string $table,
2702 string $field,
2703 int $uid,
2704 int $pid
2705 ): Statement {
2706 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
2707 $this->addDeleteRestriction($queryBuilder->getRestrictions()->removeAll());
2708 $queryBuilder
2709 ->count('uid')
2710 ->from($table)
2711 ->where(
2712 $queryBuilder->expr()->eq($field, $queryBuilder->createPositionalParameter($value, \PDO::PARAM_STR)),
2713 $queryBuilder->expr()->neq('uid', $queryBuilder->createPositionalParameter($uid, \PDO::PARAM_INT))
2714 );
2715 if ($pid !== 0) {
2716 $queryBuilder->andWhere(
2717 $queryBuilder->expr()->eq('pid', $queryBuilder->createPositionalParameter($pid, \PDO::PARAM_INT))
2718 );
2719 } else {
2720 // pid>=0 for versioning
2721 $queryBuilder->andWhere(
2722 $queryBuilder->expr()->gte('pid', $queryBuilder->createPositionalParameter(0, \PDO::PARAM_INT))
2723 );
2724 }
2725
2726 return $queryBuilder->execute();
2727 }
2728
2729 /**
2730 * gets all records that have the same value in a field
2731 * excluding the given uid
2732 *
2733 * @param string $tableName Table name
2734 * @param int $uid UID to filter out in the lookup (the record itself...)
2735 * @param string $fieldName Field name for which $value must be unique
2736 * @param string $value Value string.
2737 * @param int $pageId If set, the value will be unique for this PID
2738 * @return array
2739 */
2740 public function getRecordsWithSameValue($tableName, $uid, $fieldName, $value, $pageId = 0)
2741 {
2742 $result = [];
2743 if (empty($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) {
2744 return $result;
2745 }
2746
2747 $uid = (int)$uid;
2748 $pageId = (int)$pageId;
2749
2750 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
2751 $queryBuilder->getRestrictions()
2752 ->removeAll()
2753 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
2754 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
2755
2756 $queryBuilder->select('*')
2757 ->from($tableName)
2758 ->where(
2759 $queryBuilder->expr()->eq(
2760 $fieldName,
2761 $queryBuilder->createNamedParameter($value, \PDO::PARAM_STR)
2762 ),
2763 $queryBuilder->expr()->neq(
2764 'uid',
2765 $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
2766 )
2767 );
2768
2769 if ($pageId) {
2770 $queryBuilder->andWhere(
2771 $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT))
2772 );
2773 } else {
2774 $queryBuilder->andWhere(
2775 $queryBuilder->expr()->gte('pid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
2776 );
2777 }
2778
2779 $result = $queryBuilder->execute()->fetchAll();
2780
2781 return $result;
2782 }
2783
2784 /**
2785 * @param string $value The field value to be evaluated
2786 * @param array $evalArray Array of evaluations to traverse.
2787 * @param string $is_in The "is_in" value of the field configuration from TCA
2788 * @return array
2789 */
2790 public function checkValue_text_Eval($value, $evalArray, $is_in)
2791 {
2792 $res = [];
2793 $set = true;
2794 foreach ($evalArray as $func) {
2795 switch ($func) {
2796 case 'trim':
2797 $value = trim($value);
2798 break;
2799 case 'required':
2800 if (!$value) {
2801 $set = false;
2802 }
2803 break;
2804 default:
2805 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
2806 if (class_exists($func)) {
2807 $evalObj = GeneralUtility::makeInstance($func);
2808 if (method_exists($evalObj, 'evaluateFieldValue')) {
2809 $value = $evalObj->evaluateFieldValue($value, $is_in, $set);
2810 }
2811 }
2812 }
2813 }
2814 }
2815 if ($set) {
2816 $res['value'] = $value;
2817 }
2818 return $res;
2819 }
2820
2821 /**
2822 * Evaluation of 'input'-type values based on 'eval' list
2823 *
2824 * @param string $value Value to evaluate
2825 * @param array $evalArray Array of evaluations to traverse.
2826 * @param string $is_in Is-in string for 'is_in' evaluation
2827 * @return array Modified $value in key 'value' or empty array
2828 */
2829 public function checkValue_input_Eval($value, $evalArray, $is_in)
2830 {
2831 $res = [];
2832 $set = true;
2833 foreach ($evalArray as $func) {
2834 switch ($func) {
2835 case 'int':
2836 case 'year':
2837 $value = (int)$value;
2838 break;
2839 case 'time':
2840 case 'timesec':
2841 // If $value is a pure integer we have the number of seconds, we can store that directly
2842 if ($value !== '' && !MathUtility::canBeInterpretedAsInteger($value)) {
2843 // $value is an ISO 8601 date
2844 $value = (new \DateTime($value))->getTimestamp();
2845 }
2846 break;
2847 case 'date':
2848 case 'datetime':
2849 // If $value is a pure integer we have the number of seconds, we can store that directly
2850 if ($value !== null && $value !== '' && !MathUtility::canBeInterpretedAsInteger($value)) {
2851 // The value we receive from JS is an ISO 8601 date, which is always in UTC. (the JS code works like that, on purpose!)
2852 // For instance "1999-11-11T11:11:11Z"
2853 // Since the user actually specifies the time in the server's local time, we need to mangle this
2854 // to reflect the server TZ. So we make this 1999-11-11T11:11:11+0200 (assuming Europe/Vienna here)
2855 // In the database we store the date in UTC (1999-11-11T09:11:11Z), hence we take the timestamp of this converted value.
2856 // For achieving this we work with timestamps only (which are UTC) and simply adjust it for the
2857 // TZ difference.
2858 try {
2859 // Make the date from JS a timestamp
2860 $value = (new \DateTime($value))->getTimestamp();
2861 } catch (\Exception $e) {
2862 // set the default timezone value to achieve the value of 0 as a result
2863 $value = (int)date('Z', 0);
2864 }
2865
2866 // @todo this hacky part is problematic when it comes to times around DST switch! Add test to prove that this is broken.
2867 $value -= date('Z', $value);
2868 }
2869 break;
2870 case 'double2':
2871 $value = preg_replace('/[^0-9,\\.-]/', '', $value);
2872 $negative = $value[0] === '-';
2873 $value = strtr($value, [',' => '.', '-' => '']);
2874 if (strpos($value, '.') === false) {
2875 $value .= '.0';
2876 }
2877 $valueArray = explode('.', $value);
2878 $dec = array_pop($valueArray);
2879 $value = implode('', $valueArray) . '.' . $dec;
2880 if ($negative) {
2881 $value *= -1;
2882 }
2883 $value = number_format($value, 2, '.', '');
2884 break;
2885 case 'md5':
2886 if (strlen($value) !== 32) {
2887 $set = false;
2888 }
2889 break;
2890 case 'trim':
2891 $value = trim($value);
2892 break;
2893 case 'upper':
2894 $value = mb_strtoupper($value, 'utf-8');
2895 break;
2896 case 'lower':
2897 $value = mb_strtolower($value, 'utf-8');
2898 break;
2899 case 'required':
2900 if (!isset($value) || $value === '') {
2901 $set = false;
2902 }
2903 break;
2904 case 'is_in':
2905 $c = mb_strlen($value);
2906 if ($c) {
2907 $newVal = '';
2908 for ($a = 0; $a < $c; $a++) {
2909 $char = mb_substr($value, $a, 1);
2910 if (mb_strpos($is_in, $char) !== false) {
2911 $newVal .= $char;
2912 }
2913 }
2914 $value = $newVal;
2915 }
2916 break;
2917 case 'nospace':
2918 $value = str_replace(' ', '', $value);
2919 break;
2920 case 'alpha':
2921 $value = preg_replace('/[^a-zA-Z]/', '', $value);
2922 break;
2923 case 'num':
2924 $value = preg_replace('/[^0-9]/', '', $value);
2925 break;
2926 case 'alphanum':
2927 $value = preg_replace('/[^a-zA-Z0-9]/', '', $value);
2928 break;
2929 case 'alphanum_x':
2930 $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value);
2931 break;
2932 case 'domainname':
2933 if (!preg_match('/^[a-z0-9.\\-]*$/i', $value)) {
2934 $value = GeneralUtility::idnaEncode($value);
2935 }
2936 break;
2937 case 'email':
2938 if ((string)$value !== '') {
2939 $this->checkValue_input_ValidateEmail($value, $set);
2940 }
2941 break;