FusionDirectory
class_tests.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2003-2010 Cajus Pollmeier
5  Copyright (C) 2011-2016 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
41 class tests
42 {
48  public static function is_phone_nr ($nr)
49  {
50  if ($nr == "") {
51  return TRUE;
52  }
53 
54  return preg_match("/^[\/0-9 ()+*-]+$/", $nr);
55  }
56 
57 
63  public static function is_dns_name ($str)
64  {
65  return preg_match("/^[a-z0-9\.\-_]*$/i", $str);
66  }
67 
68 
74  public static function is_valid_hostname ($str)
75  {
76  return preg_match("/^[a-z0-9\.\-]*$/i", $str);
77  }
78 
79 
85  public static function is_url ($url)
86  {
87  if ($url == "") {
88  return TRUE;
89  }
90 
91  /* Using @stephenhay regexp from http://mathiasbynens.be/demo/url-regex (removed ftp through) */
92  return preg_match("@^(https?)://[^\s/$.?#].[^\s]*$@i", $url);
93  }
94 
95 
101  public static function is_dn ($dn)
102  {
103  if ($dn == "") {
104  return TRUE;
105  }
106 
107  return preg_match("/^[a-z0-9 _-]+$/i", $dn);
108  }
109 
110 
116  public static function is_uid ($uid)
117  {
118  if ($uid == "") {
119  return TRUE;
120  }
121 
122  /* STRICT adds spaces and case insenstivity to the uid check.
123  This is dangerous and should not be used. */
124  if (strict_uid_mode()) {
125  return preg_match("/^[a-z0-9_-]+$/", $uid);
126  } else {
127  return preg_match("/^[a-z0-9 _.-]+$/i", $uid);
128  }
129  }
130 
136  public static function is_ip ($ip)
137  {
138  return filter_var($ip, FILTER_VALIDATE_IP);
139  }
140 
146  public static function is_ipv4 ($ip)
147  {
148  return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
149  }
150 
156  public static function is_ipv6 ($ip)
157  {
158  return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
159  }
160 
161 
167  public static function is_mac ($mac)
168  {
169  return preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', $mac);
170  }
171 
172 
179  public static function is_ip_with_subnetmask ($ip)
180  {
181  /* Generate list of valid submasks */
182  $res = [];
183  for ($e = 0; $e <= 32; $e++) {
184  $res[$e] = $e;
185  }
186  $i[0] = 255;
187  $i[1] = 255;
188  $i[2] = 255;
189  $i[3] = 255;
190  for ($a = 3; $a >= 0; $a--) {
191  $c = 1;
192  while ($i[$a] > 0) {
193  $str = $i[0].".".$i[1].".".$i[2].".".$i[3];
194  $res[$str] = $str;
195  $i[$a] -= $c;
196  $c = 2 * $c;
197  }
198  }
199  $res["0.0.0.0"] = "0.0.0.0";
200  if (preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
201  "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
202  "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
203  "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/", $ip)) {
204  $mask = preg_replace("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
205  "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
206  "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
207  "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/", "", $ip);
208 
209  $mask = preg_replace("/^\//", "", $mask);
210  if ((in_array("$mask", $res)) && preg_match("/^[0-9\.]/", $mask)) {
211  return TRUE;
212  }
213  }
214  return FALSE;
215  }
216 
217 
225  public static function is_domain ($str)
226  {
227  return preg_match("/^(([a-z0-9\-]{2,63})\.)*[a-z]{2,63}$/i", $str);
228  }
229 
230 
236  public static function is_id ($id)
237  {
238  if ($id == "") {
239  return FALSE;
240  }
241 
242  return preg_match("/^[0-9]+$/", $id);
243  }
244 
245 
251  public static function is_path ($path)
252  {
253  if ($path == "") {
254  return TRUE;
255  }
256  if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)) {
257  return FALSE;
258  }
259 
260  return preg_match("/\/.+$/", $path);
261  }
262 
263 
269  public static function is_email ($address)
270  {
271  /* last test is to allow addresses like example@localhost, which are refused by some PHP version */
272  return (($address == '')
273  || (filter_var($address, FILTER_VALIDATE_EMAIL) !== FALSE)
274  || (filter_var($address.'.com', FILTER_VALIDATE_EMAIL) !== FALSE));
275  }
276 
277 
278  /*
279  * \brief Check if the given department name is valid
280  *
281  * \param string $name The deparment name
282  *
283  * \param string $base
284  */
285  public static function is_department_name_reserved ($name)
286  {
287  global $config;
288  $reservedNames = [];
289  foreach ($config->data['OBJECTS'] as $infos) {
290  if (isset($infos['ou']) && preg_match('/ou=([^,]+),$/', $infos['ou'], $m)) {
291  $reservedNames[] = $m[1];
292  }
293  }
294 
295  return in_array_ics($name, array_unique($reservedNames));
296  }
297 
298 
299  /*
300  * \brief Check if $ip1 and $ip2 represents a valid IPv4 range
301  *
302  * \param string $ip1 The first IPv4
303  *
304  * \param string $ip2 The second IPv4
305  *
306  * \return TRUE in case of a valid range, FALSE in case of an error.
307  */
308  public static function is_ip_range ($ip1, $ip2)
309  {
310  if (!tests::is_ipv4($ip1) || !tests::is_ipv4($ip2)) {
311  return FALSE;
312  } else {
313  $ar1 = array_map('intval', explode('.', $ip1));
314  $var1 = $ar1[0] * (16777216) + $ar1[1] * (65536) + $ar1[2] * (256) + $ar1[3];
315 
316  $ar2 = array_map('intval', explode('.', $ip2));
317  $var2 = $ar2[0] * (16777216) + $ar2[1] * (65536) + $ar2[2] * (256) + $ar2[3];
318  return ($var1 < $var2);
319  }
320  }
321 
322 
323  /*
324  * \brief Check if the specified IP address is inside the given network
325  *
326  * \param string $network Name of the network
327  *
328  * \param string $netmask The netmask of the IPv4 address
329  *
330  * \param string $address The IPv4 address
331  */
332  public static function is_in_network ($network, $netmask, $address)
333  {
334  $nw = array_map('intval', explode('.', $network));
335  $nm = array_map('intval', explode('.', $netmask));
336  $ad = array_map('intval', explode('.', $address));
337 
338  /* Generate inverted netmask */
339  $ni = [];
340  $la = [];
341  for ($i = 0; $i < 4; $i++) {
342  $ni[$i] = 255 - $nm[$i];
343  $la[$i] = $nw[$i] | $ni[$i];
344  }
345 
346  /* Transform to integer */
347  $first = $nw[0] * (16777216) + $nw[1] * (65536) + $nw[2] * (256) + $nw[3];
348  $curr = $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
349  $last = $la[0] * (16777216) + $la[1] * (65536) + $la[2] * (256) + $la[3];
350 
351  return ($first < $curr && $last > $curr);
352  }
353 
354  /* \brief Check if the specified IPv4 address $address is inside the given network */
355  public static function is_in_ip_range ($from, $to, $address)
356  {
357  $from = array_map('intval', explode('.', $from));
358  $to = array_map('intval', explode('.', $to));
359  $ad = array_map('intval', explode('.', $address));
360 
361  /* Transform to integer */
362  $from = $from[0] * (16777216) + $from[1] * (65536) + $from[2] * (256) + $from[3];
363  $to = $to[0] * (16777216) + $to[1] * (65536) + $to[2] * (256) + $to[3];
364  $ad = $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
365 
366  return ($ad >= $from && $ad <= $to);
367  }
368 
369  /* \brief Check if the value is a valid orcid id */
370  public static function is_orcid ($orcid)
371  {
372  /* Remove hyphens, remove last digit, convert to array */
373  $baseDigits = str_split(str_replace('-', '', substr($orcid, 0, -1)));
374  $sum = 0;
375  foreach ($baseDigits as $baseDigit) {
376  $sum = ($sum + (int)$baseDigit) * 2;
377  }
378  $remainder = $sum % 11;
379  $result = (12 - $remainder) % 11;
380  $orcidCheckSum = (($result == 10) ? "X" : (string)$result);
381 
382  return ($orcidCheckSum != substr($orcid, -1));
383  }
384 }
static is_ipv4($ip)
Test if the given string is an IPv4.
in_array_ics($value, array $items)
Check if a value exists in an array (case-insensitive)
Definition: functions.inc:814
static is_path($path)
Check if the given argument is a path.
static is_phone_nr($nr)
Test if the given string is a phone number.
Definition: class_tests.inc:48
static is_url($url)
Test if the given string is an URL.
Definition: class_tests.inc:85
static is_uid($uid)
Test if the given string is an uid.
static is_valid_hostname($str)
Test if the given string contains characters allowed in a hostname.
Definition: class_tests.inc:74
static is_dn($dn)
Test if the given string is a DN.
static is_ip($ip)
Test if the given string is an IP (v4 or v6)
static is_dns_name($str)
Test if the given string contains characters allowed in a DNS record name.
Definition: class_tests.inc:63
strict_uid_mode()
Check if strict naming rules are configured.
Definition: functions.inc:526
static is_ip_with_subnetmask($ip)
Checks if the given ip address doesn&#39;t match "is_ip" because there is also a sub net mask given...
static is_email($address)
Check if the given argument is an email.
static is_domain($str)
Simple is domain check.
This class provides various test functions.
Definition: class_tests.inc:41
static is_mac($mac)
Test if the given string is a mac address.
static is_ipv6($ip)
Test if the given string is an IPv6.
static is_id($id)
Check if the given argument is an id.