forked from kitodo/kitodo-presentation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelper.php
945 lines (876 loc) · 30.8 KB
/
Helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
<?php
/**
* (c) Kitodo. Key to digital objects e.V. <[email protected]>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Kitodo\Dlf\Common;
use TYPO3\CMS\Core\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
/**
* Helper class for the 'dlf' extension
*
* @package TYPO3
* @subpackage dlf
*
* @access public
*/
class Helper
{
/**
* @access public
* @static
* @var string The extension key
*/
public static string $extKey = 'dlf';
/**
* @access protected
* @static
* @var string This holds the cipher algorithm
*
* @see openssl_get_cipher_methods() for options
*/
protected static string $cipherAlgorithm = 'aes-256-ctr';
/**
* @access protected
* @static
* @var string This holds the hash algorithm
*
* @see openssl_get_md_methods() for options
*/
protected static string $hashAlgorithm = 'sha256';
/**
* @access protected
* @static
* @var array The locallang array for flash messages
*/
protected static array $messages = [];
/**
* Generates a flash message and adds it to a message queue.
*
* @access public
*
* @static
*
* @param string $message The body of the message
* @param string $title The title of the message
* @param int $severity The message's severity
* @param bool $session Should the message be saved in the user's session?
* @param string $queue The queue's unique identifier
*
* @return FlashMessageQueue The queue the message was added to
*/
public static function addMessage(string $message, string $title, int $severity, bool $session = false, string $queue = 'kitodo.default.flashMessages'): FlashMessageQueue
{
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$flashMessageQueue = $flashMessageService->getMessageQueueByIdentifier($queue);
$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
$message,
$title,
$severity,
$session
);
$flashMessageQueue->enqueue($flashMessage);
return $flashMessageQueue;
}
/**
* Check if given identifier is a valid identifier of the German National Library
*
* @access public
*
* @static
*
* @param string $id The identifier to check
* @param string $type What type is the identifier supposed to be? Possible values: PPN, IDN, PND, ZDB, SWD, GKD
*
* @return bool Is $id a valid GNL identifier of the given $type?
*/
public static function checkIdentifier(string $id, string $type): bool
{
$digits = substr($id, 0, 8);
$checksum = self::getChecksum($digits);
switch (strtoupper($type)) {
case 'PPN':
case 'IDN':
case 'PND':
if ($checksum == 10) {
$checksum = 'X';
}
if (!preg_match('/\d{8}[\dX]{1}/i', $id)) {
return false;
} elseif (strtoupper(substr($id, -1, 1)) != $checksum) {
return false;
}
break;
case 'ZDB':
if ($checksum == 10) {
$checksum = 'X';
}
if (!preg_match('/\d{8}-[\dX]{1}/i', $id)) {
return false;
} elseif (strtoupper(substr($id, -1, 1)) != $checksum) {
return false;
}
break;
case 'SWD':
$checksum = 11 - $checksum;
if (!preg_match('/\d{8}-\d{1}/i', $id)) {
return false;
} elseif ($checksum == 10) {
//TODO: Binary operation "+" between string and 1 results in an error.
// @phpstan-ignore-next-line
return self::checkIdentifier(($digits + 1) . substr($id, -2, 2), 'SWD');
} elseif (substr($id, -1, 1) != $checksum) {
return false;
}
break;
case 'GKD':
$checksum = 11 - $checksum;
if ($checksum == 10) {
$checksum = 'X';
}
if (!preg_match('/\d{8}-[\dX]{1}/i', $id)) {
return false;
} elseif (strtoupper(substr($id, -1, 1)) != $checksum) {
return false;
}
break;
}
return true;
}
/**
* Get checksum for given digits.
*
* @access private
*
* @static
*
* @param string $digits
*
* @return int
*/
private static function getChecksum(string $digits): int
{
$checksum = 0;
for ($i = 0, $j = strlen($digits); $i < $j; $i++) {
$checksum += (9 - $i) * (int) substr($digits, $i, 1);
}
return (11 - ($checksum % 11)) % 11;
}
/**
* Decrypt encrypted value with given control hash
*
* @access public
*
* @static
*
* @param string $encrypted The encrypted value to decrypt
*
* @return mixed The decrypted value or false on error
*/
public static function decrypt(string $encrypted)
{
if (
!in_array(self::$cipherAlgorithm, openssl_get_cipher_methods(true))
|| !in_array(self::$hashAlgorithm, openssl_get_md_methods(true))
) {
self::log('OpenSSL library doesn\'t support cipher and/or hash algorithm', LOG_SEVERITY_ERROR);
return false;
}
if (empty(self::getEncryptionKey())) {
self::log('No encryption key set in TYPO3 configuration', LOG_SEVERITY_ERROR);
return false;
}
if (
empty($encrypted)
|| strlen($encrypted) < openssl_cipher_iv_length(self::$cipherAlgorithm)
) {
self::log('Invalid parameters given for decryption', LOG_SEVERITY_ERROR);
return false;
}
// Split initialisation vector and encrypted data.
$binary = base64_decode($encrypted);
$iv = substr($binary, 0, openssl_cipher_iv_length(self::$cipherAlgorithm));
$data = substr($binary, openssl_cipher_iv_length(self::$cipherAlgorithm));
$key = openssl_digest(self::getEncryptionKey(), self::$hashAlgorithm, true);
// Decrypt data.
return openssl_decrypt($data, self::$cipherAlgorithm, $key, OPENSSL_RAW_DATA, $iv);
}
/**
* Try to parse $content into a `SimpleXmlElement`. If $content is not a
* string or does not contain valid XML, `false` is returned.
*
* @access public
*
* @static
*
* @param mixed $content content of file to read
*
* @return \SimpleXMLElement|false
*/
public static function getXmlFileAsString($content)
{
// Don't make simplexml_load_string throw (when $content is an array
// or object)
if (!is_string($content)) {
return false;
}
// Turn off libxml's error logging.
$libxmlErrors = libxml_use_internal_errors(true);
if (\PHP_VERSION_ID < 80000) {
// Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
$previousValueOfEntityLoader = libxml_disable_entity_loader(true);
}
// Try to load XML from file.
$xml = simplexml_load_string($content);
if (\PHP_VERSION_ID < 80000) {
// reset entity loader setting
libxml_disable_entity_loader($previousValueOfEntityLoader);
}
// Reset libxml's error logging.
libxml_use_internal_errors($libxmlErrors);
return $xml;
}
/**
* Add a message to the TYPO3 log
*
* @access public
*
* @static
*
* @param string $message The message to log
* @param int $severity The severity of the message 0 is info, 1 is notice, 2 is warning, 3 is fatal error, -1 is "OK" message
*
* @return void
*/
public static function log(string $message, int $severity = 0): void
{
$logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(get_called_class());
switch ($severity) {
case 0:
$logger->info($message);
break;
case 1:
$logger->notice($message);
break;
case 2:
$logger->warning($message);
break;
case 3:
$logger->error($message);
break;
default:
break;
}
}
/**
* Digest the given string
*
* @access public
*
* @static
*
* @param string $string The string to encrypt
*
* @return mixed Hashed string or false on error
*/
public static function digest(string $string)
{
if (!in_array(self::$hashAlgorithm, openssl_get_md_methods(true))) {
self::log('OpenSSL library doesn\'t support hash algorithm', LOG_SEVERITY_ERROR);
return false;
}
// Hash string.
return openssl_digest($string, self::$hashAlgorithm);
}
/**
* Encrypt the given string
*
* @access public
*
* @static
*
* @param string $string The string to encrypt
*
* @return mixed Encrypted string or false on error
*/
public static function encrypt(string $string)
{
if (
!in_array(self::$cipherAlgorithm, openssl_get_cipher_methods(true))
|| !in_array(self::$hashAlgorithm, openssl_get_md_methods(true))
) {
self::log('OpenSSL library doesn\'t support cipher and/or hash algorithm', LOG_SEVERITY_ERROR);
return false;
}
if (empty(self::getEncryptionKey())) {
self::log('No encryption key set in TYPO3 configuration', LOG_SEVERITY_ERROR);
return false;
}
// Generate random initialization vector.
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::$cipherAlgorithm));
$key = openssl_digest(self::getEncryptionKey(), self::$hashAlgorithm, true);
// Encrypt data.
$encrypted = openssl_encrypt($string, self::$cipherAlgorithm, $key, OPENSSL_RAW_DATA, $iv);
// Merge initialization vector and encrypted data.
if ($encrypted !== false) {
$encrypted = base64_encode($iv . $encrypted);
}
return $encrypted;
}
/**
* Clean up a string to use in an URL.
*
* @access public
*
* @static
*
* @param string $string The string to clean up
*
* @return string The cleaned up string
*/
public static function getCleanString(string $string): string
{
// Convert to lowercase.
$string = strtolower($string);
// Remove non-alphanumeric characters.
$string = preg_replace('/[^a-z\d_\s-]/', '', $string);
// Remove multiple dashes or whitespaces.
$string = preg_replace('/[\s-]+/', ' ', $string);
// Convert whitespaces and underscore to dash.
return preg_replace('/[\s_]/', '-', $string);
}
/**
* Get the registered hook objects for a class
*
* @access public
*
* @static
*
* @param string $scriptRelPath The path to the class file
*
* @return array Array of hook objects for the class
*/
public static function getHookObjects(string $scriptRelPath): array
{
$hookObjects = [];
if (is_array(self::getOptions()[self::$extKey . '/' . $scriptRelPath]['hookClass'])) {
foreach (self::getOptions()[self::$extKey . '/' . $scriptRelPath]['hookClass'] as $classRef) {
$hookObjects[] = GeneralUtility::makeInstance($classRef);
}
}
return $hookObjects;
}
/**
* Get the "index_name" for an UID
*
* @access public
*
* @static
*
* @param int $uid The UID of the record
* @param string $table Get the "index_name" from this table
* @param int $pid Get the "index_name" from this page
*
* @return string "index_name" for the given UID
*/
public static function getIndexNameFromUid(int $uid, string $table, int $pid = -1): string
{
// Sanitize input.
$uid = max($uid, 0);
if (
!$uid
// NOTE: Only use tables that don't have too many entries!
|| !in_array($table, ['tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_metadatasubentries', 'tx_dlf_structures', 'tx_dlf_solrcores'])
) {
self::log('Invalid UID "' . $uid . '" or table "' . $table . '"', LOG_SEVERITY_ERROR);
return '';
}
$makeCacheKey = function ($pid, $uid) {
return $pid . '.' . $uid;
};
static $cache = [];
if (!isset($cache[$table])) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($table);
$result = $queryBuilder
->select(
$table . '.index_name AS index_name',
$table . '.uid AS uid',
$table . '.pid AS pid',
)
->from($table)
->execute();
$cache[$table] = [];
while ($row = $result->fetchAssociative()) {
$cache[$table][$makeCacheKey($row['pid'], $row['uid'])]
= $cache[$table][$makeCacheKey(-1, $row['uid'])]
= $row['index_name'];
}
}
$cacheKey = $makeCacheKey($pid, $uid);
$result = $cache[$table][$cacheKey] ?? '';
if ($result === '') {
self::log('No "index_name" with UID ' . $uid . ' and PID ' . $pid . ' found in table "' . $table . '"', LOG_SEVERITY_WARNING);
}
return $result;
}
/**
* Get language name from ISO code
*
* @access public
*
* @static
*
* @param string $code ISO 639-1 or ISO 639-2/B language code
*
* @return string Localized full name of language or unchanged input
*/
public static function getLanguageName(string $code): string
{
// Analyze code and set appropriate ISO table.
$isoCode = strtolower(trim($code));
if (preg_match('/^[a-z]{3}$/', $isoCode)) {
$file = 'EXT:dlf/Resources/Private/Data/iso-639-2b.xlf';
} elseif (preg_match('/^[a-z]{2}$/', $isoCode)) {
$file = 'EXT:dlf/Resources/Private/Data/iso-639-1.xlf';
} else {
// No ISO code, return unchanged.
return $code;
}
$lang = LocalizationUtility::translate('LLL:' . $file . ':' . $code);
if (!empty($lang)) {
return $lang;
} else {
self::log('Language code "' . $code . '" not found in ISO-639 table', LOG_SEVERITY_NOTICE);
return $code;
}
}
/**
* Get all document structures as array
*
* @access public
*
* @static
*
* @param int $pid Get the "index_name" from this page only
*
* @return array
*/
public static function getDocumentStructures(int $pid = -1): array
{
// TODO: Against redundancy with getIndexNameFromUid
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_dlf_structures');
$where = '';
// Should we check for a specific PID, too?
if ($pid !== -1) {
$pid = max($pid, 0);
$where = $queryBuilder->expr()->eq('tx_dlf_structures.pid', $pid);
}
// Fetch document info for UIDs in $documentSet from DB
$kitodoStructures = $queryBuilder
->select(
'tx_dlf_structures.uid AS uid',
'tx_dlf_structures.index_name AS indexName'
)
->from('tx_dlf_structures')
->where($where)
->execute();
$allStructures = $kitodoStructures->fetchAllAssociative();
// make lookup-table indexName -> uid
return array_column($allStructures, 'indexName', 'uid');
}
/**
* Determine whether or not $url is a valid URL using HTTP or HTTPS scheme.
*
* @access public
*
* @static
*
* @param string $url
*
* @return bool
*/
public static function isValidHttpUrl(string $url): bool
{
if (!GeneralUtility::isValidUrl($url)) {
return false;
}
try {
$uri = new Uri($url);
return !empty($uri->getScheme());
} catch (\InvalidArgumentException $e) {
self::log($e->getMessage(), LOG_SEVERITY_ERROR);
return false;
}
}
/**
* Process a data and/or command map with TYPO3 core engine as admin.
*
* @access public
*
* @param array $data Data map
* @param array $cmd Command map
* @param bool $reverseOrder Should the data map be reversed?
* @param bool $cmdFirst Should the command map be processed first?
*
* @return array Array of substituted "NEW..." identifiers and their actual UIDs.
*/
public static function processDatabaseAsAdmin(array $data = [], array $cmd = [], $reverseOrder = false, $cmdFirst = false)
{
$context = GeneralUtility::makeInstance(Context::class);
if (
\TYPO3_MODE === 'BE'
&& $context->getPropertyFromAspect('backend.user', 'isAdmin')
) {
// Instantiate TYPO3 core engine.
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
// We do not use workspaces and have to bypass restrictions in DataHandler.
$dataHandler->bypassWorkspaceRestrictions = true;
// Load data and command arrays.
$dataHandler->start($data, $cmd);
// Process command map first if default order is reversed.
if (
!empty($cmd)
&& $cmdFirst
) {
$dataHandler->process_cmdmap();
}
// Process data map.
if (!empty($data)) {
$dataHandler->reverseOrder = $reverseOrder;
$dataHandler->process_datamap();
}
// Process command map if processing order is not reversed.
if (
!empty($cmd)
&& !$cmdFirst
) {
$dataHandler->process_cmdmap();
}
return $dataHandler->substNEWwithIDs;
} else {
self::log('Current backend user has no admin privileges', LOG_SEVERITY_ERROR);
return [];
}
}
/**
* Fetches and renders all available flash messages from the queue.
*
* @access public
*
* @static
*
* @param string $queue The queue's unique identifier
*
* @return string All flash messages in the queue rendered as HTML.
*/
public static function renderFlashMessages(string $queue = 'kitodo.default.flashMessages'): string
{
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$flashMessageQueue = $flashMessageService->getMessageQueueByIdentifier($queue);
$flashMessages = $flashMessageQueue->getAllMessagesAndFlush();
return GeneralUtility::makeInstance(KitodoFlashMessageRenderer::class)
->render($flashMessages);
}
/**
* This translates an internal "index_name"
*
* @access public
*
* @static
*
* @param string $indexName The internal "index_name" to translate
* @param string $table Get the translation from this table
* @param string $pid Get the translation from this page
*
* @return string Localized label for $indexName
*/
public static function translate(string $indexName, string $table, string $pid): string
{
// Load labels into static variable for future use.
static $labels = [];
// Sanitize input.
$pid = max((int) $pid, 0);
if (!$pid) {
self::log('Invalid PID ' . $pid . ' for translation', LOG_SEVERITY_WARNING);
return $indexName;
}
/** @var PageRepository $pageRepository */
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);
$languageAspect = GeneralUtility::makeInstance(Context::class)->getAspect('language');
$languageContentId = $languageAspect->getContentId();
// Check if "index_name" is an UID.
if (MathUtility::canBeInterpretedAsInteger($indexName)) {
$indexName = self::getIndexNameFromUid((int) $indexName, $table, $pid);
}
/* $labels already contains the translated content element, but with the index_name of the translated content element itself
* and not with the $indexName of the original that we receive here. So we have to determine the index_name of the
* associated translated content element. E.g. $labels['title0'] != $indexName = title. */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($table);
// First fetch the uid of the received index_name
$result = $queryBuilder
->select(
$table . '.uid AS uid',
$table . '.l18n_parent AS l18n_parent'
)
->from($table)
->where(
$queryBuilder->expr()->eq($table . '.pid', $pid),
$queryBuilder->expr()->eq($table . '.index_name', $queryBuilder->expr()->literal($indexName)),
self::whereExpression($table, true)
)
->setMaxResults(1)
->execute();
$row = $result->fetchAssociative();
if ($row) {
// Now we use the uid of the l18_parent to fetch the index_name of the translated content element.
$result = $queryBuilder
->select($table . '.index_name AS index_name')
->from($table)
->where(
$queryBuilder->expr()->eq($table . '.pid', $pid),
$queryBuilder->expr()->eq($table . '.uid', $row['l18n_parent']),
$queryBuilder->expr()->eq($table . '.sys_language_uid', (int) $languageContentId),
self::whereExpression($table, true)
)
->setMaxResults(1)
->execute();
$row = $result->fetchAssociative();
if ($row) {
// If there is an translated content element, overwrite the received $indexName.
$indexName = $row['index_name'];
}
}
// Check if we already got a translation.
if (empty($labels[$table][$pid][$languageContentId][$indexName])) {
// Check if this table is allowed for translation.
if (in_array($table, ['tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_metadatasubentries', 'tx_dlf_structures'])) {
$additionalWhere = $queryBuilder->expr()->in($table . '.sys_language_uid', [-1, 0]);
if ($languageContentId > 0) {
$additionalWhere = $queryBuilder->expr()->andX(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->in($table . '.sys_language_uid', [-1, 0]),
$queryBuilder->expr()->eq($table . '.sys_language_uid', (int) $languageContentId)
),
$queryBuilder->expr()->eq($table . '.l18n_parent', 0)
);
}
// Get labels from database.
$result = $queryBuilder
->select('*')
->from($table)
->where(
$queryBuilder->expr()->eq($table . '.pid', $pid),
$additionalWhere,
self::whereExpression($table, true)
)
->setMaxResults(10000)
->execute();
if ($result->rowCount() > 0) {
while ($resArray = $result->fetchAssociative()) {
// Overlay localized labels if available.
if ($languageContentId > 0) {
$resArray = $pageRepository->getRecordOverlay($table, $resArray, $languageContentId, $languageAspect->getLegacyOverlayType());
}
if ($resArray) {
$labels[$table][$pid][$languageContentId][$resArray['index_name']] = $resArray['label'];
}
}
} else {
self::log('No translation with PID ' . $pid . ' available in table "' . $table . '" or translation not accessible', LOG_SEVERITY_NOTICE);
}
} else {
self::log('No translations available for table "' . $table . '"', LOG_SEVERITY_WARNING);
}
}
if (!empty($labels[$table][$pid][$languageContentId][$indexName])) {
return $labels[$table][$pid][$languageContentId][$indexName];
} else {
return $indexName;
}
}
/**
* This returns the additional WHERE expression of a table based on its TCA configuration
*
* @access public
*
* @static
*
* @param string $table Table name as defined in TCA
* @param bool $showHidden Ignore the hidden flag?
*
* @return string Additional WHERE expression
*/
public static function whereExpression(string $table, bool $showHidden = false): string
{
// TODO: Check with applicationType; TYPO3_MODE is removed in v12
if (\TYPO3_MODE === 'FE') {
// Should we ignore the record's hidden flag?
$ignoreHide = 0;
if ($showHidden) {
$ignoreHide = 1;
}
/** @var PageRepository $pageRepository */
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);
$expression = $pageRepository->enableFields($table, $ignoreHide);
if (!empty($expression)) {
return substr($expression, 5);
} else {
return '';
}
// TODO: Check with applicationType; TYPO3_MODE is removed in v12
} elseif (\TYPO3_MODE === 'BE') {
return GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($table)
->expr()
->eq($table . '.' . $GLOBALS['TCA'][$table]['ctrl']['delete'], 0);
} else {
self::log('Unexpected TYPO3_MODE', LOG_SEVERITY_ERROR);
return '1=-1';
}
}
/**
* Prevent instantiation by hiding the constructor
*
* @access private
*
* @return void
*/
private function __construct()
{
// This is a static class, thus no instances should be created.
}
/**
* Returns the LanguageService
*
* @access public
*
* @static
*
* @return LanguageService
*/
public static function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
/**
* Replacement for the TYPO3 GeneralUtility::getUrl().
*
* This method respects the User Agent settings from extConf
*
* @access public
*
* @static
*
* @param string $url
*
* @return string|bool
*/
public static function getUrl(string $url)
{
if (!Helper::isValidHttpUrl($url)) {
return false;
}
// Get extension configuration.
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf', 'general');
/** @var RequestFactory $requestFactory */
$requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
$configuration = [
'timeout' => 30,
'headers' => [
'User-Agent' => $extConf['userAgent'] ?? 'Kitodo.Presentation Proxy',
],
];
try {
$response = $requestFactory->request($url, 'GET', $configuration);
} catch (\Exception $e) {
self::log('Could not fetch data from URL "' . $url . '". Error: ' . $e->getMessage() . '.', LOG_SEVERITY_WARNING);
return false;
}
return $response->getBody()->getContents();
}
/**
* Check if given value is a valid XML ID.
* @see https://www.w3.org/TR/xmlschema-2/#ID
*
* @access public
*
* @static
*
* @param mixed $id The ID value to check
*
* @return bool TRUE if $id is valid XML ID, FALSE otherwise
*/
public static function isValidXmlId($id): bool
{
return preg_match('/^[_a-z][_a-z0-9-.]*$/i', $id) === 1;
}
/**
* Get options from local configuration.
*
* @access private
*
* @static
*
* @return array
*/
private static function getOptions(): array
{
return self::getLocalConfigurationByPath('SC_OPTIONS');
}
/**
* Get encryption key from local configuration.
*
* @access private
*
* @static
*
* @return string|null
*/
private static function getEncryptionKey(): ?string
{
return self::getLocalConfigurationByPath('SYS/encryptionKey');
}
/**
* Get local configuration for given path.
*
* @access private
*
* @static
*
* @param string $path
*
* @return mixed
*/
private static function getLocalConfigurationByPath(string $path)
{
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
if (array_key_exists(strtok($path, '/'), $configurationManager->getLocalConfiguration())) {
return $configurationManager->getLocalConfigurationValueByPath($path);
}
return ArrayUtility::getValueByPath($GLOBALS['TYPO3_CONF_VARS'], $path);
}
}