FusionDirectory
class_helpersAttribute.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2012-2016 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 
26 {
27  protected $testFunc = '';
28 
29  function validate ()
30  {
31  $func = $this->testFunc;
32  if (!tests::$func($this->value)) {
33  return new SimplePluginCheckError(
34  $this,
35  SimplePluginCheckError::invalidValue(sprintf('"%s"', $this->value))
36  );
37  }
38  }
39 }
40 
45 {
46  protected $trim = TRUE;
47  protected $testFunc = 'is_email';
48  protected $inputType = 'email';
49 }
50 
55 {
56  protected $testFunc = 'is_valid_hostname';
57 }
58 
63 {
64  protected $testFunc = 'is_uid';
65 }
66 
71 {
72  protected $trim = TRUE;
73 
74  protected $testFunc = 'is_path';
75 }
76 
81 {
82  protected $trim = TRUE;
83  protected $testFunc = 'is_url';
84  protected $inputType = 'url';
85 }
86 
91 {
92  protected $trim = TRUE;
93 
94  protected $testFunc = 'is_ip';
95 }
96 
101 {
102  protected $trim = TRUE;
103 
104  protected $testFunc = 'is_ipv4';
105 }
106 
111 {
112  protected $trim = TRUE;
113 
114  protected $testFunc = 'is_ipv6';
115 }
116 
121 {
122  protected $trim = TRUE;
123 
124  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = '', $acl = '')
125  {
126  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl, '/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', '5E:FF:56:A2:AF:15');
127  }
128 
129  function setValue ($value)
130  {
131  return parent::setValue(strtolower($value));
132  }
133 }
134 
139 {
140  private $sep;
141 
142  function __construct ($description, $ldapName, $attributes, $sep, $acl = "", $label = "Composite attribute")
143  {
144  parent::__construct($description, $ldapName, $attributes, '', '', $acl, $label);
145  $this->sep = $sep;
146  }
147 
148  function readValues (string $value): array
149  {
150  return explode($this->sep, $value);
151  }
152 
153  function writeValues (array $values)
154  {
155  return join($this->sep, $values);
156  }
157 }
158 
163 {
164  function __construct ($description, $ldapName, $attributes, $acl = "", $label = "Composite attribute")
165  {
166  parent::__construct($description, $ldapName, $attributes, '|', $acl, $label);
167  }
168 }
169 
174 {
175  private $sep = ',';
176 
177  function __construct ($ldapName, $attribute, $sep = ',')
178  {
179  parent::__construct(
180  $attribute->getDescription(), $ldapName,
181  [
182  $attribute
183  ], '', ''
184  );
185  $this->sep = $sep;
186  }
187 
188  function readValues (string $value): array
189  {
190  if ($value == '') {
191  return [[]];
192  } else {
193  return [explode($this->sep, $value)];
194  }
195  }
196 
197  function writeValues (array $values)
198  {
199  return join($this->sep, $values[0]);
200  }
201 }
202 
207 {
208  /* $units should contains 1 in its keys as a fallback unit */
209  function __construct ($label, $description, $ldapName, $required, $units, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
210  {
211  parent::__construct($description, $ldapName,
212  [
213  new IntAttribute('', '', $ldapName.'_int', $required, $min, $max, $defaultValue),
214  new SelectAttribute('', '', $ldapName.'_select', TRUE, array_keys($units), '', array_values($units)),
215  ], '', '',
216  $acl, $label
217  );
218  $this->setLinearRendering(TRUE);
219  $this->setRequired($required); /* As we use linear rendering we want the asterisk in the label if required */
220  }
221 
222  function readValues (string $value): array
223  {
224  $choices = $this->attributes[1]->getChoices();
225  sort($choices);
226  if (($value === '') || ($value == 0)) {
227  return [$value, $choices[0]];
228  }
229  /* Find the appropriate unit */
230  for (
231  $i = (count($choices) - 1);
232  ($value % $choices[$i] != 0) && ($i > 0);
233  $i--
234  );
235  return [$value / $choices[$i], $choices[$i]];
236  }
237 
238  function writeValues (array $values)
239  {
240  if ($values[0] === '') {
241  return '';
242  }
243  return $values[0] * $values[1];
244  }
245 
246  function displayValue ($value): string
247  {
248  $values = $this->readValues($value);
249  return $values[0].$this->attributes[1]->displayValue($values[1]);
250  }
251 }
252 
257 {
258  function __construct ($label, $description, $ldapName, $required, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
259  {
260  $units = [
261  1 => _('B'),
262  1024 ** 1 => _('KiB'),
263  1024 ** 2 => _('MiB'),
264  1024 ** 3 => _('GiB'),
265  1024 ** 4 => _('TiB'),
266  ];
267  parent::__construct($label, $description, $ldapName, $required, $units, $min, $max, $defaultValue, $acl);
268  }
269 }
270 
275 {
276  function __construct ($label, $description, $ldapName, $required, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
277  {
278  $units = [
279  1 => _('seconds'),
280  60 => _('minutes'),
281  3600 => _('hours'),
282  86400 => _('days'),
283  ];
284  parent::__construct($label, $description, $ldapName, $required, $units, $min, $max, $defaultValue, $acl);
285  }
286 }
This class allow to handle easily a Select LDAP attribute with a set of choices.
Attribute storing its values as a comma-separated list.
CompositeAttribute of which values are joined using a character as separator.
This class allow to handle easily a String LDAP attribute that contains an uid.
This class allow to handle easily a String LDAP attribute that contains a path.
This class allow to handle easily a composite attribute.
This class allow to handle easily a String LDAP attribute that needs to be validated by a test from c...
This class allow to handle easily a String LDAP attribute that contains a mac address.
This class allow to handle easily a String LDAP attribute that contains a host name.
Attribute storing a time duration as seconds.
This class allow to handle easily a String LDAP attribute that contains an IP (v4 or v6) ...
static invalidValue(string $error)
Format error message for invalid value.
Attribute for storing a size in bytes.
Error returned by check method of SimplePlugin.
CharSeparatedCompositeAttribute where the separator is the pipe character.
This class allow to handle easily a String LDAP attribute that contains a IPv6.
Generic attribute for storing an integer allowing to chose between units.
This class allow to handle easily a String LDAP attribute that contains an IPv4.
This class allow to handle easily an Integer LDAP attribute.
This class allow to handle easily a String LDAP attribute that contains an URL.
__construct($label, $description, $ldapName, $required=FALSE, $defaultValue="", $acl="", $regexp="", $example=NULL)
The constructor of StringAttribute.
This class allow to handle easily a String LDAP attribute that contains a mail address.
This class allow to handle easily a String LDAP attribute.