File: class_helpersAttribute.inc

package info (click to toggle)
fusiondirectory 1.0.19-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 32,060 kB
  • sloc: php: 47,469; pascal: 2,993; perl: 2,431; xml: 1,822; sh: 136; makefile: 19
file content (288 lines) | stat: -rw-r--r-- 8,084 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2012-2016  FusionDirectory

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/


/*! \brief This class allow to handle easily a String LDAP attribute that contains a mail address
 *
 */
class MailAttribute extends StringAttribute
{
  private $template;
  /*! \brief The constructor of MailAttribute
   *
   *  \param string $label The label to show for this attribute
   *  \param string $description A more detailed description for the attribute
   *  \param string $ldapName The name of the attribute in the LDAP (If it's not in the ldap, still provide a unique name)
   *  \param boolean $required Is this attribute mandatory or not
   *  \param mixed $defaultValue The default value for this attribute
   *  \param string $acl The name of the acl for this attribute if he does not use its own. (Leave empty if he should use its own like most attributes do)
   *  \param boolean $template Should this email field accept template character '%' ?
   */
  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = "", $acl = "", $template = FALSE)
  {
    parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
    $this->template = $template;
  }
  function validate ()
  {
    if (!tests::is_email($this->value, $this->template)) {
      return msgPool::invalid($this->getLabel(), $this->value);
    }
  }
}


/*! \brief This class allow to handle easily a String LDAP attribute that needs to be validated by a test from class tests
 *
 */
class TestValidateAttribute extends StringAttribute
{
  protected $testFunc = '';

  function validate ()
  {
    $testFunc = $this->testFunc;
    if (!tests::$testFunc($this->value)) {
      return msgPool::invalid($this->getLabel(), $this->value);
    }
  }
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains a host name
 *
 */
class HostNameAttribute extends TestValidateAttribute
{
  protected $testFunc = 'is_valid_hostname';
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains an uid
 *
 */
class UidAttribute extends TestValidateAttribute
{
  protected $testFunc = 'is_uid';
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains a path
 *
 */
class PathAttribute extends TestValidateAttribute
{
  protected $testFunc = 'is_path';
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains an URL
 *
 */
class URLAttribute extends TestValidateAttribute
{
  protected $testFunc = 'is_url';
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains a path
 *
 */
class PhoneNumberAttribute extends TestValidateAttribute
{
  protected $testFunc = 'is_phone_nr';
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains an IP (v4 or v6)
 *
 */
class IPAttribute extends TestValidateAttribute
{
  protected $testFunc = 'is_ip';
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains an IPv4
 *
 */
class IPv4Attribute extends TestValidateAttribute
{
  protected $testFunc = 'is_ipv4';
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains a IPv6
 *
 */
class IPv6Attribute extends TestValidateAttribute
{
  protected $testFunc = 'is_ipv6';
}

/*! \brief This class allow to handle easily a String LDAP attribute that contains a mac address
 *
 */
class MacAddressAttribute extends StringAttribute
{
  function setValue ($value)
  {
    return parent::setValue(strtolower($value));
  }

  function validate ()
  {
    if (!tests::is_mac($this->value)) {
      return msgPool::invalid($this->getLabel(), $this->value);
    }
  }
}

class CharSeparatedCompositeAttribute extends CompositeAttribute
{
  private $sep;

  function __construct($description, $ldapName, $attributes, $sep, $acl = "", $label = "Composite attribute")
  {
    parent::__construct($description, $ldapName, $attributes, '', '', $acl, $label);
    $this->sep = $sep;
  }

  function readValues($value)
  {
    return explode($this->sep, $value);
  }

  function writeValues($values)
  {
    return join($this->sep, $values);
  }
}

class PipeSeparatedCompositeAttribute extends CharSeparatedCompositeAttribute
{
  function __construct($description, $ldapName, $attributes, $acl = "", $label = "Composite attribute")
  {
    parent::__construct($description, $ldapName, $attributes, '|', $acl, $label);
  }
}

class CommaListAttribute extends CompositeAttribute
{
  private $sep = ',';

  function __construct($ldapName, $attribute, $sep = ',')
  {
    parent::__construct(
      $attribute->getDescription(), $ldapName,
      array(
        $attribute
      ), '', ''
    );
    $this->sep = $sep;
  }

  function readValues($value)
  {
    if ($value == '') {
      return array(array());
    } else {
      return array(explode($this->sep, $value));
    }
  }

  function writeValues($values)
  {
    return join($this->sep, $values[0]);
  }
}

class UnitIntAttribute extends CompositeAttribute
{
  /* $units should contains 1 in its keys as a fallback unit */
  function __construct ($label, $description, $ldapName, $required, $units, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
  {
    parent::__construct($description, $ldapName,
      array(
        new IntAttribute('', '', $ldapName.'_int', $required, $min, $max, $defaultValue),
        new SelectAttribute('', '', $ldapName.'_select', TRUE, array_keys($units), '', array_values($units)),
      ), '', '',
      $acl, $label
    );
    $this->setLinearRendering(TRUE);
    $this->setRequired($required); /* As we use linear rendering we want the asterisk in the label if required */
  }

  function readValues($value)
  {
    if ($value === '') {
      return array('', 0);
    }
    $choices = $this->attributes[1]->getChoices();
    sort($choices);
    if ($value == 0) {
      return array($value, $choices[0]);
    }
    /* Find the appropriate unit */
    for (
      $i = (count($choices) - 1);
      ($value % $choices[$i] != 0) && ($i > 0);
      $i--
    )
    return array($value / $choices[$i], $choices[$i]);
  }

  function writeValues($values)
  {
    if ($values[0] === '') {
      return '';
    }
    return $values[0] * $values[1];
  }

  function displayValue($value)
  {
    $values = $this->readValues($value);
    return $values[0].$this->attributes[1]->displayValue($values[1]);
  }
}

class BytesSizeAttribute extends UnitIntAttribute
{
  function __construct ($label, $description, $ldapName, $required, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
  {
    $units = array(
      1             => _('B'),
      pow(1024, 1)  => _('KiB'),
      pow(1024, 2)  => _('MiB'),
      pow(1024, 3)  => _('GiB'),
      pow(1024, 4)  => _('TiB'),
    );
    parent::__construct ($label, $description, $ldapName, $required, $units, $min, $max, $defaultValue, $acl);
  }
}

class TimeAttribute extends UnitIntAttribute
{
  function __construct ($label, $description, $ldapName, $required, $min = FALSE, $max = FALSE, $defaultValue = "", $acl = "")
  {
    $units = array(
      1     => _('seconds'),
      60    => _('minutes'),
      3600  => _('hours'),
      86400 => _('days'),
    );
    parent::__construct ($label, $description, $ldapName, $required, $units, $min, $max, $defaultValue, $acl);
  }
}

?>