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 global $config;
62 if ($uid == ""){
63 return (TRUE);
64 }
66 /* STRICT adds spaces and case insenstivity to the uid check.
67 This is dangerous and should not be used. */
68 if (strict_uid_mode()){
69 return preg_match ("/^[a-z0-9_-]+$/", $uid);
70 } else {
71 return preg_match ("/^[a-z0-9 _.-]+$/i", $uid);
72 }
73 }
76 public static function is_ip($ip)
77 {
78 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);
79 }
82 public static function is_mac($mac)
83 {
84 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);
85 }
88 /* Checks if the given ip address dosen't match
89 "is_ip" because there is also a sub net mask given */
90 public static function is_ip_with_subnetmask($ip)
91 {
92 /* Generate list of valid submasks */
93 $res = array();
94 for($e = 0 ; $e <= 32; $e++){
95 $res[$e] = $e;
96 }
97 $i[0] =255;
98 $i[1] =255;
99 $i[2] =255;
100 $i[3] =255;
101 for($a= 3 ; $a >= 0 ; $a --){
102 $c = 1;
103 while($i[$a] > 0 ){
104 $str = $i[0].".".$i[1].".".$i[2].".".$i[3];
105 $res[$str] = $str;
106 $i[$a] -=$c;
107 $c = 2*$c;
108 }
109 }
110 $res["0.0.0.0"] = "0.0.0.0";
111 if(preg_match("/^(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]?)\.".
113 "(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]?)/", $ip)){
115 $mask = preg_replace("/^(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]?)\.".
117 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
118 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/","",$ip);
120 $mask = preg_replace("/^\//","",$mask);
121 if((in_array("$mask",$res)) && preg_match("/^[0-9\.]/",$mask)){
122 return(TRUE);
123 }
124 }
125 return(FALSE);
126 }
129 /* Simple is domain check, it checks if the given string looks like "string(...).string" */
130 public static function is_domain($str)
131 {
132 return(preg_match("/^([a-z0-9i\-]*)\.[a-z0-9]*$/i",$str));
133 }
136 public static function is_id($id)
137 {
138 if ($id == ""){
139 return (FALSE);
140 }
142 return preg_match ("/^[0-9]+$/", $id);
143 }
146 public static function is_path($path)
147 {
148 if ($path == ""){
149 return (TRUE);
150 }
151 if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
152 return (FALSE);
153 }
155 return preg_match ("/\/.+$/", $path);
156 }
159 public static function is_email($address, $template= FALSE)
160 {
161 if ($address == ""){
162 return (TRUE);
163 }
164 if ($template){
165 return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
166 $address);
167 } else {
168 return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
169 $address);
170 }
171 }
174 /* Check if the given department name is valid */
175 public static function is_department_name_reserved($name,$base)
176 {
177 $reservedName = array("systems","apps","incomming","internal","accounts","fax","addressbook",
178 preg_replace("/ou=(.*),/","\\1",get_people_ou()),
179 preg_replace("/ou=(.*),/","\\1",get_groups_ou()));
180 $follwedNames['/ou=fai,ou=configs,ou=systems,/'] = array("fai","hooks","templates","scripts","disk","packages","variables","profiles");
182 /* Check if name is one of the reserved names */
183 if(in_array_ics($name,$reservedName)) {
184 return(true);
185 }
187 /* Check all follow combinations if name is in array && parent base == array_key, return false*/
188 foreach($follwedNames as $key => $names){
189 if((in_array_ics($name,$names)) && (preg_match($key,$base))){
190 return(true);
191 }
192 }
193 return(false);
194 }
197 /* Check if $ip1 and $ip2 represents a valid IP range
198 * returns TRUE in case of a valid range, FALSE in case of an error.
199 */
200 public static function is_ip_range($ip1,$ip2)
201 {
202 if(!is_ip($ip1) || !is_ip($ip2)){
203 return(FALSE);
204 }else{
205 $ar1 = split("\.",$ip1);
206 $var1 = $ar1[0] * (16777216) + $ar1[1] * (65536) + $ar1[2] * (256) + $ar1[3];
208 $ar2 = split("\.",$ip2);
209 $var2 = $ar2[0] * (16777216) + $ar2[1] * (65536) + $ar2[2] * (256) + $ar2[3];
210 return($var1 < $var2);
211 }
212 }
215 /* Check if the specified IP address $address is inside the given network */
216 public static function is_in_network($network, $netmask, $address)
217 {
218 $nw= split('\.', $network);
219 $nm= split('\.', $netmask);
220 $ad= split('\.', $address);
222 /* Generate inverted netmask */
223 for ($i= 0; $i<4; $i++){
224 $ni[$i]= 255-$nm[$i];
225 $la[$i]= $nw[$i] | $ni[$i];
226 }
228 /* Transform to integer */
229 $first= $nw[0] * (16777216) + $nw[1] * (65536) + $nw[2] * (256) + $nw[3];
230 $curr= $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
231 $last= $la[0] * (16777216) + $la[1] * (65536) + $la[2] * (256) + $la[3];
233 return ($first < $curr&& $last > $curr);
234 }
235 }
237 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
238 ?>