ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilIniFile.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
48{
52 public string $INI_FILE_NAME = "";
53
57 public string $ERROR = "";
58
62 public array $GROUPS = array();
63
67 public string $CURRENT_GROUP = "";
68
72 public function __construct(string $a_ini_file_name)
73 {
74 //check if a filename is given
75 if (empty($a_ini_file_name)) {
76 $this->error("no_file_given");
77 }
78
79 $this->INI_FILE_NAME = $a_ini_file_name;
80 }
81
85 public function read(): bool
86 {
87 //check if file exists
88 if (!file_exists($this->INI_FILE_NAME)) {
89 $this->error("file_does_not_exist");
90 return false;
91 }
92 if (!$this->parse()) {
93 //parse the file
94 return false;
95 }
96 return true;
97 }
98
102 public function parse(): bool
103 {
104 try {
105 $ini_file_readable = is_readable($this->INI_FILE_NAME);
106 if (!$ini_file_readable) {
107 $this->error("file_not_accessible");
108 return false;
109 }
110 $this->GROUPS = parse_ini_file($this->INI_FILE_NAME, true);
111 if (!$this->GROUPS) {
112 $this->error("error_parseing_inifile");
113 return false;
114 }
115 } catch (Exception $e) {
116 $this->error($e->getMessage());
117 return false;
118 }
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
130 public function write(): bool
131 {
132 $fp = fopen($this->INI_FILE_NAME, "wb");
133
134 if (empty($fp)) {
135 $this->error("Cannot create file $this->INI_FILE_NAME");
136 return false;
137 }
138
139 //write php tags (security issue)
140 $result = fwrite($fp, "; <?php exit; ?>\r\n");
141
142 $groups = $this->readGroups();
143 $group_cnt = count($groups);
144
145 for ($i = 0; $i < $group_cnt; $i++) {
146 $group_name = $groups[$i];
147 //prevent empty line at beginning of ini-file
148 if ($i === 0) {
149 $res = sprintf("[%s]\r\n", $group_name);
150 } else {
151 $res = sprintf("\r\n[%s]\r\n", $group_name);
152 }
153
154 $result = fwrite($fp, $res);
155 $group = $this->readGroup($group_name);
156
157 for (reset($group); $key = key($group); next($group)) {
158 $res = sprintf("%s = %s\r\n", $key, "\"" . $group[$key] . "\"");
159 $result = fwrite($fp, $res);
160 }
161 }
162
163 fclose($fp);
164 return true;
165 }
166
170 public function show(): string
171 {
172 $groups = $this->readGroups();
173 $group_cnt = count($groups);
174
175 //clear content
176 $content = "";
177
178 // go through all groups
179 for ($i = 0; $i < $group_cnt; $i++) {
180 $group_name = $groups[$i];
181 //prevent empty line at beginning of ini-file
182 if ($i === 0) {
183 $content = sprintf("[%s]\n", $group_name);
184 } else {
185 $content .= sprintf("\n[%s]\n", $group_name);
186 }
187
188 $group = $this->readGroup($group_name);
189
190 //go through group an display all variables
191 for (reset($group); $key = key($group); next($group)) {
192 $content .= sprintf("%s = %s\n", $key, $group[$key]);
193 }
194 }
195
196 return $content;
197 }
198
202 public function getGroupCount(): int
203 {
204 return count($this->GROUPS);
205 }
206
210 public function readGroups(): array
211 {
212 $groups = array();
213
214 for (reset($this->GROUPS); $key = key($this->GROUPS); next($this->GROUPS)) {
215 $groups[] = $key;
216 }
217
218 return $groups;
219 }
220
224 public function groupExists(string $a_group_name): bool
225 {
226 if (!isset($this->GROUPS[$a_group_name])) {
227 return false;
228 }
229
230 return true;
231 }
232
236 public function readGroup(string $a_group_name): array
237 {
238 if (!$this->groupExists($a_group_name)) {
239 $this->error("Group '" . $a_group_name . "' does not exist");
240 return [];
241 }
242
243 return $this->GROUPS[$a_group_name];
244 }
245
249 public function addGroup(string $a_group_name): bool
250 {
251 if ($this->groupExists($a_group_name)) {
252 $this->error("Group '" . $a_group_name . "' exists");
253 return false;
254 }
255
256 $this->GROUPS[$a_group_name] = array();
257 return true;
258 }
259
263 public function removeGroup(string $a_group_name): bool
264 {
265 if (!$this->groupExists($a_group_name)) {
266 $this->error("Group '" . $a_group_name . "' does not exist");
267 return false;
268 }
269
270 unset($this->GROUPS[$a_group_name]);
271 return true;
272 }
273
277 public function variableExists(string $a_group, string $a_var_name): bool
278 {
279 return isset($this->GROUPS[$a_group][$a_var_name]);
280 }
281
285 public function readVariable(string $a_group, string $a_var_name): string
286 {
287 if (!isset($this->GROUPS[$a_group][$a_var_name])) {
288 $this->error("'" . $a_var_name . "' does not exist in '" . $a_group . "'");
289 return '';
290 }
291
292 return trim($this->GROUPS[$a_group][$a_var_name]);
293 }
294
298 public function setVariable(string $a_group_name, string $a_var_name, string $a_var_value): bool
299 {
300 if (!$this->groupExists($a_group_name)) {
301 $this->error("Group '" . $a_group_name . "' does not exist");
302 return false;
303 }
304
305 $this->GROUPS[$a_group_name][$a_var_name] = $a_var_value;
306 return true;
307 }
308
309 public function error(string $a_errmsg): bool
310 {
311 $this->ERROR = $a_errmsg;
312
313 return true;
314 }
315
316 public function getError(): string
317 {
318 return $this->ERROR;
319 }
320} //END class.ilIniFile
variableExists(string $a_group, string $a_var_name)
returns if a variable exists or not
getError()
show()
returns the content of IniFile
readGroups()
returns an array with the names of all the groups
getGroupCount()
returns number of groups
error(string $a_errmsg)
removeGroup(string $a_group_name)
removes a group
readGroup(string $a_group_name)
returns an associative array of the variables in one group
setVariable(string $a_group_name, string $a_var_name, string $a_var_value)
sets a variable in a group
readVariable(string $a_group, string $a_var_name)
reads a single variable from a group
groupExists(string $a_group_name)
checks if a group exists
addGroup(string $a_group_name)
adds a new group
INIFile Parser Early access in init proceess! Avoid further dependencies like logging or other servic...
string $ERROR
error var
write()
save ini-file-data to filesystem
__construct(string $a_ini_file_name)
Constructor.
parse()
load and parse an inifile
string $CURRENT_GROUP
actual section
read()
read from ini file
string $INI_FILE_NAME
name of file
array $GROUPS
sections in ini-file
$res
Definition: ltiservices.php:69