ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  parent::__construct($config, $reserved);
51 
52  assert('is_array($config)');
53 
54  // parse filter configuration
55  foreach ($config as $name => $value) {
56  if (is_int($name)) {
57  // check if this is an option
58  if($value === '%replace') {
59  $this->replace = TRUE;
60  } elseif ($value === '%remove') {
61  $this->remove = TRUE;
62  } else {
63  throw new SimpleSAML_Error_Exception('Unknown flag : ' . var_export($value, TRUE));
64  }
65  continue;
66  }
67 
68  // Set pattern
69  if ($name === 'pattern') {
70  $this->pattern = $value;
71  }
72 
73  // Set replacement
74  if ($name === 'replacement') {
75  $this->replacement = $value;
76  }
77 
78  // Set subject
79  if ($name === 'subject') {
80  $this->subject = $value;
81  }
82 
83  // Set target
84  if ($name === 'target') {
85  $this->target = $value;
86  }
87  }
88  }
89 
98  public function process(&$request) {
99  assert('is_array($request)');
100  assert('array_key_exists("Attributes", $request)');
101 
102  // get attributes from request
103  $attributes =& $request['Attributes'];
104 
105  // check that all required params are set in config
106  if (empty($this->pattern) || empty($this->subject)) {
107  throw new SimpleSAML_Error_Exception("Not all params set in config.");
108  }
109 
110  if (!$this->replace && !$this->remove && $this->replacement === false) {
111  throw new SimpleSAML_Error_Exception("'replacement' must be set if neither '%replace' nor ".
112  "'%remove' are set.");
113  }
114 
115  if (!$this->replace && $this->replacement === null) {
116  throw new SimpleSAML_Error_Exception("'%replace' must be set if 'replacement' is null.");
117  }
118 
119  if ($this->replace && $this->remove) {
120  throw new SimpleSAML_Error_Exception("'%replace' and '%remove' cannot be used together.");
121  }
122 
123  if (empty($this->target)) {
124  // use subject as target if target is not set
125  $this->target = $this->subject;
126  }
127 
128  if ($this->subject !== $this->target && $this->remove) {
129  throw new SimpleSAML_Error_Exception("Cannot use '%remove' when 'target' is different than 'subject'.");
130  }
131 
132  if (!array_key_exists($this->subject, $attributes)) {
133  // if no such subject, stop gracefully
134  return;
135  }
136 
137  if ($this->replace) { // replace the whole value
138  foreach ($attributes[$this->subject] as &$value) {
139  $matches = array();
140  if (preg_match($this->pattern, $value, $matches) > 0) {
141  $new_value = $matches[0];
142 
143  if ($this->replacement !== FALSE) {
144  $new_value = $this->replacement;
145  }
146 
147  if ($this->subject === $this->target) {
148  $value = $new_value;
149  } else {
150  $attributes[$this->target] = array($new_value);
151  }
152  }
153  }
154  } elseif ($this->remove) { // remove the whole value
155  $removedAttrs = array();
156  foreach ($attributes[$this->subject] as $value) {
157  $matches = array();
158  if (preg_match($this->pattern, $value, $matches) > 0) {
159  $removedAttrs[] = $value;
160  }
161  }
162  $attributes[$this->target] = array_diff($attributes[$this->subject], $removedAttrs);
163 
164  if (empty($attributes[$this->target])) {
165  unset($attributes[$this->target]);
166  }
167  } else { // replace only the part that matches
168  if ($this->subject === $this->target) {
169  $attributes[$this->target] = preg_replace($this->pattern, $this->replacement,
170  $attributes[$this->subject]);
171  } else {
172  $attributes[$this->target] = array_diff(preg_replace($this->pattern, $this->replacement,
173  $attributes[$this->subject]),
174  $attributes[$this->subject]);
175  }
176  }
177  }
178 }
$target
Attribute to place the result in.
$replace
Should the pattern found be replaced?
$attributes
if($format !==null) $name
Definition: metadata.php:146
$remove
Should the value found be removed?
Create styles array
The data for the language used.
process(&$request)
Apply the filter to modify attributes.
$replacement
String to replace the pattern found with.
__construct($config, $reserved)
Initialize this filter.