1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
6 static $runtime = array();
8 static function getLocation() {
9 $location = getenv('MTRACK_CONFIG_FILE');
10 if (!strlen($location)) {
11 $location = dirname(__FILE__) . '/../../config.ini';
16 static function parseIni() {
17 if (self::$ini !== null) {
20 $location = self::getLocation();
21 self::$ini = @parse_ini_file($location, true);
22 if (self::$ini === false) {
26 /* locate the runtime editable config data */
27 $filename = self::_get('core', 'runtime.config');
29 $filename = self::_get('core', 'vardir') . '/runtime.config';
31 if (file_exists($filename)) {
32 $fp = fopen($filename, 'r');
34 self::$runtime = @parse_ini_file($filename, true);
35 if (self::$runtime === false) {
36 self::$runtime = array();
43 static function set($section, $option, $value) {
44 self::$runtime[$section][$option] = $value;
47 static function remove($section, $option) {
48 unset(self::$runtime[$section][$option]);
51 static function save() {
52 $filename = self::_get('core', 'runtime.config');
54 $filename = self::_get('core', 'vardir') . '/runtime.config';
56 if (file_exists($filename)) {
57 $fp = fopen($filename, 'r+');
59 $fp = fopen($filename, 'w');
63 foreach (self::$runtime as $section => $opts) {
64 fwrite($fp, "[$section]\n");
65 foreach ($opts as $k => $v) {
66 $v = addcslashes($v, "\"\r\n\t");
67 fwrite($fp, "$k = \"$v\"\n");
75 static function get($section, $option) {
77 return self::_get($section, $option);
80 static function _get($section, $option) {
82 if (isset(self::$ini[$section][$option])) {
83 $val = self::$ini[$section][$option];
84 } else if (isset(self::$runtime[$section][$option])) {
85 $val = self::$runtime[$section][$option];
90 while (preg_match('/@\{([a-zA-Z0-9_]+):([a-zA-Z0-9_]+)\}/', $val, $M)) {
91 $rep = self::_get($M[1], $M[2]);
92 $val = str_replace($M[0], $rep, $val);
98 static function getSection($section) {
100 if (isset(self::$ini[$section])) {
101 $S = self::$ini[$section];
105 if (isset(self::$runtime[$section])) {
106 $R = self::$runtime[$section];
111 return array_merge($S, $R);
122 static function append($section, $option, $value) {
123 if (self::$ini[$section][$option] != $value) {
124 $location = self::getLocation();
125 $data = file_get_contents($location);
126 $data .= "\n[$section]\n$option = $value\n";
127 file_put_contents($location, $data);
128 self::$ini[$section][$option] = $value;
133 static function boot()
135 if (isset($_GLOBALS['MTRACK_CONFIG_SKIP_BOOT'])) {
138 $inc = self::get('core', 'includes');
140 foreach (preg_split("/\s*,\s*/", $inc) as $filename) {
141 require_once $filename;
144 $plugins = self::getSection('plugins');
145 if (is_array($plugins)) foreach ($plugins as $classpat => $paramline) {
146 $params = preg_split("/\s*,\s*/", $paramline);
148 $rcls = new ReflectionClass($classpat);
149 $obj = $rcls->newInstanceArgs($params);
152 static function checkInitializing()
154 if (file_exists(MTrackConfig::get('core', 'vardir') . '/.initializing')) {
155 echo "System not set up yet";