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