a34eac4ca8eb0a04547031e4dd4ce2d4cdaa078d
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 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);
97 }
100 /*! \brief Test if the given string is a mac address */
101 public static function is_mac($mac)
102 {
103 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);
104 }
107 /*! \brief Checks if the given ip address dosen't match
108 "is_ip" because there is also a sub net mask given */
109 public static function is_ip_with_subnetmask($ip)
110 {
111 /* Generate list of valid submasks */
112 $res = array();
113 for($e = 0 ; $e <= 32; $e++){
114 $res[$e] = $e;
115 }
116 $i[0] =255;
117 $i[1] =255;
118 $i[2] =255;
119 $i[3] =255;
120 for($a= 3 ; $a >= 0 ; $a --){
121 $c = 1;
122 while($i[$a] > 0 ){
123 $str = $i[0].".".$i[1].".".$i[2].".".$i[3];
124 $res[$str] = $str;
125 $i[$a] -=$c;
126 $c = 2*$c;
127 }
128 }
129 $res["0.0.0.0"] = "0.0.0.0";
130 if(preg_match("/^(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)){
134 $mask = preg_replace("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
135 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
136 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.".
137 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/","",$ip);
139 $mask = preg_replace("/^\//","",$mask);
140 if((in_array("$mask",$res)) && preg_match("/^[0-9\.]/",$mask)){
141 return(TRUE);
142 }
143 }
144 return(FALSE);
145 }
148 /*! \brief Simple is domain check
149 *
150 * This checks if the given string looks like "string(...).string"
151 */
152 public static function is_domain($str)
153 {
154 return(preg_match("/^([a-z0-9\-]*)\.[a-z0-9\-]*$/i",$str));
155 }
158 /*! \brief Check if the given argument is an id */
159 public static function is_id($id)
160 {
161 if ($id == ""){
162 return (FALSE);
163 }
165 return preg_match ("/^[0-9]+$/", $id);
166 }
169 /*! \brief Check if the given argument is a path */
170 public static function is_path($path)
171 {
172 if ($path == ""){
173 return (TRUE);
174 }
175 if (!preg_match('/^[a-z0-9%\/_.+-]+$/i', $path)){
176 return (FALSE);
177 }
179 return preg_match ("/\/.+$/", $path);
180 }
183 /*! \brief Check if the given argument is an email */
184 public static function is_email($address, $template= FALSE)
185 {
186 if ($address == ""){
187 return (TRUE);
188 }
189 if ($template){
190 return preg_match ("/^[._a-z0-9%-]+@[_a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z0-9-]+)*$/i",
191 $address);
192 } else {
193 return preg_match ("/^[._a-z0-9-]+@[_a-z0-9-]+(\.[a-z0-9i-]+)(\.[a-z0-9-]+)*$/i",
194 $address);
195 }
196 }
199 /* \brief Check if the given department name is valid */
200 public static function is_department_name_reserved($name,$base)
201 {
202 $reservedName = array("systems","apps","incomming","internal","accounts","fax","addressbook",
203 preg_replace("/ou=(.*),/","\\1",get_people_ou()),
204 preg_replace("/ou=(.*),/","\\1",get_groups_ou()));
205 $follwedNames['/ou=fai,ou=configs,ou=systems,/'] = array("fai","hooks","templates","scripts","disk","packages","variables","profiles");
207 /* Check if name is one of the reserved names */
208 if(in_array_ics($name,$reservedName)) {
209 return(true);
210 }
212 /* Check all follow combinations if name is in array && parent base == array_key, return false*/
213 foreach($follwedNames as $key => $names){
214 if((in_array_ics($name,$names)) && (preg_match($key,$base))){
215 return(true);
216 }
217 }
218 return(false);
219 }
222 /* \brief Check if $ip1 and $ip2 represents a valid IP range
223 * \return TRUE in case of a valid range, FALSE in case of an error.
224 */
225 public static function is_ip_range($ip1,$ip2)
226 {
227 if(!tests::is_ip($ip1) || !tests::is_ip($ip2)){
228 return(FALSE);
229 }else{
230 $ar1 = split("\.",$ip1);
231 $var1 = $ar1[0] * (16777216) + $ar1[1] * (65536) + $ar1[2] * (256) + $ar1[3];
233 $ar2 = split("\.",$ip2);
234 $var2 = $ar2[0] * (16777216) + $ar2[1] * (65536) + $ar2[2] * (256) + $ar2[3];
235 return($var1 < $var2);
236 }
237 }
240 /* \brief Check if the specified IP address $address is inside the given network */
241 public static function is_in_network($network, $netmask, $address)
242 {
243 $nw= split('\.', $network);
244 $nm= split('\.', $netmask);
245 $ad= split('\.', $address);
247 /* Generate inverted netmask */
248 for ($i= 0; $i<4; $i++){
249 $ni[$i]= 255-$nm[$i];
250 $la[$i]= $nw[$i] | $ni[$i];
251 }
253 /* Transform to integer */
254 $first= $nw[0] * (16777216) + $nw[1] * (65536) + $nw[2] * (256) + $nw[3];
255 $curr= $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
256 $last= $la[0] * (16777216) + $la[1] * (65536) + $la[2] * (256) + $la[3];
258 return ($first < $curr&& $last > $curr);
259 }
262 /* \brief Check if the specified IP address $address is inside the given network */
263 public static function is_in_ip_range($from, $to, $address)
264 {
265 $from = split('\.', $from);
266 $to = split('\.', $to);
267 $ad = split('\.', $address);
269 /* Transform to integer */
270 $from= $from[0] * (16777216) + $from[1] * (65536) + $from[2] * (256) + $from[3];
271 $to= $to[0] * (16777216) + $to[1] * (65536) + $to[2] * (256) + $to[3];
272 $ad= $ad[0] * (16777216) + $ad[1] * (65536) + $ad[2] * (256) + $ad[3];
274 return ($ad >= $from && $ad <= $to);
275 }
277 /* Check if given timestamp is in gosa-si time-format */
278 public static function is_gosa_si_time($time){
279 return preg_match ("/^20\d\d[0-1]\d[0-3]\d[0-2]\d[0-5]\d[0-5]\d$/i", $time);
280 }
281 }
283 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
284 ?>