FusionDirectory
 All Data Structures Files Functions Variables
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 
25 {
26  protected $trim = TRUE;
27 
28  function validate ()
29  {
30  if (!tests::is_email($this->value)) {
31  return msgPool::invalid($this->getLabel(), $this->value);
32  }
33  }
34 }
35 
36 
41 {
42  protected $testFunc = '';
43 
44  function validate ()
45  {
46  $testFunc = $this->testFunc;
47  if (!tests::$testFunc($this->value)) {
48  return msgPool::invalid($this->getLabel(), $this->value);
49  }
50  }
51 }
52 
57 {
58  protected $testFunc = 'is_valid_hostname';
59 }
60 
65 {
66  protected $testFunc = 'is_uid';
67 }
68 
73 {
74  protected $trim = TRUE;
75 
76  protected $testFunc = 'is_path';
77 }
78 
83 {
84  protected $trim = TRUE;
85 
86  protected $testFunc = 'is_url';
87 }
88 
93 {
94  protected $trim = TRUE;
95 
96  protected $testFunc = 'is_phone_nr';
97 }
98 
103 {
104  protected $trim = TRUE;
105 
106  protected $testFunc = 'is_ip';
107 }
108 
113 {
114  protected $trim = TRUE;
115 
116  protected $testFunc = 'is_ipv4';
117 }
118 
123 {
124  protected $trim = TRUE;
125 
126  protected $testFunc = 'is_ipv6';
127 }
128 
133 {
134  protected $trim = TRUE;
135 
136  function setValue ($value)
137  {
138  return parent::setValue(strtolower($value));
139  }
140 
141  function validate ()
142  {
143  if (!tests::is_mac($this->value)) {
144  return msgPool::invalid($this->getLabel(), $this->value);
145  }
146  }
147 }
148 
150 {
151  private $sep;
152 
153  function __construct($description, $ldapName, $attributes, $sep, $acl = "", $label = "Composite attribute")
154  {
155  parent::__construct($description, $ldapName, $attributes, '', '', $acl, $label);
156  $this->sep = $sep;
157  }
158 
159  function readValues($value)
160  {
161  return explode($this->sep, $value);
162  }
163 
164  function writeValues($values)
165  {
166  return join($this->sep, $values);
167  }
168 }
169 
171 {
172  function __construct($description, $ldapName, $attributes, $acl = "", $label = "Composite attribute")
173  {
174  parent::__construct($description, $ldapName, $attributes, '|', $acl, $label);
175  }
176 }
177 
179 {
180  private $sep = ',';
181 
182  function __construct($ldapName, $attribute, $sep = ',')
183  {
184  parent::__construct(
185  $attribute->getDescription(), $ldapName,
186  array(
187  $attribute
188  ), '', ''
189  );
190  $this->sep = $sep;
191  }
192 
193  function readValues($value)
194  {
195  if ($value == '') {
196  return array(array());
197  } else {
198  return array(explode($this->sep, $value));
199  }
200  }
201 
202  function writeValues($values)
203  {
204  return join($this->sep, $values[0]);
205  }
206 }
207 
209 {
210  /* $units should contains 1 in its keys as a fallback unit */
211  function __construct ($label, $description, $ldapName, $required, $units, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
212  {
213  parent::__construct($description, $ldapName,
214  array(
215  new IntAttribute('', '', $ldapName.'_int', $required, $min, $max, $defaultValue),
216  new SelectAttribute('', '', $ldapName.'_select', TRUE, array_keys($units), '', array_values($units)),
217  ), '', '',
218  $acl, $label
219  );
220  $this->setLinearRendering(TRUE);
221  $this->setRequired($required); /* As we use linear rendering we want the asterisk in the label if required */
222  }
223 
224  function readValues($value)
225  {
226  if ($value === '') {
227  return array('', 0);
228  }
229  $choices = $this->attributes[1]->getChoices();
230  sort($choices);
231  if ($value == 0) {
232  return array($value, $choices[0]);
233  }
234  /* Find the appropriate unit */
235  for (
236  $i = (count($choices) - 1);
237  ($value % $choices[$i] != 0) && ($i > 0);
238  $i--
239  );
240  return array($value / $choices[$i], $choices[$i]);
241  }
242 
243  function writeValues($values)
244  {
245  if ($values[0] === '') {
246  return '';
247  }
248  return $values[0] * $values[1];
249  }
250 
251  function displayValue($value)
252  {
253  $values = $this->readValues($value);
254  return $values[0].$this->attributes[1]->displayValue($values[1]);
255  }
256 }
257 
259 {
260  function __construct ($label, $description, $ldapName, $required, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
261  {
262  $units = array(
263  1 => _('B'),
264  pow(1024, 1) => _('KiB'),
265  pow(1024, 2) => _('MiB'),
266  pow(1024, 3) => _('GiB'),
267  pow(1024, 4) => _('TiB'),
268  );
269  parent::__construct ($label, $description, $ldapName, $required, $units, $min, $max, $defaultValue, $acl);
270  }
271 }
272 
274 {
275  function __construct ($label, $description, $ldapName, $required, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
276  {
277  $units = array(
278  1 => _('seconds'),
279  60 => _('minutes'),
280  3600 => _('hours'),
281  86400 => _('days'),
282  );
283  parent::__construct ($label, $description, $ldapName, $required, $units, $min, $max, $defaultValue, $acl);
284  }
285 }
286 
287 ?>
This class allow to handle easily a Select LDAP attribute with a set of choices.
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.
static invalid($name, $data="", $regex="", $example="")
Display error about invalid characters.
This class allow to handle easily a String LDAP attribute that contains an IP (v4 or v6) ...
This class allow to handle easily a String LDAP attribute that contains a path.
This class allow to handle easily a String LDAP attribute that contains a IPv6.
static is_email($address)
Check if the given argument is an email.
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.
static is_mac($mac)
Test if the given string is a mac address.
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.