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