ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 __construct($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 write()
193 {
194 $fp = @fopen($this->INI_FILE_NAME,"w");
195
196 if (empty($fp))
197 {
198 $this->error("Cannot create file $this->INI_FILE_NAME");
199 return false;
200 }
201
202 //write php tags (security issue)
203 $result = fwrite($fp, "; <?php exit; ?>\r\n");
204
205 $groups = $this->readGroups();
206 $group_cnt = count($groups);
207
208 for ($i=0; $i<$group_cnt; $i++)
209 {
210 $group_name = $groups[$i];
211 //prevent empty line at beginning of ini-file
212 if ($i==0)
213 {
214 $res = sprintf("[%s]\r\n",$group_name);
215 }
216 else
217 {
218 $res = sprintf("\r\n[%s]\r\n",$group_name);
219 }
220
221 $result = fwrite($fp, $res);
222 $group = $this->readGroup($group_name);
223
224 for (reset($group); $key=key($group);next($group))
225 {
226 $res = sprintf("%s = %s\r\n",$key,"\"".$group[$key]."\"");
227 $result = fwrite($fp,$res);
228 }
229 }
230
231
232 fclose($fp);
233
234 return true;
235 }
236
242 function show()
243 {
244 $groups = $this->readGroups();
245 $group_cnt = count($groups);
246
247 //clear content
248 $content = "";
249
250 // go through all groups
251 for ($i=0; $i<$group_cnt; $i++)
252 {
253 $group_name = $groups[$i];
254 //prevent empty line at beginning of ini-file
255 if ($i==0)
256 {
257 $content = sprintf("[%s]\n",$group_name);
258 }
259 else
260 {
261 $content .= sprintf("\n[%s]\n",$group_name);
262 }
263
264 $group = $this->readGroup($group_name);
265
266 //go through group an display all variables
267 for (reset($group); $key=key($group);next($group))
268 {
269 $content .= sprintf("%s = %s\n",$key,$group[$key]);
270 }
271 }
272
273 return $content;
274 }
275
281 function getGroupCount()
282 {
283 return count($this->GROUPS);
284 }
285
291 function readGroups()
292 {
293 $groups = array();
294
295 for (reset($this->GROUPS);$key=key($this->GROUPS);next($this->GROUPS))
296 {
297 $groups[]=$key;
298 }
299
300 return $groups;
301 }
302
309 function groupExists($a_group_name)
310 {
311 if (!isset($this->GROUPS[$a_group_name]))
312 {
313 return false;
314 }
315
316 return true;
317 }
318
325 function readGroup($a_group_name)
326 {
327 if (!$this->groupExists($a_group_name))
328 {
329 $this->error("Group '".$a_group_name."' does not exist");
330 return false;
331 }
332
333 return $this->GROUPS[$a_group_name];
334 }
335
342 function addGroup($a_group_name)
343 {
344 if ($this->groupExists($a_group_name))
345 {
346 $this->error("Group '".$a_group_name."' exists");
347 return false;
348 }
349
350 $this->GROUPS[$a_group_name] = array();
351 return true;
352 }
353
360 function removeGroup($a_group_name)
361 {
362 if (!$this->groupExists($a_group_name))
363 {
364 $this->error("Group '".$a_group_name."' does not exist");
365 return false;
366 }
367
368 unset($this->GROUPS[$a_group_name]);
369 return true;
370 }
371
379 function variableExists($a_group, $a_var_name)
380 {
381 return isset($this->GROUPS[$a_group][$a_var_name]);
382 }
383
384
392 function readVariable($a_group, $a_var_name)
393 {
394 if (!isset($this->GROUPS[$a_group][$a_var_name]))
395 {
396 $this->error("'".$a_var_name."' does not exist in '".$a_group."'");
397 return false;
398 }
399
400 return trim($this->GROUPS[$a_group][$a_var_name]);
401 }
402
411 function setVariable($a_group_name, $a_var_name, $a_var_value)
412 {
413 if (!$this->groupExists($a_group_name))
414 {
415 $this->error("Group '".$a_group_name."' does not exist");
416 return false;
417 }
418
419 $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
420 return true;
421 }
422
428 function error($a_errmsg)
429 {
430 $this->ERROR = $a_errmsg;
431
432 return true;
433 }
434
440 function getError()
441 {
442 return $this->ERROR;
443 }
444} //END class.ilIniFile
445?>
sprintf('%.4f', $callTime)
$result
global $l
Definition: afr.php:30
An exception for terminatinating execution or to throw for unit testing.
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)
write()
save ini-file-data to filesystem @access private
parse()
load and parse an inifile @access private
__construct($a_ini_file_name)
Constructor @access public.
read()
read from ini file @access public