Configuration.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. class Configuration {
  3. public static $VERSION = "2018-06-10";
  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. // configuration checks
  21. if(ini_get('allow_url_fopen') !== '1')
  22. die('"allow_url_fopen" is not set to "1". Please check "php.ini');
  23. // Check cache folder permissions (write permissions required)
  24. if(!is_writable(CACHE_DIR))
  25. die('RSS-Bridge does not have write permissions for ' . CACHE_DIR . '!');
  26. // Check whitelist file permissions (only in DEBUG mode)
  27. if(!file_exists(WHITELIST_FILE) && !is_writable(dirname(WHITELIST_FILE)))
  28. die('RSS-Bridge does not have write permissions for ' . WHITELIST_FILE . '!');
  29. }
  30. public static function loadConfiguration() {
  31. if(!file_exists('config.default.ini.php'))
  32. die('The default configuration file "config.default.ini.php" is missing!');
  33. Configuration::$config = parse_ini_file('config.default.ini.php', true, INI_SCANNER_TYPED);
  34. if(!Configuration::$config)
  35. die('Error parsing config.default.ini.php');
  36. if(file_exists('config.ini.php')) {
  37. // Replace default configuration with custom settings
  38. foreach(parse_ini_file('config.ini.php', true, INI_SCANNER_TYPED) as $header => $section) {
  39. foreach($section as $key => $value) {
  40. // Skip unknown sections and keys
  41. if(array_key_exists($header, Configuration::$config) && array_key_exists($key, Configuration::$config[$header])) {
  42. Configuration::$config[$header][$key] = $value;
  43. }
  44. }
  45. }
  46. }
  47. if(!is_string(self::getConfig('proxy', 'url')))
  48. die('Parameter [proxy] => "url" is not a valid string! Please check "config.ini.php"!');
  49. if(!empty(self::getConfig('proxy', 'url')))
  50. define('PROXY_URL', self::getConfig('proxy', 'url'));
  51. if(!is_bool(self::getConfig('proxy', 'by_bridge')))
  52. die('Parameter [proxy] => "by_bridge" is not a valid Boolean! Please check "config.ini.php"!');
  53. define('PROXY_BYBRIDGE', self::getConfig('proxy', 'by_bridge'));
  54. if(!is_string(self::getConfig('proxy', 'name')))
  55. die('Parameter [proxy] => "name" is not a valid string! Please check "config.ini.php"!');
  56. define('PROXY_NAME', self::getConfig('proxy', 'name'));
  57. if(!is_bool(self::getConfig('cache', 'custom_timeout')))
  58. die('Parameter [cache] => "custom_timeout" is not a valid Boolean! Please check "config.ini.php"!');
  59. define('CUSTOM_CACHE_TIMEOUT', self::getConfig('cache', 'custom_timeout'));
  60. if(!is_bool(self::getConfig('authentication', 'enable')))
  61. die('Parameter [authentication] => "enable" is not a valid Boolean! Please check "config.ini.php"!');
  62. if(!is_string(self::getConfig('authentication', 'username')))
  63. die('Parameter [authentication] => "username" is not a valid string! Please check "config.ini.php"!');
  64. if(!is_string(self::getConfig('authentication', 'password')))
  65. die('Parameter [authentication] => "password" is not a valid string! Please check "config.ini.php"!');
  66. }
  67. public static function getConfig($category, $key) {
  68. if(array_key_exists($category, self::$config) && array_key_exists($key, self::$config[$category])) {
  69. return self::$config[$category][$key];
  70. }
  71. return null;
  72. }
  73. public static function getVersion() {
  74. $headFile = '.git/HEAD';
  75. if(file_exists($headFile)) {
  76. $revisionHashFile = '.git/' . substr(file_get_contents($headFile), 5, -1);
  77. $branchName = explode('/', $revisionHashFile)[3];
  78. if(file_exists($revisionHashFile)) {
  79. return 'git.' . $branchName . '.' . substr(file_get_contents($revisionHashFile), 0, 7);
  80. }
  81. }
  82. return Configuration::$VERSION;
  83. }
  84. }