1 <?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 18 19 20
21 class MagratheaConfig {
22 private $path = __DIR__;
23 private $config_file_name = "configs/magrathea.conf";
24 private $configs = null;
25 private $environment = null;
26 protected static $inst = null;
27
28 public static function Instance(){
29 if(!isset(self::$inst)){
30 self::$inst = new MagratheaConfig();
31 }
32 return self::$inst;
33 }
34
35 36 37 38 39
40 private function loadFile(){
41 if(!file_exists($this->path."/".$this->config_file_name)){
42 die ("Config file could not be found - ".$this->path."/".$this->config_file_name);
43 }
44 }
45
46 47 48 49 50
51 public function setPath($p){
52 $this->path = $p;
53 return $this;
54 }
55
56 57 58 59 60
61 public function SetDefaultEnvironment($env){
62 $this->environment = $env;
63 return $this;
64 }
65
66 67 68 69 70
71 public function GetEnvironment(){
72 if(!isset($this->environment)){
73 $this->environment = $this->GetConfig("general/use_environment");
74 }
75 return $this->environment;
76 }
77
78 79 80 81 82 83 84 85 86
87 public function GetConfig($config_name=""){
88 if( $this->configs == null ){
89 $this->loadFile();
90 $this->configs = @parse_ini_file($this->path."/".$this->config_file_name, true);
91 if( !$this->configs ){
92 throw new MagratheaConfigException("There was an error trying to load the config file.<br/>");
93 }
94 }
95 if( empty($config_name) ){
96 return $this->configs;
97 } else {
98 $c_split = explode("/", $config_name);
99 return ( count($c_split) == 1 ? $this->configs[$config_name] : $this->configs[$c_split[0]][$c_split[1]] );
100 }
101 }
102
103 104 105 106 107
108 public function GetFromDefault($config_name){
109 return $this->GetConfigFromDefault($config_name);
110 }
111 112 113 114 115 116
117 public function GetConfigFromDefault($config_name){
118 if( $this->configs == null ){
119 $this->loadFile();
120 $this->configs = @parse_ini_file($this->path."/".$this->config_file_name, true);
121 if( !$this->configs ){
122 throw new MagratheaException("There was an error trying to load the config file.<br/>");
123 }
124 }
125 $environment = $this->configs["general"]["use_environment"];
126 if(array_key_exists($config_name, $this->configs[$environment])){
127 return $this->configs[$environment][$config_name];
128 } else {
129 throw new MagratheaConfigException("Key ".$config_name." does not exist in magratheaconf!", 704);
130 }
131 }
132
133 134 135 136 137 138
139 public function GetConfigSection($section_name){
140 $this->loadFile();
141 $configSection = @parse_ini_file($this->path."/".$this->config_file_name, true);
142 if( !$configSection ){
143 throw new MagratheaException("There was an error trying to load the config file.<br/>");
144 }
145 return $configSection[$section_name];
146 }
147 }
148
149
150 151 152 153
154 class MagratheaConfigFile {
155 private $path = __DIR__;
156 private $config_file_name = "configs/magrathea_plus.conf";
157 private $configs = null;
158
159 public function __construct(){
160 $this->configs = null;
161 }
162
163 164 165 166 167
168 public function setPath($p){
169 $this->path = $p;
170 return $this;
171 }
172 173 174 175 176
177 public function setConfig($c){
178 $this->configs = $c;
179 return $this;
180 }
181 182 183 184 185
186 public function setFile($filePath){
187 $this->config_file_name = $filePath;
188 return $this;
189 }
190 191 192
193 private function loadFile(){
194 if(!file_exists($this->path."/".$this->config_file_name)){
195 throw new MagratheaException("Config file could not be found - ".$this->path."/".$this->config_file_name);
196 }
197 }
198 199 200 201
202 public function createFileIfNotExists(){
203 if(!file_exists($this->path."/".$this->config_file_name)){
204 return $this->Save();
205 } else return true;
206 }
207 208 209 210 211 212 213
214 public function getConfig($config_name=""){
215 if( is_null($this->configs) ){
216 $this->loadFile();
217 $this->configs = @parse_ini_file($this->path."/".$this->config_file_name, true);
218 }
219 if( empty($config_name) ){
220 return $this->configs;
221 } else {
222 $c_split = explode("/", $config_name);
223 return ( count($c_split) == 1 ? $this->configs[$config_name] : $this->configs[$c_split[0]][$c_split[1]] );
224 }
225 }
226 227 228 229 230 231 232 233
234 public function getConfigSection($section_name){
235 $this->loadFile();
236 $configSection = @parse_ini_file($this->path.$this->config_file_name, true);
237 if( empty($configSection ) ) return null;
238 if( !$configSection ){
239 throw new MagratheaException("There was an error trying to load the config file.<br/>");
240 }
241 return $configSection[$section_name];
242 }
243 244 245 246 247 248 249 250
251 public function Save($save_sections=true) {
252 $content = "// generated by Magrathea at ".@date("Y-m-d h:i:s")."\n";
253 $data = $this->configs;
254 if( $data == null ) $data = array();
255 if ($save_sections) {
256 foreach ($data as $key=>$elem) {
257 $content .= "\n[".$key."]\n";
258 if(!is_array($elem)){
259 throw new MagratheaConfigException("Hey, you! If you are gonna save a config file with sections, all the configs must be inside one section...", 1);
260
261 }
262 foreach ($elem as $key2=>$elem2) {
263 if(is_array($elem2)) {
264 for($i=0;$i<count($elem2);$i++) {
265 $content .= "\t".$key2."[] = \"".$elem2[$i]."\"\n";
266 }
267 } else if($elem2=="") $content .= "\t".$key2." = \n";
268 else $content .= "\t".$key2." = \"".$elem2."\"\n";
269 }
270 }
271 } else {
272 foreach ($data as $key=>$elem) {
273 if(is_array($elem)) {
274 for($i=0;$i<count($elem);$i++) {
275 $content .= $key."[] = \"".$elem[$i]."\"\n";
276 }
277 } else if($elem=="") $content .= $key." = \n";
278 else $content .= $key." = \"".$elem."\"\n";
279 }
280 }
281 if(!is_writable($this->path)){
282 throw new MagratheaConfigException("Permission denied on path: ".$this->path);
283 return false;
284 }
285 $file = $this->path."/".$this->config_file_name;
286 if(file_exists($file)){
287 @unlink($file);
288 }
289 if (!$handle = fopen($file, 'w')) {
290 throw new MagratheaConfigException("Oh noes! Could not open File: ".$file);
291 return false;
292 }
293 if (!fwrite($handle, $content)) {
294 throw new MagratheaConfigException("Oh noes! Could not save File: ".$file);
295 return false;
296 }
297 fclose($handle);
298 return true;
299 }
300
301
302
303
304 }
305
306