Subversion Repositories oidplus

Rev

Rev 357 | Rev 362 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
class OIDplus {
281 daniel-mar 21
        private static /*OIDplusPagePlugin[]*/ $pagePlugins = array();
22
        private static /*OIDplusAuthPlugin[]*/ $authPlugins = array();
289 daniel-mar 23
        private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array();
227 daniel-mar 24
        private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array();
25
        private static /*string[]*/ $enabledObjectTypes = array();
26
        private static /*string[]*/ $disabledObjectTypes = array();
27
        private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array();
274 daniel-mar 28
        private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array();
355 daniel-mar 29
        private static /*OIDplusLanguagePlugin[]*/ $languagePlugins = array();
2 daniel-mar 30
 
280 daniel-mar 31
        protected static $html = true;
236 daniel-mar 32
 
355 daniel-mar 33
        /*public*/ const DEFAULT_LANGUAGE = 'enus';
34
 
2 daniel-mar 35
        private function __construct() {
36
        }
295 daniel-mar 37
 
263 daniel-mar 38
        # --- Static classes
274 daniel-mar 39
 
263 daniel-mar 40
        private static $baseConfig = null;
41
        private static $old_config_format = false;
261 daniel-mar 42
        public static function baseConfig() {
263 daniel-mar 43
                $first_init = false;
274 daniel-mar 44
 
263 daniel-mar 45
                if ($first_init = is_null(self::$baseConfig)) {
46
                        self::$baseConfig = new OIDplusBaseConfig();
261 daniel-mar 47
                }
48
 
263 daniel-mar 49
                if ($first_init) {
261 daniel-mar 50
                        // Include a file containing various size/depth limitations of OIDs
294 daniel-mar 51
                        // It is important to include it before userdata/baseconfig/config.inc.php was included,
52
                        // so we can give userdata/baseconfig/config.inc.php the chance to override the values.
261 daniel-mar 53
 
294 daniel-mar 54
                        include OIDplus::basePath().'/includes/limits.inc.php';
261 daniel-mar 55
 
56
                        // Include config file
295 daniel-mar 57
 
294 daniel-mar 58
                        $config_file = OIDplus::basePath() . '/userdata/baseconfig/config.inc.php';
59
                        $config_file_old = OIDplus::basePath() . '/includes/config.inc.php'; // backwards compatibility
295 daniel-mar 60
 
294 daniel-mar 61
                        if (!file_exists($config_file) && file_exists($config_file_old)) {
62
                                $config_file = $config_file_old;
63
                        }
261 daniel-mar 64
 
294 daniel-mar 65
                        if (file_exists($config_file)) {
263 daniel-mar 66
                                if (self::$old_config_format) {
67
                                        // Note: We may only include it once due to backwards compatibility,
68
                                        //       since in version 2.0, the configuration was defined using define() statements
69
                                        // Attention: This does mean that a full re-init (e.g. for test cases) is not possible
70
                                        //            if a version 2.0 config is used!
294 daniel-mar 71
                                        include_once $config_file;
263 daniel-mar 72
                                } else {
294 daniel-mar 73
                                        include $config_file;
263 daniel-mar 74
                                }
261 daniel-mar 75
 
76
                                if (defined('OIDPLUS_CONFIG_VERSION') && (OIDPLUS_CONFIG_VERSION == 2.0)) {
263 daniel-mar 77
                                        self::$old_config_format = true;
274 daniel-mar 78
 
261 daniel-mar 79
                                        // Backwards compatibility 2.0 => 2.1
80
                                        foreach (get_defined_constants(true)['user'] as $name => $value) {
81
                                                $name = str_replace('OIDPLUS_', '', $name);
82
                                                if ($name == 'SESSION_SECRET') $name = 'SERVER_SECRET';
83
                                                if ($name == 'MYSQL_QUERYLOG') $name = 'QUERY_LOGFILE';
84
                                                if (($name == 'MYSQL_PASSWORD') || ($name == 'ODBC_PASSWORD') || ($name == 'PDO_PASSWORD') || ($name == 'PGSQL_PASSWORD')) {
263 daniel-mar 85
                                                        self::$baseConfig->setValue($name, base64_decode($value));
261 daniel-mar 86
                                                } else {
87
                                                        if ($name == 'CONFIG_VERSION') $value = 2.1;
263 daniel-mar 88
                                                        self::$baseConfig->setValue($name, $value);
261 daniel-mar 89
                                                }
90
                                        }
91
                                }
92
                        } else {
294 daniel-mar 93
                                if (!is_dir(OIDplus::basePath().'/setup')) {
360 daniel-mar 94
                                        throw new OIDplusConfigInitializationException(_L('File %1 is missing, but setup can\'t be started because its directory missing.','userdata/baseconfig/config.inc.php'));
261 daniel-mar 95
                                } else {
280 daniel-mar 96
                                        if (self::$html) {
349 daniel-mar 97
                                                if (strpos($_SERVER['REQUEST_URI'], OIDplus::getSystemUrl(true).'setup/') !== 0) {
98
                                                        header('Location:'.OIDplus::getSystemUrl().'setup/');
360 daniel-mar 99
                                                        die(_L('Redirecting to setup...'));
349 daniel-mar 100
                                                } else {
101
                                                        return self::$baseConfig;
102
                                                }
261 daniel-mar 103
                                        } else {
104
                                                // This can be displayed in e.g. ajax.php
360 daniel-mar 105
                                                throw new OIDplusConfigInitializationException(_L('File %1 is missing. Please run setup again.','userdata/baseconfig/config.inc.php'));
261 daniel-mar 106
                                        }
107
                                }
108
                        }
109
 
110
                        // Check important config settings
111
 
263 daniel-mar 112
                        if (self::$baseConfig->getValue('CONFIG_VERSION') != 2.1) {
360 daniel-mar 113
                                throw new OIDplusConfigInitializationException(_L("The information located in %1 is outdated.",$config_file));
261 daniel-mar 114
                        }
115
 
263 daniel-mar 116
                        if (self::$baseConfig->getValue('SERVER_SECRET', '') === '') {
360 daniel-mar 117
                                throw new OIDplusConfigInitializationException(_L("You must set a value for SERVER_SECRET in %1 for the system to operate secure.",$config_file));
261 daniel-mar 118
                        }
119
                }
120
 
263 daniel-mar 121
                return self::$baseConfig;
261 daniel-mar 122
        }
123
 
263 daniel-mar 124
        private static $config = null;
2 daniel-mar 125
        public static function config() {
263 daniel-mar 126
                if ($first_init = is_null(self::$config)) {
127
                        self::$config = new OIDplusConfig();
2 daniel-mar 128
                }
263 daniel-mar 129
 
130
                if ($first_init) {
131
                        // These are important settings for base functionalities and therefore are not inside plugins
132
                        self::$config->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
133
                                if (empty($value)) {
360 daniel-mar 134
                                        throw new OIDplusException(_L('Please enter a value for the system title.'));
263 daniel-mar 135
                                }
136
                        });
137
                        self::$config->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
138
                                if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) {
360 daniel-mar 139
                                        throw new OIDplusException(_L('This is not a correct email address'));
263 daniel-mar 140
                                }
141
                        });
142
                        self::$config->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
143
                                if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) {
360 daniel-mar 144
                                        throw new OIDplusException(_L('This is not a correct email address'));
263 daniel-mar 145
                                }
146
                        });
147
                        self::$config->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', OIDplusConfig::PROTECTION_READONLY, function($value) {
148
                                // Nothing here yet
149
                        });
150
                        self::$config->prepareConfigKey('objecttypes_enabled', 'Enabled object types and their order, separated with a semicolon (please reload the page so that the change is applied)', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
151
                                # TODO: when objecttypes_enabled is changed at the admin control panel, we need to do a reload of the page, so that jsTree will be updated. Is there anything we can do?
152
 
153
                                $ary = explode(';',$value);
154
                                $uniq_ary = array_unique($ary);
155
 
156
                                if (count($ary) != count($uniq_ary)) {
360 daniel-mar 157
                                        throw new OIDplusException(_L('Please check your input. Some object types are double.'));
263 daniel-mar 158
                                }
159
 
160
                                foreach ($ary as $ot_check) {
161
                                        $ns_found = false;
162
                                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
163
                                                if ($ot::ns() == $ot_check) {
164
                                                        $ns_found = true;
165
                                                        break;
166
                                                }
167
                                        }
168
                                        foreach (OIDplus::getDisabledObjectTypes() as $ot) {
169
                                                if ($ot::ns() == $ot_check) {
170
                                                        $ns_found = true;
171
                                                        break;
172
                                                }
173
                                        }
174
                                        if (!$ns_found) {
360 daniel-mar 175
                                                throw new OIDplusException(_L('Please check your input. Namespace "%1" is not found',$ot_check));
263 daniel-mar 176
                                        }
177
                                }
178
                        });
179
                        self::$config->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
180
                                // Nothing here yet
181
                        });
182
                        self::$config->prepareConfigKey('oidplus_public_key', 'Public key for this system. If you "clone" your system, you must delete this key (e.g. using phpMyAdmin), so that a new one is created.', '', OIDplusConfig::PROTECTION_READONLY, function($value) {
183
                                // Nothing here yet
184
                        });
324 daniel-mar 185
                        self::$config->prepareConfigKey('last_known_system_url', 'Last known System URL', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
186
                                // Nothing here yet
187
                        });
263 daniel-mar 188
                }
189
 
190
                return self::$config;
2 daniel-mar 191
        }
192
 
263 daniel-mar 193
        private static $gui = null;
2 daniel-mar 194
        public static function gui() {
263 daniel-mar 195
                if (is_null(self::$gui)) {
196
                        self::$gui = new OIDplusGui();
86 daniel-mar 197
                }
263 daniel-mar 198
                return self::$gui;
2 daniel-mar 199
        }
200
 
263 daniel-mar 201
        private static $authUtils = null;
2 daniel-mar 202
        public static function authUtils() {
263 daniel-mar 203
                if (is_null(self::$authUtils)) {
204
                        self::$authUtils = new OIDplusAuthUtils();
86 daniel-mar 205
                }
263 daniel-mar 206
                return self::$authUtils;
2 daniel-mar 207
        }
208
 
263 daniel-mar 209
        private static $mailUtils = null;
250 daniel-mar 210
        public static function mailUtils() {
263 daniel-mar 211
                if (is_null(self::$mailUtils)) {
212
                        self::$mailUtils = new OIDplusMailUtils();
250 daniel-mar 213
                }
263 daniel-mar 214
                return self::$mailUtils;
250 daniel-mar 215
        }
216
 
263 daniel-mar 217
        private static $menuUtils = null;
250 daniel-mar 218
        public static function menuUtils() {
263 daniel-mar 219
                if (is_null(self::$menuUtils)) {
220
                        self::$menuUtils = new OIDplusMenuUtils();
250 daniel-mar 221
                }
263 daniel-mar 222
                return self::$menuUtils;
250 daniel-mar 223
        }
224
 
263 daniel-mar 225
        private static $logger = null;
115 daniel-mar 226
        public static function logger() {
263 daniel-mar 227
                if (is_null(self::$logger)) {
228
                        self::$logger = new OIDplusLogger();
115 daniel-mar 229
                }
263 daniel-mar 230
                return self::$logger;
115 daniel-mar 231
        }
232
 
263 daniel-mar 233
        private static $sesHandler = null;
86 daniel-mar 234
        public static function sesHandler() {
263 daniel-mar 235
                if (is_null(self::$sesHandler)) {
236
                        self::$sesHandler = new OIDplusSessionHandler();
86 daniel-mar 237
                }
263 daniel-mar 238
                return self::$sesHandler;
86 daniel-mar 239
        }
240
 
274 daniel-mar 241
        # --- SQL slang plugin
242
 
243
        private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) {
244
                $name = $plugin::id();
245
                if ($name === false) return false;
246
 
247
                self::$sqlSlangPlugins[$name] = $plugin;
248
 
249
                return true;
250
        }
251
 
252
        public static function getSqlSlangPlugins() {
253
                return self::$sqlSlangPlugins;
254
        }
255
 
318 daniel-mar 256
        public static function getSqlSlangPlugin($id)/*: ?OIDplusSqlSlangPlugin*/ {
257
                if (isset(self::$sqlSlangPlugins[$id])) {
258
                        return self::$sqlSlangPlugins[$id];
259
                } else {
260
                        return null;
261
                }
262
        }
263
 
230 daniel-mar 264
        # --- Database plugin
74 daniel-mar 265
 
227 daniel-mar 266
        private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) {
275 daniel-mar 267
                $name = $plugin::id();
150 daniel-mar 268
                if ($name === false) return false;
269
 
270
                self::$dbPlugins[$name] = $plugin;
271
 
272
                return true;
273
        }
274
 
275
        public static function getDatabasePlugins() {
276
                return self::$dbPlugins;
277
        }
278
 
295 daniel-mar 279
        public static function getActiveDatabasePlugin() {
261 daniel-mar 280
                if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN', '') === '') {
360 daniel-mar 281
                        throw new OIDplusConfigInitializationException(_L('No database plugin selected in config file'));
260 daniel-mar 282
                }
261 daniel-mar 283
                if (!isset(self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')])) {
360 daniel-mar 284
                        $db_plugin_name = OIDplus::baseConfig()->getValue('DATABASE_PLUGIN');
285
                        throw new OIDplusConfigInitializationException(_L('Database plugin "%1" not found',$db_plugin_name));
227 daniel-mar 286
                }
295 daniel-mar 287
                return self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')];
227 daniel-mar 288
        }
289
 
295 daniel-mar 290
        private static $dbMainSession = null;
291
        public static function db() {
292
                if (is_null(self::$dbMainSession)) {
293
                        self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection();
294
                }
295
                if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect();
296
                return self::$dbMainSession;
297
        }
298
 
299
        private static $dbIsolatedSession = null;
300
        public static function dbIsolated() {
301
                if (is_null(self::$dbIsolatedSession)) {
302
                        self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection();
303
                }
304
                if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect();
305
                return self::$dbIsolatedSession;
306
        }
307
 
227 daniel-mar 308
        # --- Page plugin
309
 
224 daniel-mar 310
        private static function registerPagePlugin(OIDplusPagePlugin $plugin) {
281 daniel-mar 311
                self::$pagePlugins[] = $plugin;
61 daniel-mar 312
 
313
                return true;
314
        }
315
 
281 daniel-mar 316
        public static function getPagePlugins() {
317
                return self::$pagePlugins;
61 daniel-mar 318
        }
319
 
227 daniel-mar 320
        # --- Auth plugin
321
 
322
        private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) {
323
                self::$authPlugins[] = $plugin;
324
                return true;
325
        }
326
 
221 daniel-mar 327
        public static function getAuthPlugins() {
328
                return self::$authPlugins;
329
        }
330
 
355 daniel-mar 331
        # --- Language plugin
332
 
333
        private static function registerLanguagePlugin(OIDplusLanguagePlugin $plugin) {
334
                self::$languagePlugins[] = $plugin;
335
                return true;
336
        }
337
 
338
        public static function getLanguagePlugins() {
339
                return self::$languagePlugins;
340
        }
341
 
289 daniel-mar 342
        # --- Logger plugin
343
 
344
        private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) {
345
                self::$loggerPlugins[] = $plugin;
346
                return true;
347
        }
348
 
349
        public static function getLoggerPlugins() {
350
                return self::$loggerPlugins;
351
        }
352
 
227 daniel-mar 353
        # --- Object type plugin
354
 
355
        private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) {
356
                self::$objectTypePlugins[] = $plugin;
357
 
358
                $ot = $plugin::getObjectTypeClassName();
359
                self::registerObjectType($ot);
360
 
361
                return true;
362
        }
363
 
224 daniel-mar 364
        private static function registerObjectType($ot) {
66 daniel-mar 365
                $ns = $ot::ns();
366
 
360 daniel-mar 367
                if (empty($ns)) throw new OIDplusException(_L('Attention: Empty NS at %1',$ot));
66 daniel-mar 368
 
369
                $ns_found = false;
227 daniel-mar 370
                foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) {
66 daniel-mar 371
                        if ($test_ot::ns() == $ns) {
372
                                $ns_found = true;
373
                                break;
374
                        }
375
                }
376
                if ($ns_found) {
360 daniel-mar 377
                        throw new OIDplusException(_L('Attention: Two objectType plugins use the same namespace "%1"!',$ns));
66 daniel-mar 378
                }
379
 
380
                $init = OIDplus::config()->getValue("objecttypes_initialized");
381
                $init_ary = empty($init) ? array() : explode(';', $init);
70 daniel-mar 382
                $init_ary = array_map('trim', $init_ary);
66 daniel-mar 383
 
384
                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
385
                $enabled_ary = empty($enabled) ? array() : explode(';', $enabled);
70 daniel-mar 386
                $enabled_ary = array_map('trim', $enabled_ary);
66 daniel-mar 387
 
79 daniel-mar 388
                $do_enable = false;
389
                if (in_array($ns, $enabled_ary)) {
390
                        $do_enable = true;
391
                } else {
392
                        if (!OIDplus::config()->getValue('registration_done')) {
393
                                $do_enable = $ns == 'oid';
394
                        } else {
395
                                $do_enable = !in_array($ns, $init_ary);
396
                        }
397
                }
398
 
399
                if ($do_enable) {
227 daniel-mar 400
                        self::$enabledObjectTypes[] = $ot;
401
                        usort(self::$enabledObjectTypes, function($a, $b) {
66 daniel-mar 402
                                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
403
                                $enabled_ary = explode(';', $enabled);
404
 
405
                                $idx_a = array_search($a::ns(), $enabled_ary);
406
                                $idx_b = array_search($b::ns(), $enabled_ary);
407
 
408
                                if ($idx_a == $idx_b) {
409
                                    return 0;
410
                                }
411
                                return ($idx_a > $idx_b) ? +1 : -1;
412
                        });
74 daniel-mar 413
                } else {
414
                        self::$disabledObjectTypes[] = $ot;
66 daniel-mar 415
                }
416
 
417
                if (!in_array($ns, $init_ary)) {
418
                        // Was never initialized before, so we add it to the list of enabled object types once
419
 
79 daniel-mar 420
                        if ($do_enable) {
421
                                $enabled_ary[] = $ns;
422
                                OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary));
423
                        }
66 daniel-mar 424
 
425
                        $init_ary[] = $ns;
426
                        OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
427
                }
61 daniel-mar 428
        }
429
 
227 daniel-mar 430
        public static function getObjectTypePlugins() {
431
                return self::$objectTypePlugins;
61 daniel-mar 432
        }
433
 
227 daniel-mar 434
        public static function getObjectTypePluginsEnabled() {
435
                $res = array();
436
                foreach (self::$objectTypePlugins as $plugin) {
437
                        $ot = $plugin::getObjectTypeClassName();
438
                        if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin;
439
                }
440
                return $res;
74 daniel-mar 441
        }
442
 
227 daniel-mar 443
        public static function getObjectTypePluginsDisabled() {
444
                $res = array();
445
                foreach (self::$objectTypePlugins as $plugin) {
446
                        $ot = $plugin::getObjectTypeClassName();
447
                        if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin;
74 daniel-mar 448
                }
227 daniel-mar 449
                return $res;
74 daniel-mar 450
        }
451
 
227 daniel-mar 452
        public static function getEnabledObjectTypes() {
453
                return self::$enabledObjectTypes;
454
        }
74 daniel-mar 455
 
227 daniel-mar 456
        public static function getDisabledObjectTypes() {
457
                return self::$disabledObjectTypes;
458
        }
74 daniel-mar 459
 
277 daniel-mar 460
        # --- Plugin handling functions
461
 
320 daniel-mar 462
        public static function getAllPlugins()/*: array*/ {
463
                $res = array();
464
                $res = array_merge($res, self::$pagePlugins);
465
                $res = array_merge($res, self::$authPlugins);
466
                $res = array_merge($res, self::$loggerPlugins);
467
                $res = array_merge($res, self::$objectTypePlugins);
468
                $res = array_merge($res, self::$dbPlugins);
469
                $res = array_merge($res, self::$sqlSlangPlugins);
355 daniel-mar 470
                $res = array_merge($res, self::$languagePlugins);
320 daniel-mar 471
                return $res;
472
        }
473
 
321 daniel-mar 474
        public static function getPluginByOid($oid)/*: ?OIDplusPlugin*/ {
320 daniel-mar 475
                $plugins = self::getAllPlugins();
321 daniel-mar 476
                foreach ($plugins as $plugin) {
477
                        if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) {
478
                                return $plugin;
320 daniel-mar 479
                        }
480
                }
481
                return null;
482
        }
483
 
307 daniel-mar 484
        public static function getPluginManifest($class_name)/*: ?OIDplusPluginManifest*/ {
277 daniel-mar 485
                $reflector = new ReflectionClass($class_name);
308 daniel-mar 486
                $ini = dirname($reflector->getFileName()).'/manifest.xml';
307 daniel-mar 487
                $manifest = new OIDplusPluginManifest();
488
                return $manifest->loadManifest($ini) ? $manifest : null;
277 daniel-mar 489
        }
490
 
307 daniel-mar 491
        public static function getAllPluginManifests($pluginFolderMask='*', $flat=true): array {
277 daniel-mar 492
                $out = array();
279 daniel-mar 493
                // Note: glob() will sort by default, so we do not need a page priority attribute.
494
                //       So you just need to use a numeric plugin directory prefix (padded).
308 daniel-mar 495
                $ary = glob(OIDplus::basePath().'/plugins/'.$pluginFolderMask.'/'.'*'.'/manifest.xml');
277 daniel-mar 496
                foreach ($ary as $ini) {
497
                        if (!file_exists($ini)) continue;
498
 
307 daniel-mar 499
                        $manifest = new OIDplusPluginManifest();
500
                        $manifest->loadManifest($ini);
277 daniel-mar 501
 
307 daniel-mar 502
                        if ($flat) {
503
                                $out[] = $manifest;
504
                        } else {
505
                                $plugintype_folder = basename(dirname(dirname($ini)));
506
                                $pluginname_folder = basename(dirname($ini));
507
 
508
                                if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array();
509
                                if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array();
510
                                $out[$plugintype_folder][$pluginname_folder] = $manifest;
511
                        }
277 daniel-mar 512
                }
513
                return $out;
514
        }
515
 
516
        public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array {
517
                $out = array();
307 daniel-mar 518
                $ary = self::getAllPluginManifests($pluginDirName, false);
320 daniel-mar 519
                $known_plugin_oids = array();
277 daniel-mar 520
                foreach ($ary as $plugintype_folder => $bry) {
521
                        foreach ($bry as $pluginname_folder => $cry) {
307 daniel-mar 522
                                $class_name = $cry->getPhpMainClass();
523
                                if (!$class_name) {
360 daniel-mar 524
                                        throw new OIDplusException(_L('Plugin "%1/%2" is erroneous').': '._L('Manifest does not declare a PHP main class',$plugintype_folder,$pluginname_folder));
277 daniel-mar 525
                                }
297 daniel-mar 526
                                if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) {
527
                                        continue;
528
                                }
292 daniel-mar 529
                                if (!class_exists($class_name)) {
360 daniel-mar 530
                                        throw new OIDplusException(_L('Plugin "%1/%2" is erroneous').': '._L('Manifest declares PHP main class as "%1", but it could not be found',$plugintype_folder,$pluginname_folder,$class_name));
292 daniel-mar 531
                                }
279 daniel-mar 532
                                if (!is_subclass_of($class_name, $expectedPluginClass)) {
360 daniel-mar 533
                                        throw new OIDplusException(_L('Plugin "%1/%2" is erroneous').': '._L('Plugin main class "%1" is expected to be a subclass of "%2"',$plugintype_folder,$pluginname_folder,$class_name,$expectedPluginClass));
279 daniel-mar 534
                                }
308 daniel-mar 535
                                if (($class_name!=$cry->getTypeClass()) && (!is_subclass_of($class_name,$cry->getTypeClass()))) {
360 daniel-mar 536
                                        throw new OIDplusException(_L('Plugin "%1/%2" is erroneous').': '._L('Plugin main class "%1" is expected to be a subclass of "%2", according to type declared in manifest',$plugintype_folder,$pluginname_folder,$class_name,$cry->getTypeClass()));
308 daniel-mar 537
                                }
538
                                if (($cry->getTypeClass()!=$expectedPluginClass) && (!is_subclass_of($cry->getTypeClass(),$expectedPluginClass))) {
360 daniel-mar 539
                                        throw new OIDplusException(_L('Plugin "%1/%2" is erroneous').': '._L('Class declared in manifest is "%1" does not fit expected class for this plugin type "%2"',$plugintype_folder,$pluginname_folder,$cry->getTypeClass(),$expectedPluginClass));
308 daniel-mar 540
                                }
541
 
320 daniel-mar 542
                                $plugin_oid = $cry->getOid();
543
                                if (!$plugin_oid) {
360 daniel-mar 544
                                        throw new OIDplusException(_L('Plugin "%1/%2" is erroneous').': '._L('Does not have an OID',$plugintype_folder,$pluginname_folder));
320 daniel-mar 545
                                }
546
                                if (!oid_valid_dotnotation($plugin_oid, false, false, 2)) {
360 daniel-mar 547
                                        throw new OIDplusException(_L('Plugin "%1/%2" is erroneous').': '._L('Plugin OID "%1" is invalid (needs to be valid dot-notation)',$plugintype_folder,$pluginname_folder,$plugin_oid));
320 daniel-mar 548
                                }
549
                                if (isset($known_plugin_oids[$plugin_oid])) {
360 daniel-mar 550
                                        throw new OIDplusException(_L('Plugin "%1/%2" is erroneous').': '._L('The OID "%1" is already used by the plugin "%2"',$plugintype_folder,$pluginname_folder,$plugin_oid,$known_plugin_oids[$plugin_oid]));
320 daniel-mar 551
                                } else {
552
                                        $known_plugin_oids[$plugin_oid] = $plugintype_folder.'/'.$pluginname_folder;
553
                                }
554
 
279 daniel-mar 555
                                $out[] = $class_name;
556
                                if (!is_null($registerCallback)) {
557
                                        call_user_func($registerCallback, new $class_name());
558
                                }
277 daniel-mar 559
                        }
560
 
561
                }
562
                return $out;
563
        }
564
 
227 daniel-mar 565
        # --- Initialization of OIDplus
206 daniel-mar 566
 
2 daniel-mar 567
        public static function init($html=true) {
236 daniel-mar 568
                self::$html = $html;
569
 
263 daniel-mar 570
                // Reset internal state, so we can re-init verything if required
274 daniel-mar 571
 
263 daniel-mar 572
                if (self::$old_config_format) {
573
                        // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice
360 daniel-mar 574
                        throw new OIDplusConfigInitializationException(_L('A full re-initialization is not possible if a version 2.0 config file (containing "defines") is used. Please update to a config 2.1 file by running setup again.'));
263 daniel-mar 575
                }
274 daniel-mar 576
 
263 daniel-mar 577
                self::$config = null;
578
                self::$baseConfig = null;
579
                self::$gui = null;
580
                self::$authUtils = null;
581
                self::$mailUtils = null;
582
                self::$menuUtils = null;
583
                self::$logger = null;
584
                self::$sesHandler = null;
295 daniel-mar 585
                self::$dbMainSession = null;
586
                self::$dbIsolatedSession = null;
263 daniel-mar 587
                self::$pagePlugins = array();
588
                self::$authPlugins = array();
289 daniel-mar 589
                self::$loggerPlugins = array();
263 daniel-mar 590
                self::$objectTypePlugins = array();
591
                self::$enabledObjectTypes = array();
592
                self::$disabledObjectTypes = array();
593
                self::$dbPlugins = array();
274 daniel-mar 594
                self::$sqlSlangPlugins = array();
355 daniel-mar 595
                self::$languagePlugins = array();
263 daniel-mar 596
                self::$system_id_cache = null;
597
                self::$sslAvailableCache = null;
74 daniel-mar 598
 
263 daniel-mar 599
                // Continue...
600
 
294 daniel-mar 601
                OIDplus::baseConfig(); // this loads the base configuration located in userdata/baseconfig/config.inc.php (once!)
263 daniel-mar 602
                                       // You can do changes to the configuration afterwards using OIDplus::baseConfig()->...
603
 
150 daniel-mar 604
                // Register database types (highest priority)
605
 
274 daniel-mar 606
                // SQL slangs
607
 
294 daniel-mar 608
                self::registerAllPlugins('sqlSlang', 'OIDplusSqlSlangPlugin', array('OIDplus','registerSqlSlangPlugin'));
274 daniel-mar 609
                foreach (OIDplus::getSqlSlangPlugins() as $plugin) {
610
                        $plugin->init($html);
611
                }
612
 
613
                // Database providers
614
 
277 daniel-mar 615
                self::registerAllPlugins('database', 'OIDplusDatabasePlugin', array('OIDplus','registerDatabasePlugin'));
237 daniel-mar 616
                foreach (OIDplus::getDatabasePlugins() as $plugin) {
617
                        $plugin->init($html);
618
                }
619
 
42 daniel-mar 620
                // Do redirect stuff etc.
74 daniel-mar 621
 
230 daniel-mar 622
                self::isSslAvailable(); // This function does automatic redirects
61 daniel-mar 623
 
263 daniel-mar 624
                // Construct the configuration manager
66 daniel-mar 625
 
263 daniel-mar 626
                OIDplus::config(); // During the construction, various system settings are prepared if required
66 daniel-mar 627
 
74 daniel-mar 628
                // Initialize public / private keys
629
 
227 daniel-mar 630
                OIDplus::getPkiStatus(true);
74 daniel-mar 631
 
237 daniel-mar 632
                // Register non-DB plugins
74 daniel-mar 633
 
277 daniel-mar 634
                self::registerAllPlugins('*Pages', 'OIDplusPagePlugin', array('OIDplus','registerPagePlugin'));
635
                self::registerAllPlugins('auth', 'OIDplusAuthPlugin', array('OIDplus','registerAuthPlugin'));
289 daniel-mar 636
                self::registerAllPlugins('logger', 'OIDplusLoggerPlugin', array('OIDplus','registerLoggerPlugin'));
277 daniel-mar 637
                self::registerAllPlugins('objectTypes', 'OIDplusObjectTypePlugin', array('OIDplus','registerObjectTypePlugin'));
355 daniel-mar 638
                self::registerAllPlugins('language', 'OIDplusLanguagePlugin', array('OIDplus','registerLanguagePlugin'));
150 daniel-mar 639
 
237 daniel-mar 640
                // Initialize non-DB plugins
224 daniel-mar 641
 
281 daniel-mar 642
                foreach (OIDplus::getPagePlugins() as $plugin) {
74 daniel-mar 643
                        $plugin->init($html);
644
                }
230 daniel-mar 645
                foreach (OIDplus::getAuthPlugins() as $plugin) {
646
                        $plugin->init($html);
647
                }
289 daniel-mar 648
                foreach (OIDplus::getLoggerPlugins() as $plugin) {
649
                        $plugin->init($html);
650
                }
230 daniel-mar 651
                foreach (OIDplus::getObjectTypePlugins() as $plugin) {
652
                        $plugin->init($html);
653
                }
355 daniel-mar 654
                foreach (OIDplus::getLanguagePlugins() as $plugin) {
655
                        $plugin->init($html);
656
                }
2 daniel-mar 657
        }
42 daniel-mar 658
 
227 daniel-mar 659
        # --- System URL, System ID, PKI, and other functions
660
 
294 daniel-mar 661
        public static function basePath() {
662
                return realpath(__DIR__ . '/../../');
663
        }
664
 
227 daniel-mar 665
        public static function getSystemUrl($relative=false) {
326 daniel-mar 666
                if (!$relative) {
667
                        $res = OIDplus::baseConfig()->getValue('EXPLICIT_ABSOLUTE_SYSTEM_URL', '');
668
                        if ($res !== '') {
349 daniel-mar 669
                                try {
670
                                        OIDplus::config()->setValue('last_known_system_url', $res);
671
                                        return $res;
672
                                } catch (Exception $e) {
673
                                }
326 daniel-mar 674
                        }
675
                }
676
 
227 daniel-mar 677
                if (!isset($_SERVER["SCRIPT_NAME"])) return false;
678
 
679
                $test_dir = dirname($_SERVER['SCRIPT_FILENAME']);
269 daniel-mar 680
                $test_dir = str_replace('\\', '/', $test_dir);
227 daniel-mar 681
                $c = 0;
682
                while (!file_exists($test_dir.'/oidplus_base.js')) {
683
                        $test_dir = dirname($test_dir);
684
                        $c++;
685
                        if ($c == 1000) return false;
686
                }
687
 
688
                $res = dirname($_SERVER['SCRIPT_NAME'].'xxx');
689
 
690
                for ($i=1; $i<=$c; $i++) {
691
                        $res = dirname($res);
692
                }
693
 
269 daniel-mar 694
                $res = str_replace('\\', '/', $res);
227 daniel-mar 695
                if ($res == '/') $res = '';
696
                $res .= '/';
697
 
698
                if (!$relative) {
315 daniel-mar 699
                        if (php_sapi_name() == 'cli') {
349 daniel-mar 700
                                try {
701
                                        return OIDplus::config()->getValue('last_known_system_url', false);
702
                                } catch (Exception $e) {
703
                                }
315 daniel-mar 704
                        }
705
 
228 daniel-mar 706
                        $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on');
360 daniel-mar 707
                        $protocol = $is_ssl ? 'https' : 'http'; // do not translate
352 daniel-mar 708
                        $host = $_SERVER['HTTP_HOST']; // includes port if it is not 80/443
709
                        $res = $protocol.'://'.$host.$res;
324 daniel-mar 710
 
349 daniel-mar 711
                        try {
712
                                OIDplus::config()->setValue('last_known_system_url', $res);
713
                        } catch (Exception $e) {
714
                        }
227 daniel-mar 715
                }
716
 
717
                return $res;
718
        }
719
 
720
        private static $system_id_cache = null;
721
        public static function getSystemId($oid=false) {
722
                if (!is_null(self::$system_id_cache)) {
723
                        $out = self::$system_id_cache;
724
                } else {
725
                        $out = false;
726
 
727
                        if (self::getPkiStatus(true)) {
728
                                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
729
                                if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
730
                                        $out = smallhash(base64_decode($m[1]));
731
                                }
732
                        }
733
                        self::$system_id_cache = $out;
734
                }
350 daniel-mar 735
                if (!$out) return false;
291 daniel-mar 736
                return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out;
227 daniel-mar 737
        }
738
 
739
        public static function getPkiStatus($try_generate=true) {
740
                if (!function_exists('openssl_pkey_new')) return false;
741
 
742
                $privKey = OIDplus::config()->getValue('oidplus_private_key');
743
                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
744
 
745
                if ($try_generate && !verify_private_public_key($privKey, $pubKey)) {
263 daniel-mar 746
                        $pkey_config = array(
227 daniel-mar 747
                            "digest_alg" => "sha512",
748
                            "private_key_bits" => 2048,
749
                            "private_key_type" => OPENSSL_KEYTYPE_RSA,
750
                        );
751
 
752
                        // Create the private and public key
263 daniel-mar 753
                        $res = openssl_pkey_new($pkey_config);
256 daniel-mar 754
 
239 daniel-mar 755
                        if (!$res) return false;
227 daniel-mar 756
 
757
                        // Extract the private key from $res to $privKey
758
                        openssl_pkey_export($res, $privKey);
759
 
760
                        // Extract the public key from $res to $pubKey
761
                        $pubKey = openssl_pkey_get_details($res)["key"];
762
 
239 daniel-mar 763
                        // Log
288 daniel-mar 764
                        OIDplus::logger()->log("[INFO]A!", "Generating new SystemID using a new key pair");
239 daniel-mar 765
 
227 daniel-mar 766
                        // Save the key pair to database
767
                        OIDplus::config()->setValue('oidplus_private_key', $privKey);
768
                        OIDplus::config()->setValue('oidplus_public_key', $pubKey);
769
 
770
                        // Log the new system ID
771
                        if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
772
                                $system_id = smallhash(base64_decode($m[1]));
288 daniel-mar 773
                                OIDplus::logger()->log("[INFO]A!", "Your SystemID is now $system_id");
227 daniel-mar 774
                        }
775
                }
776
 
777
                return verify_private_public_key($privKey, $pubKey);
778
        }
779
 
170 daniel-mar 780
        public static function getInstallType() {
294 daniel-mar 781
                if (!file_exists(OIDplus::basePath().'/oidplus_version.txt') && !is_dir(OIDplus::basePath().'/.svn')) {
360 daniel-mar 782
                        return 'unknown'; // do not translate
170 daniel-mar 783
                }
294 daniel-mar 784
                if (file_exists(OIDplus::basePath().'/oidplus_version.txt') && is_dir(OIDplus::basePath().'/.svn')) {
360 daniel-mar 785
                        return 'ambigous'; // do not translate
170 daniel-mar 786
                }
294 daniel-mar 787
                if (is_dir(OIDplus::basePath().'/.svn')) {
360 daniel-mar 788
                        return 'svn-wc'; // do not translate
170 daniel-mar 789
                }
294 daniel-mar 790
                if (file_exists(OIDplus::basePath().'/oidplus_version.txt')) {
360 daniel-mar 791
                        return 'svn-snapshot'; // do not translate
170 daniel-mar 792
                }
793
        }
794
 
111 daniel-mar 795
        public static function getVersion() {
294 daniel-mar 796
                if (file_exists(OIDplus::basePath().'/oidplus_version.txt') && is_dir(OIDplus::basePath().'/.svn')) {
360 daniel-mar 797
                        return false; // version is ambiguous
111 daniel-mar 798
                }
162 daniel-mar 799
 
294 daniel-mar 800
                if (is_dir(OIDplus::basePath().'/.svn')) {
285 daniel-mar 801
                        // Try to get the version via SQLite3
802
                        if (class_exists('SQLite3')) {
803
                                try {
294 daniel-mar 804
                                        $db = new SQLite3(OIDplus::basePath().'/.svn/wc.db');
285 daniel-mar 805
                                        $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
806
                                        while ($row = $results->fetchArray()) {
360 daniel-mar 807
                                                return 'svn-'.$row['rev']; // do not translate
285 daniel-mar 808
                                        }
809
                                        $db->close();
810
                                        $db = null;
811
                                } catch (Exception $e) {
812
                                }
813
                        }
814
                        if (class_exists('PDO')) {
815
                                try {
294 daniel-mar 816
                                        $pdo = new PDO('sqlite:' . OIDplus::basePath().'/.svn/wc.db');
285 daniel-mar 817
                                        $res = $pdo->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
818
                                        $row = $res->fetch();
360 daniel-mar 819
                                        if ($row !== false) return 'svn-'.$row['rev']; // do not translate
285 daniel-mar 820
                                        $pdo = null;
821
                                } catch (Exception $e) {
822
                                }
823
                        }
824
 
162 daniel-mar 825
                        // Try to find out the SVN version using the shell
285 daniel-mar 826
                        // We don't prioritize this method, because a failed shell access will flood the apache error log with STDERR messages
294 daniel-mar 827
                        $output = @shell_exec('svnversion '.escapeshellarg(OIDplus::basePath()));
285 daniel-mar 828
                        if (preg_match('/\d+/', $output, $match)) {
360 daniel-mar 829
                                return 'svn-'.$match[0]; // do not translate
162 daniel-mar 830
                        }
831
 
294 daniel-mar 832
                        $output = @shell_exec('svn info '.escapeshellarg(OIDplus::basePath()));
360 daniel-mar 833
                        if (preg_match('/Revision:\s*(\d+)/m', $output, $match)) { // do not translate
834
                                return 'svn-'.$match[1]; // do not translate
162 daniel-mar 835
                        }
836
                }
837
 
294 daniel-mar 838
                if (file_exists(OIDplus::basePath().'/oidplus_version.txt')) {
839
                        $cont = file_get_contents(OIDplus::basePath().'/oidplus_version.txt');
360 daniel-mar 840
                        if (preg_match('@Revision (\d+)@', $cont, $m)) // do not translate
841
                                return 'svn-'.$m[1]; // do not translate
162 daniel-mar 842
                }
843
 
239 daniel-mar 844
                return false;
111 daniel-mar 845
        }
846
 
230 daniel-mar 847
        private static $sslAvailableCache = null;
848
        public static function isSslAvailable() {
849
                if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache;
256 daniel-mar 850
 
230 daniel-mar 851
                if (php_sapi_name() == 'cli') {
852
                        self::$sslAvailableCache = false;
853
                        return false;
854
                }
855
 
49 daniel-mar 856
                $timeout = 2;
80 daniel-mar 857
                $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on");
858
                $ssl_port = 443;
227 daniel-mar 859
                $cookie_path = OIDplus::getSystemUrl(true);
83 daniel-mar 860
                if (empty($cookie_path)) $cookie_path = '/';
42 daniel-mar 861
 
261 daniel-mar 862
                $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', 2/*auto*/);
863
 
864
                if ($mode == 0) {
80 daniel-mar 865
                        // No SSL available
230 daniel-mar 866
                        self::$sslAvailableCache = $already_ssl;
80 daniel-mar 867
                        return $already_ssl;
868
                }
869
 
261 daniel-mar 870
                if ($mode == 1) {
80 daniel-mar 871
                        // Force SSL
872
                        if ($already_ssl) {
230 daniel-mar 873
                                self::$sslAvailableCache = true;
80 daniel-mar 874
                                return true;
42 daniel-mar 875
                        } else {
80 daniel-mar 876
                                $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
877
                                header('Location:'.$location);
360 daniel-mar 878
                                die(_L('Redirecting to HTTPS...'));
230 daniel-mar 879
                                self::$sslAvailableCache = true;
80 daniel-mar 880
                                return true;
881
                        }
882
                }
883
 
261 daniel-mar 884
                if ($mode == 2) {
80 daniel-mar 885
                        // Automatic SSL detection
886
 
887
                        if ($already_ssl) {
888
                                // we are already on HTTPS
83 daniel-mar 889
                                setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true);
230 daniel-mar 890
                                self::$sslAvailableCache = true;
80 daniel-mar 891
                                return true;
892
                        } else {
893
                                if (isset($_COOKIE['SSL_CHECK'])) {
894
                                        // We already had the HTTPS detection done before.
895
                                        if ($_COOKIE['SSL_CHECK']) {
896
                                                // HTTPS was detected before, but we are HTTP. Redirect now
897
                                                $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
898
                                                header('Location:'.$location);
360 daniel-mar 899
                                                die(_L('Redirecting to HTTPS...'));
230 daniel-mar 900
                                                self::$sslAvailableCache = true;
80 daniel-mar 901
                                                return true;
902
                                        } else {
903
                                                // No HTTPS available. Do nothing.
230 daniel-mar 904
                                                self::$sslAvailableCache = false;
80 daniel-mar 905
                                                return false;
906
                                        }
49 daniel-mar 907
                                } else {
80 daniel-mar 908
                                        // This is our first check (or the browser didn't accept the SSL_CHECK cookie)
909
                                        if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) {
910
                                                // HTTPS detected. Redirect now, and remember that we had detected HTTPS
83 daniel-mar 911
                                                setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true);
80 daniel-mar 912
                                                $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
913
                                                header('Location:'.$location);
360 daniel-mar 914
                                                die(_L('Redirecting to HTTPS...'));
230 daniel-mar 915
                                                self::$sslAvailableCache = true;
80 daniel-mar 916
                                                return true;
917
                                        } else {
918
                                                // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again.
83 daniel-mar 919
                                                setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true);
230 daniel-mar 920
                                                self::$sslAvailableCache = false;
80 daniel-mar 921
                                                return false;
922
                                        }
49 daniel-mar 923
                                }
42 daniel-mar 924
                        }
925
                }
926
        }
241 daniel-mar 927
 
928
        public static function webpath($target) {
929
                $dir = __DIR__;
930
                $dir = dirname($dir);
931
                $dir = dirname($dir);
932
                $target = substr($target, strlen($dir)+1, strlen($target)-strlen($dir)-1);
933
                if ($target != '') {
934
                        $target = str_replace('\\','/',$target).'/';
935
                }
936
                return $target;
937
        }
355 daniel-mar 938
 
360 daniel-mar 939
        public static function getAvailableLangs() {
940
                $langs = array();
941
                foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
942
                        $xmldata = $pluginManifest->getRawXml();
943
                        $code = $xmldata->language->code->__toString();
944
                        $langs[] = $code;
945
                }
946
                return $langs;
947
        }
948
 
355 daniel-mar 949
        public static function getCurrentLang() {
360 daniel-mar 950
                if (isset($_GET['lang'])) {
951
                        $lang = $_GET['lang'];
952
                } else if (isset($_POST['lang'])) {
953
                        $lang = $_POST['lang'];
954
                } else if (isset($_COOKIE['LANGUAGE'])) {
955
                        $lang = $_COOKIE['LANGUAGE'];
956
                } else {
957
                        $lang = self::DEFAULT_LANGUAGE;
958
                }
959
                $lang = substr(preg_replace('@[^a-z]@ismU', '', $lang),0,4); // sanitize
355 daniel-mar 960
                return $lang;
961
        }
962
 
357 daniel-mar 963
}