ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
38{
45
51 var $ERROR = "";
52
58 var $GROUPS = array();
59
66
73 function ilIniFile($a_ini_file_name)
74 {
75 //check if a filename is given
76 if (empty($a_ini_file_name))
77 {
78 $this->error("no_file_given");
79 return false;
80 }
81
82 $this->INI_FILE_NAME = $a_ini_file_name;
83 return true;
84 }
85
91 function read()
92 {
93 //check if file exists
94 if (!file_exists($this->INI_FILE_NAME))
95 {
96 $this->error("file_does_not_exist");
97 return false;
98 }
99 else
100 {
101 //parse the file
102 if ($this->parse() == false)
103 {
104 return false;
105 }
106 }
107
108 return true;
109 }
110
116 function parse()
117 {
118 //use php4 function parse_ini_file
119 $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
120
121 //check if groups are filled
122 if ($this->GROUPS == false)
123 {
124 // second try
125 $this->fixIniFile();
126
127 $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
128 if ($this->GROUPS == false)
129 {
130 $this->error("file_not_accessible");
131 return false;
132 }
133 }
134 //set current group
135 $temp = array_keys($this->GROUPS);
136 $this->CURRENT_GROUP = $temp[count($temp)-1];
137 return true;
138
139 }
140
144 function fixIniFile()
145 {
146 // first read content
147 $lines = array();
148 $fp = @fopen($this->INI_FILE_NAME,"r");
149 while (!feof($fp))
150 {
151 $l = fgets($fp, 4096);
152 $skip = false;
153 if ((substr($l, 0, 2) == "/*" && $starttag) ||
154 substr($l, 0, 5) == "*/ ?>")
155 {
156 $skip = true;
157 }
158 $starttag = false;
159 if (substr($l, 0, 5) == "<?php")
160 {
161 $l = "; <?php exit; ?>";
162 $starttag = true;
163 }
164 if (!$skip)
165 {
166 $l = str_replace("\n", "", $l);
167 $l = str_replace("\r", "", $l);
168 $lines[] = $l;
169 }
170 }
171 fclose($fp);
172
173 // now write it back
174 $fp = @fopen($this->INI_FILE_NAME,"w");
175
176 if (!empty($fp))
177 {
178 foreach ($lines as $l)
179 {
180 fwrite($fp, $l."\r\n");
181 }
182 }
183 fclose($fp);
184
185 }
186
192 function parse_data($a_data)
193 {
194 if (ereg("\[([[:alnum:]]+)\]",$a_data,$out))
195 {
196 $this->CURRENT_GROUP= trim($out[1]);
197 }
198 elseif (!empty($a_data))
199 {
200 $split_data = split("=", $a_data);
201 $this->GROUPS[$this->CURRENT_GROUP][trim($split_data[0])]=trim($split_data[1]);
202 }
203 }
204
211 function setContent($a_data)
212 {
213 $this->GROUPS = $a_data;
214 return true;
215 }
216
222 function write()
223 {
224 $fp = @fopen($this->INI_FILE_NAME,"w");
225
226 if (empty($fp))
227 {
228 $this->error("Cannot create file $this->INI_FILE_NAME");
229 return false;
230 }
231
232 //write php tags (security issue)
233 $result = fwrite($fp, "; <?php exit; ?>\r\n");
234
235 $groups = $this->readGroups();
236 $group_cnt = count($groups);
237
238 for ($i=0; $i<$group_cnt; $i++)
239 {
240 $group_name = $groups[$i];
241 //prevent empty line at beginning of ini-file
242 if ($i==0)
243 {
244 $res = sprintf("[%s]\r\n",$group_name);
245 }
246 else
247 {
248 $res = sprintf("\r\n[%s]\r\n",$group_name);
249 }
250
251 $result = fwrite($fp, $res);
252 $group = $this->readGroup($group_name);
253
254 for (reset($group); $key=key($group);next($group))
255 {
256 $res = sprintf("%s = %s\r\n",$key,"\"".$group[$key]."\"");
257 $result = fwrite($fp,$res);
258 }
259 }
260
261
262 fclose($fp);
263
264 return true;
265 }
266
272 function show()
273 {
274 $groups = $this->readGroups();
275 $group_cnt = count($groups);
276
277 //clear content
278 $content = "";
279
280 // go through all groups
281 for ($i=0; $i<$group_cnt; $i++)
282 {
283 $group_name = $groups[$i];
284 //prevent empty line at beginning of ini-file
285 if ($i==0)
286 {
287 $content = sprintf("[%s]\n",$group_name);
288 }
289 else
290 {
291 $content .= sprintf("\n[%s]\n",$group_name);
292 }
293
294 $group = $this->readGroup($group_name);
295
296 //go through group an display all variables
297 for (reset($group); $key=key($group);next($group))
298 {
299 $content .= sprintf("%s = %s\n",$key,$group[$key]);
300 }
301 }
302
303 return $content;
304 }
305
311 function getGroupCount()
312 {
313 return count($this->GROUPS);
314 }
315
321 function readGroups()
322 {
323 $groups = array();
324
325 for (reset($this->GROUPS);$key=key($this->GROUPS);next($this->GROUPS))
326 {
327 $groups[]=$key;
328 }
329
330 return $groups;
331 }
332
339 function groupExists($a_group_name)
340 {
341 if (!isset($this->GROUPS[$a_group_name]))
342 {
343 return false;
344 }
345
346 return true;
347 }
348
355 function readGroup($a_group_name)
356 {
357 if (!$this->groupExists($a_group_name))
358 {
359 $this->error("Group '".$a_group_name."' does not exist");
360 return false;
361 }
362
363 return $this->GROUPS[$a_group_name];
364 }
365
372 function addGroup($a_group_name)
373 {
374 if ($this->groupExists($a_group_name))
375 {
376 $this->error("Group '".$a_group_name."' exists");
377 return false;
378 }
379
380 $this->GROUPS[$a_group_name] = array();
381 return true;
382 }
383
390 function removeGroup($a_group_name)
391 {
392 if (!$this->groupExists($a_group_name))
393 {
394 $this->error("Group '".$a_group_name."' does not exist");
395 return false;
396 }
397
398 unset($this->GROUPS[$a_group_name]);
399 return true;
400 }
401
409 function variableExists($a_group, $a_var_name)
410 {
411 return isset($this->GROUPS[$a_group][$a_var_name]);
412 }
413
414
422 function readVariable($a_group, $a_var_name)
423 {
424 if (!isset($this->GROUPS[$a_group][$a_var_name]))
425 {
426 $this->error("'".$a_var_name."' does not exist in '".$a_group."'");
427 return false;
428 }
429
430 return trim($this->GROUPS[$a_group][$a_var_name]);
431 }
432
441 function setVariable($a_group_name, $a_var_name, $a_var_value)
442 {
443 if (!$this->groupExists($a_group_name))
444 {
445 $this->error("Group '".$a_group_name."' does not exist");
446 return false;
447 }
448
449 $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
450 return true;
451 }
452
458 function error($a_errmsg)
459 {
460 $this->ERROR = $a_errmsg;
461
462 return true;
463 }
464
470 function getError()
471 {
472 return $this->ERROR;
473 }
474} //END class.ilIniFile
475?>
$result
global $l
Definition: afr.php:30
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.