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