ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilIniFile Class Reference

INIFile Parser. More...

+ Collaboration diagram for ilIniFile:

Public Member Functions

 __construct ($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...
 
 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.

Constructor & Destructor Documentation

◆ __construct()

ilIniFile::__construct (   $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:

Member Function Documentation

◆ addGroup()

ilIniFile::addGroup (   $a_group_name)

adds a new group public

Parameters
stringgroup name
Returns
boolean

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

References array, error(), and groupExists().

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

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  }
error($a_errmsg)
set error message public
Create styles array
The data for the language used.
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 428 of file class.ilIniFile.php.

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

429  {
430  $this->ERROR = $a_errmsg;
431 
432  return true;
433  }
+ 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, and array.

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  }
Create styles array
The data for the language used.
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 440 of file class.ilIniFile.php.

References $ERROR.

441  {
442  return $this->ERROR;
443  }

◆ getGroupCount()

ilIniFile::getGroupCount ( )

returns number of groups public

Returns
integer

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

282  {
283  return count($this->GROUPS);
284  }

◆ groupExists()

ilIniFile::groupExists (   $a_group_name)

checks if a group exists public

Parameters
stringgroup name
Returns
boolean

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

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

310  {
311  if (!isset($this->GROUPS[$a_group_name]))
312  {
313  return false;
314  }
315 
316  return true;
317  }
+ Here is the caller 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:

◆ 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 325 of file class.ilIniFile.php.

References error(), and groupExists().

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

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  }
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 291 of file class.ilIniFile.php.

References array.

Referenced by show(), and write().

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  }
Create styles array
The data for the language used.
+ 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 392 of file class.ilIniFile.php.

References error().

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

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  }
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 360 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\writeToIniFile().

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  }
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:

◆ 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 411 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\writeToIniFile().

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  }
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 242 of file class.ilIniFile.php.

References readGroup(), and readGroups().

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  }
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 379 of file class.ilIniFile.php.

380  {
381  return isset($this->GROUPS[$a_group][$a_var_name]);
382  }

◆ write()

ilIniFile::write ( )

save ini-file-data to filesystem private

Returns
boolean

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

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

Referenced by ilGlobalCacheSettings\writeToIniFile().

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  }
$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.

◆ $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: