Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00054 class ilIniFile
00055 {
00061 var $INI_FILE_NAME = "";
00062
00068 var $ERROR = "";
00069
00075 var $GROUPS = array();
00076
00082 var $CURRENT_GROUP = "";
00083
00090 function ilIniFile($a_ini_file_name)
00091 {
00092
00093 if (empty($a_ini_file_name))
00094 {
00095 $this->error("no_file_given");
00096 return false;
00097 }
00098
00099 $this->INI_FILE_NAME = $a_ini_file_name;
00100 return true;
00101 }
00102
00108 function read()
00109 {
00110
00111 if (!file_exists($this->INI_FILE_NAME))
00112 {
00113 $this->error("file_does_not_exist");
00114 return false;
00115 }
00116 else
00117 {
00118
00119 if ($this->parse() == false)
00120 {
00121 return false;
00122 }
00123 }
00124
00125 return true;
00126 }
00127
00133 function parse()
00134 {
00135
00136 $this->GROUPS = parse_ini_file($this->INI_FILE_NAME, true);
00137
00138
00139 if ($this->GROUPS == false)
00140 {
00141 $this->error("file_not_accessible");
00142 return false;
00143 }
00144
00145 $temp = array_keys($this->GROUPS);
00146 $this->CURRENT_GROUP = $temp[count($temp)-1];
00147 return true;
00148 }
00149
00155 function parse_data($a_data)
00156 {
00157 if (ereg("\[([[:alnum:]]+)\]",$a_data,$out))
00158 {
00159 $this->CURRENT_GROUP= trim($out[1]);
00160 }
00161 elseif (!empty($a_data))
00162 {
00163 $split_data = split("=", $a_data);
00164 $this->GROUPS[$this->CURRENT_GROUP][trim($split_data[0])]=trim($split_data[1]);
00165 }
00166 }
00167
00174 function setContent($a_data)
00175 {
00176 $this->GROUPS = $a_data;
00177 return true;
00178 }
00179
00185 function write()
00186 {
00187 $fp = @fopen($this->INI_FILE_NAME,"w");
00188
00189 if (empty($fp))
00190 {
00191 $this->error("Cannot create file $this->INI_FILE_NAME");
00192 return false;
00193 }
00194
00195
00196 $result = fwrite($fp, "<?php /*\r\n");
00197
00198 $groups = $this->readGroups();
00199 $group_cnt = count($groups);
00200
00201 for ($i=0; $i<$group_cnt; $i++)
00202 {
00203 $group_name = $groups[$i];
00204
00205 if ($i==0)
00206 {
00207 $res = sprintf("[%s]\r\n",$group_name);
00208 }
00209 else
00210 {
00211 $res = sprintf("\r\n[%s]\r\n",$group_name);
00212 }
00213
00214 $result = fwrite($fp, $res);
00215 $group = $this->readGroup($group_name);
00216
00217 for (reset($group); $key=key($group);next($group))
00218 {
00219 $res = sprintf("%s = %s\r\n",$key,"\"".$group[$key]."\"");
00220 $result = fwrite($fp,$res);
00221 }
00222 }
00223
00224
00225 $result = fwrite($fp, "*/ ?>");
00226
00227 fclose($fp);
00228
00229 return true;
00230 }
00231
00237 function show()
00238 {
00239 $groups = $this->readGroups();
00240 $group_cnt = count($groups);
00241
00242
00243 $content = "";
00244
00245
00246 for ($i=0; $i<$group_cnt; $i++)
00247 {
00248 $group_name = $groups[$i];
00249
00250 if ($i==0)
00251 {
00252 $content = sprintf("[%s]\n",$group_name);
00253 }
00254 else
00255 {
00256 $content .= sprintf("\n[%s]\n",$group_name);
00257 }
00258
00259 $group = $this->readGroup($group_name);
00260
00261
00262 for (reset($group); $key=key($group);next($group))
00263 {
00264 $content .= sprintf("%s = %s\n",$key,$group[$key]);
00265 }
00266 }
00267
00268 return $content;
00269 }
00270
00276 function getGroupCount()
00277 {
00278 return count($this->GROUPS);
00279 }
00280
00286 function readGroups()
00287 {
00288 $groups = array();
00289
00290 for (reset($this->GROUPS);$key=key($this->GROUPS);next($this->GROUPS))
00291 {
00292 $groups[]=$key;
00293 }
00294
00295 return $groups;
00296 }
00297
00304 function groupExists($a_group_name)
00305 {
00306 if (!isset($this->GROUPS[$a_group_name]))
00307 {
00308 return false;
00309 }
00310
00311 return true;
00312 }
00313
00320 function readGroup($a_group_name)
00321 {
00322 if (!$this->groupExists($a_group_name))
00323 {
00324 $this->error("Group '".$a_group_name."' does not exist");
00325 return false;
00326 }
00327
00328 return $this->GROUPS[$a_group_name];
00329 }
00330
00337 function addGroup($a_group_name)
00338 {
00339 if ($this->groupExists($a_group_name))
00340 {
00341 $this->error("Group '".$a_group_name."' exists");
00342 return false;
00343 }
00344
00345 $this->GROUPS[$a_group_name] = array();
00346 return true;
00347 }
00348
00355 function removeGroup($a_group_name)
00356 {
00357 if (!$this->groupExists($a_group_name))
00358 {
00359 $this->error("Group '".$a_group_name."' does not exist");
00360 return false;
00361 }
00362
00363 unset($this->GROUPS[$a_group_name]);
00364 return true;
00365 }
00366
00374 function readVariable($a_group, $a_var_name)
00375 {
00376 if (!isset($this->GROUPS[$a_group][$a_var_name]))
00377 {
00378 $this->error("'".$a_var_name."' does not exist in '".$a_group."'");
00379 return false;
00380 }
00381
00382 return trim($this->GROUPS[$a_group][$a_var_name]);
00383 }
00384
00393 function setVariable($a_group_name, $a_var_name, $a_var_value)
00394 {
00395 if (!$this->groupExists($a_group_name))
00396 {
00397 $this->error("Group '".$a_group_name."' does not exist");
00398 return false;
00399 }
00400
00401 $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
00402 return true;
00403 }
00404
00410 function error($a_errmsg)
00411 {
00412 $this->ERROR = $a_errmsg;
00413
00414 return true;
00415 }
00416
00422 function getError()
00423 {
00424 return $this->ERROR;
00425 }
00426 }
00427 ?>