We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 116 |
| Total Lines | 821 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Helper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Helper { |
||
| 24 | /** |
||
| 25 | * The extension key |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | * @access public |
||
| 29 | */ |
||
| 30 | public static $extKey = 'dlf'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The locallang array for flash messages |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | * @access protected |
||
| 37 | */ |
||
| 38 | protected static $messages = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Generates a flash message and adds it to a message queue. |
||
| 42 | * |
||
| 43 | * @access public |
||
| 44 | * |
||
| 45 | * @param string $message: The body of the message |
||
| 46 | * @param string $title: The title of the message |
||
| 47 | * @param integer $severity: The message's severity |
||
| 48 | * @param boolean $session: Should the message be saved in the user's session? |
||
| 49 | * @param string $queue: The queue's unique identifier |
||
| 50 | * |
||
| 51 | * @return \TYPO3\CMS\Core\Messaging\FlashMessageQueue The queue the message was added to |
||
| 52 | */ |
||
| 53 | public static function addMessage($message, $title, $severity, $session = FALSE, $queue = 'kitodo.default.flashMessages') { |
||
| 54 | $flashMessageService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class); |
||
| 55 | $flashMessageQueue = $flashMessageService->getMessageQueueByIdentifier($queue); |
||
| 56 | $flashMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 57 | \TYPO3\CMS\Core\Messaging\FlashMessage::class, |
||
| 58 | $message, |
||
| 59 | $title, |
||
| 60 | $severity, |
||
| 61 | $session |
||
| 62 | ); |
||
| 63 | $flashMessageQueue->enqueue($flashMessage); |
||
| 64 | return $flashMessageQueue; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Check if given identifier is a valid identifier of the German National Library |
||
| 69 | * |
||
| 70 | * @access public |
||
| 71 | * |
||
| 72 | * @param string $id: The identifier to check |
||
| 73 | * @param string $type: What type is the identifier supposed to be? |
||
| 74 | * Possible values: PPN, IDN, PND, ZDB, SWD, GKD |
||
| 75 | * |
||
| 76 | * @return boolean Is $id a valid GNL identifier of the given $type? |
||
| 77 | */ |
||
| 78 | public static function checkIdentifier($id, $type) { |
||
| 79 | $digits = substr($id, 0, 8); |
||
| 80 | $checksum = 0; |
||
| 81 | for ($i = 0, $j = strlen($digits); $i < $j; $i++) { |
||
| 82 | $checksum += (9 - $i) * intval(substr($digits, $i, 1)); |
||
| 83 | } |
||
| 84 | $checksum = (11 - ($checksum % 11)) % 11; |
||
| 85 | switch (strtoupper($type)) { |
||
| 86 | case 'PPN': |
||
| 87 | case 'IDN': |
||
| 88 | case 'PND': |
||
| 89 | if ($checksum == 10) { |
||
| 90 | $checksum = 'X'; |
||
| 91 | } |
||
| 92 | if (!preg_match('/[0-9]{8}[0-9X]{1}/i', $id)) { |
||
| 93 | return FALSE; |
||
| 94 | } elseif (strtoupper(substr($id, -1, 1)) != $checksum) { |
||
| 95 | return FALSE; |
||
| 96 | } |
||
| 97 | break; |
||
| 98 | case 'ZDB': |
||
| 99 | if ($checksum == 10) { |
||
| 100 | $checksum = 'X'; |
||
| 101 | } |
||
| 102 | if (!preg_match('/[0-9]{8}-[0-9X]{1}/i', $id)) { |
||
| 103 | return FALSE; |
||
| 104 | } elseif (strtoupper(substr($id, -1, 1)) != $checksum) { |
||
| 105 | return FALSE; |
||
| 106 | } |
||
| 107 | break; |
||
| 108 | case 'SWD': |
||
| 109 | $checksum = 11 - $checksum; |
||
| 110 | if (!preg_match('/[0-9]{8}-[0-9]{1}/i', $id)) { |
||
| 111 | return FALSE; |
||
| 112 | } elseif ($checksum == 10) { |
||
| 113 | return self::checkIdentifier(($digits + 1).substr($id, -2, 2), 'SWD'); |
||
| 114 | } elseif (substr($id, -1, 1) != $checksum) { |
||
| 115 | return FALSE; |
||
| 116 | } |
||
| 117 | break; |
||
| 118 | case 'GKD': |
||
| 119 | $checksum = 11 - $checksum; |
||
| 120 | if ($checksum == 10) { |
||
| 121 | $checksum = 'X'; |
||
| 122 | } |
||
| 123 | if (!preg_match('/[0-9]{8}-[0-9X]{1}/i', $id)) { |
||
| 124 | return FALSE; |
||
| 125 | } elseif (strtoupper(substr($id, -1, 1)) != $checksum) { |
||
| 126 | return FALSE; |
||
| 127 | } |
||
| 128 | break; |
||
| 129 | } |
||
| 130 | return TRUE; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Decrypt encrypted value with given control hash |
||
| 135 | * |
||
| 136 | * @access public |
||
| 137 | * |
||
| 138 | * @param string $encrypted: The encrypted value to decrypt |
||
| 139 | * @param string $hash: The control hash for decrypting |
||
| 140 | * |
||
| 141 | * @return mixed The decrypted value or NULL on error |
||
| 142 | */ |
||
| 143 | public static function decrypt($encrypted, $hash) { |
||
| 144 | $decrypted = NULL; |
||
|
|
|||
| 145 | if (empty($encrypted) |
||
| 146 | || empty($hash)) { |
||
| 147 | self::devLog('Invalid parameters given for decryption', DEVLOG_SEVERITY_ERROR); |
||
| 148 | return; |
||
| 149 | } |
||
| 150 | if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) { |
||
| 151 | self::devLog('No encryption key set in TYPO3 configuration', DEVLOG_SEVERITY_ERROR); |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | $iv = substr(md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']), 0, openssl_cipher_iv_length('BF-CFB')); |
||
| 155 | $decrypted = openssl_decrypt($encrypted, 'BF-CFB', substr($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], 0, 56), 0, $iv); |
||
| 156 | $salt = substr($hash, 0, 10); |
||
| 157 | $hashed = $salt.substr(sha1($salt.$decrypted), -10); |
||
| 158 | if ($hashed !== $hash) { |
||
| 159 | self::devLog('Invalid hash "'.$hash.'" given for decryption', DEVLOG_SEVERITY_WARNING); |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | return $decrypted; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add a message to the TYPO3 developer log |
||
| 167 | * |
||
| 168 | * @access public |
||
| 169 | * |
||
| 170 | * @param string $message: The message to log |
||
| 171 | * @param integer $severity: The severity of the message |
||
| 172 | * 0 is info, 1 is notice, 2 is warning, 3 is fatal error, -1 is "OK" message |
||
| 173 | * |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public static function devLog($message, $severity = 0) { |
||
| 177 | if (TYPO3_DLOG) { |
||
| 178 | $stacktrace = debug_backtrace(0, 2); |
||
| 179 | // Set some defaults. |
||
| 180 | $caller = 'Kitodo\Dlf\Default\UnknownClass::unknownMethod'; |
||
| 181 | $args = []; |
||
| 182 | $data = []; |
||
| 183 | if (!empty($stacktrace[1])) { |
||
| 184 | $caller = $stacktrace[1]['class'].$stacktrace[1]['type'].$stacktrace[1]['function']; |
||
| 185 | foreach ($stacktrace[1]['args'] as $arg) { |
||
| 186 | if (is_bool($arg)) { |
||
| 187 | $args[] = ($arg ? 'TRUE' : 'FALSE'); |
||
| 188 | } elseif (is_scalar($arg)) { |
||
| 189 | $args[] = (string) $arg; |
||
| 190 | } elseif (is_null($arg)) { |
||
| 191 | $args[] = 'NULL'; |
||
| 192 | } elseif (is_array($arg)) { |
||
| 193 | $args[] = '[data]'; |
||
| 194 | $data[] = $arg; |
||
| 195 | } elseif (is_object($arg)) { |
||
| 196 | $args[] = '['.get_class($arg).']'; |
||
| 197 | $data[] = $arg; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | $arguments = '('.implode(', ', $args).')'; |
||
| 202 | $additionalData = (empty($data) ? FALSE : $data); |
||
| 203 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('['.$caller.$arguments.'] '.$message, self::$extKey, $severity, $additionalData); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Encrypt the given string |
||
| 209 | * |
||
| 210 | * @access public |
||
| 211 | * |
||
| 212 | * @param string $string: The string to encrypt |
||
| 213 | * |
||
| 214 | * @return array Array with encrypted string and control hash |
||
| 215 | */ |
||
| 216 | public static function encrypt($string) { |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the unqualified name of a class |
||
| 230 | * |
||
| 231 | * @access public |
||
| 232 | * |
||
| 233 | * @param string $qualifiedClassname: The qualified class name from get_class() |
||
| 234 | * |
||
| 235 | * @return string The unqualified class name |
||
| 236 | */ |
||
| 237 | public static function getUnqualifiedClassName($qualifiedClassname) { |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Clean up a string to use in an URL. |
||
| 244 | * |
||
| 245 | * @access public |
||
| 246 | * |
||
| 247 | * @param string $string: The string to clean up |
||
| 248 | * |
||
| 249 | * @return string The cleaned up string |
||
| 250 | */ |
||
| 251 | public static function getCleanString($string) { |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the registered hook objects for a class |
||
| 265 | * |
||
| 266 | * @access public |
||
| 267 | * |
||
| 268 | * @param string $scriptRelPath: The path to the class file |
||
| 269 | * |
||
| 270 | * @return array Array of hook objects for the class |
||
| 271 | */ |
||
| 272 | public static function getHookObjects($scriptRelPath) { |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the "index_name" for an UID |
||
| 284 | * |
||
| 285 | * @access public |
||
| 286 | * |
||
| 287 | * @param integer $uid: The UID of the record |
||
| 288 | * @param string $table: Get the "index_name" from this table |
||
| 289 | * @param integer $pid: Get the "index_name" from this page |
||
| 290 | * |
||
| 291 | * @return string "index_name" for the given UID |
||
| 292 | */ |
||
| 293 | public static function getIndexNameFromUid($uid, $table, $pid = -1) { |
||
| 294 | // Sanitize input. |
||
| 295 | $uid = max(intval($uid), 0); |
||
| 296 | if (!$uid |
||
| 297 | || !in_array($table, ['tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_structures', 'tx_dlf_solrcores'])) { |
||
| 298 | self::devLog('Invalid UID "'.$uid.'" or table "'.$table.'"', DEVLOG_SEVERITY_ERROR); |
||
| 299 | return ''; |
||
| 300 | } |
||
| 301 | $where = ''; |
||
| 302 | // Should we check for a specific PID, too? |
||
| 303 | if ($pid !== -1) { |
||
| 304 | $pid = max(intval($pid), 0); |
||
| 305 | $where = ' AND '.$table.'.pid='.$pid; |
||
| 306 | } |
||
| 307 | // Get index_name from database. |
||
| 308 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 309 | $table.'.index_name AS index_name', |
||
| 310 | $table, |
||
| 311 | $table.'.uid='.$uid |
||
| 312 | .$where |
||
| 313 | .self::whereClause($table), |
||
| 314 | '', |
||
| 315 | '', |
||
| 316 | '1' |
||
| 317 | ); |
||
| 318 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) { |
||
| 319 | $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); |
||
| 320 | return $resArray['index_name']; |
||
| 321 | } else { |
||
| 322 | self::devLog('No "index_name" with UID '.$uid.' and PID '.$pid.' found in table "'.$table.'"', DEVLOG_SEVERITY_WARNING); |
||
| 323 | return ''; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get language name from ISO code |
||
| 329 | * |
||
| 330 | * @access public |
||
| 331 | * |
||
| 332 | * @param string $code: ISO 639-1 or ISO 639-2/B language code |
||
| 333 | * |
||
| 334 | * @return string Localized full name of language or unchanged input |
||
| 335 | */ |
||
| 336 | public static function getLanguageName($code) { |
||
| 337 | // Analyze code and set appropriate ISO table. |
||
| 338 | $isoCode = strtolower(trim($code)); |
||
| 339 | if (preg_match('/^[a-z]{3}$/', $isoCode)) { |
||
| 340 | $file = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath(self::$extKey).'Resources/Private/Data/iso-639-2b.xml'; |
||
| 341 | } elseif (preg_match('/^[a-z]{2}$/', $isoCode)) { |
||
| 342 | $file = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath(self::$extKey).'Resources/Private/Data/iso-639-1.xml'; |
||
| 343 | } else { |
||
| 344 | // No ISO code, return unchanged. |
||
| 345 | return $code; |
||
| 346 | } |
||
| 347 | // Load ISO table and get localized full name of language. |
||
| 348 | if (TYPO3_MODE === 'FE') { |
||
| 349 | $iso639 = $GLOBALS['TSFE']->readLLfile($file); |
||
| 350 | if (!empty($iso639['default'][$isoCode])) { |
||
| 351 | $lang = $GLOBALS['TSFE']->getLLL($isoCode, $iso639); |
||
| 352 | } |
||
| 353 | } elseif (TYPO3_MODE === 'BE') { |
||
| 354 | $iso639 = $GLOBALS['LANG']->includeLLFile($file, FALSE, TRUE); |
||
| 355 | if (!empty($iso639['default'][$isoCode])) { |
||
| 356 | $lang = $GLOBALS['LANG']->getLLL($isoCode, $iso639, FALSE); |
||
| 357 | } |
||
| 358 | } else { |
||
| 359 | self::devLog('Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', DEVLOG_SEVERITY_ERROR); |
||
| 360 | return $code; |
||
| 361 | } |
||
| 362 | if (!empty($lang)) { |
||
| 363 | return $lang; |
||
| 364 | } else { |
||
| 365 | self::devLog('Language code "'.$code.'" not found in ISO-639 table', DEVLOG_SEVERITY_NOTICE); |
||
| 366 | return $code; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Wrapper function for getting localized messages in frontend and backend |
||
| 372 | * |
||
| 373 | * @access public |
||
| 374 | * |
||
| 375 | * @param string $key: The locallang key to translate |
||
| 376 | * @param boolean $hsc: Should the result be htmlspecialchar()'ed? |
||
| 377 | * @param string $default: Default return value if no translation is available |
||
| 378 | * |
||
| 379 | * @return string The translated string or the given key on failure |
||
| 380 | */ |
||
| 381 | public static function getMessage($key, $hsc = FALSE, $default = '') { |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get the UID for a given "index_name" |
||
| 414 | * |
||
| 415 | * @access public |
||
| 416 | * |
||
| 417 | * @param integer $index_name: The index_name of the record |
||
| 418 | * @param string $table: Get the "index_name" from this table |
||
| 419 | * @param integer $pid: Get the "index_name" from this page |
||
| 420 | * |
||
| 421 | * @return string "uid" for the given index_name |
||
| 422 | */ |
||
| 423 | public static function getUidFromIndexName($index_name, $table, $pid = -1) { |
||
| 424 | if (!$index_name |
||
| 425 | || !in_array($table, ['tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_structures', 'tx_dlf_solrcores'])) { |
||
| 426 | self::devLog('Invalid UID '.$index_name.' or table "'.$table.'"', DEVLOG_SEVERITY_ERROR); |
||
| 427 | return ''; |
||
| 428 | } |
||
| 429 | $where = ''; |
||
| 430 | // Should we check for a specific PID, too? |
||
| 431 | if ($pid !== -1) { |
||
| 432 | $pid = max(intval($pid), 0); |
||
| 433 | $where = ' AND '.$table.'.pid='.$pid; |
||
| 434 | } |
||
| 435 | // Get index_name from database. |
||
| 436 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 437 | $table.'.uid AS uid', |
||
| 438 | $table, |
||
| 439 | $table.'.index_name="'.$index_name.'"' |
||
| 440 | .$where |
||
| 441 | .self::whereClause($table), |
||
| 442 | '', |
||
| 443 | '', |
||
| 444 | '1' |
||
| 445 | ); |
||
| 446 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) { |
||
| 447 | $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); |
||
| 448 | return $resArray['uid']; |
||
| 449 | } else { |
||
| 450 | self::devLog('No UID for given index_name "'.$index_name.'" and PID '.$pid.' found in table "'.$table.'"', DEVLOG_SEVERITY_WARNING); |
||
| 451 | return ''; |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the URN of an object |
||
| 457 | * @see http://www.persistent-identifier.de/?link=316 |
||
| 458 | * |
||
| 459 | * @access public |
||
| 460 | * |
||
| 461 | * @param string $base: The namespace and base URN |
||
| 462 | * @param string $id: The object's identifier |
||
| 463 | * |
||
| 464 | * @return string Uniform Resource Name as string |
||
| 465 | */ |
||
| 466 | public static function getURN($base, $id) { |
||
| 467 | $concordance = [ |
||
| 468 | '0' => 1, |
||
| 469 | '1' => 2, |
||
| 470 | '2' => 3, |
||
| 471 | '3' => 4, |
||
| 472 | '4' => 5, |
||
| 473 | '5' => 6, |
||
| 474 | '6' => 7, |
||
| 475 | '7' => 8, |
||
| 476 | '8' => 9, |
||
| 477 | '9' => 41, |
||
| 478 | 'a' => 18, |
||
| 479 | 'b' => 14, |
||
| 480 | 'c' => 19, |
||
| 481 | 'd' => 15, |
||
| 482 | 'e' => 16, |
||
| 483 | 'f' => 21, |
||
| 484 | 'g' => 22, |
||
| 485 | 'h' => 23, |
||
| 486 | 'i' => 24, |
||
| 487 | 'j' => 25, |
||
| 488 | 'k' => 42, |
||
| 489 | 'l' => 26, |
||
| 490 | 'm' => 27, |
||
| 491 | 'n' => 13, |
||
| 492 | 'o' => 28, |
||
| 493 | 'p' => 29, |
||
| 494 | 'q' => 31, |
||
| 495 | 'r' => 12, |
||
| 496 | 's' => 32, |
||
| 497 | 't' => 33, |
||
| 498 | 'u' => 11, |
||
| 499 | 'v' => 34, |
||
| 500 | 'w' => 35, |
||
| 501 | 'x' => 36, |
||
| 502 | 'y' => 37, |
||
| 503 | 'z' => 38, |
||
| 504 | '-' => 39, |
||
| 505 | ':' => 17, |
||
| 506 | ]; |
||
| 507 | $urn = strtolower($base.$id); |
||
| 508 | if (preg_match('/[^a-z0-9:-]/', $urn)) { |
||
| 509 | self::devLog('Invalid chars in given parameters', DEVLOG_SEVERITY_WARNING); |
||
| 510 | return ''; |
||
| 511 | } |
||
| 512 | $digits = ''; |
||
| 513 | for ($i = 0, $j = strlen($urn); $i < $j; $i++) { |
||
| 514 | $digits .= $concordance[substr($urn, $i, 1)]; |
||
| 515 | } |
||
| 516 | $checksum = 0; |
||
| 517 | for ($i = 0, $j = strlen($digits); $i < $j; $i++) { |
||
| 518 | $checksum += ($i + 1) * intval(substr($digits, $i, 1)); |
||
| 519 | } |
||
| 520 | $checksum = substr(intval($checksum / intval(substr($digits, -1, 1))), -1, 1); |
||
| 521 | return $base.$id.$checksum; |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Check if given ID is a valid Pica Production Number (PPN) |
||
| 526 | * |
||
| 527 | * @access public |
||
| 528 | * |
||
| 529 | * @param string $id: The identifier to check |
||
| 530 | * |
||
| 531 | * @return boolean Is $id a valid PPN? |
||
| 532 | */ |
||
| 533 | public static function isPPN($id) { |
||
| 534 | return self::checkIdentifier($id, 'PPN'); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Load value from user's session. |
||
| 539 | * |
||
| 540 | * @access public |
||
| 541 | * |
||
| 542 | * @param string $key: Session data key for retrieval |
||
| 543 | * |
||
| 544 | * @return mixed Session value for given key or NULL on failure |
||
| 545 | */ |
||
| 546 | public static function loadFromSession($key) { |
||
| 561 | } |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Merges two arrays recursively and actually returns the modified array. |
||
| 566 | * @see \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule() |
||
| 567 | * |
||
| 568 | * @access public |
||
| 569 | * |
||
| 570 | * @param array $original: Original array |
||
| 571 | * @param array $overrule: Overrule array, overruling the original array |
||
| 572 | * @param boolean $addKeys: If set to FALSE, keys that are not found in $original will not be set |
||
| 573 | * @param boolean $includeEmptyValues: If set, values from $overrule will overrule if they are empty |
||
| 574 | * @param boolean $enableUnsetFeature: If set, special value "__UNSET" can be used in the overrule array to unset keys in the original array |
||
| 575 | * |
||
| 576 | * @return array Merged array |
||
| 577 | */ |
||
| 578 | public static function mergeRecursiveWithOverrule(array $original, array $overrule, $addKeys = TRUE, $includeEmptyValues = TRUE, $enableUnsetFeature = TRUE) { |
||
| 579 | \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($original, $overrule, $addKeys, $includeEmptyValues, $enableUnsetFeature); |
||
| 580 | return $original; |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Process a data and/or command map with TYPO3 core engine as admin. |
||
| 585 | * |
||
| 586 | * @access public |
||
| 587 | * |
||
| 588 | * @param array $data: Data map |
||
| 589 | * @param array $cmd: Command map |
||
| 590 | * @param boolean $reverseOrder: Should the data map be reversed? |
||
| 591 | * @param boolean $cmdFirst: Should the command map be processed first? |
||
| 592 | * |
||
| 593 | * @return array Array of substituted "NEW..." identifiers and their actual UIDs. |
||
| 594 | */ |
||
| 595 | public static function processDBasAdmin(array $data = [], array $cmd = [], $reverseOrder = FALSE, $cmdFirst = FALSE) { |
||
| 596 | if (TYPO3_MODE === 'BE' |
||
| 597 | && $GLOBALS['BE_USER']->isAdmin()) { |
||
| 598 | // Instantiate TYPO3 core engine. |
||
| 599 | $dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class); |
||
| 600 | // Load data and command arrays. |
||
| 601 | $dataHandler->start($data, $cmd); |
||
| 602 | // Process command map first if default order is reversed. |
||
| 603 | if (!empty($cmd) |
||
| 604 | && $cmdFirst) { |
||
| 605 | $dataHandler->process_cmdmap(); |
||
| 606 | } |
||
| 607 | // Process data map. |
||
| 608 | if (!empty($data)) { |
||
| 609 | $dataHandler->reverseOrder = $reverseOrder; |
||
| 610 | $dataHandler->process_datamap(); |
||
| 611 | } |
||
| 612 | // Process command map if processing order is not reversed. |
||
| 613 | if (!empty($cmd) |
||
| 614 | && !$cmdFirst) { |
||
| 615 | $dataHandler->process_cmdmap(); |
||
| 616 | } |
||
| 617 | return $dataHandler->substNEWwithIDs; |
||
| 618 | } else { |
||
| 619 | self::devLog('Current backend user has no admin privileges', DEVLOG_SEVERITY_ERROR); |
||
| 620 | return []; |
||
| 621 | } |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Fetches and renders all available flash messages from the queue. |
||
| 626 | * |
||
| 627 | * @access public |
||
| 628 | * |
||
| 629 | * @param string $queue: The queue's unique identifier |
||
| 630 | * |
||
| 631 | * @return string All flash messages in the queue rendered as HTML. |
||
| 632 | */ |
||
| 633 | public static function renderFlashMessages($queue = 'kitodo.default.flashMessages') { |
||
| 634 | $flashMessageService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class); |
||
| 635 | $flashMessageQueue = $flashMessageService->getMessageQueueByIdentifier($queue); |
||
| 636 | // \TYPO3\CMS\Core\Messaging\FlashMessage::getMessageAsMarkup() uses htmlspecialchars() |
||
| 637 | // on all messages, but we have messages with HTML tags. Therefore we copy the official |
||
| 638 | // implementation and remove the htmlspecialchars() call on the message body. |
||
| 639 | $content = ''; |
||
| 640 | $flashMessages = $flashMessageQueue->getAllMessagesAndFlush(); |
||
| 641 | if (!empty($flashMessages)) { |
||
| 642 | $content .= '<div class="typo3-messages">'; |
||
| 643 | foreach ($flashMessages as $flashMessage) { |
||
| 644 | $messageTitle = $flashMessage->getTitle(); |
||
| 645 | $markup = []; |
||
| 646 | $markup[] = '<div class="alert '.htmlspecialchars($flashMessage->getClass()).'">'; |
||
| 647 | $markup[] = ' <div class="media">'; |
||
| 648 | $markup[] = ' <div class="media-left">'; |
||
| 649 | $markup[] = ' <span class="fa-stack fa-lg">'; |
||
| 650 | $markup[] = ' <i class="fa fa-circle fa-stack-2x"></i>'; |
||
| 651 | $markup[] = ' <i class="fa fa-'.htmlspecialchars($flashMessage->getIconName()).' fa-stack-1x"></i>'; |
||
| 652 | $markup[] = ' </span>'; |
||
| 653 | $markup[] = ' </div>'; |
||
| 654 | $markup[] = ' <div class="media-body">'; |
||
| 655 | if (!empty($messageTitle)) { |
||
| 656 | $markup[] = ' <h4 class="alert-title">'.htmlspecialchars($messageTitle).'</h4>'; |
||
| 657 | } |
||
| 658 | $markup[] = ' <p class="alert-message">'.$flashMessage->getMessage().'</p>'; // Removed htmlspecialchars() here. |
||
| 659 | $markup[] = ' </div>'; |
||
| 660 | $markup[] = ' </div>'; |
||
| 661 | $markup[] = '</div>'; |
||
| 662 | $content .= implode('', $markup); |
||
| 663 | } |
||
| 664 | $content .= '</div>'; |
||
| 665 | } |
||
| 666 | return $content; |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Save given value to user's session. |
||
| 671 | * |
||
| 672 | * @access public |
||
| 673 | * |
||
| 674 | * @param mixed $value: Value to save |
||
| 675 | * @param string $key: Session data key for saving |
||
| 676 | * |
||
| 677 | * @return boolean TRUE on success, FALSE on failure |
||
| 678 | */ |
||
| 679 | public static function saveToSession($value, $key) { |
||
| 697 | } |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * This translates an internal "index_name" |
||
| 702 | * |
||
| 703 | * @access public |
||
| 704 | * |
||
| 705 | * @param string $index_name: The internal "index_name" to translate |
||
| 706 | * @param string $table: Get the translation from this table |
||
| 707 | * @param string $pid: Get the translation from this page |
||
| 708 | * |
||
| 709 | * @return string Localized label for $index_name |
||
| 710 | */ |
||
| 711 | public static function translate($index_name, $table, $pid) { |
||
| 712 | // Load labels into static variable for future use. |
||
| 713 | static $labels = []; |
||
| 714 | // Sanitize input. |
||
| 715 | $pid = max(intval($pid), 0); |
||
| 716 | if (!$pid) { |
||
| 717 | self::devLog('Invalid PID '.$pid.' for translation', DEVLOG_SEVERITY_WARNING); |
||
| 718 | return $index_name; |
||
| 719 | } |
||
| 720 | // Check if "index_name" is an UID. |
||
| 721 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($index_name)) { |
||
| 722 | $index_name = self::getIndexNameFromUid($index_name, $table, $pid); |
||
| 723 | } |
||
| 724 | /* $labels already contains the translated content element, but with the index_name of the translated content element itself |
||
| 725 | * and not with the $index_name of the original that we receive here. So we have to determine the index_name of the |
||
| 726 | * associated translated content element. E.g. $labels['title0'] != $index_name = title. */ |
||
| 727 | // First fetch the uid of the received index_name |
||
| 728 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 729 | 'uid, l18n_parent', |
||
| 730 | $table, |
||
| 731 | 'pid='.$pid |
||
| 732 | .' AND index_name="'.$index_name.'"' |
||
| 733 | .self::whereClause($table, TRUE), |
||
| 734 | '', |
||
| 735 | '', |
||
| 736 | '' |
||
| 737 | ); |
||
| 738 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) { |
||
| 739 | // Now we use the uid of the l18_parent to fetch the index_name of the translated content element. |
||
| 740 | $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); |
||
| 741 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 742 | 'index_name', |
||
| 743 | $table, |
||
| 744 | 'pid='.$pid |
||
| 745 | .' AND uid='.$resArray['l18n_parent'] |
||
| 746 | .' AND sys_language_uid='.intval($GLOBALS['TSFE']->sys_language_content) |
||
| 747 | .self::whereClause($table, TRUE), |
||
| 748 | '', |
||
| 749 | '', |
||
| 750 | '' |
||
| 751 | ); |
||
| 752 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) { |
||
| 753 | // If there is an translated content element, overwrite the received $index_name. |
||
| 754 | $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); |
||
| 755 | $index_name = $resArray['index_name']; |
||
| 756 | } |
||
| 757 | } |
||
| 758 | // Check if we already got a translation. |
||
| 759 | if (empty($labels[$table][$pid][$GLOBALS['TSFE']->sys_language_content][$index_name])) { |
||
| 760 | // Check if this table is allowed for translation. |
||
| 761 | if (in_array($table, ['tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_structures'])) { |
||
| 762 | $additionalWhere = ' AND sys_language_uid IN (-1,0)'; |
||
| 763 | if ($GLOBALS['TSFE']->sys_language_content > 0) { |
||
| 764 | $additionalWhere = ' AND (sys_language_uid IN (-1,0) OR (sys_language_uid='.intval($GLOBALS['TSFE']->sys_language_content).' AND l18n_parent=0))'; |
||
| 765 | } |
||
| 766 | // Get labels from database. |
||
| 767 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 768 | '*', |
||
| 769 | $table, |
||
| 770 | 'pid='.$pid |
||
| 771 | .$additionalWhere |
||
| 772 | .self::whereClause($table, TRUE), |
||
| 773 | '', |
||
| 774 | '', |
||
| 775 | '' |
||
| 776 | ); |
||
| 777 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) { |
||
| 778 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 779 | // Overlay localized labels if available. |
||
| 780 | if ($GLOBALS['TSFE']->sys_language_content > 0) { |
||
| 781 | $resArray = $GLOBALS['TSFE']->sys_page->getRecordOverlay($table, $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL); |
||
| 782 | } |
||
| 783 | if ($resArray) { |
||
| 784 | $labels[$table][$pid][$GLOBALS['TSFE']->sys_language_content][$resArray['index_name']] = $resArray['label']; |
||
| 785 | } |
||
| 786 | } |
||
| 787 | } else { |
||
| 788 | self::devLog('No translation with PID '.$pid.' available in table "'.$table.'" or translation not accessible', DEVLOG_SEVERITY_NOTICE); |
||
| 789 | } |
||
| 790 | } else { |
||
| 791 | self::devLog('No translations available for table "'.$table.'"', DEVLOG_SEVERITY_WARNING); |
||
| 792 | } |
||
| 793 | } |
||
| 794 | if (!empty($labels[$table][$pid][$GLOBALS['TSFE']->sys_language_content][$index_name])) { |
||
| 795 | return $labels[$table][$pid][$GLOBALS['TSFE']->sys_language_content][$index_name]; |
||
| 796 | } else { |
||
| 797 | return $index_name; |
||
| 798 | } |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * This returns the additional WHERE clause of a table based on its TCA configuration |
||
| 803 | * |
||
| 804 | * @access public |
||
| 805 | * |
||
| 806 | * @param string $table: Table name as defined in TCA |
||
| 807 | * @param boolean $showHidden: Ignore the hidden flag? |
||
| 808 | * |
||
| 809 | * @return string Additional WHERE clause |
||
| 810 | */ |
||
| 811 | public static function whereClause($table, $showHidden = FALSE) { |
||
| 812 | if (TYPO3_MODE === 'FE') { |
||
| 813 | // Table "tx_dlf_formats" always has PID 0. |
||
| 814 | if ($table == 'tx_dlf_formats') { |
||
| 815 | return \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($table); |
||
| 816 | } |
||
| 817 | // Should we ignore the record's hidden flag? |
||
| 818 | $ignoreHide = -1; |
||
| 819 | if ($showHidden) { |
||
| 820 | $ignoreHide = 1; |
||
| 821 | } |
||
| 822 | // $GLOBALS['TSFE']->sys_page is not always available in frontend. |
||
| 823 | if (is_object($GLOBALS['TSFE']->sys_page)) { |
||
| 824 | return $GLOBALS['TSFE']->sys_page->enableFields($table, $ignoreHide); |
||
| 825 | } else { |
||
| 826 | $pageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class); |
||
| 827 | $GLOBALS['TSFE']->includeTCA(); |
||
| 828 | return $pageRepository->enableFields($table, $ignoreHide); |
||
| 829 | } |
||
| 830 | } elseif (TYPO3_MODE === 'BE') { |
||
| 831 | return \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($table); |
||
| 832 | } else { |
||
| 833 | self::devLog('Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', DEVLOG_SEVERITY_ERROR); |
||
| 834 | return ' AND 1=-1'; |
||
| 835 | } |
||
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * This is a static class, thus no instances should be created |
||
| 840 | * |
||
| 841 | * @access private |
||
| 842 | */ |
||
| 843 | private function __construct() {} |
||
| 844 | } |
||
| 845 |