ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
ilIniFile Class Reference

INIFile Parser. More...

+ Collaboration diagram for ilIniFile:

Public Member Functions

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

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:
class.ilIniFile.php 22811 2010-01-22 11:34:13Z akill

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

Member Function Documentation

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().

{
if ($this->groupExists($a_group_name))
{
$this->error("Group '".$a_group_name."' exists");
return false;
}
$this->GROUPS[$a_group_name] = array();
return true;
}

+ Here is the call graph for this function:

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().

{
$this->ERROR = $a_errmsg;
return true;
}

+ Here is the caller graph for this function:

ilIniFile::fixIniFile ( )

Fix ini file (make it compatible for PHP 5.3)

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

References $l, and $skip.

Referenced by parse().

{
// first read content
$lines = array();
$fp = @fopen($this->INI_FILE_NAME,"r");
while (!feof($fp))
{
$l = fgets($fp, 4096);
$skip = false;
if ((substr($l, 0, 2) == "/*" && $starttag) ||
substr($l, 0, 5) == "*/ ?>")
{
$skip = true;
}
$starttag = false;
if (substr($l, 0, 5) == "<?php")
{
$l = "; <?php exit; ?>";
$starttag = true;
}
if (!$skip)
{
$l = str_replace("\n", "", $l);
$l = str_replace("\r", "", $l);
$lines[] = $l;
}
}
fclose($fp);
// now write it back
$fp = @fopen($this->INI_FILE_NAME,"w");
if (!empty($fp))
{
foreach ($lines as $l)
{
fwrite($fp, $l."\r\n");
}
}
fclose($fp);
}

+ Here is the caller graph for this function:

ilIniFile::getError ( )

returns error public

Returns
string

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

References $ERROR.

{
return $this->ERROR;
}
ilIniFile::getGroupCount ( )

returns number of groups public

Returns
integer

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

{
return count($this->GROUPS);
}
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().

{
if (!isset($this->GROUPS[$a_group_name]))
{
return false;
}
return true;
}

+ Here is the caller graph for this function:

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().

{
//check if a filename is given
if (empty($a_ini_file_name))
{
$this->error("no_file_given");
return false;
}
$this->INI_FILE_NAME = $a_ini_file_name;
return true;
}

+ Here is the call graph for this function:

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().

{
//use php4 function parse_ini_file
$this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
//check if groups are filled
if ($this->GROUPS == false)
{
// second try
$this->fixIniFile();
$this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
if ($this->GROUPS == false)
{
$this->error("file_not_accessible");
return false;
}
}
//set current group
$temp = array_keys($this->GROUPS);
$this->CURRENT_GROUP = $temp[count($temp)-1];
return true;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilIniFile::parse_data (   $a_data)

parse data private

Parameters
array

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

References $CURRENT_GROUP, $out, and elseif().

{
if (ereg("\[([[:alnum:]]+)\]",$a_data,$out))
{
$this->CURRENT_GROUP= trim($out[1]);
}
elseif (!empty($a_data))
{
$split_data = split("=", $a_data);
$this->GROUPS[$this->CURRENT_GROUP][trim($split_data[0])]=trim($split_data[1]);
}
}

+ Here is the call graph for this function:

ilIniFile::read ( )

read from ini file public

Returns
boolean

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

References error(), and parse().

{
//check if file exists
if (!file_exists($this->INI_FILE_NAME))
{
$this->error("file_does_not_exist");
return false;
}
else
{
//parse the file
if ($this->parse() == false)
{
return false;
}
}
return true;
}

+ Here is the call graph for this function:

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().

{
if (!$this->groupExists($a_group_name))
{
$this->error("Group '".$a_group_name."' does not exist");
return false;
}
return $this->GROUPS[$a_group_name];
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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.

References $key.

Referenced by show(), and write().

{
$groups = array();
for (reset($this->GROUPS);$key=key($this->GROUPS);next($this->GROUPS))
{
$groups[]=$key;
}
return $groups;
}

+ Here is the caller graph for this function:

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().

{
if (!isset($this->GROUPS[$a_group][$a_var_name]))
{
$this->error("'".$a_var_name."' does not exist in '".$a_group."'");
return false;
}
return trim($this->GROUPS[$a_group][$a_var_name]);
}

+ Here is the call graph for this function:

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().

{
if (!$this->groupExists($a_group_name))
{
$this->error("Group '".$a_group_name."' does not exist");
return false;
}
unset($this->GROUPS[$a_group_name]);
return true;
}

+ Here is the call graph for this function:

ilIniFile::setContent (   $a_data)

DESCRIPTION MISSING public.

Parameters
string
Returns
boolean true

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

{
$this->GROUPS = $a_data;
return true;
}
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().

{
if (!$this->groupExists($a_group_name))
{
$this->error("Group '".$a_group_name."' does not exist");
return false;
}
$this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
return true;
}

+ Here is the call graph for this function:

ilIniFile::show ( )

returns the content of IniFile public

Returns
string content

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

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

{
$groups = $this->readGroups();
$group_cnt = count($groups);
//clear content
$content = "";
// go through all groups
for ($i=0; $i<$group_cnt; $i++)
{
$group_name = $groups[$i];
//prevent empty line at beginning of ini-file
if ($i==0)
{
$content = sprintf("[%s]\n",$group_name);
}
else
{
$content .= sprintf("\n[%s]\n",$group_name);
}
$group = $this->readGroup($group_name);
//go through group an display all variables
for (reset($group); $key=key($group);next($group))
{
$content .= sprintf("%s = %s\n",$key,$group[$key]);
}
}
return $content;
}

+ Here is the call graph for this function:

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.

{
return isset($this->GROUPS[$a_group][$a_var_name]);
}
ilIniFile::write ( )

save ini-file-data to filesystem private

Returns
boolean

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

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

{
$fp = @fopen($this->INI_FILE_NAME,"w");
if (empty($fp))
{
$this->error("Cannot create file $this->INI_FILE_NAME");
return false;
}
//write php tags (security issue)
$result = fwrite($fp, "; <?php exit; ?>\r\n");
$groups = $this->readGroups();
$group_cnt = count($groups);
for ($i=0; $i<$group_cnt; $i++)
{
$group_name = $groups[$i];
//prevent empty line at beginning of ini-file
if ($i==0)
{
$res = sprintf("[%s]\r\n",$group_name);
}
else
{
$res = sprintf("\r\n[%s]\r\n",$group_name);
}
$result = fwrite($fp, $res);
$group = $this->readGroup($group_name);
for (reset($group); $key=key($group);next($group))
{
$res = sprintf("%s = %s\r\n",$key,"\"".$group[$key]."\"");
$result = fwrite($fp,$res);
}
}
fclose($fp);
return true;
}

+ Here is the call graph for this function:

Field Documentation

ilIniFile::$CURRENT_GROUP = ""

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

Referenced by parse_data().

ilIniFile::$ERROR = ""

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

Referenced by getError().

ilIniFile::$GROUPS = array()

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

ilIniFile::$INI_FILE_NAME = ""

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


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