FusionDirectory
 All Data Structures Files Functions Variables
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-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]$/i", $mac);
170  }
171 
172 
179  public static function is_ip_with_subnetmask($ip)
180  {
181  /* Generate list of valid submasks */
182  $res = array();
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  if ($address == "") {
272  return TRUE;
273  }
274  if (filter_var($address, FILTER_VALIDATE_EMAIL) !== FALSE) {
275  return TRUE;
276  } elseif (filter_var($address.'.com', FILTER_VALIDATE_EMAIL) !== FALSE) {
277  /* this is to allow addresses like example@localhost, which are refused by some PHP version */
278  return TRUE;
279  }
280  return FALSE;
281  }
282 
283 
284  /*
285  * \brief Check if the given department name is valid
286  *
287  * \param string $name The deparment name
288  *
289  * \param string $base
290  */
291  public static function is_department_name_reserved($name)
292  {
293  global $config;
294  $reservedNames = array();
295  foreach ($config->data['OBJECTS'] as $infos) {
296  if (isset($infos['ou'])) {
297  if ($infos['ou'] != '') {
298  if (preg_match('/ou=([^,]+),$/', $infos['ou'], $m)) {
299  $reservedNames[] = $m[1];
300  }
301  }
302  }
303  }
304 
305  return in_array_ics($name, array_unique($reservedNames));
306  }
307 
308 
309  /*
310  * \brief Check if $ip1 and $ip2 represents a valid IPv4 range
311  *
312  * \param string $ip1 The first IPv4
313  *
314  * \param string $ip2 The second IPv4
315  *
316  * \return TRUE in case of a valid range, FALSE in case of an error.
317  */
318  public static function is_ip_range($ip1, $ip2)
319  {
320  if (!tests::is_ipv4($ip1) || !tests::is_ipv4($ip2)) {
321  return FALSE;
322  } else {
323  $ar1 = explode(".", $ip1);
324  $var1 = $ar1[0] * (16777216) + $ar1[1] * (65536) + $ar1[2] * (256) + $ar1[3];
325 
326  $ar2 = explode(".", $ip2);
327  $var2 = $ar2[0] * (16777216) + $ar2[1] * (65536) + $ar2[2] * (256) + $ar2[3];
328  return ($var1 < $var2);
329  }
330  }
331 
332 
333  /*
334  * \brief Check if the specified IP address is inside the given network
335  *
336  * \param string $network Name of the network
337  *
338  * \param string $netmask The netmask of the IP address
339  *
340  * \param string $address The IP address
341  */
342  public static function is_in_network($network, $netmask, $address)
343  {
344  $nw = explode('.', $network);
345  $nm = explode('.', $netmask);
346  $ad = explode('.', $address);
347 
348  /* Generate inverted netmask */
349  for ($i = 0; $i < 4; $i++) {
350  $ni[$i] = 255 - $nm[$i];
351  $la[$i] = $nw[$i] | $ni[$i];
352  }
353 
354  /* Transform to integer */
355  $first = $nw[0] * (16777216) + $nw[1] * (65536) + $nw[2] * (256) + $nw[3];
356  $curr = $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
357  $last = $la[0] * (16777216) + $la[1] * (65536) + $la[2] * (256) + $la[3];
358 
359  return ($first < $curr && $last > $curr);
360  }
361 
362  /* \brief Check if the specified IP address $address is inside the given network */
363  public static function is_in_ip_range($from, $to, $address)
364  {
365  $from = explode('.', $from);
366  $to = explode('.', $to);
367  $ad = explode('.', $address);
368 
369  /* Transform to integer */
370  $from = $from[0] * (16777216) + $from[1] * (65536) + $from[2] * (256) + $from[3];
371  $to = $to[0] * (16777216) + $to[1] * (65536) + $to[2] * (256) + $to[3];
372  $ad = $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
373 
374  return ($ad >= $from && $ad <= $to);
375  }
376 }
377 
378 ?>
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:1657
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:1136
static is_ip_with_subnetmask($ip)
Checks if the given ip address doesn'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.