Code

Added branches container for old stuff
[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     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);
77   }
80   public static function is_mac($mac)
81   {
82     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);
83   }
86   /* Checks if the given ip address dosen't match 
87       "is_ip" because there is also a sub net mask given */
88   public static function is_ip_with_subnetmask($ip)
89   {
90           /* Generate list of valid submasks */
91           $res = array();
92           for($e = 0 ; $e <= 32; $e++){
93                   $res[$e] = $e;
94           }
95           $i[0] =255;
96           $i[1] =255;
97           $i[2] =255;
98           $i[3] =255;
99           for($a= 3 ; $a >= 0 ; $a --){
100                   $c = 1;
101                   while($i[$a] > 0 ){
102                           $str  = $i[0].".".$i[1].".".$i[2].".".$i[3];
103                           $res[$str] = $str;
104                           $i[$a] -=$c;
105                           $c = 2*$c;
106                   }
107           }
108           $res["0.0.0.0"] = "0.0.0.0";
109           if(preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
110                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
111                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
112                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/", $ip)){
113                   $mask = preg_replace("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
114                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
115                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
116                           "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/","",$ip);
118                   $mask = preg_replace("/^\//","",$mask);
119                   if((in_array("$mask",$res)) && preg_match("/^[0-9\.]/",$mask)){
120                           return(TRUE);
121                   }
122           }
123           return(FALSE);
124   }
127   /* Simple is domain check, it checks if the given string looks like "string(...).string" */
128   public static function is_domain($str)
129   {
130     return(preg_match("/^([a-z0-9\-]*)\.[a-z0-9\-]*$/i",$str));
131   }
134   public static function is_id($id)
135   {
136     if ($id == ""){
137       return (FALSE);
138     }
140     return preg_match ("/^[0-9]+$/", $id);
141   }
144   public static function is_path($path)
145   {
146     if ($path == ""){
147       return (TRUE);
148     }
149     if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
150       return (FALSE);
151     }
153     return preg_match ("/\/.+$/", $path);
154   }
157   public static function is_email($address, $template= FALSE)
158   {
159     if ($address == ""){
160       return (TRUE);
161     }
162     if ($template){
163       return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
164           $address);
165     } else {
166       return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
167           $address);
168     }
169   }
172   /* Check if the given department name is valid */
173   public static function is_department_name_reserved($name,$base)
174   {
175     $reservedName = array("systems","apps","incomming","internal","accounts","fax","addressbook",
176                             preg_replace("/ou=(.*),/","\\1",get_people_ou()),
177                             preg_replace("/ou=(.*),/","\\1",get_groups_ou()));
178     $follwedNames['/ou=fai,ou=configs,ou=systems,/'] = array("fai","hooks","templates","scripts","disk","packages","variables","profiles");
180     /* Check if name is one of the reserved names */
181     if(in_array_ics($name,$reservedName)) {
182       return(true);
183     }
185     /* Check all follow combinations if name is in array && parent base == array_key, return false*/
186     foreach($follwedNames as $key => $names){
187       if((in_array_ics($name,$names)) && (preg_match($key,$base))){
188         return(true);
189       }
190     }
191     return(false);
192   }
195   /* Check if $ip1 and $ip2 represents a valid IP range 
196    *  returns TRUE in case of a valid range, FALSE in case of an error. 
197    */
198   public static function is_ip_range($ip1,$ip2)
199   {
200     if(!tests::is_ip($ip1) || !tests::is_ip($ip2)){
201       return(FALSE);
202     }else{
203       $ar1 = split("\.",$ip1);
204       $var1 = $ar1[0] * (16777216) + $ar1[1] * (65536) + $ar1[2] * (256) + $ar1[3];
206       $ar2 = split("\.",$ip2);
207       $var2 = $ar2[0] * (16777216) + $ar2[1] * (65536) + $ar2[2] * (256) + $ar2[3];
208       return($var1 < $var2);
209     }
210   }
213   /* Check if the specified IP address $address is inside the given network */
214   public static function is_in_network($network, $netmask, $address)
215   {
216     $nw= split('\.', $network);
217     $nm= split('\.', $netmask);
218     $ad= split('\.', $address);
220     /* Generate inverted netmask */
221     for ($i= 0; $i<4; $i++){
222       $ni[$i]= 255-$nm[$i];
223       $la[$i]= $nw[$i] | $ni[$i];
224     }
226     /* Transform to integer */
227     $first= $nw[0] * (16777216) + $nw[1] * (65536) + $nw[2] * (256) + $nw[3];
228     $curr=  $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
229     $last=  $la[0] * (16777216) + $la[1] * (65536) + $la[2] * (256) + $la[3];
231     return ($first < $curr&& $last > $curr);
232   }
235   /* Check if the specified IP address $address is inside the given network */
236   public static function is_in_ip_range($from, $to, $address)
237   {
238     $from = split('\.', $from);
239     $to   = split('\.', $to);
240     $ad   = split('\.', $address);
242     /* Transform to integer */
243     $from= $from[0] * (16777216) + $from[1] * (65536) + $from[2] * (256) + $from[3];
244     $to=  $to[0] * (16777216) + $to[1] * (65536) + $to[2] * (256) + $to[3];
245     $ad=  $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
247     return ($ad >= $from && $ad <= $to);
248   }
251 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
252 ?>