ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
class.ilIniFile.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
35{
42
48 var $ERROR = "";
49
55 var $GROUPS = array();
56
63
70 function ilIniFile($a_ini_file_name)
71 {
72 //check if a filename is given
73 if (empty($a_ini_file_name))
74 {
75 $this->error("no_file_given");
76 return false;
77 }
78
79 $this->INI_FILE_NAME = $a_ini_file_name;
80 return true;
81 }
82
88 function read()
89 {
90 //check if file exists
91 if (!file_exists($this->INI_FILE_NAME))
92 {
93 $this->error("file_does_not_exist");
94 return false;
95 }
96 else
97 {
98 //parse the file
99 if ($this->parse() == false)
100 {
101 return false;
102 }
103 }
104
105 return true;
106 }
107
113 function parse()
114 {
115 //use php4 function parse_ini_file
116 $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
117
118 //check if groups are filled
119 if ($this->GROUPS == false)
120 {
121 // second try
122 $this->fixIniFile();
123
124 $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
125 if ($this->GROUPS == false)
126 {
127 $this->error("file_not_accessible");
128 return false;
129 }
130 }
131 //set current group
132 $temp = array_keys($this->GROUPS);
133 $this->CURRENT_GROUP = $temp[count($temp)-1];
134 return true;
135
136 }
137
141 function fixIniFile()
142 {
143 // first read content
144 $lines = array();
145 $fp = @fopen($this->INI_FILE_NAME,"r");
146 while (!feof($fp))
147 {
148 $l = fgets($fp, 4096);
149 $skip = false;
150 if ((substr($l, 0, 2) == "/*" && $starttag) ||
151 substr($l, 0, 5) == "*/ ?>")
152 {
153 $skip = true;
154 }
155 $starttag = false;
156 if (substr($l, 0, 5) == "<?php")
157 {
158 $l = "; <?php exit; ?>";
159 $starttag = true;
160 }
161 if (!$skip)
162 {
163 $l = str_replace("\n", "", $l);
164 $l = str_replace("\r", "", $l);
165 $lines[] = $l;
166 }
167 }
168 fclose($fp);
169
170 // now write it back
171 $fp = @fopen($this->INI_FILE_NAME,"w");
172
173 if (!empty($fp))
174 {
175 foreach ($lines as $l)
176 {
177 fwrite($fp, $l."\r\n");
178 }
179 }
180 fclose($fp);
181
182 }
183
189 function parse_data($a_data)
190 {
191 if (ereg("\[([[:alnum:]]+)\]",$a_data,$out))
192 {
193 $this->CURRENT_GROUP= trim($out[1]);
194 }
195 elseif (!empty($a_data))
196 {
197 $split_data = split("=", $a_data);
198 $this->GROUPS[$this->CURRENT_GROUP][trim($split_data[0])]=trim($split_data[1]);
199 }
200 }
201
208 function setContent($a_data)
209 {
210 $this->GROUPS = $a_data;
211 return true;
212 }
213
219 function write()
220 {
221 $fp = @fopen($this->INI_FILE_NAME,"w");
222
223 if (empty($fp))
224 {
225 $this->error("Cannot create file $this->INI_FILE_NAME");
226 return false;
227 }
228
229 //write php tags (security issue)
230 $result = fwrite($fp, "; <?php exit; ?>\r\n");
231
232 $groups = $this->readGroups();
233 $group_cnt = count($groups);
234
235 for ($i=0; $i<$group_cnt; $i++)
236 {
237 $group_name = $groups[$i];
238 //prevent empty line at beginning of ini-file
239 if ($i==0)
240 {
241 $res = sprintf("[%s]\r\n",$group_name);
242 }
243 else
244 {
245 $res = sprintf("\r\n[%s]\r\n",$group_name);
246 }
247
248 $result = fwrite($fp, $res);
249 $group = $this->readGroup($group_name);
250
251 for (reset($group); $key=key($group);next($group))
252 {
253 $res = sprintf("%s = %s\r\n",$key,"\"".$group[$key]."\"");
254 $result = fwrite($fp,$res);
255 }
256 }
257
258
259 fclose($fp);
260
261 return true;
262 }
263
269 function show()
270 {
271 $groups = $this->readGroups();
272 $group_cnt = count($groups);
273
274 //clear content
275 $content = "";
276
277 // go through all groups
278 for ($i=0; $i<$group_cnt; $i++)
279 {
280 $group_name = $groups[$i];
281 //prevent empty line at beginning of ini-file
282 if ($i==0)
283 {
284 $content = sprintf("[%s]\n",$group_name);
285 }
286 else
287 {
288 $content .= sprintf("\n[%s]\n",$group_name);
289 }
290
291 $group = $this->readGroup($group_name);
292
293 //go through group an display all variables
294 for (reset($group); $key=key($group);next($group))
295 {
296 $content .= sprintf("%s = %s\n",$key,$group[$key]);
297 }
298 }
299
300 return $content;
301 }
302
308 function getGroupCount()
309 {
310 return count($this->GROUPS);
311 }
312
318 function readGroups()
319 {
320 $groups = array();
321
322 for (reset($this->GROUPS);$key=key($this->GROUPS);next($this->GROUPS))
323 {
324 $groups[]=$key;
325 }
326
327 return $groups;
328 }
329
336 function groupExists($a_group_name)
337 {
338 if (!isset($this->GROUPS[$a_group_name]))
339 {
340 return false;
341 }
342
343 return true;
344 }
345
352 function readGroup($a_group_name)
353 {
354 if (!$this->groupExists($a_group_name))
355 {
356 $this->error("Group '".$a_group_name."' does not exist");
357 return false;
358 }
359
360 return $this->GROUPS[$a_group_name];
361 }
362
369 function addGroup($a_group_name)
370 {
371 if ($this->groupExists($a_group_name))
372 {
373 $this->error("Group '".$a_group_name."' exists");
374 return false;
375 }
376
377 $this->GROUPS[$a_group_name] = array();
378 return true;
379 }
380
387 function removeGroup($a_group_name)
388 {
389 if (!$this->groupExists($a_group_name))
390 {
391 $this->error("Group '".$a_group_name."' does not exist");
392 return false;
393 }
394
395 unset($this->GROUPS[$a_group_name]);
396 return true;
397 }
398
406 function variableExists($a_group, $a_var_name)
407 {
408 return isset($this->GROUPS[$a_group][$a_var_name]);
409 }
410
411
419 function readVariable($a_group, $a_var_name)
420 {
421 if (!isset($this->GROUPS[$a_group][$a_var_name]))
422 {
423 $this->error("'".$a_var_name."' does not exist in '".$a_group."'");
424 return false;
425 }
426
427 return trim($this->GROUPS[$a_group][$a_var_name]);
428 }
429
438 function setVariable($a_group_name, $a_var_name, $a_var_value)
439 {
440 if (!$this->groupExists($a_group_name))
441 {
442 $this->error("Group '".$a_group_name."' does not exist");
443 return false;
444 }
445
446 $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
447 return true;
448 }
449
455 function error($a_errmsg)
456 {
457 $this->ERROR = $a_errmsg;
458
459 return true;
460 }
461
467 function getError()
468 {
469 return $this->ERROR;
470 }
471} //END class.ilIniFile
472?>
$result
getError()
returns error @access public
show()
returns the content of IniFile @access public
readGroups()
returns an array with the names of all the groups @access public
variableExists($a_group, $a_var_name)
returns if a variable exists or not @access public
getGroupCount()
returns number of groups @access public
removeGroup($a_group_name)
removes a group @access public
readGroup($a_group_name)
returns an associative array of the variables in one group @access public
addGroup($a_group_name)
adds a new group @access public
readVariable($a_group, $a_var_name)
reads a single variable from a group @access public
setVariable($a_group_name, $a_var_name, $a_var_value)
sets a variable in a group @access public
groupExists($a_group_name)
checks if a group exists @access public
error($a_errmsg)
set error message @access public
INIFile Parser.
fixIniFile()
Fix ini file (make it compatible for PHP 5.3)
ilIniFile($a_ini_file_name)
Constructor @access public.
write()
save ini-file-data to filesystem @access private
parse()
load and parse an inifile @access private
parse_data($a_data)
parse data @access private
read()
read from ini file @access public
setContent($a_data)
DESCRIPTION MISSING @access public.