Subversion Repositories oidplus

Rev

Rev 1112 | Rev 1117 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1112 Rev 1116
Line 22... Line 22...
22
// phpcs:disable PSR1.Files.SideEffects
22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
24
// phpcs:enable PSR1.Files.SideEffects
25
 
25
 
26
class OIDplus extends OIDplusBaseClass {
26
class OIDplus extends OIDplusBaseClass {
-
 
27
        /**
-
 
28
         * @var OIDplusPagePlugin[]
-
 
29
         */
27
        private static /*OIDplusPagePlugin[]*/ $pagePlugins = array();
30
        private static /*OIDplusPagePlugin[]*/ $pagePlugins = array();
-
 
31
        /**
-
 
32
         * @var OIDplusAuthPlugin[]
-
 
33
         */
28
        private static /*OIDplusAuthPlugin[]*/ $authPlugins = array();
34
        private static /*OIDplusAuthPlugin[]*/ $authPlugins = array();
-
 
35
        /**
-
 
36
         * @var OIDplusLoggerPlugin[]
-
 
37
         */
29
        private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array();
38
        private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array();
-
 
39
        /**
-
 
40
         * @var OIDplusObjectTypePlugin[]
-
 
41
         */
30
        private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array();
42
        private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array();
-
 
43
        /**
-
 
44
         * @var string[]|OIDplusObject[] Classnames of OIDplusObject classes
-
 
45
         */
31
        private static /*string[]*/ $enabledObjectTypes = array();
46
        private static /*string[]*/ $enabledObjectTypes = array();
-
 
47
        /**
-
 
48
         * @var string[]|OIDplusObject[] Classnames of OIDplusObject classes
-
 
49
         */
32
        private static /*string[]*/ $disabledObjectTypes = array();
50
        private static /*string[]*/ $disabledObjectTypes = array();
-
 
51
        /**
-
 
52
         * @var OIDplusDatabasePlugin[]
-
 
53
         */
33
        private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array();
54
        private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array();
-
 
55
        /**
-
 
56
         * @var OIDplusCaptchaPlugin[]
-
 
57
         */
34
        private static /*OIDplusCaptchaPlugin[]*/ $captchaPlugins = array();
58
        private static /*OIDplusCaptchaPlugin[]*/ $captchaPlugins = array();
-
 
59
        /**
-
 
60
         * @var OIDplusSqlSlangPlugin[]
-
 
61
         */
35
        private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array();
62
        private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array();
-
 
63
        /**
-
 
64
         * @var OIDplusLanguagePlugin[]
-
 
65
         */
36
        private static /*OIDplusLanguagePlugin[]*/ $languagePlugins = array();
66
        private static /*OIDplusLanguagePlugin[]*/ $languagePlugins = array();
-
 
67
        /**
-
 
68
         * @var OIDplusDesignPlugin[]
-
 
69
         */
37
        private static /*OIDplusDesignPlugin[]*/ $designPlugins = array();
70
        private static /*OIDplusDesignPlugin[]*/ $designPlugins = array();
38
 
71
 
-
 
72
        /**
-
 
73
         * @var bool
-
 
74
         */
39
        protected static $html = true;
75
        protected static $html = true;
40
 
76
 
-
 
77
        /**
-
 
78
         * e.g. "../"
-
 
79
         */
41
        /*public*/ const PATH_RELATIVE = 1;                   // e.g. "../"
80
        /*public*/ const PATH_RELATIVE = 1;
-
 
81
 
-
 
82
        /**
42
        /*public*/ const PATH_ABSOLUTE = 2;                   // e.g. "http://www.example.com/oidplus/"
83
         * e.g. "http://www.example.com/oidplus/"
-
 
84
         */
-
 
85
        /*public*/ const PATH_ABSOLUTE = 2;
-
 
86
 
-
 
87
        /**
43
        /*public*/ const PATH_ABSOLUTE_CANONICAL = 3;         // e.g. "http://www.example.org/oidplus/" (if baseconfig CANONICAL_SYSTEM_URL is set)
88
         * e.g. "http://www.example.org/oidplus/" (if baseconfig CANONICAL_SYSTEM_URL is set)
-
 
89
         */
-
 
90
        /*public*/ const PATH_ABSOLUTE_CANONICAL = 3;
-
 
91
 
-
 
92
        /**
-
 
93
         * e.g. "/oidplus/"
-
 
94
         */
44
        /*public*/ const PATH_RELATIVE_TO_ROOT = 4;           // e.g. "/oidplus/"
95
        /*public*/ const PATH_RELATIVE_TO_ROOT = 4;
-
 
96
 
-
 
97
        /**
45
        /*public*/ const PATH_RELATIVE_TO_ROOT_CANONICAL = 5; // e.g. "/oidplus/" (if baseconfig CANONICAL_SYSTEM_URL is set)
98
         * e.g. "/oidplus/" (if baseconfig CANONICAL_SYSTEM_URL is set)
-
 
99
         */
-
 
100
        /*public*/ const PATH_RELATIVE_TO_ROOT_CANONICAL = 5;
46
 
101
 
-
 
102
        /**
47
        // These plugin types can contain HTML code and therefore may
103
         * These plugin types can contain HTML code and therefore may
48
        // emit (non-setup) CSS/JS code via their manifest.
104
         * emit (non-setup) CSS/JS code via their manifest.
49
        // Note that design plugins may only output CSS, not JS.
105
         * Note that design plugins may only output CSS, not JS.
-
 
106
         */
50
        /*public*/ const INTERACTIVE_PLUGIN_TYPES = array(
107
        /*public*/ const INTERACTIVE_PLUGIN_TYPES = array(
51
                'publicPages',
108
                'publicPages',
52
                'raPages',
109
                'raPages',
53
                'adminPages',
110
                'adminPages',
54
                'objectTypes',
111
                'objectTypes',
55
                'captcha'
112
                'captcha'
56
        );
113
        );
57
 
114
 
58
        const UUID_NAMEBASED_NS_Base64PubKey = 'fd16965c-8bab-11ed-8744-3c4a92df8582';
115
        const UUID_NAMEBASED_NS_Base64PubKey = 'fd16965c-8bab-11ed-8744-3c4a92df8582';
59
 
116
 
-
 
117
        /**
-
 
118
         * Private constructor (Singleton)
-
 
119
         */
60
        private function __construct() {
120
        private function __construct() {
61
        }
121
        }
62
 
122
 
-
 
123
        /**
-
 
124
         * @return bool
-
 
125
         * @throws OIDplusException
-
 
126
         */
63
        private static function insideSetup(): bool {
127
        private static function insideSetup(): bool {
64
                if (PHP_SAPI == 'cli') return false;
128
                if (PHP_SAPI == 'cli') return false;
65
                if (!isset($_SERVER['REQUEST_URI'])) return false;
129
                if (!isset($_SERVER['REQUEST_URI'])) return false;
66
                return (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,OIDplus::PATH_RELATIVE_TO_ROOT).'setup/') === 0);
130
                return (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,OIDplus::PATH_RELATIVE_TO_ROOT).'setup/') === 0);
67
        }
131
        }
68
 
132
 
69
        // --- Static classes
133
        // --- Static classes
70
 
134
 
71
        private static $baseConfig = null;
135
        private static $baseConfig = null;
72
        private static $oldConfigFormatLoaded = false;
136
        private static $oldConfigFormatLoaded = false;
73
        public static function baseConfig() {
-
 
74
                $first_init = false;
-
 
75
 
137
 
-
 
138
        /**
-
 
139
         * @return OIDplusBaseConfig
-
 
140
         * @throws OIDplusException, OIDplusConfigInitializationException
-
 
141
         */
-
 
142
        public static function baseConfig(): OIDplusBaseConfig {
76
                if ($first_init = is_null(self::$baseConfig)) {
143
                if ($first_init = is_null(self::$baseConfig)) {
77
                        self::$baseConfig = new OIDplusBaseConfig();
144
                        self::$baseConfig = new OIDplusBaseConfig();
78
                }
145
                }
79
 
146
 
80
                if (self::insideSetup()) return self::$baseConfig;
147
                if (self::insideSetup()) return self::$baseConfig;
Line 198... Line 265...
198
 
265
 
199
                return self::$baseConfig;
266
                return self::$baseConfig;
200
        }
267
        }
201
 
268
 
202
        private static $config = null;
269
        private static $config = null;
-
 
270
 
-
 
271
        /**
-
 
272
         * @return OIDplusConfig
-
 
273
         * @throws OIDplusException
-
 
274
         */
203
        public static function config() {
275
        public static function config(): OIDplusConfig {
204
                if ($first_init = is_null(self::$config)) {
276
                if ($first_init = is_null(self::$config)) {
205
                        self::$config = new OIDplusConfig();
277
                        self::$config = new OIDplusConfig();
206
                }
278
                }
207
 
279
 
208
                if ($first_init) {
280
                if ($first_init) {
Line 300... Line 372...
300
 
372
 
301
                return self::$config;
373
                return self::$config;
302
        }
374
        }
303
 
375
 
304
        private static $gui = null;
376
        private static $gui = null;
-
 
377
 
-
 
378
        /**
-
 
379
         * @return OIDplusGui
-
 
380
         */
305
        public static function gui() {
381
        public static function gui(): OIDplusGui {
306
                if (is_null(self::$gui)) {
382
                if (is_null(self::$gui)) {
307
                        self::$gui = new OIDplusGui();
383
                        self::$gui = new OIDplusGui();
308
                }
384
                }
309
                return self::$gui;
385
                return self::$gui;
310
        }
386
        }
311
 
387
 
312
        private static $authUtils = null;
388
        private static $authUtils = null;
-
 
389
 
-
 
390
        /**
-
 
391
         * @return OIDplusAuthUtils
-
 
392
         */
313
        public static function authUtils() {
393
        public static function authUtils(): OIDplusAuthUtils {
314
                if (is_null(self::$authUtils)) {
394
                if (is_null(self::$authUtils)) {
315
                        self::$authUtils = new OIDplusAuthUtils();
395
                        self::$authUtils = new OIDplusAuthUtils();
316
                }
396
                }
317
                return self::$authUtils;
397
                return self::$authUtils;
318
        }
398
        }
319
 
399
 
320
        private static $mailUtils = null;
400
        private static $mailUtils = null;
-
 
401
 
-
 
402
        /**
-
 
403
         * @return OIDplusMailUtils
-
 
404
         */
321
        public static function mailUtils() {
405
        public static function mailUtils(): OIDplusMailUtils {
322
                if (is_null(self::$mailUtils)) {
406
                if (is_null(self::$mailUtils)) {
323
                        self::$mailUtils = new OIDplusMailUtils();
407
                        self::$mailUtils = new OIDplusMailUtils();
324
                }
408
                }
325
                return self::$mailUtils;
409
                return self::$mailUtils;
326
        }
410
        }
327
 
411
 
328
        private static $cookieUtils = null;
412
        private static $cookieUtils = null;
-
 
413
 
-
 
414
        /**
-
 
415
         * @return OIDplusCookieUtils
-
 
416
         */
329
        public static function cookieUtils() {
417
        public static function cookieUtils(): OIDplusCookieUtils {
330
                if (is_null(self::$cookieUtils)) {
418
                if (is_null(self::$cookieUtils)) {
331
                        self::$cookieUtils = new OIDplusCookieUtils();
419
                        self::$cookieUtils = new OIDplusCookieUtils();
332
                }
420
                }
333
                return self::$cookieUtils;
421
                return self::$cookieUtils;
334
        }
422
        }
335
 
423
 
336
        private static $menuUtils = null;
424
        private static $menuUtils = null;
-
 
425
 
-
 
426
        /**
-
 
427
         * @return OIDplusMenuUtils
-
 
428
         */
337
        public static function menuUtils() {
429
        public static function menuUtils(): OIDplusMenuUtils {
338
                if (is_null(self::$menuUtils)) {
430
                if (is_null(self::$menuUtils)) {
339
                        self::$menuUtils = new OIDplusMenuUtils();
431
                        self::$menuUtils = new OIDplusMenuUtils();
340
                }
432
                }
341
                return self::$menuUtils;
433
                return self::$menuUtils;
342
        }
434
        }
343
 
435
 
344
        private static $logger = null;
436
        private static $logger = null;
-
 
437
 
-
 
438
        /**
-
 
439
         * @return OIDplusLogger
-
 
440
         */
345
        public static function logger() {
441
        public static function logger(): OIDplusLogger {
346
                if (is_null(self::$logger)) {
442
                if (is_null(self::$logger)) {
347
                        self::$logger = new OIDplusLogger();
443
                        self::$logger = new OIDplusLogger();
348
                }
444
                }
349
                return self::$logger;
445
                return self::$logger;
350
        }
446
        }
351
 
447
 
352
        // --- SQL slang plugin
448
        // --- SQL slang plugin
353
 
449
 
354
        /**
450
        /**
-
 
451
         * @param OIDplusSqlSlangPlugin $plugin
355
        * @return void
452
         * @return void
-
 
453
         * @throws OIDplusException
356
        */
454
         */
357
        private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) {
455
        private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) {
358
                $name = $plugin::id();
456
                $name = $plugin::id();
359
 
457
 
360
                if ($name === '') {
458
                if ($name === '') {
Line 367... Line 465...
367
                }
465
                }
368
 
466
 
369
                self::$sqlSlangPlugins[$name] = $plugin;
467
                self::$sqlSlangPlugins[$name] = $plugin;
370
        }
468
        }
371
 
469
 
-
 
470
        /**
-
 
471
         * @return OIDplusSqlSlangPlugin[]
-
 
472
         */
372
        public static function getSqlSlangPlugins() {
473
        public static function getSqlSlangPlugins(): array {
373
                return self::$sqlSlangPlugins;
474
                return self::$sqlSlangPlugins;
374
        }
475
        }
375
 
476
 
-
 
477
        /**
-
 
478
         * @param string $id
-
 
479
         * @return OIDplusSqlSlangPlugin|null
-
 
480
         */
376
        public static function getSqlSlangPlugin($id)/*: ?OIDplusSqlSlangPlugin*/ {
481
        public static function getSqlSlangPlugin(string $id)/*: ?OIDplusSqlSlangPlugin*/ {
377
                if (isset(self::$sqlSlangPlugins[$id])) {
482
                if (isset(self::$sqlSlangPlugins[$id])) {
378
                        return self::$sqlSlangPlugins[$id];
483
                        return self::$sqlSlangPlugins[$id];
379
                } else {
484
                } else {
380
                        return null;
485
                        return null;
381
                }
486
                }
382
        }
487
        }
383
 
488
 
384
        // --- Database plugin
489
        // --- Database plugin
385
 
490
 
386
        /**
491
        /**
-
 
492
         * @param OIDplusDatabasePlugin $plugin
387
        * @return void
493
         * @return void
-
 
494
         * @throws OIDplusException
388
        */
495
         */
389
        private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) {
496
        private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) {
390
                $name = $plugin::id();
497
                $name = $plugin::id();
391
 
498
 
392
                if ($name === '') {
499
                if ($name === '') {
Line 399... Line 506...
399
                }
506
                }
400
 
507
 
401
                self::$dbPlugins[$name] = $plugin;
508
                self::$dbPlugins[$name] = $plugin;
402
        }
509
        }
403
 
510
 
-
 
511
        /**
-
 
512
         * @return OIDplusDatabasePlugin[]
-
 
513
         */
404
        public static function getDatabasePlugins() {
514
        public static function getDatabasePlugins(): array {
405
                return self::$dbPlugins;
515
                return self::$dbPlugins;
406
        }
516
        }
407
 
517
 
-
 
518
        /**
-
 
519
         * @return OIDplusDatabasePlugin
-
 
520
         * @throws OIDplusException, OIDplusConfigInitializationException
-
 
521
         */
408
        public static function getActiveDatabasePlugin() {
522
        public static function getActiveDatabasePlugin(): OIDplusDatabasePlugin {
409
                $db_plugin_name = OIDplus::baseConfig()->getValue('DATABASE_PLUGIN','');
523
                $db_plugin_name = OIDplus::baseConfig()->getValue('DATABASE_PLUGIN','');
410
                if ($db_plugin_name === '') {
524
                if ($db_plugin_name === '') {
411
                        throw new OIDplusConfigInitializationException(_L('No database plugin selected in config file'));
525
                        throw new OIDplusConfigInitializationException(_L('No database plugin selected in config file'));
412
                }
526
                }
413
                foreach (self::$dbPlugins as $name => $plugin) {
527
                foreach (self::$dbPlugins as $name => $plugin) {
Line 416... Line 530...
416
                        }
530
                        }
417
                }
531
                }
418
                throw new OIDplusConfigInitializationException(_L('Database plugin "%1" not found',$db_plugin_name));
532
                throw new OIDplusConfigInitializationException(_L('Database plugin "%1" not found',$db_plugin_name));
419
        }
533
        }
420
 
534
 
-
 
535
        /**
-
 
536
         * @var OIDplusDatabaseConnection|null
-
 
537
         */
421
        private static $dbMainSession = null;
538
        private static $dbMainSession = null;
-
 
539
 
-
 
540
        /**
-
 
541
         * @return OIDplusDatabaseConnection
-
 
542
         * @throws OIDplusException, OIDplusConfigInitializationException
-
 
543
         */
422
        public static function db() {
544
        public static function db(): OIDplusDatabaseConnection {
423
                if (is_null(self::$dbMainSession)) {
545
                if (is_null(self::$dbMainSession)) {
424
                        self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection();
546
                        self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection();
425
                }
547
                }
426
                if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect();
548
                if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect();
427
                return self::$dbMainSession;
549
                return self::$dbMainSession;
428
        }
550
        }
429
 
551
 
-
 
552
        /**
-
 
553
         * @var OIDplusDatabaseConnection|null
-
 
554
         */
430
        private static $dbIsolatedSession = null;
555
        private static $dbIsolatedSession = null;
-
 
556
 
-
 
557
        /**
-
 
558
         * @return OIDplusDatabaseConnection
-
 
559
         * @throws OIDplusException, OIDplusConfigInitializationException
-
 
560
         */
431
        public static function dbIsolated() {
561
        public static function dbIsolated(): OIDplusDatabaseConnection {
432
                if (is_null(self::$dbIsolatedSession)) {
562
                if (is_null(self::$dbIsolatedSession)) {
433
                        self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection();
563
                        self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection();
434
                }
564
                }
435
                if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect();
565
                if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect();
436
                return self::$dbIsolatedSession;
566
                return self::$dbIsolatedSession;
437
        }
567
        }
438
 
568
 
439
        // --- CAPTCHA plugin
569
        // --- CAPTCHA plugin
440
 
570
 
441
        /**
571
        /**
-
 
572
         * @param OIDplusCaptchaPlugin $plugin
442
        * @return void
573
         * @return void
-
 
574
         * @throws OIDplusException
443
        */
575
         */
444
        private static function registerCaptchaPlugin(OIDplusCaptchaPlugin $plugin) {
576
        private static function registerCaptchaPlugin(OIDplusCaptchaPlugin $plugin) {
445
                $name = $plugin::id();
577
                $name = $plugin::id();
446
 
578
 
447
                if ($name === '') {
579
                if ($name === '') {
Line 454... Line 586...
454
                }
586
                }
455
 
587
 
456
                self::$captchaPlugins[$name] = $plugin;
588
                self::$captchaPlugins[$name] = $plugin;
457
        }
589
        }
458
 
590
 
-
 
591
        /**
-
 
592
         * @return OIDplusCaptchaPlugin[]
-
 
593
         */
459
        public static function getCaptchaPlugins() {
594
        public static function getCaptchaPlugins(): array {
460
                return self::$captchaPlugins;
595
                return self::$captchaPlugins;
461
        }
596
        }
462
 
597
 
-
 
598
        /**
-
 
599
         * @return string
-
 
600
         * @throws OIDplusException, OIDplusConfigInitializationException
-
 
601
         */
463
        public static function getActiveCaptchaPluginId() {
602
        public static function getActiveCaptchaPluginId(): string {
464
                $captcha_plugin_name = OIDplus::baseConfig()->getValue('CAPTCHA_PLUGIN', '');
603
                $captcha_plugin_name = OIDplus::baseConfig()->getValue('CAPTCHA_PLUGIN', '');
465
 
604
 
466
                if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) && ($captcha_plugin_name === '')) {
605
                if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) && ($captcha_plugin_name === '')) {
467
                        // Legacy config file support!
606
                        // Legacy config file support!
468
                        $captcha_plugin_name = 'reCAPTCHA';
607
                        $captcha_plugin_name = 'reCAPTCHA';
Line 471... Line 610...
471
                if ($captcha_plugin_name === '') $captcha_plugin_name = 'None'; // the "None" plugin is a must-have!
610
                if ($captcha_plugin_name === '') $captcha_plugin_name = 'None'; // the "None" plugin is a must-have!
472
 
611
 
473
                return $captcha_plugin_name;
612
                return $captcha_plugin_name;
474
        }
613
        }
475
 
614
 
-
 
615
        /**
-
 
616
         * @return OIDplusCaptchaPlugin
-
 
617
         * @throws OIDplusException, OIDplusConfigInitializationException
-
 
618
         */
476
        public static function getActiveCaptchaPlugin() {
619
        public static function getActiveCaptchaPlugin(): OIDplusCaptchaPlugin {
477
                $captcha_plugin_name = OIDplus::getActiveCaptchaPluginId();
620
                $captcha_plugin_name = OIDplus::getActiveCaptchaPluginId();
478
                foreach (self::$captchaPlugins as $name => $plugin) {
621
                foreach (self::$captchaPlugins as $name => $plugin) {
479
                        if (strtolower($name) == strtolower($captcha_plugin_name)) {
622
                        if (strtolower($name) == strtolower($captcha_plugin_name)) {
480
                                return $plugin;
623
                                return $plugin;
481
                        }
624
                        }
Line 484... Line 627...
484
        }
627
        }
485
 
628
 
486
        // --- Page plugin
629
        // --- Page plugin
487
 
630
 
488
        /**
631
        /**
-
 
632
         * @param OIDplusPagePlugin $plugin
489
        * @return void
633
         * @return void
490
        */
634
         */
491
        private static function registerPagePlugin(OIDplusPagePlugin $plugin) {
635
        private static function registerPagePlugin(OIDplusPagePlugin $plugin) {
492
                self::$pagePlugins[] = $plugin;
636
                self::$pagePlugins[] = $plugin;
493
        }
637
        }
494
 
638
 
-
 
639
        /**
-
 
640
         * @return OIDplusPagePlugin[]
-
 
641
         */
495
        public static function getPagePlugins() {
642
        public static function getPagePlugins(): array {
496
                return self::$pagePlugins;
643
                return self::$pagePlugins;
497
        }
644
        }
498
 
645
 
499
        // --- Auth plugin
646
        // --- Auth plugin
500
 
647
 
-
 
648
        /**
-
 
649
         * @param string $foldername
-
 
650
         * @return OIDplusAuthPlugin|null
-
 
651
         */
501
        public static function getAuthPluginByFoldername($foldername)/*: ?OIDplusAuthPlugin*/ {
652
        public static function getAuthPluginByFoldername(string $foldername)/*: ?OIDplusAuthPlugin*/ {
502
                $plugins = OIDplus::getAuthPlugins();
653
                $plugins = OIDplus::getAuthPlugins();
503
                foreach ($plugins as $plugin) {
654
                foreach ($plugins as $plugin) {
504
                        if (basename($plugin->getPluginDirectory()) === $foldername) {
655
                        if (basename($plugin->getPluginDirectory()) === $foldername) {
505
                                return $plugin;
656
                                return $plugin;
506
                        }
657
                        }
507
                }
658
                }
508
                return null;
659
                return null;
509
        }
660
        }
510
 
661
 
-
 
662
        /**
-
 
663
         * @param string $plugin_foldername
-
 
664
         * @param bool $must_hash
-
 
665
         * @return void
-
 
666
         * @throws OIDplusException
-
 
667
         */
511
        private static function checkRaAuthPluginAvailable($plugin_foldername, $must_hash) {
668
        private static function checkRaAuthPluginAvailable(string $plugin_foldername, bool $must_hash) {
512
                        // if (!wildcard_is_dir(OIDplus::localpath().'plugins/'.'*'.'/auth/'.$plugin_foldername)) {
669
                        // if (!wildcard_is_dir(OIDplus::localpath().'plugins/'.'*'.'/auth/'.$plugin_foldername)) {
513
                        $plugin = OIDplus::getAuthPluginByFoldername($plugin_foldername);
670
                        $plugin = OIDplus::getAuthPluginByFoldername($plugin_foldername);
514
                        if (is_null($plugin)) {
671
                        if (is_null($plugin)) {
515
                                throw new OIDplusException(_L('The auth plugin "%1" does not exist in plugin directory %2',$plugin_foldername,'plugins/[vendorname]/auth/'));
672
                                throw new OIDplusException(_L('The auth plugin "%1" does not exist in plugin directory %2',$plugin_foldername,'plugins/[vendorname]/auth/'));
516
                        }
673
                        }
Line 522... Line 679...
522
                        if ($must_hash && !$plugin->availableForHash($reason)) {
679
                        if ($must_hash && !$plugin->availableForHash($reason)) {
523
                                throw new OIDplusException(trim(_L('The auth plugin "%1" is not available for hashing on this system.',$plugin_foldername).' '.$reason));
680
                                throw new OIDplusException(trim(_L('The auth plugin "%1" is not available for hashing on this system.',$plugin_foldername).' '.$reason));
524
                        }
681
                        }
525
        }
682
        }
526
 
683
 
-
 
684
        /**
-
 
685
         * @param bool $must_hash
-
 
686
         * @return OIDplusAuthPlugin|null
-
 
687
         * @throws OIDplusException
-
 
688
         */
527
        public static function getDefaultRaAuthPlugin($must_hash)/*: OIDplusAuthPlugin*/ {
689
        public static function getDefaultRaAuthPlugin(bool $must_hash)/*: OIDplusAuthPlugin*/ {
528
                // 1. Priority: Use the auth plugin the user prefers
690
                // 1. Priority: Use the auth plugin the user prefers
529
                $def_plugin_foldername = OIDplus::config()->getValue('default_ra_auth_method');
691
                $def_plugin_foldername = OIDplus::config()->getValue('default_ra_auth_method');
530
                if (trim($def_plugin_foldername) !== '') {
692
                if (trim($def_plugin_foldername) !== '') {
531
                        OIDplus::checkRaAuthPluginAvailable($def_plugin_foldername, $must_hash);
693
                        OIDplus::checkRaAuthPluginAvailable($def_plugin_foldername, $must_hash);
532
                        $plugin = OIDplus::getAuthPluginByFoldername($def_plugin_foldername);
694
                        return OIDplus::getAuthPluginByFoldername($def_plugin_foldername);
533
                        return $plugin;
-
 
534
                }
695
                }
535
 
696
 
536
                // 2. Priority: If empty (i.e. OIDplus may decide), choose the best ViaThinkSoft plugin that is supported on this system
697
                // 2. Priority: If empty (i.e. OIDplus may decide), choose the best ViaThinkSoft plugin that is supported on this system
537
                $preferred_auth_plugins = array(
698
                $preferred_auth_plugins = array(
538
                        // Sorted by preference
699
                        // Sorted by preference
Line 563... Line 724...
563
                // 4. Priority: We must deny the creation of the password because we have no auth plugin!
724
                // 4. Priority: We must deny the creation of the password because we have no auth plugin!
564
                throw new OIDplusException(_L('Could not find a fitting auth plugin!'));
725
                throw new OIDplusException(_L('Could not find a fitting auth plugin!'));
565
        }
726
        }
566
 
727
 
567
        /**
728
        /**
-
 
729
         * @param OIDplusAuthPlugin $plugin
568
        * @return void
730
         * @return void
-
 
731
         * @throws OIDplusConfigInitializationException
-
 
732
         * @throws OIDplusException
569
        */
733
         */
570
        private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) {
734
        private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) {
571
                $reason = '';
735
                $reason = '';
572
                if (OIDplus::baseConfig()->getValue('DEBUG') && $plugin->availableForHash($reason) && $plugin->availableForVerify($reason)) {
736
                if (OIDplus::baseConfig()->getValue('DEBUG') && $plugin->availableForHash($reason) && $plugin->availableForVerify($reason)) {
573
                        $password = generateRandomString(25);
737
                        $password = generateRandomString(25);
Line 578... Line 742...
578
                                // This can happen when the AuthKey is too long for the database field
742
                                // This can happen when the AuthKey is too long for the database field
579
                                // Note: The constructor and setters of OIDplusRAAuthInfo() already check for length and null/false values.
743
                                // Note: The constructor and setters of OIDplusRAAuthInfo() already check for length and null/false values.
580
                                throw new OIDplusException(_L('Auth plugin "%1" is erroneous: %2',basename($plugin->getPluginDirectory()),$e->getMessage()));
744
                                throw new OIDplusException(_L('Auth plugin "%1" is erroneous: %2',basename($plugin->getPluginDirectory()),$e->getMessage()));
581
                        }
745
                        }
582
 
746
 
583
                        $authKey = $authInfo->getAuthKey();
-
 
584
 
-
 
585
                        $authInfo_AuthKeyDiff = clone $authInfo;
747
                        $authInfo_AuthKeyDiff = clone $authInfo;
586
                        $authInfo_AuthKeyDiff->setAuthKey(strrev($authInfo_AuthKeyDiff->getAuthKey()));
748
                        $authInfo_AuthKeyDiff->setAuthKey(strrev($authInfo_AuthKeyDiff->getAuthKey()));
587
 
749
 
588
                        if ((!$plugin->verify($authInfo,$password)) ||
750
                        if ((!$plugin->verify($authInfo,$password)) ||
589
                           ($plugin->verify($authInfo_AuthKeyDiff,$password)) ||
751
                           ($plugin->verify($authInfo_AuthKeyDiff,$password)) ||
Line 593... Line 755...
593
                }
755
                }
594
 
756
 
595
                self::$authPlugins[] = $plugin;
757
                self::$authPlugins[] = $plugin;
596
        }
758
        }
597
 
759
 
-
 
760
        /**
-
 
761
         * @return OIDplusAuthPlugin[]
-
 
762
         */
598
        public static function getAuthPlugins() {
763
        public static function getAuthPlugins(): array {
599
                return self::$authPlugins;
764
                return self::$authPlugins;
600
        }
765
        }
601
 
766
 
602
        // --- Language plugin
767
        // --- Language plugin
603
 
768
 
604
        /**
769
        /**
-
 
770
         * @param OIDplusLanguagePlugin $plugin
605
        * @return void
771
         * @return void
606
        */
772
         */
607
        private static function registerLanguagePlugin(OIDplusLanguagePlugin $plugin) {
773
        private static function registerLanguagePlugin(OIDplusLanguagePlugin $plugin) {
608
                self::$languagePlugins[] = $plugin;
774
                self::$languagePlugins[] = $plugin;
609
        }
775
        }
610
 
776
 
-
 
777
        /**
-
 
778
         * @return OIDplusLanguagePlugin[]
-
 
779
         */
611
        public static function getLanguagePlugins() {
780
        public static function getLanguagePlugins(): array {
612
                return self::$languagePlugins;
781
                return self::$languagePlugins;
613
        }
782
        }
614
 
783
 
615
        // --- Design plugin
784
        // --- Design plugin
616
 
785
 
617
        /**
786
        /**
-
 
787
         * @param OIDplusDesignPlugin $plugin
618
        * @return void
788
         * @return void
619
        */
789
         */
620
        private static function registerDesignPlugin(OIDplusDesignPlugin $plugin) {
790
        private static function registerDesignPlugin(OIDplusDesignPlugin $plugin) {
621
                self::$designPlugins[] = $plugin;
791
                self::$designPlugins[] = $plugin;
622
        }
792
        }
623
 
793
 
-
 
794
        /**
-
 
795
         * @return OIDplusDesignPlugin[]
-
 
796
         */
624
        public static function getDesignPlugins() {
797
        public static function getDesignPlugins(): array {
625
                return self::$designPlugins;
798
                return self::$designPlugins;
626
        }
799
        }
627
 
800
 
-
 
801
        /**
-
 
802
         * @return OIDplusDesignPlugin|null
-
 
803
         * @throws OIDplusException
-
 
804
         */
628
        public static function getActiveDesignPlugin() {
805
        public static function getActiveDesignPlugin()/*: ?OIDplusDesignPlugin*/ {
629
                $plugins = OIDplus::getDesignPlugins();
806
                $plugins = OIDplus::getDesignPlugins();
630
                foreach ($plugins as $plugin) {
807
                foreach ($plugins as $plugin) {
631
                        if ((basename($plugin->getPluginDirectory())) == OIDplus::config()->getValue('design','default')) {
808
                        if ((basename($plugin->getPluginDirectory())) == OIDplus::config()->getValue('design','default')) {
632
                                return $plugin;
809
                                return $plugin;
633
                        }
810
                        }
Line 636... Line 813...
636
        }
813
        }
637
 
814
 
638
        // --- Logger plugin
815
        // --- Logger plugin
639
 
816
 
640
        /**
817
        /**
-
 
818
         * @param OIDplusLoggerPlugin $plugin
641
        * @return void
819
         * @return void
642
        */
820
         */
643
        private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) {
821
        private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) {
644
                self::$loggerPlugins[] = $plugin;
822
                self::$loggerPlugins[] = $plugin;
645
        }
823
        }
646
 
824
 
-
 
825
        /**
-
 
826
         * @return OIDplusLoggerPlugin[]
-
 
827
         */
647
        public static function getLoggerPlugins() {
828
        public static function getLoggerPlugins(): array {
648
                return self::$loggerPlugins;
829
                return self::$loggerPlugins;
649
        }
830
        }
650
 
831
 
651
        // --- Object type plugin
832
        // --- Object type plugin
652
 
833
 
653
        /**
834
        /**
-
 
835
         * @param OIDplusObjectTypePlugin $plugin
654
        * @return void
836
         * @return void
-
 
837
         * @throws OIDplusException
655
        */
838
         */
656
        private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) {
839
        private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) {
657
                self::$objectTypePlugins[] = $plugin;
840
                self::$objectTypePlugins[] = $plugin;
658
 
841
 
659
                $ot = $plugin::getObjectTypeClassName();
842
                $ot = $plugin::getObjectTypeClassName();
660
                self::registerObjectType($ot);
843
                self::registerObjectType($ot);
661
        }
844
        }
662
 
845
 
663
        /**
846
        /**
-
 
847
         * @param string|OIDplusObject $ot Object type class name (OIDplusObject)
664
        * @return void
848
         * @return void
-
 
849
         * @throws OIDplusException
665
        */
850
         */
666
        private static function registerObjectType($ot) {
851
        private static function registerObjectType($ot) {
667
                $ns = $ot::ns();
852
                $ns = $ot::ns();
668
                if (empty($ns)) throw new OIDplusException(_L('ObjectType plugin %1 is erroneous: Namespace must not be empty',$ot));
853
                if (empty($ns)) throw new OIDplusException(_L('ObjectType plugin %1 is erroneous: Namespace must not be empty',$ot));
669
 
854
 
Line 690... Line 875...
690
 
875
 
691
                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
876
                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
692
                $enabled_ary = empty($enabled) ? array() : explode(';', $enabled);
877
                $enabled_ary = empty($enabled) ? array() : explode(';', $enabled);
693
                $enabled_ary = array_map('trim', $enabled_ary);
878
                $enabled_ary = array_map('trim', $enabled_ary);
694
 
879
 
695
                $do_enable = false;
-
 
696
                if (in_array($ns, $enabled_ary)) {
880
                if (in_array($ns, $enabled_ary)) {
697
                        // If it is in the list of enabled object types, it is enabled (obviously)
881
                        // If it is in the list of enabled object types, it is enabled (obviously)
698
                        $do_enable = true;
882
                        $do_enable = true;
699
                } else {
883
                } else {
700
                        if (!OIDplus::config()->getValue('oobe_objects_done')) {
884
                        if (!OIDplus::config()->getValue('oobe_objects_done')) {
Line 736... Line 920...
736
                        $init_ary[] = $ns;
920
                        $init_ary[] = $ns;
737
                        OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
921
                        OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
738
                }
922
                }
739
        }
923
        }
740
 
924
 
-
 
925
        /**
-
 
926
         * @return OIDplusObjectTypePlugin[]
-
 
927
         */
741
        public static function getObjectTypePlugins() {
928
        public static function getObjectTypePlugins(): array {
742
                return self::$objectTypePlugins;
929
                return self::$objectTypePlugins;
743
        }
930
        }
744
 
931
 
-
 
932
        /**
-
 
933
         * @return OIDplusObjectTypePlugin[]
-
 
934
         */
745
        public static function getObjectTypePluginsEnabled() {
935
        public static function getObjectTypePluginsEnabled(): array {
746
                $res = array();
936
                $res = array();
747
                foreach (self::$objectTypePlugins as $plugin) {
937
                foreach (self::$objectTypePlugins as $plugin) {
748
                        $ot = $plugin::getObjectTypeClassName();
938
                        $ot = $plugin::getObjectTypeClassName();
749
                        if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin;
939
                        if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin;
750
                }
940
                }
751
                return $res;
941
                return $res;
752
        }
942
        }
753
 
943
 
-
 
944
        /**
-
 
945
         * @return OIDplusObjectTypePlugin[]
-
 
946
         */
754
        public static function getObjectTypePluginsDisabled() {
947
        public static function getObjectTypePluginsDisabled(): array {
755
                $res = array();
948
                $res = array();
756
                foreach (self::$objectTypePlugins as $plugin) {
949
                foreach (self::$objectTypePlugins as $plugin) {
757
                        $ot = $plugin::getObjectTypeClassName();
950
                        $ot = $plugin::getObjectTypeClassName();
758
                        if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin;
951
                        if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin;
759
                }
952
                }
760
                return $res;
953
                return $res;
761
        }
954
        }
762
 
955
 
-
 
956
        /**
-
 
957
         * @return string[]|OIDplusObject[] Classname of a OIDplusObject class
-
 
958
         */
763
        public static function getEnabledObjectTypes() {
959
        public static function getEnabledObjectTypes(): array {
764
                return self::$enabledObjectTypes;
960
                return self::$enabledObjectTypes;
765
        }
961
        }
766
 
962
 
-
 
963
        /**
-
 
964
         * @return string[]|OIDplusObject[] Classname of a OIDplusObject class
-
 
965
         */
767
        public static function getDisabledObjectTypes() {
966
        public static function getDisabledObjectTypes(): array {
768
                return self::$disabledObjectTypes;
967
                return self::$disabledObjectTypes;
769
        }
968
        }
770
 
969
 
771
        // --- Plugin handling functions
970
        // --- Plugin handling functions
772
 
971
 
-
 
972
        /**
-
 
973
         * @return OIDplusPlugin[]
-
 
974
         */
773
        public static function getAllPlugins()/*: array*/ {
975
        public static function getAllPlugins(): array {
774
                $res = array();
976
                $res = array();
775
                $res = array_merge($res, self::$pagePlugins);
977
                $res = array_merge($res, self::$pagePlugins);
776
                $res = array_merge($res, self::$authPlugins);
978
                $res = array_merge($res, self::$authPlugins);
777
                $res = array_merge($res, self::$loggerPlugins);
979
                $res = array_merge($res, self::$loggerPlugins);
778
                $res = array_merge($res, self::$objectTypePlugins);
980
                $res = array_merge($res, self::$objectTypePlugins);
779
                $res = array_merge($res, self::$dbPlugins);
981
                $res = array_merge($res, self::$dbPlugins);
780
                $res = array_merge($res, self::$captchaPlugins);
982
                $res = array_merge($res, self::$captchaPlugins);
781
                $res = array_merge($res, self::$sqlSlangPlugins);
983
                $res = array_merge($res, self::$sqlSlangPlugins);
782
                $res = array_merge($res, self::$languagePlugins);
984
                $res = array_merge($res, self::$languagePlugins);
783
                $res = array_merge($res, self::$designPlugins);
985
                return array_merge($res, self::$designPlugins);
784
                return $res;
-
 
785
        }
986
        }
786
 
987
 
-
 
988
        /**
-
 
989
         * @param string $oid
-
 
990
         * @return OIDplusPlugin|null
-
 
991
         */
787
        public static function getPluginByOid($oid)/*: ?OIDplusPlugin*/ {
992
        public static function getPluginByOid(string $oid)/*: ?OIDplusPlugin*/ {
788
                $plugins = self::getAllPlugins();
993
                $plugins = self::getAllPlugins();
789
                foreach ($plugins as $plugin) {
994
                foreach ($plugins as $plugin) {
790
                        if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) {
995
                        if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) {
791
                                return $plugin;
996
                                return $plugin;
792
                        }
997
                        }
793
                }
998
                }
794
                return null;
999
                return null;
795
        }
1000
        }
796
 
1001
 
-
 
1002
        /**
-
 
1003
         * @param string $classname
-
 
1004
         * @return OIDplusPlugin|null
-
 
1005
         */
797
        public static function getPluginByClassName($classname)/*: ?OIDplusPlugin*/ {
1006
        public static function getPluginByClassName(string $classname)/*: ?OIDplusPlugin*/ {
798
                $plugins = self::getAllPlugins();
1007
                $plugins = self::getAllPlugins();
799
                foreach ($plugins as $plugin) {
1008
                foreach ($plugins as $plugin) {
800
                        if (get_class($plugin) === $classname) {
1009
                        if (get_class($plugin) === $classname) {
801
                                return $plugin;
1010
                                return $plugin;
802
                        }
1011
                        }
Line 804... Line 1013...
804
                return null;
1013
                return null;
805
        }
1014
        }
806
 
1015
 
807
        /**
1016
        /**
808
        * Checks if the plugin is disabled
1017
        * Checks if the plugin is disabled
809
        * @return boolean true if plugin is enabled, false if plugin is disabled
1018
        * @return bool true if plugin is enabled, false if plugin is disabled
810
        * @throws OIDplusException if the class name or config file (disabled setting) does not contain a namespace
1019
        * @throws OIDplusException if the class name or config file (disabled setting) does not contain a namespace
811
        */
1020
        */
812
        private static function pluginCheckDisabled($class_name): bool {
1021
        private static function pluginCheckDisabled($class_name): bool {
813
                $path = explode('\\', $class_name);
1022
                $path = explode('\\', $class_name);
814
 
1023
 
Line 827... Line 1036...
827
 
1036
 
828
                return true;
1037
                return true;
829
        }
1038
        }
830
 
1039
 
831
        /**
1040
        /**
-
 
1041
         * @param string $pluginFolderMasks
-
 
1042
         * @param bool $flat
832
        * @return array<OIDplusPluginManifest>|array<string,array<string,OIDplusPluginManifest>>
1043
         * @return OIDplusPluginManifest[]|array<string,array<string,OIDplusPluginManifest>>
-
 
1044
         * @throws OIDplusException
833
        */
1045
         */
834
        public static function getAllPluginManifests($pluginFolderMasks='*', $flat=true): array {
1046
        public static function getAllPluginManifests(string $pluginFolderMasks='*', bool $flat=true): array {
835
                $out = array();
1047
                $out = array();
836
                // Note: glob() will sort by default, so we do not need a page priority attribute.
1048
                // Note: glob() will sort by default, so we do not need a page priority attribute.
837
                //       So you just need to use a numeric plugin directory prefix (padded).
1049
                //       So you just need to use a numeric plugin directory prefix (padded).
838
                $ary = array();
1050
                $ary = array();
839
                foreach (explode(',',$pluginFolderMasks) as $pluginFolderMask) {
1051
                foreach (explode(',',$pluginFolderMasks) as $pluginFolderMask) {
Line 877... Line 1089...
877
                        if ($class_name) if (!self::pluginCheckDisabled($class_name)) continue;
1089
                        if ($class_name) if (!self::pluginCheckDisabled($class_name)) continue;
878
 
1090
 
879
                        if ($flat) {
1091
                        if ($flat) {
880
                                $out[] = $manifest;
1092
                                $out[] = $manifest;
881
                        } else {
1093
                        } else {
882
                                $plugintype_folder = basename(dirname(dirname($ini)));
1094
                                $plugintype_folder = basename(dirname($ini, 2));
883
                                $pluginname_folder = basename(dirname($ini));
1095
                                $pluginname_folder = basename(dirname($ini));
884
 
1096
 
885
                                if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array();
1097
                                if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array();
886
                                if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array();
1098
                                if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array();
887
                                $out[$plugintype_folder][$pluginname_folder] = $manifest;
1099
                                $out[$plugintype_folder][$pluginname_folder] = $manifest;
Line 889... Line 1101...
889
                }
1101
                }
890
                return $out;
1102
                return $out;
891
        }
1103
        }
892
 
1104
 
893
        /**
1105
        /**
-
 
1106
         * @param $pluginDirName
-
 
1107
         * @param $expectedPluginClass
-
 
1108
         * @param $registerCallback
894
        * @return array<string>
1109
         * @return string[]
-
 
1110
         * @throws OIDplusConfigInitializationException
-
 
1111
         * @throws OIDplusException
-
 
1112
         * @throws \ReflectionException
895
        */
1113
         */
896
        public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array {
1114
        public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array {
897
                $out = array();
1115
                $out = array();
898
                if (is_array($pluginDirName)) {
1116
                if (is_array($pluginDirName)) {
899
                        $ary = array();
1117
                        $ary = array();
Line 1025... Line 1243...
1025
                return $out;
1243
                return $out;
1026
        }
1244
        }
1027
 
1245
 
1028
        // --- Initialization of OIDplus
1246
        // --- Initialization of OIDplus
1029
 
1247
 
-
 
1248
        /**
-
 
1249
         * @param bool $html
-
 
1250
         * @param bool $keepBaseConfig
-
 
1251
         * @return void
-
 
1252
         * @throws OIDplusConfigInitializationException
-
 
1253
         * @throws OIDplusException
-
 
1254
         */
1030
        public static function init($html=true, $keepBaseConfig=true) {
1255
        public static function init(bool $html=true, bool $keepBaseConfig=true) {
1031
                self::$html = $html;
1256
                self::$html = $html;
1032
 
1257
 
1033
                // Reset internal state, so we can re-init verything if required
1258
                // Reset internal state, so we can re-init verything if required
1034
 
1259
 
1035
                self::$config = null;
1260
                self::$config = null;
Line 1258... Line 1483...
1258
                        // The headers are sent automatically when the first output comes or the script ends
1483
                        // The headers are sent automatically when the first output comes or the script ends
1259
 
1484
 
1260
                        foreach ($http_headers as $name => $val) {
1485
                        foreach ($http_headers as $name => $val) {
1261
 
1486
 
1262
                                // Plugins can remove standard OIDplus headers by setting the value to null.
1487
                                // Plugins can remove standard OIDplus headers by setting the value to null.
1263
                                if (is_null($val)) continue; /** @phpstan-ignore-line */
1488
                                if (is_null($val)) continue;
1264
 
1489
 
1265
                                // Some headers can be written as arrays to make it easier for plugin authors
1490
                                // Some headers can be written as arrays to make it easier for plugin authors
1266
                                // to manipulate/extend the contents.
1491
                                // to manipulate/extend the contents.
1267
                                if (is_array($val)) {
1492
                                if (is_array($val)) {
1268
                                        if ((strtolower($name) == 'cache-control') ||
1493
                                        if ((strtolower($name) == 'cache-control') ||
Line 1295... Line 1520...
1295
                OIDplus::recognizeVersion(); // Make sure "last_known_version" is set and a log entry is created
1520
                OIDplus::recognizeVersion(); // Make sure "last_known_version" is set and a log entry is created
1296
        }
1521
        }
1297
 
1522
 
1298
        // --- System URL, System ID, PKI, and other functions
1523
        // --- System URL, System ID, PKI, and other functions
1299
 
1524
 
-
 
1525
        /**
-
 
1526
         * @return void
-
 
1527
         */
1300
        private static function recognizeSystemUrl() {
1528
        private static function recognizeSystemUrl() {
1301
                try {
1529
                try {
1302
                        $url = OIDplus::webpath(null,self::PATH_ABSOLUTE_CANONICAL);
1530
                        $url = OIDplus::webpath(null,self::PATH_ABSOLUTE_CANONICAL);
1303
                        OIDplus::config()->setValue('last_known_system_url', $url);
1531
                        OIDplus::config()->setValue('last_known_system_url', $url);
1304
                } catch (\Exception $e) {
1532
                } catch (\Exception $e) {
1305
                }
1533
                }
1306
        }
1534
        }
1307
 
1535
 
-
 
1536
        /**
-
 
1537
         * @return false|int
-
 
1538
         */
1308
        private static function getExecutingScriptPathDepth() {
1539
        private static function getExecutingScriptPathDepth() {
1309
                if (PHP_SAPI == 'cli') {
1540
                if (PHP_SAPI == 'cli') {
1310
                        global $argv;
1541
                        global $argv;
1311
                        $test_dir = dirname(realpath($argv[0]));
1542
                        $test_dir = dirname(realpath($argv[0]));
1312
                } else {
1543
                } else {
Line 1321... Line 1552...
1321
                        if ($steps_up == 1000) return false; // to make sure there will never be an infinite loop
1552
                        if ($steps_up == 1000) return false; // to make sure there will never be an infinite loop
1322
                }
1553
                }
1323
                return $steps_up;
1554
                return $steps_up;
1324
        }
1555
        }
1325
 
1556
 
-
 
1557
        /**
-
 
1558
         * @return bool
-
 
1559
         */
1326
        public static function isSSL() {
1560
        public static function isSSL(): bool {
1327
                return isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on');
1561
                return isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on');
1328
        }
1562
        }
1329
 
1563
 
1330
        /**
1564
        /**
1331
         * Returns the URL of the system.
1565
         * Returns the URL of the system.
1332
         * @param int $mode If true or OIDplus::PATH_RELATIVE, the returning path is relative to the currently executed
1566
         * @param int $mode If true or OIDplus::PATH_RELATIVE, the returning path is relative to the currently executed
1333
         *                  PHP script (i.e. index.php , not the plugin PHP script!). False or OIDplus::PATH_ABSOLUTE is
1567
         *                  PHP script (i.e. index.php , not the plugin PHP script!). False or OIDplus::PATH_ABSOLUTE is
1334
         *                  results in an absolute URL. OIDplus::PATH_ABSOLUTE_CANONICAL is an absolute URL,
1568
         *                  results in an absolute URL. OIDplus::PATH_ABSOLUTE_CANONICAL is an absolute URL,
1335
         *                  but a canonical path (set by base config setting CANONICAL_SYSTEM_URL) is preferred.
1569
         *                  but a canonical path (set by base config setting CANONICAL_SYSTEM_URL) is preferred.
1336
         * @return string|false The URL, with guaranteed trailing path delimiter for directories
1570
         * @return string|false The URL, with guaranteed trailing path delimiter for directories
-
 
1571
         * @throws OIDplusException
1337
         */
1572
         */
1338
        private static function getSystemUrl($mode) {
1573
        private static function getSystemUrl(int $mode) {
1339
                if ($mode === self::PATH_RELATIVE) {
1574
                if ($mode === self::PATH_RELATIVE) {
1340
                        $steps_up = self::getExecutingScriptPathDepth();
1575
                        $steps_up = self::getExecutingScriptPathDepth();
1341
                        if ($steps_up === false) {
1576
                        if ($steps_up === false) {
1342
                                return false;
1577
                                return false;
1343
                        } else {
1578
                        } else {
Line 1377... Line 1612...
1377
                                return $protocol.'://'.$host.$res.'/';
1612
                                return $protocol.'://'.$host.$res.'/';
1378
                        }
1613
                        }
1379
                }
1614
                }
1380
        }
1615
        }
1381
 
1616
 
-
 
1617
        /**
-
 
1618
         * @param $pubKey
-
 
1619
         * @return false|string
-
 
1620
         */
1382
        private static function pubKeyToRaw($pubKey) {
1621
        private static function pubKeyToRaw($pubKey) {
1383
                $m = array();
1622
                $m = array();
1384
                if (preg_match('@BEGIN PUBLIC KEY\\-+([^\\-]+)\\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
1623
                if (preg_match('@BEGIN PUBLIC KEY\\-+([^\\-]+)\\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
1385
                        return base64_decode($m[1], false);
1624
                        return base64_decode($m[1], false);
1386
                }
1625
                }
1387
                return false;
1626
                return false;
1388
        }
1627
        }
1389
 
1628
 
-
 
1629
        /**
-
 
1630
         * @param $pubKey
-
 
1631
         * @return false|int
-
 
1632
         */
1390
        private static function getSystemIdFromPubKey($pubKey) {
1633
        private static function getSystemIdFromPubKey($pubKey) {
1391
                $rawData = self::pubKeyToRaw($pubKey);
1634
                $rawData = self::pubKeyToRaw($pubKey);
1392
                if ($rawData === false) return false;
1635
                if ($rawData === false) return false;
1393
                return smallhash($rawData);
1636
                return smallhash($rawData);
1394
        }
1637
        }
1395
 
1638
 
-
 
1639
        /**
-
 
1640
         * @param $pubKey
-
 
1641
         * @return false|string
-
 
1642
         */
1396
        private static function getSystemGuidFromPubKey($pubKey) {
1643
        private static function getSystemGuidFromPubKey($pubKey) {
1397
                $rawData = self::pubKeyToRaw($pubKey);
1644
                $rawData = self::pubKeyToRaw($pubKey);
1398
                if ($rawData === false) return false;
1645
                if ($rawData === false) return false;
1399
                $normalizedBase64 = base64_encode($rawData);
1646
                $normalizedBase64 = base64_encode($rawData);
1400
                return gen_uuid_sha1_namebased(self::UUID_NAMEBASED_NS_Base64PubKey, $normalizedBase64);
1647
                return gen_uuid_sha1_namebased(self::UUID_NAMEBASED_NS_Base64PubKey, $normalizedBase64);
1401
        }
1648
        }
1402
 
1649
 
1403
        private static $system_id_cache = null;
1650
        private static $system_id_cache = null;
-
 
1651
 
-
 
1652
        /**
-
 
1653
         * @param bool $oid
-
 
1654
         * @return false|string
-
 
1655
         * @throws OIDplusException
-
 
1656
         */
1404
        public static function getSystemId($oid=false) {
1657
        public static function getSystemId(bool $oid=false) {
1405
                if (!is_null(self::$system_id_cache)) {
1658
                if (!is_null(self::$system_id_cache)) {
1406
                        $out = self::$system_id_cache;
1659
                        $out = self::$system_id_cache;
1407
                } else {
1660
                } else {
1408
                        $out = false;
1661
                        $out = false;
1409
 
1662
 
Line 1416... Line 1669...
1416
                if (!$out) return false;
1669
                if (!$out) return false;
1417
                return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out;
1670
                return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out;
1418
        }
1671
        }
1419
 
1672
 
1420
        private static $system_guid_cache = null;
1673
        private static $system_guid_cache = null;
-
 
1674
 
-
 
1675
        /**
-
 
1676
         * @return false|string
-
 
1677
         * @throws OIDplusException
-
 
1678
         */
1421
        public static function getSystemGuid() {
1679
        public static function getSystemGuid() {
1422
                if (!is_null(self::$system_guid_cache)) {
1680
                if (!is_null(self::$system_guid_cache)) {
1423
                        $out = self::$system_guid_cache;
1681
                        $out = self::$system_guid_cache;
1424
                } else {
1682
                } else {
1425
                        $out = false;
1683
                        $out = false;
Line 1432... Line 1690...
1432
                }
1690
                }
1433
                if (!$out) return false;
1691
                if (!$out) return false;
1434
                return $out;
1692
                return $out;
1435
        }
1693
        }
1436
 
1694
 
-
 
1695
        /**
-
 
1696
         * @return array|string
-
 
1697
         */
1437
        public static function getOpenSslCnf() {
1698
        public static function getOpenSslCnf() {
1438
                // The following functions need a config file, otherway they don't work
1699
                // The following functions need a config file, otherway they don't work
1439
                // - openssl_csr_new
1700
                // - openssl_csr_new
1440
                // - openssl_csr_sign
1701
                // - openssl_csr_sign
1441
                // - openssl_pkey_export
1702
                // - openssl_pkey_export
Line 1449... Line 1710...
1449
                // Bug reports are more than 10 years old and nobody cares...
1710
                // Bug reports are more than 10 years old and nobody cares...
1450
                // Use our own config file
1711
                // Use our own config file
1451
                return __DIR__.'/../../vendor/phpseclib/phpseclib/phpseclib/openssl.cnf';
1712
                return __DIR__.'/../../vendor/phpseclib/phpseclib/phpseclib/openssl.cnf';
1452
        }
1713
        }
1453
 
1714
 
-
 
1715
        /**
-
 
1716
         * @return string
-
 
1717
         */
1454
        private static function getPrivKeyPassphraseFilename() {
1718
        private static function getPrivKeyPassphraseFilename(): string {
1455
                return OIDplus::localpath() . 'userdata/privkey_secret.php';
1719
                return OIDplus::localpath() . 'userdata/privkey_secret.php';
1456
        }
1720
        }
1457
 
1721
 
-
 
1722
        /**
-
 
1723
         * @return void
-
 
1724
         */
1458
        private static function tryCreatePrivKeyPassphrase() {
1725
        private static function tryCreatePrivKeyPassphrase() {
1459
                $file = self::getPrivKeyPassphraseFilename();
1726
                $file = self::getPrivKeyPassphraseFilename();
1460
 
1727
 
1461
                $passphrase = generateRandomString(64);
1728
                $passphrase = generateRandomString(64);
1462
                $cont = "<?php\n";
1729
                $cont = "<?php\n";
Line 1469... Line 1736...
1469
                $cont .= "// End of file\n";
1736
                $cont .= "// End of file\n";
1470
 
1737
 
1471
                @file_put_contents($file, $cont);
1738
                @file_put_contents($file, $cont);
1472
        }
1739
        }
1473
 
1740
 
-
 
1741
        /**
-
 
1742
         * @return string|false
-
 
1743
         */
1474
        private static function getPrivKeyPassphrase() {
1744
        private static function getPrivKeyPassphrase() {
1475
                $file = self::getPrivKeyPassphraseFilename();
1745
                $file = self::getPrivKeyPassphraseFilename();
1476
                if (!file_exists($file)) return false;
1746
                if (!file_exists($file)) return false;
1477
                $cont = file_get_contents($file);
1747
                $cont = file_get_contents($file);
1478
                $m = array();
1748
                $m = array();
1479
                if (!preg_match("@'(.+)'@isU", $cont, $m)) return false;
1749
                if (!preg_match("@'(.+)'@isU", $cont, $m)) return false;
1480
                return $m[1];
1750
                return $m[1];
1481
        }
1751
        }
1482
 
1752
 
-
 
1753
        /**
-
 
1754
         * @return string|false
-
 
1755
         * @throws OIDplusException
-
 
1756
         */
1483
        public static function getSystemPrivateKey() {
1757
        public static function getSystemPrivateKey() {
1484
                $privKey = OIDplus::config()->getValue('oidplus_private_key');
1758
                $privKey = OIDplus::config()->getValue('oidplus_private_key');
1485
                if ($privKey == '') return false;
1759
                if ($privKey == '') return false;
1486
 
1760
 
1487
                $passphrase = self::getPrivKeyPassphrase();
1761
                $passphrase = self::getPrivKeyPassphrase();
Line 1495... Line 1769...
1495
                }
1769
                }
1496
 
1770
 
1497
                return $privKey;
1771
                return $privKey;
1498
        }
1772
        }
1499
 
1773
 
-
 
1774
        /**
-
 
1775
         * @return string|false
-
 
1776
         * @throws OIDplusException
-
 
1777
         */
1500
        public static function getSystemPublicKey() {
1778
        public static function getSystemPublicKey() {
1501
                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
1779
                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
1502
                if ($pubKey == '') return false;
1780
                if ($pubKey == '') return false;
1503
                return $pubKey;
1781
                return $pubKey;
1504
        }
1782
        }
1505
 
1783
 
-
 
1784
        /**
-
 
1785
         * @param bool $try_generate
-
 
1786
         * @return bool
-
 
1787
         * @throws OIDplusException
-
 
1788
         */
1506
        public static function getPkiStatus($try_generate=false) {
1789
        public static function getPkiStatus(bool $try_generate=false): bool {
1507
                if (!function_exists('openssl_pkey_new')) return false;
1790
                if (!function_exists('openssl_pkey_new')) return false;
1508
 
1791
 
1509
                if ($try_generate) {
1792
                if ($try_generate) {
1510
                        // For debug purposes: Invalidate current key once:
1793
                        // For debug purposes: Invalidate current key once:
1511
                        //OIDplus::config()->setValue('oidplus_private_key', '');
1794
                        //OIDplus::config()->setValue('oidplus_private_key', '');
Line 1572... Line 1855...
1572
                $privKey = OIDplus::getSystemPrivateKey();
1855
                $privKey = OIDplus::getSystemPrivateKey();
1573
                $pubKey = OIDplus::getSystemPublicKey();
1856
                $pubKey = OIDplus::getSystemPublicKey();
1574
                return verify_private_public_key($privKey, $pubKey);
1857
                return verify_private_public_key($privKey, $pubKey);
1575
        }
1858
        }
1576
 
1859
 
-
 
1860
        /**
-
 
1861
         * @return string|void
-
 
1862
         */
1577
        public static function getInstallType() {
1863
        public static function getInstallType() {
1578
                $counter = 0;
1864
                $counter = 0;
1579
 
1865
 
1580
                if ($new_version_file_exists = file_exists(OIDplus::localpath().'.version.php')) {
1866
                if ($new_version_file_exists = file_exists(OIDplus::localpath().'.version.php')) {
1581
                        $counter++;
1867
                        $counter++;
Line 1608... Line 1894...
1608
                else if ($version_file_exists) {
1894
                else if ($version_file_exists) {
1609
                        return 'svn-snapshot'; // do not translate
1895
                        return 'svn-snapshot'; // do not translate
1610
                }
1896
                }
1611
        }
1897
        }
1612
 
1898
 
-
 
1899
        /**
-
 
1900
         * @return void
-
 
1901
         */
1613
        private static function recognizeVersion() {
1902
        private static function recognizeVersion() {
1614
                try {
1903
                try {
1615
                        $ver_prev = OIDplus::config()->getValue("last_known_version");
1904
                        $ver_prev = OIDplus::config()->getValue("last_known_version");
1616
                        $ver_now = OIDplus::getVersion();
1905
                        $ver_now = OIDplus::getVersion();
1617
                        if (($ver_now != '') && ($ver_prev != '') && ($ver_now != $ver_prev)) {
1906
                        if (($ver_now != '') && ($ver_prev != '') && ($ver_now != $ver_prev)) {
Line 1624... Line 1913...
1624
                        OIDplus::config()->setValue("last_known_version", $ver_now);
1913
                        OIDplus::config()->setValue("last_known_version", $ver_now);
1625
                } catch (\Exception $e) {
1914
                } catch (\Exception $e) {
1626
                }
1915
                }
1627
        }
1916
        }
1628
 
1917
 
-
 
1918
        /**
-
 
1919
         * @return false|string|null
-
 
1920
         */
1629
        public static function getVersion() {
1921
        public static function getVersion() {
1630
                static $cachedVersion = null;
1922
                static $cachedVersion = null;
1631
                if (!is_null($cachedVersion)) {
1923
                if (!is_null($cachedVersion)) {
1632
                        return $cachedVersion;
1924
                        return $cachedVersion;
1633
                }
1925
                }
Line 1642... Line 1934...
1642
                        if ($ver)
1934
                        if ($ver)
1643
                                return ($cachedVersion = 'svn-'.$ver);
1935
                                return ($cachedVersion = 'svn-'.$ver);
1644
                }
1936
                }
1645
 
1937
 
1646
                if ($installType === 'git-wc') {
1938
                if ($installType === 'git-wc') {
1647
                        $ver = OIDplus::getGitsvnRevision(OIDplus::localpath());
1939
                        $ver = OIDplus::getGitsvnRevision();
1648
                        if ($ver)
1940
                        if ($ver)
1649
                                return ($cachedVersion = 'svn-'.$ver);
1941
                                return ($cachedVersion = 'svn-'.$ver);
1650
                }
1942
                }
1651
 
1943
 
1652
                if ($installType === 'svn-snapshot') {
1944
                if ($installType === 'svn-snapshot') {
Line 1665... Line 1957...
1665
 
1957
 
1666
        const ENFORCE_SSL_NO   = 0;
1958
        const ENFORCE_SSL_NO   = 0;
1667
        const ENFORCE_SSL_YES  = 1;
1959
        const ENFORCE_SSL_YES  = 1;
1668
        const ENFORCE_SSL_AUTO = 2;
1960
        const ENFORCE_SSL_AUTO = 2;
1669
        private static $sslAvailableCache = null;
1961
        private static $sslAvailableCache = null;
-
 
1962
 
-
 
1963
        /**
-
 
1964
         * @return bool|void|null
-
 
1965
         * @throws OIDplusException, OIDplusConfigInitializationException
-
 
1966
         */
1670
        public static function isSslAvailable() {
1967
        public static function isSslAvailable() {
1671
                if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache;
1968
                if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache;
1672
 
1969
 
1673
                if (PHP_SAPI == 'cli') {
1970
                if (PHP_SAPI == 'cli') {
1674
                        self::$sslAvailableCache = false;
1971
                        self::$sslAvailableCache = false;
Line 1747... Line 2044...
1747
                }
2044
                }
1748
        }
2045
        }
1749
 
2046
 
1750
        /**
2047
        /**
1751
         * Gets a local path pointing to a resource
2048
         * Gets a local path pointing to a resource
1752
         * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory
2049
         * @param string|null $target Target resource (file or directory must exist), or null to get the OIDplus base directory
1753
         * @param boolean $relative If true, the returning path is relative to the currently executed PHP file (not the CLI working directory)
2050
         * @param bool $relative If true, the returning path is relative to the currently executed PHP file (not the CLI working directory)
1754
         * @return string|false The local path, with guaranteed trailing path delimiter for directories
2051
         * @return string|false The local path, with guaranteed trailing path delimiter for directories
1755
         */
2052
         */
1756
        public static function localpath($target=null, $relative=false) {
2053
        public static function localpath(string $target=null, bool $relative=false) {
1757
                if (is_null($target)) {
2054
                if (is_null($target)) {
1758
                        $target = __DIR__.'/../../';
2055
                        $target = __DIR__.'/../../';
1759
                }
2056
                }
1760
 
2057
 
1761
                if ($relative) {
2058
                if ($relative) {
Line 1776... Line 2073...
1776
                        $res = realpath($target);
2073
                        $res = realpath($target);
1777
                }
2074
                }
1778
 
2075
 
1779
                if (is_dir($target)) $res .= '/';
2076
                if (is_dir($target)) $res .= '/';
1780
 
2077
 
1781
                $res = str_replace('/', DIRECTORY_SEPARATOR, $res);
2078
                return str_replace('/', DIRECTORY_SEPARATOR, $res);
1782
 
-
 
1783
                return $res;
-
 
1784
        }
2079
        }
1785
 
2080
 
1786
        /**
2081
        /**
1787
         * Gets a URL pointing to a resource
2082
         * Gets a URL pointing to a resource
1788
         * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory
2083
         * @param string|null $target Target resource (file or directory must exist), or null to get the OIDplus base directory
1789
         * @param int|boolean $mode If true or OIDplus::PATH_RELATIVE, the returning path is relative to the currently executed
2084
         * @param int|bool $mode If true or OIDplus::PATH_RELATIVE, the returning path is relative to the currently executed
1790
         *                          PHP script (i.e. index.php , not the plugin PHP script!). False or OIDplus::PATH_ABSOLUTE is
2085
         *                          PHP script (i.e. index.php , not the plugin PHP script!). False or OIDplus::PATH_ABSOLUTE is
1791
         *                          results in an absolute URL. OIDplus::PATH_ABSOLUTE_CANONICAL is an absolute URL,
2086
         *                          results in an absolute URL. OIDplus::PATH_ABSOLUTE_CANONICAL is an absolute URL,
1792
         *                          but a canonical path (set by base config setting CANONICAL_SYSTEM_URL) is preferred.
2087
         *                          but a canonical path (set by base config setting CANONICAL_SYSTEM_URL) is preferred.
1793
         * @return string|false The URL, with guaranteed trailing path delimiter for directories
2088
         * @return string|false The URL, with guaranteed trailing path delimiter for directories
-
 
2089
         * @throws OIDplusException
1794
         */
2090
         */
1795
        public static function webpath($target=null, $mode=self::PATH_ABSOLUTE_CANONICAL) {
2091
        public static function webpath(string $target=null, $mode=self::PATH_ABSOLUTE_CANONICAL) {
1796
                // backwards compatibility
2092
                // backwards compatibility
1797
                if ($mode === true) $mode = self::PATH_RELATIVE;
2093
                if ($mode === true) $mode = self::PATH_RELATIVE;
1798
                if ($mode === false) $mode = self::PATH_ABSOLUTE;
2094
                if ($mode === false) $mode = self::PATH_ABSOLUTE;
1799
 
2095
 
1800
                if ($mode == OIDplus::PATH_RELATIVE_TO_ROOT) {
2096
                if ($mode == OIDplus::PATH_RELATIVE_TO_ROOT) {
Line 1828... Line 2124...
1828
                }
2124
                }
1829
 
2125
 
1830
                return $res;
2126
                return $res;
1831
        }
2127
        }
1832
 
2128
 
-
 
2129
        /**
-
 
2130
         * @return false|string
-
 
2131
         * @throws OIDplusException
-
 
2132
         */
1833
        public static function canonicalURL() {
2133
        public static function canonicalURL() {
1834
                // First part: OIDplus system URL (or canonical system URL)
2134
                // First part: OIDplus system URL (or canonical system URL)
1835
                $sysurl = OIDplus::getSystemUrl(self::PATH_ABSOLUTE_CANONICAL);
2135
                $sysurl = OIDplus::getSystemUrl(self::PATH_ABSOLUTE_CANONICAL);
1836
 
2136
 
1837
                // Second part: Directory
2137
                // Second part: Directory
Line 1852... Line 2152...
1852
 
2152
 
1853
                return $sysurl.$res.$tmp.$tmp2;
2153
                return $sysurl.$res.$tmp.$tmp2;
1854
        }
2154
        }
1855
 
2155
 
1856
        private static $shutdown_functions = array();
2156
        private static $shutdown_functions = array();
-
 
2157
 
-
 
2158
        /**
-
 
2159
         * @param $func
-
 
2160
         * @return void
-
 
2161
         */
1857
        public static function register_shutdown_function($func) {
2162
        public static function register_shutdown_function($func) {
1858
                self::$shutdown_functions[] = $func;
2163
                self::$shutdown_functions[] = $func;
1859
        }
2164
        }
1860
 
2165
 
-
 
2166
        /**
-
 
2167
         * @return void
-
 
2168
         */
1861
        public static function invoke_shutdown() {
2169
        public static function invoke_shutdown() {
1862
                foreach (self::$shutdown_functions as $func) {
2170
                foreach (self::$shutdown_functions as $func) {
1863
                        $func();
2171
                        $func();
1864
                }
2172
                }
1865
        }
2173
        }
1866
 
2174
 
-
 
2175
        /**
-
 
2176
         * @return string[]
-
 
2177
         * @throws OIDplusException
-
 
2178
         */
1867
        public static function getAvailableLangs() {
2179
        public static function getAvailableLangs(): array {
1868
                $langs = array();
2180
                $langs = array();
1869
                foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
2181
                foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
1870
                        $code = $pluginManifest->getLanguageCode();
2182
                        $code = $pluginManifest->getLanguageCode();
1871
                        $langs[] = $code;
2183
                        $langs[] = $code;
1872
                }
2184
                }
1873
                return $langs;
2185
                return $langs;
1874
        }
2186
        }
1875
 
2187
 
-
 
2188
        /**
-
 
2189
         * @return string
-
 
2190
         * @throws OIDplusConfigInitializationException
-
 
2191
         * @throws OIDplusException
-
 
2192
         */
1876
        public static function getDefaultLang() {
2193
        public static function getDefaultLang(): string {
1877
                static $thrownOnce = false; // avoid endless loop inside OIDplusConfigInitializationException
2194
                static $thrownOnce = false; // avoid endless loop inside OIDplusConfigInitializationException
1878
 
2195
 
1879
                $lang = self::baseConfig()->getValue('DEFAULT_LANGUAGE', 'enus');
2196
                $lang = self::baseConfig()->getValue('DEFAULT_LANGUAGE', 'enus');
1880
 
2197
 
1881
                if (!in_array($lang,self::getAvailableLangs())) {
2198
                if (!in_array($lang,self::getAvailableLangs())) {
Line 1888... Line 2205...
1888
                }
2205
                }
1889
 
2206
 
1890
                return $lang;
2207
                return $lang;
1891
        }
2208
        }
1892
 
2209
 
-
 
2210
        /**
-
 
2211
         * @return false|string
-
 
2212
         * @throws OIDplusConfigInitializationException
-
 
2213
         * @throws OIDplusException
-
 
2214
         */
1893
        public static function getCurrentLang() {
2215
        public static function getCurrentLang() {
1894
                if (isset($_GET['lang'])) {
2216
                if (isset($_GET['lang'])) {
1895
                        $lang = $_GET['lang'];
2217
                        $lang = $_GET['lang'];
1896
                } else if (isset($_POST['lang'])) {
2218
                } else if (isset($_POST['lang'])) {
1897
                        $lang = $_POST['lang'];
2219
                        $lang = $_POST['lang'];
1898
                } else if (isset($_COOKIE['LANGUAGE'])) {
2220
                } else if (isset($_COOKIE['LANGUAGE'])) {
1899
                        $lang = $_COOKIE['LANGUAGE'];
2221
                        $lang = $_COOKIE['LANGUAGE'];
1900
                } else {
2222
                } else {
1901
                        $lang = self::getDefaultLang();
2223
                        $lang = self::getDefaultLang();
1902
                }
2224
                }
1903
                $lang = substr(preg_replace('@[^a-z]@ismU', '', $lang),0,4); // sanitize
2225
                return substr(preg_replace('@[^a-z]@ismU', '', $lang),0,4); // sanitize
1904
                return $lang;
-
 
1905
        }
2226
        }
1906
 
2227
 
-
 
2228
        /**
-
 
2229
         * @return void
-
 
2230
         * @throws OIDplusException
-
 
2231
         */
1907
        public static function handleLangArgument() {
2232
        public static function handleLangArgument() {
1908
                if (isset($_GET['lang'])) {
2233
                if (isset($_GET['lang'])) {
1909
                        // The "?lang=" argument is only for NoScript-Browsers/SearchEngines
2234
                        // The "?lang=" argument is only for NoScript-Browsers/SearchEngines
1910
                        // In case someone who has JavaScript clicks a ?lang= link, they should get
2235
                        // In case someone who has JavaScript clicks a ?lang= link, they should get
1911
                        // the page in that language, but the cookie must be set, otherwise
2236
                        // the page in that language, but the cookie must be set, otherwise
Line 1916... Line 2241...
1916
                        OIDplus::cookieUtils()->setcookie('LANGUAGE', $_POST['lang'], 0, true/*HttpOnly off, because JavaScript also needs translation*/);
2241
                        OIDplus::cookieUtils()->setcookie('LANGUAGE', $_POST['lang'], 0, true/*HttpOnly off, because JavaScript also needs translation*/);
1917
                }
2242
                }
1918
        }
2243
        }
1919
 
2244
 
1920
        private static $translationArray = array();
2245
        private static $translationArray = array();
-
 
2246
 
-
 
2247
        /**
-
 
2248
         * @param $translation_file
-
 
2249
         * @return array|mixed
-
 
2250
         */
1921
        protected static function getTranslationFileContents($translation_file) {
2251
        protected static function getTranslationFileContents($translation_file) {
1922
                // First, try the cache
2252
                // First, try the cache
1923
                $cache_file = __DIR__ . '/../../userdata/cache/translation_'.md5($translation_file).'.ser';
2253
                $cache_file = __DIR__ . '/../../userdata/cache/translation_'.md5($translation_file).'.ser';
1924
                if (file_exists($cache_file) && (filemtime($cache_file) == filemtime($translation_file))) {
2254
                if (file_exists($cache_file) && (filemtime($cache_file) == filemtime($translation_file))) {
1925
                        $cac = @unserialize(file_get_contents($cache_file));
2255
                        $cac = @unserialize(file_get_contents($cache_file));
Line 1937... Line 2267...
1937
                }
2267
                }
1938
                @file_put_contents($cache_file,serialize($cac));
2268
                @file_put_contents($cache_file,serialize($cac));
1939
                @touch($cache_file,filemtime($translation_file));
2269
                @touch($cache_file,filemtime($translation_file));
1940
                return $cac;
2270
                return $cac;
1941
        }
2271
        }
-
 
2272
 
-
 
2273
        /**
-
 
2274
         * @param string $requested_lang
-
 
2275
         * @return array
-
 
2276
         * @throws OIDplusException
-
 
2277
         */
1942
        public static function getTranslationArray($requested_lang='*') {
2278
        public static function getTranslationArray(string $requested_lang='*'): array {
1943
                foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
2279
                foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
1944
                        $lang = $pluginManifest->getLanguageCode();
2280
                        $lang = $pluginManifest->getLanguageCode();
1945
                        if (strpos($lang,'/') !== false) continue; // just to be sure
2281
                        if (strpos($lang,'/') !== false) continue; // just to be sure
1946
                        if (strpos($lang,'\\') !== false) continue; // just to be sure
2282
                        if (strpos($lang,'\\') !== false) continue; // just to be sure
1947
                        if (strpos($lang,'..') !== false) continue; // just to be sure
2283
                        if (strpos($lang,'..') !== false) continue; // just to be sure
Line 1968... Line 2304...
1968
                        }
2304
                        }
1969
                }
2305
                }
1970
                return self::$translationArray;
2306
                return self::$translationArray;
1971
        }
2307
        }
1972
 
2308
 
-
 
2309
        /**
-
 
2310
         * @return mixed
-
 
2311
         */
1973
        public static function getEditionInfo() {
2312
        public static function getEditionInfo() {
1974
                return @parse_ini_file(__DIR__.'/../edition.ini', true)['Edition'];
2313
                return @parse_ini_file(__DIR__.'/../edition.ini', true)['Edition'];
1975
        }
2314
        }
1976
 
2315
 
-
 
2316
        /**
-
 
2317
         * @return false|string
-
 
2318
         */
1977
        public static function findGitFolder() {
2319
        public static function findGitFolder() {
1978
                // Git command line saves git information in folder ".git"
2320
                // Git command line saves git information in folder ".git"
1979
                // Plesk git saves git information in folder "../../../git/oidplus/" (or similar)
2321
                // Plesk git saves git information in folder "../../../git/oidplus/" (or similar)
1980
                $dir = OIDplus::localpath();
2322
                $dir = OIDplus::localpath();
1981
                if (is_dir($dir.'/.git')) return $dir.'/.git';
2323
                if (is_dir($dir.'/.git')) return $dir.'/.git';
Line 1993... Line 2335...
1993
                        $i++;
2335
                        $i++;
1994
                } while (($i<100) && ($dir != ($new_dir = @realpath($dir.'/../'))) && ($dir = $new_dir));
2336
                } while (($i<100) && ($dir != ($new_dir = @realpath($dir.'/../'))) && ($dir = $new_dir));
1995
                return false;
2337
                return false;
1996
        }
2338
        }
1997
 
2339
 
-
 
2340
        /**
-
 
2341
         * @return false|string
-
 
2342
         */
1998
        public static function getGitsvnRevision($dir='') {
2343
        public static function getGitsvnRevision() {
1999
                try {
2344
                try {
2000
                        // tries command line and binary parsing
2345
                        // tries command line and binary parsing
2001
                        // requires vendor/danielmarschall/git_utils.inc.php
2346
                        // requires vendor/danielmarschall/git_utils.inc.php
2002
                        $git_dir = OIDplus::findGitFolder();
2347
                        $git_dir = OIDplus::findGitFolder();
2003
                        if ($git_dir === false) return false;
2348
                        if ($git_dir === false) return false;
Line 2012... Line 2357...
2012
                } else {
2357
                } else {
2013
                        return false;
2358
                        return false;
2014
                }
2359
                }
2015
        }
2360
        }
2016
 
2361
 
-
 
2362
        /**
-
 
2363
         * @param string $static_node_id
-
 
2364
         * @param bool $throw_exception
-
 
2365
         * @return string
-
 
2366
         */
2017
        public static function prefilterQuery($static_node_id, $throw_exception) {
2367
        public static function prefilterQuery(string $static_node_id, bool $throw_exception): string {
2018
                // Let namespace be case-insensitive
2368
                // Let namespace be case-insensitive
2019
                $ary = explode(':', $static_node_id, 2);
2369
                $ary = explode(':', $static_node_id, 2);
2020
                $ary[0] = strtolower($ary[0]);
2370
                $ary[0] = strtolower($ary[0]);
2021
                $static_node_id = implode(':', $ary);
2371
                $static_node_id = implode(':', $ary);
2022
 
2372
 
Line 2026... Line 2376...
2026
                }
2376
                }
2027
 
2377
 
2028
                return $static_node_id;
2378
                return $static_node_id;
2029
        }
2379
        }
2030
 
2380
 
-
 
2381
        /**
-
 
2382
         * @return bool
-
 
2383
         */
2031
        public static function isCronjob() {
2384
        public static function isCronjob(): bool {
2032
                return explode('.',basename($_SERVER['SCRIPT_NAME']))[0] === 'cron';
2385
                return explode('.',basename($_SERVER['SCRIPT_NAME']))[0] === 'cron';
2033
        }
2386
        }
2034
 
2387
 
-
 
2388
        /**
-
 
2389
         * Since OIDplus svn-184, entries in the database need to have a canonical ID
-
 
2390
         * If the ID is not canonical (e.g. GUIDs missing hyphens), the object cannot be opened in OIDplus
-
 
2391
         * This script re-canonizes the object IDs if required.
-
 
2392
         * In SVN Rev 856, the canonization for GUID, IPv4 and IPv6 have changed, requiring another
-
 
2393
         * re-canonization
-
 
2394
         * @return void
-
 
2395
         * @throws OIDplusException
-
 
2396
         */
2035
        private static function recanonizeObjects() {
2397
        private static function recanonizeObjects() {
2036
                //
-
 
2037
                // Since OIDplus svn-184, entries in the database need to have a canonical ID
-
 
2038
                // If the ID is not canonical (e.g. GUIDs missing hyphens), the object cannot be opened in OIDplus
-
 
2039
                // This script re-canonizes the object IDs if required.
-
 
2040
                // In SVN Rev 856, the canonization for GUID, IPv4 and IPv6 have changed, requiring another
-
 
2041
                // re-canonization
-
 
2042
                //
-
 
2043
                $res = OIDplus::db()->query("select id from ###objects");
2398
                $res = OIDplus::db()->query("select id from ###objects");
2044
                while ($row = $res->fetch_array()) {
2399
                while ($row = $res->fetch_array()) {
2045
                        $ida = $row['id'];
2400
                        $ida = $row['id'];
2046
                        $obj = OIDplusObject::parse($ida);
2401
                        $obj = OIDplusObject::parse($ida);
2047
                        if (!$obj) continue;
2402
                        if (!$obj) continue;