ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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...
 
 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  $this->error("no_file_given");
78  return false;
79  }
80 
81  $this->INI_FILE_NAME = $a_ini_file_name;
82  return true;
83  }
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 268 of file class.ilIniFile.php.

References error(), and groupExists().

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

269  {
270  if ($this->groupExists($a_group_name)) {
271  $this->error("Group '" . $a_group_name . "' exists");
272  return false;
273  }
274 
275  $this->GROUPS[$a_group_name] = array();
276  return true;
277  }
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 350 of file class.ilIniFile.php.

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

351  {
352  $this->ERROR = $a_errmsg;
353 
354  return true;
355  }
+ Here is the caller graph for this function:

◆ getError()

ilIniFile::getError ( )

returns error public

Returns
string

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

References $ERROR.

363  {
364  return $this->ERROR;
365  }

◆ getGroupCount()

ilIniFile::getGroupCount ( )

returns number of groups public

Returns
integer

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

211  {
212  return count($this->GROUPS);
213  }

◆ groupExists()

ilIniFile::groupExists (   $a_group_name)

checks if a group exists public

Parameters
stringgroup name
Returns
boolean

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

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

238  {
239  if (!isset($this->GROUPS[$a_group_name])) {
240  return false;
241  }
242 
243  return true;
244  }
+ Here is the caller graph for this function:

◆ parse()

ilIniFile::parse ( )

load and parse an inifile private

Returns
boolean

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

References error().

Referenced by read().

112  {
113  $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
114 
115  //check if groups are filled
116  if ($this->GROUPS == false) {
117  $this->error("file_not_accessible");
118  return false;
119 
120  }
121  //set current group
122  $temp = array_keys($this->GROUPS);
123  $this->CURRENT_GROUP = $temp[count($temp) - 1];
124  return true;
125  }
error($a_errmsg)
set error message public
+ 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 90 of file class.ilIniFile.php.

References error(), and parse().

91  {
92  //check if file exists
93  if (!file_exists($this->INI_FILE_NAME)) {
94  $this->error("file_does_not_exist");
95  return false;
96  } else {
97  //parse the file
98  if ($this->parse() == false) {
99  return false;
100  }
101  }
102 
103  return true;
104  }
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 252 of file class.ilIniFile.php.

References error(), and groupExists().

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

253  {
254  if (!$this->groupExists($a_group_name)) {
255  $this->error("Group '" . $a_group_name . "' does not exist");
256  return false;
257  }
258 
259  return $this->GROUPS[$a_group_name];
260  }
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 220 of file class.ilIniFile.php.

Referenced by show(), and write().

221  {
222  $groups = array();
223 
224  for (reset($this->GROUPS);$key = key($this->GROUPS);next($this->GROUPS)) {
225  $groups[] = $key;
226  }
227 
228  return $groups;
229  }
+ 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 316 of file class.ilIniFile.php.

References error().

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

317  {
318  if (!isset($this->GROUPS[$a_group][$a_var_name])) {
319  $this->error("'" . $a_var_name . "' does not exist in '" . $a_group . "'");
320  return false;
321  }
322 
323  return trim($this->GROUPS[$a_group][$a_var_name]);
324  }
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 285 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\writeToIniFile().

286  {
287  if (!$this->groupExists($a_group_name)) {
288  $this->error("Group '" . $a_group_name . "' does not exist");
289  return false;
290  }
291 
292  unset($this->GROUPS[$a_group_name]);
293  return true;
294  }
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 334 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\writeToIniFile().

335  {
336  if (!$this->groupExists($a_group_name)) {
337  $this->error("Group '" . $a_group_name . "' does not exist");
338  return false;
339  }
340 
341  $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
342  return true;
343  }
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 176 of file class.ilIniFile.php.

References $i, readGroup(), and readGroups().

177  {
178  $groups = $this->readGroups();
179  $group_cnt = count($groups);
180 
181  //clear content
182  $content = "";
183 
184  // go through all groups
185  for ($i = 0; $i < $group_cnt; $i++) {
186  $group_name = $groups[$i];
187  //prevent empty line at beginning of ini-file
188  if ($i == 0) {
189  $content = sprintf("[%s]\n", $group_name);
190  } else {
191  $content .= sprintf("\n[%s]\n", $group_name);
192  }
193 
194  $group = $this->readGroup($group_name);
195 
196  //go through group an display all variables
197  for (reset($group); $key = key($group);next($group)) {
198  $content .= sprintf("%s = %s\n", $key, $group[$key]);
199  }
200  }
201 
202  return $content;
203  }
readGroups()
returns an array with the names of all the groups public
$i
Definition: metadata.php:24
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 303 of file class.ilIniFile.php.

304  {
305  return isset($this->GROUPS[$a_group][$a_var_name]);
306  }

◆ write()

ilIniFile::write ( )

save ini-file-data to filesystem private

Returns
boolean

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

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

Referenced by ilGlobalCacheSettings\writeToIniFile().

133  {
134  $fp = @fopen($this->INI_FILE_NAME, "w");
135 
136  if (empty($fp)) {
137  $this->error("Cannot create file $this->INI_FILE_NAME");
138  return false;
139  }
140 
141  //write php tags (security issue)
142  $result = fwrite($fp, "; <?php exit; ?>\r\n");
143 
144  $groups = $this->readGroups();
145  $group_cnt = count($groups);
146 
147  for ($i = 0; $i < $group_cnt; $i++) {
148  $group_name = $groups[$i];
149  //prevent empty line at beginning of ini-file
150  if ($i == 0) {
151  $res = sprintf("[%s]\r\n", $group_name);
152  } else {
153  $res = sprintf("\r\n[%s]\r\n", $group_name);
154  }
155 
156  $result = fwrite($fp, $res);
157  $group = $this->readGroup($group_name);
158 
159  for (reset($group); $key = key($group);next($group)) {
160  $res = sprintf("%s = %s\r\n", $key, "\"" . $group[$key] . "\"");
161  $result = fwrite($fp, $res);
162  }
163  }
164 
165 
166  fclose($fp);
167 
168  return true;
169  }
$result
foreach($_POST as $key=> $value) $res
error($a_errmsg)
set error message public
readGroups()
returns an array with the names of all the groups public
$i
Definition: metadata.php:24
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: