~slub.team/goobi-presentation/old-bzr-trunk

« back to all changes in this revision

Viewing changes to dlf/common/class.tx_dlf_helper.php

  • Committer: Sebastian Meyer
  • Date: 2012-08-17 07:02:36 UTC
  • mfrom: (82.1.54 Goobi.Presentation 1.1)
  • Revision ID: sebastian.meyer@slub-dresden.de-20120817070236-d6gh9s8l47q9k5j8
Merge changes from Goobi.Presentation CE 1.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
231
231
                // Check for PHP extension "mcrypt".
232
232
                if (!extension_loaded('mcrypt')) {
233
233
 
234
 
                        trigger_error('PHP extension "mcrypt" not available', E_USER_WARNING);
235
 
 
236
 
                        return NULL;
 
234
                        if (TYPO3_DLOG) {
 
235
 
 
236
                                t3lib_div::devLog('[tx_dlf_helper->decrypt('.$encrypted.', '.$hash.')] PHP extension "mcrypt" not available', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
237
 
 
238
                        }
 
239
 
 
240
                        return;
237
241
 
238
242
                }
239
243
 
240
244
                if (empty($encrypted) || empty($hash)) {
241
245
 
242
 
                        return NULL;
 
246
                        if (TYPO3_DLOG) {
 
247
 
 
248
                                t3lib_div::devLog('[tx_dlf_helper->decrypt('.$encrypted.', '.$hash.')] Invalid parameters given for decryption', $this->extKey, SYSLOG_SEVERITY_ERROR);
 
249
 
 
250
                        }
 
251
 
 
252
                        return;
243
253
 
244
254
                }
245
255
 
253
263
 
254
264
                if ($hashed !== $hash) {
255
265
 
256
 
                        return NULL;
 
266
                        if (TYPO3_DLOG) {
 
267
 
 
268
                                t3lib_div::devLog('[tx_dlf_helper->decrypt('.$encrypted.', '.$hash.')] Invalid hash "'.$hash.'" given for decryption', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
269
 
 
270
                        }
 
271
 
 
272
                        return;
257
273
 
258
274
                }
259
275
 
276
292
                // Check for PHP extension "mcrypt".
277
293
                if (!extension_loaded('mcrypt')) {
278
294
 
279
 
                        trigger_error('PHP extension "mcrypt" not available', E_USER_WARNING);
280
 
 
281
 
                        return NULL;
 
295
                        if (TYPO3_DLOG) {
 
296
 
 
297
                                t3lib_div::devLog('[tx_dlf_helper->encrypt('.$string.')] PHP extension "mcrypt" not available', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
298
 
 
299
                        }
 
300
 
 
301
                        return;
282
302
 
283
303
                }
284
304
 
305
325
 
306
326
                if (TYPO3_MODE === 'FE') {
307
327
 
308
 
                        // TODO: Anpassen! (aus typo3/init.php übernommen)
309
 
                        $userObj = t3lib_div::makeInstance('t3lib_beUserAuth');
310
 
 
311
 
                        $userObj->start();
312
 
 
313
 
                        $userObj->backendCheckLogin();
314
 
 
315
 
                        return $userObj;
 
328
                        // Check for existing backend login.
 
329
                        if ($GLOBALS['TSFE']->beUserLogin > 0 && isset($GLOBALS['BE_USER'])) {
 
330
 
 
331
                                return $GLOBALS['BE_USER'];
 
332
 
 
333
                        } elseif (!isset($_COOKIE['be_typo_user'])) {
 
334
 
 
335
                                // Initialize backend session with CLI user's rights.
 
336
                                $userObj = t3lib_div::makeInstance('t3lib_beUserAuth');
 
337
 
 
338
                                $userObj->dontSetCookie = TRUE;
 
339
 
 
340
                                $userObj->start();
 
341
 
 
342
                                $userObj->setBeUserByName('_cli_dlf');
 
343
 
 
344
                                $userObj->backendCheckLogin();
 
345
 
 
346
                                return $userObj;
 
347
 
 
348
                        } else {
 
349
 
 
350
                                if (TYPO3_DLOG) {
 
351
 
 
352
                                        t3lib_div::devLog('[tx_dlf_helper->getBeUser()] Could not determine current user\'s login status', $this->extKey, SYSLOG_SEVERITY_ERROR);
 
353
 
 
354
                                }
 
355
 
 
356
                                return;
 
357
 
 
358
                        }
316
359
 
317
360
                } elseif (TYPO3_MODE === 'BE') {
318
361
 
 
362
                        // Return current backend user.
319
363
                        return $GLOBALS['BE_USER'];
320
364
 
321
365
                } else {
322
366
 
323
 
                        trigger_error('Unexpected TYPO3_MODE', E_USER_WARNING);
 
367
                        if (TYPO3_DLOG) {
 
368
 
 
369
                                t3lib_div::devLog('[tx_dlf_helper->getBeUser()] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
 
370
 
 
371
                        }
324
372
 
325
373
                        return;
326
374
 
341
389
         */
342
390
        public static function getIndexName($uid, $table, $pid = -1) {
343
391
 
 
392
                // Save parameters for logging purposes.
 
393
                $_uid = $uid;
 
394
 
 
395
                $_pid = $pid;
 
396
 
 
397
                // Sanitize input.
344
398
                $uid = max(intval($uid), 0);
345
399
 
346
400
                if (!$uid || !in_array($table, array ('tx_dlf_collections', 'tx_dlf_libraries', 'tx_dlf_metadata', 'tx_dlf_structures', 'tx_dlf_solrcores'))) {
347
401
 
348
 
                        trigger_error('At least one argument is not valid: UID='.$uid.' or TABLE='.$table, E_USER_WARNING);
 
402
                        if (TYPO3_DLOG) {
 
403
 
 
404
                                t3lib_div::devLog('[tx_dlf_helper->getIndexName('.$_uid.', '.$table.', '.$_pid.')] Invalid UID "'.$uid.'" or table "'.$table.'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
 
405
 
 
406
                        }
349
407
 
350
408
                        return '';
351
409
 
353
411
 
354
412
                $where = '';
355
413
 
 
414
                // Should we check for a specific PID, too?
356
415
                if ($pid !== -1) {
357
416
 
358
417
                        $pid = max(intval($pid), 0);
361
420
 
362
421
                }
363
422
 
364
 
                $_result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
 
423
                // Get index_name from database.
 
424
                $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
365
425
                        $table.'.index_name AS index_name',
366
426
                        $table,
367
427
                        $table.'.uid='.$uid.$where.self::whereClause($table),
370
430
                        '1'
371
431
                );
372
432
 
373
 
                if ($GLOBALS['TYPO3_DB']->sql_num_rows($_result) > 0) {
 
433
                if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) {
374
434
 
375
 
                        $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($_result);
 
435
                        $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result);
376
436
 
377
437
                        return $resArray['index_name'];
378
438
 
 
439
                } else {
 
440
 
 
441
                        if (TYPO3_DLOG) {
 
442
 
 
443
                                t3lib_div::devLog('[tx_dlf_helper->getIndexName('.$_uid.', '.$table.', '.$_pid.')] No "index_name" with UID "'.$uid.'" and PID "'.$pid.'" found in table "'.$table.'"', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
444
 
 
445
                        }
 
446
 
 
447
                        return '';
 
448
 
379
449
                }
380
450
 
381
 
                trigger_error('No "index_name" with UID '.$uid.' found for PID '.$pid.' in TABLE '.$table, E_USER_WARNING);
382
 
 
383
 
                return '';
384
 
 
385
451
        }
386
452
 
387
453
        /**
395
461
         */
396
462
        public static function getLanguageName($code) {
397
463
 
398
 
                $_code = strtolower(trim($code));
399
 
 
400
 
                if (preg_match('/^[a-z]{3}$/', $_code)) {
401
 
 
402
 
                        $_file = t3lib_extMgm::extPath(self::$extKey).'lib/ISO-639/iso-639-2b.xml';
403
 
 
404
 
                } elseif (preg_match('/^[a-z]{2}$/', $_code)) {
405
 
 
406
 
                        $_file = t3lib_extMgm::extPath(self::$extKey).'lib/ISO-639/iso-639-1.xml';
 
464
                // Analyze code and set appropriate ISO table.
 
465
                $isoCode = strtolower(trim($code));
 
466
 
 
467
                if (preg_match('/^[a-z]{3}$/', $isoCode)) {
 
468
 
 
469
                        $file = t3lib_extMgm::extPath(self::$extKey).'lib/ISO-639/iso-639-2b.xml';
 
470
 
 
471
                } elseif (preg_match('/^[a-z]{2}$/', $isoCode)) {
 
472
 
 
473
                        $file = t3lib_extMgm::extPath(self::$extKey).'lib/ISO-639/iso-639-1.xml';
407
474
 
408
475
                } else {
409
476
 
 
477
                        // No ISO code, return unchanged.
410
478
                        return $code;
411
479
 
412
480
                }
413
481
 
 
482
                // Load ISO table and get localized full name of language.
414
483
                if (TYPO3_MODE === 'FE') {
415
484
 
416
 
                        $iso639 = $GLOBALS['TSFE']->readLLfile($_file);
417
 
 
418
 
                        if (!empty($iso639['default'][$_code])) {
419
 
 
420
 
                                $lang = $GLOBALS['TSFE']->getLLL($_code, $iso639);
 
485
                        $iso639 = $GLOBALS['TSFE']->readLLfile($file);
 
486
 
 
487
                        if (!empty($iso639['default'][$isoCode])) {
 
488
 
 
489
                                $lang = $GLOBALS['TSFE']->getLLL($isoCode, $iso639);
421
490
 
422
491
                        }
423
492
 
424
493
                } elseif (TYPO3_MODE === 'BE') {
425
494
 
426
 
                        $iso639 = $GLOBALS['LANG']->includeLLFile($_file, FALSE, TRUE);
427
 
 
428
 
                        if (!empty($iso639['default'][$_code])) {
429
 
 
430
 
                                $lang = $GLOBALS['LANG']->getLLL($_code, $iso639, FALSE);
 
495
                        $iso639 = $GLOBALS['LANG']->includeLLFile($file, FALSE, TRUE);
 
496
 
 
497
                        if (!empty($iso639['default'][$isoCode])) {
 
498
 
 
499
                                $lang = $GLOBALS['LANG']->getLLL($isoCode, $iso639, FALSE);
431
500
 
432
501
                        }
433
502
 
434
503
                } else {
435
504
 
436
 
                        trigger_error('Unexpected TYPO3_MODE', E_USER_WARNING);
 
505
                        if (TYPO3_DLOG) {
 
506
 
 
507
                                t3lib_div::devLog('[tx_dlf_helper->getLanguageName('.$code.')] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
 
508
 
 
509
                        }
437
510
 
438
511
                        return $code;
439
512
 
445
518
 
446
519
                } else {
447
520
 
448
 
                        trigger_error('Language code "'.$code.'" not found', E_USER_WARNING);
 
521
                        if (TYPO3_DLOG) {
 
522
 
 
523
                                t3lib_div::devLog('[tx_dlf_helper->getLanguageName('.$code.')] Language code "'.$code.'" not found in ISO-639 table', $this->extKey, SYSLOG_SEVERITY_NOTICE);
 
524
 
 
525
                        }
449
526
 
450
527
                        return $code;
451
528
 
466
543
         */
467
544
        public static function getURN($base, $id) {
468
545
 
469
 
                $concordance = array(
 
546
                $concordance = array (
470
547
                        '0' => 1,
471
548
                        '1' => 2,
472
549
                        '2' => 3,
511
588
 
512
589
                if (preg_match('/[^a-z0-9:-]/', $urn)) {
513
590
 
514
 
                        trigger_error('Invalid chars in URN', E_USER_WARNING);
 
591
                        if (TYPO3_DLOG) {
 
592
 
 
593
                                t3lib_div::devLog('[tx_dlf_helper->getURN('.$base.', '.$id.')] Invalid chars in given parameters', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
594
 
 
595
                        }
515
596
 
516
597
                        return '';
517
598
 
559
640
         *
560
641
         * @access      public
561
642
         *
562
 
         * @param       string          $key: Session key for retrieval
 
643
         * @param       string          $key: Session data key for retrieval
563
644
         *
564
645
         * @return      mixed           Session value for given key or NULL on failure
565
646
         */
566
647
        public static function loadFromSession($key) {
567
648
 
 
649
                // Save parameter for logging purposes.
 
650
                $_key = $key;
 
651
 
568
652
                // Cast to string for security reasons.
569
653
                $key = (string) $key;
570
654
 
571
655
                if (!$key) {
572
656
 
573
 
                        trigger_error('No session key given', E_USER_WARNING);
 
657
                        if (TYPO3_DLOG) {
 
658
 
 
659
                                t3lib_div::devLog('[tx_dlf_helper->loadFromSession('.$_key.')] Invalid key "'.$key.'" for session data retrieval', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
660
 
 
661
                        }
574
662
 
575
663
                        return;
576
664
 
577
665
                }
578
666
 
 
667
                // Get the session data.
579
668
                if (TYPO3_MODE === 'FE') {
580
669
 
581
670
                        return $GLOBALS['TSFE']->fe_user->getKey('ses', $key);
586
675
 
587
676
                } else {
588
677
 
589
 
                        trigger_error('Unexpected TYPO3_MODE', E_USER_WARNING);
 
678
                        if (TYPO3_DLOG) {
 
679
 
 
680
                                t3lib_div::devLog('[tx_dlf_helper->loadFromSession('.$_key.')] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
 
681
 
 
682
                        }
590
683
 
591
684
                        return;
592
685
 
647
740
         * @access      public
648
741
         *
649
742
         * @param       string          $value: Value to save
650
 
         * @param       string          $key: Session key for retrieval
 
743
         * @param       string          $key: Session data key for saving
651
744
         *
652
745
         * @return      boolean         TRUE on success, FALSE on failure
653
746
         */
654
747
        public static function saveToSession($value, $key) {
655
748
 
 
749
                // Save parameter for logging purposes.
 
750
                $_key = $key;
 
751
 
656
752
                // Cast to string for security reasons.
657
753
                $key = (string) $key;
658
754
 
659
755
                if (!$key) {
660
756
 
661
 
                        trigger_error('No session key given', E_USER_WARNING);
 
757
                        if (TYPO3_DLOG) {
 
758
 
 
759
                                t3lib_div::devLog('[tx_dlf_helper->saveToSession('.$value.', '.$_key.')] Invalid key "'.$key.'" for session data saving', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
760
 
 
761
                        }
662
762
 
663
763
                        return FALSE;
664
764
 
665
765
                }
666
766
 
 
767
                // Save value in session data.
667
768
                if (TYPO3_MODE === 'FE') {
668
769
 
669
770
                        $GLOBALS['TSFE']->fe_user->setKey('ses', $key, $value);
680
781
 
681
782
                } else {
682
783
 
683
 
                        trigger_error('Unexpected TYPO3_MODE', E_USER_WARNING);
 
784
                        if (TYPO3_DLOG) {
 
785
 
 
786
                                t3lib_div::devLog('[tx_dlf_helper->saveToSession('.$value.', '.$_key.')] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
 
787
 
 
788
                        }
684
789
 
685
790
                        return FALSE;
686
791
 
689
794
        }
690
795
 
691
796
        /**
692
 
         * This validates a METS file against its schemas
693
 
         * TODO: nicht funktionstüchtig!
694
 
         *
695
 
         * @access      public
696
 
         *
697
 
         * @param       SimpleXMLElement                $xml:
698
 
         *
699
 
         * @return      void
700
 
         */
701
 
        public static function schemaValidate(SimpleXMLElement $xml) {
702
 
 
703
 
                $_libxmlErrors = libxml_use_internal_errors(TRUE);
704
 
 
705
 
                // Get schema locations.
706
 
                $xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
707
 
 
708
 
                $_schemaLocations = $xml->xpath('//*[@xsi:schemaLocation]');
709
 
 
710
 
                foreach ($_schemaLocations as $_schemaLocation) {
711
 
 
712
 
                        $_schemas = explode(' ', (string) $_schemaLocation->attributes('http://www.w3.org/2001/XMLSchema-instance')->schemaLocation);
713
 
 
714
 
                        for ($i = 1, $j = count($_schemas); $i <= $j; $i++) {
715
 
 
716
 
                                if ($_schemas[$i] == 'http://www.loc.gov/METS/') {
717
 
 
718
 
                                        $schema['mets'] = $_schemas[$i + 1];
719
 
 
720
 
                                } elseif ($_schemas[$i] == 'http://www.loc.gov/mods/v3') {
721
 
 
722
 
                                        $schema['mods'] = $_schemas[$i + 1];
723
 
 
724
 
                                }
725
 
 
726
 
                        }
727
 
 
728
 
                }
729
 
                // TODO: Error-Handling (keine Schemas gefunden)
730
 
 
731
 
                // Validate METS part against schema.
732
 
                $dom = new DOMDocument('1.0', 'UTF-8');
733
 
 
734
 
                $dom->appendChild($dom->importNode(dom_import_simplexml($this->mets), TRUE));
735
 
 
736
 
                $dom->schemaValidate($schema['mets']);
737
 
 
738
 
                // TODO: Error-Handling (invalider METS-Part)
739
 
                // libxml_get_last_error() || libxml_get_errors() || libxml_clear_errors()
740
 
 
741
 
                // Validate dmdSec parts against schema.
742
 
                foreach ($this->dmdSec as $dmdSec) {
743
 
 
744
 
                        switch ($dmdSec['type']) {
745
 
 
746
 
                                case 'MODS':
747
 
 
748
 
                                        $dom = new DOMDocument('1.0', 'UTF-8');
749
 
 
750
 
                                        $dom->appendChild($dom->importNode(dom_import_simplexml($dmdSec['xml']), TRUE));
751
 
 
752
 
                                        $dom->schemaValidate($schema['mods']);
753
 
 
754
 
                                        // TODO: Error-Handling (invalider MODS-Part)
755
 
                                        // libxml_get_last_error() || libxml_get_errors() || libxml_clear_errors()
756
 
 
757
 
                                        break;
758
 
 
759
 
                        }
760
 
 
761
 
                }
762
 
 
763
 
                libxml_use_internal_errors($_libxmlErrors);
764
 
 
765
 
        }
766
 
 
767
 
        /**
768
797
         * This translates an internal "index_name"
769
798
         *
770
799
         * @access      public
777
806
         */
778
807
        public static function translate($index_name, $table, $pid) {
779
808
 
 
809
                // Save parameters for logging purposes.
 
810
                $_index_name = $index_name;
 
811
 
 
812
                $_pid = $pid;
 
813
 
 
814
                // Load labels into static variable for future use.
780
815
                static $labels = array ();
781
816
 
 
817
                // Sanitize input.
782
818
                $pid = max(intval($pid), 0);
783
819
 
784
820
                if (!$pid) {
785
821
 
786
 
                        trigger_error('No PID given for translations', E_USER_WARNING);
 
822
                        if (TYPO3_DLOG) {
 
823
 
 
824
                                t3lib_div::devLog('[tx_dlf_helper->translate('.$_index_name.', '.$table.', '.$_pid.')] Invalid PID "'.$pid.'" for translation', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
825
 
 
826
                        }
787
827
 
788
828
                        return $index_name;
789
829
 
811
851
                                }
812
852
 
813
853
                                // Get labels from database.
814
 
                                $_result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
 
854
                                $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
815
855
                                        '*',
816
856
                                        $table,
817
857
                                        'pid='.$pid.$additionalWhere.self::whereClause($table),
820
860
                                        ''
821
861
                                );
822
862
 
823
 
                                if ($GLOBALS['TYPO3_DB']->sql_num_rows($_result) > 0) {
 
863
                                if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) {
824
864
 
825
 
                                        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($_result)) {
 
865
                                        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
826
866
 
827
867
                                                // Overlay localized labels if available.
828
868
                                                if ($GLOBALS['TSFE']->sys_language_content > 0) {
841
881
 
842
882
                                } else {
843
883
 
844
 
                                        trigger_error('There are no entries with PID '.$pid.' in table '.$table.' or you are not allowed to access them', E_USER_ERROR);
 
884
                                        if (TYPO3_DLOG) {
 
885
 
 
886
                                                t3lib_div::devLog('[tx_dlf_helper->translate('.$_index_name.', '.$table.', '.$_pid.')] No translation with PID "'.$pid.'" available in table "'.$table.'" or translation not accessible', $this->extKey, SYSLOG_SEVERITY_NOTICE);
 
887
 
 
888
                                        }
845
889
 
846
890
                                }
847
891
 
848
892
                        } else {
849
893
 
850
 
                                trigger_error('The table '.$table.' is not allowed for translation', E_USER_ERROR);
 
894
                                if (TYPO3_DLOG) {
 
895
 
 
896
                                        t3lib_div::devLog('[tx_dlf_helper->translate('.$_index_name.', '.$table.', '.$_pid.')] No translations available for table "'.$table.'"', $this->extKey, SYSLOG_SEVERITY_WARNING);
 
897
 
 
898
                                }
851
899
 
852
900
                        }
853
901
 
916
964
 
917
965
                } else {
918
966
 
919
 
                        trigger_error('Unexpected TYPO3_MODE', E_USER_ERROR);
 
967
                        if (TYPO3_DLOG) {
 
968
 
 
969
                                t3lib_div::devLog('[tx_dlf_helper->whereClause('.$table.', ['.($showHidden ? 'TRUE' : 'FALSE').'])] Unexpected TYPO3_MODE "'.TYPO3_MODE.'"', $this->extKey, SYSLOG_SEVERITY_ERROR);
 
970
 
 
971
                        }
920
972
 
921
973
                        return ' AND 1=-1';
922
974