php8
[web.mtrack] / MTrack / Config.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 class MTrackConfig {
5   static $ini = null;
6   static $runtime = array();
7
8   static function getLocation() {
9     $location = getenv('MTRACK_CONFIG_FILE');
10     if (!strlen($location)) {
11       $location = dirname(__FILE__) . '/../config.ini';
12     }
13     return $location;
14   }
15
16   static function parseIni() {
17     if (self::$ini !== null) {
18       return self::$ini;
19     }
20     $location = self::getLocation();
21     self::$ini = @parse_ini_file($location, true);
22     if (self::$ini === false) {
23       self::$ini = array();
24     }
25
26     /* locate the runtime editable config data */
27     $filename = self::_get('core', 'runtime.config');
28     if (!$filename) {
29       $filename = self::_get('core', 'vardir') . '/runtime.config';
30     }
31     if (file_exists($filename)) {
32       $fp = fopen($filename, 'r');
33       flock($fp, LOCK_SH);
34       self::$runtime = @parse_ini_file($filename, true);
35       if (self::$runtime === false) {
36         self::$runtime = array();
37       }
38       flock($fp, LOCK_UN);
39       $fp = null;
40     }
41   }
42
43   static function set($section, $option, $value) {
44     self::$runtime[$section][$option] = $value;
45   }
46
47   static function remove($section, $option) {
48     unset(self::$runtime[$section][$option]);
49   }
50
51   static function save() {
52     $filename = self::_get('core', 'runtime.config');
53     if (!$filename) {
54       $filename = self::_get('core', 'vardir') . '/runtime.config';
55     }
56     if (file_exists($filename)) {
57       $fp = fopen($filename, 'r+');
58     } else {
59       $fp = fopen($filename, 'w');
60     }
61     flock($fp, LOCK_EX);
62     ftruncate($fp, 0);
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");
68       }
69       fwrite($fp, "\n");
70     }
71     flock($fp, LOCK_UN);
72     $fp = null;
73   }
74
75   static function get($section, $option) {
76     self::parseIni();
77     return self::_get($section, $option);
78   }
79
80   static function _get($section, $option) {
81     $ini = self::$ini;
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];
86     } else {
87       return null;
88     }
89
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);
93     }
94
95     return $val;
96   }
97
98   static function getSection($section) {
99     self::parseIni();
100     if (isset(self::$ini[$section])) {
101       $S = self::$ini[$section];
102     } else {
103       $S = null;
104     }
105     if (isset(self::$runtime[$section])) {
106       $R = self::$runtime[$section];
107     } else {
108       $R = null;
109     }
110     if ($S && $R) {
111       return array_merge($S, $R);
112     }
113     if ($S) {
114       return $S;
115     }
116     if ($R) {
117       return $R;
118     }
119     return array();
120   }
121
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;
129     }
130   }
131
132   /* loads plugins */
133   static function boot() 
134   {
135     if (isset($_GLOBALS['MTRACK_CONFIG_SKIP_BOOT'])) {
136       return;
137     }
138     $inc = self::get('core', 'includes');
139     if ($inc !== null) {
140       foreach (preg_split("/\s*,\s*/", $inc) as $filename) {
141         require_once $filename;
142       }
143     }
144     $plugins = self::getSection('plugins');
145     if (is_array($plugins)) foreach ($plugins as $classpat => $paramline) {
146       $params = preg_split("/\s*,\s*/", $paramline);
147
148       $rcls = new ReflectionClass($classpat);
149       $obj = $rcls->newInstanceArgs($params);
150     }
151   }
152     static function checkInitializing() 
153     {
154         if (file_exists(MTrackConfig::get('core', 'vardir') . '/.initializing')) {
155             echo "System not set up yet";
156             exit(0);
157         }      
158     }
159
160 }
161