ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  $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 313 of file class.ilIniFile.php.

References error(), and groupExists().

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

314  {
315  if ($this->groupExists($a_group_name)) {
316  $this->error("Group '" . $a_group_name . "' exists");
317  return false;
318  }
319 
320  $this->GROUPS[$a_group_name] = array();
321  return true;
322  }
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 395 of file class.ilIniFile.php.

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

396  {
397  $this->ERROR = $a_errmsg;
398 
399  return true;
400  }
+ Here is the caller graph for this function:

◆ fixIniFile()

ilIniFile::fixIniFile ( )

Fix ini file (make it compatible for PHP 5.3)

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

References $l.

Referenced by parse().

137  {
138  // first read content
139  $lines = array();
140  $fp = @fopen($this->INI_FILE_NAME, "r");
141  while (!feof($fp)) {
142  $l = fgets($fp, 4096);
143  $skip = false;
144  if ((substr($l, 0, 2) == "/*" && $starttag) ||
145  substr($l, 0, 5) == "*/ ?>") {
146  $skip = true;
147  }
148  $starttag = false;
149  if (substr($l, 0, 5) == "<?php") {
150  $l = "; <?php exit; ?>";
151  $starttag = true;
152  }
153  if (!$skip) {
154  $l = str_replace("\n", "", $l);
155  $l = str_replace("\r", "", $l);
156  $lines[] = $l;
157  }
158  }
159  fclose($fp);
160 
161  // now write it back
162  $fp = @fopen($this->INI_FILE_NAME, "w");
163 
164  if (!empty($fp)) {
165  foreach ($lines as $l) {
166  fwrite($fp, $l . "\r\n");
167  }
168  }
169  fclose($fp);
170  }
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 407 of file class.ilIniFile.php.

References $ERROR.

408  {
409  return $this->ERROR;
410  }

◆ getGroupCount()

ilIniFile::getGroupCount ( )

returns number of groups public

Returns
integer

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

256  {
257  return count($this->GROUPS);
258  }

◆ groupExists()

ilIniFile::groupExists (   $a_group_name)

checks if a group exists public

Parameters
stringgroup name
Returns
boolean

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

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

283  {
284  if (!isset($this->GROUPS[$a_group_name])) {
285  return false;
286  }
287 
288  return true;
289  }
+ 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(), and fixIniFile().

Referenced by read().

112  {
113  //use php4 function parse_ini_file
114  $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
115 
116  //check if groups are filled
117  if ($this->GROUPS == false) {
118  // second try
119  $this->fixIniFile();
120 
121  $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
122  if ($this->GROUPS == false) {
123  $this->error("file_not_accessible");
124  return false;
125  }
126  }
127  //set current group
128  $temp = array_keys($this->GROUPS);
129  $this->CURRENT_GROUP = $temp[count($temp) - 1];
130  return true;
131  }
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 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 297 of file class.ilIniFile.php.

References error(), and groupExists().

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

298  {
299  if (!$this->groupExists($a_group_name)) {
300  $this->error("Group '" . $a_group_name . "' does not exist");
301  return false;
302  }
303 
304  return $this->GROUPS[$a_group_name];
305  }
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 265 of file class.ilIniFile.php.

References $key.

Referenced by show(), and write().

266  {
267  $groups = array();
268 
269  for (reset($this->GROUPS);$key = key($this->GROUPS);next($this->GROUPS)) {
270  $groups[] = $key;
271  }
272 
273  return $groups;
274  }
$key
Definition: croninfo.php:18
+ 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 361 of file class.ilIniFile.php.

References error().

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

362  {
363  if (!isset($this->GROUPS[$a_group][$a_var_name])) {
364  $this->error("'" . $a_var_name . "' does not exist in '" . $a_group . "'");
365  return false;
366  }
367 
368  return trim($this->GROUPS[$a_group][$a_var_name]);
369  }
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 330 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\writeToIniFile().

331  {
332  if (!$this->groupExists($a_group_name)) {
333  $this->error("Group '" . $a_group_name . "' does not exist");
334  return false;
335  }
336 
337  unset($this->GROUPS[$a_group_name]);
338  return true;
339  }
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 379 of file class.ilIniFile.php.

References error(), and groupExists().

Referenced by ilGlobalCacheSettings\writeToIniFile().

380  {
381  if (!$this->groupExists($a_group_name)) {
382  $this->error("Group '" . $a_group_name . "' does not exist");
383  return false;
384  }
385 
386  $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
387  return true;
388  }
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 221 of file class.ilIniFile.php.

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

222  {
223  $groups = $this->readGroups();
224  $group_cnt = count($groups);
225 
226  //clear content
227  $content = "";
228 
229  // go through all groups
230  for ($i = 0; $i < $group_cnt; $i++) {
231  $group_name = $groups[$i];
232  //prevent empty line at beginning of ini-file
233  if ($i == 0) {
234  $content = sprintf("[%s]\n", $group_name);
235  } else {
236  $content .= sprintf("\n[%s]\n", $group_name);
237  }
238 
239  $group = $this->readGroup($group_name);
240 
241  //go through group an display all variables
242  for (reset($group); $key = key($group);next($group)) {
243  $content .= sprintf("%s = %s\n", $key, $group[$key]);
244  }
245  }
246 
247  return $content;
248  }
$i
Definition: disco.tpl.php:19
$key
Definition: croninfo.php:18
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 348 of file class.ilIniFile.php.

349  {
350  return isset($this->GROUPS[$a_group][$a_var_name]);
351  }

◆ write()

ilIniFile::write ( )

save ini-file-data to filesystem private

Returns
boolean

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

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

Referenced by ilGlobalCacheSettings\writeToIniFile().

178  {
179  $fp = @fopen($this->INI_FILE_NAME, "w");
180 
181  if (empty($fp)) {
182  $this->error("Cannot create file $this->INI_FILE_NAME");
183  return false;
184  }
185 
186  //write php tags (security issue)
187  $result = fwrite($fp, "; <?php exit; ?>\r\n");
188 
189  $groups = $this->readGroups();
190  $group_cnt = count($groups);
191 
192  for ($i = 0; $i < $group_cnt; $i++) {
193  $group_name = $groups[$i];
194  //prevent empty line at beginning of ini-file
195  if ($i == 0) {
196  $res = sprintf("[%s]\r\n", $group_name);
197  } else {
198  $res = sprintf("\r\n[%s]\r\n", $group_name);
199  }
200 
201  $result = fwrite($fp, $res);
202  $group = $this->readGroup($group_name);
203 
204  for (reset($group); $key = key($group);next($group)) {
205  $res = sprintf("%s = %s\r\n", $key, "\"" . $group[$key] . "\"");
206  $result = fwrite($fp, $res);
207  }
208  }
209 
210 
211  fclose($fp);
212 
213  return true;
214  }
$result
foreach($_POST as $key=> $value) $res
error($a_errmsg)
set error message public
$i
Definition: disco.tpl.php:19
$key
Definition: croninfo.php:18
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: