ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
ilIniFile Class Reference

INIFile Parser. More...

+ Collaboration diagram for ilIniFile:

Public Member Functions

 ilIniFile ($a_ini_file_name)
 Constructor public. More...
 
 read ()
 read from ini file public More...
 
 parse ()
 load and parse an inifile private More...
 
 fixIniFile ()
 Fix ini file (make it compatible for PHP 5.3) More...
 
 parse_data ($a_data)
 parse data private More...
 
 setContent ($a_data)
 DESCRIPTION MISSING public. More...
 
 write ()
 save ini-file-data to filesystem private More...
 
 show ()
 returns the content of IniFile public More...
 
 getGroupCount ()
 returns number of groups public More...
 
 readGroups ()
 returns an array with the names of all the groups public More...
 
 groupExists ($a_group_name)
 checks if a group exists public More...
 
 readGroup ($a_group_name)
 returns an associative array of the variables in one group public More...
 
 addGroup ($a_group_name)
 adds a new group public More...
 
 removeGroup ($a_group_name)
 removes a group public More...
 
 variableExists ($a_group, $a_var_name)
 returns if a variable exists or not public More...
 
 readVariable ($a_group, $a_var_name)
 reads a single variable from a group public More...
 
 setVariable ($a_group_name, $a_var_name, $a_var_value)
 sets a variable in a group public More...
 
 error ($a_errmsg)
 set error message public More...
 
 getError ()
 returns error public More...
 

Data Fields

 $INI_FILE_NAME = ""
 
 $ERROR = ""
 
 $GROUPS = array()
 
 $CURRENT_GROUP = ""
 

Detailed Description

INIFile Parser.

Early access in init proceess! Avoid further dependencies like logging or other services

Description:

A Simpe Ini File Implementation to keep settings in a simple file instead of in a DB Based upon class.INIfile.php by Mircho Mirev mirch.nosp@m.o@ma.nosp@m.cropo.nosp@m.int..nosp@m.com

Usage Examples: $ini = new IniFile("./ini.ini"); Read entire group in an associative array $grp = $ini->read_group("MAIN"); //prints the variables in the group if ($grp) for(reset($grp); $key=key($grp); next($grp)) { echo "GROUP ".$key."=".$grp[$key]."<br>"; } //set a variable to a value $ini->setVariable("NEW","USER","JOHN"); //Save the file $ini->save_data();

Author
Mircho Mirev mirch.nosp@m.o@ma.nosp@m.cropo.nosp@m.int..nosp@m.com
Peter Gabriel peter.nosp@m.@gab.nosp@m.riel-.nosp@m.onli.nosp@m.ne.ne.nosp@m.t
Version
$Id$

Definition at line 37 of file class.ilIniFile.php.

Member Function Documentation

◆ addGroup()

ilIniFile::addGroup (   $a_group_name)

adds a new group public

Parameters
stringgroup name
Returns
boolean

Definition at line 372 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\checkIniHeader(), and ilGlobalCacheSettings\writeToIniFile().

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  }
error($a_errmsg)
set error message public
groupExists($a_group_name)
checks if a group exists public
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ error()

ilIniFile::error (   $a_errmsg)

set error message public

Parameters
string

Definition at line 458 of file class.ilIniFile.php.

Referenced by addGroup(), ilIniFile(), parse(), read(), readGroup(), readVariable(), removeGroup(), setVariable(), and write().

459  {
460  $this->ERROR = $a_errmsg;
461 
462  return true;
463  }
+ Here is the caller graph for this function:

◆ fixIniFile()

ilIniFile::fixIniFile ( )

Fix ini file (make it compatible for PHP 5.3)

Definition at line 144 of file class.ilIniFile.php.

References $l.

Referenced by parse().

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  }
global $l
Definition: afr.php:30
+ Here is the caller graph for this function:

◆ getError()

ilIniFile::getError ( )

returns error public

Returns
string

Definition at line 470 of file class.ilIniFile.php.

References $ERROR.

471  {
472  return $this->ERROR;
473  }

◆ getGroupCount()

ilIniFile::getGroupCount ( )

returns number of groups public

Returns
integer

Definition at line 311 of file class.ilIniFile.php.

312  {
313  return count($this->GROUPS);
314  }

◆ groupExists()

ilIniFile::groupExists (   $a_group_name)

checks if a group exists public

Parameters
stringgroup name
Returns
boolean

Definition at line 339 of file class.ilIniFile.php.

Referenced by addGroup(), readGroup(), removeGroup(), and setVariable().

340  {
341  if (!isset($this->GROUPS[$a_group_name]))
342  {
343  return false;
344  }
345 
346  return true;
347  }
+ Here is the caller graph for this function:

◆ ilIniFile()

ilIniFile::ilIniFile (   $a_ini_file_name)

Constructor public.

Parameters
stringname of file to be parsed
Returns
boolean

Definition at line 73 of file class.ilIniFile.php.

References error().

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  }
error($a_errmsg)
set error message public
+ Here is the call graph for this function:

◆ parse()

ilIniFile::parse ( )

load and parse an inifile private

Returns
boolean

Definition at line 116 of file class.ilIniFile.php.

References error(), and fixIniFile().

Referenced by read().

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  }
error($a_errmsg)
set error message public
fixIniFile()
Fix ini file (make it compatible for PHP 5.3)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_data()

ilIniFile::parse_data (   $a_data)

parse data private

Parameters
array

Definition at line 192 of file class.ilIniFile.php.

References $CURRENT_GROUP, and $out.

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  }

◆ read()

ilIniFile::read ( )

read from ini file public

Returns
boolean

Definition at line 91 of file class.ilIniFile.php.

References error(), and parse().

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  }
parse()
load and parse an inifile private
error($a_errmsg)
set error message public
+ Here is the call graph for this function:

◆ readGroup()

ilIniFile::readGroup (   $a_group_name)

returns an associative array of the variables in one group public

Parameters
stringgroup name
Returns
mixed return array of values or boolean 'false' on failure

Definition at line 355 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\checkIniHeader(), ilGlobalCacheSettings\readFromIniFile(), show(), and write().

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  }
error($a_errmsg)
set error message public
groupExists($a_group_name)
checks if a group exists public
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readGroups()

ilIniFile::readGroups ( )

returns an array with the names of all the groups public

Returns
array groups

Definition at line 321 of file class.ilIniFile.php.

Referenced by show(), and write().

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  }
+ Here is the caller graph for this function:

◆ readVariable()

ilIniFile::readVariable (   $a_group,
  $a_var_name 
)

reads a single variable from a group public

Parameters
stringgroup name
stringvalue
Returns
mixed return value string or boolean 'false' on failure

Definition at line 422 of file class.ilIniFile.php.

References error().

Referenced by ilFileDelivery\checkExisting(), ilTimeZone\initDefaultTimeZone(), and ilGlobalCacheSettings\readFromIniFile().

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  }
error($a_errmsg)
set error message public
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removeGroup()

ilIniFile::removeGroup (   $a_group_name)

removes a group public

Parameters
stringgroup name
Returns
boolean

Definition at line 390 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\writeToIniFile().

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  }
error($a_errmsg)
set error message public
groupExists($a_group_name)
checks if a group exists public
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setContent()

ilIniFile::setContent (   $a_data)

DESCRIPTION MISSING public.

Parameters
string
Returns
boolean true

Definition at line 211 of file class.ilIniFile.php.

212  {
213  $this->GROUPS = $a_data;
214  return true;
215  }

◆ setVariable()

ilIniFile::setVariable (   $a_group_name,
  $a_var_name,
  $a_var_value 
)

sets a variable in a group public

Parameters
string
string
string
Returns
boolean

Definition at line 441 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\writeToIniFile().

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  }
error($a_errmsg)
set error message public
groupExists($a_group_name)
checks if a group exists public
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ show()

ilIniFile::show ( )

returns the content of IniFile public

Returns
string content

Definition at line 272 of file class.ilIniFile.php.

References readGroup(), and readGroups().

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  }
readGroups()
returns an array with the names of all the groups public
readGroup($a_group_name)
returns an associative array of the variables in one group public
+ Here is the call graph for this function:

◆ variableExists()

ilIniFile::variableExists (   $a_group,
  $a_var_name 
)

returns if a variable exists or not public

Parameters
stringgroup name
stringvalue
Returns
mixed return true if value exists or false

Definition at line 409 of file class.ilIniFile.php.

410  {
411  return isset($this->GROUPS[$a_group][$a_var_name]);
412  }

◆ write()

ilIniFile::write ( )

save ini-file-data to filesystem private

Returns
boolean

Definition at line 222 of file class.ilIniFile.php.

References $res, $result, error(), readGroup(), and readGroups().

Referenced by ilGlobalCacheSettings\writeToIniFile().

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  }
$result
error($a_errmsg)
set error message public
readGroups()
returns an array with the names of all the groups public
readGroup($a_group_name)
returns an associative array of the variables in one group public
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $CURRENT_GROUP

ilIniFile::$CURRENT_GROUP = ""

Definition at line 65 of file class.ilIniFile.php.

Referenced by parse_data().

◆ $ERROR

ilIniFile::$ERROR = ""

Definition at line 51 of file class.ilIniFile.php.

Referenced by getError().

◆ $GROUPS

ilIniFile::$GROUPS = array()

Definition at line 58 of file class.ilIniFile.php.

◆ $INI_FILE_NAME

ilIniFile::$INI_FILE_NAME = ""

Definition at line 44 of file class.ilIniFile.php.


The documentation for this class was generated from the following file: