ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCtrlStructure.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
32  private const PARAM_NAME_REGEX = '/^[A-Za-z0-9_-]*$/';
33 
39  private array $temporary_parameters = [];
40 
46  private array $permanent_parameters = [];
47 
52  private array $return_targets = [];
53 
58  private array $structure;
59 
64  private array $base_classes;
65 
70  private array $security;
71 
77  private array $mapped_structure = [];
78 
85  public function __construct(
86  array $ctrl_structure,
87  array $base_classes,
88  array $security_info
89  ) {
90  $this->base_classes = $base_classes;
91  $this->security = $security_info;
92  $this->structure = $ctrl_structure;
93  }
94 
98  public function isBaseClass(string $class_name): bool
99  {
100  // baseclass must be contained within the current structure
101  // and within the current baseclass array.
102  return
103  null !== $this->getClassCidByName($class_name) &&
104  in_array($this->lowercase($class_name), $this->base_classes, true);
105  }
106 
110  public function getObjNameByCid(string $cid): ?string
111  {
112  return $this->getValueForKeyByCid(self::KEY_CLASS_NAME, $cid);
113  }
114 
118  public function getObjNameByName(string $class_name): ?string
119  {
120  return $this->getValueForKeyByName(self::KEY_CLASS_NAME, $class_name);
121  }
122 
126  public function getClassNameByCid(string $cid): ?string
127  {
128  $class_name = $this->getValueForKeyByCid(
129  self::KEY_CLASS_NAME,
130  $cid
131  );
132 
133  return (null !== $class_name) ? $this->lowercase($class_name) : null;
134  }
135 
139  public function getClassCidByName(string $class_name): ?string
140  {
141  return $this->getValueForKeyByName(self::KEY_CLASS_CID, $class_name);
142  }
143 
147  public function getRelativePathByName(string $class_name): ?string
148  {
149  return $this->getValueForKeyByName(self::KEY_CLASS_PATH, $class_name);
150  }
151 
155  public function getRelativePathByCid(string $cid): ?string
156  {
157  return $this->getValueForKeyByCid(self::KEY_CLASS_PATH, $cid);
158  }
159 
163  public function getChildrenByCid(string $cid): ?array
164  {
165  $children = $this->getValueForKeyByCid(self::KEY_CLASS_CHILDREN, $cid);
166  if (empty($children)) {
167  return null;
168  }
169 
170  return $children;
171  }
172 
176  public function getChildrenByName(string $class_name): ?array
177  {
178  $children = $this->getValueForKeyByName(self::KEY_CLASS_CHILDREN, $class_name);
179  if (empty($children)) {
180  return null;
181  }
182 
183  return $children;
184  }
185 
189  public function getParentsByCid(string $cid): ?array
190  {
191  $parents = $this->getValueForKeyByCid(self::KEY_CLASS_PARENTS, $cid);
192  if (empty($parents)) {
193  return null;
194  }
195 
196  return $parents;
197  }
198 
202  public function getParentsByName(string $class_name): ?array
203  {
204  $parents = $this->getValueForKeyByName(self::KEY_CLASS_PARENTS, $class_name);
205  if (empty($parents)) {
206  return null;
207  }
208 
209  return $parents;
210  }
211 
215  public function setPermanentParameterByClass(string $class_name, string $parameter_name): void
216  {
217  if (in_array($parameter_name, ilCtrlInterface::PROTECTED_PARAMETERS, true)) {
218  throw new ilCtrlException("Parameter '$parameter_name' must not be saved, it could mess with the control flow.");
219  }
220 
221  if (!preg_match(self::PARAM_NAME_REGEX, $parameter_name)) {
222  throw new ilCtrlException("Cannot save parameter '$parameter_name', as it contains invalid characters.");
223  }
224 
225  $this->permanent_parameters[$this->lowercase($class_name)][] = $parameter_name;
226  }
227 
231  public function removePermanentParametersByClass(string $class_name): void
232  {
233  $class_name = $this->lowercase($class_name);
234  if (isset($this->permanent_parameters[$class_name])) {
235  unset($this->permanent_parameters[$class_name]);
236  }
237  }
238 
242  public function getPermanentParametersByClass(string $class_name): ?array
243  {
244  return $this->permanent_parameters[$this->lowercase($class_name)] ?? null;
245  }
246 
250  public function setTemporaryParameterByClass(string $class_name, string $parameter_name, $value): void
251  {
252  if (!preg_match(self::PARAM_NAME_REGEX, $parameter_name)) {
253  throw new ilCtrlException("Cannot save parameter '$parameter_name', as it contains invalid characters.");
254  }
255 
256  $this->temporary_parameters[$this->lowercase($class_name)][$parameter_name] = $value;
257  }
258 
262  public function removeTemporaryParametersByClass(string $class_name): void
263  {
264  $class_name = $this->lowercase($class_name);
265  if (isset($this->temporary_parameters[$class_name])) {
266  unset($this->temporary_parameters[$class_name]);
267  }
268  }
269 
273  public function getTemporaryParametersByClass(string $class_name): ?array
274  {
275  return $this->temporary_parameters[$this->lowercase($class_name)] ?? null;
276  }
277 
281  public function removeSingleParameterByClass(string $class_name, string $parameter_name): void
282  {
283  $class_name = $this->lowercase($class_name);
284 
285  // permanent parameters are lists of parameter names
286  // mapped to the classname, therefore the index is
287  // unknown and has to be figured out.
288  if (!empty($this->permanent_parameters[$class_name])) {
289  foreach ($this->permanent_parameters[$class_name] as $index => $permanent_parameter) {
290  if ($parameter_name === $permanent_parameter) {
291  unset($this->permanent_parameters[$class_name][$index]);
292 
293  // reindex the array values.
294  $permanent_parameters = &$this->permanent_parameters[$class_name];
295  $permanent_parameters = array_values($permanent_parameters);
296  }
297  }
298  }
299 
300  // the temporary parameters are key => value pairs mapped
301  // to the classname, whereas key is the parameter name.
302  // The index is therefore known and can be unset directly.
303  if (isset($this->temporary_parameters[$class_name])) {
304  unset($this->temporary_parameters[$class_name][$parameter_name]);
305  }
306  }
307 
311  public function setReturnTargetByClass(string $class_name, string $target_url): void
312  {
313  $this->return_targets[$this->lowercase($class_name)] = $target_url;
314  }
315 
319  public function getReturnTargetByClass(string $class_name): ?string
320  {
321  return $this->return_targets[$this->lowercase($class_name)] ?? null;
322  }
323 
327  public function getUnsafeCommandsByCid(string $cid): array
328  {
329  $class_name = $this->getClassNameByCid($cid);
330  if (null !== $class_name) {
331  return $this->getUnsafeCommandsByName($class_name);
332  }
333 
334  return [];
335  }
336 
340  public function getUnsafeCommandsByName(string $class_name): array
341  {
342  return $this->security[$this->lowercase($class_name)][self::KEY_UNSAFE_COMMANDS] ?? [];
343  }
344 
348  public function getSafeCommandsByCid(string $cid): array
349  {
350  $class_name = $this->getClassNameByCid($cid);
351  if (null !== $class_name) {
352  return $this->getSafeCommandsByName($class_name);
353  }
354 
355  return [];
356  }
357 
361  public function getSafeCommandsByName(string $class_name): array
362  {
363  return $this->security[$this->lowercase($class_name)][self::KEY_SAFE_COMMANDS] ?? [];
364  }
365 
373  private function getValueForKeyByCid(string $identifier_key, string $cid)
374  {
375  if (isset($this->mapped_structure[$cid][$identifier_key])) {
376  return $this->mapped_structure[$cid][$identifier_key];
377  }
378 
379  foreach ($this->structure as $class_info) {
380  if (isset($class_info[$identifier_key]) && $class_info[self::KEY_CLASS_CID] === $cid) {
381  $this->mapped_structure[$cid] = $class_info;
382  return $class_info[$identifier_key];
383  }
384  }
385 
386  return null;
387  }
388 
396  private function getValueForKeyByName(string $identifier_key, string $class_name)
397  {
398  $class_name = $this->lowercase($class_name);
399  if (isset($this->structure[$class_name])) {
400  return $this->structure[$class_name][$identifier_key];
401  }
402 
403  return null;
404  }
405 
411  private function lowercase(string $string): string
412  {
413  return strtolower($string);
414  }
415 }
getRelativePathByCid(string $cid)
__construct(array $ctrl_structure, array $base_classes, array $security_info)
ilCtrlStructure Constructor
getPermanentParametersByClass(string $class_name)
isBaseClass(string $class_name)
setReturnTargetByClass(string $class_name, string $target_url)
Class ilCtrlStructure holds the currently read control structure.
setTemporaryParameterByClass(string $class_name, string $parameter_name, $value)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
removeSingleParameterByClass(string $class_name, string $parameter_name)
getParentsByName(string $class_name)
getClassNameByCid(string $cid)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getRelativePathByName(string $class_name)
getChildrenByName(string $class_name)
getValueForKeyByName(string $identifier_key, string $class_name)
Returns a stored structure value of the given key from the corresponding class mapped by name...
getUnsafeCommandsByName(string $class_name)
getReturnTargetByClass(string $class_name)
getObjNameByName(string $class_name)
removePermanentParametersByClass(string $class_name)
getParentsByCid(string $cid)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
lowercase(string $string)
Helper function to lowercase strings.
getObjNameByCid(string $cid)
getChildrenByCid(string $cid)
getUnsafeCommandsByCid(string $cid)
getClassCidByName(string $class_name)
setPermanentParameterByClass(string $class_name, string $parameter_name)
removeTemporaryParametersByClass(string $class_name)
getValueForKeyByCid(string $identifier_key, string $cid)
Returns a stored structure value of the given key from the corresponding class mapped by CID...
getSafeCommandsByName(string $class_name)
getSafeCommandsByCid(string $cid)
getTemporaryParametersByClass(string $class_name)