Configuration.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. class Configuration {
  3. public static $VERSION = '2018-08-07';
  4. public static $config = null;
  5. public static function verifyInstallation() {
  6. // Check PHP version
  7. if(version_compare(PHP_VERSION, PHP_VERSION_REQUIRED) === -1)
  8. die('RSS-Bridge requires at least PHP version ' . PHP_VERSION_REQUIRED . '!');
  9. // extensions check
  10. if(!extension_loaded('openssl'))
  11. die('"openssl" extension not loaded. Please check "php.ini"');
  12. if(!extension_loaded('libxml'))
  13. die('"libxml" extension not loaded. Please check "php.ini"');
  14. if(!extension_loaded('mbstring'))
  15. die('"mbstring" extension not loaded. Please check "php.ini"');
  16. if(!extension_loaded('simplexml'))
  17. die('"simplexml" extension not loaded. Please check "php.ini"');
  18. if(!extension_loaded('curl'))
  19. die('"curl" extension not loaded. Please check "php.ini"');
  20. // Check cache folder permissions (write permissions required)
  21. if(!is_writable(CACHE_DIR))
  22. die('RSS-Bridge does not have write permissions for ' . CACHE_DIR . '!');
  23. // Check whitelist file permissions (only in DEBUG mode)
  24. if(!file_exists(WHITELIST_FILE) && !is_writable(dirname(WHITELIST_FILE)))
  25. die('RSS-Bridge does not have write permissions for ' . WHITELIST_FILE . '!');
  26. }
  27. public static function loadConfiguration() {
  28. if(!file_exists('config.default.ini.php'))
  29. die('The default configuration file "config.default.ini.php" is missing!');
  30. Configuration::$config = parse_ini_file('config.default.ini.php', true, INI_SCANNER_TYPED);
  31. if(!Configuration::$config)
  32. die('Error parsing config.default.ini.php');
  33. if(file_exists('config.ini.php')) {
  34. // Replace default configuration with custom settings
  35. foreach(parse_ini_file('config.ini.php', true, INI_SCANNER_TYPED) as $header => $section) {
  36. foreach($section as $key => $value) {
  37. // Skip unknown sections and keys
  38. if(array_key_exists($header, Configuration::$config) && array_key_exists($key, Configuration::$config[$header])) {
  39. Configuration::$config[$header][$key] = $value;
  40. }
  41. }
  42. }
  43. }
  44. if(!is_string(self::getConfig('proxy', 'url')))
  45. die('Parameter [proxy] => "url" is not a valid string! Please check "config.ini.php"!');
  46. if(!empty(self::getConfig('proxy', 'url')))
  47. define('PROXY_URL', self::getConfig('proxy', 'url'));
  48. if(!is_bool(self::getConfig('proxy', 'by_bridge')))
  49. die('Parameter [proxy] => "by_bridge" is not a valid Boolean! Please check "config.ini.php"!');
  50. define('PROXY_BYBRIDGE', self::getConfig('proxy', 'by_bridge'));
  51. if(!is_string(self::getConfig('proxy', 'name')))
  52. die('Parameter [proxy] => "name" is not a valid string! Please check "config.ini.php"!');
  53. define('PROXY_NAME', self::getConfig('proxy', 'name'));
  54. if(!is_bool(self::getConfig('cache', 'custom_timeout')))
  55. die('Parameter [cache] => "custom_timeout" is not a valid Boolean! Please check "config.ini.php"!');
  56. define('CUSTOM_CACHE_TIMEOUT', self::getConfig('cache', 'custom_timeout'));
  57. if(!is_bool(self::getConfig('authentication', 'enable')))
  58. die('Parameter [authentication] => "enable" is not a valid Boolean! Please check "config.ini.php"!');
  59. if(!is_string(self::getConfig('authentication', 'username')))
  60. die('Parameter [authentication] => "username" is not a valid string! Please check "config.ini.php"!');
  61. if(!is_string(self::getConfig('authentication', 'password')))
  62. die('Parameter [authentication] => "password" is not a valid string! Please check "config.ini.php"!');
  63. }
  64. public static function getConfig($category, $key) {
  65. if(array_key_exists($category, self::$config) && array_key_exists($key, self::$config[$category])) {
  66. return self::$config[$category][$key];
  67. }
  68. return null;
  69. }
  70. public static function getVersion() {
  71. $headFile = '.git/HEAD';
  72. if(file_exists($headFile)) {
  73. $revisionHashFile = '.git/' . substr(file_get_contents($headFile), 5, -1);
  74. $branchName = explode('/', $revisionHashFile)[3];
  75. if(file_exists($revisionHashFile)) {
  76. return 'git.' . $branchName . '.' . substr(file_get_contents($revisionHashFile), 0, 7);
  77. }
  78. }
  79. return Configuration::$VERSION;
  80. }
  81. }