ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilIniFile.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
54 class ilIniFile
55 {
61  var $INI_FILE_NAME = "";
62 
68  var $ERROR = "";
69 
75  var $GROUPS = array();
76 
82  var $CURRENT_GROUP = "";
83 
90  function ilIniFile($a_ini_file_name)
91  {
92  //check if a filename is given
93  if (empty($a_ini_file_name))
94  {
95  $this->error("no_file_given");
96  return false;
97  }
98 
99  $this->INI_FILE_NAME = $a_ini_file_name;
100  return true;
101  }
102 
108  function read()
109  {
110  //check if file exists
111  if (!file_exists($this->INI_FILE_NAME))
112  {
113  $this->error("file_does_not_exist");
114  return false;
115  }
116  else
117  {
118  //parse the file
119  if ($this->parse() == false)
120  {
121  return false;
122  }
123  }
124 
125  return true;
126  }
127 
133  function parse()
134  {
135  //use php4 function parse_ini_file
136  $this->GROUPS = parse_ini_file($this->INI_FILE_NAME, true);
137 
138  //check if groups are filled
139  if ($this->GROUPS == false)
140  {
141  $this->error("file_not_accessible");
142  return false;
143  }
144  //set current group
145  $temp = array_keys($this->GROUPS);
146  $this->CURRENT_GROUP = $temp[count($temp)-1];
147  return true;
148  }
149 
155  function parse_data($a_data)
156  {
157  if (ereg("\[([[:alnum:]]+)\]",$a_data,$out))
158  {
159  $this->CURRENT_GROUP= trim($out[1]);
160  }
161  elseif (!empty($a_data))
162  {
163  $split_data = split("=", $a_data);
164  $this->GROUPS[$this->CURRENT_GROUP][trim($split_data[0])]=trim($split_data[1]);
165  }
166  }
167 
174  function setContent($a_data)
175  {
176  $this->GROUPS = $a_data;
177  return true;
178  }
179 
185  function write()
186  {
187  $fp = @fopen($this->INI_FILE_NAME,"w");
188 
189  if (empty($fp))
190  {
191  $this->error("Cannot create file $this->INI_FILE_NAME");
192  return false;
193  }
194 
195  //write php tags (security issue)
196  $result = fwrite($fp, "; <?php exit; ?>\r\n");
197 
198  $groups = $this->readGroups();
199  $group_cnt = count($groups);
200 
201  for ($i=0; $i<$group_cnt; $i++)
202  {
203  $group_name = $groups[$i];
204  //prevent empty line at beginning of ini-file
205  if ($i==0)
206  {
207  $res = sprintf("[%s]\r\n",$group_name);
208  }
209  else
210  {
211  $res = sprintf("\r\n[%s]\r\n",$group_name);
212  }
213 
214  $result = fwrite($fp, $res);
215  $group = $this->readGroup($group_name);
216 
217  for (reset($group); $key=key($group);next($group))
218  {
219  $res = sprintf("%s = %s\r\n",$key,"\"".$group[$key]."\"");
220  $result = fwrite($fp,$res);
221  }
222  }
223 
224 
225  fclose($fp);
226 
227  return true;
228  }
229 
235  function show()
236  {
237  $groups = $this->readGroups();
238  $group_cnt = count($groups);
239 
240  //clear content
241  $content = "";
242 
243  // go through all groups
244  for ($i=0; $i<$group_cnt; $i++)
245  {
246  $group_name = $groups[$i];
247  //prevent empty line at beginning of ini-file
248  if ($i==0)
249  {
250  $content = sprintf("[%s]\n",$group_name);
251  }
252  else
253  {
254  $content .= sprintf("\n[%s]\n",$group_name);
255  }
256 
257  $group = $this->readGroup($group_name);
258 
259  //go through group an display all variables
260  for (reset($group); $key=key($group);next($group))
261  {
262  $content .= sprintf("%s = %s\n",$key,$group[$key]);
263  }
264  }
265 
266  return $content;
267  }
268 
274  function getGroupCount()
275  {
276  return count($this->GROUPS);
277  }
278 
284  function readGroups()
285  {
286  $groups = array();
287 
288  for (reset($this->GROUPS);$key=key($this->GROUPS);next($this->GROUPS))
289  {
290  $groups[]=$key;
291  }
292 
293  return $groups;
294  }
295 
302  function groupExists($a_group_name)
303  {
304  if (!isset($this->GROUPS[$a_group_name]))
305  {
306  return false;
307  }
308 
309  return true;
310  }
311 
318  function readGroup($a_group_name)
319  {
320  if (!$this->groupExists($a_group_name))
321  {
322  $this->error("Group '".$a_group_name."' does not exist");
323  return false;
324  }
325 
326  return $this->GROUPS[$a_group_name];
327  }
328 
335  function addGroup($a_group_name)
336  {
337  if ($this->groupExists($a_group_name))
338  {
339  $this->error("Group '".$a_group_name."' exists");
340  return false;
341  }
342 
343  $this->GROUPS[$a_group_name] = array();
344  return true;
345  }
346 
353  function removeGroup($a_group_name)
354  {
355  if (!$this->groupExists($a_group_name))
356  {
357  $this->error("Group '".$a_group_name."' does not exist");
358  return false;
359  }
360 
361  unset($this->GROUPS[$a_group_name]);
362  return true;
363  }
364 
372  function variableExists($a_group, $a_var_name)
373  {
374  return isset($this->GROUPS[$a_group][$a_var_name]);
375  }
376 
377 
385  function readVariable($a_group, $a_var_name)
386  {
387  if (!isset($this->GROUPS[$a_group][$a_var_name]))
388  {
389  $this->error("'".$a_var_name."' does not exist in '".$a_group."'");
390  return false;
391  }
392 
393  return trim($this->GROUPS[$a_group][$a_var_name]);
394  }
395 
404  function setVariable($a_group_name, $a_var_name, $a_var_value)
405  {
406  if (!$this->groupExists($a_group_name))
407  {
408  $this->error("Group '".$a_group_name."' does not exist");
409  return false;
410  }
411 
412  $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
413  return true;
414  }
415 
421  function error($a_errmsg)
422  {
423  $this->ERROR = $a_errmsg;
424 
425  return true;
426  }
427 
433  function getError()
434  {
435  return $this->ERROR;
436  }
437 } //END class.ilIniFile
438 ?>