ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 //use php4 function parse_ini_file
114 $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
115
116 //check if groups are filled
117 if ($this->GROUPS == false) {
118 // second try
119 $this->fixIniFile();
120
121 $this->GROUPS = @parse_ini_file($this->INI_FILE_NAME, true);
122 if ($this->GROUPS == false) {
123 $this->error("file_not_accessible");
124 return false;
125 }
126 }
127 //set current group
128 $temp = array_keys($this->GROUPS);
129 $this->CURRENT_GROUP = $temp[count($temp) - 1];
130 return true;
131 }
132
136 public function fixIniFile()
137 {
138 // first read content
139 $lines = array();
140 $fp = @fopen($this->INI_FILE_NAME, "r");
141 while (!feof($fp)) {
142 $l = fgets($fp, 4096);
143 $skip = false;
144 if ((substr($l, 0, 2) == "/*" && $starttag) ||
145 substr($l, 0, 5) == "*/ ?>") {
146 $skip = true;
147 }
148 $starttag = false;
149 if (substr($l, 0, 5) == "<?php") {
150 $l = "; <?php exit; ?>";
151 $starttag = true;
152 }
153 if (!$skip) {
154 $l = str_replace("\n", "", $l);
155 $l = str_replace("\r", "", $l);
156 $lines[] = $l;
157 }
158 }
159 fclose($fp);
160
161 // now write it back
162 $fp = @fopen($this->INI_FILE_NAME, "w");
163
164 if (!empty($fp)) {
165 foreach ($lines as $l) {
166 fwrite($fp, $l . "\r\n");
167 }
168 }
169 fclose($fp);
170 }
171
177 public function write()
178 {
179 $fp = @fopen($this->INI_FILE_NAME, "w");
180
181 if (empty($fp)) {
182 $this->error("Cannot create file $this->INI_FILE_NAME");
183 return false;
184 }
185
186 //write php tags (security issue)
187 $result = fwrite($fp, "; <?php exit; ?>\r\n");
188
189 $groups = $this->readGroups();
190 $group_cnt = count($groups);
191
192 for ($i = 0; $i < $group_cnt; $i++) {
193 $group_name = $groups[$i];
194 //prevent empty line at beginning of ini-file
195 if ($i == 0) {
196 $res = sprintf("[%s]\r\n", $group_name);
197 } else {
198 $res = sprintf("\r\n[%s]\r\n", $group_name);
199 }
200
201 $result = fwrite($fp, $res);
202 $group = $this->readGroup($group_name);
203
204 for (reset($group); $key = key($group);next($group)) {
205 $res = sprintf("%s = %s\r\n", $key, "\"" . $group[$key] . "\"");
206 $result = fwrite($fp, $res);
207 }
208 }
209
210
211 fclose($fp);
212
213 return true;
214 }
215
221 public function show()
222 {
223 $groups = $this->readGroups();
224 $group_cnt = count($groups);
225
226 //clear content
227 $content = "";
228
229 // go through all groups
230 for ($i = 0; $i < $group_cnt; $i++) {
231 $group_name = $groups[$i];
232 //prevent empty line at beginning of ini-file
233 if ($i == 0) {
234 $content = sprintf("[%s]\n", $group_name);
235 } else {
236 $content .= sprintf("\n[%s]\n", $group_name);
237 }
238
239 $group = $this->readGroup($group_name);
240
241 //go through group an display all variables
242 for (reset($group); $key = key($group);next($group)) {
243 $content .= sprintf("%s = %s\n", $key, $group[$key]);
244 }
245 }
246
247 return $content;
248 }
249
255 public function getGroupCount()
256 {
257 return count($this->GROUPS);
258 }
259
265 public function readGroups()
266 {
267 $groups = array();
268
269 for (reset($this->GROUPS);$key = key($this->GROUPS);next($this->GROUPS)) {
270 $groups[] = $key;
271 }
272
273 return $groups;
274 }
275
282 public function groupExists($a_group_name)
283 {
284 if (!isset($this->GROUPS[$a_group_name])) {
285 return false;
286 }
287
288 return true;
289 }
290
297 public function readGroup($a_group_name)
298 {
299 if (!$this->groupExists($a_group_name)) {
300 $this->error("Group '" . $a_group_name . "' does not exist");
301 return false;
302 }
303
304 return $this->GROUPS[$a_group_name];
305 }
306
313 public function addGroup($a_group_name)
314 {
315 if ($this->groupExists($a_group_name)) {
316 $this->error("Group '" . $a_group_name . "' exists");
317 return false;
318 }
319
320 $this->GROUPS[$a_group_name] = array();
321 return true;
322 }
323
330 public function removeGroup($a_group_name)
331 {
332 if (!$this->groupExists($a_group_name)) {
333 $this->error("Group '" . $a_group_name . "' does not exist");
334 return false;
335 }
336
337 unset($this->GROUPS[$a_group_name]);
338 return true;
339 }
340
348 public function variableExists($a_group, $a_var_name)
349 {
350 return isset($this->GROUPS[$a_group][$a_var_name]);
351 }
352
353
361 public function readVariable($a_group, $a_var_name)
362 {
363 if (!isset($this->GROUPS[$a_group][$a_var_name])) {
364 $this->error("'" . $a_var_name . "' does not exist in '" . $a_group . "'");
365 return false;
366 }
367
368 return trim($this->GROUPS[$a_group][$a_var_name]);
369 }
370
379 public function setVariable($a_group_name, $a_var_name, $a_var_value)
380 {
381 if (!$this->groupExists($a_group_name)) {
382 $this->error("Group '" . $a_group_name . "' does not exist");
383 return false;
384 }
385
386 $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
387 return true;
388 }
389
395 public function error($a_errmsg)
396 {
397 $this->ERROR = $a_errmsg;
398
399 return true;
400 }
401
407 public function getError()
408 {
409 return $this->ERROR;
410 }
411} //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.
fixIniFile()
Fix ini file (make it compatible for PHP 5.3)
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