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