KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.2KB

  1. <?php
  2. if (!defined("INCLUDES_DIR")) define("INCLUDES_DIR", dirname(__FILE__));
  3. /**
  4. * Class: Config
  5. * Holds all of the configuration variables for the entire site, as well as Module settings.
  6. */
  7. class Config {
  8. # Variable: $yaml
  9. # Holds all of the YAML settings as a $key => $val array.
  10. private $yaml = array();
  11. /**
  12. * Function: __construct
  13. * Loads the configuration YAML file.
  14. */
  15. private function __construct() {
  16. if (!file_exists(INCLUDES_DIR."/config.yaml.php"))
  17. return false;
  18. $contents = str_replace("<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n",
  19. "",
  20. file_get_contents(INCLUDES_DIR."/config.yaml.php"));
  21. $this->yaml = YAML::load($contents);
  22. $arrays = array("enabled_modules", "enabled_feathers", "routes");
  23. foreach ($this->yaml as $setting => $value)
  24. if (in_array($setting, $arrays) and empty($value))
  25. $this->$setting = array();
  26. elseif (!is_int($setting))
  27. $this->$setting = (is_string($value)) ? stripslashes($value) : $value ;
  28. fallback($this->url, $this->chyrp_url);
  29. }
  30. /**
  31. * Function: set
  32. * Adds or replaces a configuration setting with the given value.
  33. *
  34. * Parameters:
  35. * $setting - The setting name.
  36. * $value - The value.
  37. * $overwrite - If the setting exists and is the same value, should it be overwritten?
  38. */
  39. public function set($setting, $value, $overwrite = true) {
  40. if (isset($this->$setting) and $this->$setting == $value and !$overwrite)
  41. return false;
  42. if (isset($this->file) and file_exists($this->file)) {
  43. $contents = str_replace("<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n",
  44. "",
  45. file_get_contents($this->file));
  46. $this->yaml = YAML::load($contents);
  47. }
  48. # Add the setting
  49. $this->yaml[$setting] = $this->$setting = $value;
  50. if (class_exists("Trigger"))
  51. Trigger::current()->call("change_setting", $setting, $value, $overwrite);
  52. # Add the PHP protection!
  53. $contents = "<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n";
  54. # Generate the new YAML settings
  55. $contents.= YAML::dump($this->yaml);
  56. if (!@file_put_contents(INCLUDES_DIR."/config.yaml.php", $contents)) {
  57. Flash::warning(_f("Could not set \"<code>%s</code>\" configuration setting because <code>%s</code> is not writable.",
  58. array($setting, "/includes/config.yaml.php")));
  59. return false;
  60. } else
  61. return true;
  62. }
  63. /**
  64. * Function: remove
  65. * Removes a configuration setting.
  66. *
  67. * Parameters:
  68. * $setting - The name of the setting to remove.
  69. */
  70. public function remove($setting) {
  71. if (isset($this->file) and file_exists($this->file)) {
  72. $contents = str_replace("<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n",
  73. "",
  74. file_get_contents($this->file));
  75. $this->yaml = YAML::load($contents);
  76. }
  77. # Add the setting
  78. unset($this->yaml[$setting]);
  79. # Add the PHP protection!
  80. $contents = "<?php header(\"Status: 403\"); exit(\"Access denied.\"); ?>\n";
  81. # Generate the new YAML settings
  82. $contents.= YAML::dump($this->yaml);
  83. file_put_contents(INCLUDES_DIR."/config.yaml.php", $contents);
  84. }
  85. /**
  86. * Function: current
  87. * Returns a singleton reference to the current configuration.
  88. */
  89. public static function & current() {
  90. static $instance = null;
  91. return $instance = (empty($instance)) ? new self() : $instance ;
  92. }
  93. }