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