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