Rev 632 | Rev 635 | 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 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 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 | |||
511 | daniel-mar | 20 | if (!defined('INSIDE_OIDPLUS')) die(); |
21 | |||
2 | daniel-mar | 22 | class OIDplus { |
281 | daniel-mar | 23 | private static /*OIDplusPagePlugin[]*/ $pagePlugins = array(); |
24 | private static /*OIDplusAuthPlugin[]*/ $authPlugins = array(); |
||
289 | daniel-mar | 25 | private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array(); |
227 | daniel-mar | 26 | private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array(); |
27 | private static /*string[]*/ $enabledObjectTypes = array(); |
||
28 | private static /*string[]*/ $disabledObjectTypes = array(); |
||
29 | private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array(); |
||
274 | daniel-mar | 30 | private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array(); |
355 | daniel-mar | 31 | private static /*OIDplusLanguagePlugin[]*/ $languagePlugins = array(); |
449 | daniel-mar | 32 | private static /*OIDplusDesignPlugin[]*/ $designPlugins = array(); |
2 | daniel-mar | 33 | |
280 | daniel-mar | 34 | protected static $html = true; |
236 | daniel-mar | 35 | |
362 | daniel-mar | 36 | /*public*/ const DEFAULT_LANGUAGE = 'enus'; // the language of the source code |
355 | daniel-mar | 37 | |
2 | daniel-mar | 38 | private function __construct() { |
39 | } |
||
295 | daniel-mar | 40 | |
263 | daniel-mar | 41 | # --- Static classes |
274 | daniel-mar | 42 | |
263 | daniel-mar | 43 | private static $baseConfig = null; |
44 | private static $old_config_format = false; |
||
261 | daniel-mar | 45 | public static function baseConfig() { |
263 | daniel-mar | 46 | $first_init = false; |
274 | daniel-mar | 47 | |
263 | daniel-mar | 48 | if ($first_init = is_null(self::$baseConfig)) { |
49 | self::$baseConfig = new OIDplusBaseConfig(); |
||
261 | daniel-mar | 50 | } |
51 | |||
263 | daniel-mar | 52 | if ($first_init) { |
261 | daniel-mar | 53 | // Include a file containing various size/depth limitations of OIDs |
294 | daniel-mar | 54 | // It is important to include it before userdata/baseconfig/config.inc.php was included, |
55 | // so we can give userdata/baseconfig/config.inc.php the chance to override the values. |
||
261 | daniel-mar | 56 | |
496 | daniel-mar | 57 | include OIDplus::localpath().'includes/oidplus_limits.inc.php'; |
261 | daniel-mar | 58 | |
59 | // Include config file |
||
295 | daniel-mar | 60 | |
496 | daniel-mar | 61 | $config_file = OIDplus::localpath() . 'userdata/baseconfig/config.inc.php'; |
62 | $config_file_old = OIDplus::localpath() . 'includes/config.inc.php'; // backwards compatibility |
||
295 | daniel-mar | 63 | |
294 | daniel-mar | 64 | if (!file_exists($config_file) && file_exists($config_file_old)) { |
65 | $config_file = $config_file_old; |
||
66 | } |
||
261 | daniel-mar | 67 | |
294 | daniel-mar | 68 | if (file_exists($config_file)) { |
263 | daniel-mar | 69 | if (self::$old_config_format) { |
70 | // Note: We may only include it once due to backwards compatibility, |
||
71 | // since in version 2.0, the configuration was defined using define() statements |
||
72 | // Attention: This does mean that a full re-init (e.g. for test cases) is not possible |
||
73 | // if a version 2.0 config is used! |
||
294 | daniel-mar | 74 | include_once $config_file; |
263 | daniel-mar | 75 | } else { |
294 | daniel-mar | 76 | include $config_file; |
263 | daniel-mar | 77 | } |
261 | daniel-mar | 78 | |
79 | if (defined('OIDPLUS_CONFIG_VERSION') && (OIDPLUS_CONFIG_VERSION == 2.0)) { |
||
263 | daniel-mar | 80 | self::$old_config_format = true; |
274 | daniel-mar | 81 | |
261 | daniel-mar | 82 | // Backwards compatibility 2.0 => 2.1 |
83 | foreach (get_defined_constants(true)['user'] as $name => $value) { |
||
84 | $name = str_replace('OIDPLUS_', '', $name); |
||
85 | if ($name == 'SESSION_SECRET') $name = 'SERVER_SECRET'; |
||
86 | if ($name == 'MYSQL_QUERYLOG') $name = 'QUERY_LOGFILE'; |
||
87 | if (($name == 'MYSQL_PASSWORD') || ($name == 'ODBC_PASSWORD') || ($name == 'PDO_PASSWORD') || ($name == 'PGSQL_PASSWORD')) { |
||
263 | daniel-mar | 88 | self::$baseConfig->setValue($name, base64_decode($value)); |
261 | daniel-mar | 89 | } else { |
90 | if ($name == 'CONFIG_VERSION') $value = 2.1; |
||
263 | daniel-mar | 91 | self::$baseConfig->setValue($name, $value); |
261 | daniel-mar | 92 | } |
93 | } |
||
94 | } |
||
95 | } else { |
||
496 | daniel-mar | 96 | if (!is_dir(OIDplus::localpath().'setup')) { |
360 | daniel-mar | 97 | 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 | 98 | } else { |
280 | daniel-mar | 99 | if (self::$html) { |
496 | daniel-mar | 100 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
101 | header('Location:'.OIDplus::webpath().'setup/'); |
||
360 | daniel-mar | 102 | die(_L('Redirecting to setup...')); |
349 | daniel-mar | 103 | } else { |
104 | return self::$baseConfig; |
||
105 | } |
||
261 | daniel-mar | 106 | } else { |
107 | // This can be displayed in e.g. ajax.php |
||
360 | daniel-mar | 108 | throw new OIDplusConfigInitializationException(_L('File %1 is missing. Please run setup again.','userdata/baseconfig/config.inc.php')); |
261 | daniel-mar | 109 | } |
110 | } |
||
111 | } |
||
112 | |||
113 | // Check important config settings |
||
114 | |||
263 | daniel-mar | 115 | if (self::$baseConfig->getValue('CONFIG_VERSION') != 2.1) { |
503 | daniel-mar | 116 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
117 | throw new OIDplusConfigInitializationException(_L("The information located in %1 is outdated.",realpath($config_file))); |
||
118 | } |
||
261 | daniel-mar | 119 | } |
120 | |||
263 | daniel-mar | 121 | if (self::$baseConfig->getValue('SERVER_SECRET', '') === '') { |
503 | daniel-mar | 122 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
123 | throw new OIDplusConfigInitializationException(_L("You must set a value for SERVER_SECRET in %1 for the system to operate secure.",realpath($config_file))); |
||
124 | } |
||
261 | daniel-mar | 125 | } |
126 | } |
||
127 | |||
263 | daniel-mar | 128 | return self::$baseConfig; |
261 | daniel-mar | 129 | } |
130 | |||
263 | daniel-mar | 131 | private static $config = null; |
2 | daniel-mar | 132 | public static function config() { |
263 | daniel-mar | 133 | if ($first_init = is_null(self::$config)) { |
134 | self::$config = new OIDplusConfig(); |
||
2 | daniel-mar | 135 | } |
263 | daniel-mar | 136 | |
137 | if ($first_init) { |
||
138 | // These are important settings for base functionalities and therefore are not inside plugins |
||
139 | self::$config->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
140 | if (empty($value)) { |
||
360 | daniel-mar | 141 | throw new OIDplusException(_L('Please enter a value for the system title.')); |
263 | daniel-mar | 142 | } |
143 | }); |
||
144 | self::$config->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
145 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
360 | daniel-mar | 146 | throw new OIDplusException(_L('This is not a correct email address')); |
263 | daniel-mar | 147 | } |
148 | }); |
||
149 | self::$config->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
150 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
360 | daniel-mar | 151 | throw new OIDplusException(_L('This is not a correct email address')); |
263 | daniel-mar | 152 | } |
153 | }); |
||
154 | self::$config->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', OIDplusConfig::PROTECTION_READONLY, function($value) { |
||
155 | // Nothing here yet |
||
156 | }); |
||
157 | 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) { |
||
158 | # 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? |
||
159 | |||
160 | $ary = explode(';',$value); |
||
161 | $uniq_ary = array_unique($ary); |
||
162 | |||
163 | if (count($ary) != count($uniq_ary)) { |
||
360 | daniel-mar | 164 | throw new OIDplusException(_L('Please check your input. Some object types are double.')); |
263 | daniel-mar | 165 | } |
166 | |||
167 | foreach ($ary as $ot_check) { |
||
168 | $ns_found = false; |
||
169 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
||
170 | if ($ot::ns() == $ot_check) { |
||
171 | $ns_found = true; |
||
172 | break; |
||
173 | } |
||
174 | } |
||
175 | foreach (OIDplus::getDisabledObjectTypes() as $ot) { |
||
176 | if ($ot::ns() == $ot_check) { |
||
177 | $ns_found = true; |
||
178 | break; |
||
179 | } |
||
180 | } |
||
181 | if (!$ns_found) { |
||
360 | daniel-mar | 182 | throw new OIDplusException(_L('Please check your input. Namespace "%1" is not found',$ot_check)); |
263 | daniel-mar | 183 | } |
184 | } |
||
185 | }); |
||
186 | self::$config->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
||
187 | // Nothing here yet |
||
188 | }); |
||
189 | 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) { |
||
190 | // Nothing here yet |
||
191 | }); |
||
324 | daniel-mar | 192 | self::$config->prepareConfigKey('last_known_system_url', 'Last known System URL', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
193 | // Nothing here yet |
||
194 | }); |
||
412 | daniel-mar | 195 | self::$config->prepareConfigKey('last_known_version', 'Last known OIDplus Version', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
196 | // Nothing here yet |
||
197 | }); |
||
632 | daniel-mar | 198 | self::$config->prepareConfigKey('default_ra_auth_method', 'Default auth method used for generating password of RAs (must exist in plugins/auth/ or plugins/_thirdParty/vendorname/auth/)?', 'A3_bcrypt', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
453 | daniel-mar | 199 | $good = true; |
200 | if (strpos($value,'/') !== false) $good = false; |
||
201 | if (strpos($value,'\\') !== false) $good = false; |
||
202 | if (strpos($value,'..') !== false) $good = false; |
||
203 | if (!$good) { |
||
204 | throw new OIDplusException(_L('Invalid auth plugin folder name. Do only enter a folder name, not an absolute or relative path')); |
||
205 | } |
||
206 | |||
632 | daniel-mar | 207 | if (!is_dir(OIDplus::localpath().'plugins/auth/'.$value) && !rec_is_dir(OIDplus::localpath().'plugins/_thirdParty/'.'*'.'/auth/'.$value)) { |
208 | throw new OIDplusException(_L('The auth plugin "%1" does not exist in plugin directory %2 or %3',$value,'plugins/auth/','plugins/_thirdParty/vendorname/auth/')); |
||
453 | daniel-mar | 209 | } |
210 | }); |
||
263 | daniel-mar | 211 | } |
212 | |||
213 | return self::$config; |
||
2 | daniel-mar | 214 | } |
215 | |||
263 | daniel-mar | 216 | private static $gui = null; |
2 | daniel-mar | 217 | public static function gui() { |
263 | daniel-mar | 218 | if (is_null(self::$gui)) { |
219 | self::$gui = new OIDplusGui(); |
||
86 | daniel-mar | 220 | } |
263 | daniel-mar | 221 | return self::$gui; |
2 | daniel-mar | 222 | } |
223 | |||
263 | daniel-mar | 224 | private static $authUtils = null; |
2 | daniel-mar | 225 | public static function authUtils() { |
263 | daniel-mar | 226 | if (is_null(self::$authUtils)) { |
227 | self::$authUtils = new OIDplusAuthUtils(); |
||
86 | daniel-mar | 228 | } |
263 | daniel-mar | 229 | return self::$authUtils; |
2 | daniel-mar | 230 | } |
231 | |||
263 | daniel-mar | 232 | private static $mailUtils = null; |
250 | daniel-mar | 233 | public static function mailUtils() { |
263 | daniel-mar | 234 | if (is_null(self::$mailUtils)) { |
235 | self::$mailUtils = new OIDplusMailUtils(); |
||
250 | daniel-mar | 236 | } |
263 | daniel-mar | 237 | return self::$mailUtils; |
250 | daniel-mar | 238 | } |
239 | |||
557 | daniel-mar | 240 | private static $cookieUtils = null; |
241 | public static function cookieUtils() { |
||
242 | if (is_null(self::$cookieUtils)) { |
||
243 | self::$cookieUtils = new OIDplusCookieUtils(); |
||
244 | } |
||
245 | return self::$cookieUtils; |
||
246 | } |
||
247 | |||
263 | daniel-mar | 248 | private static $menuUtils = null; |
250 | daniel-mar | 249 | public static function menuUtils() { |
263 | daniel-mar | 250 | if (is_null(self::$menuUtils)) { |
251 | self::$menuUtils = new OIDplusMenuUtils(); |
||
250 | daniel-mar | 252 | } |
263 | daniel-mar | 253 | return self::$menuUtils; |
250 | daniel-mar | 254 | } |
255 | |||
263 | daniel-mar | 256 | private static $logger = null; |
115 | daniel-mar | 257 | public static function logger() { |
263 | daniel-mar | 258 | if (is_null(self::$logger)) { |
259 | self::$logger = new OIDplusLogger(); |
||
115 | daniel-mar | 260 | } |
263 | daniel-mar | 261 | return self::$logger; |
115 | daniel-mar | 262 | } |
263 | |||
274 | daniel-mar | 264 | # --- SQL slang plugin |
265 | |||
266 | private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) { |
||
267 | $name = $plugin::id(); |
||
591 | daniel-mar | 268 | if ($name === '') return false; |
274 | daniel-mar | 269 | |
449 | daniel-mar | 270 | if (isset(self::$sqlSlangPlugins[$name])) { |
451 | daniel-mar | 271 | $plugintype_hf = _L('SQL slang'); |
592 | daniel-mar | 272 | throw new OIDplusException(_L('Multiple %1 plugins use the ID %2', $plugintype_hf, $name)); |
449 | daniel-mar | 273 | } |
274 | |||
274 | daniel-mar | 275 | self::$sqlSlangPlugins[$name] = $plugin; |
276 | |||
277 | return true; |
||
278 | } |
||
279 | |||
280 | public static function getSqlSlangPlugins() { |
||
281 | return self::$sqlSlangPlugins; |
||
282 | } |
||
283 | |||
318 | daniel-mar | 284 | public static function getSqlSlangPlugin($id)/*: ?OIDplusSqlSlangPlugin*/ { |
285 | if (isset(self::$sqlSlangPlugins[$id])) { |
||
286 | return self::$sqlSlangPlugins[$id]; |
||
287 | } else { |
||
288 | return null; |
||
289 | } |
||
290 | } |
||
291 | |||
230 | daniel-mar | 292 | # --- Database plugin |
74 | daniel-mar | 293 | |
227 | daniel-mar | 294 | private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) { |
275 | daniel-mar | 295 | $name = $plugin::id(); |
591 | daniel-mar | 296 | if ($name === '') return false; |
150 | daniel-mar | 297 | |
449 | daniel-mar | 298 | if (isset(self::$dbPlugins[$name])) { |
299 | $plugintype_hf = _L('Database'); |
||
592 | daniel-mar | 300 | throw new OIDplusException(_L('Multiple %1 plugins use the ID %2', $plugintype_hf, $name)); |
449 | daniel-mar | 301 | } |
302 | |||
150 | daniel-mar | 303 | self::$dbPlugins[$name] = $plugin; |
304 | |||
305 | return true; |
||
306 | } |
||
307 | |||
308 | public static function getDatabasePlugins() { |
||
309 | return self::$dbPlugins; |
||
310 | } |
||
311 | |||
295 | daniel-mar | 312 | public static function getActiveDatabasePlugin() { |
261 | daniel-mar | 313 | if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN', '') === '') { |
360 | daniel-mar | 314 | throw new OIDplusConfigInitializationException(_L('No database plugin selected in config file')); |
260 | daniel-mar | 315 | } |
261 | daniel-mar | 316 | if (!isset(self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')])) { |
360 | daniel-mar | 317 | $db_plugin_name = OIDplus::baseConfig()->getValue('DATABASE_PLUGIN'); |
318 | throw new OIDplusConfigInitializationException(_L('Database plugin "%1" not found',$db_plugin_name)); |
||
227 | daniel-mar | 319 | } |
295 | daniel-mar | 320 | return self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')]; |
227 | daniel-mar | 321 | } |
322 | |||
295 | daniel-mar | 323 | private static $dbMainSession = null; |
324 | public static function db() { |
||
325 | if (is_null(self::$dbMainSession)) { |
||
326 | self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection(); |
||
327 | } |
||
328 | if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect(); |
||
329 | return self::$dbMainSession; |
||
330 | } |
||
331 | |||
332 | private static $dbIsolatedSession = null; |
||
333 | public static function dbIsolated() { |
||
334 | if (is_null(self::$dbIsolatedSession)) { |
||
335 | self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection(); |
||
336 | } |
||
337 | if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect(); |
||
338 | return self::$dbIsolatedSession; |
||
339 | } |
||
340 | |||
227 | daniel-mar | 341 | # --- Page plugin |
342 | |||
224 | daniel-mar | 343 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
281 | daniel-mar | 344 | self::$pagePlugins[] = $plugin; |
61 | daniel-mar | 345 | |
346 | return true; |
||
347 | } |
||
348 | |||
281 | daniel-mar | 349 | public static function getPagePlugins() { |
350 | return self::$pagePlugins; |
||
61 | daniel-mar | 351 | } |
352 | |||
227 | daniel-mar | 353 | # --- Auth plugin |
354 | |||
355 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
||
456 | daniel-mar | 356 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
357 | $password = generateRandomString(25); |
||
459 | daniel-mar | 358 | |
461 | daniel-mar | 359 | try { |
360 | $authInfo = $plugin->generate($password); |
||
361 | } catch (OIDplusException $e) { |
||
362 | // This can happen when the AuthKey or Salt is too long |
||
363 | throw new OIDplusException(_L('Auth plugin "%1" is erroneous: %2',basename($plugin->getPluginDirectory()),$e->getMessage())); |
||
364 | } |
||
459 | daniel-mar | 365 | $salt = $authInfo->getSalt(); |
461 | daniel-mar | 366 | $authKey = $authInfo->getAuthKey(); |
459 | daniel-mar | 367 | |
461 | daniel-mar | 368 | $authInfo_SaltDiff = clone $authInfo; |
369 | $authInfo_SaltDiff->setSalt(strrev($authInfo_SaltDiff->getSalt())); |
||
370 | |||
371 | $authInfo_AuthKeyDiff = clone $authInfo; |
||
372 | $authInfo_AuthKeyDiff->setAuthKey(strrev($authInfo_AuthKeyDiff->getAuthKey())); |
||
373 | |||
374 | if ((!$plugin->verify($authInfo,$password)) || |
||
375 | (!empty($salt) && $plugin->verify($authInfo_SaltDiff,$password)) || |
||
376 | ($plugin->verify($authInfo_AuthKeyDiff,$password)) || |
||
377 | ($plugin->verify($authInfo,$password.'x'))) { |
||
456 | daniel-mar | 378 | throw new OIDplusException(_L('Auth plugin "%1" is erroneous: Generate/Verify self test failed',basename($plugin->getPluginDirectory()))); |
379 | } |
||
453 | daniel-mar | 380 | } |
381 | |||
227 | daniel-mar | 382 | self::$authPlugins[] = $plugin; |
383 | return true; |
||
384 | } |
||
385 | |||
221 | daniel-mar | 386 | public static function getAuthPlugins() { |
387 | return self::$authPlugins; |
||
388 | } |
||
389 | |||
355 | daniel-mar | 390 | # --- Language plugin |
391 | |||
392 | private static function registerLanguagePlugin(OIDplusLanguagePlugin $plugin) { |
||
393 | self::$languagePlugins[] = $plugin; |
||
394 | return true; |
||
395 | } |
||
396 | |||
397 | public static function getLanguagePlugins() { |
||
398 | return self::$languagePlugins; |
||
399 | } |
||
400 | |||
449 | daniel-mar | 401 | # --- Design plugin |
402 | |||
403 | private static function registerDesignPlugin(OIDplusDesignPlugin $plugin) { |
||
404 | self::$designPlugins[] = $plugin; |
||
405 | return true; |
||
406 | } |
||
407 | |||
408 | public static function getDesignPlugins() { |
||
409 | return self::$designPlugins; |
||
410 | } |
||
411 | |||
289 | daniel-mar | 412 | # --- Logger plugin |
413 | |||
414 | private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) { |
||
415 | self::$loggerPlugins[] = $plugin; |
||
416 | return true; |
||
417 | } |
||
418 | |||
419 | public static function getLoggerPlugins() { |
||
420 | return self::$loggerPlugins; |
||
421 | } |
||
422 | |||
227 | daniel-mar | 423 | # --- Object type plugin |
424 | |||
425 | private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) { |
||
426 | self::$objectTypePlugins[] = $plugin; |
||
427 | |||
428 | $ot = $plugin::getObjectTypeClassName(); |
||
429 | self::registerObjectType($ot); |
||
430 | |||
431 | return true; |
||
432 | } |
||
433 | |||
224 | daniel-mar | 434 | private static function registerObjectType($ot) { |
66 | daniel-mar | 435 | $ns = $ot::ns(); |
436 | |||
360 | daniel-mar | 437 | if (empty($ns)) throw new OIDplusException(_L('Attention: Empty NS at %1',$ot)); |
66 | daniel-mar | 438 | |
439 | $ns_found = false; |
||
227 | daniel-mar | 440 | foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) { |
66 | daniel-mar | 441 | if ($test_ot::ns() == $ns) { |
442 | $ns_found = true; |
||
443 | break; |
||
444 | } |
||
445 | } |
||
446 | if ($ns_found) { |
||
360 | daniel-mar | 447 | throw new OIDplusException(_L('Attention: Two objectType plugins use the same namespace "%1"!',$ns)); |
66 | daniel-mar | 448 | } |
449 | |||
450 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
451 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 452 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 453 | |
454 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
455 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 456 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 457 | |
79 | daniel-mar | 458 | $do_enable = false; |
459 | if (in_array($ns, $enabled_ary)) { |
||
447 | daniel-mar | 460 | // If it is in the list of enabled object types, it is enabled (obviously) |
79 | daniel-mar | 461 | $do_enable = true; |
462 | } else { |
||
447 | daniel-mar | 463 | if (!OIDplus::config()->getValue('oobe_objects_done')) { |
464 | // If the OOBE wizard is NOT done, then just enable the "oid" object type by default |
||
79 | daniel-mar | 465 | $do_enable = $ns == 'oid'; |
466 | } else { |
||
447 | daniel-mar | 467 | // If the OOBE wizard was done (once), then |
468 | // we will enable all object types which were never initialized |
||
469 | // (i.e. a plugin folder was freshly added) |
||
79 | daniel-mar | 470 | $do_enable = !in_array($ns, $init_ary); |
471 | } |
||
472 | } |
||
473 | |||
474 | if ($do_enable) { |
||
227 | daniel-mar | 475 | self::$enabledObjectTypes[] = $ot; |
476 | usort(self::$enabledObjectTypes, function($a, $b) { |
||
66 | daniel-mar | 477 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
478 | $enabled_ary = explode(';', $enabled); |
||
479 | |||
480 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
481 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
482 | |||
483 | if ($idx_a == $idx_b) { |
||
484 | return 0; |
||
485 | } |
||
486 | return ($idx_a > $idx_b) ? +1 : -1; |
||
487 | }); |
||
74 | daniel-mar | 488 | } else { |
489 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 490 | } |
491 | |||
492 | if (!in_array($ns, $init_ary)) { |
||
493 | // Was never initialized before, so we add it to the list of enabled object types once |
||
494 | |||
79 | daniel-mar | 495 | if ($do_enable) { |
496 | $enabled_ary[] = $ns; |
||
497 | OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary)); |
||
498 | } |
||
66 | daniel-mar | 499 | |
500 | $init_ary[] = $ns; |
||
501 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
502 | } |
||
61 | daniel-mar | 503 | } |
504 | |||
227 | daniel-mar | 505 | public static function getObjectTypePlugins() { |
506 | return self::$objectTypePlugins; |
||
61 | daniel-mar | 507 | } |
508 | |||
227 | daniel-mar | 509 | public static function getObjectTypePluginsEnabled() { |
510 | $res = array(); |
||
511 | foreach (self::$objectTypePlugins as $plugin) { |
||
512 | $ot = $plugin::getObjectTypeClassName(); |
||
513 | if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin; |
||
514 | } |
||
515 | return $res; |
||
74 | daniel-mar | 516 | } |
517 | |||
227 | daniel-mar | 518 | public static function getObjectTypePluginsDisabled() { |
519 | $res = array(); |
||
520 | foreach (self::$objectTypePlugins as $plugin) { |
||
521 | $ot = $plugin::getObjectTypeClassName(); |
||
522 | if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin; |
||
74 | daniel-mar | 523 | } |
227 | daniel-mar | 524 | return $res; |
74 | daniel-mar | 525 | } |
526 | |||
227 | daniel-mar | 527 | public static function getEnabledObjectTypes() { |
528 | return self::$enabledObjectTypes; |
||
529 | } |
||
74 | daniel-mar | 530 | |
227 | daniel-mar | 531 | public static function getDisabledObjectTypes() { |
532 | return self::$disabledObjectTypes; |
||
533 | } |
||
74 | daniel-mar | 534 | |
277 | daniel-mar | 535 | # --- Plugin handling functions |
536 | |||
320 | daniel-mar | 537 | public static function getAllPlugins()/*: array*/ { |
538 | $res = array(); |
||
539 | $res = array_merge($res, self::$pagePlugins); |
||
540 | $res = array_merge($res, self::$authPlugins); |
||
541 | $res = array_merge($res, self::$loggerPlugins); |
||
542 | $res = array_merge($res, self::$objectTypePlugins); |
||
543 | $res = array_merge($res, self::$dbPlugins); |
||
544 | $res = array_merge($res, self::$sqlSlangPlugins); |
||
355 | daniel-mar | 545 | $res = array_merge($res, self::$languagePlugins); |
449 | daniel-mar | 546 | $res = array_merge($res, self::$designPlugins); |
320 | daniel-mar | 547 | return $res; |
548 | } |
||
549 | |||
321 | daniel-mar | 550 | public static function getPluginByOid($oid)/*: ?OIDplusPlugin*/ { |
320 | daniel-mar | 551 | $plugins = self::getAllPlugins(); |
321 | daniel-mar | 552 | foreach ($plugins as $plugin) { |
553 | if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) { |
||
554 | return $plugin; |
||
320 | daniel-mar | 555 | } |
556 | } |
||
557 | return null; |
||
558 | } |
||
559 | |||
380 | daniel-mar | 560 | public static function getPluginByClassName($classname)/*: ?OIDplusPlugin*/ { |
561 | $plugins = self::getAllPlugins(); |
||
562 | foreach ($plugins as $plugin) { |
||
563 | if (get_class($plugin) === $classname) { |
||
564 | return $plugin; |
||
565 | } |
||
566 | } |
||
567 | return null; |
||
277 | daniel-mar | 568 | } |
569 | |||
594 | daniel-mar | 570 | /** |
571 | * @return array<OIDplusPluginManifest>|array<string,array<string,OIDplusPluginManifest>> |
||
572 | */ |
||
307 | daniel-mar | 573 | public static function getAllPluginManifests($pluginFolderMask='*', $flat=true): array { |
277 | daniel-mar | 574 | $out = array(); |
279 | daniel-mar | 575 | // Note: glob() will sort by default, so we do not need a page priority attribute. |
576 | // So you just need to use a numeric plugin directory prefix (padded). |
||
632 | daniel-mar | 577 | $ary1 = glob(OIDplus::localpath().'plugins/'.$pluginFolderMask.'/'.'*'.'/manifest.xml'); |
578 | $ary2 = glob(OIDplus::localpath().'plugins/_thirdParty/'.'*'.'/'.$pluginFolderMask.'/'.'*'.'/manifest.xml'); |
||
579 | $ary = array_merge($ary1, $ary2); |
||
426 | daniel-mar | 580 | sort($ary); |
277 | daniel-mar | 581 | foreach ($ary as $ini) { |
582 | if (!file_exists($ini)) continue; |
||
583 | |||
307 | daniel-mar | 584 | $manifest = new OIDplusPluginManifest(); |
585 | $manifest->loadManifest($ini); |
||
277 | daniel-mar | 586 | |
473 | daniel-mar | 587 | $class_name = $manifest->getPhpMainClass(); |
588 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) { |
||
589 | continue; |
||
590 | } |
||
591 | |||
307 | daniel-mar | 592 | if ($flat) { |
593 | $out[] = $manifest; |
||
594 | } else { |
||
595 | $plugintype_folder = basename(dirname(dirname($ini))); |
||
596 | $pluginname_folder = basename(dirname($ini)); |
||
597 | |||
598 | if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array(); |
||
599 | if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array(); |
||
600 | $out[$plugintype_folder][$pluginname_folder] = $manifest; |
||
601 | } |
||
277 | daniel-mar | 602 | } |
603 | return $out; |
||
604 | } |
||
605 | |||
594 | daniel-mar | 606 | /** |
607 | * @return array<string> |
||
608 | */ |
||
277 | daniel-mar | 609 | public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array { |
610 | $out = array(); |
||
525 | daniel-mar | 611 | if (is_array($pluginDirName)) { |
612 | $ary = array(); |
||
613 | foreach ($pluginDirName as $pluginDirName_) { |
||
614 | $ary = array_merge($ary, self::getAllPluginManifests($pluginDirName_, false)); |
||
615 | } |
||
616 | } else { |
||
617 | $ary = self::getAllPluginManifests($pluginDirName, false); |
||
618 | } |
||
320 | daniel-mar | 619 | $known_plugin_oids = array(); |
456 | daniel-mar | 620 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
621 | $fake_feature = uuid_to_oid(gen_uuid()); |
||
588 | daniel-mar | 622 | } else { |
623 | $fake_feature = null; |
||
456 | daniel-mar | 624 | } |
277 | daniel-mar | 625 | foreach ($ary as $plugintype_folder => $bry) { |
438 | daniel-mar | 626 | foreach ($bry as $pluginname_folder => $manifest) { |
627 | $class_name = $manifest->getPhpMainClass(); |
||
628 | |||
629 | // Before we load the plugin, we want to make some checks to confirm |
||
630 | // that the plugin is working correctly. |
||
631 | |||
307 | daniel-mar | 632 | if (!$class_name) { |
438 | daniel-mar | 633 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Manifest does not declare a PHP main class')); |
277 | daniel-mar | 634 | } |
297 | daniel-mar | 635 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) { |
636 | continue; |
||
637 | } |
||
292 | daniel-mar | 638 | if (!class_exists($class_name)) { |
438 | daniel-mar | 639 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Manifest declares PHP main class as "%1", but it could not be found',$class_name)); |
292 | daniel-mar | 640 | } |
279 | daniel-mar | 641 | if (!is_subclass_of($class_name, $expectedPluginClass)) { |
438 | daniel-mar | 642 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Plugin main class "%1" is expected to be a subclass of "%2"',$class_name,$expectedPluginClass)); |
279 | daniel-mar | 643 | } |
438 | daniel-mar | 644 | if (($class_name!=$manifest->getTypeClass()) && (!is_subclass_of($class_name,$manifest->getTypeClass()))) { |
645 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Plugin main class "%1" is expected to be a subclass of "%2", according to type declared in manifest',$class_name,$manifest->getTypeClass())); |
||
308 | daniel-mar | 646 | } |
438 | daniel-mar | 647 | if (($manifest->getTypeClass()!=$expectedPluginClass) && (!is_subclass_of($manifest->getTypeClass(),$expectedPluginClass))) { |
648 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Class declared in manifest is "%1" does not fit expected class for this plugin type "%2"',$manifest->getTypeClass(),$expectedPluginClass)); |
||
308 | daniel-mar | 649 | } |
650 | |||
438 | daniel-mar | 651 | $plugin_oid = $manifest->getOid(); |
320 | daniel-mar | 652 | if (!$plugin_oid) { |
438 | daniel-mar | 653 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Does not have an OID')); |
320 | daniel-mar | 654 | } |
655 | if (!oid_valid_dotnotation($plugin_oid, false, false, 2)) { |
||
438 | daniel-mar | 656 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Plugin OID "%1" is invalid (needs to be valid dot-notation)',$plugin_oid)); |
320 | daniel-mar | 657 | } |
658 | if (isset($known_plugin_oids[$plugin_oid])) { |
||
438 | daniel-mar | 659 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('The OID "%1" is already used by the plugin "%2"',$plugin_oid,$known_plugin_oids[$plugin_oid])); |
320 | daniel-mar | 660 | } |
632 | daniel-mar | 661 | $full_plugin_dir = dirname($manifest->getManifestFile()); |
662 | $full_plugin_dir = substr($full_plugin_dir, strlen(OIDplus::localpath())); |
||
633 | daniel-mar | 663 | // { iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 37476 products(2) oidplus(5) v2(2) plugins(4) } |
632 | daniel-mar | 664 | if (str_starts_with($full_plugin_dir, 'plugins/_thirdParty/') != !str_starts_with($plugin_oid, '1.3.6.1.4.1.37476.2.5.2.4.')) { |
665 | throw new OIDplusException(_L('Plugin "%1/%2" is misplaced',$plugintype_folder,$pluginname_folder).': '._L('The plugin is in the wrong folder. The folder %1 can only be used by official ViaThinkSoft plugins, while the folder %2 can only be used for third-party plugins','plugins/','plugins/_thirdParty/"vendorname"/')); |
||
666 | } |
||
320 | daniel-mar | 667 | |
632 | daniel-mar | 668 | $known_plugin_oids[$plugin_oid] = $plugintype_folder.'/'.$pluginname_folder; |
669 | |||
438 | daniel-mar | 670 | $obj = new $class_name(); |
456 | daniel-mar | 671 | |
672 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
||
673 | if ($obj->implementsFeature($fake_feature)) { |
||
674 | // see https://devblogs.microsoft.com/oldnewthing/20040211-00/?p=40663 |
||
675 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('implementsFeature() always returns true')); |
||
676 | } |
||
438 | daniel-mar | 677 | } |
678 | |||
679 | // TODO: Maybe as additional plugin-test, we should also check if plugins are allowed to define CSS/JS (since only page plugins may have them!) |
||
680 | $tmp = array_merge( |
||
681 | $manifest->getJSFiles(), |
||
682 | $manifest->getCSSFiles(), |
||
683 | $manifest->getJSFilesSetup(), |
||
684 | $manifest->getCSSFilesSetup() |
||
685 | ); |
||
686 | foreach ($tmp as $file) { |
||
687 | if (!file_exists($file)) { |
||
688 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('File %1 was defined in manifest, but it is not existing',$file)); |
||
689 | } |
||
690 | } |
||
691 | |||
692 | // Now we can continue |
||
693 | |||
279 | daniel-mar | 694 | $out[] = $class_name; |
695 | if (!is_null($registerCallback)) { |
||
438 | daniel-mar | 696 | call_user_func($registerCallback, $obj); |
444 | daniel-mar | 697 | |
698 | // Alternative approaches: |
||
699 | //$registerCallback[0]::{$registerCallback[1]}($obj); |
||
700 | // or: |
||
701 | //forward_static_call($registerCallback, $obj); |
||
279 | daniel-mar | 702 | } |
277 | daniel-mar | 703 | } |
704 | |||
705 | } |
||
706 | return $out; |
||
707 | } |
||
708 | |||
227 | daniel-mar | 709 | # --- Initialization of OIDplus |
206 | daniel-mar | 710 | |
374 | daniel-mar | 711 | public static function init($html=true, $keepBaseConfig=true) { |
236 | daniel-mar | 712 | self::$html = $html; |
713 | |||
263 | daniel-mar | 714 | // Reset internal state, so we can re-init verything if required |
274 | daniel-mar | 715 | |
263 | daniel-mar | 716 | if (self::$old_config_format) { |
717 | // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice |
||
360 | daniel-mar | 718 | 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 | 719 | } |
274 | daniel-mar | 720 | |
263 | daniel-mar | 721 | self::$config = null; |
374 | daniel-mar | 722 | if (!$keepBaseConfig) self::$baseConfig = null; // for test cases we need to be able to control base config and setting values manually, so $keepBaseConfig needs to be true |
263 | daniel-mar | 723 | self::$gui = null; |
724 | self::$authUtils = null; |
||
725 | self::$mailUtils = null; |
||
726 | self::$menuUtils = null; |
||
727 | self::$logger = null; |
||
295 | daniel-mar | 728 | self::$dbMainSession = null; |
729 | self::$dbIsolatedSession = null; |
||
263 | daniel-mar | 730 | self::$pagePlugins = array(); |
731 | self::$authPlugins = array(); |
||
289 | daniel-mar | 732 | self::$loggerPlugins = array(); |
263 | daniel-mar | 733 | self::$objectTypePlugins = array(); |
734 | self::$enabledObjectTypes = array(); |
||
735 | self::$disabledObjectTypes = array(); |
||
736 | self::$dbPlugins = array(); |
||
274 | daniel-mar | 737 | self::$sqlSlangPlugins = array(); |
355 | daniel-mar | 738 | self::$languagePlugins = array(); |
449 | daniel-mar | 739 | self::$designPlugins = array(); |
263 | daniel-mar | 740 | self::$system_id_cache = null; |
741 | self::$sslAvailableCache = null; |
||
468 | daniel-mar | 742 | self::$translationArray = array(); |
74 | daniel-mar | 743 | |
263 | daniel-mar | 744 | // Continue... |
745 | |||
294 | daniel-mar | 746 | OIDplus::baseConfig(); // this loads the base configuration located in userdata/baseconfig/config.inc.php (once!) |
263 | daniel-mar | 747 | // You can do changes to the configuration afterwards using OIDplus::baseConfig()->... |
748 | |||
150 | daniel-mar | 749 | // Register database types (highest priority) |
750 | |||
274 | daniel-mar | 751 | // SQL slangs |
752 | |||
294 | daniel-mar | 753 | self::registerAllPlugins('sqlSlang', 'OIDplusSqlSlangPlugin', array('OIDplus','registerSqlSlangPlugin')); |
274 | daniel-mar | 754 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
755 | $plugin->init($html); |
||
756 | } |
||
757 | |||
758 | // Database providers |
||
759 | |||
277 | daniel-mar | 760 | self::registerAllPlugins('database', 'OIDplusDatabasePlugin', array('OIDplus','registerDatabasePlugin')); |
237 | daniel-mar | 761 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
762 | $plugin->init($html); |
||
763 | } |
||
764 | |||
42 | daniel-mar | 765 | // Do redirect stuff etc. |
74 | daniel-mar | 766 | |
230 | daniel-mar | 767 | self::isSslAvailable(); // This function does automatic redirects |
61 | daniel-mar | 768 | |
263 | daniel-mar | 769 | // Construct the configuration manager |
66 | daniel-mar | 770 | |
263 | daniel-mar | 771 | OIDplus::config(); // During the construction, various system settings are prepared if required |
66 | daniel-mar | 772 | |
74 | daniel-mar | 773 | // Initialize public / private keys |
774 | |||
227 | daniel-mar | 775 | OIDplus::getPkiStatus(true); |
74 | daniel-mar | 776 | |
237 | daniel-mar | 777 | // Register non-DB plugins |
74 | daniel-mar | 778 | |
525 | daniel-mar | 779 | self::registerAllPlugins(array('publicPages', 'raPages', 'adminPages'), 'OIDplusPagePlugin', array('OIDplus','registerPagePlugin')); |
277 | daniel-mar | 780 | self::registerAllPlugins('auth', 'OIDplusAuthPlugin', array('OIDplus','registerAuthPlugin')); |
289 | daniel-mar | 781 | self::registerAllPlugins('logger', 'OIDplusLoggerPlugin', array('OIDplus','registerLoggerPlugin')); |
277 | daniel-mar | 782 | self::registerAllPlugins('objectTypes', 'OIDplusObjectTypePlugin', array('OIDplus','registerObjectTypePlugin')); |
355 | daniel-mar | 783 | self::registerAllPlugins('language', 'OIDplusLanguagePlugin', array('OIDplus','registerLanguagePlugin')); |
449 | daniel-mar | 784 | self::registerAllPlugins('design', 'OIDplusDesignPlugin', array('OIDplus','registerDesignPlugin')); |
150 | daniel-mar | 785 | |
237 | daniel-mar | 786 | // Initialize non-DB plugins |
224 | daniel-mar | 787 | |
281 | daniel-mar | 788 | foreach (OIDplus::getPagePlugins() as $plugin) { |
74 | daniel-mar | 789 | $plugin->init($html); |
790 | } |
||
230 | daniel-mar | 791 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
792 | $plugin->init($html); |
||
793 | } |
||
289 | daniel-mar | 794 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
795 | $plugin->init($html); |
||
796 | } |
||
230 | daniel-mar | 797 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
798 | $plugin->init($html); |
||
799 | } |
||
355 | daniel-mar | 800 | foreach (OIDplus::getLanguagePlugins() as $plugin) { |
801 | $plugin->init($html); |
||
802 | } |
||
449 | daniel-mar | 803 | foreach (OIDplus::getDesignPlugins() as $plugin) { |
804 | $plugin->init($html); |
||
805 | } |
||
412 | daniel-mar | 806 | |
807 | // Initialize other stuff (i.e. things which require the logger!) |
||
808 | |||
809 | OIDplus::recognizeSystemUrl(); // Make sure "last_known_system_url" is set |
||
810 | OIDplus::recognizeVersion(); // Make sure "last_known_version" is set and a log entry is created |
||
2 | daniel-mar | 811 | } |
42 | daniel-mar | 812 | |
227 | daniel-mar | 813 | # --- System URL, System ID, PKI, and other functions |
814 | |||
412 | daniel-mar | 815 | private static function recognizeSystemUrl() { |
816 | try { |
||
496 | daniel-mar | 817 | $url = OIDplus::webpath(); |
412 | daniel-mar | 818 | OIDplus::config()->setValue('last_known_system_url', $url); |
819 | } catch (Exception $e) { |
||
820 | } |
||
821 | } |
||
497 | daniel-mar | 822 | |
496 | daniel-mar | 823 | private static function getExecutingScriptPathDepth() { |
824 | if (PHP_SAPI == 'cli') { |
||
825 | global $argv; |
||
826 | $test_dir = dirname(realpath($argv[0])); |
||
827 | } else { |
||
828 | if (!isset($_SERVER["SCRIPT_FILENAME"])) return false; |
||
829 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
830 | } |
||
831 | $test_dir = str_replace('\\', '/', $test_dir); |
||
832 | $steps_up = 0; |
||
833 | while (!file_exists($test_dir.'/oidplus.min.css.php')) { // We just assume that only the OIDplus base directory contains "oidplus.min.css.php" and not any subsequent directory! |
||
834 | $test_dir = dirname($test_dir); |
||
835 | $steps_up++; |
||
836 | if ($steps_up == 1000) return false; // to make sure there will never be an infinite loop |
||
837 | } |
||
838 | return $steps_up; |
||
839 | } |
||
632 | daniel-mar | 840 | |
580 | daniel-mar | 841 | public static function isSSL() { |
842 | return isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on'); |
||
843 | } |
||
412 | daniel-mar | 844 | |
496 | daniel-mar | 845 | private static function getSystemUrl($relative=false) { |
326 | daniel-mar | 846 | if (!$relative) { |
847 | $res = OIDplus::baseConfig()->getValue('EXPLICIT_ABSOLUTE_SYSTEM_URL', ''); |
||
848 | if ($res !== '') { |
||
494 | daniel-mar | 849 | return rtrim($res,'/').'/'; |
326 | daniel-mar | 850 | } |
495 | daniel-mar | 851 | if (PHP_SAPI == 'cli') { |
494 | daniel-mar | 852 | try { |
853 | return OIDplus::config()->getValue('last_known_system_url', false); |
||
854 | } catch (Exception $e) { |
||
855 | return false; |
||
856 | } |
||
857 | } |
||
326 | daniel-mar | 858 | } |
859 | |||
494 | daniel-mar | 860 | // First, try to find out how many levels we need to go up |
496 | daniel-mar | 861 | $steps_up = self::getExecutingScriptPathDepth(); |
862 | if ($steps_up === false) return false; |
||
227 | daniel-mar | 863 | |
496 | daniel-mar | 864 | // Now go up these amount of levels, based on SCRIPT_NAME/argv[0] |
495 | daniel-mar | 865 | if (PHP_SAPI == 'cli') { |
866 | if ($relative) { |
||
867 | return str_repeat('../',$steps_up); |
||
868 | } else { |
||
869 | return false; |
||
870 | } |
||
871 | } else { |
||
872 | $res = dirname($_SERVER['SCRIPT_NAME'].'index.php'); // This fake 'index.php' ensures that SCRIPT_NAME does not end with '/', which would make dirname() fail |
||
873 | for ($i=0; $i<$steps_up; $i++) { |
||
874 | $res = dirname($res); |
||
875 | } |
||
876 | $res = str_replace('\\', '/', $res); |
||
877 | if ($res == '/') $res = ''; |
||
878 | $res .= '/'; |
||
496 | daniel-mar | 879 | |
495 | daniel-mar | 880 | // Do we want to have an absolute URI? |
881 | if (!$relative) { |
||
580 | daniel-mar | 882 | $is_ssl = self::isSSL(); |
495 | daniel-mar | 883 | $protocol = $is_ssl ? 'https' : 'http'; // do not translate |
884 | $host = $_SERVER['HTTP_HOST']; // includes port if it is not 80/443 |
||
885 | $res = $protocol.'://'.$host.$res; |
||
886 | } |
||
496 | daniel-mar | 887 | |
495 | daniel-mar | 888 | return $res; |
227 | daniel-mar | 889 | } |
890 | } |
||
891 | |||
892 | private static $system_id_cache = null; |
||
893 | public static function getSystemId($oid=false) { |
||
894 | if (!is_null(self::$system_id_cache)) { |
||
895 | $out = self::$system_id_cache; |
||
896 | } else { |
||
897 | $out = false; |
||
898 | |||
899 | if (self::getPkiStatus(true)) { |
||
900 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
386 | daniel-mar | 901 | $m = array(); |
227 | daniel-mar | 902 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
903 | $out = smallhash(base64_decode($m[1])); |
||
904 | } |
||
905 | } |
||
906 | self::$system_id_cache = $out; |
||
907 | } |
||
350 | daniel-mar | 908 | if (!$out) return false; |
291 | daniel-mar | 909 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
227 | daniel-mar | 910 | } |
911 | |||
912 | public static function getPkiStatus($try_generate=true) { |
||
913 | if (!function_exists('openssl_pkey_new')) return false; |
||
914 | |||
915 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
916 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
917 | |||
918 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
263 | daniel-mar | 919 | $pkey_config = array( |
227 | daniel-mar | 920 | "digest_alg" => "sha512", |
921 | "private_key_bits" => 2048, |
||
922 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
923 | ); |
||
924 | |||
925 | // Create the private and public key |
||
263 | daniel-mar | 926 | $res = openssl_pkey_new($pkey_config); |
256 | daniel-mar | 927 | |
239 | daniel-mar | 928 | if (!$res) return false; |
227 | daniel-mar | 929 | |
930 | // Extract the private key from $res to $privKey |
||
931 | openssl_pkey_export($res, $privKey); |
||
932 | |||
933 | // Extract the public key from $res to $pubKey |
||
934 | $pubKey = openssl_pkey_get_details($res)["key"]; |
||
935 | |||
239 | daniel-mar | 936 | // Log |
288 | daniel-mar | 937 | OIDplus::logger()->log("[INFO]A!", "Generating new SystemID using a new key pair"); |
239 | daniel-mar | 938 | |
227 | daniel-mar | 939 | // Save the key pair to database |
940 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
941 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
942 | |||
943 | // Log the new system ID |
||
386 | daniel-mar | 944 | $m = array(); |
227 | daniel-mar | 945 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
946 | $system_id = smallhash(base64_decode($m[1])); |
||
288 | daniel-mar | 947 | OIDplus::logger()->log("[INFO]A!", "Your SystemID is now $system_id"); |
227 | daniel-mar | 948 | } |
949 | } |
||
950 | |||
951 | return verify_private_public_key($privKey, $pubKey); |
||
952 | } |
||
953 | |||
170 | daniel-mar | 954 | public static function getInstallType() { |
486 | daniel-mar | 955 | $counter = 0; |
956 | |||
591 | daniel-mar | 957 | if ($version_file_exists = file_exists(OIDplus::localpath().'oidplus_version.txt')) { |
486 | daniel-mar | 958 | $counter++; |
591 | daniel-mar | 959 | } |
960 | if ($svn_dir_exists = is_dir(OIDplus::localpath().'.svn') || |
||
961 | is_dir(OIDplus::localpath().'../.svn')) { // in case we checked out the root instead of the "trunk" |
||
486 | daniel-mar | 962 | $counter++; |
591 | daniel-mar | 963 | } |
964 | if ($git_dir_exists = is_dir(OIDplus::localpath().'.git')) { |
||
486 | daniel-mar | 965 | $counter++; |
591 | daniel-mar | 966 | } |
486 | daniel-mar | 967 | |
968 | if ($counter === 0) { |
||
360 | daniel-mar | 969 | return 'unknown'; // do not translate |
170 | daniel-mar | 970 | } |
591 | daniel-mar | 971 | else if ($counter > 1) { |
360 | daniel-mar | 972 | return 'ambigous'; // do not translate |
170 | daniel-mar | 973 | } |
591 | daniel-mar | 974 | else if ($svn_dir_exists) { |
360 | daniel-mar | 975 | return 'svn-wc'; // do not translate |
170 | daniel-mar | 976 | } |
591 | daniel-mar | 977 | else if ($git_dir_exists) { |
486 | daniel-mar | 978 | return 'git-wc'; // do not translate |
979 | } |
||
591 | daniel-mar | 980 | else if ($version_file_exists) { |
360 | daniel-mar | 981 | return 'svn-snapshot'; // do not translate |
170 | daniel-mar | 982 | } |
983 | } |
||
984 | |||
412 | daniel-mar | 985 | private static function recognizeVersion() { |
986 | try { |
||
987 | $ver_prev = OIDplus::config()->getValue("last_known_version"); |
||
988 | $ver_now = OIDplus::getVersion(); |
||
989 | if (($ver_now != '') && ($ver_prev != '') && ($ver_now != $ver_prev)) { |
||
455 | daniel-mar | 990 | // TODO: Problem: When the system was updated using SVN, then the IP address of the next random visitor of the website is logged! |
412 | daniel-mar | 991 | OIDplus::logger()->log("[INFO]A!", "System version changed from '$ver_prev' to '$ver_now'"); |
992 | } |
||
993 | OIDplus::config()->setValue("last_known_version", $ver_now); |
||
994 | } catch (Exception $e) { |
||
995 | } |
||
996 | } |
||
997 | |||
111 | daniel-mar | 998 | public static function getVersion() { |
412 | daniel-mar | 999 | static $cachedVersion = null; |
1000 | if (!is_null($cachedVersion)) { |
||
1001 | return $cachedVersion; |
||
1002 | } |
||
1003 | |||
486 | daniel-mar | 1004 | $installType = OIDplus::getInstallType(); |
1005 | |||
1006 | if ($installType === 'svn-wc') { |
||
496 | daniel-mar | 1007 | $ver = get_svn_revision(OIDplus::localpath()); |
486 | daniel-mar | 1008 | if ($ver) |
1009 | return ($cachedVersion = 'svn-'.$ver); |
||
558 | daniel-mar | 1010 | $ver = get_svn_revision(OIDplus::localpath().'../'); // in case we checked out the root instead of the "trunk" |
1011 | if ($ver) |
||
1012 | return ($cachedVersion = 'svn-'.$ver); |
||
111 | daniel-mar | 1013 | } |
162 | daniel-mar | 1014 | |
486 | daniel-mar | 1015 | if ($installType === 'git-wc') { |
496 | daniel-mar | 1016 | $ver = get_gitsvn_revision(OIDplus::localpath()); |
486 | daniel-mar | 1017 | if ($ver) |
1018 | return ($cachedVersion = 'svn-'.$ver); |
||
162 | daniel-mar | 1019 | } |
1020 | |||
486 | daniel-mar | 1021 | if ($installType === 'svn-snapshot') { |
496 | daniel-mar | 1022 | $cont = file_get_contents(OIDplus::localpath().'oidplus_version.txt'); |
386 | daniel-mar | 1023 | $m = array(); |
360 | daniel-mar | 1024 | if (preg_match('@Revision (\d+)@', $cont, $m)) // do not translate |
412 | daniel-mar | 1025 | return ($cachedVersion = 'svn-'.$m[1]); // do not translate |
162 | daniel-mar | 1026 | } |
1027 | |||
486 | daniel-mar | 1028 | return ($cachedVersion = false); // version ambigous or unknown |
111 | daniel-mar | 1029 | } |
1030 | |||
230 | daniel-mar | 1031 | private static $sslAvailableCache = null; |
1032 | public static function isSslAvailable() { |
||
1033 | if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache; |
||
256 | daniel-mar | 1034 | |
495 | daniel-mar | 1035 | if (PHP_SAPI == 'cli') { |
230 | daniel-mar | 1036 | self::$sslAvailableCache = false; |
1037 | return false; |
||
1038 | } |
||
1039 | |||
49 | daniel-mar | 1040 | $timeout = 2; |
580 | daniel-mar | 1041 | $already_ssl = self::isSSL(); |
80 | daniel-mar | 1042 | $ssl_port = 443; |
42 | daniel-mar | 1043 | |
261 | daniel-mar | 1044 | $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', 2/*auto*/); |
1045 | |||
1046 | if ($mode == 0) { |
||
80 | daniel-mar | 1047 | // No SSL available |
230 | daniel-mar | 1048 | self::$sslAvailableCache = $already_ssl; |
80 | daniel-mar | 1049 | return $already_ssl; |
1050 | } |
||
1051 | |||
261 | daniel-mar | 1052 | if ($mode == 1) { |
80 | daniel-mar | 1053 | // Force SSL |
1054 | if ($already_ssl) { |
||
230 | daniel-mar | 1055 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1056 | return true; |
42 | daniel-mar | 1057 | } else { |
80 | daniel-mar | 1058 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
1059 | header('Location:'.$location); |
||
360 | daniel-mar | 1060 | die(_L('Redirecting to HTTPS...')); |
591 | daniel-mar | 1061 | #self::$sslAvailableCache = true; |
1062 | #return true; |
||
80 | daniel-mar | 1063 | } |
1064 | } |
||
1065 | |||
261 | daniel-mar | 1066 | if ($mode == 2) { |
80 | daniel-mar | 1067 | // Automatic SSL detection |
1068 | |||
1069 | if ($already_ssl) { |
||
1070 | // we are already on HTTPS |
||
557 | daniel-mar | 1071 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '1', 0, false); |
230 | daniel-mar | 1072 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1073 | return true; |
1074 | } else { |
||
1075 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
1076 | // We already had the HTTPS detection done before. |
||
1077 | if ($_COOKIE['SSL_CHECK']) { |
||
1078 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
1079 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
1080 | header('Location:'.$location); |
||
360 | daniel-mar | 1081 | die(_L('Redirecting to HTTPS...')); |
591 | daniel-mar | 1082 | #self::$sslAvailableCache = true; |
1083 | #return true; |
||
80 | daniel-mar | 1084 | } else { |
1085 | // No HTTPS available. Do nothing. |
||
230 | daniel-mar | 1086 | self::$sslAvailableCache = false; |
80 | daniel-mar | 1087 | return false; |
1088 | } |
||
49 | daniel-mar | 1089 | } else { |
80 | daniel-mar | 1090 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
386 | daniel-mar | 1091 | $errno = -1; |
1092 | $errstr = ''; |
||
80 | daniel-mar | 1093 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
1094 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
557 | daniel-mar | 1095 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '1', 0, false); |
80 | daniel-mar | 1096 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
1097 | header('Location:'.$location); |
||
360 | daniel-mar | 1098 | die(_L('Redirecting to HTTPS...')); |
591 | daniel-mar | 1099 | #self::$sslAvailableCache = true; |
1100 | #return true; |
||
80 | daniel-mar | 1101 | } else { |
1102 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
557 | daniel-mar | 1103 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '0', 0, false); |
230 | daniel-mar | 1104 | self::$sslAvailableCache = false; |
80 | daniel-mar | 1105 | return false; |
1106 | } |
||
49 | daniel-mar | 1107 | } |
42 | daniel-mar | 1108 | } |
1109 | } |
||
1110 | } |
||
497 | daniel-mar | 1111 | |
496 | daniel-mar | 1112 | /** |
1113 | * Gets a local path pointing to a resource |
||
1114 | * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory |
||
1115 | * @param boolean $relative If true, the returning path is relative to the currently executed PHP file (not the CLI working directory) |
||
590 | daniel-mar | 1116 | * @return string|false The local path, with guaranteed trailing path delimiter for directories |
496 | daniel-mar | 1117 | */ |
1118 | public static function localpath($target=null, $relative=false) { |
||
1119 | if (is_null($target)) { |
||
1120 | $target = __DIR__.'/../../'; |
||
1121 | } |
||
241 | daniel-mar | 1122 | |
496 | daniel-mar | 1123 | if ($relative) { |
1124 | // First, try to find out how many levels we need to go up |
||
1125 | $steps_up = self::getExecutingScriptPathDepth(); |
||
1126 | if ($steps_up === false) return false; |
||
497 | daniel-mar | 1127 | |
496 | daniel-mar | 1128 | // Virtually go back from the executing PHP script to the OIDplus base path |
1129 | $res = str_repeat('../',$steps_up); |
||
497 | daniel-mar | 1130 | |
496 | daniel-mar | 1131 | // Then go to the desired location |
1132 | $basedir = realpath(__DIR__.'/../../'); |
||
1133 | $target = realpath($target); |
||
500 | daniel-mar | 1134 | if ($target === false) return false; |
496 | daniel-mar | 1135 | $res .= substr($target, strlen($basedir)+1); |
1136 | $res = rtrim($res,'/'); // avoid '..//' for localpath(null,true) |
||
1137 | } else { |
||
1138 | $res = realpath($target); |
||
241 | daniel-mar | 1139 | } |
497 | daniel-mar | 1140 | |
591 | daniel-mar | 1141 | if (is_dir($target)) $res .= DIRECTORY_SEPARATOR; |
497 | daniel-mar | 1142 | |
496 | daniel-mar | 1143 | return $res; |
241 | daniel-mar | 1144 | } |
355 | daniel-mar | 1145 | |
496 | daniel-mar | 1146 | /** |
1147 | * Gets a URL pointing to a resource |
||
1148 | * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory |
||
1149 | * @param boolean $relative If true, the returning path is relative to the currently executed PHP script (i.e. index.php , not the plugin PHP script!) |
||
590 | daniel-mar | 1150 | * @return string|false The URL, with guaranteed trailing path delimiter for directories |
496 | daniel-mar | 1151 | */ |
1152 | public static function webpath($target=null, $relative=false) { |
||
1153 | $res = self::getSystemUrl($relative); // Note: already contains a trailing path delimiter |
||
1154 | if (!$res) return false; |
||
497 | daniel-mar | 1155 | |
496 | daniel-mar | 1156 | if (!is_null($target)) { |
1157 | $basedir = realpath(__DIR__.'/../../'); |
||
1158 | $target = realpath($target); |
||
500 | daniel-mar | 1159 | if ($target === false) return false; |
496 | daniel-mar | 1160 | $tmp = substr($target, strlen($basedir)+1); |
500 | daniel-mar | 1161 | $res .= str_replace(DIRECTORY_SEPARATOR,'/',$tmp); // remove OS specific path delimiters introduced by realpath() |
497 | daniel-mar | 1162 | if (is_dir($target)) $res .= '/'; |
496 | daniel-mar | 1163 | } |
497 | daniel-mar | 1164 | |
496 | daniel-mar | 1165 | return $res; |
1166 | } |
||
497 | daniel-mar | 1167 | |
360 | daniel-mar | 1168 | public static function getAvailableLangs() { |
1169 | $langs = array(); |
||
1170 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
||
389 | daniel-mar | 1171 | $code = $pluginManifest->getLanguageCode(); |
360 | daniel-mar | 1172 | $langs[] = $code; |
1173 | } |
||
1174 | return $langs; |
||
1175 | } |
||
1176 | |||
355 | daniel-mar | 1177 | public static function getCurrentLang() { |
360 | daniel-mar | 1178 | if (isset($_GET['lang'])) { |
1179 | $lang = $_GET['lang']; |
||
1180 | } else if (isset($_POST['lang'])) { |
||
1181 | $lang = $_POST['lang']; |
||
1182 | } else if (isset($_COOKIE['LANGUAGE'])) { |
||
1183 | $lang = $_COOKIE['LANGUAGE']; |
||
1184 | } else { |
||
1185 | $lang = self::DEFAULT_LANGUAGE; |
||
1186 | } |
||
1187 | $lang = substr(preg_replace('@[^a-z]@ismU', '', $lang),0,4); // sanitize |
||
355 | daniel-mar | 1188 | return $lang; |
1189 | } |
||
1190 | |||
362 | daniel-mar | 1191 | public static function handleLangArgument() { |
1192 | if (isset($_GET['lang'])) { |
||
1193 | // The "?lang=" argument is only for NoScript-Browsers/SearchEngines |
||
1194 | // In case someone who has JavaScript clicks a ?lang= link, they should get |
||
1195 | // the page in that language, but the cookie must be set, otherwise |
||
1196 | // the menu and other stuff would be in their cookie-based-language and not the |
||
1197 | // argument-based-language. |
||
557 | daniel-mar | 1198 | OIDplus::cookieUtils()->setcookie('LANGUAGE', $_GET['lang'], 0, true/*HttpOnly off, because JavaScript also needs translation*/); |
362 | daniel-mar | 1199 | } else if (isset($_POST['lang'])) { |
557 | daniel-mar | 1200 | OIDplus::cookieUtils()->setcookie('LANGUAGE', $_POST['lang'], 0, true/*HttpOnly off, because JavaScript also needs translation*/); |
362 | daniel-mar | 1201 | } |
1202 | } |
||
1203 | |||
468 | daniel-mar | 1204 | private static $translationArray = array(); |
469 | daniel-mar | 1205 | protected static function getTranslationFileContents($translation_file) { |
1206 | // First, try the cache |
||
481 | daniel-mar | 1207 | $cache_file = __DIR__ . '/../../userdata/cache/translation_'.md5($translation_file).'.ser'; |
469 | daniel-mar | 1208 | if (file_exists($cache_file) && (filemtime($cache_file) == filemtime($translation_file))) { |
1209 | $cac = @unserialize(file_get_contents($cache_file)); |
||
1210 | if ($cac) return $cac; |
||
1211 | } |
||
481 | daniel-mar | 1212 | |
469 | daniel-mar | 1213 | // If not successful, then load the XML file |
1214 | $xml = @simplexml_load_string(file_get_contents($translation_file)); |
||
1215 | if (!$xml) return array(); // if there is an UTF-8 or parsing error, don't output any errors, otherwise the JavaScript is corrupt and the page won't render correctly |
||
1216 | $cac = array(); |
||
1217 | foreach ($xml->message as $msg) { |
||
1218 | $src = trim($msg->source->__toString()); |
||
1219 | $dst = trim($msg->target->__toString()); |
||
1220 | $cac[$src] = $dst; |
||
1221 | } |
||
1222 | @file_put_contents($cache_file,serialize($cac)); |
||
1223 | @touch($cache_file,filemtime($translation_file)); |
||
1224 | return $cac; |
||
1225 | } |
||
468 | daniel-mar | 1226 | public static function getTranslationArray($requested_lang='*') { |
362 | daniel-mar | 1227 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
389 | daniel-mar | 1228 | $lang = $pluginManifest->getLanguageCode(); |
362 | daniel-mar | 1229 | if (strpos($lang,'/') !== false) continue; // just to be sure |
1230 | if (strpos($lang,'\\') !== false) continue; // just to be sure |
||
1231 | if (strpos($lang,'..') !== false) continue; // just to be sure |
||
401 | daniel-mar | 1232 | |
468 | daniel-mar | 1233 | if (($requested_lang != '*') && ($lang != $requested_lang)) continue; |
401 | daniel-mar | 1234 | |
468 | daniel-mar | 1235 | if (!isset(self::$translationArray[$lang])) { |
1236 | self::$translationArray[$lang] = array(); |
||
1237 | |||
1238 | $wildcard = $pluginManifest->getLanguageMessages(); |
||
1239 | if (strpos($wildcard,'/') !== false) continue; // just to be sure |
||
1240 | if (strpos($wildcard,'\\') !== false) continue; // just to be sure |
||
1241 | if (strpos($wildcard,'..') !== false) continue; // just to be sure |
||
1242 | |||
632 | daniel-mar | 1243 | $translation_files1 = glob(__DIR__.'/../../plugins/language/'.$lang.'/'.$wildcard); |
1244 | $translation_files2 = glob(__DIR__.'/../../plugins/_thirdParty/'.'*'.'/language/'.$lang.'/'.$wildcard); |
||
1245 | $translation_files = array_merge($translation_files1, $translation_files2); |
||
468 | daniel-mar | 1246 | sort($translation_files); |
1247 | foreach ($translation_files as $translation_file) { |
||
1248 | if (!file_exists($translation_file)) continue; |
||
469 | daniel-mar | 1249 | $cac = self::getTranslationFileContents($translation_file); |
1250 | foreach ($cac as $src => $dst) { |
||
1251 | self::$translationArray[$lang][$src] = $dst; |
||
468 | daniel-mar | 1252 | } |
401 | daniel-mar | 1253 | } |
362 | daniel-mar | 1254 | } |
1255 | } |
||
468 | daniel-mar | 1256 | return self::$translationArray; |
362 | daniel-mar | 1257 | } |
1258 | |||
374 | daniel-mar | 1259 | } |