ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilIniFile.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
38{
44 public $INI_FILE_NAME = "";
45
51 public $ERROR = "";
52
58 public $GROUPS = array();
59
65 public $CURRENT_GROUP = "";
66
73 public function __construct($a_ini_file_name)
74 {
75 //check if a filename is given
76 if (empty($a_ini_file_name)) {
77 $this->error("no_file_given");
78 return false;
79 }
80
81 $this->INI_FILE_NAME = $a_ini_file_name;
82 return true;
83 }
84
90 public function read()
91 {
92 //check if file exists
93 if (!file_exists($this->INI_FILE_NAME)) {
94 $this->error("file_does_not_exist");
95 return false;
96 } else {
97 //parse the file
98 if ($this->parse() == false) {
99 return false;
100 }
101 }
102
103 return true;
104 }
105
111 public function parse()
112 {
113 $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
114
115 //check if groups are filled
116 if ($this->GROUPS == false) {
117 $this->error("file_not_accessible");
118 return false;
119
120 }
121 //set current group
122 $temp = array_keys($this->GROUPS);
123 $this->CURRENT_GROUP = $temp[count($temp) - 1];
124 return true;
125 }
126
132 public function write()
133 {
134 $fp = @fopen($this->INI_FILE_NAME, "w");
135
136 if (empty($fp)) {
137 $this->error("Cannot create file $this->INI_FILE_NAME");
138 return false;
139 }
140
141 //write php tags (security issue)
142 $result = fwrite($fp, "; <?php exit; ?>\r\n");
143
144 $groups = $this->readGroups();
145 $group_cnt = count($groups);
146
147 for ($i = 0; $i < $group_cnt; $i++) {
148 $group_name = $groups[$i];
149 //prevent empty line at beginning of ini-file
150 if ($i == 0) {
151 $res = sprintf("[%s]\r\n", $group_name);
152 } else {
153 $res = sprintf("\r\n[%s]\r\n", $group_name);
154 }
155
156 $result = fwrite($fp, $res);
157 $group = $this->readGroup($group_name);
158
159 for (reset($group); $key = key($group);next($group)) {
160 $res = sprintf("%s = %s\r\n", $key, "\"" . $group[$key] . "\"");
161 $result = fwrite($fp, $res);
162 }
163 }
164
165
166 fclose($fp);
167
168 return true;
169 }
170
176 public function show()
177 {
178 $groups = $this->readGroups();
179 $group_cnt = count($groups);
180
181 //clear content
182 $content = "";
183
184 // go through all groups
185 for ($i = 0; $i < $group_cnt; $i++) {
186 $group_name = $groups[$i];
187 //prevent empty line at beginning of ini-file
188 if ($i == 0) {
189 $content = sprintf("[%s]\n", $group_name);
190 } else {
191 $content .= sprintf("\n[%s]\n", $group_name);
192 }
193
194 $group = $this->readGroup($group_name);
195
196 //go through group an display all variables
197 for (reset($group); $key = key($group);next($group)) {
198 $content .= sprintf("%s = %s\n", $key, $group[$key]);
199 }
200 }
201
202 return $content;
203 }
204
210 public function getGroupCount()
211 {
212 return count($this->GROUPS);
213 }
214
220 public function readGroups()
221 {
222 $groups = array();
223
224 for (reset($this->GROUPS);$key = key($this->GROUPS);next($this->GROUPS)) {
225 $groups[] = $key;
226 }
227
228 return $groups;
229 }
230
237 public function groupExists($a_group_name)
238 {
239 if (!isset($this->GROUPS[$a_group_name])) {
240 return false;
241 }
242
243 return true;
244 }
245
252 public function readGroup($a_group_name)
253 {
254 if (!$this->groupExists($a_group_name)) {
255 $this->error("Group '" . $a_group_name . "' does not exist");
256 return false;
257 }
258
259 return $this->GROUPS[$a_group_name];
260 }
261
268 public function addGroup($a_group_name)
269 {
270 if ($this->groupExists($a_group_name)) {
271 $this->error("Group '" . $a_group_name . "' exists");
272 return false;
273 }
274
275 $this->GROUPS[$a_group_name] = array();
276 return true;
277 }
278
285 public function removeGroup($a_group_name)
286 {
287 if (!$this->groupExists($a_group_name)) {
288 $this->error("Group '" . $a_group_name . "' does not exist");
289 return false;
290 }
291
292 unset($this->GROUPS[$a_group_name]);
293 return true;
294 }
295
303 public function variableExists($a_group, $a_var_name)
304 {
305 return isset($this->GROUPS[$a_group][$a_var_name]);
306 }
307
308
316 public function readVariable($a_group, $a_var_name)
317 {
318 if (!isset($this->GROUPS[$a_group][$a_var_name])) {
319 $this->error("'" . $a_var_name . "' does not exist in '" . $a_group . "'");
320 return false;
321 }
322
323 return trim($this->GROUPS[$a_group][$a_var_name]);
324 }
325
334 public function setVariable($a_group_name, $a_var_name, $a_var_value)
335 {
336 if (!$this->groupExists($a_group_name)) {
337 $this->error("Group '" . $a_group_name . "' does not exist");
338 return false;
339 }
340
341 $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
342 return true;
343 }
344
350 public function error($a_errmsg)
351 {
352 $this->ERROR = $a_errmsg;
353
354 return true;
355 }
356
362 public function getError()
363 {
364 return $this->ERROR;
365 }
366} //END class.ilIniFile
$result
An exception for terminatinating execution or to throw for unit testing.
getError()
returns error @access public
show()
returns the content of IniFile @access public
readGroups()
returns an array with the names of all the groups @access public
variableExists($a_group, $a_var_name)
returns if a variable exists or not @access public
getGroupCount()
returns number of groups @access public
removeGroup($a_group_name)
removes a group @access public
readGroup($a_group_name)
returns an associative array of the variables in one group @access public
addGroup($a_group_name)
adds a new group @access public
readVariable($a_group, $a_var_name)
reads a single variable from a group @access public
setVariable($a_group_name, $a_var_name, $a_var_value)
sets a variable in a group @access public
groupExists($a_group_name)
checks if a group exists @access public
error($a_errmsg)
set error message @access public
INIFile Parser.
write()
save ini-file-data to filesystem @access private
parse()
load and parse an inifile @access private
__construct($a_ini_file_name)
Constructor @access public.
read()
read from ini file @access public
$i
Definition: metadata.php:24
foreach($_POST as $key=> $value) $res