Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 118576...18bf9c )
by Sebastian
14:55
created

tx_dlf_helper::whereClause()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 48
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 8
nop 2
dl 0
loc 48
rs 5.9322
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
/**
13
 * Helper class 'tx_dlf_helper' for the 'dlf' extension.
14
 *
15
 * @author	Sebastian Meyer <[email protected]>
16
 * @author	Henrik Lochmann <[email protected]>
17
 * @package	TYPO3
18
 * @subpackage	tx_dlf
19
 * @access	public
20
 */
21
class tx_dlf_helper {
22
23
    /**
24
     * The extension key
25
     *
26
     * @var	string
27
     * @access public
28
     */
29
    public static $extKey = 'dlf';
30
31
    /**
32
     * The locallang array for common use
33
     *
34
     * @var	array
35
     * @access protected
36
     */
37
    protected static $locallang = array ();
38
39
    /**
40
     * Adds a message to the message queue.
41
     *
42
     * @access	public
43
     *
44
     * @param	\TYPO3\CMS\Core\Messaging\FlashMessage		$message: Instance of \TYPO3\CMS\Core\Messaging\FlashMessage
45
     *
46
     * @return	void
47
     */
48
    public static function addMessage($message) {
49
50
        $flashMessageService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
51
52
        $flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
53
54
    }
55
56
    /**
57
     * Implements array_merge_recursive_overrule() in a cross-version way
58
     * This code is a copy from realurl, written by Dmitry Dulepov <[email protected]>.
59
     *
60
     * @access	public
61
     *
62
     * @param	array		$array1: First array
63
     * @param	array		$array2: Second array
64
     *
65
     * @return	array		Merged array with second array overruling first one
66
     */
67
    static public function array_merge_recursive_overrule($array1, $array2) {
68
69
        if (class_exists('\\TYPO3\\CMS\\Core\\Utility\\ArrayUtility')) {
70
71
            \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($array1, $array2);
72
73
        } else {
74
75
            $array1 = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($array1, $array2);
0 ignored issues
show
Bug introduced by
The method array_merge_recursive_overrule() does not exist on TYPO3\CMS\Core\Utility\GeneralUtility. Did you maybe mean array_merge()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            /** @scrutinizer ignore-call */ 
76
            $array1 = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($array1, $array2);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
77
        }
78
79
        return $array1;
80
81
    }
82
83
    /**
84
     * Check if given identifier is a valid identifier of the German National Library
85
     * @see	http://support.d-nb.de/iltis/onlineRoutinen/Pruefziffernberechnung.htm
86
     *
87
     * @access	public
88
     *
89
     * @param	string		$id: The identifier to check
90
     * @param	string		$type: What type is the identifier supposed to be?
91
     * 						Possible values: PPN, IDN, PND, ZDB, SWD, GKD
92
     *
93
     * @return	boolean		Is $id a valid GNL identifier of the given $type?
94
     */
95
    public static function checkIdentifier($id, $type) {
96
97
        $digits = substr($id, 0, 8);
98
99
        $checksum = 0;
100
101
        for ($i = 0, $j = strlen($digits); $i < $j; $i++) {
102
103
            $checksum += (9 - $i) * intval(substr($digits, $i, 1));
104
105
        }
106
107
        $checksum = (11 - ($checksum % 11)) % 11;
108
109
        switch (strtoupper($type)) {
110
111
            case 'PPN':
112
            case 'IDN':
113
            case 'PND':
114
115
                if ($checksum == 10) {
116
117
                    $checksum = 'X';
118
119
                }
120
121
                if (!preg_match('/[0-9]{8}[0-9X]{1}/i', $id)) {
122
123
                    return FALSE;
124
125
                } elseif (strtoupper(substr($id, -1, 1)) != $checksum) {
126
127
                    return FALSE;
128
129
                }
130
131
                break;
132
133
            case 'ZDB':
134
135
                if ($checksum == 10) {
136
137
                    $checksum = 'X';
138
139
                }
140
141
                if (!preg_match('/[0-9]{8}-[0-9X]{1}/i', $id)) {
142
143
                    return FALSE;
144
145
                } elseif (strtoupper(substr($id, -1, 1)) != $checksum) {
146
147
                    return FALSE;
148
149
                }
150
151
                break;
152
153
            case 'SWD':
154
155
                $checksum = 11 - $checksum;
156
157
                if (!preg_match('/[0-9]{8}-[0-9]{1}/i', $id)) {
158
159
                    return FALSE;
160
161
                } elseif ($checksum == 10) {
162
163
                    return self::checkIdentifier(($digits + 1).substr($id, -2, 2), 'SWD');
164
165
                } elseif (substr($id, -1, 1) != $checksum) {
166
167
                    return FALSE;
168
169
                }
170
171
                break;
172
173
            case 'GKD':
174
175
                $checksum = 11 - $checksum;
176
177
                if ($checksum == 10) {
178
179
                    $checksum = 'X';
180
181
                }
182
183
                if (!preg_match('/[0-9]{8}-[0-9X]{1}/i', $id)) {
184
185
                    return FALSE;
186
187
                } elseif (strtoupper(substr($id, -1, 1)) != $checksum) {
188
189
                    return FALSE;
190
191
                }
192
193
                break;
194
195
        }
196
197
        return TRUE;
198
199
    }
200
201
    /**
202
     * Decrypt encrypted value with given control hash
203
     * @see http://yavkata.co.uk/weblog/php/securing-html-hidden-input-fields-using-encryption-and-hashing/
204
     *
205
     * @access	public
206
     *
207
     * @param	string		$encrypted: The encrypted value to decrypt
208
     * @param	string		$hash: The control hash for decrypting
209
     *
210
     * @return	mixed		The decrypted value or NULL on error
211
     */
212
    public static function decrypt($encrypted, $hash) {
213
214
        $decrypted = NULL;
0 ignored issues
show
Unused Code introduced by
The assignment to $decrypted is dead and can be removed.
Loading history...
215
216
        // Check for PHP extension "mcrypt".
217
        if (!extension_loaded('mcrypt')) {
218
219
            if (TYPO3_DLOG) {
220
221
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->decrypt('.$encrypted.', '.$hash.')] PHP extension "mcrypt" not available', self::$extKey, SYSLOG_SEVERITY_WARNING);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
222
223
            }
224
225
            return;
226
227
        }
228
229
        if (empty($encrypted) || empty($hash)) {
230
231
            if (TYPO3_DLOG) {
232
233
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->decrypt('.$encrypted.', '.$hash.')] Invalid parameters given for decryption', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
234
235
            }
236
237
            return;
238
239
        }
240
241
        if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
242
243
            if (TYPO3_DLOG) {
244
245
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->decrypt('.$encrypted.', '.$hash.')] No encryption key set in TYPO3 configuration', self::$extKey, SYSLOG_SEVERITY_ERROR);
246
247
            }
248
249
            return;
250
251
        }
252
253
        $iv = substr(md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']), 0, mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CFB));
0 ignored issues
show
Deprecated Code introduced by
The function mcrypt_get_iv_size() has been deprecated: 7.1 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

253
        $iv = substr(md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']), 0, /** @scrutinizer ignore-deprecated */ mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CFB));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
254
255
        $decrypted = mcrypt_decrypt(MCRYPT_BLOWFISH, substr($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], 0, 56), base64_decode($encrypted), MCRYPT_MODE_CFB, $iv);
0 ignored issues
show
Deprecated Code introduced by
The function mcrypt_decrypt() has been deprecated: 7.1 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

255
        $decrypted = /** @scrutinizer ignore-deprecated */ mcrypt_decrypt(MCRYPT_BLOWFISH, substr($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], 0, 56), base64_decode($encrypted), MCRYPT_MODE_CFB, $iv);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
256
257
        $salt = substr($hash, 0, 10);
258
259
        $hashed = $salt.substr(sha1($salt.$decrypted), -10);
260
261
        if ($hashed !== $hash) {
262
263
            if (TYPO3_DLOG) {
264
265
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->decrypt('.$encrypted.', '.$hash.')] Invalid hash "'.$hash.'" given for decryption', self::$extKey, SYSLOG_SEVERITY_WARNING);
266
267
            }
268
269
            return;
270
271
        }
272
273
        return $decrypted;
274
275
    }
276
277
    /**
278
     * Encrypt the given string
279
     * @see http://yavkata.co.uk/weblog/php/securing-html-hidden-input-fields-using-encryption-and-hashing/
280
     *
281
     * @access	public
282
     *
283
     * @param	string		$string: The string to encrypt
284
     *
285
     * @return	array		Array with encrypted string and control hash
286
     */
287
    public static function encrypt($string) {
288
289
        // Check for PHP extension "mcrypt".
290
        if (!extension_loaded('mcrypt')) {
291
292
            if (TYPO3_DLOG) {
293
294
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->encrypt('.$string.')] PHP extension "mcrypt" not available', self::$extKey, SYSLOG_SEVERITY_WARNING);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
295
296
            }
297
298
            return;
299
300
        }
301
302
        if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
303
304
            if (TYPO3_DLOG) {
305
306
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->encrypt('.$string.')] No encryption key set in TYPO3 configuration', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
307
308
            }
309
310
            return;
311
312
        }
313
314
        $iv = substr(md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']), 0, mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CFB));
0 ignored issues
show
Deprecated Code introduced by
The function mcrypt_get_iv_size() has been deprecated: 7.1 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

314
        $iv = substr(md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']), 0, /** @scrutinizer ignore-deprecated */ mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CFB));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
315
316
        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH, substr($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], 0, 56), $string, MCRYPT_MODE_CFB, $iv));
0 ignored issues
show
Deprecated Code introduced by
The function mcrypt_encrypt() has been deprecated: 7.1 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

316
        $encrypted = base64_encode(/** @scrutinizer ignore-deprecated */ mcrypt_encrypt(MCRYPT_BLOWFISH, substr($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], 0, 56), $string, MCRYPT_MODE_CFB, $iv));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
317
318
        $salt = substr(md5(uniqid(rand(), TRUE)), 0, 10);
319
320
        $hash = $salt.substr(sha1($salt.$string), -10);
321
322
        return array ('encrypted' => $encrypted, 'hash' => $hash);
323
324
    }
325
326
    /**
327
     * Get a backend user object (even in frontend mode)
328
     *
329
     * @access	public
330
     *
331
     * @return	\TYPO3\CMS\Core\Authentication\BackendUserAuthentication		Instance of \TYPO3\CMS\Core\Authentication\BackendUserAuthentication or NULL on failure
332
     */
333
    public static function getBeUser() {
334
335
        if (TYPO3_MODE === 'FE' || TYPO3_MODE === 'BE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'BE' is always true.
Loading history...
336
337
            // Initialize backend session with CLI user's rights.
338
            $userObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
339
340
            $userObj->dontSetCookie = TRUE;
341
342
            $userObj->start();
343
344
            $userObj->setBeUserByName('_cli_dlf');
345
346
            $userObj->backendCheckLogin();
347
348
            return $userObj;
349
350
        } else {
351
352
            if (TYPO3_DLOG) {
353
354
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getBeUser()] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
355
356
            }
357
358
            return;
359
360
        }
361
362
    }
363
364
    /**
365
     * Get the current frontend user object
366
     *
367
     * @access	public
368
     *
369
     * @return	\TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication		Instance of \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication or NULL on failure
370
     */
371
    public static function getFeUser() {
372
373
        if (TYPO3_MODE === 'FE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'FE' is always false.
Loading history...
374
375
            // Check if a user is currently logged in.
376
            if (!empty($GLOBALS['TSFE']->loginUser)) {
377
378
                return $GLOBALS['TSFE']->fe_user;
379
380
            } elseif (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('eID') !== NULL) {
381
382
                return \TYPO3\CMS\Frontend\Utility\EidUtility::initFeUser();
383
384
            }
385
386
        } else {
387
388
            if (TYPO3_DLOG) {
389
390
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getFeUser()] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
391
392
            }
393
394
        }
395
396
        return;
397
398
    }
399
400
    /**
401
     * Get the registered hook objects for a class
402
     *
403
     * @access	public
404
     *
405
     * @param	string		$scriptRelPath: The path to the class file
406
     *
407
     * @return	array		Array of hook objects for the class
408
     */
409
    public static function getHookObjects($scriptRelPath) {
410
411
        $hookObjects = array ();
412
413
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::$extKey.'/'.$scriptRelPath]['hookClass'])) {
414
415
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::$extKey.'/'.$scriptRelPath]['hookClass'] as $classRef) {
416
417
                $hookObjects[] = &\TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($classRef);
418
419
            }
420
421
        }
422
423
        return $hookObjects;
424
425
    }
426
427
    /**
428
     * Get the "index_name" for an UID
429
     *
430
     * @access	public
431
     *
432
     * @param	integer		$uid: The UID of the record
433
     * @param	string		$table: Get the "index_name" from this table
434
     * @param	integer		$pid: Get the "index_name" from this page
435
     *
436
     * @return	string		"index_name" for the given UID
437
     */
438
    public static function getIndexName($uid, $table, $pid = -1) {
439
440
        // Save parameters for logging purposes.
441
        $_uid = $uid;
442
443
        $_pid = $pid;
444
445
        // Sanitize input.
446
        $uid = max(intval($uid), 0);
447
448
        if (!$uid || !in_array($table, array ('tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_structures', 'tx_dlf_solrcores'))) {
449
450
            if (TYPO3_DLOG) {
451
452
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getIndexName('.$_uid.', '.$table.', '.$_pid.')] Invalid UID "'.$uid.'" or table "'.$table.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
453
454
            }
455
456
            return '';
457
458
        }
459
460
        $where = '';
461
462
        // Should we check for a specific PID, too?
463
        if ($pid !== -1) {
464
465
            $pid = max(intval($pid), 0);
466
467
            $where = ' AND '.$table.'.pid='.$pid;
468
469
        }
470
471
        // Get index_name from database.
472
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
473
            $table.'.index_name AS index_name',
474
            $table,
475
            $table.'.uid='.$uid.$where.self::whereClause($table),
476
            '',
477
            '',
478
            '1'
479
        );
480
481
        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) {
482
483
            $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
484
485
            return $resArray['index_name'];
486
487
        } else {
488
489
            if (TYPO3_DLOG) {
490
491
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getIndexName('.$_uid.', '.$table.', '.$_pid.')] No "index_name" with UID "'.$uid.'" and PID "'.$pid.'" found in table "'.$table.'"', self::$extKey, SYSLOG_SEVERITY_WARNING);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
492
493
            }
494
495
            return '';
496
497
        }
498
499
    }
500
501
    /**
502
     * Get the UID for a given "index_name"
503
     *
504
     * @access	public
505
     *
506
     * @param	integer		$index_name: The index_name of the record
507
     * @param	string		$table: Get the "index_name" from this table
508
     * @param	integer		$pid: Get the "index_name" from this page
509
     *
510
     * @return	string		"uid" for the given index_name
511
     */
512
    public static function getIdFromIndexName($index_name, $table, $pid = -1) {
513
514
        // Save parameters for logging purposes.
515
        $_index_name = $index_name;
516
517
        $_pid = $pid;
518
519
        if (!$index_name || !in_array($table, array ('tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_structures', 'tx_dlf_solrcores'))) {
520
521
            if (TYPO3_DLOG) {
522
523
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getIdFromIndexName('.$_index_name.', '.$table.', '.$_pid.')] Invalid UID "'.$index_name.'" or table "'.$table.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
524
525
            }
526
527
            return '';
528
529
        }
530
531
        $where = '';
532
533
        // Should we check for a specific PID, too?
534
        if ($pid !== -1) {
535
536
            $pid = max(intval($pid), 0);
537
538
            $where = ' AND '.$table.'.pid='.$pid;
539
540
        }
541
542
        // Get index_name from database.
543
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
544
            $table.'.uid AS uid',
545
            $table,
546
            $table.'.index_name="'.$index_name.'"'.$where.self::whereClause($table),
547
            '',
548
            '',
549
            '1'
550
        );
551
552
        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) {
553
554
            $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
555
556
            return $resArray['uid'];
557
558
        } else {
559
560
            if (TYPO3_DLOG) {
561
562
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getIdFromIndexName('.$_index_name.', '.$table.', '.$_pid.')] No UID for given "index_name" "'.$index_name.'" and PID "'.$pid.'" found in table "'.$table.'"', self::$extKey, SYSLOG_SEVERITY_WARNING);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
563
564
            }
565
566
            return '';
567
568
        }
569
570
    }
571
572
    /**
573
     * Get language name from ISO code
574
     *
575
     * @access	public
576
     *
577
     * @param	string		$code: ISO 639-1 or ISO 639-2/B language code
578
     *
579
     * @return	string		Localized full name of language or unchanged input
580
     */
581
    public static function getLanguageName($code) {
582
583
        // Analyze code and set appropriate ISO table.
584
        $isoCode = strtolower(trim($code));
585
586
        if (preg_match('/^[a-z]{3}$/', $isoCode)) {
587
588
            $file = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath(self::$extKey).'lib/ISO-639/iso-639-2b.xml';
589
590
        } elseif (preg_match('/^[a-z]{2}$/', $isoCode)) {
591
592
            $file = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath(self::$extKey).'lib/ISO-639/iso-639-1.xml';
593
594
        } else {
595
596
            // No ISO code, return unchanged.
597
            return $code;
598
599
        }
600
601
        // Load ISO table and get localized full name of language.
602
        if (TYPO3_MODE === 'FE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'FE' is always false.
Loading history...
603
604
            $iso639 = $GLOBALS['TSFE']->readLLfile($file);
605
606
            if (!empty($iso639['default'][$isoCode])) {
607
608
                $lang = $GLOBALS['TSFE']->getLLL($isoCode, $iso639);
609
610
            }
611
612
        } elseif (TYPO3_MODE === 'BE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'BE' is always true.
Loading history...
613
614
            $iso639 = $GLOBALS['LANG']->includeLLFile($file, FALSE, TRUE);
615
616
            if (!empty($iso639['default'][$isoCode])) {
617
618
                $lang = $GLOBALS['LANG']->getLLL($isoCode, $iso639, FALSE);
619
620
            }
621
622
        } else {
623
624
            if (TYPO3_DLOG) {
625
626
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getLanguageName('.$code.')] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
627
628
            }
629
630
            return $code;
631
632
        }
633
634
        if (!empty($lang)) {
635
636
            return $lang;
637
638
        } else {
639
640
            if (TYPO3_DLOG) {
641
642
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getLanguageName('.$code.')] Language code "'.$code.'" not found in ISO-639 table', self::$extKey, SYSLOG_SEVERITY_NOTICE);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_NOTICE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
643
644
            }
645
646
            return $code;
647
648
        }
649
650
    }
651
652
    /**
653
     * Wrapper function for getting localizations in frontend and backend
654
     *
655
     * @param	string		$key: The locallang key to translate
656
     * @param	boolean		$hsc: Should the result be htmlspecialchar()'ed?
657
     * @param	string		$default: Default return value if no translation is available
658
     *
659
     * @return	string		The translated string or the given key on failure
660
     */
661
    public static function getLL($key, $hsc = FALSE, $default = '') {
662
663
        // Set initial output to default value.
664
        $translated = (string) $default;
665
666
        // Load common locallang file.
667
        if (empty(self::$locallang)) {
668
669
            $file = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath(self::$extKey, 'common/locallang.xml');
670
671
            if (TYPO3_MODE === 'FE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'FE' is always false.
Loading history...
672
673
                self::$locallang = $GLOBALS['TSFE']->readLLfile($file);
674
675
            } elseif (TYPO3_MODE === 'BE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'BE' is always true.
Loading history...
676
677
                self::$locallang = $GLOBALS['LANG']->includeLLFile($file, FALSE, TRUE);
678
679
            } elseif (TYPO3_DLOG) {
680
681
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getLL('.$key.', '.$default.', ['.($hsc ? 'TRUE' : 'FALSE').'])] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
682
683
            }
684
685
        }
686
687
        // Get translation.
688
        if (!empty(self::$locallang['default'][$key])) {
689
690
            if (TYPO3_MODE === 'FE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'FE' is always false.
Loading history...
691
692
                $translated = $GLOBALS['TSFE']->getLLL($key, self::$locallang);
693
694
            } elseif (TYPO3_MODE === 'BE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'BE' is always true.
Loading history...
695
696
                $translated = $GLOBALS['LANG']->getLLL($key, self::$locallang, FALSE);
697
698
            } elseif (TYPO3_DLOG) {
699
700
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getLL('.$key.', '.$default.', ['.($hsc ? 'TRUE' : 'FALSE').'])] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
701
702
            }
703
704
        }
705
706
        // Escape HTML characters if applicable.
707
        if ($hsc) {
708
709
            $translated = htmlspecialchars($translated);
710
711
        }
712
713
        return $translated;
714
715
    }
716
717
    /**
718
     * Get the URN of an object
719
     * @see	http://www.persistent-identifier.de/?link=316
720
     *
721
     * @access	public
722
     *
723
     * @param	string		$base: The namespace and base URN
724
     * @param	string		$id: The object's identifier
725
     *
726
     * @return	string		Uniform Resource Name as string
727
     */
728
    public static function getURN($base, $id) {
729
730
        $concordance = array (
731
            '0' => 1,
732
            '1' => 2,
733
            '2' => 3,
734
            '3' => 4,
735
            '4' => 5,
736
            '5' => 6,
737
            '6' => 7,
738
            '7' => 8,
739
            '8' => 9,
740
            '9' => 41,
741
            'a' => 18,
742
            'b' => 14,
743
            'c' => 19,
744
            'd' => 15,
745
            'e' => 16,
746
            'f' => 21,
747
            'g' => 22,
748
            'h' => 23,
749
            'i' => 24,
750
            'j' => 25,
751
            'k' => 42,
752
            'l' => 26,
753
            'm' => 27,
754
            'n' => 13,
755
            'o' => 28,
756
            'p' => 29,
757
            'q' => 31,
758
            'r' => 12,
759
            's' => 32,
760
            't' => 33,
761
            'u' => 11,
762
            'v' => 34,
763
            'w' => 35,
764
            'x' => 36,
765
            'y' => 37,
766
            'z' => 38,
767
            '-' => 39,
768
            ':' => 17,
769
        );
770
771
        $urn = strtolower($base.$id);
772
773
        if (preg_match('/[^a-z0-9:-]/', $urn)) {
774
775
            if (TYPO3_DLOG) {
776
777
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->getURN('.$base.', '.$id.')] Invalid chars in given parameters', self::$extKey, SYSLOG_SEVERITY_WARNING);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
778
779
            }
780
781
            return '';
782
783
        }
784
785
        $digits = '';
786
787
        for ($i = 0, $j = strlen($urn); $i < $j; $i++) {
788
789
            $digits .= $concordance[substr($urn, $i, 1)];
790
791
        }
792
793
        $checksum = 0;
794
795
        for ($i = 0, $j = strlen($digits); $i < $j; $i++) {
796
797
            $checksum += ($i + 1) * intval(substr($digits, $i, 1));
798
799
        }
800
801
        $checksum = substr(intval($checksum / intval(substr($digits, -1, 1))), -1, 1);
802
803
        return $base.$id.$checksum;
804
805
    }
806
807
    /**
808
     * Check if given ID is a valid Pica Production Number (PPN)
809
     *
810
     * @access	public
811
     *
812
     * @param	string		$ppn: The identifier to check
813
     *
814
     * @return	boolean		Is $id a valid PPN?
815
     */
816
    public static function isPPN($id) {
817
818
        return self::checkIdentifier($id, 'PPN');
819
820
    }
821
822
    /**
823
     * Load value from user's session.
824
     *
825
     * @access	public
826
     *
827
     * @param	string		$key: Session data key for retrieval
828
     *
829
     * @return	mixed		Session value for given key or NULL on failure
830
     */
831
    public static function loadFromSession($key) {
832
833
        // Save parameter for logging purposes.
834
        $_key = $key;
835
836
        // Cast to string for security reasons.
837
        $key = (string) $key;
838
839
        if (!$key) {
840
841
            if (TYPO3_DLOG) {
842
843
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->loadFromSession('.$_key.')] Invalid key "'.$key.'" for session data retrieval', self::$extKey, SYSLOG_SEVERITY_WARNING);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
844
845
            }
846
847
            return;
848
849
        }
850
851
        // Get the session data.
852
        if (TYPO3_MODE === 'FE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'FE' is always false.
Loading history...
853
854
            return $GLOBALS['TSFE']->fe_user->getKey('ses', $key);
855
856
        } elseif (TYPO3_MODE === 'BE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'BE' is always true.
Loading history...
857
858
            return $GLOBALS['BE_USER']->getSessionData($key);
859
860
        } else {
861
862
            if (TYPO3_DLOG) {
863
864
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->loadFromSession('.$_key.')] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
865
866
            }
867
868
            return;
869
870
        }
871
872
    }
873
874
    /**
875
     * Process a data and/or command map with TYPO3 core engine.
876
     *
877
     * @access	public
878
     *
879
     * @param	array		$data: Data map
880
     * @param	array		$cmd: Command map
881
     * @param	boolean		$reverseOrder: Should the command map be processed first?
882
     * @param	boolean		$be_user: Use current backend user's rights for processing?
883
     *
884
     * @return	array		Array of substituted "NEW..." identifiers and their actual UIDs.
885
     */
886
    public static function processDB(array $data = array (), array $cmd = array (), $reverseOrder = FALSE, $be_user = FALSE) {
887
888
        // Instantiate TYPO3 core engine.
889
        $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
890
891
        // Set some configuration variables.
892
        $tce->stripslashes_values = FALSE;
893
894
        // Get backend user for processing.
895
        if ($be_user && isset($GLOBALS['BE_USER'])) {
896
897
            $user = $GLOBALS['BE_USER'];
898
899
        } else {
900
901
            $user = self::getBeUser();
902
903
        }
904
905
        // Load data and command arrays.
906
        $tce->start($data, $cmd, $user);
907
908
        // Process command map first if default order is reversed.
909
        if ($cmd && $reverseOrder) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cmd of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
910
911
            $tce->process_cmdmap();
912
913
        }
914
915
        // Process data map.
916
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
917
918
            $tce->process_datamap();
919
920
        }
921
922
        // Process command map if processing order is not reversed.
923
        if ($cmd && !$reverseOrder) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cmd of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
924
925
            $tce->process_cmdmap();
926
927
        }
928
929
        return $tce->substNEWwithIDs;
930
931
    }
932
933
    /**
934
     * Process a data and/or command map with TYPO3 core engine as admin.
935
     *
936
     * @access	public
937
     *
938
     * @param	array		$data: Data map
939
     * @param	array		$cmd: Command map
940
     * @param	boolean		$reverseOrder: Should the command map be processed first?
941
     *
942
     * @return	array		Array of substituted "NEW..." identifiers and their actual UIDs.
943
     */
944
    public static function processDBasAdmin(array $data = array (), array $cmd = array (), $reverseOrder = FALSE) {
945
946
        if (TYPO3_MODE === 'BE' && $GLOBALS['BE_USER']->isAdmin()) {
947
948
            return self::processDB($data, $cmd, $reverseOrder, TRUE);
949
950
        } else {
951
952
            if (TYPO3_DLOG) {
953
954
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->processDBasAdmin([data->data], [data->cmd], ['.($reverseOrder ? 'TRUE' : 'FALSE').'])] Current backend user has no admin privileges', self::$extKey, SYSLOG_SEVERITY_ERROR, array ('data' => $data, 'cmd' => $cmd));
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
955
956
            }
957
958
            return array ();
959
960
        }
961
962
    }
963
964
    /**
965
     * Fetches and renders all available flash messages from the queue.
966
     *
967
     * @return	string		All flash messages in the queue rendered as HTML.
968
     */
969
    public static function renderFlashMessages() {
970
971
        $flashMessageService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
972
973
        $content = '';
974
975
        if (version_compare(TYPO3_branch, '7.4', '<')) {
976
977
            // For TYPO3 6.2 - 7.3, we can use the existing method.
978
            $content .= $flashMessageService->getMessageQueueByIdentifier()->renderFlashMessages();
979
980
        } else {
981
982
            // Since TYPO3 7.4.0, \TYPO3\CMS\Core\Messaging\FlashMessageQueue::renderFlashMessages
983
            // uses htmlspecialchars on all texts, but we have message text with HTML tags.
984
            // Therefore we copy the implementation from 7.4.0, but remove the htmlspecialchars call.
985
            $flashMessages = $flashMessageService->getMessageQueueByIdentifier()->getAllMessagesAndFlush();
986
987
            if (!empty($flashMessages)) {
988
989
                $content .= '<ul class="typo3-messages">';
990
991
                foreach ($flashMessages as $flashMessage) {
992
993
                    $severityClass = sprintf('alert %s', $flashMessage->getClass());
994
995
                    //~ $messageContent = htmlspecialchars($flashMessage->getMessage());
996
997
                    $messageContent = $flashMessage->getMessage();
998
999
                    if ($flashMessage->getTitle() !== '') {
1000
1001
                        $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($flashMessage->getTitle())).$messageContent;
1002
1003
                    }
1004
1005
                    $content .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
1006
1007
                }
1008
1009
                $content .= '</ul>';
1010
1011
            }
1012
1013
        }
1014
1015
        return $content;
1016
1017
    }
1018
1019
    /**
1020
     * Save given value to user's session.
1021
     *
1022
     * @access	public
1023
     *
1024
     * @param	mixed		$value: Value to save
1025
     * @param	string		$key: Session data key for saving
1026
     *
1027
     * @return	boolean		TRUE on success, FALSE on failure
1028
     */
1029
    public static function saveToSession($value, $key) {
1030
1031
        // Save parameter for logging purposes.
1032
        $_key = $key;
1033
1034
        // Cast to string for security reasons.
1035
        $key = (string) $key;
1036
1037
        if (!$key) {
1038
1039
            if (TYPO3_DLOG) {
1040
1041
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->saveToSession([data], '.$_key.')] Invalid key "'.$key.'" for session data saving', self::$extKey, SYSLOG_SEVERITY_WARNING, $value);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1042
1043
            }
1044
1045
            return FALSE;
1046
1047
        }
1048
1049
        // Save value in session data.
1050
        if (TYPO3_MODE === 'FE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'FE' is always false.
Loading history...
1051
1052
            $GLOBALS['TSFE']->fe_user->setKey('ses', $key, $value);
1053
1054
            $GLOBALS['TSFE']->fe_user->storeSessionData();
1055
1056
            return TRUE;
1057
1058
        } elseif (TYPO3_MODE === 'BE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'BE' is always true.
Loading history...
1059
1060
            $GLOBALS['BE_USER']->setAndSaveSessionData($key, $value);
1061
1062
            return TRUE;
1063
1064
        } else {
1065
1066
            if (TYPO3_DLOG) {
1067
1068
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->saveToSession([data], '.$_key.')] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', self::$extKey, SYSLOG_SEVERITY_ERROR, $data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1069
1070
            }
1071
1072
            return FALSE;
1073
1074
        }
1075
1076
    }
1077
1078
    /**
1079
     * This translates an internal "index_name"
1080
     *
1081
     * @access	public
1082
     *
1083
     * @param	string		$index_name: The internal "index_name" to translate
1084
     * @param	string		$table: Get the translation from this table
1085
     * @param	string		$pid: Get the translation from this page
1086
     *
1087
     * @return	string		Localized label for $index_name
1088
     */
1089
    public static function translate($index_name, $table, $pid) {
1090
1091
        // Save parameters for logging purposes.
1092
        $_index_name = $index_name;
1093
1094
        $_pid = $pid;
1095
1096
        // Load labels into static variable for future use.
1097
        static $labels = array ();
1098
1099
        // Sanitize input.
1100
        $pid = max(intval($pid), 0);
1101
1102
        if (!$pid) {
1103
1104
            if (TYPO3_DLOG) {
1105
1106
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->translate('.$_index_name.', '.$table.', '.$_pid.')] Invalid PID "'.$pid.'" for translation', self::$extKey, SYSLOG_SEVERITY_WARNING);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_WARNING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1107
1108
            }
1109
1110
            return $index_name;
1111
1112
        }
1113
1114
        // Check if "index_name" is an UID.
1115
        if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($index_name)) {
1116
1117
            $index_name = self::getIndexName($index_name, $table, $pid);
1118
1119
        }
1120
1121
        /* $labels already contains the translated content element, but with the index_name of the translated content element itself
1122
         * and not with the $index_name of the original that we receive here. So we have to determine the index_name of the
1123
         * associated translated content element. E.g. $labels['title0'] != $index_name = title. */
1124
1125
        // First fetch the uid of the received index_name
1126
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
1127
                'uid, l18n_parent',
1128
                $table,
1129
                'pid='.$pid.' AND index_name="'.$index_name.'"'.self::whereClause($table, TRUE),
1130
                '',
1131
                '',
1132
                ''
1133
        );
1134
1135
        if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) {
1136
1137
            // Now we use the uid of the l18_parent to fetch the index_name of the translated content element.
1138
            $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
1139
1140
            $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
1141
                    'index_name',
1142
                    $table,
1143
                    'pid='.$pid.' AND uid='.$resArray['l18n_parent'].' AND sys_language_uid='.intval($GLOBALS['TSFE']->sys_language_content).self::whereClause($table, TRUE),
1144
                    '',
1145
                    '',
1146
                    ''
1147
            );
1148
1149
            if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) {
1150
1151
                // If there is an translated content element, overwrite the received $index_name.
1152
                $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
1153
1154
                $index_name = $resArray['index_name'];
1155
1156
            }
1157
1158
        }
1159
1160
        // Check if we already got a translation.
1161
        if (empty($labels[$table][$pid][$GLOBALS['TSFE']->sys_language_content][$index_name])) {
1162
1163
            // Check if this table is allowed for translation.
1164
            if (in_array($table, array ('tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_structures'))) {
1165
1166
                $additionalWhere = ' AND sys_language_uid IN (-1,0)';
1167
1168
                if ($GLOBALS['TSFE']->sys_language_content > 0) {
1169
1170
                    $additionalWhere = ' AND (sys_language_uid IN (-1,0) OR (sys_language_uid='.intval($GLOBALS['TSFE']->sys_language_content).' AND l18n_parent=0))';
1171
1172
                }
1173
1174
                // Get labels from database.
1175
                $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
1176
                    '*',
1177
                    $table,
1178
                    'pid='.$pid.$additionalWhere.self::whereClause($table, TRUE),
1179
                    '',
1180
                    '',
1181
                    ''
1182
                );
1183
1184
                if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) {
1185
1186
                    while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
1187
1188
                        // Overlay localized labels if available.
1189
                        if ($GLOBALS['TSFE']->sys_language_content > 0) {
1190
1191
                            $resArray = $GLOBALS['TSFE']->sys_page->getRecordOverlay($table, $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL);
1192
1193
                        }
1194
1195
                        if ($resArray) {
1196
1197
                            $labels[$table][$pid][$GLOBALS['TSFE']->sys_language_content][$resArray['index_name']] = $resArray['label'];
1198
1199
                        }
1200
1201
                    }
1202
1203
                } else {
1204
1205
                    if (TYPO3_DLOG) {
1206
1207
                        \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->translate('.$_index_name.', '.$table.', '.$_pid.')] No translation with PID "'.$pid.'" available in table "'.$table.'" or translation not accessible', self::extKey, SYSLOG_SEVERITY_NOTICE);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_NOTICE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant tx_dlf_helper::extKey was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1208
1209
                    }
1210
1211
                }
1212
1213
            } else {
1214
1215
                if (TYPO3_DLOG) {
1216
1217
                    \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->translate('.$_index_name.', '.$table.', '.$_pid.')] No translations available for table "'.$table.'"', self::$extKey, SYSLOG_SEVERITY_WARNING);
1218
1219
                }
1220
1221
            }
1222
1223
        }
1224
1225
        if (!empty($labels[$table][$pid][$GLOBALS['TSFE']->sys_language_content][$index_name])) {
1226
1227
            return $labels[$table][$pid][$GLOBALS['TSFE']->sys_language_content][$index_name];
1228
1229
        } else {
1230
1231
            return $index_name;
1232
1233
        }
1234
1235
    }
1236
1237
    /**
1238
     * This returns the additional WHERE clause of a table based on its TCA configuration
1239
     *
1240
     * @access	public
1241
     *
1242
     * @param	string		$table: Table name as defined in TCA
1243
     * @param	boolean		$showHidden: Ignore the hidden flag?
1244
     *
1245
     * @return	string		Additional WHERE clause
1246
     */
1247
    public static function whereClause($table, $showHidden = FALSE) {
1248
1249
        if (TYPO3_MODE === 'FE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'FE' is always false.
Loading history...
1250
1251
            // Table "tx_dlf_formats" always has PID 0.
1252
            if ($table == 'tx_dlf_formats') {
1253
1254
                return \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($table);
1255
1256
            }
1257
1258
            // Should we ignore the record's hidden flag?
1259
            $ignoreHide = -1;
1260
1261
            if ($showHidden) {
1262
1263
                $ignoreHide = 1;
1264
1265
            }
1266
1267
            // $GLOBALS['TSFE']->sys_page is not always available in frontend.
1268
            if (is_object($GLOBALS['TSFE']->sys_page)) {
1269
1270
                return $GLOBALS['TSFE']->sys_page->enableFields($table, $ignoreHide);
1271
1272
            } else {
1273
1274
                $pageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
1275
1276
                $GLOBALS['TSFE']->includeTCA();
1277
1278
                return $pageRepository->enableFields($table, $ignoreHide);
1279
1280
            }
1281
1282
        } elseif (TYPO3_MODE === 'BE') {
1 ignored issue
show
introduced by
The condition TYPO3_MODE === 'BE' is always true.
Loading history...
1283
1284
            return \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($table);
1285
1286
        } else {
1287
1288
            if (TYPO3_DLOG) {
1289
1290
                \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_helper->whereClause('.$table.', ['.($showHidden ? 'TRUE' : 'FALSE').'])] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', self::$extKey, SYSLOG_SEVERITY_ERROR);
0 ignored issues
show
Bug introduced by
The constant SYSLOG_SEVERITY_ERROR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
1291
1292
            }
1293
1294
            return ' AND 1=-1';
1295
1296
        }
1297
1298
    }
1299
1300
    /**
1301
     * This is a static class, thus no instances should be created
1302
     *
1303
     * @access private
1304
     */
1305
    private function __construct() {}
1306
1307
}
1308