Code

Made is_date be more correct
[gosa.git] / gosa-core / include / utils / class_tests.inc
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 class tests {
22   public static function is_phone_nr($nr)
23   {
24     if ($nr == ""){
25       return (TRUE);
26     }
28     return preg_match ("/^[\/0-9 ()+*-]+$/", $nr);
29   }
32   public static function is_dns_name($str)
33   {
34     return(preg_match("/^[a-z0-9\.\-_]*$/i",$str));
35   }
38   public static function is_url($url)
39   {
40     if ($url == ""){
41       return (TRUE);
42     }
44     return preg_match ("/^(http|https):\/\/((?:[a-zA-Z0-9_-]+\.?)+):?(\d*)/", $url);
45   }
48   public static function is_dn($dn)
49   {
50     if ($dn == ""){
51       return (TRUE);
52     }
54     return preg_match ("/^[a-z0-9 _-]+$/i", $dn);
55   }
58   public static function is_uid($uid)
59   {
60     if ($uid == ""){
61       return (TRUE);
62     }
64     /* STRICT adds spaces and case insenstivity to the uid check.
65        This is dangerous and should not be used. */
66     if (strict_uid_mode()){
67       return preg_match ("/^[a-z0-9_-]+$/", $uid);
68     } else {
69       return preg_match ("/^[a-z0-9 _.-]+$/i", $uid);
70     }
71   }
74   public static function is_ip($ip)
75   {
76     if(function_exists('filter_var')) {
77       return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
78     } else {
79       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);
80     }
81   }
84   public static function is_ipv6($ip)
85   {
86     if(function_exists('filter_var')) {
87         return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
88     } else {
89         $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]?)';
90         $g = '([0-9a-f]{1,4})'; //IPv6 group
91         return preg_match("/^$g:$g:$g:$g:$g:$g:$g:$g$/", $ip) ||
92                preg_match("/^$g:$g:$g:$g:$g:$g:$ipv4$/", $ip);
93     }
94   }
97   public static function is_mac($mac)
98   {
99     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);
100   }
103   /* Checks if the given ip address dosen't match 
104       "is_ip" because there is also a sub net mask given */
105   public static function is_ip_with_subnetmask($ip)
106   {
107           /* Generate list of valid submasks */
108           $res = array();
109           for($e = 0 ; $e <= 32; $e++){
110                   $res[$e] = $e;
111           }
112           $i[0] =255;
113           $i[1] =255;
114           $i[2] =255;
115           $i[3] =255;
116           for($a= 3 ; $a >= 0 ; $a --){
117                   $c = 1;
118                   while($i[$a] > 0 ){
119                           $str  = $i[0].".".$i[1].".".$i[2].".".$i[3];
120                           $res[$str] = $str;
121                           $i[$a] -=$c;
122                           $c = 2*$c;
123                   }
124           }
125           $res["0.0.0.0"] = "0.0.0.0";
126           if(preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
127                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
128                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
129                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/", $ip)){
130                   $mask = preg_replace("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
131                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
132                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
133                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/","",$ip);
135                   $mask = preg_replace("/^\//","",$mask);
136                   if((in_array("$mask",$res)) && preg_match("/^[0-9\.]/",$mask)){
137                           return(TRUE);
138                   }
139           }
140           return(FALSE);
141   }
144   /* Simple is domain check, it checks if the given string looks like "string(...).string" */
145   public static function is_domain($str)
146   {
147     return(preg_match("/^(([a-z0-9\-]{2,63})\.)*[a-z]{2,63}$/i",$str));
148   }
151   public static function is_id($id)
152   {
153     if ($id == ""){
154       return (FALSE);
155     }
157     return preg_match ("/^[0-9]+$/", $id);
158   }
161   public static function is_path($path)
162   {
163     if ($path == ""){
164       return (TRUE);
165     }
166     if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
167       return (FALSE);
168     }
170     return preg_match ("/\/.+$/", $path);
171   }
174   public static function is_email($address, $template= FALSE)
175   {
176     if ($address == ""){
177       return (TRUE);
178     }
179     if ($template){
180       return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
181           $address);
182     } else {
183       return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
184           $address);
185     }
186   }
189   /* Check if the given department name is valid */
190   public static function is_department_name_reserved($name,$base)
191   {
192     $reservedName = array("systems","apps","incomming","internal","accounts","fax","addressbook",
193                             preg_replace("/ou=(.*),/","\\1",get_people_ou()),
194                             preg_replace("/ou=(.*),/","\\1",get_groups_ou()));
195     $follwedNames['/ou=fai,ou=configs,ou=systems,/'] = array("fai","hooks","templates","scripts","disk","packages","variables","profiles");
197     /* Check if name is one of the reserved names */
198     if(in_array_ics($name,$reservedName)) {
199       return(true);
200     }
202     /* Check all follow combinations if name is in array && parent base == array_key, return false*/
203     foreach($follwedNames as $key => $names){
204       if((in_array_ics($name,$names)) && (preg_match($key,$base))){
205         return(true);
206       }
207     }
208     return(false);
209   }
212   /* Check if $ip1 and $ip2 represents a valid IP range 
213    *  returns TRUE in case of a valid range, FALSE in case of an error. 
214    */
215   public static function is_ip_range($ip1,$ip2)
216   {
217     if(!tests::is_ip($ip1) || !tests::is_ip($ip2)){
218       return(FALSE);
219     }else{
220       $ar1 = split("\.",$ip1);
221       $var1 = $ar1[0] * (16777216) + $ar1[1] * (65536) + $ar1[2] * (256) + $ar1[3];
223       $ar2 = split("\.",$ip2);
224       $var2 = $ar2[0] * (16777216) + $ar2[1] * (65536) + $ar2[2] * (256) + $ar2[3];
225       return($var1 < $var2);
226     }
227   }
230   /* Check if the specified IP address $address is inside the given network */
231   public static function is_in_network($network, $netmask, $address)
232   {
233     $nw= split('\.', $network);
234     $nm= split('\.', $netmask);
235     $ad= split('\.', $address);
237     /* Generate inverted netmask */
238     for ($i= 0; $i<4; $i++){
239       $ni[$i]= 255-$nm[$i];
240       $la[$i]= $nw[$i] | $ni[$i];
241     }
243     /* Transform to integer */
244     $first= $nw[0] * (16777216) + $nw[1] * (65536) + $nw[2] * (256) + $nw[3];
245     $curr=  $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
246     $last=  $la[0] * (16777216) + $la[1] * (65536) + $la[2] * (256) + $la[3];
248     return ($first < $curr&& $last > $curr);
249   }
251   /* Check if entry value is a valid date */
252   public static function is_date($date)
253   {
254     global $lang;
256     if ($date == ""){
257       return (TRUE);
258     }
260     #TODO: use $lang to check date format
261     if (!preg_match("/([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})/", $date, $matches)) {
262       return false;
263     }
265     return checkdate($matches[1],$matches[2],$matches[3]);
266   }
268   /* Check if the specified IP address $address is inside the given network */
269   public static function is_in_ip_range($from, $to, $address)
270   {
271     $from = split('\.', $from);
272     $to   = split('\.', $to);
273     $ad   = split('\.', $address);
275     /* Transform to integer */
276     $from= $from[0] * (16777216) + $from[1] * (65536) + $from[2] * (256) + $from[3];
277     $to=  $to[0] * (16777216) + $to[1] * (65536) + $to[2] * (256) + $to[3];
278     $ad=  $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
280     return ($ad >= $from && $ad <= $to);
281   }
284 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
285 ?>