ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
AttributeAlter.php
Go to the documentation of this file.
1 <?php
11 {
15  private $replace = false;
16 
20  private $remove = false;
21 
25  private $pattern = '';
26 
30  private $replacement = false;
31 
35  private $subject = '';
36 
40  private $target = '';
41 
49  public function __construct($config, $reserved)
50  {
51  parent::__construct($config, $reserved);
52 
53  assert(is_array($config));
54 
55  // parse filter configuration
56  foreach ($config as $name => $value) {
57  if (is_int($name)) {
58  // check if this is an option
59  if ($value === '%replace') {
60  $this->replace = true;
61  } elseif ($value === '%remove') {
62  $this->remove = true;
63  } else {
64  throw new SimpleSAML_Error_Exception('Unknown flag : ' . var_export($value, true));
65  }
66  continue;
67  } elseif ($name === 'pattern') {
68  // Set pattern
69  $this->pattern = $value;
70  } elseif ($name === 'replacement') {
71  // Set replacement
72  $this->replacement = $value;
73  } elseif ($name === 'subject') {
74  // Set subject
75  $this->subject = $value;
76  } elseif ($name === 'target') {
77  // Set target
78  $this->target = $value;
79  }
80  }
81  }
82 
91  public function process(&$request) {
92  assert(is_array($request));
93  assert(array_key_exists('Attributes', $request));
94 
95  // get attributes from request
96  $attributes =& $request['Attributes'];
97 
98  // check that all required params are set in config
99  if (empty($this->pattern) || empty($this->subject)) {
100  throw new SimpleSAML_Error_Exception("Not all params set in config.");
101  }
102 
103  if (!$this->replace && !$this->remove && $this->replacement === false) {
104  throw new SimpleSAML_Error_Exception("'replacement' must be set if neither '%replace' nor ".
105  "'%remove' are set.");
106  }
107 
108  if (!$this->replace && $this->replacement === null) {
109  throw new SimpleSAML_Error_Exception("'%replace' must be set if 'replacement' is null.");
110  }
111 
112  if ($this->replace && $this->remove) {
113  throw new SimpleSAML_Error_Exception("'%replace' and '%remove' cannot be used together.");
114  }
115 
116  if (empty($this->target)) {
117  // use subject as target if target is not set
118  $this->target = $this->subject;
119  }
120 
121  if ($this->subject !== $this->target && $this->remove) {
122  throw new SimpleSAML_Error_Exception("Cannot use '%remove' when 'target' is different than 'subject'.");
123  }
124 
125  if (!array_key_exists($this->subject, $attributes)) {
126  // if no such subject, stop gracefully
127  return;
128  }
129 
130  if ($this->replace) { // replace the whole value
131  foreach ($attributes[$this->subject] as &$value) {
132  $matches = array();
133  if (preg_match($this->pattern, $value, $matches) > 0) {
134  $new_value = $matches[0];
135 
136  if ($this->replacement !== FALSE) {
137  $new_value = $this->replacement;
138  }
139 
140  if ($this->subject === $this->target) {
141  $value = $new_value;
142  } else {
143  $attributes[$this->target] = array($new_value);
144  }
145  }
146  }
147  } elseif ($this->remove) { // remove the whole value
148  $removedAttrs = array();
149  foreach ($attributes[$this->subject] as $value) {
150  $matches = array();
151  if (preg_match($this->pattern, $value, $matches) > 0) {
152  $removedAttrs[] = $value;
153  }
154  }
155  $attributes[$this->target] = array_diff($attributes[$this->subject], $removedAttrs);
156 
157  if (empty($attributes[$this->target])) {
158  unset($attributes[$this->target]);
159  }
160  } else { // replace only the part that matches
161  if ($this->subject === $this->target) {
162  $attributes[$this->target] = preg_replace(
163  $this->pattern, $this->replacement,
164  $attributes[$this->subject]
165  );
166  } else {
167  $attributes[$this->target] = array_diff(
168  preg_replace(
169  $this->pattern,
170  $this->replacement,
171  $attributes[$this->subject]
172  ),
173  $attributes[$this->subject]
174  );
175  }
176  }
177  }
178 }
$target
Attribute to place the result in.
$replace
Should the pattern found be replaced?
$config
Definition: bootstrap.php:15
foreach($paths as $path) $request
Definition: asyncclient.php:32
$remove
Should the value found be removed?
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
process(&$request)
Apply the filter to modify attributes.
$replacement
String to replace the pattern found with.
__construct($config, $reserved)
Initialize this filter.