ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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.

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 34 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 369 of file class.ilIniFile.php.

References error(), and groupExists().

370  {
371  if ($this->groupExists($a_group_name))
372  {
373  $this->error("Group '".$a_group_name."' exists");
374  return false;
375  }
376 
377  $this->GROUPS[$a_group_name] = array();
378  return true;
379  }
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:

◆ error()

ilIniFile::error (   $a_errmsg)

set error message public

Parameters
string

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

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

456  {
457  $this->ERROR = $a_errmsg;
458 
459  return true;
460  }
+ Here is the caller graph for this function:

◆ fixIniFile()

ilIniFile::fixIniFile ( )

Fix ini file (make it compatible for PHP 5.3)

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

Referenced by parse().

142  {
143  // first read content
144  $lines = array();
145  $fp = @fopen($this->INI_FILE_NAME,"r");
146  while (!feof($fp))
147  {
148  $l = fgets($fp, 4096);
149  $skip = false;
150  if ((substr($l, 0, 2) == "/*" && $starttag) ||
151  substr($l, 0, 5) == "*/ ?>")
152  {
153  $skip = true;
154  }
155  $starttag = false;
156  if (substr($l, 0, 5) == "<?php")
157  {
158  $l = "; <?php exit; ?>";
159  $starttag = true;
160  }
161  if (!$skip)
162  {
163  $l = str_replace("\n", "", $l);
164  $l = str_replace("\r", "", $l);
165  $lines[] = $l;
166  }
167  }
168  fclose($fp);
169 
170  // now write it back
171  $fp = @fopen($this->INI_FILE_NAME,"w");
172 
173  if (!empty($fp))
174  {
175  foreach ($lines as $l)
176  {
177  fwrite($fp, $l."\r\n");
178  }
179  }
180  fclose($fp);
181 
182  }
+ Here is the caller graph for this function:

◆ getError()

ilIniFile::getError ( )

returns error public

Returns
string

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

References $ERROR.

468  {
469  return $this->ERROR;
470  }

◆ getGroupCount()

ilIniFile::getGroupCount ( )

returns number of groups public

Returns
integer

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

309  {
310  return count($this->GROUPS);
311  }

◆ groupExists()

ilIniFile::groupExists (   $a_group_name)

checks if a group exists public

Parameters
stringgroup name
Returns
boolean

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

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

337  {
338  if (!isset($this->GROUPS[$a_group_name]))
339  {
340  return false;
341  }
342 
343  return true;
344  }
+ 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 70 of file class.ilIniFile.php.

References error().

71  {
72  //check if a filename is given
73  if (empty($a_ini_file_name))
74  {
75  $this->error("no_file_given");
76  return false;
77  }
78 
79  $this->INI_FILE_NAME = $a_ini_file_name;
80  return true;
81  }
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 113 of file class.ilIniFile.php.

References error(), and fixIniFile().

Referenced by read().

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

References $CURRENT_GROUP, and $out.

190  {
191  if (ereg("\[([[:alnum:]]+)\]",$a_data,$out))
192  {
193  $this->CURRENT_GROUP= trim($out[1]);
194  }
195  elseif (!empty($a_data))
196  {
197  $split_data = split("=", $a_data);
198  $this->GROUPS[$this->CURRENT_GROUP][trim($split_data[0])]=trim($split_data[1]);
199  }
200  }

◆ read()

ilIniFile::read ( )

read from ini file public

Returns
boolean

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

References error(), and parse().

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

References error(), and groupExists().

Referenced by show(), and write().

353  {
354  if (!$this->groupExists($a_group_name))
355  {
356  $this->error("Group '".$a_group_name."' does not exist");
357  return false;
358  }
359 
360  return $this->GROUPS[$a_group_name];
361  }
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 318 of file class.ilIniFile.php.

Referenced by show(), and write().

319  {
320  $groups = array();
321 
322  for (reset($this->GROUPS);$key=key($this->GROUPS);next($this->GROUPS))
323  {
324  $groups[]=$key;
325  }
326 
327  return $groups;
328  }
+ 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 419 of file class.ilIniFile.php.

References error().

420  {
421  if (!isset($this->GROUPS[$a_group][$a_var_name]))
422  {
423  $this->error("'".$a_var_name."' does not exist in '".$a_group."'");
424  return false;
425  }
426 
427  return trim($this->GROUPS[$a_group][$a_var_name]);
428  }
error($a_errmsg)
set error message public
+ Here is the call graph for this function:

◆ removeGroup()

ilIniFile::removeGroup (   $a_group_name)

removes a group public

Parameters
stringgroup name
Returns
boolean

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

References error(), and groupExists().

388  {
389  if (!$this->groupExists($a_group_name))
390  {
391  $this->error("Group '".$a_group_name."' does not exist");
392  return false;
393  }
394 
395  unset($this->GROUPS[$a_group_name]);
396  return true;
397  }
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:

◆ setContent()

ilIniFile::setContent (   $a_data)

DESCRIPTION MISSING public.

Parameters
string
Returns
boolean true

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

209  {
210  $this->GROUPS = $a_data;
211  return true;
212  }

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

References error(), and groupExists().

439  {
440  if (!$this->groupExists($a_group_name))
441  {
442  $this->error("Group '".$a_group_name."' does not exist");
443  return false;
444  }
445 
446  $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
447  return true;
448  }
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:

◆ show()

ilIniFile::show ( )

returns the content of IniFile public

Returns
string content

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

References readGroup(), and readGroups().

270  {
271  $groups = $this->readGroups();
272  $group_cnt = count($groups);
273 
274  //clear content
275  $content = "";
276 
277  // go through all groups
278  for ($i=0; $i<$group_cnt; $i++)
279  {
280  $group_name = $groups[$i];
281  //prevent empty line at beginning of ini-file
282  if ($i==0)
283  {
284  $content = sprintf("[%s]\n",$group_name);
285  }
286  else
287  {
288  $content .= sprintf("\n[%s]\n",$group_name);
289  }
290 
291  $group = $this->readGroup($group_name);
292 
293  //go through group an display all variables
294  for (reset($group); $key=key($group);next($group))
295  {
296  $content .= sprintf("%s = %s\n",$key,$group[$key]);
297  }
298  }
299 
300  return $content;
301  }
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 406 of file class.ilIniFile.php.

407  {
408  return isset($this->GROUPS[$a_group][$a_var_name]);
409  }

◆ write()

ilIniFile::write ( )

save ini-file-data to filesystem private

Returns
boolean

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

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

220  {
221  $fp = @fopen($this->INI_FILE_NAME,"w");
222 
223  if (empty($fp))
224  {
225  $this->error("Cannot create file $this->INI_FILE_NAME");
226  return false;
227  }
228 
229  //write php tags (security issue)
230  $result = fwrite($fp, "; <?php exit; ?>\r\n");
231 
232  $groups = $this->readGroups();
233  $group_cnt = count($groups);
234 
235  for ($i=0; $i<$group_cnt; $i++)
236  {
237  $group_name = $groups[$i];
238  //prevent empty line at beginning of ini-file
239  if ($i==0)
240  {
241  $res = sprintf("[%s]\r\n",$group_name);
242  }
243  else
244  {
245  $res = sprintf("\r\n[%s]\r\n",$group_name);
246  }
247 
248  $result = fwrite($fp, $res);
249  $group = $this->readGroup($group_name);
250 
251  for (reset($group); $key=key($group);next($group))
252  {
253  $res = sprintf("%s = %s\r\n",$key,"\"".$group[$key]."\"");
254  $result = fwrite($fp,$res);
255  }
256  }
257 
258 
259  fclose($fp);
260 
261  return true;
262  }
$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:

Field Documentation

◆ $CURRENT_GROUP

ilIniFile::$CURRENT_GROUP = ""

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

Referenced by parse_data().

◆ $ERROR

ilIniFile::$ERROR = ""

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

Referenced by getError().

◆ $GROUPS

ilIniFile::$GROUPS = array()

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

◆ $INI_FILE_NAME

ilIniFile::$INI_FILE_NAME = ""

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


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