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