validate.php ➔ ValidateIpOld()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 5
nop 1
dl 0
loc 10
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Functions about validate some data.
4
 * @package		fwolflib
5
 * @subpackage	func
6
 * @copyright	Copyright 2006-2010, Fwolf
7
 * @author		Fwolf <[email protected]>
8
 * @since		2006-07-09
9
 */
10
11
12
require_once(dirname(__FILE__) . '/../fwolflib.php');
13
require_once(FWOLFLIB . 'func/env.php');
14
15
16
/**
17
 * Validate an email address.
18
 *
19
 * Provide email address (raw input)
20
 * Returns true if the email address has the email
21
 * address format and the domain exists.
22
 *
23
 * @deprecated      Use Fwlib\Validator\Constraint\Email
24
 * @link http://www.linuxjournal.com/article/9585
25
 * @param	string	$email
26
 * @return	boolean
27
 */
28
function ValidateEmail($email)
29
{
30
	$is_valid = true;
31
	$at_index = strrpos($email, '@');
32
	if (is_bool($at_index) && !$at_index)
33
	{
34
		$is_valid = false;
35
	}
36
	else
37
	{
38
		$domain = substr($email, $at_index + 1);
39
		$local = substr($email, 0, $at_index);
40
		$local_len = strlen($local);
41
		$domain_len = strlen($domain);
42
		if ($local_len < 1 || $local_len > 64)
43
		{
44
			// local part length exceeded
45
			$is_valid = false;
46
		}
47
		elseif ($domain_len < 1 || $domain_len > 255)
48
		{
49
			// domain part length exceeded
50
			$is_valid = false;
51
		}
52
		elseif ($local[0] == '.' || $local[$local_len-1] == '.')
53
		{
54
			// local part starts or ends with '.'
55
			$is_valid = false;
56
		}
57
		elseif (preg_match('/\\.\\./', $local))
58
		{
59
			// local part has two consecutive dots
60
			$is_valid = false;
61
		}
62
		elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
63
		{
64
			// character not valid in domain part
65
			$is_valid = false;
66
		}
67
		elseif (preg_match('/\\.\\./', $domain))
68
		{
69
			// domain part has two consecutive dots
70
			$is_valid = false;
71
		}
72 View Code Duplication
		elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
			str_replace("\\\\", "", $local)))
74
		{
75
			// character not valid in local part unless
76
			// local part is quoted
77
			if (!preg_match('/^"(\\\\"|[^"])+"$/',
78
				str_replace("\\\\", "", $local)))
79
			{
80
				$is_valid = false;
81
			}
82
		}
83
84
		// :NOTICE: Some network provider will return fake A record if
85
		// a dns query return fail, usually disp some ads.
86
		// So we only check MX record.
87
		if ($is_valid && NixOs())
0 ignored issues
show
Deprecated Code introduced by
The function NixOs() has been deprecated with message: Use Fwlib\Util\Env::isNixOs()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
88
			if (false == checkdnsrr($domain, "MX"))
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
89
				$is_valid = false;
90
	}
91
	return $is_valid;
92
}
93
94
95
/**
96
 * If a string is valid ip address.
97
 *
98
 * @deprecated      Use Fwlib\Util\Validator::ipv4()
99
 * @param	string	$ip
100
 * @return	boolean
101
 */
102 View Code Duplication
function ValidateIp($ip)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
{
104
	 if (!strcmp(long2ip(sprintf("%u", ip2long($ip))), $ip))
105
		return true;
106
	 else
107
		return false;
108
} // end of function ValidateIp
109
110
111
/**
112
 * 老版本的检查ip函数, obsolete, leave here as reference.
113
 * @param	$str	string
114
 * @return	boolean
115
 */
116
function ValidateIpOld($str)
117
{
118
	 $ip = explode(".", $str);
119
	 if (count($ip)<4 || count($ip)>4) return false;
120
	 foreach($ip as $ip_addr) {
121
		  if ( !is_numeric($ip_addr) ) return false;
122
		  if ( $ip_addr<0 || $ip_addr>255 ) return false;
123
	 }
124
	 return true;
125
} // end of function ValidateIpOld
126
127
//如果简单的判断格式a.b.c.d而不考虑abcd的值的话:
128
//return (preg_match("/^([0-9]{1,3}\.){3}[0-9]{1,3}$/is", $str));
129
//不过如果需要真的ip的时候就不好玩了
130
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
131