1 <?php
2 /*
3 * This code is part of GOsa (https://gosa.gonicus.de)
4 * Copyright (C) 2008 Cajus Pollmeier
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 /*! \brief Test functions
22 *
23 * This class provides various test functions. It enables to check
24 * if a given value is:
25 *
26 * - a phone numnber
27 * - a DNS name
28 * - an URL
29 * etc.
30 *
31 * The functions need to be handled with care, because they are not as strict
32 * as one might expect.
33 */
34 class tests {
36 /*! \brief Test if the given string is a phone number */
37 public static function is_phone_nr($nr)
38 {
39 if ($nr == ""){
40 return (TRUE);
41 }
43 return preg_match ("/^[\/0-9 ()+*-]+$/", $nr);
44 }
47 /*! \brief Test if the given string contains characters allowed in a DNS name */
48 public static function is_dns_name($str)
49 {
50 return(preg_match("/^[a-z0-9\.\-_]*$/i",$str));
51 }
54 /*! \brief Test if the given string is an URL */
55 public static function is_url($url)
56 {
57 if ($url == ""){
58 return (TRUE);
59 }
61 return preg_match ("/^(http|https):\/\/((?:[a-zA-Z0-9_-]+\.?)+):?(\d*)/", $url);
62 }
65 /*! \brief Test if the given string is a DN */
66 public static function is_dn($dn)
67 {
68 if ($dn == ""){
69 return (TRUE);
70 }
72 return preg_match ("/^[a-z0-9 _-]+$/i", $dn);
73 }
76 /*! \brief Test if the given string is an uid */
77 public static function is_uid($uid)
78 {
79 if ($uid == ""){
80 return (TRUE);
81 }
83 /* STRICT adds spaces and case insenstivity to the uid check.
84 This is dangerous and should not be used. */
85 if (strict_uid_mode()){
86 return preg_match ("/^[a-z0-9_-]+$/", $uid);
87 } else {
88 return preg_match ("/^[a-z0-9 _.-]+$/i", $uid);
89 }
90 }
93 /*! \brief Test if the given string is an IP */
94 public static function is_ip($ip)
95 {
96 if(function_exists('filter_var')) {
97 return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
98 } else {
99 return preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/", $ip);
100 }
101 }
104 public static function is_ipv6($ip)
105 {
106 if(function_exists('filter_var')) {
107 return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
108 } else {
109 $ipv4 = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)';
110 $g = '([0-9a-f]{1,4})'; //IPv6 group
111 return preg_match("/^$g:$g:$g:$g:$g:$g:$g:$g$/", $ip) ||
112 preg_match("/^$g:$g:$g:$g:$g:$g:$ipv4$/", $ip);
113 }
114 }
117 /*! \brief Test if the given string is a mac address */
118 public static function is_mac($mac)
119 {
120 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);
121 }
124 /*! \brief Checks if the given ip address dosen't match
125 "is_ip" because there is also a sub net mask given */
126 public static function is_ip_with_subnetmask($ip)
127 {
128 /* Generate list of valid submasks */
129 $res = array();
130 for($e = 0 ; $e <= 32; $e++){
131 $res[$e] = $e;
132 }
133 $i[0] =255;
134 $i[1] =255;
135 $i[2] =255;
136 $i[3] =255;
137 for($a= 3 ; $a >= 0 ; $a --){
138 $c = 1;
139 while($i[$a] > 0 ){
140 $str = $i[0].".".$i[1].".".$i[2].".".$i[3];
141 $res[$str] = $str;
142 $i[$a] -=$c;
143 $c = 2*$c;
144 }
145 }
146 $res["0.0.0.0"] = "0.0.0.0";
147 if(preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
148 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
149 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
150 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/", $ip)){
151 $mask = preg_replace("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
152 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
153 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
154 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/","",$ip);
156 $mask = preg_replace("/^\//","",$mask);
157 if((in_array("$mask",$res)) && preg_match("/^[0-9\.]/",$mask)){
158 return(TRUE);
159 }
160 }
161 return(FALSE);
162 }
165 /*! \brief Simple is domain check
166 *
167 * This checks if the given string looks like "string(...).string"
168 */
169 public static function is_domain($str)
170 {
171 return(preg_match("/^(([a-z0-9\-]{2,63})\.)*[a-z]{2,63}$/i",$str));
172 }
175 /*! \brief Check if the given argument is an id */
176 public static function is_id($id)
177 {
178 if ($id == ""){
179 return (FALSE);
180 }
182 return preg_match ("/^[0-9]+$/", $id);
183 }
186 /*! \brief Check if the given argument is a path */
187 public static function is_path($path)
188 {
189 if ($path == ""){
190 return (TRUE);
191 }
192 if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
193 return (FALSE);
194 }
196 return preg_match ("/\/.+$/", $path);
197 }
200 /*! \brief Check if the given argument is an email */
201 public static function is_email($address, $template= FALSE)
202 {
203 if ($address == ""){
204 return (TRUE);
205 }
206 if ($template){
207 return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
208 $address);
209 } else {
210 return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
211 $address);
212 }
213 }
216 /* \brief Check if the given department name is valid */
217 public static function is_department_name_reserved($name,$base)
218 {
219 $reservedName = array("systems","apps","incomming","internal","accounts","fax","addressbook",
220 preg_replace("/ou=(.*),/","\\1",get_people_ou()),
221 preg_replace("/ou=(.*),/","\\1",get_groups_ou()));
222 $follwedNames['/ou=fai,ou=configs,ou=systems,/'] = array("fai","hooks","templates","scripts","disk","packages","variables","profiles");
224 /* Check if name is one of the reserved names */
225 if(in_array_ics($name,$reservedName)) {
226 return(true);
227 }
229 /* Check all follow combinations if name is in array && parent base == array_key, return false*/
230 foreach($follwedNames as $key => $names){
231 if((in_array_ics($name,$names)) && (preg_match($key,$base))){
232 return(true);
233 }
234 }
235 return(false);
236 }
239 /* \brief Check if $ip1 and $ip2 represents a valid IP range
240 * \return TRUE in case of a valid range, FALSE in case of an error.
241 */
242 public static function is_ip_range($ip1,$ip2)
243 {
244 if(!tests::is_ip($ip1) || !tests::is_ip($ip2)){
245 return(FALSE);
246 }else{
247 $ar1 = explode(".",$ip1);
248 $var1 = $ar1[0] * (16777216) + $ar1[1] * (65536) + $ar1[2] * (256) + $ar1[3];
250 $ar2 = explode(".",$ip2);
251 $var2 = $ar2[0] * (16777216) + $ar2[1] * (65536) + $ar2[2] * (256) + $ar2[3];
252 return($var1 < $var2);
253 }
254 }
257 /* \brief Check if the specified IP address $address is inside the given network */
258 public static function is_in_network($network, $netmask, $address)
259 {
260 $nw= explode('.', $network);
261 $nm= explode('.', $netmask);
262 $ad= explode('.', $address);
264 /* Generate inverted netmask */
265 for ($i= 0; $i<4; $i++){
266 $ni[$i]= 255-$nm[$i];
267 $la[$i]= $nw[$i] | $ni[$i];
268 }
270 /* Transform to integer */
271 $first= $nw[0] * (16777216) + $nw[1] * (65536) + $nw[2] * (256) + $nw[3];
272 $curr= $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
273 $last= $la[0] * (16777216) + $la[1] * (65536) + $la[2] * (256) + $la[3];
275 return ($first < $curr&& $last > $curr);
276 }
278 /* Check if entry value is a valid date */
279 public static function is_date($date)
280 {
281 global $lang;
283 if ($date == ""){
284 return (TRUE);
285 }
287 #TODO: use $lang to check date format
288 if (!preg_match("/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/", $date, $matches)) {
289 return false;
290 }
292 return checkdate($matches[2],$matches[1],$matches[3]);
293 }
295 /* \brief Check if the specified IP address $address is inside the given network */
296 public static function is_in_ip_range($from, $to, $address)
297 {
298 $from = explode('.', $from);
299 $to = explode('.', $to);
300 $ad = explode('.', $address);
302 /* Transform to integer */
303 $from= $from[0] * (16777216) + $from[1] * (65536) + $from[2] * (256) + $from[3];
304 $to= $to[0] * (16777216) + $to[1] * (65536) + $to[2] * (256) + $to[3];
305 $ad= $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
307 return ($ad >= $from && $ad <= $to);
308 }
309 }
311 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
312 ?>